Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add nominal charge and discharge power to batteries
authorAdrien Gougeon <adrien.gougeon@ens-rennes.fr>
Fri, 21 Jul 2023 18:23:47 +0000 (20:23 +0200)
committerAdrien Gougeon <adrien.gougeon@ens-rennes.fr>
Fri, 21 Jul 2023 18:23:47 +0000 (20:23 +0200)
examples/cpp/battery-degradation/s4u-battery-degradation.cpp
examples/cpp/battery-energy/s4u-battery-energy.cpp
examples/cpp/battery-simple/s4u-battery-simple.cpp
examples/cpp/battery-simple/s4u-battery-simple.tesh
include/simgrid/plugins/battery.hpp
src/plugins/battery.cpp

index cc7e003..897728b 100644 (file)
@@ -12,7 +12,7 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(battery_degradation, "Messages specific for this s4
 
 static void manager()
 {
 
 static void manager()
 {
-  auto battery = simgrid::plugins::Battery::init("Battery", 0.8, 0.9, 0.9, 10, 100);
+  auto battery = simgrid::plugins::Battery::init("Battery", 0.8, -200, 200, 0.9, 0.9, 10, 100);
 
   battery->set_load("load", 100);
 
 
   battery->set_load("load", 100);
 
index a950620..dae8731 100644 (file)
@@ -13,7 +13,7 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(battery_energy, "Messages specific for this s4u exa
 
 static void manager()
 {
 
 static void manager()
 {
-  auto battery = simgrid::plugins::Battery::init("Battery", 0.8, 0.9, 0.9, 10, 1000);
+  auto battery = simgrid::plugins::Battery::init("Battery", 0.8, -300, 300, 0.9, 0.9, 10, 1000);
 
   auto* host1 = simgrid::s4u::Engine::get_instance()->host_by_name("MyHost1");
   auto* host2 = simgrid::s4u::Engine::get_instance()->host_by_name("MyHost2");
 
   auto* host1 = simgrid::s4u::Engine::get_instance()->host_by_name("MyHost1");
   auto* host2 = simgrid::s4u::Engine::get_instance()->host_by_name("MyHost2");
@@ -40,7 +40,7 @@ static void manager()
 
   double flops = 1e9;
   XBT_INFO("Host %s will now execute %f flops", host1->get_cname(), flops);
 
   double flops = 1e9;
   XBT_INFO("Host %s will now execute %f flops", host1->get_cname(), flops);
-  host1->execute(flops);
+  host2->execute(flops);
 
   simgrid::s4u::this_actor::sleep_until(200);
   XBT_INFO("Battery state: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
 
   simgrid::s4u::this_actor::sleep_until(200);
   XBT_INFO("Battery state: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
index 6f47cc7..48d93c2 100644 (file)
@@ -14,13 +14,16 @@ int main(int argc, char* argv[])
   simgrid::s4u::Engine e(&argc, argv);
   e.load_platform(argv[1]);
 
   simgrid::s4u::Engine e(&argc, argv);
   e.load_platform(argv[1]);
 
-  auto battery = simgrid::plugins::Battery::init("Battery", 0.8, 0.9, 0.9, 10, 1000);
+  auto battery = simgrid::plugins::Battery::init("Battery", 0.8, -100, 100, 0.9, 0.9, 10, 1000);
 
   XBT_INFO("Initial state: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
            battery->get_state_of_charge(), battery->get_state_of_health(), battery->get_energy_stored(),
            battery->get_energy_provided(), battery->get_energy_consumed());
 
 
   XBT_INFO("Initial state: SoC: %f SoH: %f Energy stored: %fJ Energy provided: %fJ Energy consumed %fJ",
            battery->get_state_of_charge(), battery->get_state_of_health(), battery->get_energy_stored(),
            battery->get_energy_provided(), battery->get_energy_consumed());
 
-  double load_w = 100;
+  /* This power is beyond the nominal values of the battery
+   * see documentation for more info
+   */
+  double load_w = 150;
   battery->set_load("load", load_w);
   XBT_INFO("Set load to %fW", load_w);
 
   battery->set_load("load", load_w);
   XBT_INFO("Set load to %fW", load_w);
 
index 253e7d1..3674d1f 100644 (file)
@@ -2,8 +2,9 @@
 
 $ ${bindir:=.}/s4u-battery-simple ${platfdir}/energy_platform.xml
 > [0.000000] [battery_simple/INFO] Initial state: SoC: 0.800000 SoH: 1.000000 Energy stored: 28800.000000J Energy provided: 0.000000J Energy consumed 0.000000J
 
 $ ${bindir:=.}/s4u-battery-simple ${platfdir}/energy_platform.xml
 > [0.000000] [battery_simple/INFO] Initial state: SoC: 0.800000 SoH: 1.000000 Energy stored: 28800.000000J Energy provided: 0.000000J Energy consumed 0.000000J
-> [0.000000] [battery_simple/INFO] Set load to 100.000000W
-> [194.419442] [battery_simple/INFO] Discharged state: SoC: 0.200000 SoH: 0.999700 Energy stored: 7197.839784J Energy provided: 19441.944194J Energy consumed 0.000000J
-> [194.419442] [battery_simple/INFO] Set load to -100.000000W
-> [434.251502] [battery_simple/INFO] Charged state: SoC: 0.800000 SoH: 0.999400 Energy stored: 28782.725182J Energy provided: 19441.944194J Energy consumed 23983.205998J
-> [434.251502] [battery_simple/INFO] Set load to 0.000000W
+> [0.000000] [battery_simple/INFO] Set load to 150.000000W
+> [216.021602] [battery_simple/INFO] Discharged state: SoC: 0.200000 SoH: 0.999700 Energy stored: 7197.839784J Energy provided: 19441.944194J Energy consumed 0.000000J
+> [216.021602] [battery_simple/INFO] Set load to -150.000000W
+> [455.853662] [battery_simple/INFO] Charged state: SoC: 0.800000 SoH: 0.999400 Energy stored: 28782.725182J Energy provided: 19441.944194J Energy consumed 23983.205998J
+> [455.853662] [battery_simple/INFO] Set load to 0.000000W
+
index b85cec6..ebe28f9 100644 (file)
@@ -83,6 +83,8 @@ private:
   static std::shared_ptr<BatteryModel> battery_model_;
 
   std::string name_;
   static std::shared_ptr<BatteryModel> battery_model_;
 
   std::string name_;
+  double nominal_charge_power_w_;
+  double nominal_discharge_power_w_;
   double charge_efficiency_;
   double discharge_efficiency_;
   double initial_capacity_wh_;
   double charge_efficiency_;
   double discharge_efficiency_;
   double initial_capacity_wh_;
@@ -98,8 +100,9 @@ private:
   double energy_consumed_j_ = 0;
   double last_updated_      = 0;
 
   double energy_consumed_j_ = 0;
   double last_updated_      = 0;
 
-  explicit Battery(const std::string& name, double state_of_charge, double charge_efficiency,
-                   double discharge_efficiency, double initial_capacity_wh, int cycles);
+  explicit Battery(const std::string& name, double state_of_charge, double nominal_charge_power_w,
+                   double nominal_discharge_power_w, double charge_efficiency, double discharge_efficiency,
+                   double initial_capacity_wh, int cycles);
   static void init_plugin();
   void update();
   double next_occurring_event();
   static void init_plugin();
   void update();
   double next_occurring_event();
@@ -117,8 +120,9 @@ private:
 #endif
 
 public:
 #endif
 
 public:
-  static BatteryPtr init(const std::string& name, double state_of_charge, double charge_efficiency,
-                         double discharge_efficiency, double initial_capacity_wh, int cycles);
+  static BatteryPtr init(const std::string& name, double state_of_charge, double nominal_charge_power_w,
+                         double nominal_discharge_power_w, double charge_efficiency, double discharge_efficiency,
+                         double initial_capacity_wh, int cycles);
   void set_load(const std::string& name, double power_w);
   void connect_host(s4u::Host* host, bool active = true);
   double get_state_of_charge();
   void set_load(const std::string& name, double power_w);
   void connect_host(s4u::Host* host, bool active = true);
   double get_state_of_charge();
index 15c715a..148ec60 100644 (file)
@@ -24,8 +24,13 @@ This is the battery plugin, enabling management of batteries.
 Batteries
 .........
 
 Batteries
 .........
 
-A battery has an initial State of Charge :math:`SoC`, a charge efficiency :math:`\eta_{charge}`, a discharge efficiency
-:math:`\eta_{discharge}`, an initial capacity :math:`C_{initial}` and a number of cycle :math:`N`.
+A battery has an initial State of Charge :math:`SoC`, a nominal charge power, a nominal discharge power, a charge
+efficiency :math:`\eta_{charge}`, a discharge efficiency :math:`\eta_{discharge}`, an initial capacity
+:math:`C_{initial}` and a number of cycle :math:`N`.
+
+The nominal charge(discharge) power is the maximum power the Battery can consume(provide), before application of the
+charge(discharge) efficiency factor. For instance, if a load provides(consumes) 100W to(from) the Battery with a nominal
+charge(discharge) power of 50W and a charge(discharge) efficiency of 0.9, the Battery will only gain(provide) 45W.
 
 We distinguish the energy provided :math:`E_{provided}` / consumed :math:`E_{consumed}` from the energy lost
 :math:`E_{lost}` / gained :math:`E_{gained}`. The energy provided / consumed shows the external point of view, and the
 
 We distinguish the energy provided :math:`E_{provided}` / consumed :math:`E_{consumed}` from the energy lost
 :math:`E_{lost}` / gained :math:`E_{gained}`. The energy provided / consumed shows the external point of view, and the
@@ -40,6 +45,9 @@ energy lost / gained shows the internal point of view:
 For instance, if you apply a load of 100W to a battery for 10s with a discharge efficiency of 0.8, the energy provided
 will be equal to 10kJ, and the energy lost will be equal to 12.5kJ.
 
 For instance, if you apply a load of 100W to a battery for 10s with a discharge efficiency of 0.8, the energy provided
 will be equal to 10kJ, and the energy lost will be equal to 12.5kJ.
 
+All the energies are positive, but loads connected to a Battery may be positive or negative, as explained in the next
+section.
+
 Use the battery reduces its State of Health :math:`SoH` and its capacity :math:`C` linearly in consequence:
 
 .. math::
 Use the battery reduces its State of Health :math:`SoH` and its capacity :math:`C` linearly in consequence:
 
 .. math::
@@ -66,7 +74,7 @@ Loads & Hosts
 ..............
 
 You can add named loads to a battery. Those loads may be positive and consume energy from the battery, or negative and
 ..............
 
 You can add named loads to a battery. Those loads may be positive and consume energy from the battery, or negative and
-add energy to the battery. You can also connect hosts to a battery. Theses hosts will consume their energy from the
+provide energy to the battery. You can also connect hosts to a battery. Theses hosts will consume their energy from the
 battery until the battery is empty or until the connection between the hosts and the battery is set inactive.
 
 Events
 battery until the battery is empty or until the connection between the hosts and the battery is set inactive.
 
 Events
@@ -152,6 +160,9 @@ void Battery::update()
       else
         consumed_power_w += -load;
     }
       else
         consumed_power_w += -load;
     }
+    provided_power_w = std::min(provided_power_w, nominal_discharge_power_w_ * discharge_efficiency_);
+    consumed_power_w = std::min(consumed_power_w, -nominal_charge_power_w_);
+
     double energy_lost_delta_j   = provided_power_w / discharge_efficiency_ * time_delta_s;
     double energy_gained_delta_j = consumed_power_w * charge_efficiency_ * time_delta_s;
 
     double energy_lost_delta_j   = provided_power_w / discharge_efficiency_ * time_delta_s;
     double energy_gained_delta_j = consumed_power_w * charge_efficiency_ * time_delta_s;
 
@@ -206,6 +217,9 @@ double Battery::next_occurring_event()
       consumed_power_w += -load;
   }
 
       consumed_power_w += -load;
   }
 
+  provided_power_w = std::min(provided_power_w, nominal_discharge_power_w_ * discharge_efficiency_);
+  consumed_power_w = std::min(consumed_power_w, -nominal_charge_power_w_);
+
   double time_delta = -1;
   for (auto& event : events_) {
     double lost_power_w   = provided_power_w / discharge_efficiency_;
   double time_delta = -1;
   for (auto& event : events_) {
     double lost_power_w   = provided_power_w / discharge_efficiency_;
@@ -235,9 +249,12 @@ double Battery::next_occurring_event()
   return time_delta;
 }
 
   return time_delta;
 }
 
-Battery::Battery(const std::string& name, double state_of_charge, double charge_efficiency, double discharge_efficiency,
+Battery::Battery(const std::string& name, double state_of_charge, double nominal_charge_power_w,
+                 double nominal_discharge_power_w, double charge_efficiency, double discharge_efficiency,
                  double initial_capacity_wh, int cycles)
     : name_(name)
                  double initial_capacity_wh, int cycles)
     : name_(name)
+    , nominal_charge_power_w_(nominal_charge_power_w)
+    , nominal_discharge_power_w_(nominal_discharge_power_w)
     , charge_efficiency_(charge_efficiency)
     , discharge_efficiency_(discharge_efficiency)
     , initial_capacity_wh_(initial_capacity_wh)
     , charge_efficiency_(charge_efficiency)
     , discharge_efficiency_(discharge_efficiency)
     , initial_capacity_wh_(initial_capacity_wh)
@@ -245,6 +262,10 @@ Battery::Battery(const std::string& name, double state_of_charge, double charge_
     , capacity_wh_(initial_capacity_wh)
     , energy_stored_j_(state_of_charge * 3600 * initial_capacity_wh)
 {
     , capacity_wh_(initial_capacity_wh)
     , energy_stored_j_(state_of_charge * 3600 * initial_capacity_wh)
 {
+  xbt_assert(nominal_charge_power_w <= 0, " : nominal charge power must be non-negative (provided: %f)",
+             nominal_charge_power_w);
+  xbt_assert(nominal_discharge_power_w >= 0, " : nominal discharge power must be non-negative (provided: %f)",
+             nominal_discharge_power_w);
   xbt_assert(state_of_charge >= 0 and state_of_charge <= 1, " : state of charge should be in [0, 1] (provided: %f)",
              state_of_charge);
   xbt_assert(charge_efficiency > 0 and charge_efficiency <= 1, " : charge efficiency should be in [0,1] (provided: %f)",
   xbt_assert(state_of_charge >= 0 and state_of_charge <= 1, " : state of charge should be in [0, 1] (provided: %f)",
              state_of_charge);
   xbt_assert(charge_efficiency > 0 and charge_efficiency <= 1, " : charge efficiency should be in [0,1] (provided: %f)",
@@ -258,22 +279,25 @@ Battery::Battery(const std::string& name, double state_of_charge, double charge_
 /** @ingroup plugin_battery
  *  @param name The name of the Battery.
  *  @param state_of_charge The initial state of charge of the Battery [0,1].
 /** @ingroup plugin_battery
  *  @param name The name of the Battery.
  *  @param state_of_charge The initial state of charge of the Battery [0,1].
+ *  @param nominal_charge_power_w The maximum power delivered by the Battery in W (<= 0).
+ *  @param nominal_discharge_power_w The maximum power absorbed by the Battery in W (>= 0).
  *  @param charge_efficiency The charge efficiency of the Battery [0,1].
  *  @param discharge_efficiency The discharge efficiency of the Battery [0,1].
  *  @param initial_capacity_wh The initial capacity of the Battery in Wh (>0).
  *  @param cycles The number of charge-discharge cycles until complete depletion of the Battery capacity.
  *  @return A BatteryPtr pointing to the new Battery.
  */
  *  @param charge_efficiency The charge efficiency of the Battery [0,1].
  *  @param discharge_efficiency The discharge efficiency of the Battery [0,1].
  *  @param initial_capacity_wh The initial capacity of the Battery in Wh (>0).
  *  @param cycles The number of charge-discharge cycles until complete depletion of the Battery capacity.
  *  @return A BatteryPtr pointing to the new Battery.
  */
-BatteryPtr Battery::init(const std::string& name, double state_of_charge, double charge_efficiency,
-                         double discharge_efficiency, double initial_capacity_wh, int cycles)
+BatteryPtr Battery::init(const std::string& name, double state_of_charge, double nominal_charge_power_w,
+                         double nominal_discharge_power_w, double charge_efficiency, double discharge_efficiency,
+                         double initial_capacity_wh, int cycles)
 {
   static bool plugin_inited = false;
   if (not plugin_inited) {
     init_plugin();
     plugin_inited = true;
   }
 {
   static bool plugin_inited = false;
   if (not plugin_inited) {
     init_plugin();
     plugin_inited = true;
   }
-  auto battery = BatteryPtr(
-      new Battery(name, state_of_charge, charge_efficiency, discharge_efficiency, initial_capacity_wh, cycles));
+  auto battery = BatteryPtr(new Battery(name, state_of_charge, nominal_charge_power_w, nominal_discharge_power_w,
+                                        charge_efficiency, discharge_efficiency, initial_capacity_wh, cycles));
   battery_model_->add_battery(battery);
   return battery;
 }
   battery_model_->add_battery(battery);
   return battery;
 }