Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'plugins-energy-battery-interaction' into 'master'
[simgrid.git] / include / simgrid / plugins / battery.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
6 #ifndef SIMGRID_PLUGINS_BATTERY_HPP_
7 #define SIMGRID_PLUGINS_BATTERY_HPP_
8
9 #include <memory>
10 #include <simgrid/kernel/resource/Model.hpp>
11 #include <simgrid/s4u/Activity.hpp>
12 #include <xbt/Extendable.hpp>
13
14 namespace simgrid::plugins {
15
16 class Battery;
17 using BatteryPtr = boost::intrusive_ptr<Battery>;
18 XBT_PUBLIC void intrusive_ptr_release(Battery* o);
19 XBT_PUBLIC void intrusive_ptr_add_ref(Battery* o);
20
21 class BatteryModel : public kernel::resource::Model {
22   std::vector<BatteryPtr> batteries_;
23
24 public:
25   explicit BatteryModel();
26
27   void add_battery(BatteryPtr b);
28   void update_actions_state(double now, double delta) override;
29   double next_occurring_event(double now) override;
30 };
31
32 class Battery {
33
34   friend BatteryModel;
35
36 public:
37   enum Flow { CHARGE, DISCHARGE };
38
39   class Handler {
40     friend Battery;
41
42   private:
43     double state_of_charge_;
44     Flow flow_;
45     double time_delta_ = -1;
46     std::function<void()> callback_;
47     bool repeat_;
48
49   public:
50     Handler(double state_of_charge, Flow flow, bool repeat, std::function<void()> callback);
51     static std::shared_ptr<Handler> init(double state_of_charge, Flow flow, bool repeat,
52                                          std::function<void()> callback);
53
54     /** @ingroup plugin_battery
55      *  @return The state of charge at which the Handler will happen.
56      *  @note For Battery::Handler objects
57      */
58     double get_state_of_charge() { return state_of_charge_; }
59     /** @ingroup plugin_battery
60      *  @return The flow in which the Handler will happen, either when the Battery is charging or discharging.
61      *  @note For Battery::Handler objects
62      */
63     Flow get_flow() { return flow_; }
64     /** @ingroup plugin_battery
65      *  @return The time delta until the Handler happen.
66      -1 means that is will never happen with the current state the Battery,
67      for instance when there is no load connected to the Battery.
68      *  @note For Battery::Handler objects
69     */
70     double get_time_delta() { return time_delta_; }
71     /** @ingroup plugin_battery
72      *  @return The callback to trigger when the Handler happen.
73      *  @note For Battery::Handler objects
74      */
75     std::function<void()> get_callback() { return callback_; }
76     /** @ingroup plugin_battery
77      *  @return true if its a recurrent Handler.
78      *  @note For Battery::Handler objects
79      */
80     bool get_repeat() { return repeat_; }
81   };
82
83 private:
84   static std::shared_ptr<BatteryModel> battery_model_;
85
86   std::string name_;
87   double nominal_charge_power_w_;
88   double nominal_discharge_power_w_;
89   double charge_efficiency_;
90   double discharge_efficiency_;
91   double initial_capacity_wh_;
92   double energy_budget_j_;
93
94   std::map<const s4u::Host*, bool> host_loads_     = {};
95   std::map<const std::string, double> named_loads_ = {};
96   std::vector<std::shared_ptr<Handler>> handlers_;
97
98   double capacity_wh_;
99   double energy_stored_j_;
100   double energy_provided_j_ = 0;
101   double energy_consumed_j_ = 0;
102   double last_updated_      = 0;
103
104   explicit Battery(const std::string& name, double state_of_charge, double nominal_charge_power_w,
105                    double nominal_discharge_power_w, double charge_efficiency, double discharge_efficiency,
106                    double initial_capacity_wh, int cycles);
107   static void init_plugin();
108   void update();
109   double next_occurring_handler();
110
111   std::atomic_int_fast32_t refcount_{0};
112 #ifndef DOXYGEN
113   friend void intrusive_ptr_release(Battery* o)
114   {
115     if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
116       std::atomic_thread_fence(std::memory_order_acquire);
117       delete o;
118     }
119   }
120   friend void intrusive_ptr_add_ref(Battery* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
121 #endif
122
123 public:
124   static BatteryPtr init(const std::string& name, double state_of_charge, double nominal_charge_power_w,
125                          double nominal_discharge_power_w, double charge_efficiency, double discharge_efficiency,
126                          double initial_capacity_wh, int cycles);
127   void set_load(const std::string& name, double power_w);
128   void connect_host(s4u::Host* host, bool active = true);
129   double get_state_of_charge();
130   double get_state_of_health();
131   double get_capacity();
132   double get_energy_provided();
133   double get_energy_consumed();
134   double get_energy_stored(std::string unit = "J");
135   std::shared_ptr<Handler> schedule_handler(double state_of_charge, Flow flow, bool repeat,
136                                             std::function<void()> callback);
137   std::vector<std::shared_ptr<Handler>> get_handlers();
138   void delete_handler(std::shared_ptr<Handler> handler);
139 };
140 } // namespace simgrid::plugins
141 #endif