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