A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
acoustic-modem-energy-model.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Andrea Sacco
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Andrea Sacco <andrea.sacco85@gmail.com>
7 */
8
10
11#include "uan-net-device.h"
12#include "uan-phy.h"
13
14#include "ns3/double.h"
15#include "ns3/energy-source.h"
16#include "ns3/log.h"
17#include "ns3/simulator.h"
18#include "ns3/trace-source-accessor.h"
19
20namespace ns3
21{
22
23NS_LOG_COMPONENT_DEFINE("AcousticModemEnergyModel");
24
25NS_OBJECT_ENSURE_REGISTERED(AcousticModemEnergyModel);
26
27TypeId
29{
30 static TypeId tid =
31 TypeId("ns3::AcousticModemEnergyModel")
33 .AddConstructor<AcousticModemEnergyModel>()
34 .AddAttribute("TxPowerW",
35 "The modem Tx power in Watts",
36 DoubleValue(50),
40 .AddAttribute("RxPowerW",
41 "The modem Rx power in Watts",
42 DoubleValue(0.158),
46 .AddAttribute("IdlePowerW",
47 "The modem Idle power in Watts",
48 DoubleValue(0.158),
52 .AddAttribute("SleepPowerW",
53 "The modem Sleep power in Watts",
54 DoubleValue(0.0058),
58 .AddTraceSource(
59 "TotalEnergyConsumption",
60 "Total energy consumption of the modem device.",
62 "ns3::TracedValueCallback::Double");
63 return tid;
64}
65
75
79
80void
82{
83 NS_LOG_FUNCTION(this << node);
84 NS_ASSERT(node);
85 m_node = node;
86}
87
90{
91 return m_node;
92}
93
94void
101
102double
108
109double
111{
112 NS_LOG_FUNCTION(this);
113 return m_txPowerW;
114}
115
116void
118{
119 NS_LOG_FUNCTION(this << txPowerW);
120 m_txPowerW = txPowerW;
121}
122
123double
125{
126 NS_LOG_FUNCTION(this);
127 return m_rxPowerW;
128}
129
130void
132{
133 NS_LOG_FUNCTION(this << rxPowerW);
134 m_rxPowerW = rxPowerW;
135}
136
137double
143
144void
146{
147 NS_LOG_FUNCTION(this << idlePowerW);
148 m_idlePowerW = idlePowerW;
149}
150
151double
157
158void
160{
161 NS_LOG_FUNCTION(this << sleepPowerW);
162 m_sleepPowerW = sleepPowerW;
163}
164
165int
171
172void
174{
175 NS_LOG_FUNCTION(this);
176 if (callback.IsNull())
177 {
178 NS_LOG_DEBUG("AcousticModemEnergyModel:Setting NULL energy depletion callback!");
179 }
180 m_energyDepletionCallback = callback;
181}
182
183void
185{
186 NS_LOG_FUNCTION(this);
187 if (callback.IsNull())
188 {
189 NS_LOG_DEBUG("AcousticModemEnergyModel:Setting NULL energy recharge callback!");
190 }
191 m_energyRechargeCallback = callback;
192}
193
194void
196{
197 NS_LOG_FUNCTION(this << newState);
198 // NS_ASSERT (IsStateTransitionValid ((MicroModemState) newState));
199
200 Time duration = Simulator::Now() - m_lastUpdateTime;
201 NS_ASSERT(duration.GetNanoSeconds() >= 0); // check if duration is valid
202
203 // energy to decrease = current * voltage * time
204 double energyToDecrease = 0.0;
205
206 switch (m_currentState)
207 {
208 case UanPhy::TX:
209 energyToDecrease = duration.GetSeconds() * m_txPowerW;
210 break;
211 case UanPhy::RX:
212 energyToDecrease = duration.GetSeconds() * m_rxPowerW;
213 break;
214 case UanPhy::IDLE:
215 energyToDecrease = duration.GetSeconds() * m_idlePowerW;
216 break;
217 case UanPhy::SLEEP:
218 energyToDecrease = duration.GetSeconds() * m_sleepPowerW;
219 break;
220 case UanPhy::DISABLED:
221 energyToDecrease = 0;
222 break;
223 default:
224 NS_FATAL_ERROR("AcousticModemEnergyModel:Undefined radio state!");
225 }
226
227 // update total energy consumption
228 m_totalEnergyConsumption += energyToDecrease;
229
230 // update last update time stamp
232
233 // notify energy source
234 m_source->UpdateEnergySource();
235
237 {
238 // update current state & last update time stamp
239 SetMicroModemState(newState);
240 }
241
242 // some debug message
243 NS_LOG_DEBUG("AcousticModemEnergyModel:Total energy consumption at node #"
244 << m_node->GetId() << " is " << m_totalEnergyConsumption << "J");
245}
246
247void
249{
250 NS_LOG_FUNCTION(this);
251 NS_LOG_DEBUG("AcousticModemEnergyModel:Energy is depleted at node #" << m_node->GetId());
252 // invoke energy depletion callback, if set.
254 {
256 }
257 // invoke the phy energy depletion handler
258 Ptr<UanNetDevice> dev = m_node->GetDevice(0)->GetObject<UanNetDevice>();
259 dev->GetPhy()->EnergyDepletionHandler();
261}
262
263void
265{
266 NS_LOG_FUNCTION(this);
267 NS_LOG_DEBUG("AcousticModemEnergyModel:Energy is recharged at node #" << m_node->GetId());
268 // invoke energy recharge callback, if set.
270 {
272 }
273 // invoke the phy energy recharge handler
274 Ptr<UanNetDevice> dev = m_node->GetDevice(0)->GetObject<UanNetDevice>();
275 dev->GetPhy()->EnergyRechargeHandler();
277}
278
279void
281{
282 NS_LOG_FUNCTION(this);
283 // Not implemented
284}
285
286/*
287 * Private functions start here.
288 */
289
290void
298
299double
301{
302 NS_LOG_FUNCTION(this);
303
304 double supplyVoltage = m_source->GetSupplyVoltage();
305 NS_ASSERT(supplyVoltage != 0.0);
306 double stateCurrent = 0.0;
307 switch (m_currentState)
308 {
309 case UanPhy::TX:
310 stateCurrent = m_txPowerW / supplyVoltage;
311 break;
312 case UanPhy::RX:
313 stateCurrent = m_rxPowerW / supplyVoltage;
314 break;
315 case UanPhy::IDLE:
316 stateCurrent = m_idlePowerW / supplyVoltage;
317 break;
318 case UanPhy::SLEEP:
319 stateCurrent = m_sleepPowerW / supplyVoltage;
320 break;
321 case UanPhy::DISABLED:
322 stateCurrent = 0.0;
323 break;
324 default:
325 NS_FATAL_ERROR("AcousticModemEnergyModel:Undefined radio state!");
326 }
327
328 return stateCurrent;
329}
330
331bool
333{
334 NS_LOG_FUNCTION(this << destState);
335 return true;
336}
337
338void
340{
341 NS_LOG_FUNCTION(this);
342 if (IsStateTransitionValid(state))
343 {
344 m_currentState = state;
345 std::string stateName;
346 switch (state)
347 {
348 case UanPhy::TX:
349 stateName = "TX";
350 break;
351 case UanPhy::RX:
352 stateName = "RX";
353 break;
354 case UanPhy::IDLE:
355 stateName = "IDLE";
356 break;
357 case UanPhy::SLEEP:
358 stateName = "SLEEP";
359 break;
360 case UanPhy::DISABLED:
361 stateName = "DISABLED";
362 break;
363 }
364 NS_LOG_DEBUG("AcousticModemEnergyModel:Switching to state: " << stateName << " at time = "
365 << Simulator::Now());
366 }
367 else
368 {
369 NS_FATAL_ERROR("AcousticModemEnergyModel:Invalid state transition!");
370 }
371}
372
373} // namespace ns3
double GetTotalEnergyConsumption() const override
void SetEnergyRechargeCallback(AcousticModemEnergyRechargeCallback callback)
double m_rxPowerW
The receiver power, in watts.
double m_idlePowerW
The idle power, in watts.
void SetRxPowerW(double rxPowerW)
Set the receiving power of the modem.
Time m_lastUpdateTime
Time stamp of previous energy update.
void HandleEnergyRecharged() override
Handles energy recharged.
static TypeId GetTypeId()
Register this type.
double m_txPowerW
The transmitter power, in watts.
TracedValue< double > m_totalEnergyConsumption
The total energy consumed by this model.
void SetEnergySource(Ptr< energy::EnergySource > source) override
virtual Ptr< Node > GetNode() const
Gets pointer to node.
void HandleEnergyDepletion() override
Handles energy depletion.
virtual void SetNode(Ptr< Node > node)
Sets pointer to node.
~AcousticModemEnergyModel() override
Dummy destructor, see DoDispose.
AcousticModemEnergyDepletionCallback m_energyDepletionCallback
Energy depletion callback.
double GetTxPowerW() const
Get the transmission power of the modem.
void ChangeState(int newState) override
Changes state of the AcousticModemEnergyModel.
void SetIdlePowerW(double idlePowerW)
Set the idle state power of the modem.
Ptr< energy::EnergySource > m_source
The energy source.
void DoDispose() override
Destructor implementation.
double m_sleepPowerW
The sleep power, in watts.
void SetSleepPowerW(double sleepPowerW)
Set the sleep power of the modem.
void SetEnergyDepletionCallback(AcousticModemEnergyDepletionCallback callback)
double GetRxPowerW() const
Get the receiving power.
double GetSleepPowerW() const
Get the sleep state power of the modem.
double GetIdlePowerW() const
Get the idle power of the modem.
int GetCurrentState() const
Get the current state of the modem.
AcousticModemEnergyRechargeCallback m_energyRechargeCallback
Energy recharge callback.
void SetTxPowerW(double txPowerW)
Set the transmission power of the modem.
void HandleEnergyChanged() override
Handles energy changed.
bool IsStateTransitionValid(const int destState)
Ptr< Node > m_node
The node hosting this transducer.
void Nullify()
Discard the implementation, set it to null.
Definition callback.h:561
bool IsNull() const
Check for null implementation.
Definition callback.h:555
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
uint32_t GetId() const
Definition node.cc:106
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition node.cc:138
Smart pointer class similar to boost::intrusive_ptr.
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:407
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Net device for UAN models.
@ RX
Receiving.
Definition uan-phy.h:174
@ SLEEP
Sleeping.
Definition uan-phy.h:176
@ IDLE
Idle state.
Definition uan-phy.h:172
@ DISABLED
Disabled.
Definition uan-phy.h:177
@ TX
Transmitting.
Definition uan-phy.h:175
Base class for device energy models.
#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_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeDoubleChecker()
Definition double.h:82
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition double.h:32