Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add battery-chiller-solar example.
[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   public:
43     enum Persistancy { PERSISTANT, ONESHOT };
44
45   private:
46     double state_of_charge_;
47     Flow flow_;
48     double time_delta_ = -1;
49     std::function<void()> callback_;
50     Persistancy persistancy_;
51
52   public:
53     Handler(double state_of_charge, Flow flow, Persistancy p, std::function<void()> callback);
54     static std::shared_ptr<Handler> init(double state_of_charge, Flow flow, Persistancy p,
55                                          std::function<void()> callback);
56
57     /** @ingroup plugin_battery
58      *  @return The state of charge at which the Handler will happen.
59      *  @note For Battery::Handler objects
60      */
61     double get_state_of_charge() { return state_of_charge_; }
62     /** @ingroup plugin_battery
63      *  @return The flow in which the Handler will happen, either when the Battery is charging or discharging.
64      *  @note For Battery::Handler objects
65      */
66     Flow get_flow() { return flow_; }
67     /** @ingroup plugin_battery
68      *  @return The time delta until the Handler happen.
69      -1 means that is will never happen with the current state the Battery,
70      for instance when there is no load connected to the Battery.
71      *  @note For Battery::Handler objects
72     */
73     double get_time_delta() { return time_delta_; }
74     /** @ingroup plugin_battery
75      *  @return The callback to trigger when the Handler happen.
76      *  @note For Battery::Handler objects
77      */
78     std::function<void()> get_callback() { return callback_; }
79     /** @ingroup plugin_battery
80      *  @return true if its a recurrent Handler.
81      *  @note For Battery::Handler objects
82      */
83     Persistancy get_persistancy() { return persistancy_; }
84   };
85
86 private:
87   static std::shared_ptr<BatteryModel> battery_model_;
88
89   std::string name_;
90   double nominal_charge_power_w_;
91   double nominal_discharge_power_w_;
92   double charge_efficiency_;
93   double discharge_efficiency_;
94   double initial_capacity_wh_;
95   double energy_budget_j_;
96
97   std::map<const s4u::Host*, bool> host_loads_                      = {};
98   std::map<const std::string, std::pair<bool, double>> named_loads_ = {};
99   std::vector<std::shared_ptr<Handler>> handlers_;
100
101   double capacity_wh_;
102   double energy_stored_j_;
103   double energy_provided_j_ = 0;
104   double energy_consumed_j_ = 0;
105   double last_updated_      = 0;
106
107   explicit Battery(const std::string& name, double state_of_charge, double nominal_charge_power_w,
108                    double nominal_discharge_power_w, double charge_efficiency, double discharge_efficiency,
109                    double initial_capacity_wh, int cycles);
110   static void init_plugin();
111   void update();
112   double next_occurring_handler();
113
114   std::atomic_int_fast32_t refcount_{0};
115 #ifndef DOXYGEN
116   friend void intrusive_ptr_release(Battery* o)
117   {
118     if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
119       std::atomic_thread_fence(std::memory_order_acquire);
120       delete o;
121     }
122   }
123   friend void intrusive_ptr_add_ref(Battery* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
124 #endif
125
126 public:
127   static BatteryPtr init(const std::string& name, double state_of_charge, double nominal_charge_power_w,
128                          double nominal_discharge_power_w, double charge_efficiency, double discharge_efficiency,
129                          double initial_capacity_wh, int cycles);
130   void set_load(const std::string& name, double power_w);
131   void set_load(const std::string& name, bool active);
132   void connect_host(s4u::Host* host, bool active = true);
133   double get_state_of_charge();
134   double get_state_of_health();
135   double get_capacity();
136   double get_energy_provided();
137   double get_energy_consumed();
138   double get_energy_stored(std::string unit = "J");
139   std::shared_ptr<Handler> schedule_handler(double state_of_charge, Flow flow, Handler::Persistancy p,
140                                             std::function<void()> callback);
141   std::vector<std::shared_ptr<Handler>> get_handlers();
142   void delete_handler(std::shared_ptr<Handler> handler);
143 };
144 } // namespace simgrid::plugins
145 #endif