RC models for model predictive control (mpc library)
Detailed Modelica libraries (Buildings, IDEAS, AixLib) are well suited to simulation but not to
optimisation: they rely on events, tables, media models and large algebraic loops that optimal
control solvers cannot handle. The mpc library generates, from the same YAML building description,
simple resistance-capacitance (RC) models that can be translated directly into a symbolic
CasADi ODE and used for MPC with IPOPT.
trano create-model house.yaml mpc # R3C2 zones (default)
trano create-model house.yaml mpc --rc-model-type R4C3 # R1C1 | R3C2 | R4C3 | ISO13790
trano simulate-model house.yaml mpc # runnable, like the other libraries
or in Python:
from pathlib import Path
from trano.data_models.conversion import convert_network
from trano.elements.library.library import Library
from trano.mpc import RCModelType
library = Library.from_configuration("mpc").model_copy(update={"rc_model_type": RCModelType.r4c3})
network = convert_network("house", Path("house.yaml"), library=library)
network.external_data = Path("measurements.csv") # optional, see below
modelica_model = network.model()
Generated package
| Model | Content |
|---|---|
house.Trano.MPC.Zones |
the single-zone RC models (embedded in the Trano package of every model generated by Trano) |
house.building_mpc |
the flat multi-zone RC model: the MPC model, translatable into a CasADi ODE |
house.building |
the runnable model: weather file, solar gains, occupancy and external data connected to building_mpc |
building_mpc: the MPC model
One RC zone per space, coupled through the conductance of the internal walls. Inputs:
TOutoutdoor air temperature [K],HSol_<orientation>total solar irradiance on each facade orientation [W/m²], e.g.HSol_azi0_til90for the south facade (azimuth 0°, tilt 90°, Buildings library convention),<space>_QIntinternal gains [W] (disturbance) and<space>_QHeaheating power [W] (control input).
The states are the zone temperatures (<space>_Ti, <space>_Te, ...). The solar gains are
gA_<orientation>*HSol_<orientation> for the windows (to the indoor air) and
aE_<orientation>*HSol_<orientation> for the opaque envelope, with parameters per zone and orientation.
The model only uses the Modelica subset that CasADi translators (e.g. rumoca) accept:
- flat, scalar model (no sub-components, no arrays),
- explicit state equations
der(x) = f(x, u, p)without algebraic variables, - parameters bound to literal values (they stay symbolic in CasADi, which enables identification),
- smooth expressions: no events,
if,min/max, tables or functions, - inputs declared with the local
Trano.MPC.RealInputconnector: no dependency on any library.
The test suite translates the building_mpc model of every test building, for every RC structure,
with rumoca to guarantee that the generated models stay MPC ready.
import rumoca
export = rumoca.Session().loads(modelica_model, model="house.building_mpc").to_casadi()
export.rhs # casadi.Function xdot = rhs(t, x, u, p)
export.state_names, export.input_names, export.parameter_names
building: the runnable model
building instantiates building_mpc and connects all its inputs, like the other Trano libraries:
- weather: the same
ReaderTMY3reader and.mosweather file as the Buildings library (weather.parameters.pathin the YAML file);TOutis the dry-bulb temperature; - solar gains: direct (
DirectTiltedSurface) plus diffuse (DiffuseIsotropic) irradiance for each orientation of the envelope, connected toHSol_<orientation>; - occupancy: the Trano occupancy models (schedule, or CO₂-based estimation from data); their
sensible gains are scaled by the floor area and connected to
<space>_QInt; - external data: the CSV file of the network (
network.external_data, same as the other libraries) becomes aCombiTimeTable. Occupancy data sources read their column from it, and a column named<space>_QHeareplays a measured heating power; - heating: otherwise
<space>_QHeais a top-level input ofbuilding, to be set by the MPC (e.g. in co-simulation or through an FMU);<space>_TZonoutputs give the zone temperatures.
The emission systems of the YAML file (radiators, boilers, controls...) are abstracted as the heating power of each zone.
Plugging the model into an MPC runtime
trano create-model house.yaml mpc writes two files:
house.mo- the Modelica package (house.building_mpcand the runnablehouse.building),house.mpc.json- theMPCModelInterfaceofhouse.building_mpc.
The interface lists the states, inputs and parameters in the order of the CasADi vectors
x, u and p of the translated model, so an MPC runtime never has to parse the Modelica code:
states: name, zone, RC node (Ti,Te,Th,Tm) and initial value,inputs:role(controlfor the heating powers,disturbanceotherwise) and, for the disturbances, theirsource: the weather variable (TOut), the orientation of the irradiance (HSol_<orientation>: azimuth and tilt in degrees), or the occupancy model, its parameters and floor area (<space>_QInt), plus the external data column replaying an input, if any,parameters: name, value, unit and zone (they stay symbolic in CasADi, e.g. for identification),zones: comfort state (indoor_temperature), heating input and an estimateddesign_heating_powerusable as upper bound of the control input,weather_file: the weather file of the runnable model, to build the forecasts.
import rumoca
from trano.mpc import InputRole, MPCModelInterface
interface = MPCModelInterface.read("house.mpc.json")
export = rumoca.Session().loads(open("house.mo").read(), model=interface.model).to_casadi()
assert interface.input_names == list(export.input_names) # guaranteed by the test suite
controls = [signal.name for signal in interface.inputs if signal.role == InputRole.control]
rhs = export.rhs # casadi.Function xdot = rhs(t, x, u, p), p = interface.parameter_values
The test suite contains a reference runtime (tests/test_mpc_interface.py) built only from these two
files: it discretises the ODE, builds the forecasts from the disturbance sources and solves an
economic MPC problem with IPOPT for every RC structure.
Zone models (Trano.MPC.Zones)
| Model | States | Structure | Reference |
|---|---|---|---|
R1C1 |
Ti | one lumped capacity, indoor-outdoor and indoor-ground resistances | Bacher & Madsen (2011), Ti model |
R3C2 |
Ti, Te | indoor air + envelope/mass; windows and ventilation connect indoor air to outdoor | Bacher & Madsen (2011), TiTe; Harb et al. (2016) |
R4C3 |
Ti, Te, Th | R3C2 + heat emitter capacity (radiator or floor heating lag) |
Bacher & Madsen (2011), TiTeTh |
ISO13790 |
Ti, Tm | ISO 13790 5R1C network with a capacitive air node (5R2C) | ISO 13790:2008, annex C |
For example, the R3C2 zone equations are:
der(Ti) = ((Te - Ti)/Rie + (TOut - Ti)/Ria + gA*HSol + QInt + QHea)/Ci;
der(Te) = ((Ti - Te)/Rie + (TOut - Te)/Rea + (TGro - Te)/Reg + aE*HSol)/Ce;
In the ISO 13790 model the massless surface node of the standard is eliminated analytically, so the model stays an explicit ODE while matching the standard heat balances.
Parameters
The RC parameters are derived from the geometry and the constructions of the YAML file: ISO 6946
surface resistances, opaque elements split in two halves around the mass node, windows and
ventilation directly between indoor and outdoor air, half of the internal walls capacity assigned to
each adjacent zone, window apertures g·(1-Ff)·A and opaque absorption areas α·Rse·U·A per
orientation. The assumptions are gathered in EstimationSettings:
from trano.mpc import EstimationSettings, rc_building_from_network
building = rc_building_from_network(network, settings=EstimationSettings(air_change_rate=0.4))
modelica_model = building.to_modelica("house") # Trano library + building_mpc
Note
The derived parameters are physically consistent initial guesses. For a real building, calibrate
them on measurements: they remain symbolic once building_mpc is translated into CasADi.
References
- P. Bacher, H. Madsen (2011). Identifying suitable models for the heat dynamics of buildings. Energy and Buildings, 43(7), 1511-1522.
- H. Harb, N. Boyanov, L. Hernandez, R. Streblow, D. Müller (2016). Development and validation of grey-box models for forecasting the thermal response of occupied buildings. Energy and Buildings, 117, 199-207.
- ISO 13790:2008. Energy performance of buildings - Calculation of energy use for space heating and cooling (simple hourly method, annex C).