Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / plugins / chiller.cpp
1 /* Copyright (c) 2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5 #include <simgrid/Exception.hpp>
6 #include <simgrid/plugins/chiller.hpp>
7 #include <simgrid/plugins/energy.h>
8 #include <simgrid/simix.hpp>
9 #include <xbt/asserts.h>
10 #include <xbt/log.h>
11
12 #include "src/kernel/resource/CpuImpl.hpp"
13 #include "src/simgrid/module.hpp"
14
15 SIMGRID_REGISTER_PLUGIN(chiller, "Chiller management", nullptr)
16
17 /** @defgroup plugin_chiller Plugin Chiller
18
19   @beginrst
20
21 This is the chiller plugin, enabling management of chillers.
22
23 Chiller
24 .......
25
26 A chiller is placed inside a room with several machines. The role of the chiller is to keep the temperature of the room
27 below a threshold. This plugin and its equations are based on the paper "Co-simulation of FMUs and Distributed
28 Applications with SimGrid" by Camus et al. (https://hal.science/hal-01762540).
29
30 The heat generated inside the room :math:`Q_{room}` depends on the heat from the machines :math:`Q_{machines}` and
31 from the heat of the other devices, such as lighing, accounted using a factor :math:`\alpha` such as:
32
33 .. math::
34
35   Q_{room} = (1 + \alpha) \times Q_{machines}
36
37 This energy heats the input temperature :math:`T_{in}` and gives an output temperature :math:`T_{out}` based on the
38 mass of air inside the room :math:`m_{air}` and its specific heat :math:`C_{p}`:
39
40 .. math::
41
42   T_{out} = T_{in} + {Q_{room} \over m_{air} \times C_{p}}
43
44 If the output temperature is above the goal temperature :math:`T_{goal}` the chiller compensates the excessive heat
45 using electrical energy :math:`Q_{cooling}` depending on its cooling efficiency :math:`\eta_{cooling}` :
46
47 .. math::
48
49   Q_{cooling} = (T_{out} - T_{goal}) \times m_{air} \times C_{p} / \eta_{cooling}
50
51 The chiller has a power threshold that cannot be exceeded. If the power needed is above this threshold, or if the
52 chiller is not active, the temperature of the room increases.
53
54   @endrst
55  */
56
57 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(Chiller, kernel, "Logging specific to the solar panel plugin");
58
59 namespace simgrid::plugins {
60 xbt::signal<void(Chiller*)> Chiller::on_power_change; // initialisation of static field
61
62 /* ChillerModel */
63
64 ChillerModel::ChillerModel() : Model("ChillerModel") {}
65
66 void ChillerModel::add_chiller(ChillerPtr c)
67 {
68   chillers_.push_back(c);
69 }
70
71 void ChillerModel::update_actions_state(double now, double delta)
72 {
73   for (auto chiller : chillers_)
74     chiller->update();
75 }
76
77 double ChillerModel::next_occurring_event(double now)
78 {
79   static bool init = false;
80   if (not init) {
81     init = true;
82     return 0;
83   } else
84     return -1;
85 }
86
87 /* Chiller */
88
89 std::shared_ptr<ChillerModel> Chiller::chiller_model_;
90
91 void Chiller::init_plugin()
92 {
93   auto model = std::make_shared<ChillerModel>();
94   simgrid::s4u::Engine::get_instance()->add_model(model);
95   Chiller::chiller_model_ = model;
96 }
97
98 void Chiller::update()
99 {
100   simgrid::kernel::actor::simcall_answered([this] {
101     double now          = s4u::Engine::get_clock();
102     double time_delta_s = now - last_updated_;
103
104     if (time_delta_s <= 0)
105       return;
106
107     double hosts_power_w = 0;
108     for (auto const& host : hosts_) {
109       hosts_power_w += sg_host_get_current_consumption(host);
110     }
111
112     double heat_generated_j = hosts_power_w * (1 + alpha_) * time_delta_s;
113     temp_out_c_             = temp_in_c_ + heat_generated_j / (air_mass_kg_ * specific_heat_j_per_kg_per_c_);
114     double cooling_demand_w =
115         std::max(temp_out_c_ - goal_temp_c_, 0.0) * air_mass_kg_ * specific_heat_j_per_kg_per_c_ / time_delta_s;
116     if (not active_)
117       power_w_ = 0;
118     else
119       power_w_ = std::min(max_power_w_, cooling_demand_w / cooling_efficiency_);
120     temp_in_c_ =
121         temp_out_c_ - (power_w_ * time_delta_s * cooling_efficiency_) / (air_mass_kg_ * specific_heat_j_per_kg_per_c_);
122     energy_consumed_j_ += power_w_ * time_delta_s;
123     last_updated_ = now;
124   });
125 }
126
127 Chiller::Chiller(const std::string& name, double air_mass_kg, double specific_heat_j_per_kg_per_c, double alpha,
128                  double cooling_efficiency, double initial_temp_c, double goal_temp_c, double max_power_w)
129     : name_(name)
130     , air_mass_kg_(air_mass_kg)
131     , specific_heat_j_per_kg_per_c_(specific_heat_j_per_kg_per_c)
132     , alpha_(alpha)
133     , cooling_efficiency_(cooling_efficiency)
134     , temp_in_c_(initial_temp_c)
135     , temp_out_c_(initial_temp_c)
136     , goal_temp_c_(goal_temp_c)
137     , max_power_w_(max_power_w)
138 {
139   xbt_assert(air_mass_kg > 0, ": air mass must be > 0 (provided: %f)", air_mass_kg);
140   xbt_assert(specific_heat_j_per_kg_per_c > 0, ": specific heat must be > 0 (provided: %f)",
141              specific_heat_j_per_kg_per_c);
142   xbt_assert(alpha >= 0, ": alpha must be >= 0 (provided: %f)", alpha);
143   xbt_assert(cooling_efficiency >= 0 and cooling_efficiency <= 1,
144              ": cooling efficiency must be in [0,1] (provided: %f)", cooling_efficiency);
145   xbt_assert(max_power_w >= 0, ": maximal power must be >=0 (provided: %f)", max_power_w);
146 }
147
148 /** @ingroup plugin_chiller
149  *  @param name The name of the Chiller.
150  *  @param air_mass_kg The air mass of the room managed by the Chiller in kg (> 0).
151  *  @param specific_heat_j_per_kg_per_c The specific heat of air in J per kg per °C (> 0).
152  *  @param alpha The ratio of the other devices in the total heat dissipation (e.g. lighting, Power Distribution Unit)
153  * (>= 0).
154  *  @param cooling_efficiency The cooling efficiency of the Chiller [0, 1].
155  *  @param initial_temp_c The initial temperature of the room managed by the Chiller.
156  *  @param goal_temp_c The goal temperature of the room. The Chiller is idle below this temperature.
157  *  @param max_power_w The maximal power delivered by the Chiller in W (> 0). If this power is reached the room
158  * temperature will raise above the goal temperature.
159  *  @return A ChillerPtr pointing to the new Chiller.
160  */
161 ChillerPtr Chiller::init(const std::string& name, double air_mass_kg, double specific_heat_j_per_kg_per_c, double alpha,
162                          double cooling_efficiency, double initial_temp_c, double goal_temp_c, double max_power_w)
163 {
164   static bool plugin_inited = false;
165   if (not plugin_inited) {
166     init_plugin();
167     plugin_inited = true;
168   }
169   auto chiller = ChillerPtr(new Chiller(name, air_mass_kg, specific_heat_j_per_kg_per_c, alpha, cooling_efficiency,
170                                         initial_temp_c, goal_temp_c, max_power_w));
171   chiller_model_->add_chiller(chiller);
172   return chiller;
173 }
174
175 /** @ingroup plugin_chiller
176  *  @param name The new name of the Chiller.
177  *  @return A ChillerPtr pointing to the modified Chiller.
178  */
179 ChillerPtr Chiller::set_name(std::string name)
180 {
181   simgrid::kernel::actor::simcall_answered([this, name] { name_ = name; });
182   return this;
183 }
184
185 /** @ingroup plugin_chiller
186  *  @param air_mass_kg The new air mass of the Chiller in kg.
187  *  @return A ChillerPtr pointing to the modified Chiller.
188  */
189 ChillerPtr Chiller::set_air_mass(double air_mass_kg)
190 {
191   xbt_assert(air_mass_kg > 0, ": air mass must be > 0 (provided: %f)", air_mass_kg);
192   simgrid::kernel::actor::simcall_answered([this, air_mass_kg] { air_mass_kg_ = air_mass_kg; });
193   return this;
194 }
195
196 /** @ingroup plugin_chiller
197  *  @param specific_heat_j_per_kg_per_c The specific heat of the Chiller in J per kg per °C.
198  *  @return A ChillerPtr pointing to the modified Chiller.
199  */
200 ChillerPtr Chiller::set_specific_heat(double specific_heat_j_per_kg_per_c)
201 {
202   xbt_assert(specific_heat_j_per_kg_per_c > 0, ": specific heat must be > 0 (provided: %f)",
203              specific_heat_j_per_kg_per_c);
204   simgrid::kernel::actor::simcall_answered(
205       [this, specific_heat_j_per_kg_per_c] { specific_heat_j_per_kg_per_c_ = specific_heat_j_per_kg_per_c; });
206   return this;
207 }
208
209 /** @ingroup plugin_chiller
210  *  @param alpha The new alpha of the Chiller.
211  *  @return A ChillerPtr pointing to the modified Chiller.
212  */
213 ChillerPtr Chiller::set_alpha(double alpha)
214 {
215   xbt_assert(alpha >= 0, ": alpha must be >= 0 (provided: %f)", alpha);
216   simgrid::kernel::actor::simcall_answered([this, alpha] { alpha_ = alpha; });
217   return this;
218 }
219
220 /** @ingroup plugin_chiller
221  *  @param cooling_efficiency The new coolingefficiency of the Chiller.
222  *  @return A ChillerPtr pointing to the modified Chiller.
223  */
224 ChillerPtr Chiller::set_cooling_efficiency(double cooling_efficiency)
225 {
226   xbt_assert(cooling_efficiency >= 0 and cooling_efficiency <= 1,
227              ": cooling efficiency must be in [0,1] (provided: %f)", cooling_efficiency);
228   simgrid::kernel::actor::simcall_answered([this, cooling_efficiency] { cooling_efficiency_ = cooling_efficiency; });
229   return this;
230 }
231
232 /** @ingroup plugin_chiller
233  *  @param goal_temp_c The new goal temperature of the Chiller in °C.
234  *  @return A ChillerPtr pointing to the modified Chiller.
235  */
236 ChillerPtr Chiller::set_goal_temp(double goal_temp_c)
237 {
238   simgrid::kernel::actor::simcall_answered([this, goal_temp_c] { goal_temp_c_ = goal_temp_c; });
239   return this;
240 }
241
242 /** @ingroup plugin_chiller
243  *  @param max_power_w The new maximal power of the Chiller in W.
244  *  @return A ChillerPtr pointing to the modified Chiller.
245  */
246 ChillerPtr Chiller::set_max_power(double max_power_w)
247 {
248   xbt_assert(max_power_w >= 0, ": maximal power must be >=0 (provided: %f)", max_power_w);
249   simgrid::kernel::actor::simcall_answered([this, max_power_w] { max_power_w_ = max_power_w; });
250   return this;
251 }
252
253 /** @ingroup plugin_chiller
254  *  @param active The new active status of the Chiller.
255  *  @return A ChillerPtr pointing to the modified Chiller.
256  */
257 ChillerPtr Chiller::set_active(bool active)
258 {
259   simgrid::kernel::actor::simcall_answered([this, active] { active_ = active; });
260   return this;
261 }
262
263 /** @ingroup plugin_chiller
264  *  @param host The host to add to the room managed by the Chiller.
265  *  @return A ChillerPtr pointing to the modified Chiller.
266  */
267 ChillerPtr Chiller::add_host(s4u::Host* host)
268 {
269   simgrid::kernel::actor::simcall_answered([this, host] { hosts_.insert(host); });
270   return this;
271 }
272
273 /** @ingroup plugin_chiller
274  *  @param host The host to remove from the room managed by the Chiller.
275  *  @return A ChillerPtr pointing to the modified Chiller.
276  */
277 ChillerPtr Chiller::remove_host(s4u::Host* host)
278 {
279   simgrid::kernel::actor::simcall_answered([this, host] { hosts_.erase(host); });
280   return this;
281 }
282
283 /** @ingroup plugin_chiller
284  *  @return The time to reach to goal temp, assuming that the system remain in the same state.
285  */
286 double Chiller::get_time_to_goal_temp() const
287 {
288   if (goal_temp_c_ == temp_in_c_)
289     return 0;
290
291   double heat_power_w = 0;
292   for (auto const& host : hosts_)
293     heat_power_w += sg_host_get_current_consumption(host);
294   heat_power_w = heat_power_w * (1 + alpha_);
295
296   if (temp_in_c_ < goal_temp_c_)
297     return air_mass_kg_ * (goal_temp_c_ - temp_in_c_) * specific_heat_j_per_kg_per_c_ / heat_power_w;
298
299   if (not active_)
300     return -1;
301   else
302     return air_mass_kg_ * (temp_in_c_ - goal_temp_c_) * specific_heat_j_per_kg_per_c_ /
303            (power_w_ * cooling_efficiency_ - heat_power_w);
304 }
305 } // namespace simgrid::plugins