Line data Source code
1 : // EnergyPlus, Copyright (c) 1996-2024, The Board of Trustees of the University of Illinois,
2 : // The Regents of the University of California, through Lawrence Berkeley National Laboratory
3 : // (subject to receipt of any required approvals from the U.S. Dept. of Energy), Oak Ridge
4 : // National Laboratory, managed by UT-Battelle, Alliance for Sustainable Energy, LLC, and other
5 : // contributors. All rights reserved.
6 : //
7 : // NOTICE: This Software was developed under funding from the U.S. Department of Energy and the
8 : // U.S. Government consequently retains certain rights. As such, the U.S. Government has been
9 : // granted for itself and others acting on its behalf a paid-up, nonexclusive, irrevocable,
10 : // worldwide license in the Software to reproduce, distribute copies to the public, prepare
11 : // derivative works, and perform publicly and display publicly, and to permit others to do so.
12 : //
13 : // Redistribution and use in source and binary forms, with or without modification, are permitted
14 : // provided that the following conditions are met:
15 : //
16 : // (1) Redistributions of source code must retain the above copyright notice, this list of
17 : // conditions and the following disclaimer.
18 : //
19 : // (2) Redistributions in binary form must reproduce the above copyright notice, this list of
20 : // conditions and the following disclaimer in the documentation and/or other materials
21 : // provided with the distribution.
22 : //
23 : // (3) Neither the name of the University of California, Lawrence Berkeley National Laboratory,
24 : // the University of Illinois, U.S. Dept. of Energy nor the names of its contributors may be
25 : // used to endorse or promote products derived from this software without specific prior
26 : // written permission.
27 : //
28 : // (4) Use of EnergyPlus(TM) Name. If Licensee (i) distributes the software in stand-alone form
29 : // without changes from the version obtained under this License, or (ii) Licensee makes a
30 : // reference solely to the software portion of its product, Licensee must refer to the
31 : // software as "EnergyPlus version X" software, where "X" is the version number Licensee
32 : // obtained under this License and may not use a different name for the software. Except as
33 : // specifically required in this Section (4), Licensee shall not use in a company name, a
34 : // product name, in advertising, publicity, or other promotional activities any name, trade
35 : // name, trademark, logo, or other designation of "EnergyPlus", "E+", "e+" or confusingly
36 : // similar designation, without the U.S. Department of Energy's prior written consent.
37 : //
38 : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
39 : // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
40 : // AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
41 : // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 : // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
43 : // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
44 : // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
45 : // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46 : // POSSIBILITY OF SUCH DAMAGE.
47 :
48 : #ifndef RuntimeLanguageProcessor_hh_INCLUDED
49 : #define RuntimeLanguageProcessor_hh_INCLUDED
50 :
51 : // ObjexxFCL Headers
52 : #include <ObjexxFCL/Array1D.hh>
53 : #include <ObjexxFCL/Optional.hh>
54 :
55 : // EnergyPlus Headers
56 : #include <EnergyPlus/Data/BaseData.hh>
57 : #include <EnergyPlus/DataGlobals.hh>
58 : #include <EnergyPlus/DataRuntimeLanguage.hh>
59 : #include <EnergyPlus/EnergyPlus.hh>
60 :
61 : namespace EnergyPlus {
62 :
63 : // Forward declarations
64 : struct EnergyPlusData;
65 :
66 : namespace RuntimeLanguageProcessor {
67 :
68 : // Using/Aliasing
69 : using DataRuntimeLanguage::ErlFunc;
70 : using DataRuntimeLanguage::ErlValueType;
71 :
72 : int constexpr MaxErrors(20);
73 :
74 : enum class Token
75 : {
76 : Invalid = -1,
77 : Number = 1, // matches the ValueNumber
78 : Variable = 4, // matches the ValueVariable
79 : Expression = 5, // matches the ValueExpression
80 : Operator = 7, // includes basic operators and built-in functions.
81 : Parenthesis = 9, // parenthesis token
82 : ParenthesisLeft = 10, // indicates left side parenthesis found in parsing
83 : ParenthesisRight = 11, // indicates right side parenthesis found in parsing
84 : Num
85 : };
86 :
87 : struct TokenType
88 : {
89 : // Members
90 : // structure for token information for parsing Erl code
91 : Token Type; // token type, eg. TokenNumber
92 : Real64 Number; // May want to store all literals as a variable?
93 : std::string String; // Serves double duty, also saves string version of token for easy debugging
94 : ErlFunc Operator; // indentifies operator or function 1..64
95 : int Variable; // points to a variable in ErlVariable structure
96 : Token Parenthesis; // identifes if token is left or right parenthesis
97 : int Expression; // points to an expression in ErlExpression structure
98 : std::string Error; // holds token processing error message content
99 :
100 : // Default Constructor
101 34997 : TokenType() : Type(Token::Invalid), Number(0.0), Operator(ErlFunc::Invalid), Variable(0), Parenthesis(Token::Invalid), Expression(0)
102 : {
103 34997 : }
104 : };
105 :
106 : struct RuntimeReportVarType
107 : {
108 : // Members
109 : std::string Name; // name of custom Erl report variable
110 : int VariableNum; // pointer to Erl variable associated with custom report variable
111 : Real64 Value; // Value registered with output processor for report variable
112 :
113 : // Default Constructor
114 38 : RuntimeReportVarType() : VariableNum(0), Value(0.0)
115 : {
116 38 : }
117 : };
118 :
119 : void InitializeRuntimeLanguage(EnergyPlusData &state);
120 :
121 : void BeginEnvrnInitializeRuntimeLanguage(EnergyPlusData &state);
122 :
123 : void ParseStack(EnergyPlusData &state, int StackNum);
124 :
125 : int AddInstruction(EnergyPlusData &state,
126 : int StackNum,
127 : int LineNum,
128 : DataRuntimeLanguage::ErlKeywordParam Keyword,
129 : ObjexxFCL::Optional_int_const Argument1 = _, // Erl variable index
130 : ObjexxFCL::Optional_int_const Argument2 = _);
131 :
132 : void AddError(EnergyPlusData &state,
133 : int StackNum, // index pointer to location in ErlStack structure
134 : int LineNum, // Erl program line number
135 : std::string const &Error // error message to be added to ErlStack
136 : );
137 :
138 : ErlValueType EvaluateStack(EnergyPlusData &state, int StackNum);
139 :
140 : void WriteTrace(EnergyPlusData &state, int StackNum, int InstructionNum, ErlValueType const &ReturnValue, bool seriousErrorFound);
141 :
142 : void ParseExpression(EnergyPlusData &state,
143 : std::string const &InString, // String of expression text written in the Runtime Language
144 : int StackNum, // Parent StackNum??
145 : int &ExpressionNum, // index of expression in structure
146 : std::string const &Line // Actual line from string
147 : );
148 :
149 : int ProcessTokens(EnergyPlusData &state, const Array1D<TokenType> &TokenIN, int NumTokensIN, int StackNum, std::string const &ParsingString);
150 :
151 : int NewExpression(EnergyPlusData &state);
152 :
153 : ErlValueType EvaluateExpression(EnergyPlusData &state, int ExpressionNum, bool &seriousErrorFound);
154 :
155 : void GetRuntimeLanguageUserInput(EnergyPlusData &state);
156 :
157 : void ReportRuntimeLanguage(EnergyPlusData &state);
158 :
159 : ErlValueType SetErlValueNumber(Real64 Number, ObjexxFCL::Optional<ErlValueType const> OrigValue = _);
160 :
161 : ErlValueType StringValue(std::string const &String);
162 :
163 : std::string ValueToString(ErlValueType const &Value);
164 :
165 : int FindEMSVariable(EnergyPlusData &state,
166 : std::string const &VariableName, // variable name in Erl
167 : int StackNum);
168 :
169 : int NewEMSVariable(EnergyPlusData &state, std::string const &VariableName, int StackNum, ObjexxFCL::Optional<ErlValueType const> Value = _);
170 :
171 : void ExternalInterfaceSetErlVariable(EnergyPlusData &state,
172 : int varNum, // The variable index to be written during run time
173 : Real64 value // The real time value of the vairable to be set
174 : );
175 :
176 : void ExternalInterfaceInitializeErlVariable(EnergyPlusData &state,
177 : int varNum, // The variable index to be written during run time
178 : ErlValueType const &initialValue, // The initial value
179 : bool setToNull // Flag, if true, value will be initialized to Null
180 : );
181 :
182 : bool isExternalInterfaceErlVariable(EnergyPlusData &state, int varNum); // The variable index to be written during run time
183 :
184 : } // namespace RuntimeLanguageProcessor
185 :
186 : struct RuntimeLanguageProcessorData : BaseGlobalStruct
187 : {
188 : bool AlreadyDidOnce = false;
189 : bool GetInput = true;
190 : bool InitializeOnce = true;
191 : bool MyEnvrnFlag = true;
192 : int NullVariableNum = 0;
193 : int FalseVariableNum = 0;
194 : int TrueVariableNum = 0;
195 : int OffVariableNum = 0;
196 : int OnVariableNum = 0;
197 : int PiVariableNum = 0;
198 : Array1D_int CurveIndexVariableNums;
199 : Array1D_int ConstructionIndexVariableNums;
200 : int YearVariableNum = 0;
201 : int CalendarYearVariableNum = 0;
202 : int MonthVariableNum = 0;
203 : int DayOfMonthVariableNum = 0;
204 : int DayOfWeekVariableNum = 0;
205 : int DayOfYearVariableNum = 0;
206 : int HourVariableNum = 0;
207 : int TimeStepsPerHourVariableNum = 0;
208 : int TimeStepNumVariableNum = 0;
209 : int MinuteVariableNum = 0;
210 : int HolidayVariableNum = 0;
211 : int DSTVariableNum = 0;
212 : int CurrentTimeVariableNum = 0;
213 : int SunIsUpVariableNum = 0;
214 : int IsRainingVariableNum = 0;
215 : int SystemTimeStepVariableNum = 0;
216 : int ZoneTimeStepVariableNum = 0;
217 : int CurrentEnvironmentPeriodNum = 0;
218 : int ActualDateAndTimeNum = 0;
219 : int ActualTimeNum = 0;
220 : int WarmUpFlagNum = 0;
221 : Array1D<RuntimeLanguageProcessor::RuntimeReportVarType> RuntimeReportVar;
222 : std::unordered_map<std::string, std::string> ErlStackUniqueNames;
223 : std::unordered_map<std::string, std::string> RuntimeReportVarUniqueNames;
224 : bool WriteTraceMyOneTimeFlag = false;
225 : Array1D<RuntimeLanguageProcessor::TokenType> Token;
226 : Array1D<RuntimeLanguageProcessor::TokenType> PEToken;
227 :
228 796 : void init_state([[maybe_unused]] EnergyPlusData &state) override
229 : {
230 796 : }
231 :
232 0 : void clear_state() override
233 : {
234 0 : this->AlreadyDidOnce = false;
235 0 : this->GetInput = true;
236 0 : this->InitializeOnce = true;
237 0 : this->MyEnvrnFlag = true;
238 0 : this->NullVariableNum = 0;
239 0 : this->FalseVariableNum = 0;
240 0 : this->TrueVariableNum = 0;
241 0 : this->OffVariableNum = 0;
242 0 : this->OnVariableNum = 0;
243 0 : this->PiVariableNum = 0;
244 0 : this->CurveIndexVariableNums.clear();
245 0 : this->ConstructionIndexVariableNums.clear();
246 0 : this->YearVariableNum = 0;
247 0 : this->CalendarYearVariableNum = 0;
248 0 : this->MonthVariableNum = 0;
249 0 : this->DayOfMonthVariableNum = 0;
250 0 : this->DayOfWeekVariableNum = 0;
251 0 : this->DayOfYearVariableNum = 0;
252 0 : this->HourVariableNum = 0;
253 0 : this->TimeStepsPerHourVariableNum = 0;
254 0 : this->TimeStepNumVariableNum = 0;
255 0 : this->MinuteVariableNum = 0;
256 0 : this->HolidayVariableNum = 0;
257 0 : this->DSTVariableNum = 0;
258 0 : this->CurrentTimeVariableNum = 0;
259 0 : this->SunIsUpVariableNum = 0;
260 0 : this->IsRainingVariableNum = 0;
261 0 : this->SystemTimeStepVariableNum = 0;
262 0 : this->ZoneTimeStepVariableNum = 0;
263 0 : this->CurrentEnvironmentPeriodNum = 0;
264 0 : this->ActualDateAndTimeNum = 0;
265 0 : this->ActualTimeNum = 0;
266 0 : this->WarmUpFlagNum = 0;
267 0 : this->RuntimeReportVar.clear();
268 0 : this->ErlStackUniqueNames.clear();
269 0 : this->RuntimeReportVarUniqueNames.clear();
270 0 : this->WriteTraceMyOneTimeFlag = false;
271 0 : this->PEToken.clear();
272 0 : this->Token.clear();
273 0 : }
274 : };
275 :
276 : } // namespace EnergyPlus
277 :
278 : #endif
|