Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add nominal charge and discharge power to batteries
[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->create_event(0.2, simgrid::plugins::Battery::DISCHARGE, [battery, &load_w]() {
31     XBT_INFO("Discharged state: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
32              battery->get_state_of_charge(), battery->get_state_of_health(), battery->get_energy_stored(),
33              battery->get_energy_provided(), battery->get_energy_consumed());
34     battery->set_load("load", -load_w);
35     XBT_INFO("Set load to %fW", -load_w);
36   });
37
38   battery->create_event(0.8, simgrid::plugins::Battery::CHARGE, [battery]() {
39     XBT_INFO("Charged state: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
40              battery->get_state_of_charge(), battery->get_state_of_health(), battery->get_energy_stored(),
41              battery->get_energy_provided(), battery->get_energy_consumed());
42     XBT_INFO("Set load to %fW", 0.0);
43   });
44
45   e.run();
46   return 0;
47 }