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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu>
7 *
8 * Copyright (c) 2014 Wireless Communications and Networking Group (WCNG),
9 * University of Rochester, Rochester, NY, USA.
10 *
11 * Modifications made by: Cristiano Tapparello <cristiano.tapparello@rochester.edu>
12 */
13
14#include "energy-source.h"
15
16#include <ns3/log.h>
17
18namespace ns3
19{
20namespace energy
21{
22
23NS_LOG_COMPONENT_DEFINE("EnergySource");
24NS_OBJECT_ENSURE_REGISTERED(EnergySource);
25
26TypeId
28{
29 static TypeId tid = TypeId("ns3::energy::EnergySource")
30 .AddDeprecatedName("ns3::EnergySource")
32 .SetGroupName("Energy");
33 return tid;
34}
35
40
45
46void
48{
49 NS_LOG_FUNCTION(this);
50 NS_ASSERT(node);
51 m_node = node;
52}
53
56{
57 return m_node;
58}
59
60void
62{
63 NS_LOG_FUNCTION(this << deviceEnergyModelPtr);
64 NS_ASSERT(deviceEnergyModelPtr); // model must exist
65 m_models.Add(deviceEnergyModelPtr);
66}
67
70{
71 NS_LOG_FUNCTION(this << tid);
74 for (i = m_models.Begin(); i != m_models.End(); i++)
75 {
76 if ((*i)->GetInstanceTypeId() == tid)
77 {
78 container.Add(*i);
79 }
80 }
81 return container;
82}
83
86{
87 NS_LOG_FUNCTION(this << name);
90 for (i = m_models.Begin(); i != m_models.End(); i++)
91 {
92 if ((*i)->GetInstanceTypeId().GetName() == name)
93 {
94 container.Add(*i);
95 }
96 }
97 return container;
98}
99
100void
102{
103 NS_LOG_FUNCTION(this);
104 /*
105 * Device models are not aggregated to the node, hence we have to manually
106 * call dispose method here.
107 */
109 for (i = m_models.Begin(); i != m_models.End(); i++)
110 {
111 (*i)->Initialize();
112 }
113}
114
115void
117{
118 NS_LOG_FUNCTION(this);
119 /*
120 * Device models are not aggregated to the node, hence we have to manually
121 * call dispose method here.
122 */
124 for (i = m_models.Begin(); i != m_models.End(); i++)
125 {
126 (*i)->Dispose();
127 }
128}
129
130void
132{
133 NS_LOG_FUNCTION(this << energyHarvesterPtr);
134 NS_ASSERT(energyHarvesterPtr); // energy harvester must exist
135 m_harvesters.push_back(energyHarvesterPtr);
136}
137
138/*
139 * Private function starts here.
140 */
141
142void
148
149/*
150 * Protected functions start here.
151 */
152
153double
155{
156 NS_LOG_FUNCTION(this);
157 double totalCurrentA = 0.0;
159 for (i = m_models.Begin(); i != m_models.End(); i++)
160 {
161 totalCurrentA += (*i)->GetCurrentA();
162 }
163
164 if (!m_harvesters.empty())
165 {
166 double totalHarvestedPower = 0.0;
167
168 for (auto harvester = m_harvesters.begin(); harvester != m_harvesters.end(); harvester++)
169 {
170 totalHarvestedPower += (*harvester)->GetPower();
171 }
172
173 double supplyVoltage = GetSupplyVoltage();
174
175 if (supplyVoltage != 0)
176 {
177 double currentHarvestersA = totalHarvestedPower / supplyVoltage;
178 NS_LOG_DEBUG(" Total harvested power: " << totalHarvestedPower
179 << "| Current from harvesters: "
180 << currentHarvestersA);
181 totalCurrentA -= currentHarvestersA;
182 }
183 }
184
185 return totalCurrentA;
186}
187
188void
190{
191 NS_LOG_FUNCTION(this);
192 // notify all device energy models installed on node
194 for (i = m_models.Begin(); i != m_models.End(); i++)
195 {
196 (*i)->HandleEnergyDepletion();
197 }
198}
199
200void
202{
203 NS_LOG_FUNCTION(this);
204 // notify all device energy models installed on node
206 for (i = m_models.Begin(); i != m_models.End(); i++)
207 {
208 (*i)->HandleEnergyRecharged();
209 }
210}
211
212void
214{
215 NS_LOG_FUNCTION(this);
216 // notify all device energy models installed on node
218 for (i = m_models.Begin(); i != m_models.End(); i++)
219 {
220 (*i)->HandleEnergyChanged();
221 }
222}
223
224void
226{
227 NS_LOG_FUNCTION(this);
228 m_models.Clear();
229 m_harvesters.clear();
230 m_node = nullptr;
231}
232
233} // namespace energy
234} // namespace ns3
A base class which provides memory management and object aggregation.
Definition object.h:78
Smart pointer class similar to boost::intrusive_ptr.
a unique identifier for an interface.
Definition type-id.h:48
TypeId AddDeprecatedName(const std::string &name)
Add an deprecated name for a TypeId.
Definition type-id.cc:862
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
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:55
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#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:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.