Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix memleak in battery examples and task-storm
[simgrid.git] / examples / cpp / battery-simple / s4u-battery-simple.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 <simgrid/s4u/Actor.hpp>
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(battery_simple, "Messages specific for this s4u example");
11
12 int main(int argc, char* argv[])
13 {
14   simgrid::s4u::Engine e(&argc, argv);
15   e.load_platform(argv[1]);
16
17   auto battery = simgrid::plugins::Battery::init("Battery", 0.8, -100, 100, 0.9, 0.9, 10, 1000);
18
19   XBT_INFO("Initial state: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
20            battery->get_state_of_charge(), battery->get_state_of_health(), battery->get_energy_stored(),
21            battery->get_energy_provided(), battery->get_energy_consumed());
22
23   /* This power is beyond the nominal values of the battery
24    * see documentation for more info
25    */
26   double load_w = 150;
27   battery->set_load("load", load_w);
28   XBT_INFO("Set load to %fW", load_w);
29
30   battery->schedule_handler(
31       0.2, simgrid::plugins::Battery::DISCHARGE, simgrid::plugins::Battery::Handler::PERSISTANT, [&battery, &load_w]() {
32         XBT_INFO("Discharged state: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
33                  battery->get_state_of_charge(), battery->get_state_of_health(), battery->get_energy_stored(),
34                  battery->get_energy_provided(), battery->get_energy_consumed());
35         battery->set_load("load", -load_w);
36         XBT_INFO("Set load to %fW", -load_w);
37       });
38
39   battery->schedule_handler(
40       0.8, simgrid::plugins::Battery::CHARGE, simgrid::plugins::Battery::Handler::PERSISTANT, [&battery]() {
41         XBT_INFO("Charged state: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
42                  battery->get_state_of_charge(), battery->get_state_of_health(), battery->get_energy_stored(),
43                  battery->get_energy_provided(), battery->get_energy_consumed());
44         XBT_INFO("Set load to %fW", 0.0);
45       });
46
47   e.run();
48
49   return 0;
50 }