Coverage for examples/OneRoom_SimpleMPC/mpc_and_sim/simple_model.py: 100%
17 statements
« prev ^ index » next coverage.py v7.4.4, created at 2025-09-19 15:08 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2025-09-19 15:08 +0000
1from agentlib_mpc.models.casadi_model import (
2 CasadiModel,
3 CasadiInput,
4 CasadiState,
5 CasadiParameter,
6 CasadiOutput,
7 CasadiModelConfig,
8)
9from math import inf
12class BaselineMPCModelConfig(CasadiModelConfig):
13 inputs: list[CasadiInput] = [
14 # controls
15 CasadiInput(
16 name="mDot", value=0.0225, unit="kg/s", description="Air mass flow into zone"
17 ),
18 # disturbances
19 CasadiInput(
20 name="load", value=150, unit="W", description="Heat " "load into zone"
21 ),
22 CasadiInput(
23 name="T_in", value=280.15, unit="K", description="Inflow air temperature"
24 ),
25 # settings
26 CasadiInput(
27 name="T_upper",
28 value=294.15,
29 unit="K",
30 description="Upper boundary (soft) for T.",
31 ),
32 CasadiInput(
33 name="T_lower",
34 value=292.15,
35 unit="K",
36 description="Upper boundary (soft) for T.",
37 ),
39 ]
41 states: list[CasadiState] = [
42 # differential
43 CasadiState(
44 name="T", value=293.15, unit="K", description="Temperature of zone"
45 ),
46 # algebraic
47 # slack variables
48 CasadiState(
49 name="T_slack",
50 value=0,
51 unit="K",
52 description="Slack variable of temperature of zone",
53 ),
55 ]
57 parameters: list[CasadiParameter] = [
58 CasadiParameter(
59 name="cp",
60 value=1000,
61 unit="J/kg*K",
62 description="thermal capacity of the air",
63 ),
64 CasadiParameter(
65 name="C", value=100000, unit="J/K", description="thermal capacity of zone"
66 ),
67 CasadiParameter(
68 name="s_T",
69 value=1,
70 unit="-",
71 description="Weight for T in constraint function",
72 ),
73 CasadiParameter(
74 name="r_mDot",
75 value=1,
76 unit="-",
77 description="Weight for mDot in objective function",
78 ),
80 ]
81 outputs: list[CasadiOutput] = [
82 CasadiOutput(name="T_out", unit="K", description="Temperature of zone"),
83 CasadiOutput(name="E_out", unit="kWh", description="Stored energy in the zone w.r.t. 0K"),
84 CasadiOutput(
85 name="P_el",
86 unit="W",
87 description="The power input to the system",
88 ),
89 ]
92class BaselineMPCModel(CasadiModel):
93 config: BaselineMPCModelConfig
95 def setup_system(self):
96 # Define ode
97 self.T.ode = (
98 self.cp * self.mDot / self.C * (self.T_in - self.T) + self.load / self.C
99 )
101 # Define ae
102 self.P_el.alg = self.cp * self.mDot * (self.T - self.T_in) / 1000
103 self.T_out.alg = self.T # math operation to get the symbolic variable
104 self.E_out.alg = - self.T * self.C / (3600*1000) # stored electrical energy in kWh
106 # Constraints: list[(lower bound, function, upper bound)]
107 self.constraints = [
108 # soft constraints
109 (self.T_lower, self.T + self.T_slack, inf),
110 (-inf, self.T - self.T_slack, self.T_upper),
111 (0, self.T_slack, inf)
112 ]
114 # Objective function
115 objective = sum(
116 [
117 self.r_mDot * self.mDot,
118 self.s_T * self.T_slack**2,
119 ]
120 )
121 return objective