1 /* Copyright (c) 2023. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_PLUGINS_BATTERY_HPP_
7 #define SIMGRID_PLUGINS_BATTERY_HPP_
11 #include <simgrid/kernel/resource/Model.hpp>
12 #include <simgrid/s4u/Activity.hpp>
13 #include <xbt/Extendable.hpp>
15 namespace simgrid::plugins {
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);
22 class BatteryModel : public kernel::resource::Model {
23 std::vector<BatteryPtr> batteries_;
26 explicit BatteryModel();
28 void add_battery(BatteryPtr b);
29 void update_actions_state(double now, double delta) override;
30 double next_occurring_event(double now) override;
38 enum Flow { CHARGE, DISCHARGE };
44 enum Persistancy { PERSISTANT, ONESHOT };
47 double state_of_charge_;
49 double time_delta_ = -1;
50 std::function<void()> callback_;
51 Persistancy persistancy_;
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);
58 /** @ingroup plugin_battery
59 * @return The state of charge at which the Handler will happen.
60 * @note For Battery::Handler objects
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
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
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
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
84 Persistancy get_persistancy() { return persistancy_; }
88 static std::shared_ptr<BatteryModel> battery_model_;
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;
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_;
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;
108 explicit Battery() = default;
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();
114 double next_occurring_handler();
116 std::atomic_int_fast32_t refcount_{0};
118 friend void intrusive_ptr_release(Battery* o)
120 if (o->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
121 std::atomic_thread_fence(std::memory_order_acquire);
125 friend void intrusive_ptr_add_ref(Battery* o) { o->refcount_.fetch_add(1, std::memory_order_relaxed); }
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() const { 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);
148 } // namespace simgrid::plugins