A Discrete-Event Network Simulator
API
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  * Author: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu>
19  *
20  * Copyright (c) 2014 Wireless Communications and Networking Group (WCNG),
21  * University of Rochester, Rochester, NY, USA.
22  *
23  * Modifications made by: Cristiano Tapparello <cristiano.tapparello@rochester.edu>
24  */
25 
26 #include "energy-source.h"
27 #include "ns3/log.h"
28 
29 namespace ns3 {
30 
31 NS_LOG_COMPONENT_DEFINE ("EnergySource");
32 
33 NS_OBJECT_ENSURE_REGISTERED (EnergySource);
34 
35 TypeId
37 {
38  static TypeId tid = TypeId ("ns3::EnergySource")
39  .SetParent<Object> ()
40  .SetGroupName ("Energy")
41  ;
42  return tid;
43 }
44 
46 {
47  NS_LOG_FUNCTION (this);
48 }
49 
51 {
52  NS_LOG_FUNCTION (this);
53 }
54 
55 void
57 {
58  NS_LOG_FUNCTION (this);
59  NS_ASSERT (node != NULL);
60  m_node = node;
61 }
62 
65 {
66  return m_node;
67 }
68 
69 void
71 {
72  NS_LOG_FUNCTION (this << deviceEnergyModelPtr);
73  NS_ASSERT (deviceEnergyModelPtr != NULL); // model must exist
74  m_models.Add (deviceEnergyModelPtr);
75 }
76 
79 {
80  NS_LOG_FUNCTION (this << tid);
83  for (i = m_models.Begin (); i != m_models.End (); i++)
84  {
85  if ((*i)->GetInstanceTypeId () == tid)
86  {
87  container.Add (*i);
88  }
89  }
90  return container;
91 }
92 
95 {
96  NS_LOG_FUNCTION (this << name);
99  for (i = m_models.Begin (); i != m_models.End (); i++)
100  {
101  if ((*i)->GetInstanceTypeId ().GetName ().compare (name) == 0)
102  {
103  container.Add (*i);
104  }
105  }
106  return container;
107 }
108 
109 void
111 {
112  NS_LOG_FUNCTION (this);
113  /*
114  * Device models are not aggregated to the node, hence we have to manually
115  * call dispose method here.
116  */
118  for (i = m_models.Begin (); i != m_models.End (); i++)
119  {
120  (*i)->Initialize ();
121  }
122 }
123 
124 void
126 {
127  NS_LOG_FUNCTION (this);
128  /*
129  * Device models are not aggregated to the node, hence we have to manually
130  * call dispose method here.
131  */
133  for (i = m_models.Begin (); i != m_models.End (); i++)
134  {
135  (*i)->Dispose ();
136  }
137 }
138 
139 void
141 {
142  NS_LOG_FUNCTION (this << energyHarvesterPtr);
143  NS_ASSERT (energyHarvesterPtr != 0); // energy harvester must exist
144  m_harvesters.push_back (energyHarvesterPtr);
145 }
146 
147 /*
148  * Private function starts here.
149  */
150 
151 void
153 {
154  NS_LOG_FUNCTION (this);
156 }
157 
158 /*
159  * Protected functions start here.
160  */
161 
162 double
164 {
165  NS_LOG_FUNCTION (this);
166  double totalCurrentA = 0.0;
168  for (i = m_models.Begin (); i != m_models.End (); i++)
169  {
170  totalCurrentA += (*i)->GetCurrentA ();
171  }
172 
173  double totalHarvestedPower = 0.0;
174 
175  std::vector< Ptr<EnergyHarvester> >::const_iterator harvester;
176  for (harvester = m_harvesters.begin (); harvester != m_harvesters.end (); harvester++)
177  {
178  totalHarvestedPower += (*harvester)->GetPower ();
179  }
180 
181  NS_LOG_DEBUG ("EnergySource("<< GetNode ()->GetId () << "): Total harvested power = " << totalHarvestedPower);
182 
183  double currentHarvestersA = totalHarvestedPower / GetSupplyVoltage ();
184  NS_LOG_DEBUG ("EnergySource("<< GetNode ()->GetId () << "): Current from harvesters = " << currentHarvestersA);
185 
186  totalCurrentA -= currentHarvestersA;
187 
188  return totalCurrentA;
189 }
190 
191 void
193 {
194  NS_LOG_FUNCTION (this);
195  // notify all device energy models installed on node
197  for (i = m_models.Begin (); i != m_models.End (); i++)
198  {
199  (*i)->HandleEnergyDepletion ();
200  }
201 }
202 
203 void
205 {
206  NS_LOG_FUNCTION (this);
207  // notify all device energy models installed on node
209  for (i = m_models.Begin (); i != m_models.End (); i++)
210  {
211  (*i)->HandleEnergyRecharged ();
212  }
213 }
214 
215 void
217 {
218  NS_LOG_FUNCTION (this);
219  // notify all device energy models installed on node
221  for (i = m_models.Begin (); i != m_models.End (); i++)
222  {
223  (*i)->HandleEnergyChanged ();
224  }
225 }
226 
227 void
229 {
230  NS_LOG_FUNCTION (this);
231  m_models.Clear ();
232  m_harvesters.clear ();
233  m_node = NULL;
234 }
235 
236 } // namespace ns3
ns3::DeviceEnergyModelContainer::End
Iterator End(void) const
Get an iterator which refers to the last DeviceEnergyModel pointer in the container.
Definition: device-energy-model-container.cc:67
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::EnergySource::m_harvesters
std::vector< Ptr< EnergyHarvester > > m_harvesters
Vector of EnergyHarvester pointer connected to the same energy source.
Definition: energy-source.h:210
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
NS_ASSERT
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::DeviceEnergyModelContainer::Begin
Iterator Begin(void) const
Get an iterator which refers to the first DeviceEnergyModel pointer in the container.
Definition: device-energy-model-container.cc:60
ns3::EnergySource::NotifyEnergyChanged
void NotifyEnergyChanged(void)
This function notifies all DeviceEnergyModel of energy changed event.
Definition: energy-source.cc:216
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::EnergySource::GetNode
Ptr< Node > GetNode(void) const
Definition: energy-source.cc:64
ns3::EnergySource::NotifyEnergyDrained
void NotifyEnergyDrained(void)
This function notifies all DeviceEnergyModel of energy depletion event.
Definition: energy-source.cc:192
ns3::Ptr< Node >
ns3::EnergySource::CalculateTotalCurrent
double CalculateTotalCurrent(void)
Definition: energy-source.cc:163
ns3::Object
A base class which provides memory management and object aggregation.
Definition: object.h:88
ns3::EnergySource::~EnergySource
virtual ~EnergySource()
Definition: energy-source.cc:50
ns3::EnergySource::InitializeDeviceModels
void InitializeDeviceModels(void)
Calls Start () method of the device energy models.
Definition: energy-source.cc:110
ns3::EnergySource::SetNode
void SetNode(Ptr< Node > node)
Sets pointer to node containing this EnergySource.
Definition: energy-source.cc:56
ns3::DeviceEnergyModelContainer::Iterator
std::vector< Ptr< DeviceEnergyModel > >::const_iterator Iterator
Definition: device-energy-model-container.h:46
energy-source.h
ns3::DeviceEnergyModelContainer
Holds a vector of ns3::DeviceEnergyModel pointers.
Definition: device-energy-model-container.h:44
ns3::EnergySource::BreakDeviceEnergyModelRefCycle
void BreakDeviceEnergyModelRefCycle(void)
This function is called to break reference cycle between EnergySource and DeviceEnergyModel.
Definition: energy-source.cc:228
NS_LOG_DEBUG
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
ns3::EnergySource::DisposeDeviceModels
void DisposeDeviceModels(void)
Calls Dispose () method of the device energy models.
Definition: energy-source.cc:125
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::EnergySource::GetSupplyVoltage
virtual double GetSupplyVoltage(void) const =0
ns3::EnergySource::m_node
Ptr< Node > m_node
Pointer to node containing this EnergySource.
Definition: energy-source.h:203
ns3::DeviceEnergyModelContainer::Clear
void Clear(void)
Removes all elements in the container.
Definition: device-energy-model-container.cc:115
ns3::EnergySource::NotifyEnergyRecharged
void NotifyEnergyRecharged(void)
This function notifies all DeviceEnergyModel of energy recharged event.
Definition: energy-source.cc:204
ns3::EnergySource::EnergySource
EnergySource()
Definition: energy-source.cc:45
ns3::EnergySource::DoDispose
virtual void DoDispose(void)
All child's implementation must call BreakDeviceEnergyModelRefCycle to ensure reference cycles to Dev...
Definition: energy-source.cc:152
ns3::EnergySource::GetTypeId
static TypeId GetTypeId(void)
Definition: energy-source.cc:36
ns3::EnergySource::FindDeviceEnergyModels
DeviceEnergyModelContainer FindDeviceEnergyModels(TypeId tid)
Definition: energy-source.cc:78
ns3::DeviceEnergyModelContainer::Add
void Add(DeviceEnergyModelContainer container)
Definition: device-energy-model-container.cc:88
ns3::EnergySource::m_models
DeviceEnergyModelContainer m_models
List of device energy models installed on the same node.
Definition: energy-source.h:197
ns3::EnergySource::AppendDeviceEnergyModel
void AppendDeviceEnergyModel(Ptr< DeviceEnergyModel > deviceEnergyModelPtr)
Definition: energy-source.cc:70
ns3::EnergySource::ConnectEnergyHarvester
void ConnectEnergyHarvester(Ptr< EnergyHarvester > energyHarvesterPtr)
Definition: energy-source.cc:140