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 TypeId
36 {
37  static TypeId tid = TypeId ("ns3::BasicEnergySource")
39  .AddConstructor<BasicEnergySource> ()
40  .AddAttribute ("BasicEnergySourceInitialEnergyJ",
41  "Initial energy stored in basic energy source.",
42  DoubleValue (10), // in Joules
43  MakeDoubleAccessor (&BasicEnergySource::SetInitialEnergy,
45  MakeDoubleChecker<double> ())
46  .AddAttribute ("BasicEnergySupplyVoltageV",
47  "Initial supply voltage for basic energy source.",
48  DoubleValue (3.0), // in Volts
49  MakeDoubleAccessor (&BasicEnergySource::SetSupplyVoltage,
51  MakeDoubleChecker<double> ())
52  .AddAttribute ("PeriodicEnergyUpdateInterval",
53  "Time between two consecutive periodic energy updates.",
54  TimeValue (Seconds (1.0)),
57  MakeTimeChecker ())
58  .AddTraceSource ("RemainingEnergy",
59  "Remaining energy at BasicEnergySource.",
61  ;
62  return tid;
63 }
64 
66 {
67  m_lastUpdateTime = Seconds (0.0);
68 }
69 
71 {
72 }
73 
74 void
75 BasicEnergySource::SetInitialEnergy (double initialEnergyJ)
76 {
77  NS_LOG_FUNCTION (this << initialEnergyJ);
78  NS_ASSERT (initialEnergyJ >= 0);
79  m_initialEnergyJ = initialEnergyJ;
81 }
82 
83 void
84 BasicEnergySource::SetSupplyVoltage (double supplyVoltageV)
85 {
86  NS_LOG_FUNCTION (this << supplyVoltageV);
87  m_supplyVoltageV = supplyVoltageV;
88 }
89 
90 void
92 {
93  NS_LOG_FUNCTION (this << interval);
94  m_energyUpdateInterval = interval;
95 }
96 
97 Time
99 {
100  return m_energyUpdateInterval;
101 }
102 
103 double
105 {
106  return m_supplyVoltageV;
107 }
108 
109 double
111 {
112  return m_initialEnergyJ;
113 }
114 
115 double
117 {
118  NS_LOG_FUNCTION (this);
119  // update energy source to get the latest remaining energy.
121  return m_remainingEnergyJ;
122 }
123 
124 double
126 {
127  NS_LOG_FUNCTION (this);
128  // update energy source to get the latest remaining energy.
131 }
132 
133 void
135 {
136  NS_LOG_FUNCTION (this);
137  NS_LOG_DEBUG ("BasicEnergySource:Updating remaining energy.");
138 
139  // do not update if simulation has finished
140  if (Simulator::IsFinished ())
141  {
142  return;
143  }
144 
146 
148 
149  if (m_remainingEnergyJ <= 0)
150  {
152  return; // stop periodic update
153  }
154 
156 
159  this);
160 }
161 
162 /*
163  * Private functions start here.
164  */
165 
166 void
168 {
169  NS_LOG_FUNCTION (this);
170  UpdateEnergySource (); // start periodic update
171 }
172 
173 void
175 {
176  NS_LOG_FUNCTION (this);
177  BreakDeviceEnergyModelRefCycle (); // break reference cycle
178 }
179 
180 void
182 {
183  NS_LOG_FUNCTION (this);
184  NS_LOG_DEBUG ("BasicEnergySource:Energy depleted!");
185  NotifyEnergyDrained (); // notify DeviceEnergyModel objects
186  m_remainingEnergyJ = 0; // energy never goes below 0
187 }
188 
189 void
191 {
192  NS_LOG_FUNCTION (this);
193  double totalCurrentA = CalculateTotalCurrent ();
194  Time duration = Simulator::Now () - m_lastUpdateTime;
195  NS_ASSERT (duration.GetSeconds () >= 0);
196  // energy = current * voltage * time
197  double energyToDecreaseJ = totalCurrentA * m_supplyVoltageV * duration.GetSeconds ();
198  m_remainingEnergyJ -= energyToDecreaseJ;
199  NS_LOG_DEBUG ("BasicEnergySource:Remaining energy = " << m_remainingEnergyJ);
200 }
201 
202 } // namespace ns3