From 61bab979b831854822b1da3f4418bcc2e29cf915 Mon Sep 17 00:00:00 2001 From: bball Date: Mon, 23 Apr 2018 11:17:16 -0600 Subject: [PATCH] do not translate EMS Sensor if there is a UID as key value but no model object can be found with that UID --- ...dTranslateEnergyManagementSystemSensor.cpp | 10 +++- .../src/energyplus/Test/EMS_GTest.cpp | 51 +++++++++++++++++++ .../EnergyManagementSystemSensor_GTest.cpp | 33 ++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) diff --git a/openstudiocore/src/energyplus/ForwardTranslator/ForwardTranslateEnergyManagementSystemSensor.cpp b/openstudiocore/src/energyplus/ForwardTranslator/ForwardTranslateEnergyManagementSystemSensor.cpp index 63ba8f20642..c5258df3b4d 100644 --- a/openstudiocore/src/energyplus/ForwardTranslator/ForwardTranslateEnergyManagementSystemSensor.cpp +++ b/openstudiocore/src/energyplus/ForwardTranslator/ForwardTranslateEnergyManagementSystemSensor.cpp @@ -61,7 +61,6 @@ boost::optional ForwardTranslator::translateEnergyManagementSystemSen boost::optional s; IdfObject idfObject(openstudio::IddObjectType::EnergyManagementSystem_Sensor); - m_idfObjects.push_back(idfObject); //Name s = modelObject.name(); if (s) { @@ -91,24 +90,33 @@ boost::optional ForwardTranslator::translateEnergyManagementSystemSen newline.replace(pos, 38, mObject.get().nameString()); } } + else { + //did not find an object with the UID so do not FT + LOG(Error, "Key Name for Sensor '" << modelObject.nameString() << "' is UID but does not exist, it will not be translated."); + return boost::none; + } } idfObject.setString(EnergyManagementSystem_SensorFields::Output_VariableorOutput_MeterIndexKeyName, newline); } d = modelObject.outputVariable(); if (d.is_initialized()) { idfObject.setString(EnergyManagementSystem_SensorFields::Output_VariableorOutput_MeterName, d.get().variableName()); + m_idfObjects.push_back(idfObject); return idfObject; } m = modelObject.outputMeter(); if (m.is_initialized()){ idfObject.setString(EnergyManagementSystem_SensorFields::Output_VariableorOutput_MeterName, m.get().name()); + m_idfObjects.push_back(idfObject); return idfObject; } s = modelObject.outputVariableOrMeterName(); if (s.is_initialized()) { idfObject.setString(EnergyManagementSystem_SensorFields::Output_VariableorOutput_MeterName, s.get()); + m_idfObjects.push_back(idfObject); return idfObject; } + m_idfObjects.push_back(idfObject); return idfObject; } diff --git a/openstudiocore/src/energyplus/Test/EMS_GTest.cpp b/openstudiocore/src/energyplus/Test/EMS_GTest.cpp index f8ba42aac65..15eb26893ca 100644 --- a/openstudiocore/src/energyplus/Test/EMS_GTest.cpp +++ b/openstudiocore/src/energyplus/Test/EMS_GTest.cpp @@ -132,6 +132,11 @@ #include "../../model/OtherEquipment_Impl.hpp" #include "../../model/OtherEquipmentDefinition.hpp" #include "../../model/OtherEquipmentDefinition_Impl.hpp" +#include "../../model/PlantLoop.hpp" +#include "../../model/Node.hpp" +#include "../../model/Node_Impl.hpp" +#include "../../model/AvailabilityManagerHighTemperatureTurnOff.hpp" +#include "../../model/AvailabilityManagerHighTemperatureTurnOff_Impl.hpp" #include "../../model/Version.hpp" #include "../../model/Version_Impl.hpp" @@ -1595,3 +1600,49 @@ TEST_F(EnergyPlusFixture, ReverseTranslatorTrendVariable2_EMS) { model.save(toPath("./EMS_TrendVariable2T.osm"), true); } + +TEST_F(EnergyPlusFixture, ForwardTranslatorSensorDelete_EMS) { + + Model model; + PlantLoop plantLoop(model); + AvailabilityManagerHighTemperatureTurnOff avm(model); + avm.setSensorNode(model.outdoorAirNode()); + plantLoop.addAvailabilityManager(avm); + std::vector avm_names = avm.outputVariableNames(); + + // add sensor 1 + EnergyManagementSystemSensor sensor(model, avm_names[0]); + sensor.setKeyName(toString(avm.handle())); + + // Sensor attached to AVM + std::string key = toString(avm.handle()); + EXPECT_EQ(key, sensor.keyName()); + // 1 sensor in the model + EXPECT_EQ(static_cast(1), model.getModelObjects().size()); + // 1 avm in the model + EXPECT_EQ(static_cast(1), model.getModelObjects().size()); + + ForwardTranslator forwardTranslator; + Workspace workspace = forwardTranslator.translateModel(model); + + //there should be 1 sensor + ASSERT_EQ(1u, workspace.getObjectsByType(IddObjectType::EnergyManagementSystem_Sensor).size()); + WorkspaceObject object = workspace.getObjectsByType(IddObjectType::EnergyManagementSystem_Sensor)[0]; + + //model.save(toPath("./EMS_SensorBeforeDelete.osm"), true); + //workspace.save(toPath("./EMS_SensorBeforeDelete.idf"), true); + + avm.remove(); + // 0 avm in the model + EXPECT_EQ(static_cast(0), model.getModelObjects().size()); + //sensor still has keyName as avm UUID string (will not FT though eventually) + EXPECT_EQ(key, sensor.keyName()); + + Workspace workspaceDelete = forwardTranslator.translateModel(model); + + ASSERT_EQ(0u, workspaceDelete.getObjectsByType(IddObjectType::EnergyManagementSystem_Sensor).size()); + + //model.save(toPath("./EMS_SensorDelete.osm"), true); + //workspaceDelete.save(toPath("./EMS_SensorDelete.idf"), true); + +} diff --git a/openstudiocore/src/model/test/EnergyManagementSystemSensor_GTest.cpp b/openstudiocore/src/model/test/EnergyManagementSystemSensor_GTest.cpp index b7f6f98531e..d64ec3e0201 100644 --- a/openstudiocore/src/model/test/EnergyManagementSystemSensor_GTest.cpp +++ b/openstudiocore/src/model/test/EnergyManagementSystemSensor_GTest.cpp @@ -32,6 +32,11 @@ #include "ModelFixture.hpp" #include "../Building.hpp" #include "../Building_Impl.hpp" +#include "../PlantLoop.hpp" +#include "../Node.hpp" +#include "../Node_Impl.hpp" +#include "../AvailabilityManagerHighTemperatureTurnOff.hpp" +#include "../AvailabilityManagerHighTemperatureTurnOff_Impl.hpp" #include "../ThermalZone.hpp" #include "../EnergyManagementSystemSensor.hpp" #include "../EnergyManagementSystemSensor_Impl.hpp" @@ -177,4 +182,32 @@ TEST_F(ModelFixture, EMSSensorOutVar) { } +TEST_F(ModelFixture, EMSSensorDelete) { + Model model; + PlantLoop plantLoop(model); + AvailabilityManagerHighTemperatureTurnOff avm(model); + avm.setSensorNode(model.outdoorAirNode()); + plantLoop.addAvailabilityManager(avm); + std::vector avm_names = avm.outputVariableNames(); + + // add sensor 1 + EnergyManagementSystemSensor sensor(model, avm_names[0]); + sensor.setKeyName(toString(avm.handle())); + + // Sensor attached to AVM + std::string key = toString(avm.handle()); + EXPECT_EQ(key, sensor.keyName()); + // 1 sensor in the model + EXPECT_EQ(static_cast(1), model.getModelObjects().size()); + // 1 avm in the model + EXPECT_EQ(static_cast(1), model.getModelObjects().size()); + model.save(toPath("./EMS_sensor_delete_test.osm"), true); + + avm.remove(); + // 0 avm in the model + EXPECT_EQ(static_cast(0), model.getModelObjects().size()); + //sensor still has keyName as avm UUID string (will not FT though eventually) + EXPECT_EQ(key, sensor.keyName()); +} +