5252
5353// EnergyPlus Headers
5454#include " Fixtures/EnergyPlusFixture.hh"
55+ #include < EnergyPlus/ConfiguredFunctions.hh>
5556#include < EnergyPlus/Construction.hh>
5657#include < EnergyPlus/CurveManager.hh>
5758#include < EnergyPlus/Data/EnergyPlusData.hh>
@@ -936,7 +937,7 @@ TEST_F(EnergyPlusFixture, TestUnInitializedEMSVariable2)
936937 state->dataEMSMgr ->FinishProcessingUserInput = true ;
937938 bool anyRan;
938939 EMSManager::ManageEMS (*state, EMSManager::EMSCallFrom::SetupSimulation, anyRan, ObjexxFCL::Optional_int_const ());
939- // Expect the variable to not yet be initialized, call EvaluateExpresssion and check argument
940+ // Expect the variable to not yet be initialized, call EvaluateExpression and check argument
940941
941942 ErlValueType ReturnValue;
942943 bool seriousErrorFound = false ;
@@ -958,6 +959,181 @@ TEST_F(EnergyPlusFixture, TestUnInitializedEMSVariable2)
958959 EXPECT_FALSE (seriousErrorFound);
959960}
960961
962+ TEST_F (EnergyPlusFixture, TestEMSVariableInitAfterRef1)
963+ {
964+ // test for #11360 - EMS variable initialized after reference, outside of ManageSimulation
965+ std::string const idf_objects = delimited_string ({
966+
967+ " EnergyManagementSystem:Program," ,
968+ " ev_discharge_program, !- Name" ,
969+ " Set power_mult = site_temp_adj, !- Program Line 1" ,
970+ " Set site_temp_adj = 0.1; !- Program Line 2" ,
971+
972+ " EnergyManagementSystem:ProgramCallingManager," ,
973+ " ev_discharge_pcm, !- Name" ,
974+ " BeginTimestepBeforePredictor, !- EnergyPlus Model Calling Point" ,
975+ " ev_discharge_program; !- Program Name 1" ,
976+ });
977+
978+ ASSERT_TRUE (process_idf (idf_objects));
979+ state->init_state (*state);
980+
981+ int internalVarNum = RuntimeLanguageProcessor::FindEMSVariable (*state, " site_temp_adj" , 1 );
982+ EXPECT_EQ (internalVarNum, 0 );
983+
984+ bool anyRan;
985+ EXPECT_TRUE (state->dataEMSMgr ->GetEMSUserInput );
986+ EMSManager::ManageEMS (*state, EMSManager::EMSCallFrom::SetupSimulation, anyRan, ObjexxFCL::Optional_int_const ());
987+
988+ internalVarNum = RuntimeLanguageProcessor::FindEMSVariable (*state, " site_temp_adj" , 1 );
989+ ASSERT_GT (internalVarNum, 0 );
990+ EXPECT_FALSE (state->dataRuntimeLang ->ErlVariable (internalVarNum).Value .initialized );
991+
992+ EXPECT_FALSE (state->dataEMSMgr ->GetEMSUserInput );
993+ EMSManager::ManageEMS (*state, EMSManager::EMSCallFrom::BeginNewEnvironment, anyRan, ObjexxFCL::Optional_int_const ());
994+
995+ internalVarNum = RuntimeLanguageProcessor::FindEMSVariable (*state, " site_temp_adj" , 1 );
996+ ASSERT_GT (internalVarNum, 0 );
997+ EXPECT_FALSE (state->dataRuntimeLang ->ErlVariable (internalVarNum).Value .initialized );
998+
999+ EXPECT_FALSE (state->dataEMSMgr ->GetEMSUserInput );
1000+ ASSERT_THROW (EMSManager::ManageEMS (*state, EMSManager::EMSCallFrom::BeginTimestepBeforePredictor, anyRan, ObjexxFCL::Optional_int_const ()),
1001+ EnergyPlus::FatalError);
1002+
1003+ internalVarNum = RuntimeLanguageProcessor::FindEMSVariable (*state, " site_temp_adj" , 1 );
1004+ ASSERT_GT (internalVarNum, 0 );
1005+ EXPECT_FALSE (state->dataRuntimeLang ->ErlVariable (internalVarNum).Value .initialized );
1006+
1007+ // Expect the variable to not yet be initialized, call EvaluateExpression and check argument
1008+ bool seriousErrorFound = false ;
1009+ ErlValueType ReturnValue = RuntimeLanguageProcessor::EvaluateExpression (
1010+ *state,
1011+ state->dataRuntimeLang ->ErlStack (Util::FindItemInList (" EV_DISCHARGE_PROGRAM" , state->dataRuntimeLang ->ErlStack )).Instruction (1 ).Argument2 ,
1012+ seriousErrorFound);
1013+ EXPECT_TRUE (seriousErrorFound);
1014+
1015+ const std::string expected_error = delimited_string ({
1016+ " ** Severe ** Problem found in EMS EnergyPlus Runtime Language." ,
1017+ " ** ~~~ ** Erl program name: EV_DISCHARGE_PROGRAM" ,
1018+ " ** ~~~ ** Erl program line number: 1" ,
1019+ " ** ~~~ ** Erl program line text: SET POWER_MULT = SITE_TEMP_ADJ" ,
1020+ " ** ~~~ ** Error message: *** Error: EvaluateExpression: Variable = 'SITE_TEMP_ADJ' used in expression has not been initialized! "
1021+ " *** " ,
1022+ " ** ~~~ ** Environment=, at Simulation time= 00:-15 - 00:00" ,
1023+ " ** Fatal ** Previous EMS error caused program termination." ,
1024+ " ...Summary of Errors that led to program termination:" ,
1025+ " ..... Reference severe error count=1" ,
1026+ " ..... Last severe error=Problem found in EMS EnergyPlus Runtime Language." ,
1027+ });
1028+
1029+ compare_err_stream (expected_error);
1030+ }
1031+
1032+ TEST_F (EnergyPlusFixture, TestEMSVariableInitAfterRef2)
1033+ {
1034+ // test for #11360 - EMS variable initialized after reference, within ManageSimulation
1035+ std::string const idf_objects = delimited_string ({
1036+ " Version," + DataStringGlobals::MatchVersion + " ;" ,
1037+
1038+ " RunPeriod," ,
1039+ " Run Period 1, !- Name" ,
1040+ " 1, !- Begin Month" ,
1041+ " 1, !- Begin Day of Month" ,
1042+ " 2007, !- Begin Year" ,
1043+ " 1, !- End Month" ,
1044+ " 1, !- End Day of Month" ,
1045+ " 2007, !- End Year" ,
1046+ " Monday, !- Day of Week for Start Day" ,
1047+ " No, !- Use Weather File Holidays and Special Days" ,
1048+ " No, !- Use Weather File Daylight Saving Period" ,
1049+ " No, !- Apply Weekend Holiday Rule" ,
1050+ " Yes, !- Use Weather File Rain Indicators" ,
1051+ " Yes; !- Use Weather File Snow Indicators" ,
1052+
1053+ " SimulationControl," ,
1054+ " No, !- Do Zone Sizing Calculation" ,
1055+ " No, !- Do System Sizing Calculation" ,
1056+ " No, !- Do Plant Sizing Calculation" ,
1057+ " No, !- Run Simulation for Sizing Periods" ,
1058+ " Yes, !- Run Simulation for Weather File Run Periods" ,
1059+ " , !- Do HVAC Sizing Simulation for Sizing Periods" ,
1060+ " ; !- Maximum Number of HVAC Sizing Simulation Passes" ,
1061+
1062+ " Site:Location," ,
1063+ " Denver Stapleton Intl Arpt CO USA WMO=724690, !- Name" ,
1064+ " 39.77, !- Latitude {deg}" ,
1065+ " -104.87, !- Longitude {deg}" ,
1066+ " -7.00, !- Time Zone {hr}" ,
1067+ " 1611.00; !- Elevation {m}" ,
1068+
1069+ " Material," ,
1070+ " Concrete Block, !- Name" ,
1071+ " MediumRough, !- Roughness" ,
1072+ " 0.1014984, !- Thickness {m}" ,
1073+ " 0.3805070, !- Conductivity {W/m-K}" ,
1074+ " 608.7016, !- Density {kg/m3}" ,
1075+ " 836.8000; !- Specific Heat {J/kg-K}" ,
1076+
1077+ " Construction," ,
1078+ " ConcConstruction, !- Name" ,
1079+ " Concrete Block; !- Outside Layer" ,
1080+
1081+ " BuildingSurface:Detailed,"
1082+ " Wall, !- Name" ,
1083+ " Wall, !- Surface Type" ,
1084+ " ConcConstruction, !- Construction Name" ,
1085+ " Zone, !- Zone Name" ,
1086+ " , !- Space Name" ,
1087+ " Outdoors, !- Outside Boundary Condition" ,
1088+ " , !- Outside Boundary Condition Object" ,
1089+ " SunExposed, !- Sun Exposure" ,
1090+ " WindExposed, !- Wind Exposure" ,
1091+ " 0.5000000, !- View Factor to Ground" ,
1092+ " 4, !- Number of Vertices" ,
1093+ " 0.000000,0.000000,10.00000, !- X,Y,Z ==> Vertex 1 {m}" ,
1094+ " 0.000000,0.000000,0, !- X,Y,Z ==> Vertex 2 {m}" ,
1095+ " 10.00000,0.000000,0, !- X,Y,Z ==> Vertex 3 {m}" ,
1096+ " 10.00000,0.000000,10.00000; !- X,Y,Z ==> Vertex 4 {m}" ,
1097+
1098+ " Zone,"
1099+ " Zone, !- Name" ,
1100+ " 0, !- Direction of Relative North {deg}" ,
1101+ " 6.000000, !- X Origin {m}" ,
1102+ " 6.000000, !- Y Origin {m}" ,
1103+ " 0, !- Z Origin {m}" ,
1104+ " 1, !- Type" ,
1105+ " 1, !- Multiplier" ,
1106+ " autocalculate, !- Ceiling Height {m}" ,
1107+ " autocalculate; !- Volume {m3}" ,
1108+
1109+ " EnergyManagementSystem:Program," ,
1110+ " ev_discharge_program, !- Name" ,
1111+ " Set power_mult = site_temp_adj, !- Program Line 1" ,
1112+ " Set site_temp_adj = 0.1; !- Program Line 2" ,
1113+
1114+ " EnergyManagementSystem:ProgramCallingManager," ,
1115+ " ev_discharge_pcm, !- Name" ,
1116+ " BeginTimestepBeforePredictor, !- EnergyPlus Model Calling Point" ,
1117+ " ev_discharge_program; !- Program Name 1" ,
1118+ });
1119+
1120+ ASSERT_TRUE (process_idf (idf_objects));
1121+ state->init_state (*state);
1122+
1123+ state->dataWeather ->WeatherFileExists = true ;
1124+ state->files .inputWeatherFilePath .filePath = configured_source_directory () / " weather/USA_CO_Golden-NREL.724666_TMY3.epw" ;
1125+
1126+ int internalVarNum = RuntimeLanguageProcessor::FindEMSVariable (*state, " site_temp_adj" , 1 );
1127+ EXPECT_EQ (internalVarNum, 0 );
1128+
1129+ EXPECT_TRUE (state->dataEMSMgr ->GetEMSUserInput );
1130+ ASSERT_THROW (SimulationManager::ManageSimulation (*state), EnergyPlus::FatalError);
1131+
1132+ internalVarNum = RuntimeLanguageProcessor::FindEMSVariable (*state, " site_temp_adj" , 1 );
1133+ ASSERT_GT (internalVarNum, 0 );
1134+ EXPECT_FALSE (state->dataRuntimeLang ->ErlVariable (internalVarNum).Value .initialized );
1135+ }
1136+
9611137TEST_F (EnergyPlusFixture, EMSManager_CheckIfAnyEMS_OutEMS)
9621138{
9631139 std::string const idf_objects = delimited_string ({
0 commit comments