A Discrete-Event Network Simulator
API
energy-source.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu>
18  *
19  * Copyright (c) 2014 Wireless Communications and Networking Group (WCNG),
20  * University of Rochester, Rochester, NY, USA.
21  *
22  * Modifications made by: Cristiano Tapparello <cristiano.tapparello@rochester.edu>
23  */
24 
25 #include "energy-source.h"
26 
27 #include <ns3/log.h>
28 
29 namespace ns3
30 {
31 
32 NS_LOG_COMPONENT_DEFINE("EnergySource");
33 
34 NS_OBJECT_ENSURE_REGISTERED(EnergySource);
35 
36 TypeId
38 {
39  static TypeId tid = TypeId("ns3::EnergySource").SetParent<Object>().SetGroupName("Energy");
40  return tid;
41 }
42 
44 {
45  NS_LOG_FUNCTION(this);
46 }
47 
49 {
50  NS_LOG_FUNCTION(this);
51 }
52 
53 void
55 {
56  NS_LOG_FUNCTION(this);
57  NS_ASSERT(node);
58  m_node = node;
59 }
60 
63 {
64  return m_node;
65 }
66 
67 void
69 {
70  NS_LOG_FUNCTION(this << deviceEnergyModelPtr);
71  NS_ASSERT(deviceEnergyModelPtr); // model must exist
72  m_models.Add(deviceEnergyModelPtr);
73 }
74 
77 {
78  NS_LOG_FUNCTION(this << tid);
81  for (i = m_models.Begin(); i != m_models.End(); i++)
82  {
83  if ((*i)->GetInstanceTypeId() == tid)
84  {
85  container.Add(*i);
86  }
87  }
88  return container;
89 }
90 
93 {
94  NS_LOG_FUNCTION(this << name);
97  for (i = m_models.Begin(); i != m_models.End(); i++)
98  {
99  if ((*i)->GetInstanceTypeId().GetName() == name)
100  {
101  container.Add(*i);
102  }
103  }
104  return container;
105 }
106 
107 void
109 {
110  NS_LOG_FUNCTION(this);
111  /*
112  * Device models are not aggregated to the node, hence we have to manually
113  * call dispose method here.
114  */
116  for (i = m_models.Begin(); i != m_models.End(); i++)
117  {
118  (*i)->Initialize();
119  }
120 }
121 
122 void
124 {
125  NS_LOG_FUNCTION(this);
126  /*
127  * Device models are not aggregated to the node, hence we have to manually
128  * call dispose method here.
129  */
131  for (i = m_models.Begin(); i != m_models.End(); i++)
132  {
133  (*i)->Dispose();
134  }
135 }
136 
137 void
139 {
140  NS_LOG_FUNCTION(this << energyHarvesterPtr);
141  NS_ASSERT(energyHarvesterPtr); // energy harvester must exist
142  m_harvesters.push_back(energyHarvesterPtr);
143 }
144 
145 /*
146  * Private function starts here.
147  */
148 
149 void
151 {
152  NS_LOG_FUNCTION(this);
154 }
155 
156 /*
157  * Protected functions start here.
158  */
159 
160 double
162 {
163  NS_LOG_FUNCTION(this);
164  double totalCurrentA = 0.0;
166  for (i = m_models.Begin(); i != m_models.End(); i++)
167  {
168  totalCurrentA += (*i)->GetCurrentA();
169  }
170 
171  if (!m_harvesters.empty())
172  {
173  double totalHarvestedPower = 0.0;
174 
175  for (auto harvester = m_harvesters.begin(); harvester != m_harvesters.end(); harvester++)
176  {
177  totalHarvestedPower += (*harvester)->GetPower();
178  }
179 
180  double supplyVoltage = GetSupplyVoltage();
181 
182  if (supplyVoltage != 0)
183  {
184  double currentHarvestersA = totalHarvestedPower / supplyVoltage;
185  NS_LOG_DEBUG(" Total harvested power: " << totalHarvestedPower
186  << "| Current from harvesters: "
187  << currentHarvestersA);
188  totalCurrentA -= currentHarvestersA;
189  }
190  }
191 
192  return totalCurrentA;
193 }
194 
195 void
197 {
198  NS_LOG_FUNCTION(this);
199  // notify all device energy models installed on node
201  for (i = m_models.Begin(); i != m_models.End(); i++)
202  {
203  (*i)->HandleEnergyDepletion();
204  }
205 }
206 
207 void
209 {
210  NS_LOG_FUNCTION(this);
211  // notify all device energy models installed on node
213  for (i = m_models.Begin(); i != m_models.End(); i++)
214  {
215  (*i)->HandleEnergyRecharged();
216  }
217 }
218 
219 void
221 {
222  NS_LOG_FUNCTION(this);
223  // notify all device energy models installed on node
225  for (i = m_models.Begin(); i != m_models.End(); i++)
226  {
227  (*i)->HandleEnergyChanged();
228  }
229 }
230 
231 void
233 {
234  NS_LOG_FUNCTION(this);
235  m_models.Clear();
236  m_harvesters.clear();
237  m_node = nullptr;
238 }
239 
240 } // namespace ns3
Holds a vector of ns3::DeviceEnergyModel pointers.
void Clear()
Removes all elements in the container.
void Add(DeviceEnergyModelContainer container)
std::vector< Ptr< DeviceEnergyModel > >::const_iterator Iterator
Const iterator of DeviceEnergyModel container.
Iterator Begin() const
Get an iterator which refers to the first DeviceEnergyModel pointer in the container.
Iterator End() const
Get an iterator which refers to the last DeviceEnergyModel pointer in the container.
void AppendDeviceEnergyModel(Ptr< DeviceEnergyModel > deviceEnergyModelPtr)
void ConnectEnergyHarvester(Ptr< EnergyHarvester > energyHarvesterPtr)
void DisposeDeviceModels()
Calls Dispose () method of the device energy models.
double CalculateTotalCurrent()
void NotifyEnergyRecharged()
This function notifies all DeviceEnergyModel of energy recharged event.
void InitializeDeviceModels()
Calls Start () method of the device energy models.
DeviceEnergyModelContainer m_models
List of device energy models installed on the same node.
Ptr< Node > m_node
Pointer to node containing this EnergySource.
void NotifyEnergyChanged()
This function notifies all DeviceEnergyModel of energy changed event.
void BreakDeviceEnergyModelRefCycle()
This function is called to break reference cycle between EnergySource and DeviceEnergyModel.
void NotifyEnergyDrained()
This function notifies all DeviceEnergyModel of energy depletion event.
std::vector< Ptr< EnergyHarvester > > m_harvesters
Vector of EnergyHarvester pointer connected to the same energy source.
Ptr< Node > GetNode() const
static TypeId GetTypeId()
Get the type ID.
DeviceEnergyModelContainer FindDeviceEnergyModels(TypeId tid)
~EnergySource() override
void DoDispose() override
All child's implementation must call BreakDeviceEnergyModelRefCycle to ensure reference cycles to Dev...
void SetNode(Ptr< Node > node)
Sets pointer to node containing this EnergySource.
virtual double GetSupplyVoltage() const =0
A base class which provides memory management and object aggregation.
Definition: object.h:89
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#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:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.