-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_bangbang_RC.py
More file actions
49 lines (38 loc) · 1.53 KB
/
example_bangbang_RC.py
File metadata and controls
49 lines (38 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from src.thermal_sim.thermal_sim import ThermalMass, ThermalResistance, ThermalSystem, PowerSource, Solver
if __name__ == "__main__":
# Simulate a paralell connected thermal mass and leakage resistance controlled with a bang bang controller
T0 = 17
GND = ThermalMass.get_ground(T=T0) # non zero "ambient" temperature
# Create elements
floor_name = "Floor"
m_floor = ThermalMass(C=1440 * 440, T_initial=T0, name=floor_name)
R_floor_gnd = ThermalResistance(R=0.01)
def bangbang(t: float, prev_Q: float, system: ThermalSystem) -> float:
# Ugly way to get the floor mass/temperature
for mass in system.thermal_masses:
if mass.__name__ == floor_name:
floor = mass
# Bang Bang controller on the floor temperature
P = 1000
T_hyst = 2
T_ref = 20
T_max = T_ref + T_hyst
T_min = T_ref - T_hyst
prev_output = 1 if prev_Q > 0 else 0
if prev_output and floor.T > T_max:
output = 0
elif not prev_output and floor.T < T_min:
output = 1
else:
output = prev_output
return P*output
heat_source = PowerSource(bangbang)
# Connect elements
m_floor.connect(heat_source)
m_floor.connect(R_floor_gnd, GND)
# Simulate
system = ThermalSystem(m_floor)
system.generate_diagram('img/bangbang_RC.png')
dt = 60 # [s]
total_time = 1 * 24 * 3600 # [s]
system.simulate(dt, total_time, solver=Solver.RK4) # Use e.g a more accurate but slower solver