diff -r 7f3095ccfcb1 src/energy/model/basic-energy-source.cc --- a/src/energy/model/basic-energy-source.cc Thu May 05 12:17:12 2016 +0200 +++ b/src/energy/model/basic-energy-source.cc Thu May 05 21:34:06 2016 -0400 @@ -40,10 +40,16 @@ .AddConstructor () .AddAttribute ("BasicEnergySourceInitialEnergyJ", "Initial energy stored in basic energy source.", - DoubleValue (10), // in Joules + DoubleValue (0), // in Joules MakeDoubleAccessor (&BasicEnergySource::SetInitialEnergy, &BasicEnergySource::GetInitialEnergy), MakeDoubleChecker ()) + .AddAttribute ("BasicEnergySourceCapacityJ", + "Energy capacity that can be stored in basic energy source.", + DoubleValue (0), // in Joules + MakeDoubleAccessor (&BasicEnergySource::SetEnergyCapacity, + &BasicEnergySource::GetEnergyCapacity), + MakeDoubleChecker ()) .AddAttribute ("BasicEnergySupplyVoltageV", "Initial supply voltage for basic energy source.", DoubleValue (3.0), // in Volts @@ -77,6 +83,7 @@ BasicEnergySource::BasicEnergySource () { NS_LOG_FUNCTION (this); + m_energyCapacityJ = 0; m_lastUpdateTime = Seconds (0.0); m_depleted = false; } @@ -91,8 +98,13 @@ { NS_LOG_FUNCTION (this << initialEnergyJ); NS_ASSERT (initialEnergyJ >= 0); + NS_ASSERT (initialEnergyJ <= m_energyCapacityJ || m_energyCapacityJ == 0); m_initialEnergyJ = initialEnergyJ; m_remainingEnergyJ = m_initialEnergyJ; + if(m_energyCapacityJ == 0) + { + m_energyCapacityJ = m_initialEnergyJ; + } } void @@ -145,7 +157,11 @@ NS_LOG_FUNCTION (this); // update energy source to get the latest remaining energy. UpdateEnergySource (); - return m_remainingEnergyJ / m_initialEnergyJ; + if(m_energyCapacityJ == 0) + { + return 0; + } + return m_remainingEnergyJ / m_energyCapacityJ; } void @@ -161,6 +177,15 @@ } m_energyUpdateEvent.Cancel (); + + if(m_energyCapacityJ == 0) + { + m_energyCapacityJ = m_initialEnergyJ; + } + if(m_initialEnergyJ == 0) + { + m_initialEnergyJ = m_energyCapacityJ; + } CalculateRemainingEnergy (); @@ -183,6 +208,27 @@ this); } +void +BasicEnergySource::SetEnergyCapacity (double energyCapacityJ) +{ + NS_LOG_FUNCTION (this << energyCapacityJ); + NS_ASSERT (energyCapacityJ >= 0); + NS_ASSERT (energyCapacityJ >= m_initialEnergyJ || energyCapacityJ == 0); + m_energyCapacityJ = energyCapacityJ; + if(m_initialEnergyJ == 0) + { + m_initialEnergyJ = m_energyCapacityJ; + m_remainingEnergyJ = m_initialEnergyJ; + } +} + +double +BasicEnergySource::GetEnergyCapacity () const +{ + NS_LOG_FUNCTION (this); + return m_energyCapacityJ; +} + /* * Private functions start here. */ @@ -226,15 +272,18 @@ NS_ASSERT (duration.GetSeconds () >= 0); // energy = current * voltage * time double energyToDecreaseJ = totalCurrentA * m_supplyVoltageV * duration.GetSeconds (); - - if (m_remainingEnergyJ < energyToDecreaseJ) + + // handles consuming energy below the low battery threshold + if (m_remainingEnergyJ <= m_energyCapacityJ * m_lowBatteryTh && energyToDecreaseJ > 0) { - m_remainingEnergyJ = 0; // energy never goes below 0 - } - else + energyToDecreaseJ = 0; + } + // handles recharging above the energy capacity + if (m_remainingEnergyJ >= m_energyCapacityJ && energyToDecreaseJ < 0) { - m_remainingEnergyJ -= energyToDecreaseJ; - } + energyToDecreaseJ = 0; + } + m_remainingEnergyJ -= energyToDecreaseJ; NS_LOG_DEBUG ("BasicEnergySource:Remaining energy = " << m_remainingEnergyJ); } diff -r 7f3095ccfcb1 src/energy/model/basic-energy-source.h --- a/src/energy/model/basic-energy-source.h Thu May 05 12:17:12 2016 +0200 +++ b/src/energy/model/basic-energy-source.h Thu May 05 21:34:06 2016 -0400 @@ -47,6 +47,13 @@ * Implements GetInitialEnergy. */ virtual double GetInitialEnergy (void) const; + + /** + * \return The source energy capacity, in Joules. + * + * Implements GetEnergyCapacity. + */ + virtual double GetEnergyCapacity (void) const; /** * \returns Supply voltage at the energy source. @@ -80,8 +87,19 @@ * Sets initial energy stored in the energy source. Note that initial energy * is assumed to be set before simulation starts and is set only once per * simulation. + * Note that if the initial energy is not set, it is assumed to be equal to + * the source energy capacity. */ void SetInitialEnergy (double initialEnergyJ); + + /** + * \param The source energy capacity, in Joules. + * + * Sets the energy capacity of the source. + * Note that if the energy capacity is not set, it is assumed to be equal to + * the initial energy. + */ + void SetEnergyCapacity (double energyCapacityJ); /** * \param supplyVoltageV Supply voltage at the energy source, in Volts. @@ -137,6 +155,7 @@ private: double m_initialEnergyJ; // initial energy, in Joules + double m_energyCapacityJ; // energy capacity, in Joules double m_supplyVoltageV; // supply voltage, in Volts double m_lowBatteryTh; // low battery threshold, as a fraction of the initial energy double m_highBatteryTh; // high battery threshold, as a fraction of the initial energy diff -r 7f3095ccfcb1 src/energy/model/energy-source.h --- a/src/energy/model/energy-source.h Thu May 05 12:17:12 2016 +0200 +++ b/src/energy/model/energy-source.h Thu May 05 21:34:06 2016 -0400 @@ -100,6 +100,14 @@ * with a fixed initial energy (energy capacity), set method is not needed. */ virtual double GetInitialEnergy (void) const = 0; + + /** + * \returns Energy capacity of the energy source. + * + * Set method is to be defined in child class only if necessary. For sources + * with a fixed energy capacity, set method is not needed. + */ + virtual double GetEnergyCapacity (void) const = 0; /** * \returns Remaining energy at the energy source. diff -r 7f3095ccfcb1 src/energy/model/li-ion-energy-source.cc --- a/src/energy/model/li-ion-energy-source.cc Thu May 05 12:17:12 2016 +0200 +++ b/src/energy/model/li-ion-energy-source.cc Thu May 05 21:34:06 2016 -0400 @@ -41,11 +41,17 @@ .SetGroupName ("Energy") .AddConstructor () .AddAttribute ("LiIonEnergySourceInitialEnergyJ", - "Initial energy stored in basic energy source.", - DoubleValue (31752.0), // in Joules + "Initial energy stored in LiIon energy source.", + DoubleValue (0), // in Joules MakeDoubleAccessor (&LiIonEnergySource::SetInitialEnergy, &LiIonEnergySource::GetInitialEnergy), MakeDoubleChecker ()) + .AddAttribute ("LiIonEnergySourceCapacityJ", + "Energy capacity that can be stored in LiIon energy source.", + DoubleValue (0), // in Joules + MakeDoubleAccessor (&LiIonEnergySource::SetEnergyCapacity, + &LiIonEnergySource::GetEnergyCapacity), + MakeDoubleChecker ()) .AddAttribute ("LiIonEnergyLowBatteryThreshold", "Low battery threshold for LiIon energy source.", DoubleValue (0.10), // as a fraction of the initial energy @@ -112,7 +118,8 @@ } LiIonEnergySource::LiIonEnergySource () - : m_drainedCapacity (0.0), + : m_energyCapacityJ (0.0), + m_drainedCapacity (0.0), m_lastUpdateTime (Seconds (0.0)) { NS_LOG_FUNCTION (this); @@ -128,9 +135,14 @@ { NS_LOG_FUNCTION (this << initialEnergyJ); NS_ASSERT (initialEnergyJ >= 0); + NS_ASSERT (initialEnergyJ <= m_energyCapacityJ || m_energyCapacityJ == 0); m_initialEnergyJ = initialEnergyJ; // set remaining energy to be initial energy m_remainingEnergyJ = m_initialEnergyJ; + if(m_energyCapacityJ == 0) + { + m_energyCapacityJ = m_initialEnergyJ; + } } double @@ -184,6 +196,10 @@ NS_LOG_FUNCTION (this); // update energy source to get the latest remaining energy. UpdateEnergySource (); + if(m_energyCapacityJ == 0) + { + return 0; + } return m_remainingEnergyJ / m_initialEnergyJ; } @@ -223,6 +239,15 @@ } m_energyUpdateEvent.Cancel (); + + if(m_energyCapacityJ == 0) + { + m_energyCapacityJ = m_initialEnergyJ; + } + if(m_initialEnergyJ == 0) + { + m_initialEnergyJ = m_energyCapacityJ; + } CalculateRemainingEnergy (); @@ -239,6 +264,27 @@ this); } +void +LiIonEnergySource::SetEnergyCapacity (double energyCapacityJ) +{ + NS_LOG_FUNCTION (this << energyCapacityJ); + NS_ASSERT (energyCapacityJ >= 0); + NS_ASSERT (energyCapacityJ >= m_initialEnergyJ || energyCapacityJ == 0); + m_energyCapacityJ = energyCapacityJ; + if(m_initialEnergyJ == 0) + { + m_initialEnergyJ = m_energyCapacityJ; + m_remainingEnergyJ = m_initialEnergyJ; + } +} + +double +LiIonEnergySource::GetEnergyCapacity () const +{ + NS_LOG_FUNCTION (this); + return m_energyCapacityJ; +} + /* * Private functions start here. */ @@ -277,14 +323,17 @@ // energy = current * voltage * time double energyToDecreaseJ = totalCurrentA * m_supplyVoltageV * duration.GetSeconds (); - if (m_remainingEnergyJ < energyToDecreaseJ) + // handles consuming energy below the low battery threshold + if (m_remainingEnergyJ <= m_energyCapacityJ * m_lowBatteryTh && energyToDecreaseJ > 0) { - m_remainingEnergyJ = 0; // energy never goes below 0 - } - else + energyToDecreaseJ = 0; + } + // handles recharging above the energy capacity + if (m_remainingEnergyJ >= m_energyCapacityJ && energyToDecreaseJ < 0) { - m_remainingEnergyJ -= energyToDecreaseJ; - } + energyToDecreaseJ = 0; + } + m_remainingEnergyJ -= energyToDecreaseJ; m_drainedCapacity += (totalCurrentA * duration.GetSeconds () / 3600); // update the supply voltage diff -r 7f3095ccfcb1 src/energy/model/li-ion-energy-source.h --- a/src/energy/model/li-ion-energy-source.h Thu May 05 12:17:12 2016 +0200 +++ b/src/energy/model/li-ion-energy-source.h Thu May 05 21:34:06 2016 -0400 @@ -90,6 +90,22 @@ * before simulation starts and is set only once per simulation. */ void SetInitialEnergy (double initialEnergyJ); + + /** + * \return The source energy capacity, in Joules. + * + * Implements GetEnergyCapacity. + */ + virtual double GetEnergyCapacity (void) const; + + /** + * \param The source energy capacity, in Joules. + * + * Sets the energy capacity of the source. + * Note that if the energy capacity is not set, it is assumed to be equal to + * the initial energy. + */ + void SetEnergyCapacity (double energyCapacityJ); /** * \returns Supply voltage at the energy source. @@ -182,6 +198,7 @@ private: double m_initialEnergyJ; // initial energy, in Joules + double m_energyCapacityJ; // energy capacity, in Joules TracedValue m_remainingEnergyJ; // remaining energy, in Joules double m_drainedCapacity; // capacity drained from the cell, in Ah double m_supplyVoltageV; // actual voltage of the cell diff -r 7f3095ccfcb1 src/energy/model/rv-battery-model.cc --- a/src/energy/model/rv-battery-model.cc Thu May 05 12:17:12 2016 +0200 +++ b/src/energy/model/rv-battery-model.cc Thu May 05 21:34:06 2016 -0400 @@ -115,6 +115,13 @@ } double +RvBatteryModel::GetEnergyCapacity (void) const +{ + NS_LOG_FUNCTION (this); + return m_alpha * GetSupplyVoltage (); +} + +double RvBatteryModel::GetSupplyVoltage (void) const { NS_LOG_FUNCTION (this); diff -r 7f3095ccfcb1 src/energy/model/rv-battery-model.h --- a/src/energy/model/rv-battery-model.h Thu May 05 12:17:12 2016 +0200 +++ b/src/energy/model/rv-battery-model.h Thu May 05 21:34:06 2016 -0400 @@ -60,6 +60,13 @@ * Implements GetInitialEnergy. */ virtual double GetInitialEnergy (void) const; + + /** + * \return The source energy capacity, in Joules. + * + * Implements GetEnergyCapacity. + */ + virtual double GetEnergyCapacity (void) const; /** * \returns Supply voltage at the energy source.