battery->set_load("load", 100);
- auto event1 = battery->create_event(
- 0.2, simgrid::plugins::Battery::DISCHARGE,
- [battery]() {
- XBT_INFO("%f,%f,SoC", simgrid::s4u::Engine::get_clock(), battery->get_state_of_charge());
- XBT_INFO("%f,%f,SoH", simgrid::s4u::Engine::get_clock(), battery->get_state_of_health());
- battery->set_load("load", -100);
- },
- true);
-
- std::shared_ptr<simgrid::plugins::Battery::Event> event2;
- event2 = battery->create_event(
- 0.8, simgrid::plugins::Battery::CHARGE,
- [battery, event1, event2]() {
- XBT_INFO("%f,%f,SoC", simgrid::s4u::Engine::get_clock(), battery->get_state_of_charge());
- XBT_INFO("%f,%f,SoH", simgrid::s4u::Engine::get_clock(), battery->get_state_of_health());
- if (battery->get_state_of_health() < 0.1) {
- battery->delete_event(event1);
- battery->delete_event(event2);
- }
- battery->set_load("load", 100);
- },
- true);
+ auto handler1 = battery->schedule_handler(0.2, simgrid::plugins::Battery::DISCHARGE, true, [battery]() {
+ XBT_INFO("%f,%f,SoC", simgrid::s4u::Engine::get_clock(), battery->get_state_of_charge());
+ XBT_INFO("%f,%f,SoH", simgrid::s4u::Engine::get_clock(), battery->get_state_of_health());
+ battery->set_load("load", -100);
+ });
+
+ std::shared_ptr<simgrid::plugins::Battery::Handler> handler2;
+ handler2 = battery->schedule_handler(0.8, simgrid::plugins::Battery::CHARGE, true, [battery, handler1, handler2]() {
+ XBT_INFO("%f,%f,SoC", simgrid::s4u::Engine::get_clock(), battery->get_state_of_charge());
+ XBT_INFO("%f,%f,SoH", simgrid::s4u::Engine::get_clock(), battery->get_state_of_health());
+ if (battery->get_state_of_health() < 0.1) {
+ battery->delete_handler(handler1);
+ battery->delete_handler(handler2);
+ }
+ battery->set_load("load", 100);
+ });
}
int main(int argc, char* argv[])
auto* host2 = simgrid::s4u::Engine::get_instance()->host_by_name("MyHost2");
auto* host3 = simgrid::s4u::Engine::get_instance()->host_by_name("MyHost3");
- battery->create_event(0.2, simgrid::plugins::Battery::DISCHARGE, [battery, &host1, &host2, &host3]() {
- XBT_INFO("Event -> Battery low: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
+ battery->schedule_handler(0.2, simgrid::plugins::Battery::DISCHARGE, true, [battery, &host1, &host2, &host3]() {
+ XBT_INFO("Handler -> Battery low: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
battery->get_state_of_charge(), battery->get_state_of_health(), battery->get_energy_stored(),
battery->get_energy_provided(), battery->get_energy_consumed());
XBT_INFO("Disconnecting hosts %s and %s", host1->get_cname(), host2->get_cname());
> [MyHost1:manager:(1) 0.000000] [battery_energy/INFO] Battery state: SoC: 0.800000 SoH: 1.000000 Energy stored: 28800.000000J Energy provided: 0.000000J Energy consumed 0.000000J
> [MyHost1:manager:(1) 0.000000] [battery_energy/INFO] Connecting hosts MyHost1 and MyHost2 to the battery
> [MyHost1:manager:(1) 0.000000] [battery_energy/INFO] Host MyHost1 will now execute 1000000000.000000 flops
-> [96.209721] [battery_energy/INFO] Event -> Battery low: SoC: 0.200000 SoH: 0.999700 Energy stored: 7197.839784J Energy provided: 19441.944194J Energy consumed 0.000000J
+> [96.209721] [battery_energy/INFO] Handler -> Battery low: SoC: 0.200000 SoH: 0.999700 Energy stored: 7197.839784J Energy provided: 19441.944194J Energy consumed 0.000000J
> [96.209721] [battery_energy/INFO] Disconnecting hosts MyHost1 and MyHost2
> [96.209721] [battery_energy/INFO] Energy consumed this far by: MyHost1: 9820.972097J, MyHost2: 9620.972097J, MyHost3: 9620.972097J
> [MyHost1:manager:(1) 200.000000] [battery_energy/INFO] Battery state: SoC: 0.200000 SoH: 0.999700 Energy stored: 7197.839784J Energy provided: 19441.944194J Energy consumed 0.000000J
battery->set_load("load", load_w);
XBT_INFO("Set load to %fW", load_w);
- battery->create_event(0.2, simgrid::plugins::Battery::DISCHARGE, [battery, &load_w]() {
+ battery->schedule_handler(0.2, simgrid::plugins::Battery::DISCHARGE, true, [battery, &load_w]() {
XBT_INFO("Discharged state: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
battery->get_state_of_charge(), battery->get_state_of_health(), battery->get_energy_stored(),
battery->get_energy_provided(), battery->get_energy_consumed());
XBT_INFO("Set load to %fW", -load_w);
});
- battery->create_event(0.8, simgrid::plugins::Battery::CHARGE, [battery]() {
+ battery->schedule_handler(0.8, simgrid::plugins::Battery::CHARGE, true, [battery]() {
XBT_INFO("Charged state: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
battery->get_state_of_charge(), battery->get_state_of_health(), battery->get_energy_stored(),
battery->get_energy_provided(), battery->get_energy_consumed());
public:
enum Flow { CHARGE, DISCHARGE };
- class Event {
+ class Handler {
friend Battery;
private:
bool repeat_;
public:
- Event(double state_of_charge, Flow flow, std::function<void()> callback, bool repeat);
- static std::shared_ptr<Event> init(double state_of_charge, Flow flow, std::function<void()> callback, bool repeat);
+ Handler(double state_of_charge, Flow flow, bool repeat, std::function<void()> callback);
+ static std::shared_ptr<Handler> init(double state_of_charge, Flow flow, bool repeat,
+ std::function<void()> callback);
/** @ingroup plugin_battery
- * @return The state of charge at which the Event will happen.
- * @note For Battery::Event objects
+ * @return The state of charge at which the Handler will happen.
+ * @note For Battery::Handler objects
*/
double get_state_of_charge() { return state_of_charge_; }
/** @ingroup plugin_battery
- * @return The flow in which the Event will happen, either when the Battery is charging or discharging.
- * @note For Battery::Event objects
+ * @return The flow in which the Handler will happen, either when the Battery is charging or discharging.
+ * @note For Battery::Handler objects
*/
Flow get_flow() { return flow_; }
/** @ingroup plugin_battery
- * @return The time delta until the Event happen.
+ * @return The time delta until the Handler happen.
-1 means that is will never happen with the current state the Battery,
for instance when there is no load connected to the Battery.
- * @note For Battery::Event objects
+ * @note For Battery::Handler objects
*/
double get_time_delta() { return time_delta_; }
/** @ingroup plugin_battery
- * @return The callback to trigger when the Event happen.
- * @note For Battery::Event objects
+ * @return The callback to trigger when the Handler happen.
+ * @note For Battery::Handler objects
*/
std::function<void()> get_callback() { return callback_; }
/** @ingroup plugin_battery
- * @return true if its a recurrent Event.
- * @note For Battery::Event objects
+ * @return true if its a recurrent Handler.
+ * @note For Battery::Handler objects
*/
bool get_repeat() { return repeat_; }
};
std::map<const s4u::Host*, bool> host_loads_ = {};
std::map<const std::string, double> named_loads_ = {};
- std::vector<std::shared_ptr<Event>> events_;
+ std::vector<std::shared_ptr<Handler>> handlers_;
double capacity_wh_;
double energy_stored_j_;
double initial_capacity_wh, int cycles);
static void init_plugin();
void update();
- double next_occurring_event();
+ double next_occurring_handler();
std::atomic_int_fast32_t refcount_{0};
#ifndef DOXYGEN
double get_energy_provided();
double get_energy_consumed();
double get_energy_stored(std::string unit = "J");
- std::shared_ptr<Event> create_event(double state_of_charge, Flow flow, std::function<void()> callback,
- bool repeat = false);
- std::vector<std::shared_ptr<Event>> get_events();
- void delete_event(std::shared_ptr<Event> event);
+ std::shared_ptr<Handler> schedule_handler(double state_of_charge, Flow flow, bool repeat,
+ std::function<void()> callback);
+ std::vector<std::shared_ptr<Handler>> get_handlers();
+ void delete_handler(std::shared_ptr<Handler> handler);
};
} // namespace simgrid::plugins
#endif
\ No newline at end of file
provide energy to the battery. You can also connect hosts to a battery. Theses hosts will consume their energy from the
battery until the battery is empty or until the connection between the hosts and the battery is set inactive.
-Events
+Handlers
......
-You can create events that will happen at specific SoC of the battery and trigger a callback.
-Theses events may be recurrent, for instance you may want to always set all loads to zero and deactivate all hosts
+You can schedule handlers that will happen at specific SoC of the battery and trigger a callback.
+Theses handlers may be recurrent, for instance you may want to always set all loads to zero and deactivate all hosts
connections when the battery reaches 20% SoC.
@endrst
{
double time_delta = -1;
for (auto battery : batteries_) {
- double time_delta_battery = battery->next_occurring_event();
+ double time_delta_battery = battery->next_occurring_handler();
time_delta = time_delta == -1 or time_delta_battery < time_delta ? time_delta_battery : time_delta;
}
return time_delta;
}
-/* Event */
+/* Handler */
-Battery::Event::Event(double state_of_charge, Flow flow, std::function<void()> callback, bool repeat)
+Battery::Handler::Handler(double state_of_charge, Flow flow, bool repeat, std::function<void()> callback)
: state_of_charge_(state_of_charge), flow_(flow), callback_(callback), repeat_(repeat)
{
}
-std::shared_ptr<Battery::Event> Battery::Event::init(double state_of_charge, Flow flow, std::function<void()> callback,
- bool repeat)
+std::shared_ptr<Battery::Handler> Battery::Handler::init(double state_of_charge, Flow flow, bool repeat,
+ std::function<void()> callback)
{
- return std::make_shared<Battery::Event>(state_of_charge, flow, callback, repeat);
+ return std::make_shared<Battery::Handler>(state_of_charge, flow, repeat, callback);
}
/* Battery */
energy_stored_j_ = std::min(energy_stored_j_, 3600 * capacity_wh_);
last_updated_ = now;
- std::vector<std::shared_ptr<Event>> to_delete = {};
- for (auto event : events_) {
- if (abs(event->time_delta_ - time_delta_s) < 0.000000001) {
- event->callback_();
- if (event->repeat_)
- event->time_delta_ = -1;
+ std::vector<std::shared_ptr<Handler>> to_delete = {};
+ for (auto handler : handlers_) {
+ if (abs(handler->time_delta_ - time_delta_s) < 0.000000001) {
+ handler->callback_();
+ if (handler->repeat_)
+ handler->time_delta_ = -1;
else
- to_delete.push_back(event);
+ to_delete.push_back(handler);
}
}
- for (auto event : to_delete)
- delete_event(event);
+ for (auto handler : to_delete)
+ delete_handler(handler);
});
}
-double Battery::next_occurring_event()
+double Battery::next_occurring_handler()
{
double provided_power_w = 0;
double consumed_power_w = 0;
consumed_power_w = std::min(consumed_power_w, -nominal_charge_power_w_);
double time_delta = -1;
- for (auto& event : events_) {
+ for (auto& handler : handlers_) {
double lost_power_w = provided_power_w / discharge_efficiency_;
double gained_power_w = consumed_power_w * charge_efficiency_;
- // Event cannot happen
- if ((lost_power_w == gained_power_w) or (event->state_of_charge_ == energy_stored_j_ / (3600 * capacity_wh_)) or
- (lost_power_w > gained_power_w and event->flow_ == Flow::CHARGE) or
- (lost_power_w < gained_power_w and event->flow_ == Flow::DISCHARGE)) {
+ // Handler cannot happen
+ if ((lost_power_w == gained_power_w) or (handler->state_of_charge_ == energy_stored_j_ / (3600 * capacity_wh_)) or
+ (lost_power_w > gained_power_w and handler->flow_ == Flow::CHARGE) or
+ (lost_power_w < gained_power_w and handler->flow_ == Flow::DISCHARGE)) {
continue;
}
- // Evaluate time until event happen
+ // Evaluate time until handler happen
else {
/* The time to reach a state of charge depends on the capacity, but charging and discharging deteriorate the
* capacity, so we need to evaluate the time considering a capacity that also depends on time
*/
- event->time_delta_ =
- (3600 * event->state_of_charge_ * initial_capacity_wh_ *
+ handler->time_delta_ =
+ (3600 * handler->state_of_charge_ * initial_capacity_wh_ *
(1 - (energy_provided_j_ / discharge_efficiency_ + energy_consumed_j_ * charge_efficiency_) /
energy_budget_j_) -
energy_stored_j_) /
(gained_power_w - lost_power_w +
- 3600 * event->state_of_charge_ * initial_capacity_wh_ * (gained_power_w + lost_power_w) / energy_budget_j_);
- if ((time_delta == -1 or event->time_delta_ < time_delta) and abs(event->time_delta_) > 0.000000001)
- time_delta = event->time_delta_;
+ 3600 * handler->state_of_charge_ * initial_capacity_wh_ * (gained_power_w + lost_power_w) /
+ energy_budget_j_);
+ if ((time_delta == -1 or handler->time_delta_ < time_delta) and abs(handler->time_delta_) > 0.000000001)
+ time_delta = handler->time_delta_;
}
}
return time_delta;
}
/** @ingroup plugin_battery
- * @brief Create a new Event.
- * @param state_of_charge The state of charge at which the Event will happen.
- * @param flow The flow in which the Event will happen, either when the Battery is charging or discharging.
- * @param callback The callable to trigger when the Event happen.
- * @param repeat If the Event is a recurrent Event or a single Event.
- * @return A shared pointer of the new Event.
+ * @brief Schedule a new Handler.
+ * @param state_of_charge The state of charge at which the Handler will happen.
+ * @param flow The flow in which the Handler will happen, either when the Battery is charging or discharging.
+ * @param callback The callable to trigger when the Handler happen.
+ * @param repeat If the Handler is recurrent or unique.
+ * @return A shared pointer of the new Handler.
*/
-std::shared_ptr<Battery::Event> Battery::create_event(double state_of_charge, Flow flow, std::function<void()> callback,
- bool repeat)
+std::shared_ptr<Battery::Handler> Battery::schedule_handler(double state_of_charge, Flow flow, bool repeat, std::function<void()> callback)
{
- auto event = Event::init(state_of_charge, flow, callback, repeat);
- events_.push_back(event);
- return event;
+ auto handler = Handler::init(state_of_charge, flow, repeat, callback);
+ handlers_.push_back(handler);
+ return handler;
}
/** @ingroup plugin_battery
- * @return A vector containing the Events associated to the Battery.
+ * @return A vector containing the Handlers associated to the Battery.
*/
-std::vector<std::shared_ptr<Battery::Event>> Battery::get_events()
+std::vector<std::shared_ptr<Battery::Handler>> Battery::get_handlers()
{
- return events_;
+ return handlers_;
}
/** @ingroup plugin_battery
- * @brief Remove an Event from the Battery.
+ * @brief Remove an Handler from the Battery.
*/
-void Battery::delete_event(std::shared_ptr<Event> event)
+void Battery::delete_handler(std::shared_ptr<Handler> handler)
{
- events_.erase(
- std::remove_if(events_.begin(), events_.end(), [&event](std::shared_ptr<Event> e) { return event == e; }),
- events_.end());
+ handlers_.erase(std::remove_if(handlers_.begin(), handlers_.end(),
+ [&handler](std::shared_ptr<Handler> e) { return handler == e; }),
+ handlers_.end());
}
} // namespace simgrid::plugins
\ No newline at end of file