A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
basic-energy-source.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu>
7 */
8
10
11#include "ns3/assert.h"
12#include "ns3/double.h"
13#include "ns3/log.h"
14#include "ns3/simulator.h"
15#include "ns3/trace-source-accessor.h"
16
17namespace ns3
18{
19namespace energy
20{
21
22NS_LOG_COMPONENT_DEFINE("BasicEnergySource");
23NS_OBJECT_ENSURE_REGISTERED(BasicEnergySource);
24
25TypeId
27{
28 static TypeId tid =
29 TypeId("ns3::energy::BasicEnergySource")
30 .AddDeprecatedName("ns3::BasicEnergySource")
32 .SetGroupName("Energy")
33 .AddConstructor<BasicEnergySource>()
34 .AddAttribute("BasicEnergySourceInitialEnergyJ",
35 "Initial energy stored in basic energy source.",
36 DoubleValue(10), // in Joules
40 .AddAttribute("BasicEnergySupplyVoltageV",
41 "Initial supply voltage for basic energy source.",
42 DoubleValue(3.0), // in Volts
46 .AddAttribute("BasicEnergyLowBatteryThreshold",
47 "Low battery threshold for basic energy source.",
48 DoubleValue(0.10), // as a fraction of the initial energy
51 .AddAttribute("BasicEnergyHighBatteryThreshold",
52 "High battery threshold for basic energy source.",
53 DoubleValue(0.15), // as a fraction of the initial energy
56 .AddAttribute("PeriodicEnergyUpdateInterval",
57 "Time between two consecutive periodic energy updates.",
58 TimeValue(Seconds(1.0)),
62 .AddTraceSource("RemainingEnergy",
63 "Remaining energy at BasicEnergySource.",
65 "ns3::TracedValueCallback::Double");
66 return tid;
67}
68
75
80
81void
83{
84 NS_LOG_FUNCTION(this << initialEnergyJ);
85 NS_ASSERT(initialEnergyJ >= 0);
86 m_initialEnergyJ = initialEnergyJ;
88}
89
90void
92{
93 NS_LOG_FUNCTION(this << supplyVoltageV);
94 m_supplyVoltageV = supplyVoltageV;
95}
96
97void
99{
100 NS_LOG_FUNCTION(this << interval);
101 m_energyUpdateInterval = interval;
102}
103
104Time
110
111double
117
118double
124
125double
127{
128 NS_LOG_FUNCTION(this);
129 // update energy source to get the latest remaining energy.
131 return m_remainingEnergyJ;
132}
133
134double
136{
137 NS_LOG_FUNCTION(this);
138 // update energy source to get the latest remaining energy.
141}
142
143void
145{
146 NS_LOG_FUNCTION(this);
147 NS_LOG_DEBUG("BasicEnergySource:Updating remaining energy.");
148
149 double remainingEnergy = m_remainingEnergyJ;
151
153
155 {
156 m_depleted = true;
158 }
160 {
161 m_depleted = false;
163 }
164 else if (m_remainingEnergyJ != remainingEnergy)
165 {
167 }
168
170 {
173 this);
174 }
175}
176
177/*
178 * Private functions start here.
179 */
180
181void
183{
184 NS_LOG_FUNCTION(this);
185 UpdateEnergySource(); // start periodic update
186}
187
188void
190{
191 NS_LOG_FUNCTION(this);
192 BreakDeviceEnergyModelRefCycle(); // break reference cycle
193}
194
195void
197{
198 NS_LOG_FUNCTION(this);
199 NS_LOG_DEBUG("BasicEnergySource:Energy depleted!");
200 NotifyEnergyDrained(); // notify DeviceEnergyModel objects
201}
202
203void
205{
206 NS_LOG_FUNCTION(this);
207 NS_LOG_DEBUG("BasicEnergySource:Energy recharged!");
208 NotifyEnergyRecharged(); // notify DeviceEnergyModel objects
209}
210
211void
213{
214 NS_LOG_FUNCTION(this);
215 double totalCurrentA = CalculateTotalCurrent();
216 Time duration = Simulator::Now() - m_lastUpdateTime;
217 NS_ASSERT(duration.IsPositive());
218 // energy = current * voltage * time
219 double energyToDecreaseJ = (totalCurrentA * m_supplyVoltageV * duration).GetSeconds();
220 NS_ASSERT(m_remainingEnergyJ >= energyToDecreaseJ);
221 m_remainingEnergyJ -= energyToDecreaseJ;
222 NS_LOG_DEBUG("BasicEnergySource:Remaining energy = " << m_remainingEnergyJ);
223}
224
225} // namespace energy
226} // namespace ns3
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition event-id.cc:58
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
bool IsPositive() const
Exactly equivalent to t >= 0.
Definition nstime.h:322
a unique identifier for an interface.
Definition type-id.h:48
TypeId AddDeprecatedName(const std::string &name)
Add an deprecated name for a TypeId.
Definition type-id.cc:862
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
BasicEnergySource decreases/increases remaining energy stored in itself in linearly.
double m_lowBatteryTh
low battery threshold, as a fraction of the initial energy
double m_initialEnergyJ
initial energy, in Joules
static TypeId GetTypeId()
Get the type ID.
double GetSupplyVoltage() const override
Time m_energyUpdateInterval
energy update interval
EventId m_energyUpdateEvent
energy update event
void SetInitialEnergy(double initialEnergyJ)
bool m_depleted
set to true when the remaining energy goes below the low threshold, set to false again when the remai...
TracedValue< double > m_remainingEnergyJ
remaining energy, in Joules
void DoDispose() override
Defined in ns3::Object.
void SetSupplyVoltage(double supplyVoltageV)
void SetEnergyUpdateInterval(Time interval)
double GetInitialEnergy() const override
void HandleEnergyDrainedEvent()
Handles the remaining energy going to zero event.
void UpdateEnergySource() override
Implements UpdateEnergySource.
void CalculateRemainingEnergy()
Calculates remaining energy.
double m_highBatteryTh
high battery threshold, as a fraction of the initial energy
void HandleEnergyRechargedEvent()
Handles the remaining energy exceeding the high threshold after it went below the low threshold.
void DoInitialize() override
Defined in ns3::Object.
double m_supplyVoltageV
supply voltage, in Volts
Energy source base class.
void BreakDeviceEnergyModelRefCycle()
This function is called to break reference cycle between EnergySource and DeviceEnergyModel.
void NotifyEnergyDrained()
This function notifies all DeviceEnergyModel of energy depletion event.
void NotifyEnergyRecharged()
This function notifies all DeviceEnergyModel of energy recharged event.
void NotifyEnergyChanged()
This function notifies all DeviceEnergyModel of energy changed event.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition nstime.h:1396
Ptr< const AttributeChecker > MakeDoubleChecker()
Definition double.h:82
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition double.h:32
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition nstime.h:1416