Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / plugins / solar_panel.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/solar_panel.hpp>
7 #include <simgrid/simix.hpp>
8 #include <xbt/asserts.h>
9 #include <xbt/log.h>
10
11 #include "src/kernel/resource/CpuImpl.hpp"
12 #include "src/simgrid/module.hpp"
13
14 SIMGRID_REGISTER_PLUGIN(solar_panel, "Solar Panel management", nullptr)
15
16 /** @defgroup plugin_solar_panel Plugin Solar Panel
17
18   @beginrst
19
20 This is the solar panel plugin, enabling management of solar panels on hosts.
21
22 This plugin allows the use of solar panels to generate power during simulation depending on size, solar
23 irradiance and conversion factor.
24
25 The power model is taken from the paper `"Reinforcement Learning Based Load Balancing for
26 Geographically Distributed Data Centres" <https://dro.dur.ac.uk/33395/1/33395.pdf?DDD280+kkgc95+vbdv77>`_ by Max Mackie
27 et. al.
28
29 Solar Panel
30 ....................
31
32 A solar panel has an area :math:`A` in m², a conversion efficiency :math:`\eta` and a solar irradiance :math:`S` in
33 W/m². The power generated :math:`P` in W by a solar panel is given by the following equation:
34
35 .. math::
36
37   P = A \times \eta \times S
38
39   @endrst
40  */
41
42 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(SolarPanel, kernel, "Logging specific to the solar panel plugin");
43
44 namespace simgrid::plugins {
45
46 xbt::signal<void(SolarPanel*)> SolarPanel::on_power_change;
47
48 /* SolarPanel */
49
50 void SolarPanel::update()
51 {
52   simgrid::kernel::actor::simcall_answered([this] {
53     double power_w = conversion_efficiency_ * area_m2_ * solar_irradiance_w_per_m2_;
54     if (power_w < min_power_w_)
55       power_w = 0;
56     if (power_w > max_power_w_)
57       power_w = max_power_w_;
58     auto previous_power_w = power_w_;
59     power_w_              = power_w;
60     if (previous_power_w != power_w_) {
61       on_this_power_change(this);
62       on_power_change(this);
63     }
64   });
65 }
66
67 SolarPanel::SolarPanel(std::string name, double area_m2, double conversion_efficiency, double solar_irradiance_w_per_m2,
68                        double min_power_w, double max_power_w)
69     : name_(name)
70     , area_m2_(area_m2)
71     , conversion_efficiency_(conversion_efficiency)
72     , solar_irradiance_w_per_m2_(solar_irradiance_w_per_m2)
73     , min_power_w_(min_power_w)
74     , max_power_w_(max_power_w)
75 {
76   xbt_assert(area_m2 >= 0, " : area must be >= 0 (provided: %f)", area_m2);
77   xbt_assert(conversion_efficiency >= 0 and conversion_efficiency <= 1,
78              " : conversion efficiency must be in [0,1] (provided: %f)", conversion_efficiency);
79   xbt_assert(solar_irradiance_w_per_m2 >= 0, " : solar irradiance must be >= 0 (provided: %f)",
80              solar_irradiance_w_per_m2);
81   xbt_assert(min_power_w >= 0, " : minimal power must be >= 0 (provided: %f)", min_power_w);
82   xbt_assert(max_power_w > 0, " : maximal power must be > 0 (provided: %f)", max_power_w);
83   xbt_assert(max_power_w > min_power_w, " : maximal power must be above minimal power (provided: %f, %f)", max_power_w,
84              min_power_w);
85 }
86
87 /** @ingroup plugin_solar_panel
88  *  @param name The name of the Solar Panel.
89  *  @param area_m2 The area of the Solar Panel in m² (> 0).
90  *  @param conversion_efficiency The conversion efficiency of the Solar Panel [0,1].
91  *  @param solar_irradiance_w_per_m2 The solar irradiance of the Solar Panel in W/m² (> 0).
92  *  @param min_power_w The minimal power delivered by the Solar Panel in W (> 0 and < max_power_w).
93  *  @param max_power_w The maximal power delivered by the Solar Panel in W (> 0 and > min_power_w).
94  *  @return A SolarPanelPtr pointing to the new SolarPanel.
95  */
96 SolarPanelPtr SolarPanel::init(const std::string& name, double area_m2, double conversion_efficiency,
97                                double solar_irradiance_w_per_m2, double min_power_w, double max_power_w)
98 {
99   auto solar_panel = SolarPanelPtr(
100       new SolarPanel(name, area_m2, conversion_efficiency, solar_irradiance_w_per_m2, min_power_w, max_power_w));
101   solar_panel->update();
102   return solar_panel;
103 }
104
105 /** @ingroup plugin_solar_panel
106  *  @param name The new name of the Solar Panel.
107  *  @return A SolarPanelPtr pointing to the modified SolarPanel.
108  */
109 SolarPanelPtr SolarPanel::set_name(std::string name)
110 {
111   kernel::actor::simcall_answered([this, name] { name_ = name; });
112   return this;
113 }
114
115 /** @ingroup plugin_solar_panel
116  *  @param area_m2 The new area of the Solar Panel in m².
117  *  @return A SolarPanelPtr pointing to the modified SolarPanel.
118  */
119 SolarPanelPtr SolarPanel::set_area(double area_m2)
120 {
121   xbt_assert(area_m2 >= 0, " : area must be > 0 (provided: %f)", area_m2);
122   kernel::actor::simcall_answered([this, area_m2] { area_m2_ = area_m2; });
123   update();
124   return this;
125 }
126
127 /** @ingroup plugin_solar_panel
128  *  @param e The new convesion efficiency of the Solar Panel.
129  *  @return A SolarPanelPtr pointing to the modified SolarPanel.
130  */
131 SolarPanelPtr SolarPanel::set_conversion_efficiency(double e)
132 {
133   xbt_assert(e >= 0 and e <= 1, " : conversion efficiency must be in [0,1] (provided: %f)", e);
134   kernel::actor::simcall_answered([this, e] { conversion_efficiency_ = e; });
135   update();
136   return this;
137 }
138
139 /** @ingroup plugin_solar_panel
140  *  @param solar_irradiance_w_per_m2 The new solar irradiance of the Solar Panel in W/m².
141  *  @return A SolarPanelPtr pointing to the modified SolarPanel.
142  */
143 SolarPanelPtr SolarPanel::set_solar_irradiance(double solar_irradiance_w_per_m2)
144 {
145   xbt_assert(solar_irradiance_w_per_m2 >= 0, " : solar irradiance must be >= 0 (provided: %f)",
146              solar_irradiance_w_per_m2);
147   kernel::actor::simcall_answered(
148       [this, solar_irradiance_w_per_m2] { solar_irradiance_w_per_m2_ = solar_irradiance_w_per_m2; });
149   update();
150   return this;
151 }
152
153 /** @ingroup plugin_solar_panel
154  *  @param power_w The new minimal power of the Solar Panel in W.
155  *  @return A SolarPanelPtr pointing to the modified SolarPanel.
156  */
157 SolarPanelPtr SolarPanel::set_min_power(double power_w)
158 {
159   xbt_assert(power_w >= 0, " : minimal power must be >= 0 (provided: %f)", power_w);
160   xbt_assert(max_power_w_ > power_w, " : maximal power must be above minimal power (provided: %f, max: %f)", power_w,
161              max_power_w_);
162   kernel::actor::simcall_answered([this, power_w] { min_power_w_ = power_w; });
163   update();
164   return this;
165 }
166
167 /** @ingroup plugin_solar_panel
168  *  @param power_w The new maximal power of the Solar Panel in W.
169  *  @return A SolarPanelPtr pointing to the modified SolarPanel.
170  */
171 SolarPanelPtr SolarPanel::set_max_power(double power_w)
172 {
173   xbt_assert(power_w > 0, " : maximal power must be > 0 (provided: %f)", power_w);
174   xbt_assert(min_power_w_ < power_w, " : maximal power must be above minimal power (provided: %f, min: %f)", power_w,
175              min_power_w_);
176   kernel::actor::simcall_answered([this, power_w] { max_power_w_ = power_w; });
177   update();
178   return this;
179 }
180
181 } // namespace simgrid::plugins