1 /* Copyright (c) 2017-2023. The SimGrid Team. All rights reserved. */
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. */
6 /* This example combine the battery plugin, the chiller plugin and the solar
7 panel plugin. It illustrates how to use them together to evaluate the amount
8 of brown energy (from the electrical grid) and green energy (from the solar
9 panel) consumed by several machines.
11 In this scenario we have two host placed in a room.
12 The room is maintained at 24°C by a chiller, powered by the electrical grid
13 and consumes brown energy.
14 The two hosts are powered by a battery when available, and the electrical
15 grid otherwise. The battery is charged by a solar panel.
17 We simulate two days from 00h00 to 00h00.
18 The solar panel generates power from 8h to 20h with a peak at 14h.
19 During the simulation, when the charge of the battery goes:
20 - below 75% the solar panel is connected to the battery
21 - above 80% the solar panel is disconnected from the battery
22 - below 20% the hosts are disconnected from the battery
23 - above 25% the hosts are connected to the battery
25 The two hosts are always idle, except from 12h to 16h on the first day.
28 #include "simgrid/plugins/battery.hpp"
29 #include "simgrid/plugins/chiller.hpp"
30 #include "simgrid/plugins/energy.h"
31 #include "simgrid/plugins/solar_panel.hpp"
32 #include "simgrid/s4u.hpp"
35 XBT_LOG_NEW_DEFAULT_CATEGORY(battery_chiller_solar, "Messages specific for this s4u example");
36 namespace sg4 = simgrid::s4u;
37 namespace sp = simgrid::plugins;
39 static void irradiance_manager(sp::SolarPanelPtr solar_panel)
43 double amplitude = 1000 / 2.0;
44 double period = 24 * 60 * 60;
45 double shift = 16 * 60 * 60;
48 irradiance = amplitude * sin(2 * M_PI * (time + shift) / period);
49 irradiance = irradiance < 0 ? 0 : irradiance;
50 solar_panel->set_solar_irradiance(irradiance);
51 sg4::this_actor::sleep_for(time_step);
56 static void host_job_manager(double start, double duration)
58 sg4::this_actor::sleep_until(start);
59 sg4::this_actor::get_host()->execute(duration * sg4::this_actor::get_host()->get_speed());
62 static void end_manager(sp::BatteryPtr b)
64 sg4::this_actor::sleep_until(86400 * 2);
65 for (auto& handler : b->get_handlers())
66 b->delete_handler(handler);
69 static void logger(sp::BatteryPtr battery, sp::SolarPanelPtr solar_panel, sp::ChillerPtr chiller, sg4::Host* host1,
73 XBT_INFO("SoC: %f Solar_Power: %f E_chiller: %f E_hosts_brown: %f E_hosts_green: %f",
74 battery->get_state_of_charge(), solar_panel->get_power(), chiller->get_energy_consumed(),
75 sg_host_get_consumed_energy(host1) + sg_host_get_consumed_energy(host2) - battery->get_energy_provided(),
76 battery->get_energy_provided());
77 simgrid::s4u::this_actor::sleep_for(100);
81 int main(int argc, char* argv[])
83 sg4::Engine e(&argc, argv);
84 e.load_platform(argv[1]);
85 sg_host_energy_plugin_init();
87 auto myhost1 = e.host_by_name("MyHost1");
88 auto myhost2 = e.host_by_name("MyHost2");
90 auto battery = sp::Battery::init("Battery", 0.2, -1e3, 1e3, 0.9, 0.9, 2000, 1000);
91 auto chiller = sp::Chiller::init("Chiller", 50, 1006, 0.2, 0.9, 24, 24, 1e3);
92 auto solar_panel = sp::SolarPanel::init("Solar Panel", 1.1, 0.9, 0, 0, 1e3);
93 chiller->add_host(myhost1);
94 chiller->add_host(myhost2);
95 solar_panel->on_this_power_change_cb(
96 [battery](sp::SolarPanel* s) { battery->set_load("Solar Panel", s->get_power() * -1); });
97 battery->schedule_handler(0.8, sp::Battery::CHARGE, sp::Battery::Handler::PERSISTANT,
98 [battery]() { battery->set_load("Solar Panel", false); });
99 battery->schedule_handler(0.75, sp::Battery::DISCHARGE, sp::Battery::Handler::PERSISTANT,
100 [battery]() { battery->set_load("Solar Panel", true); });
101 battery->schedule_handler(0.2, sp::Battery::DISCHARGE, sp::Battery::Handler::PERSISTANT,
102 [battery, &myhost1, &myhost2]() {
103 battery->connect_host(myhost1, false);
104 battery->connect_host(myhost2, false);
106 battery->schedule_handler(0.25, sp::Battery::CHARGE, sp::Battery::Handler::PERSISTANT,
107 [battery, &myhost1, &myhost2]() {
108 battery->connect_host(myhost1);
109 battery->connect_host(myhost2);
112 sg4::Actor::create("irradiance_manager", myhost1, irradiance_manager, solar_panel)->daemonize();
113 sg4::Actor::create("host_job_manager", myhost1, host_job_manager, 12 * 60 * 60, 4 * 60 * 60);
114 sg4::Actor::create("host_job_manager", myhost2, host_job_manager, 12 * 60 * 60, 4 * 60 * 60);
115 sg4::Actor::create("end_manager", myhost1, end_manager, battery);
116 // sg4::Actor::create("logger", myhost1, logger, battery, solar_panel, chiller, myhost1, myhost2)->daemonize();
119 XBT_INFO("State of charge of the battery: %0.1f%%", battery->get_state_of_charge() * 100);
121 "Energy consumed by the hosts (green / brown): %.2fMJ "
123 battery->get_energy_provided() / 1e6,
124 (sg_host_get_consumed_energy(myhost1) + sg_host_get_consumed_energy(myhost2) - battery->get_energy_provided()) /
126 XBT_INFO("Energy consumed by the chiller (brown): %.2fMJ", chiller->get_energy_consumed() / 1e6);