Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix an ODR reported by ASan
[simgrid.git] / include / simgrid / plugins / solar_panel.hpp
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 #ifndef SIMGRID_PLUGINS_SOLAR_PANEL_H_
6 #define SIMGRID_PLUGINS_SOLAR_PANEL_H_
7
8 #include <simgrid/kernel/resource/Model.hpp>
9 #include <simgrid/s4u/Activity.hpp>
10 #include <xbt/Extendable.hpp>
11
12 namespace simgrid::plugins {
13
14 class SolarPanel;
15 using SolarPanelPtr = boost::intrusive_ptr<SolarPanel>;
16 XBT_PUBLIC void intrusive_ptr_release(SolarPanel* o);
17 XBT_PUBLIC void intrusive_ptr_add_ref(SolarPanel* o);
18
19 class SolarPanel {
20
21   std::string name_;
22   double area_m2_;
23   double conversion_efficiency_;
24   double solar_irradiance_w_per_m2_;
25   double min_power_w_;
26   double max_power_w_;
27   double power_w_ = -1;
28
29   explicit SolarPanel(std::string name, double area_m2, double conversion_efficiency, double solar_irradiance_w_per_m2,
30                       double min_power_w, double max_power_w);
31   void update();
32
33   std::atomic_int_fast32_t refcount_{0};
34 #ifndef DOXYGEN
35   friend void intrusive_ptr_release(SolarPanel* o)
36   {
37     if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
38       std::atomic_thread_fence(std::memory_order_acquire);
39       delete o;
40     }
41   }
42   friend void intrusive_ptr_add_ref(SolarPanel* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
43 #endif
44
45   static xbt::signal<void(SolarPanel*)> on_power_change;
46   xbt::signal<void(SolarPanel*)> on_this_power_change;
47
48 public:
49   static SolarPanelPtr init(const std::string& name, double area_m2, double conversion_efficiency,
50                             double solar_irradiance_w_per_m2, double min_power_w, double max_power_w);
51
52   SolarPanelPtr set_name(std::string name);
53   SolarPanelPtr set_area(double area_m2);
54   SolarPanelPtr set_conversion_efficiency(double e);
55   SolarPanelPtr set_solar_irradiance(double solar_irradiance_w_per_m2);
56   SolarPanelPtr set_min_power(double power_w);
57   SolarPanelPtr set_max_power(double power_w);
58
59   std::string get_name() const { return name_; }
60   const char* get_cname() const { return name_.c_str(); }
61   double get_area() const { return area_m2_; }
62   double get_conversion_efficiency() const { return conversion_efficiency_; }
63   double get_solar_irradiance() const { return solar_irradiance_w_per_m2_; }
64   double get_min_power() const { return min_power_w_; }
65   double get_max_power() const { return max_power_w_; }
66   double get_power() const { return power_w_; }
67
68   /** Add a callback fired after this solar panel power changed. */
69   void on_this_power_change_cb(const std::function<void(SolarPanel*)>& func) { on_this_power_change.connect(func); };
70   /** Add a callback fired after a solar panel power changed.
71    * Triggered after the on_this_power_change function.**/
72   static void on_power_change_cb(const std::function<void(SolarPanel*)>& cb) { on_power_change.connect(cb); }
73 };
74 } // namespace simgrid::plugins
75 #endif