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 <ns3/energy-source.h>
26#include <ns3/log.h>
27
28namespace ns3
29{
30
31NS_LOG_COMPONENT_DEFINE("EnergySource");
32
33NS_OBJECT_ENSURE_REGISTERED(EnergySource);
34
35TypeId
37{
38 static TypeId tid = TypeId("ns3::EnergySource").SetParent<Object>().SetGroupName("Energy");
39 return tid;
40}
41
43{
44 NS_LOG_FUNCTION(this);
45}
46
48{
49 NS_LOG_FUNCTION(this);
50}
51
52void
54{
55 NS_LOG_FUNCTION(this);
56 NS_ASSERT(node);
57 m_node = node;
58}
59
62{
63 return m_node;
64}
65
66void
68{
69 NS_LOG_FUNCTION(this << deviceEnergyModelPtr);
70 NS_ASSERT(deviceEnergyModelPtr); // model must exist
71 m_models.Add(deviceEnergyModelPtr);
72}
73
76{
77 NS_LOG_FUNCTION(this << tid);
80 for (i = m_models.Begin(); i != m_models.End(); i++)
81 {
82 if ((*i)->GetInstanceTypeId() == tid)
83 {
84 container.Add(*i);
85 }
86 }
87 return container;
88}
89
92{
93 NS_LOG_FUNCTION(this << name);
96 for (i = m_models.Begin(); i != m_models.End(); i++)
97 {
98 if ((*i)->GetInstanceTypeId().GetName() == name)
99 {
100 container.Add(*i);
101 }
102 }
103 return container;
104}
105
106void
108{
109 NS_LOG_FUNCTION(this);
110 /*
111 * Device models are not aggregated to the node, hence we have to manually
112 * call dispose method here.
113 */
115 for (i = m_models.Begin(); i != m_models.End(); i++)
116 {
117 (*i)->Initialize();
118 }
119}
120
121void
123{
124 NS_LOG_FUNCTION(this);
125 /*
126 * Device models are not aggregated to the node, hence we have to manually
127 * call dispose method here.
128 */
130 for (i = m_models.Begin(); i != m_models.End(); i++)
131 {
132 (*i)->Dispose();
133 }
134}
135
136void
138{
139 NS_LOG_FUNCTION(this << energyHarvesterPtr);
140 NS_ASSERT(energyHarvesterPtr); // energy harvester must exist
141 m_harvesters.push_back(energyHarvesterPtr);
142}
143
144/*
145 * Private function starts here.
146 */
147
148void
150{
151 NS_LOG_FUNCTION(this);
153}
154
155/*
156 * Protected functions start here.
157 */
158
159double
161{
162 NS_LOG_FUNCTION(this);
163 double totalCurrentA = 0.0;
165 for (i = m_models.Begin(); i != m_models.End(); i++)
166 {
167 totalCurrentA += (*i)->GetCurrentA();
168 }
169
170 if (!m_harvesters.empty())
171 {
172 double totalHarvestedPower = 0.0;
173
174 std::vector<Ptr<EnergyHarvester>>::const_iterator harvester;
175 for (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
195void
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
207void
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
219void
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
231void
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.
std::vector< Ptr< DeviceEnergyModel > >::const_iterator Iterator
Const iterator of DeviceEnergyModel container.
void Add(DeviceEnergyModelContainer 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
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:936
#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.