A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
29namespace ns3
30{
31namespace energy
32{
33
34NS_LOG_COMPONENT_DEFINE("EnergySource");
35NS_OBJECT_ENSURE_REGISTERED(EnergySource);
36
37TypeId
39{
40 static TypeId tid = TypeId("ns3::EnergySource").SetParent<Object>().SetGroupName("Energy");
41 return tid;
42}
43
45{
46 NS_LOG_FUNCTION(this);
47}
48
50{
51 NS_LOG_FUNCTION(this);
52}
53
54void
56{
57 NS_LOG_FUNCTION(this);
58 NS_ASSERT(node);
59 m_node = node;
60}
61
64{
65 return m_node;
66}
67
68void
70{
71 NS_LOG_FUNCTION(this << deviceEnergyModelPtr);
72 NS_ASSERT(deviceEnergyModelPtr); // model must exist
73 m_models.Add(deviceEnergyModelPtr);
74}
75
78{
79 NS_LOG_FUNCTION(this << tid);
82 for (i = m_models.Begin(); i != m_models.End(); i++)
83 {
84 if ((*i)->GetInstanceTypeId() == tid)
85 {
86 container.Add(*i);
87 }
88 }
89 return container;
90}
91
94{
95 NS_LOG_FUNCTION(this << name);
98 for (i = m_models.Begin(); i != m_models.End(); i++)
99 {
100 if ((*i)->GetInstanceTypeId().GetName() == name)
101 {
102 container.Add(*i);
103 }
104 }
105 return container;
106}
107
108void
110{
111 NS_LOG_FUNCTION(this);
112 /*
113 * Device models are not aggregated to the node, hence we have to manually
114 * call dispose method here.
115 */
117 for (i = m_models.Begin(); i != m_models.End(); i++)
118 {
119 (*i)->Initialize();
120 }
121}
122
123void
125{
126 NS_LOG_FUNCTION(this);
127 /*
128 * Device models are not aggregated to the node, hence we have to manually
129 * call dispose method here.
130 */
132 for (i = m_models.Begin(); i != m_models.End(); i++)
133 {
134 (*i)->Dispose();
135 }
136}
137
138void
140{
141 NS_LOG_FUNCTION(this << energyHarvesterPtr);
142 NS_ASSERT(energyHarvesterPtr); // energy harvester must exist
143 m_harvesters.push_back(energyHarvesterPtr);
144}
145
146/*
147 * Private function starts here.
148 */
149
150void
152{
153 NS_LOG_FUNCTION(this);
155}
156
157/*
158 * Protected functions start here.
159 */
160
161double
163{
164 NS_LOG_FUNCTION(this);
165 double totalCurrentA = 0.0;
167 for (i = m_models.Begin(); i != m_models.End(); i++)
168 {
169 totalCurrentA += (*i)->GetCurrentA();
170 }
171
172 if (!m_harvesters.empty())
173 {
174 double totalHarvestedPower = 0.0;
175
176 for (auto harvester = m_harvesters.begin(); harvester != m_harvesters.end(); harvester++)
177 {
178 totalHarvestedPower += (*harvester)->GetPower();
179 }
180
181 double supplyVoltage = GetSupplyVoltage();
182
183 if (supplyVoltage != 0)
184 {
185 double currentHarvestersA = totalHarvestedPower / supplyVoltage;
186 NS_LOG_DEBUG(" Total harvested power: " << totalHarvestedPower
187 << "| Current from harvesters: "
188 << currentHarvestersA);
189 totalCurrentA -= currentHarvestersA;
190 }
191 }
192
193 return totalCurrentA;
194}
195
196void
198{
199 NS_LOG_FUNCTION(this);
200 // notify all device energy models installed on node
202 for (i = m_models.Begin(); i != m_models.End(); i++)
203 {
204 (*i)->HandleEnergyDepletion();
205 }
206}
207
208void
210{
211 NS_LOG_FUNCTION(this);
212 // notify all device energy models installed on node
214 for (i = m_models.Begin(); i != m_models.End(); i++)
215 {
216 (*i)->HandleEnergyRecharged();
217 }
218}
219
220void
222{
223 NS_LOG_FUNCTION(this);
224 // notify all device energy models installed on node
226 for (i = m_models.Begin(); i != m_models.End(); i++)
227 {
228 (*i)->HandleEnergyChanged();
229 }
230}
231
232void
234{
235 NS_LOG_FUNCTION(this);
236 m_models.Clear();
237 m_harvesters.clear();
238 m_node = nullptr;
239}
240
241} // namespace energy
242} // namespace ns3
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
Holds a vector of ns3::DeviceEnergyModel pointers.
void Add(DeviceEnergyModelContainer container)
std::vector< Ptr< DeviceEnergyModel > >::const_iterator Iterator
Const iterator of DeviceEnergyModel container.
Iterator End() const
Get an iterator which refers to the last DeviceEnergyModel pointer in the container.
Iterator Begin() const
Get an iterator which refers to the first DeviceEnergyModel pointer in the container.
void Clear()
Removes all elements in the container.
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.
DeviceEnergyModelContainer FindDeviceEnergyModels(TypeId tid)
void ConnectEnergyHarvester(Ptr< EnergyHarvester > energyHarvesterPtr)
void InitializeDeviceModels()
Calls Start () method of the device energy models.
void NotifyEnergyRecharged()
This function notifies all DeviceEnergyModel of energy recharged event.
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.
static TypeId GetTypeId()
Get the type ID.
void DoDispose() override
All child's implementation must call BreakDeviceEnergyModelRefCycle to ensure reference cycles to Dev...
Ptr< Node > m_node
Pointer to node containing this EnergySource.
void SetNode(Ptr< Node > node)
Sets pointer to node containing this EnergySource.
void NotifyEnergyChanged()
This function notifies all DeviceEnergyModel of energy changed event.
void AppendDeviceEnergyModel(Ptr< DeviceEnergyModel > deviceEnergyModelPtr)
virtual double GetSupplyVoltage() const =0
void DisposeDeviceModels()
Calls Dispose () method of the device energy models.
Ptr< Node > GetNode() 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: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.