A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
basic-energy-source.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Authors: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu>
19  */
20 
21 #include "basic-energy-source.h"
22 #include "ns3/log.h"
23 #include "ns3/assert.h"
24 #include "ns3/double.h"
25 #include "ns3/trace-source-accessor.h"
26 #include "ns3/simulator.h"
27 
28 NS_LOG_COMPONENT_DEFINE ("BasicEnergySource");
29 
30 namespace ns3 {
31 
32 NS_OBJECT_ENSURE_REGISTERED (BasicEnergySource)
33  ;
34 
35 TypeId
37 {
38  static TypeId tid = TypeId ("ns3::BasicEnergySource")
40  .AddConstructor<BasicEnergySource> ()
41  .AddAttribute ("BasicEnergySourceInitialEnergyJ",
42  "Initial energy stored in basic energy source.",
43  DoubleValue (10), // in Joules
44  MakeDoubleAccessor (&BasicEnergySource::SetInitialEnergy,
46  MakeDoubleChecker<double> ())
47  .AddAttribute ("BasicEnergySupplyVoltageV",
48  "Initial supply voltage for basic energy source.",
49  DoubleValue (3.0), // in Volts
50  MakeDoubleAccessor (&BasicEnergySource::SetSupplyVoltage,
52  MakeDoubleChecker<double> ())
53  .AddAttribute ("PeriodicEnergyUpdateInterval",
54  "Time between two consecutive periodic energy updates.",
55  TimeValue (Seconds (1.0)),
58  MakeTimeChecker ())
59  .AddTraceSource ("RemainingEnergy",
60  "Remaining energy at BasicEnergySource.",
62  ;
63  return tid;
64 }
65 
67 {
68  NS_LOG_FUNCTION (this);
69  m_lastUpdateTime = Seconds (0.0);
70 }
71 
73 {
74  NS_LOG_FUNCTION (this);
75 }
76 
77 void
78 BasicEnergySource::SetInitialEnergy (double initialEnergyJ)
79 {
80  NS_LOG_FUNCTION (this << initialEnergyJ);
81  NS_ASSERT (initialEnergyJ >= 0);
82  m_initialEnergyJ = initialEnergyJ;
84 }
85 
86 void
87 BasicEnergySource::SetSupplyVoltage (double supplyVoltageV)
88 {
89  NS_LOG_FUNCTION (this << supplyVoltageV);
90  m_supplyVoltageV = supplyVoltageV;
91 }
92 
93 void
95 {
96  NS_LOG_FUNCTION (this << interval);
97  m_energyUpdateInterval = interval;
98 }
99 
100 Time
102 {
103  NS_LOG_FUNCTION (this);
104  return m_energyUpdateInterval;
105 }
106 
107 double
109 {
110  NS_LOG_FUNCTION (this);
111  return m_supplyVoltageV;
112 }
113 
114 double
116 {
117  NS_LOG_FUNCTION (this);
118  return m_initialEnergyJ;
119 }
120 
121 double
123 {
124  NS_LOG_FUNCTION (this);
125  // update energy source to get the latest remaining energy.
127  return m_remainingEnergyJ;
128 }
129 
130 double
132 {
133  NS_LOG_FUNCTION (this);
134  // update energy source to get the latest remaining energy.
137 }
138 
139 void
141 {
142  NS_LOG_FUNCTION (this);
143  NS_LOG_DEBUG ("BasicEnergySource:Updating remaining energy.");
144 
145  // do not update if simulation has finished
146  if (Simulator::IsFinished ())
147  {
148  return;
149  }
150 
152 
154 
155  if (m_remainingEnergyJ <= 0)
156  {
158  return; // stop periodic update
159  }
160 
162 
165  this);
166 }
167 
168 /*
169  * Private functions start here.
170  */
171 
172 void
174 {
175  NS_LOG_FUNCTION (this);
176  UpdateEnergySource (); // start periodic update
177 }
178 
179 void
181 {
182  NS_LOG_FUNCTION (this);
183  BreakDeviceEnergyModelRefCycle (); // break reference cycle
184 }
185 
186 void
188 {
189  NS_LOG_FUNCTION (this);
190  NS_LOG_DEBUG ("BasicEnergySource:Energy depleted!");
191  NotifyEnergyDrained (); // notify DeviceEnergyModel objects
192  m_remainingEnergyJ = 0; // energy never goes below 0
193 }
194 
195 void
197 {
198  NS_LOG_FUNCTION (this);
199  double totalCurrentA = CalculateTotalCurrent ();
200  Time duration = Simulator::Now () - m_lastUpdateTime;
201  NS_ASSERT (duration.GetSeconds () >= 0);
202  // energy = current * voltage * time
203  double energyToDecreaseJ = totalCurrentA * m_supplyVoltageV * duration.GetSeconds ();
204  m_remainingEnergyJ -= energyToDecreaseJ;
205  NS_LOG_DEBUG ("BasicEnergySource:Remaining energy = " << m_remainingEnergyJ);
206 }
207 
208 } // namespace ns3
void CalculateRemainingEnergy(void)
Calculates remaining energy.
NS_LOG_COMPONENT_DEFINE("BasicEnergySource")
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
Time GetEnergyUpdateInterval(void) const
virtual double GetSupplyVoltage(void) const
Energy source base class.
Definition: energy-source.h:71
virtual double GetRemainingEnergy(void)
#define NS_ASSERT(condition)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
virtual void UpdateEnergySource(void)
Implements UpdateEnergySource.
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:824
void HandleEnergyDrainedEvent(void)
Handles the remaining energy going to zero event.
void BreakDeviceEnergyModelRefCycle(void)
This function is called to break reference cycle between EnergySource and DeviceEnergyModel.
double GetSeconds(void) const
Definition: nstime.h:274
virtual double GetInitialEnergy(void) const
void DoDispose(void)
Defined in ns3::Object.
hold objects of type ns3::Time
Definition: nstime.h:961
TracedValue< double > m_remainingEnergyJ
static TypeId GetTypeId(void)
double CalculateTotalCurrent(void)
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
void DoInitialize(void)
Defined in ns3::Object.
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
void NotifyEnergyDrained(void)
This function notifies all DeviceEnergyModel of energy depletion event.
void SetSupplyVoltage(double supplyVoltageV)
void SetEnergyUpdateInterval(Time interval)
#define NS_LOG_DEBUG(msg)
Definition: log.h:289
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::cancel method.
Definition: event-id.cc:47
static bool IsFinished(void)
If there are no more events lefts to be scheduled, or if simulation time has already reached the "sto...
Definition: simulator.cc:150
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:452
void SetInitialEnergy(double initialEnergyJ)
Hold a floating point type.
Definition: double.h:41
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
virtual double GetEnergyFraction(void)