Skip to content

Commit

Permalink
Add space heater power to space model output
Browse files Browse the repository at this point in the history
  • Loading branch information
JBjoernskov committed Jun 4, 2024
1 parent ec946f5 commit 0daa49e
Show file tree
Hide file tree
Showing 29 changed files with 151 additions and 103 deletions.
2 changes: 1 addition & 1 deletion twin4build/api/codes/ml_layer/request_to_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def request_to_simulator_api(self,start_time,end_time,time_with_warmup,forecast)
else:
table_to_add_data = self.forecast_table_to_add_data

self.db_handler.add_data(table_to_add_data,inputs=input_list_data)
# self.db_handler.add_data(table_to_add_data,inputs=input_list_data)

logger.info("[request_class]: data from the reponse is added to the database in table")
else:
Expand Down
18 changes: 12 additions & 6 deletions twin4build/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1596,8 +1596,10 @@ def apply_model_extensions(self):
system.hasSubSystem.append(damper)
for property_ in damper.hasProperty:
property_.isPropertyOf = damper
for component in damper.hasFluidFedBy:
component.feedsFluidTo.append(damper)
for component in damper.hasFluidReturnedBy:
component.returnsFluidTo.append(damper)
damper.hasFluidFedBy = damper.hasFluidReturnedBy


for space_heater in space_heater_instances:
base_kwargs = self.get_object_properties(space_heater)
Expand All @@ -1608,8 +1610,10 @@ def apply_model_extensions(self):
space_heater = components.SpaceHeaterSystem(**base_kwargs)
space_heater.heatTransferCoefficient = 8.31495759e+01
space_heater.thermalMassHeatCapacity.hasvalue = 2.72765272e+06
for component in space_heater.hasFluidFedBy:
component.feedsFluidTo.append(space_heater)
for component in space_heater.hasFluidSuppliedBy:
component.suppliesFluidTo.append(space_heater)

space_heater.hasFluidFedBy = space_heater.hasFluidSuppliedBy

self._add_component(space_heater)
space_heater.isContainedIn = self.component_dict[space_heater.isContainedIn.id]
Expand Down Expand Up @@ -1815,6 +1819,7 @@ def apply_model_extensions(self):

for ventilation_system in self.system_dict["ventilation"].values():
self._add_object(ventilation_system)


logger.info("[Model Class] : Exited from Apply Model Extensions Function")

Expand Down Expand Up @@ -2651,7 +2656,7 @@ def connect(self):
fan_instances = self.get_component_by_class(self.component_dict, components.FanSystem)
controller_instances = self.get_component_by_class(self.component_dict, base.Controller)
shading_device_instances = self.get_component_by_class(self.component_dict, components.ShadingDeviceSystem)
sensor_instances = self.get_component_by_class(self.component_dict, components.SensorSystem, filter=lambda v: isinstance(v.observes, base.Pressure)==False)
sensor_instances = self.get_component_by_class(self.component_dict, components.SensorSystem)#, filter=lambda v: isinstance(v.observes, base.Pressure)==False)
meter_instances = self.get_component_by_class(self.component_dict, components.MeterSystem)
node_instances = self.get_component_by_class(self.component_dict, components.FlowJunctionSystem)

Expand Down Expand Up @@ -2989,7 +2994,7 @@ def set_initial_values(self):
components.CoilHeatingSystem.__name__: {"outletAirTemperature": 21},
components.CoilCoolingSystem.__name__: {},
components.DamperSystem.__name__: {"airFlowRate": 0,
"damperPosition": 0},
"damperPosition": 0},
components.ValveSystem.__name__: {"waterFlowRate": 0,
"valvePosition": 0},
components.ValveFMUSystem.__name__: {"waterFlowRate": 0,
Expand Down Expand Up @@ -3096,6 +3101,7 @@ def load_model(self, semantic_model_filename=None, input_config=None, infer_conn
self._create_object_graph(self.component_base_dict)
self.draw_object_graph(filename="object_graph_input")
self.apply_model_extensions()
# self.parse_semantic_model()

if fcn is not None:
self.fcn = fcn.__get__(self, Model)
Expand Down
Binary file modified twin4build/model/tests/configuration_template_OU44_room_case.xlsm
Binary file not shown.
100 changes: 60 additions & 40 deletions twin4build/model/tests/test_OU44_room_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@
uppath = lambda _path,n: os.sep.join(_path.split(os.sep)[:-n])
file_path = uppath(os.path.abspath(__file__), 4)
sys.path.append(file_path)
from twin4build.model.model import Model
from twin4build.simulator.simulator import Simulator
import twin4build as tb
import twin4build.utils.plot.plot as plot
from twin4build.saref.profile.schedule.schedule_system import ScheduleSystem
from twin4build.utils.piecewise_linear_schedule import PiecewiseLinearScheduleSystem
from twin4build.utils.uppath import uppath
def fcn(self):
'''
Expand All @@ -24,7 +21,7 @@ def fcn(self):
The test() function sets simulation parameters and runs a simulation of the system
model using the Simulator() class. It then generates several plots of the simulation results using functions from the plot module.
'''
occupancy_schedule = ScheduleSystem(
occupancy_schedule = tb.ScheduleSystem(
weekDayRulesetDict = {
"ruleset_default_value": 0,
"ruleset_start_minute": [0,0,0,0,0,0,0],
Expand All @@ -34,9 +31,9 @@ def fcn(self):
"ruleset_value": [3,5,20,25,27,7,3]}, #35
add_noise = True,
saveSimulationResult = True,
id = "OE20-601b-2_occupancy_schedule")
id = "Occupancy schedule")

indoor_temperature_setpoint_schedule = ScheduleSystem(
indoor_temperature_setpoint_schedule = tb.ScheduleSystem(
weekDayRulesetDict = {
"ruleset_default_value": 20,
"ruleset_start_minute": [0],
Expand All @@ -59,9 +56,9 @@ def fcn(self):
"ruleset_end_hour": [17],
"ruleset_value": [21]},
saveSimulationResult = True,
id = "OE20-601b-2_temperature_setpoint_schedule")
id = "Temperature setpoint schedule")

supply_water_temperature_setpoint_schedule = PiecewiseLinearScheduleSystem(
supply_water_temperature_setpoint_schedule = tb.PiecewiseLinearScheduleSystem(
weekDayRulesetDict = {
"ruleset_default_value": {"X": [-5, 5, 7],
"Y": [58, 65, 60.5]},
Expand All @@ -72,9 +69,9 @@ def fcn(self):
"ruleset_value": [{"X": [-7, 5, 9],
"Y": [72, 55, 50]}]},
saveSimulationResult = True,
id = "Heating system_supply_water_temperature_schedule")
id = "Supply water temperature")

system_supply_air_temperature_schedule = ScheduleSystem(
supply_air_temperature_schedule = tb.ScheduleSystem(
weekDayRulesetDict = {
"ruleset_default_value": 21,
"ruleset_start_minute": [],
Expand All @@ -83,37 +80,61 @@ def fcn(self):
"ruleset_end_hour": [],
"ruleset_value": []},
saveSimulationResult = True,
id = "Ventilation system_supply_air_temperature_schedule")
id = "Supply air temperature")


space = tb.BuildingSpaceSystem(id="OE20-601b-2", airVolume=466.54)
temperature_controller = tb.ControllerSystem(id="Temperature controller", K_p=2.50773924e-01, K_i=4.38174242e-01, K_d=0)
co2_controller = tb.RulebasedControllerSystem(id="CO2 controller")
supply_damper = tb.DamperSystem(id="Supply damper", airFlowRateMax=tb.PropertyValue(hasValue=0.544444444))
exhaust_damper = tb.DamperSystem(id="Exhaust damper", airFlowRateMax=tb.PropertyValue(hasValue=0.544444444))
space_heater = tb.SpaceHeaterSystem(id="Space heater",
heatTransferCoefficient=8.31495759e+01,
thermalMassHeatCapacity=tb.PropertyValue(hasValue=2.72765272e+06),
temperatureClassification=tb.PropertyValue("45/30-21"))
valve = tb.ValveSystem(id="Valve", waterFlowRateMax=0.0202, valveAuthority=1)
heating_meter = tb.MeterSystem(id="Heating meter")
temperature_sensor = tb.SensorSystem(id="Temperature sensor")
co2_sensor = tb.SensorSystem(id="CO2 sensor")
outdoor_environment = self.component_dict["outdoor_environment"]

self._add_component(occupancy_schedule)
self._add_component(indoor_temperature_setpoint_schedule)
self._add_component(supply_water_temperature_setpoint_schedule)
self._add_component(system_supply_air_temperature_schedule)
initial_temperature = 21
custom_initial_dict = {"OE20-601b-2": {"indoorTemperature": initial_temperature}}
self.set_custom_initial_dict(custom_initial_dict)

def export_csv(simulator):
model = simulator.model
df_input = pd.DataFrame()
df_output = pd.DataFrame()
df_input.insert(0, "time", simulator.dateTimeSteps)
df_output.insert(0, "time", simulator.dateTimeSteps)
# self.add_connection(co2_controller, supply_damper, "inputSignal", "damperPosition")
# self.add_connection(co2_controller, exhaust_damper, "inputSignal", "damperPosition")
# self.add_connection(co2_sensor, co2_controller, "measuredValue", "actualValue")
# self.add_connection(supply_damper, space, "airFlowRate", "supplyAirFlowRate")
# self.add_connection(exhaust_damper, space, "airFlowRate", "returnAirFlowRate")
# self.add_connection(supply_damper, space, "damperPosition", "supplyDamperPosition")
# self.add_connection(exhaust_damper, space, "damperPosition", "returnDamperPosition")
self.add_connection(outdoor_environment, space, "outdoorTemperature", "outdoorTemperature")
self.add_connection(outdoor_environment, space, "globalIrradiation", "globalIrradiation")
self.add_connection(outdoor_environment, supply_water_temperature_setpoint_schedule, "outdoorTemperature", "outdoorTemperature")
self.add_connection(supply_water_temperature_setpoint_schedule, space_heater, "supplyWaterTemperature", "supplyWaterTemperature")
self.add_connection(supply_water_temperature_setpoint_schedule, space, "supplyWaterTemperature", "supplyWaterTemperature")
self.add_connection(supply_air_temperature_schedule, space, "scheduleValue", "supplyAirTemperature")
self.add_connection(space, temperature_sensor, "indoorTemperature", "measuredValue")
self.add_connection(space, co2_sensor, "indoorCo2Concentration", "measuredValue")
self.add_connection(indoor_temperature_setpoint_schedule, temperature_controller, "scheduleValue", "setpointValue")
self.add_connection(temperature_sensor, temperature_controller, "measuredValue", "actualValue")
self.add_connection(occupancy_schedule, space, "scheduleValue", "numberOfPeople")
self.add_connection(valve, space, "valvePosition", "valvePosition")
self.add_connection(temperature_controller, valve, "inputSignal", "valvePosition")
self.add_connection(valve, space_heater, "waterFlowRate", "waterFlowRate")
self.add_connection(space, space_heater, "indoorTemperature", "indoorTemperature")

for component in model.component_dict.values():
for property_, arr in component.savedInput.items():
column_name = f"{component.id} ||| {property_}"
df_input = df_input.join(pd.DataFrame({column_name: arr}))

for property_, arr in component.savedOutput.items():
column_name = f"{component.id} ||| {property_}"
df_output = df_output.join(pd.DataFrame({column_name: arr}))
t = tb.Temperature()
c = tb.Co2()
temperature_controller.observes = t
co2_controller.observes = c
t.isPropertyOf = space
c.isPropertyOf = space

df_measuring_devices = simulator.get_simulation_readings()

df_input.set_index("time").to_csv("input.csv")
df_output.set_index("time").to_csv("output.csv")
df_measuring_devices.set_index("time").to_csv("measuring_devices.csv")

initial_temperature = 21
custom_initial_dict = {"OE20-601b-2": {"indoorTemperature": initial_temperature}}
self.set_custom_initial_dict(custom_initial_dict)


class TestOU44RoomCase(unittest.TestCase):
Expand All @@ -123,15 +144,14 @@ def test_OU44_room_case(self, show=False):
startTime = datetime.datetime(year=2022, month=1, day=3, hour=0, minute=0, second=0, tzinfo=tz.gettz("Europe/Copenhagen"))
endTime = datetime.datetime(year=2022, month=1, day=8, hour=0, minute=0, second=0, tzinfo=tz.gettz("Europe/Copenhagen"))

model = Model(id="default", saveSimulationResult=True)
model = tb.Model(id="default", saveSimulationResult=True)

filename = os.path.join(uppath(os.path.abspath(__file__), 1), "weather_DMI.csv")
model.add_outdoor_environment_system(filename=filename)
filename = os.path.join(uppath(os.path.abspath(__file__), 3), "model", "tests", "configuration_template_OU44_room_case.xlsm")
model.load_model(semantic_model_filename=filename, infer_connections=True, fcn=fcn)
model.load_model(fcn=fcn, infer_connections=False)


simulator = Simulator()
simulator = tb.Simulator()
simulator.simulate(model,
stepSize=stepSize,
startTime=startTime,
Expand Down
2 changes: 1 addition & 1 deletion twin4build/saref/property_value/property_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def __init__(self,
hasValue: Union[float, int, None]=None,
isMeasuredIn: Union[unit_of_measure.UnitOfMeasure, None]=None,
isValueOfProperty: Union[property_.Property, None]=None):
assert isinstance(hasValue, float) or isinstance(hasValue, int) or hasValue is None, "Attribute \"hasValue\" is of type \"" + str(type(hasValue)) + "\" but must be of type \"" + str(int) + "\""
assert isinstance(hasValue, float) or isinstance(hasValue, int) or isinstance(hasValue, str) or hasValue is None, "Attribute \"hasValue\" is of type \"" + str(type(hasValue)) + "\" but must be of type \"" + str(int) + "\""
assert isMeasuredIn is None or issubclass(isMeasuredIn, unit_of_measure.UnitOfMeasure), "Attribute \"isMeasuredIn\" is of type \"" + str(isMeasuredIn.__name__) + "\" but must be of type \"" + "<class 'type'>" + "\""
assert isinstance(isValueOfProperty, property_.Property) or isValueOfProperty is None, "Attribute \"isValueOfProperty\" is of type \"" + str(type(isValueOfProperty)) + "\" but must be of type \"" + str(property_.Property) + "\""
self.hasValue = hasValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ def __init__(self,
'numberOfPeople': None,
"outdoorCo2Concentration": None,
"T_boundary": None}
self.output = {"indoorTemperature": None, "indoorCo2Concentration": None}
self.output = {"indoorTemperature": None,
"indoorCo2Concentration": None,
"spaceHeaterPower": None}

self.FMUinputMap = {'airFlowRate': "m_a_flow",
'waterFlowRate': "m_w_flow",
Expand All @@ -130,7 +132,8 @@ def __init__(self,
"indoorTemperature_adj1": "T_adj1",
"T_boundary": "T_boundary"}
self.FMUoutputMap = {"indoorTemperature": "T_air",
"indoorCo2Concentration": "CO2_concentration"}
"indoorCo2Concentration": "CO2_concentration",
"spaceHeaterPower": "r2C2_1.rad.Q_flow"}

self.FMUparameterMap = {"C_supply": "C_supply",
"C_wall": "C_wall",
Expand Down Expand Up @@ -182,7 +185,7 @@ def __init__(self,



self.input_unit_conversion = {'airFlowRate': do_nothing,
self.input_conversion = {'airFlowRate': do_nothing,
'waterFlowRate': do_nothing,
'supplyAirTemperature': to_degK_from_degC,
'supplyWaterTemperature': to_degK_from_degC,
Expand All @@ -192,7 +195,9 @@ def __init__(self,
"outdoorCo2Concentration": do_nothing,
"indoorTemperature_adj1": to_degK_from_degC,
"T_boundary": to_degK_from_degC}
self.output_unit_conversion = {"indoorTemperature": to_degC_from_degK, "indoorCo2Concentration": do_nothing}
self.output_conversion = {"indoorTemperature": to_degC_from_degK,
"indoorCo2Concentration": do_nothing,
"spaceHeaterPower": lambda x: -x}

self.INITIALIZED = False
self._config = {"parameters": list(self.parameter.keys())}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ def __init__(self,
"outdoorCo2Concentration": None,
"indoorTemperature_adj1": None,
"T_boundary": None}
self.output = {"indoorTemperature": None, "indoorCo2Concentration": None}
self.output = {"indoorTemperature": None,
"indoorCo2Concentration": None,
"spaceHeaterPower": None}

self.FMUinputMap = {'airFlowRate': "m_a_flow",
'waterFlowRate': "m_w_flow",
Expand All @@ -134,7 +136,8 @@ def __init__(self,
"indoorTemperature_adj1": "T_adj1",
"T_boundary": "T_boundary"}
self.FMUoutputMap = {"indoorTemperature": "T_air",
"indoorCo2Concentration": "CO2_concentration"}
"indoorCo2Concentration": "CO2_concentration",
"spaceHeaterPower": "r2C2_1.rad.Q_flow"}

self.FMUparameterMap = {"C_supply": "C_supply",
"C_wall": "C_wall",
Expand Down Expand Up @@ -186,7 +189,7 @@ def __init__(self,



self.input_unit_conversion = {'airFlowRate': do_nothing,
self.input_conversion = {'airFlowRate': do_nothing,
'waterFlowRate': do_nothing,
'supplyAirTemperature': to_degK_from_degC,
'supplyWaterTemperature': to_degK_from_degC,
Expand All @@ -196,7 +199,9 @@ def __init__(self,
"outdoorCo2Concentration": do_nothing,
"indoorTemperature_adj1": to_degK_from_degC,
"T_boundary": to_degK_from_degC}
self.output_unit_conversion = {"indoorTemperature": to_degC_from_degK, "indoorCo2Concentration": do_nothing}
self.output_conversion = {"indoorTemperature": to_degC_from_degK,
"indoorCo2Concentration": do_nothing,
"spaceHeaterPower": lambda x: -x}

self.INITIALIZED = False
self._config = {"parameters": list(self.parameter.keys())}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def __init__(self,



self.input_unit_conversion = {'airFlowRate': do_nothing,
self.input_conversion = {'airFlowRate': do_nothing,
'waterFlowRate': do_nothing,
'supplyAirTemperature': to_degK_from_degC,
'supplyWaterTemperature': to_degK_from_degC,
Expand All @@ -183,7 +183,7 @@ def __init__(self,
'numberOfPeople': do_nothing,
"outdoorCo2Concentration": do_nothing,
"indoorTemperature_adj1": to_degK_from_degC}
self.output_unit_conversion = {"indoorTemperature": to_degC_from_degK, "indoorCo2Concentration": do_nothing}
self.output_conversion = {"indoorTemperature": to_degC_from_degK, "indoorCo2Concentration": do_nothing}

self.INITIALIZED = False
self._config = {"parameters": list(self.parameter.keys())}
Expand Down
Loading

0 comments on commit 0daa49e

Please sign in to comment.