Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix memleak in battery examples and task-storm
[simgrid.git] / examples / cpp / battery-degradation / s4u-battery-degradation.cpp
1 /* Copyright (c) 2003-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 #include "simgrid/plugins/battery.hpp"
7 #include "simgrid/s4u.hpp"
8 #include <memory>
9 #include <simgrid/s4u/Actor.hpp>
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(battery_degradation, "Messages specific for this s4u example");
12
13 int main(int argc, char* argv[])
14 {
15   simgrid::s4u::Engine e(&argc, argv);
16   e.load_platform(argv[1]);
17
18   auto battery = simgrid::plugins::Battery::init("Battery", 0.8, -200, 200, 0.9, 0.9, 10, 100);
19
20   battery->set_load("load", 100.0);
21
22   auto handler1 = battery->schedule_handler(
23       0.2, simgrid::plugins::Battery::DISCHARGE, simgrid::plugins::Battery::Handler::PERSISTANT, [&battery]() {
24         XBT_INFO("%f,%f,SoC", simgrid::s4u::Engine::get_clock(), battery->get_state_of_charge());
25         XBT_INFO("%f,%f,SoH", simgrid::s4u::Engine::get_clock(), battery->get_state_of_health());
26         battery->set_load("load", -100.0);
27       });
28
29   std::shared_ptr<simgrid::plugins::Battery::Handler> handler2;
30   handler2 = battery->schedule_handler(
31       0.8, simgrid::plugins::Battery::CHARGE, simgrid::plugins::Battery::Handler::PERSISTANT,
32       [&battery, &handler1, &handler2]() {
33         XBT_INFO("%f,%f,SoC", simgrid::s4u::Engine::get_clock(), battery->get_state_of_charge());
34         XBT_INFO("%f,%f,SoH", simgrid::s4u::Engine::get_clock(), battery->get_state_of_health());
35         if (battery->get_state_of_health() < 0.1) {
36           battery->delete_handler(handler1);
37           battery->delete_handler(handler2);
38         }
39         battery->set_load("load", 100.0);
40       });
41
42   e.run();
43   return 0;
44 }