Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
897728b65a088a28b8878e6f1c5235fa57a51b1f
[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 static void manager()
14 {
15   auto battery = simgrid::plugins::Battery::init("Battery", 0.8, -200, 200, 0.9, 0.9, 10, 100);
16
17   battery->set_load("load", 100);
18
19   auto event1 = battery->create_event(
20       0.2, simgrid::plugins::Battery::DISCHARGE,
21       [battery]() {
22         XBT_INFO("%f,%f,SoC", simgrid::s4u::Engine::get_clock(), battery->get_state_of_charge());
23         XBT_INFO("%f,%f,SoH", simgrid::s4u::Engine::get_clock(), battery->get_state_of_health());
24         battery->set_load("load", -100);
25       },
26       true);
27
28   std::shared_ptr<simgrid::plugins::Battery::Event> event2;
29   event2 = battery->create_event(
30       0.8, simgrid::plugins::Battery::CHARGE,
31       [battery, event1, event2]() {
32         XBT_INFO("%f,%f,SoC", simgrid::s4u::Engine::get_clock(), battery->get_state_of_charge());
33         XBT_INFO("%f,%f,SoH", simgrid::s4u::Engine::get_clock(), battery->get_state_of_health());
34         if (battery->get_state_of_health() < 0.1) {
35           battery->delete_event(event1);
36           battery->delete_event(event2);
37         }
38         battery->set_load("load", 100);
39       },
40       true);
41 }
42
43 int main(int argc, char* argv[])
44 {
45   simgrid::s4u::Engine e(&argc, argv);
46   e.load_platform(argv[1]);
47
48   simgrid::s4u::Actor::create("manager", e.host_by_name("MyHost1"), manager);
49
50   e.run();
51   return 0;
52 }