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  NS_LOG_FUNCTION (this);
67  return m_node;
68 }
69 
70 void
72 {
73  NS_LOG_FUNCTION (this << deviceEnergyModelPtr);
74  NS_ASSERT (deviceEnergyModelPtr != NULL); // model must exist
75  m_models.Add (deviceEnergyModelPtr);
76 }
77 
80 {
81  NS_LOG_FUNCTION (this << tid);
84  for (i = m_models.Begin (); i != m_models.End (); i++)
85  {
86  if ((*i)->GetInstanceTypeId () == tid)
87  {
88  container.Add (*i);
89  }
90  }
91  return container;
92 }
93 
96 {
97  NS_LOG_FUNCTION (this << name);
100  for (i = m_models.Begin (); i != m_models.End (); i++)
101  {
102  if ((*i)->GetInstanceTypeId ().GetName ().compare (name) == 0)
103  {
104  container.Add (*i);
105  }
106  }
107  return container;
108 }
109 
110 void
112 {
113  NS_LOG_FUNCTION (this);
114  /*
115  * Device models are not aggregated to the node, hence we have to manually
116  * call dispose method here.
117  */
119  for (i = m_models.Begin (); i != m_models.End (); i++)
120  {
121  (*i)->Initialize ();
122  }
123 }
124 
125 void
127 {
128  NS_LOG_FUNCTION (this);
129  /*
130  * Device models are not aggregated to the node, hence we have to manually
131  * call dispose method here.
132  */
134  for (i = m_models.Begin (); i != m_models.End (); i++)
135  {
136  (*i)->Dispose ();
137  }
138 }
139 
140 void
142 {
143  NS_LOG_FUNCTION (this << energyHarvesterPtr);
144  NS_ASSERT (energyHarvesterPtr != 0); // energy harvester must exist
145  m_harvesters.push_back (energyHarvesterPtr);
146 }
147 
148 /*
149  * Private function starts here.
150  */
151 
152 void
154 {
155  NS_LOG_FUNCTION (this);
157 }
158 
159 /*
160  * Protected functions start here.
161  */
162 
163 double
165 {
166  NS_LOG_FUNCTION (this);
167  double totalCurrentA = 0.0;
169  for (i = m_models.Begin (); i != m_models.End (); i++)
170  {
171  totalCurrentA += (*i)->GetCurrentA ();
172  }
173 
174  double totalHarvestedPower = 0.0;
175 
176  std::vector< Ptr<EnergyHarvester> >::const_iterator harvester;
177  for (harvester = m_harvesters.begin (); harvester != m_harvesters.end (); harvester++)
178  {
179  totalHarvestedPower += (*harvester)->GetPower ();
180  }
181 
182  NS_LOG_DEBUG ("EnergySource("<< GetNode ()->GetId () << "): Total harvested power = " << totalHarvestedPower);
183 
184  double currentHarvestersA = totalHarvestedPower / GetSupplyVoltage ();
185  NS_LOG_DEBUG ("EnergySource("<< GetNode ()->GetId () << "): Current from harvesters = " << currentHarvestersA);
186 
187  totalCurrentA -= currentHarvestersA;
188 
189  return totalCurrentA;
190 }
191 
192 void
194 {
195  NS_LOG_FUNCTION (this);
196  // notify all device energy models installed on node
198  for (i = m_models.Begin (); i != m_models.End (); i++)
199  {
200  (*i)->HandleEnergyDepletion ();
201  }
202 }
203 
204 void
206 {
207  NS_LOG_FUNCTION (this);
208  // notify all device energy models installed on node
210  for (i = m_models.Begin (); i != m_models.End (); i++)
211  {
212  (*i)->HandleEnergyRecharged ();
213  }
214 }
215 
216 void
218 {
219  NS_LOG_FUNCTION (this);
220  m_models.Clear ();
221  m_harvesters.clear ();
222  m_node = NULL;
223 }
224 
225 } // 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
Ptr< Node > GetNode(void) const
#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:201
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.
virtual void DoDispose(void)
All child's implementation must call BreakDeviceEnergyModelRefCycle to ensure reference cycles to Dev...
static TypeId GetTypeId(void)
double CalculateTotalCurrent(void)
void ConnectEnergyHarvester(Ptr< EnergyHarvester > energyHarvesterPtr)
Iterator Begin(void) const
Get an iterator which refers to the first DeviceEnergyModel pointer in the container.
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.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:269
Iterator End(void) const
Get an iterator which refers to the last 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
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:914
void Clear(void)
Removes all elements in the container.
virtual double GetSupplyVoltage(void) const =0