diff --git a/src/EnergyPlus/DataRuntimeLanguage.hh b/src/EnergyPlus/DataRuntimeLanguage.hh index 42410612235..c6f9ad55272 100644 --- a/src/EnergyPlus/DataRuntimeLanguage.hh +++ b/src/EnergyPlus/DataRuntimeLanguage.hh @@ -394,9 +394,11 @@ namespace DataRuntimeLanguage { ErlValueType Value; // values taken by Erl variables bool ReadOnly; // true if Erl variable is read-only bool SetByExternalInterface; // set to true if value is set by ExternalInterface + bool SetByGlobalVariable; + bool SetByInternalVariable; // Default Constructor - ErlVariableType() : StackNum(0), ReadOnly(false), SetByExternalInterface(false) + ErlVariableType() : StackNum(0), ReadOnly(false), SetByExternalInterface(false), SetByGlobalVariable(false), SetByInternalVariable(false) { } }; diff --git a/src/EnergyPlus/EMSManager.cc b/src/EnergyPlus/EMSManager.cc index 9abcd0a99c6..13975a73c00 100644 --- a/src/EnergyPlus/EMSManager.cc +++ b/src/EnergyPlus/EMSManager.cc @@ -809,6 +809,7 @@ namespace EMSManager { } else { VariableNum = RuntimeLanguageProcessor::NewEMSVariable(state, cAlphaArgs(1), 0); state.dataRuntimeLang->EMSInternalVarsUsed(InternVarNum).ErlVariableNum = VariableNum; + state.dataRuntimeLang->ErlVariable(VariableNum).SetByInternalVariable = true; } state.dataRuntimeLang->EMSInternalVarsUsed(InternVarNum).UniqueIDName = cAlphaArgs(2); diff --git a/src/EnergyPlus/RuntimeLanguageProcessor.cc b/src/EnergyPlus/RuntimeLanguageProcessor.cc index b2a0a2d29ed..95ca0ddc393 100644 --- a/src/EnergyPlus/RuntimeLanguageProcessor.cc +++ b/src/EnergyPlus/RuntimeLanguageProcessor.cc @@ -816,7 +816,6 @@ ErlValueType EvaluateStack(EnergyPlusData &state, int const StackNum) ReturnValue.Number = 0.0; auto const &thisErlStack = state.dataRuntimeLang->ErlStack(StackNum); - InstructionNum = 1; while (InstructionNum <= thisErlStack.NumInstructions) { @@ -1801,7 +1800,9 @@ ErlValueType EvaluateExpression(EnergyPlusData &state, int const ExpressionNum, } else { // value has never been set ReturnValue.Type = Value::Error; ReturnValue.Error = "EvaluateExpression: Variable = '" + thisErlVar.Name + "' used in expression has not been initialized!"; - if (!state.dataGlobal->DoingSizing && !state.dataGlobal->KickOffSimulation && !state.dataEMSMgr->FinishProcessingUserInput) { + + if ((!state.dataGlobal->DoingSizing && !state.dataGlobal->KickOffSimulation && !state.dataEMSMgr->FinishProcessingUserInput) || + (!(thisErlVar.SetByGlobalVariable || thisErlVar.SetByInternalVariable))) { // check if this is an arg in CurveValue, if (thisErlExpression.Operator != @@ -2972,6 +2973,10 @@ void GetRuntimeLanguageUserInput(EnergyPlusData &state) // Initialize variables for the ExternalInterface variables. // This object requires an initial value. ExternalInterfaceInitializeErlVariable(state, VariableNum, SetErlValueNumber(rNumericArgs(1)), false); + } else { + DisplayString(state, cCurrentModuleObject); + DisplayString(state, cAlphaArgs(ErlVarLoop)); + state.dataRuntimeLang->ErlVariable(VariableNum).SetByGlobalVariable = true; } } } diff --git a/src/EnergyPlus/SimulationManager.cc b/src/EnergyPlus/SimulationManager.cc index 50927c01252..b4103f19a86 100644 --- a/src/EnergyPlus/SimulationManager.cc +++ b/src/EnergyPlus/SimulationManager.cc @@ -268,6 +268,7 @@ namespace SimulationManager { state.dataGlobal->KickOffSimulation = true; Weather::ResetEnvironmentCounter(state); + SetupSimulation(state, ErrorsFound); FaultsManager::CheckAndReadFaults(state); @@ -330,6 +331,7 @@ namespace SimulationManager { ReportNodeConnections(state); } SystemReports::CreateEnergyReportStructure(state); + bool anyEMSRan; // point to finish setup processing EMS, sensor ready now EMSManager::ManageEMS(state, EMSManager::EMSCallFrom::SetupSimulation, anyEMSRan, ObjexxFCL::Optional_int_const()); diff --git a/testfiles/LrgOff_GridStorageEMSSmoothing.idf b/testfiles/LrgOff_GridStorageEMSSmoothing.idf index 7e95d35677b..863e8a8cc4a 100644 --- a/testfiles/LrgOff_GridStorageEMSSmoothing.idf +++ b/testfiles/LrgOff_GridStorageEMSSmoothing.idf @@ -9887,7 +9887,7 @@ Output:EnergyManagementSystem, Verbose, !- Actuator Availability Dictionary Reporting Verbose, !- Internal Variable Availability Dictionary Reporting - ErrorsOnly; !- EMS Runtime Language Debug Output Level + Verbose; !- EMS Runtime Language Debug Output Level EnergyManagementSystem:Sensor, CurntFacilityElectDemand,!- Name diff --git a/testfiles/RetailPackagedTESCoil.idf b/testfiles/RetailPackagedTESCoil.idf index 4117d73a7dd..57dc097d9d8 100644 --- a/testfiles/RetailPackagedTESCoil.idf +++ b/testfiles/RetailPackagedTESCoil.idf @@ -8114,8 +8114,7 @@ Run SumIntakeMassFlowHeatCap, !- Run SumReliefAirMCTterm, !- Run CalcWindTerms, !- - Run CalcBouyancyTerms, !- - SEt Numerator = WindMCT + NatBouyMCT + SumHATroof + QdotCond + SumReliefMCT, !- + Set Numerator = WindMCT + NatBouyMCT + SumHATroof + QdotCond + SumReliefMCT, !- Set Denominator = WindMC + NatBouyMC + SumHAroof + SumIntakeMdotCp, !- Set Troof = Numerator / Denominator, !- IF Ta < 2.0, !- @@ -8123,7 +8122,8 @@ ENDIF, !- IF Troof < Twb, !- Set Twb = Troof - 0.2, !- - ENDIF; !- + ENDIF, !- + Run CalcBouyancyTerms; !- EnergyManagementSystem:Program, ApplyMicroClimeActuators,!- Name diff --git a/testfiles/_ResidentialBase.idf b/testfiles/_ResidentialBase.idf index fc17d29a393..f6aeb9559f4 100644 --- a/testfiles/_ResidentialBase.idf +++ b/testfiles/_ResidentialBase.idf @@ -8143,6 +8143,11 @@ Yes, !- Output ExtShd Yes; !- Output Tarcog + Output:EnergyManagementSystem, + Verbose, !- Actuator Availability Dictionary Reporting + Verbose, !- Internal Variable Availability Dictionary Reporting + Verbose; !- EMS Runtime Language Debug Output Level + !- =========== ALL OBJECTS IN CLASS: OUTPUT:JSON =========== Output:JSON, diff --git a/tst/EnergyPlus/unit/EMSManager.unit.cc b/tst/EnergyPlus/unit/EMSManager.unit.cc index 9814a590449..53c810e9e04 100644 --- a/tst/EnergyPlus/unit/EMSManager.unit.cc +++ b/tst/EnergyPlus/unit/EMSManager.unit.cc @@ -52,6 +52,7 @@ // EnergyPlus Headers #include "Fixtures/EnergyPlusFixture.hh" +#include #include #include #include @@ -936,7 +937,7 @@ TEST_F(EnergyPlusFixture, TestUnInitializedEMSVariable2) state->dataEMSMgr->FinishProcessingUserInput = true; bool anyRan; EMSManager::ManageEMS(*state, EMSManager::EMSCallFrom::SetupSimulation, anyRan, ObjexxFCL::Optional_int_const()); - // Expect the variable to not yet be initialized, call EvaluateExpresssion and check argument + // Expect the variable to not yet be initialized, call EvaluateExpression and check argument ErlValueType ReturnValue; bool seriousErrorFound = false; @@ -958,6 +959,179 @@ TEST_F(EnergyPlusFixture, TestUnInitializedEMSVariable2) EXPECT_FALSE(seriousErrorFound); } +TEST_F(EnergyPlusFixture, TestEMSVariableInitAfterRef1) +{ + // test for #11360 - EMS variable initialized after reference, outside of ManageSimulation + std::string const idf_objects = delimited_string({ + + "EnergyManagementSystem:Program,", + " ev_discharge_program, !- Name", + " Set power_mult = site_temp_adj, !- Program Line 1", + " Set site_temp_adj = 0.1; !- Program Line 2", + + "EnergyManagementSystem:ProgramCallingManager,", + " ev_discharge_pcm, !- Name", + " BeginTimestepBeforePredictor, !- EnergyPlus Model Calling Point", + " ev_discharge_program; !- Program Name 1", + }); + + ASSERT_TRUE(process_idf(idf_objects)); + state->init_state(*state); + + int internalVarNum = RuntimeLanguageProcessor::FindEMSVariable(*state, "site_temp_adj", 1); + EXPECT_EQ(internalVarNum, 0); + + bool anyRan; + EXPECT_TRUE(state->dataEMSMgr->GetEMSUserInput); + EMSManager::ManageEMS(*state, EMSManager::EMSCallFrom::SetupSimulation, anyRan, ObjexxFCL::Optional_int_const()); + + internalVarNum = RuntimeLanguageProcessor::FindEMSVariable(*state, "site_temp_adj", 1); + ASSERT_GT(internalVarNum, 0); + EXPECT_FALSE(state->dataRuntimeLang->ErlVariable(internalVarNum).Value.initialized); + + EXPECT_FALSE(state->dataEMSMgr->GetEMSUserInput); + EMSManager::ManageEMS(*state, EMSManager::EMSCallFrom::BeginNewEnvironment, anyRan, ObjexxFCL::Optional_int_const()); + + internalVarNum = RuntimeLanguageProcessor::FindEMSVariable(*state, "site_temp_adj", 1); + ASSERT_GT(internalVarNum, 0); + EXPECT_FALSE(state->dataRuntimeLang->ErlVariable(internalVarNum).Value.initialized); + + EXPECT_FALSE(state->dataEMSMgr->GetEMSUserInput); + ASSERT_THROW(EMSManager::ManageEMS(*state, EMSManager::EMSCallFrom::BeginTimestepBeforePredictor, anyRan, ObjexxFCL::Optional_int_const()), EnergyPlus::FatalError); + + internalVarNum = RuntimeLanguageProcessor::FindEMSVariable(*state, "site_temp_adj", 1); + ASSERT_GT(internalVarNum, 0); + EXPECT_FALSE(state->dataRuntimeLang->ErlVariable(internalVarNum).Value.initialized); + + // Expect the variable to not yet be initialized, call EvaluateExpression and check argument + bool seriousErrorFound = false; + ErlValueType ReturnValue = RuntimeLanguageProcessor::EvaluateExpression( + *state, + state->dataRuntimeLang->ErlStack(Util::FindItemInList("EV_DISCHARGE_PROGRAM", state->dataRuntimeLang->ErlStack)).Instruction(1).Argument2, + seriousErrorFound); + EXPECT_TRUE(seriousErrorFound); + + const std::string expected_error = delimited_string({ + " ** Severe ** Problem found in EMS EnergyPlus Runtime Language.", + " ** ~~~ ** Erl program name: EV_DISCHARGE_PROGRAM", + " ** ~~~ ** Erl program line number: 1", + " ** ~~~ ** Erl program line text: SET POWER_MULT = SITE_TEMP_ADJ", + " ** ~~~ ** Error message: *** Error: EvaluateExpression: Variable = 'SITE_TEMP_ADJ' used in expression has not been initialized! *** ", + " ** ~~~ ** Environment=, at Simulation time= 00:-15 - 00:00", + " ** Fatal ** Previous EMS error caused program termination.", + " ...Summary of Errors that led to program termination:", + " ..... Reference severe error count=1", + " ..... Last severe error=Problem found in EMS EnergyPlus Runtime Language.", + }); + + compare_err_stream(expected_error); +} + +TEST_F(EnergyPlusFixture, TestEMSVariableInitAfterRef2) +{ + // test for #11360 - EMS variable initialized after reference, within ManageSimulation + std::string const idf_objects = delimited_string({ + "Version," + DataStringGlobals::MatchVersion + ";", + + "RunPeriod,", + " Run Period 1, !- Name", + " 1, !- Begin Month", + " 1, !- Begin Day of Month", + " 2007, !- Begin Year", + " 1, !- End Month", + " 1, !- End Day of Month", + " 2007, !- End Year", + " Monday, !- Day of Week for Start Day", + " No, !- Use Weather File Holidays and Special Days", + " No, !- Use Weather File Daylight Saving Period", + " No, !- Apply Weekend Holiday Rule", + " Yes, !- Use Weather File Rain Indicators", + " Yes; !- Use Weather File Snow Indicators", + + "SimulationControl,", + " No, !- Do Zone Sizing Calculation", + " No, !- Do System Sizing Calculation", + " No, !- Do Plant Sizing Calculation", + " No, !- Run Simulation for Sizing Periods", + " Yes, !- Run Simulation for Weather File Run Periods", + " , !- Do HVAC Sizing Simulation for Sizing Periods", + " ; !- Maximum Number of HVAC Sizing Simulation Passes", + + "Site:Location,", + " Denver Stapleton Intl Arpt CO USA WMO=724690, !- Name", + " 39.77, !- Latitude {deg}", + " -104.87, !- Longitude {deg}", + " -7.00, !- Time Zone {hr}", + " 1611.00; !- Elevation {m}", + + "Material,", + " Concrete Block, !- Name", + " MediumRough, !- Roughness", + " 0.1014984, !- Thickness {m}", + " 0.3805070, !- Conductivity {W/m-K}", + " 608.7016, !- Density {kg/m3}", + " 836.8000; !- Specific Heat {J/kg-K}", + + "Construction,", + " ConcConstruction, !- Name", + " Concrete Block; !- Outside Layer", + + "BuildingSurface:Detailed," + " Wall, !- Name", + " Wall, !- Surface Type", + " ConcConstruction, !- Construction Name", + " Zone, !- Zone Name", + " , !- Space Name", + " Outdoors, !- Outside Boundary Condition", + " , !- Outside Boundary Condition Object", + " SunExposed, !- Sun Exposure", + " WindExposed, !- Wind Exposure", + " 0.5000000, !- View Factor to Ground", + " 4, !- Number of Vertices", + " 0.000000,0.000000,10.00000, !- X,Y,Z ==> Vertex 1 {m}", + " 0.000000,0.000000,0, !- X,Y,Z ==> Vertex 2 {m}", + " 10.00000,0.000000,0, !- X,Y,Z ==> Vertex 3 {m}", + " 10.00000,0.000000,10.00000; !- X,Y,Z ==> Vertex 4 {m}", + + "Zone," + " Zone, !- Name", + " 0, !- Direction of Relative North {deg}", + " 6.000000, !- X Origin {m}", + " 6.000000, !- Y Origin {m}", + " 0, !- Z Origin {m}", + " 1, !- Type", + " 1, !- Multiplier", + " autocalculate, !- Ceiling Height {m}", + " autocalculate; !- Volume {m3}", + + "EnergyManagementSystem:Program,", + " ev_discharge_program, !- Name", + " Set power_mult = site_temp_adj, !- Program Line 1", + " Set site_temp_adj = 0.1; !- Program Line 2", + + "EnergyManagementSystem:ProgramCallingManager,", + " ev_discharge_pcm, !- Name", + " BeginTimestepBeforePredictor, !- EnergyPlus Model Calling Point", + " ev_discharge_program; !- Program Name 1", + }); + + ASSERT_TRUE(process_idf(idf_objects)); + state->init_state(*state); + + state->dataWeather->WeatherFileExists = true; + state->files.inputWeatherFilePath.filePath = configured_source_directory() / "weather/USA_CO_Golden-NREL.724666_TMY3.epw"; + + int internalVarNum = RuntimeLanguageProcessor::FindEMSVariable(*state, "site_temp_adj", 1); + EXPECT_EQ(internalVarNum, 0); + + EXPECT_TRUE(state->dataEMSMgr->GetEMSUserInput); + ASSERT_THROW(SimulationManager::ManageSimulation(*state), EnergyPlus::FatalError); + + internalVarNum = RuntimeLanguageProcessor::FindEMSVariable(*state, "site_temp_adj", 1); + ASSERT_GT(internalVarNum, 0); + EXPECT_FALSE(state->dataRuntimeLang->ErlVariable(internalVarNum).Value.initialized); +} + TEST_F(EnergyPlusFixture, EMSManager_CheckIfAnyEMS_OutEMS) { std::string const idf_objects = delimited_string({