Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
435c402a21441e0618ab05ed44ad75c90cf7e022
[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
9 XBT_LOG_NEW_DEFAULT_CATEGORY(battery_simple, "Messages specific for this s4u example");
10
11 static void manager()
12 {
13   auto battery         = simgrid::s4u::Engine::get_instance()->host_by_name("battery");
14   double consumption_w = 200;
15
16   XBT_INFO("Initial Battery: SoC: %f SoH: %f Capacity (Total): %fWh Capacity (Usable): %fWh P: %f",
17            sg_battery_get_state_of_charge(battery), sg_battery_get_state_of_health(battery),
18            sg_battery_get_capacity(battery),
19            sg_battery_get_capacity(battery) *
20                (sg_battery_get_state_of_charge_max(battery) - sg_battery_get_state_of_charge_min(battery)),
21            sg_battery_get_power(battery));
22   double start = simgrid::s4u::Engine::get_clock();
23   sg_battery_set_power(battery, consumption_w);
24   XBT_INFO("Battery power set to: %fW", consumption_w);
25   double end = sg_battery_get_next_event_date(battery);
26   XBT_INFO("The battery will be depleted at: %f", end);
27   simgrid::s4u::this_actor::sleep_until(end);
28   XBT_INFO("Energy consumed : %fJ (%fWh)", (end - start) * consumption_w, (end - start) * consumption_w / 3600);
29 }
30
31 int main(int argc, char* argv[])
32 {
33   simgrid::s4u::Engine e(&argc, argv);
34   e.load_platform(argv[1]);
35
36   sg_battery_plugin_init();
37
38   simgrid::s4u::Actor::create("manager", e.host_by_name("host1"), manager);
39   e.run();
40   return 0;
41 }