Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
abf999513d38b0db6af07aa2df968f4b33124889
[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 /* SolarPanel */
47
48 void SolarPanel::update()
49 {
50   simgrid::kernel::actor::simcall_answered([this] {
51     double power_w = conversion_efficiency_ * area_m2_ * solar_irradiance_w_per_m2_;
52     if (power_w < min_power_w_)
53       power_w = 0;
54     if (power_w > max_power_w_)
55       power_w = max_power_w_;
56     auto previous_power_w = power_w_;
57     power_w_              = power_w;
58     if (previous_power_w != power_w_) {
59       on_this_power_change(this);
60       on_power_change(this);
61     }
62   });
63 }
64
65 SolarPanel::SolarPanel(std::string name, double area_m2, double conversion_efficiency, double solar_irradiance_w_per_m2,
66                        double min_power_w, double max_power_w)
67     : name_(name)
68     , area_m2_(area_m2)
69     , conversion_efficiency_(conversion_efficiency)
70     , solar_irradiance_w_per_m2_(solar_irradiance_w_per_m2)
71     , min_power_w_(min_power_w)
72     , max_power_w_(max_power_w)
73 {
74   xbt_assert(area_m2 >= 0, " : area must be >= 0 (provided: %f)", area_m2);
75   xbt_assert(conversion_efficiency >= 0 and conversion_efficiency <= 1,
76              " : conversion efficiency must be in [0,1] (provided: %f)", conversion_efficiency);
77   xbt_assert(solar_irradiance_w_per_m2 >= 0, " : solar irradiance must be >= 0 (provided: %f)",
78              solar_irradiance_w_per_m2);
79   xbt_assert(min_power_w >= 0, " : minimal power must be >= 0 (provided: %f)", min_power_w);
80   xbt_assert(max_power_w > 0, " : maximal power must be > 0 (provided: %f)", max_power_w);
81   xbt_assert(max_power_w > min_power_w, " : maximal power must be above minimal power (provided: %f, %f)", max_power_w,
82              min_power_w);
83 }
84
85 /** @ingroup plugin_solar_panel
86  *  @param name The name of the Solar Panel.
87  *  @param area_m2 The area of the Solar Panel in m² (> 0).
88  *  @param conversion_efficiency The conversion efficiency of the Solar Panel [0,1].
89  *  @param solar_irradiance_w_per_m2 The solar irradiance of the Solar Panel in W/m² (> 0).
90  *  @param min_power_w The minimal power delivered by the Solar Panel in W (> 0 and < max_power_w).
91  *  @param max_power_w The maximal power delivered by the Solar Panel in W (> 0 and > min_power_w).
92  *  @return A SolarPanelPtr pointing to the new SolarPanel.
93  */
94 SolarPanelPtr SolarPanel::init(const std::string& name, double area_m2, double conversion_efficiency,
95                                double solar_irradiance_w_per_m2, double min_power_w, double max_power_w)
96 {
97   auto solar_panel = SolarPanelPtr(
98       new SolarPanel(name, area_m2, conversion_efficiency, solar_irradiance_w_per_m2, min_power_w, max_power_w));
99   solar_panel->update();
100   return solar_panel;
101 }
102
103 /** @ingroup plugin_solar_panel
104  *  @param name The new name of the Solar Panel.
105  *  @return A SolarPanelPtr pointing to the modified SolarPanel.
106  */
107 SolarPanelPtr SolarPanel::set_name(std::string name)
108 {
109   kernel::actor::simcall_answered([this, name] { name_ = name; });
110   return this;
111 }
112
113 /** @ingroup plugin_solar_panel
114  *  @param area_m2 The new area of the Solar Panel in m².
115  *  @return A SolarPanelPtr pointing to the modified SolarPanel.
116  */
117 SolarPanelPtr SolarPanel::set_area(double area_m2)
118 {
119   xbt_assert(area_m2 >= 0, " : area must be > 0 (provided: %f)", area_m2);
120   kernel::actor::simcall_answered([this, area_m2] { area_m2_ = area_m2; });
121   update();
122   return this;
123 }
124
125 /** @ingroup plugin_solar_panel
126  *  @param e The new convesion efficiency of the Solar Panel.
127  *  @return A SolarPanelPtr pointing to the modified SolarPanel.
128  */
129 SolarPanelPtr SolarPanel::set_conversion_efficiency(double e)
130 {
131   xbt_assert(e >= 0 and e <= 1, " : conversion efficiency must be in [0,1] (provided: %f)", e);
132   kernel::actor::simcall_answered([this, e] { conversion_efficiency_ = e; });
133   update();
134   return this;
135 }
136
137 /** @ingroup plugin_solar_panel
138  *  @param solar_irradiance_w_per_m2 The new solar irradiance of the Solar Panel in W/m².
139  *  @return A SolarPanelPtr pointing to the modified SolarPanel.
140  */
141 SolarPanelPtr SolarPanel::set_solar_irradiance(double solar_irradiance_w_per_m2)
142 {
143   xbt_assert(solar_irradiance_w_per_m2 >= 0, " : solar irradiance must be >= 0 (provided: %f)",
144              solar_irradiance_w_per_m2);
145   kernel::actor::simcall_answered(
146       [this, solar_irradiance_w_per_m2] { solar_irradiance_w_per_m2_ = solar_irradiance_w_per_m2; });
147   update();
148   return this;
149 }
150
151 /** @ingroup plugin_solar_panel
152  *  @param power_w The new minimal power of the Solar Panel in W.
153  *  @return A SolarPanelPtr pointing to the modified SolarPanel.
154  */
155 SolarPanelPtr SolarPanel::set_min_power(double power_w)
156 {
157   xbt_assert(power_w >= 0, " : minimal power must be >= 0 (provided: %f)", power_w);
158   xbt_assert(max_power_w_ > power_w, " : maximal power must be above minimal power (provided: %f, max: %f)", power_w,
159              max_power_w_);
160   kernel::actor::simcall_answered([this, power_w] { min_power_w_ = power_w; });
161   update();
162   return this;
163 }
164
165 /** @ingroup plugin_solar_panel
166  *  @param power_w The new maximal power of the Solar Panel in W.
167  *  @return A SolarPanelPtr pointing to the modified SolarPanel.
168  */
169 SolarPanelPtr SolarPanel::set_max_power(double power_w)
170 {
171   xbt_assert(power_w > 0, " : maximal power must be > 0 (provided: %f)", power_w);
172   xbt_assert(min_power_w_ < power_w, " : maximal power must be above minimal power (provided: %f, min: %f)", power_w,
173              min_power_w_);
174   kernel::actor::simcall_answered([this, power_w] { max_power_w_ = power_w; });
175   update();
176   return this;
177 }
178
179 } // namespace simgrid::plugins