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
std::vector< Ptr< DeviceEnergyModel > >::const_iterator Iterator
#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:45
#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
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
void Add(DeviceEnergyModelContainer container)
Holds a vector of ns3::DeviceEnergyModel pointers.
void BreakDeviceEnergyModelRefCycle(void)
This function is called to break reference cycle between EnergySource and DeviceEnergyModel.
void NotifyEnergyRecharged(void)
This function notifies all DeviceEnergyModel of energy recharged event.
void NotifyEnergyChanged(void)
This function notifies all DeviceEnergyModel of energy changed event.
virtual void DoDispose(void)
All child&#39;s implementation must call BreakDeviceEnergyModelRefCycle to ensure reference cycles to Dev...
static TypeId GetTypeId(void)
double CalculateTotalCurrent(void)
void ConnectEnergyHarvester(Ptr< EnergyHarvester > energyHarvesterPtr)
void InitializeDeviceModels(void)
Calls Start () method of the device energy models.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void AppendDeviceEnergyModel(Ptr< DeviceEnergyModel > deviceEnergyModelPtr)
virtual ~EnergySource()
Ptr< Node > m_node
Pointer to node containing this EnergySource.
void NotifyEnergyDrained(void)
This function notifies all DeviceEnergyModel of energy depletion event.
Iterator End(void) const
Get an iterator which refers to the last DeviceEnergyModel pointer in the container.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:270
Iterator Begin(void) const
Get an iterator which refers to the first DeviceEnergyModel pointer in the container.
std::vector< Ptr< EnergyHarvester > > m_harvesters
Vector of EnergyHarvester pointer connected to the same energy source.
DeviceEnergyModelContainer m_models
List of device energy models installed on the same node.
void DisposeDeviceModels(void)
Calls Dispose () method of the device energy models.
DeviceEnergyModelContainer FindDeviceEnergyModels(TypeId tid)
A base class which provides memory management and object aggregation.
Definition: object.h:87
void SetNode(Ptr< Node > node)
Sets pointer to node containing this EnergySource.
a unique identifier for an interface.
Definition: type-id.h:58
Ptr< Node > GetNode(void) const
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
void Clear(void)
Removes all elements in the container.
virtual double GetSupplyVoltage(void) const =0