Logo AND Algorithmique Numérique Distribuée

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