Coverage for tutorials/ngsi_v2/simulation_model.py: 0%

30 statements  

« prev     ^ index     » next       coverage.py v7.10.2, created at 2025-08-05 11:07 +0000

1""" 

2Simulation model to provide dynamic data throughout the tutorial 

3""" 

4 

5from math import cos 

6import numpy as np 

7 

8 

9class SimulationModel: 

10 """ 

11 Simulation model for a thermal zone and periodic ambient temperature 

12 simulation. 

13 

14 The ambient temperature is simulated as daily periodic cosine ranging 

15 between `temp_max` and `temp_min`. 

16 

17 The thermal zone is roughly parametrized as follows: 

18 Zone volume: 10m x 10m x 5m 

19 Outer wall area plus roof ares: 4 x 10m x 5m + 10m x 10m 

20 Thermal insulation factor U: 0.4 W/(m^2 K) 

21 

22 Args: 

23 t_start: simulation start time in seconds 

24 t_end: simulation end time in seconds 

25 dt: model integration step in seconds 

26 temp_max: maximal ambient temperature in °C 

27 temp_min: minimal ambient temperature in °C 

28 temp_start: initial zone temperature in °C 

29 """ 

30 

31 def __init__( 

32 self, 

33 t_start: int = 0, 

34 t_end: int = 24 * 60 * 60, 

35 dt: int = 1, 

36 temp_max: float = 10, 

37 temp_min: float = -5, 

38 temp_start: float = 20, 

39 ): 

40 

41 self.t_start = t_start 

42 self.t_end = t_end 

43 self.dt = dt 

44 self.temp_max = temp_max 

45 self.temp_min = temp_min 

46 self.temp_start = temp_start 

47 self.ua = 120 

48 self.c_p = 612.5 * 1000 

49 self.q_h = 3000 

50 self.t_sim = self.t_start 

51 self.t_amb = temp_min 

52 self.t_zone = temp_start 

53 self.on_off: bool = False 

54 

55 # define the function that returns a virtual ambient temperature depend from the 

56 # the simulation time using cosinus function 

57 def do_step(self, t_sim: int): 

58 """ 

59 Performs a simulation step of length `t_sim` 

60 

61 Args: 

62 t_sim: simulation step in seconds 

63 

64 Returns: 

65 t_sim: simulation step end time in °C 

66 t_amb: ambient temperature in °C 

67 t_zone: zone temperature in °C 

68 """ 

69 for t in range(self.t_sim, t_sim, self.dt): 

70 self.t_zone = ( 

71 self.t_zone 

72 + self.dt 

73 * (self.ua * (self.t_amb - self.t_zone) + self.on_off * self.q_h) 

74 / self.c_p 

75 ) 

76 

77 self.t_amb = ( 

78 -(self.temp_max - self.temp_min) 

79 / 2 

80 * cos(2 * np.pi * t / (24 * 60 * 60)) 

81 + self.temp_min 

82 + (self.temp_max - self.temp_min) / 2 

83 ) 

84 

85 self.t_sim = t_sim 

86 

87 return self.t_sim, self.t_amb, self, self.t_zone 

88 

89 @property 

90 def heater_on(self): 

91 """ 

92 Returns heater state 

93 """ 

94 return self.on_off 

95 

96 @heater_on.setter 

97 def heater_on(self, switch: bool): 

98 """ 

99 Sets heater state 

100 

101 Args: 

102 switch: heater state `True` for on and `False` for off 

103 """ 

104 switch = bool(switch) 

105 self.on_off = switch