Coverage for examples/controller/bangbang_with_simulator.py: 78%

40 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-10-30 13:39 +0000

1import logging 

2from typing import List 

3 

4import agentlib as ag 

5 

6 

7class HeatedRoomConfig(ag.ModelConfig): 

8 inputs: List[ag.ModelInput] = [ 

9 ag.ModelInput(name="heating_power_in_watt", value=100) 

10 ] 

11 states: List[ag.ModelState] = [ 

12 ag.ModelState(name="temperature_in_celsius", value=20) 

13 ] 

14 parameters: List[ag.ModelParameter] = [ 

15 ag.ModelParameter(name="heat_loss_in_watt", value=150), 

16 ag.ModelParameter(name="thermal_capacity_zone", value=100_000), 

17 ] 

18 

19 

20class HeatedRoom(ag.Model): 

21 config: HeatedRoomConfig 

22 

23 def initialize(self, **kwargs): 

24 pass 

25 

26 def do_step(self, *, t_start, t_sample): 

27 t = self.get("temperature_in_celsius").value 

28 power = self.get("heating_power_in_watt").value 

29 loss = self.get("heat_loss_in_watt").value 

30 capacity = self.get("thermal_capacity_zone").value 

31 t = t + ((power - loss) / capacity) * t_sample 

32 self.set("temperature_in_celsius", t) 

33 

34 

35pid_agent_config = { 

36 "id": "PID", 

37 "modules": { 

38 "myBangbang": { 

39 "type": "bangbang", 

40 "gain": 600, 

41 "lb": 20, 

42 "ub": 22, 

43 "input": {"name": "u", "value": 0, "alias": "room_temp"}, 

44 "output": { 

45 "name": "y", 

46 "value": 0, 

47 "alias": "heating_power", 

48 "shared": True, 

49 }, 

50 }, 

51 "myLogger": {"type": "AgentLogger"}, 

52 "myComm": {"type": "local", "subscriptions": ["Process"]}, 

53 }, 

54} 

55 

56process_agent_config = { 

57 "id": "Process", 

58 "modules": [ 

59 { 

60 "module_id": "sim", 

61 "type": "simulator", 

62 "model": {"type": {"file": __file__, "class_name": "HeatedRoom"}}, 

63 "t_sample": 10, 

64 "inputs": [ 

65 {"name": "heating_power_in_watt", "value": 0, "alias": "heating_power"} 

66 ], 

67 "states": [ 

68 { 

69 "name": "temperature_in_celsius", 

70 "value": 21, 

71 "shared": True, 

72 "alias": "room_temp", 

73 } 

74 ], 

75 }, 

76 {"module_id": "myLogger", "type": "AgentLogger"}, 

77 {"module_id": "myComm", "type": "local", "subscriptions": ["PID"]}, 

78 ], 

79} 

80 

81 

82def run_example(with_plots=True, log_level=logging.INFO): 

83 # Set the log-level 

84 logging.basicConfig(level=log_level) 

85 

86 env_config = {"rt": False, "factor": 0.01, "clock": True, "t_sample": 60} 

87 env = ag.Environment(config=env_config) 

88 pid_agent = ag.Agent(config=pid_agent_config, env=env) 

89 process_agent = ag.Agent(config=process_agent_config, env=env) 

90 env.run(until=1000) 

91 

92 results = process_agent.get_results() 

93 res = results["myLogger"] 

94 if with_plots: 

95 import matplotlib.pyplot as plt 

96 

97 fig, ax = plt.subplots(2, 1) 

98 res["room_temp"].plot(ax=ax[0], label="$T_{room}$") 

99 ax[0].axhline(20, label="_", linestyle="--", color="black") 

100 ax[0].axhline(22, label="_", linestyle="--", color="black") 

101 

102 res["heating_power"].plot(ax=ax[1], label="$\dot{Q}_{heat}$") 

103 ax[0].legend() 

104 ax[1].legend() 

105 plt.show() 

106 return {"PID_1": results} 

107 

108 

109if __name__ == "__main__": 

110 run_example(log_level="INFO")