A Discrete-Event Network Simulator
API
acoustic-modem-energy-model.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Andrea Sacco
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: Andrea Sacco <andrea.sacco85@gmail.com>
18  */
19 
21 
22 #include "uan-net-device.h"
23 #include "uan-phy.h"
24 
25 #include "ns3/double.h"
26 #include "ns3/energy-source.h"
27 #include "ns3/log.h"
28 #include "ns3/simulator.h"
29 #include "ns3/trace-source-accessor.h"
30 
31 namespace ns3
32 {
33 
34 NS_LOG_COMPONENT_DEFINE("AcousticModemEnergyModel");
35 
36 NS_OBJECT_ENSURE_REGISTERED(AcousticModemEnergyModel);
37 
38 TypeId
40 {
41  static TypeId tid =
42  TypeId("ns3::AcousticModemEnergyModel")
44  .AddConstructor<AcousticModemEnergyModel>()
45  .AddAttribute("TxPowerW",
46  "The modem Tx power in Watts",
47  DoubleValue(50),
50  MakeDoubleChecker<double>())
51  .AddAttribute("RxPowerW",
52  "The modem Rx power in Watts",
53  DoubleValue(0.158),
56  MakeDoubleChecker<double>())
57  .AddAttribute("IdlePowerW",
58  "The modem Idle power in Watts",
59  DoubleValue(0.158),
62  MakeDoubleChecker<double>())
63  .AddAttribute("SleepPowerW",
64  "The modem Sleep power in Watts",
65  DoubleValue(0.0058),
68  MakeDoubleChecker<double>())
69  .AddTraceSource(
70  "TotalEnergyConsumption",
71  "Total energy consumption of the modem device.",
73  "ns3::TracedValueCallback::Double");
74  return tid;
75 }
76 
78 {
79  NS_LOG_FUNCTION(this);
80  m_currentState = UanPhy::IDLE; // initially IDLE
83  m_node = nullptr;
84  m_source = nullptr;
85 }
86 
88 {
89 }
90 
91 void
93 {
94  NS_LOG_FUNCTION(this << node);
95  NS_ASSERT(node);
96  m_node = node;
97 }
98 
101 {
102  return m_node;
103 }
104 
105 void
107 {
108  NS_LOG_FUNCTION(this << source);
109  NS_ASSERT(source);
110  m_source = source;
111 }
112 
113 double
115 {
116  NS_LOG_FUNCTION(this);
118 }
119 
120 double
122 {
123  NS_LOG_FUNCTION(this);
124  return m_txPowerW;
125 }
126 
127 void
129 {
130  NS_LOG_FUNCTION(this << txPowerW);
131  m_txPowerW = txPowerW;
132 }
133 
134 double
136 {
137  NS_LOG_FUNCTION(this);
138  return m_rxPowerW;
139 }
140 
141 void
143 {
144  NS_LOG_FUNCTION(this << rxPowerW);
145  m_rxPowerW = rxPowerW;
146 }
147 
148 double
150 {
151  NS_LOG_FUNCTION(this);
152  return m_idlePowerW;
153 }
154 
155 void
157 {
158  NS_LOG_FUNCTION(this << idlePowerW);
159  m_idlePowerW = idlePowerW;
160 }
161 
162 double
164 {
165  NS_LOG_FUNCTION(this);
166  return m_sleepPowerW;
167 }
168 
169 void
171 {
172  NS_LOG_FUNCTION(this << sleepPowerW);
173  m_sleepPowerW = sleepPowerW;
174 }
175 
176 int
178 {
179  NS_LOG_FUNCTION(this);
180  return m_currentState;
181 }
182 
183 void
185 {
186  NS_LOG_FUNCTION(this);
187  if (callback.IsNull())
188  {
189  NS_LOG_DEBUG("AcousticModemEnergyModel:Setting NULL energy depletion callback!");
190  }
191  m_energyDepletionCallback = callback;
192 }
193 
194 void
196 {
197  NS_LOG_FUNCTION(this);
198  if (callback.IsNull())
199  {
200  NS_LOG_DEBUG("AcousticModemEnergyModel:Setting NULL energy recharge callback!");
201  }
202  m_energyRechargeCallback = callback;
203 }
204 
205 void
207 {
208  NS_LOG_FUNCTION(this << newState);
209  // NS_ASSERT (IsStateTransitionValid ((MicroModemState) newState));
210 
211  Time duration = Simulator::Now() - m_lastUpdateTime;
212  NS_ASSERT(duration.GetNanoSeconds() >= 0); // check if duration is valid
213 
214  // energy to decrease = current * voltage * time
215  double energyToDecrease = 0.0;
216 
217  switch (m_currentState)
218  {
219  case UanPhy::TX:
220  energyToDecrease = duration.GetSeconds() * m_txPowerW;
221  break;
222  case UanPhy::RX:
223  energyToDecrease = duration.GetSeconds() * m_rxPowerW;
224  break;
225  case UanPhy::IDLE:
226  energyToDecrease = duration.GetSeconds() * m_idlePowerW;
227  break;
228  case UanPhy::SLEEP:
229  energyToDecrease = duration.GetSeconds() * m_sleepPowerW;
230  break;
231  case UanPhy::DISABLED:
232  energyToDecrease = 0;
233  break;
234  default:
235  NS_FATAL_ERROR("AcousticModemEnergyModel:Undefined radio state!");
236  }
237 
238  // update total energy consumption
239  m_totalEnergyConsumption += energyToDecrease;
240 
241  // update last update time stamp
243 
244  // notify energy source
245  m_source->UpdateEnergySource();
246 
248  {
249  // update current state & last update time stamp
250  SetMicroModemState(newState);
251  }
252 
253  // some debug message
254  NS_LOG_DEBUG("AcousticModemEnergyModel:Total energy consumption at node #"
255  << m_node->GetId() << " is " << m_totalEnergyConsumption << "J");
256 }
257 
258 void
260 {
261  NS_LOG_FUNCTION(this);
262  NS_LOG_DEBUG("AcousticModemEnergyModel:Energy is depleted at node #" << m_node->GetId());
263  // invoke energy depletion callback, if set.
265  {
267  }
268  // invoke the phy energy depletion handler
269  Ptr<UanNetDevice> dev = m_node->GetDevice(0)->GetObject<UanNetDevice>();
270  dev->GetPhy()->EnergyDepletionHandler();
272 }
273 
274 void
276 {
277  NS_LOG_FUNCTION(this);
278  NS_LOG_DEBUG("AcousticModemEnergyModel:Energy is recharged at node #" << m_node->GetId());
279  // invoke energy recharge callback, if set.
281  {
283  }
284  // invoke the phy energy recharge handler
285  Ptr<UanNetDevice> dev = m_node->GetDevice(0)->GetObject<UanNetDevice>();
286  dev->GetPhy()->EnergyRechargeHandler();
288 }
289 
290 void
292 {
293  NS_LOG_FUNCTION(this);
294  // Not implemented
295 }
296 
297 /*
298  * Private functions start here.
299  */
300 
301 void
303 {
304  NS_LOG_FUNCTION(this);
305  m_node = nullptr;
306  m_source = nullptr;
308 }
309 
310 double
312 {
313  NS_LOG_FUNCTION(this);
314 
315  double supplyVoltage = m_source->GetSupplyVoltage();
316  NS_ASSERT(supplyVoltage != 0.0);
317  double stateCurrent = 0.0;
318  switch (m_currentState)
319  {
320  case UanPhy::TX:
321  stateCurrent = m_txPowerW / supplyVoltage;
322  break;
323  case UanPhy::RX:
324  stateCurrent = m_rxPowerW / supplyVoltage;
325  break;
326  case UanPhy::IDLE:
327  stateCurrent = m_idlePowerW / supplyVoltage;
328  break;
329  case UanPhy::SLEEP:
330  stateCurrent = m_sleepPowerW / supplyVoltage;
331  break;
332  case UanPhy::DISABLED:
333  stateCurrent = 0.0;
334  break;
335  default:
336  NS_FATAL_ERROR("AcousticModemEnergyModel:Undefined radio state!");
337  }
338 
339  return stateCurrent;
340 }
341 
342 bool
344 {
345  NS_LOG_FUNCTION(this << destState);
346  return true;
347 }
348 
349 void
351 {
352  NS_LOG_FUNCTION(this);
353  if (IsStateTransitionValid(state))
354  {
355  m_currentState = state;
356  std::string stateName;
357  switch (state)
358  {
359  case UanPhy::TX:
360  stateName = "TX";
361  break;
362  case UanPhy::RX:
363  stateName = "RX";
364  break;
365  case UanPhy::IDLE:
366  stateName = "IDLE";
367  break;
368  case UanPhy::SLEEP:
369  stateName = "SLEEP";
370  break;
371  case UanPhy::DISABLED:
372  stateName = "DISABLED";
373  break;
374  }
375  NS_LOG_DEBUG("AcousticModemEnergyModel:Switching to state: " << stateName << " at time = "
376  << Simulator::Now());
377  }
378  else
379  {
380  NS_FATAL_ERROR("AcousticModemEnergyModel:Invalid state transition!");
381  }
382 }
383 
384 } // namespace ns3
double GetTotalEnergyConsumption() const override
Ptr< EnergySource > m_source
The energy source.
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.
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.
void SetEnergySource(Ptr< EnergySource > source) override
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:575
bool IsNull() const
Check for null implementation.
Definition: callback.h:569
Base class for device energy models.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
uint32_t GetId() const
Definition: node.cc:117
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:152
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:418
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
Net device for UAN models.
@ RX
Receiving.
Definition: uan-phy.h:185
@ SLEEP
Sleeping.
Definition: uan-phy.h:187
@ IDLE
Idle state.
Definition: uan-phy.h:183
@ DISABLED
Disabled.
Definition: uan-phy.h:188
@ TX
Transmitting.
Definition: uan-phy.h:186
#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_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#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
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
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 AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:43