A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
acoustic-modem-energy-model.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Andrea Sacco
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Andrea Sacco <andrea.sacco85@gmail.com>
19  */
20 
21 #include "ns3/log.h"
22 #include "ns3/double.h"
23 #include "ns3/simulator.h"
24 #include "ns3/trace-source-accessor.h"
25 #include "ns3/energy-source.h"
26 #include "ns3/uan-phy.h"
27 #include "ns3/uan-net-device.h"
29 
30 NS_LOG_COMPONENT_DEFINE ("AcousticModemEnergyModel");
31 
32 namespace ns3 {
33 
34 NS_OBJECT_ENSURE_REGISTERED (AcousticModemEnergyModel);
35 
36 TypeId
38 {
39  static TypeId tid = TypeId ("ns3::AcousticModemEnergyModel")
41  .AddConstructor<AcousticModemEnergyModel> ()
42  .AddAttribute ("TxPowerW",
43  "The modem Tx power in Watts",
44  DoubleValue (50),
45  MakeDoubleAccessor (&AcousticModemEnergyModel::SetTxPowerW,
47  MakeDoubleChecker<double> ())
48  .AddAttribute ("RxPowerW",
49  "The modem Rx power in Watts",
50  DoubleValue (0.158),
51  MakeDoubleAccessor (&AcousticModemEnergyModel::SetRxPowerW,
53  MakeDoubleChecker<double> ())
54  .AddAttribute ("IdlePowerW",
55  "The modem Idle power in Watts",
56  DoubleValue (0.158),
57  MakeDoubleAccessor (&AcousticModemEnergyModel::SetIdlePowerW,
59  MakeDoubleChecker<double> ())
60  .AddAttribute ("SleepPowerW",
61  "The modem Sleep power in Watts",
62  DoubleValue (0.0058),
63  MakeDoubleAccessor (&AcousticModemEnergyModel::SetSleepPowerW,
65  MakeDoubleChecker<double> ())
66  .AddTraceSource ("TotalEnergyConsumption",
67  "Total energy consumption of the modem device.",
69  ;
70  return tid;
71 }
72 
74 {
75  NS_LOG_FUNCTION (this);
76  m_currentState = UanPhy::IDLE; // initially IDLE
77  m_lastUpdateTime = Seconds (0.0);
79  m_node = 0;
80  m_source = 0;
81 }
82 
84 {
85 }
86 
87 void
89 {
90  NS_LOG_FUNCTION (this << node);
91  NS_ASSERT (node != 0);
92  m_node = node;
93 }
94 
97 {
98  return m_node;
99 }
100 
101 void
103 {
104  NS_LOG_FUNCTION (this << source);
105  NS_ASSERT (source != 0);
106  m_source = source;
107 }
108 
109 double
111 {
112  NS_LOG_FUNCTION (this);
114 }
115 
116 double
118 {
119  NS_LOG_FUNCTION (this);
120  return m_txPowerW;
121 }
122 
123 void
125 {
126  NS_LOG_FUNCTION (this << txPowerW);
127  m_txPowerW = txPowerW;
128 }
129 
130 double
132 {
133  NS_LOG_FUNCTION (this);
134  return m_rxPowerW;
135 }
136 
137 void
139 {
140  NS_LOG_FUNCTION (this << rxPowerW);
141  m_rxPowerW = rxPowerW;
142 }
143 
144 double
146 {
147  NS_LOG_FUNCTION (this);
148  return m_idlePowerW;
149 }
150 
151 void
153 {
154  NS_LOG_FUNCTION (this << idlePowerW);
155  m_idlePowerW = idlePowerW;
156 }
157 
158 double
160 {
161  NS_LOG_FUNCTION (this);
162  return m_sleepPowerW;
163 }
164 
165 void
167 {
168  NS_LOG_FUNCTION (this << sleepPowerW);
169  m_sleepPowerW = sleepPowerW;
170 }
171 
172 int
174 {
175  NS_LOG_FUNCTION (this);
176  return m_currentState;
177 }
178 
179 void
182 {
183  NS_LOG_FUNCTION (this);
184  if (callback.IsNull ())
185  {
186  NS_LOG_DEBUG ("AcousticModemEnergyModel:Setting NULL energy depletion callback!");
187  }
188  m_energyDepletionCallback = callback;
189 }
190 
191 void
193 {
194  NS_LOG_FUNCTION (this << newState);
195  // NS_ASSERT (IsStateTransitionValid ((MicroModemState) newState));
196 
197  Time duration = Simulator::Now () - m_lastUpdateTime;
198  NS_ASSERT (duration.GetNanoSeconds () >= 0); // check if duration is valid
199 
200  // energy to decrease = current * voltage * time
201  double energyToDecrease = 0.0;
202  double supplyVoltage = m_source->GetSupplyVoltage ();
203  switch (m_currentState)
204  {
205  case UanPhy::TX:
206  energyToDecrease = duration.GetSeconds () * m_txPowerW * supplyVoltage;
207  break;
208  case UanPhy::RX:
209  energyToDecrease = duration.GetSeconds () * m_rxPowerW * supplyVoltage;
210  break;
211  case UanPhy::IDLE:
212  energyToDecrease = duration.GetSeconds () * m_idlePowerW * supplyVoltage;
213  break;
214  case UanPhy::SLEEP:
215  energyToDecrease = duration.GetSeconds () * m_sleepPowerW * supplyVoltage;
216  break;
217  default:
218  NS_FATAL_ERROR ("AcousticModemEnergyModel:Undefined radio state!");
219  }
220 
221  // update total energy consumption
222  m_totalEnergyConsumption += energyToDecrease;
223 
224  // update last update time stamp
226 
227  // notify energy source
228  m_source->UpdateEnergySource ();
229 
230  // update current state & last update time stamp
231  SetMicroModemState (newState);
232 
233  // some debug message
234  NS_LOG_DEBUG ("AcousticModemEnergyModel:Total energy consumption at node #" <<
235  m_node->GetId () << " is " << m_totalEnergyConsumption << "J");
236 }
237 
238 void
240 {
241  NS_LOG_FUNCTION (this);
242  NS_LOG_DEBUG ("AcousticModemEnergyModel:Energy is depleted at node #" <<
243  m_node->GetId ());
244  // invoke energy depletion callback, if set.
246  {
248  }
249  // invoke the phy energy depletion handler
250  Ptr<UanNetDevice> dev = m_node->GetDevice (0)->GetObject<UanNetDevice> ();
251  dev->GetPhy ()->EnergyDepletionHandler ();
252 }
253 
254 /*
255  * Private functions start here.
256  */
257 
258 void
260 {
261  NS_LOG_FUNCTION (this);
262  m_node = 0;
263  m_source = 0;
265 }
266 
267 double
269 {
270  NS_LOG_FUNCTION (this);
271 
272  double supplyVoltage = m_source->GetSupplyVoltage ();
273  NS_ASSERT (supplyVoltage != 0.0);
274  double stateCurrent = 0.0;
275  switch (m_currentState)
276  {
277  case UanPhy::TX:
278  stateCurrent = m_txPowerW / supplyVoltage;
279  break;
280  case UanPhy::RX:
281  stateCurrent = m_rxPowerW / supplyVoltage;
282  break;
283  case UanPhy::IDLE:
284  stateCurrent = m_idlePowerW / supplyVoltage;
285  break;
286  case UanPhy::SLEEP:
287  stateCurrent = m_sleepPowerW / supplyVoltage;
288  break;
289  default:
290  NS_FATAL_ERROR ("AcousticModemEnergyModel:Undefined radio state!");
291  }
292 
293  return stateCurrent;
294 }
295 
296 bool
298 {
299  NS_LOG_FUNCTION (this << destState);
300  return true;
301 }
302 
303 void
305 {
306  NS_LOG_FUNCTION (this);
307  if (IsStateTransitionValid (state))
308  {
309  m_currentState = state;
310  std::string stateName;
311  switch (state)
312  {
313  case UanPhy::TX:
314  stateName = "TX";
315  break;
316  case UanPhy::RX:
317  stateName = "RX";
318  break;
319  case UanPhy::IDLE:
320  stateName = "IDLE";
321  break;
322  case UanPhy::SLEEP:
323  stateName = "SLEEP";
324  break;
325  }
326  NS_LOG_DEBUG ("AcousticModemEnergyModel:Switching to state: " << stateName <<
327  " at time = " << Simulator::Now ());
328  }
329  else
330  {
331  NS_FATAL_ERROR ("AcousticModemEnergyModel:Invalid state transition!");
332  }
333 }
334 
335 } // namespace ns3
double GetRxPowerW(void) const
Get the recieving power.
virtual void ChangeState(int newState)
Changes state of the AcousticModemEnergyModel.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
virtual ~AcousticModemEnergyModel()
Dummy destructor, see DoDispose.
virtual void HandleEnergyDepletion(void)
Handles energy depletion.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual double DoGetCurrentA(void) const
double m_idlePowerW
The idle power, in watts.
Transmitting.
Definition: uan-phy.h:184
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
Base class for device energy models.
virtual double GetTotalEnergyConsumption(void) const
double m_rxPowerW
The receiver power, in watts.
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1018
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
AcousticModemEnergyDepletionCallback m_energyDepletionCallback
Energy depletion callback.
void SetIdlePowerW(double idlePowerW)
Set the idle state power of the modem.
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:95
bool IsStateTransitionValid(const int destState)
Ptr< Node > m_node
The node hosting this transducer.
double GetSeconds(void) const
Definition: nstime.h:272
void SetEnergyDepletionCallback(AcousticModemEnergyDepletionCallback callback)
double m_sleepPowerW
The sleep power, in watts.
Receiving.
Definition: uan-phy.h:183
void SetTxPowerW(double txPowerW)
Set the transmission power of the modem.
Ptr< NetDevice > GetDevice(uint32_t index) const
Definition: node.cc:134
void SetRxPowerW(double rxPowerW)
Set the receiving power of the modem.
virtual void SetEnergySource(Ptr< EnergySource > source)
Idle state.
Definition: uan-phy.h:181
Sleeping.
Definition: uan-phy.h:185
static TypeId GetTypeId(void)
Register this type.
Ptr< EnergySource > m_source
The energy source.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
Net device for UAN models.
int64_t GetNanoSeconds(void) const
Definition: nstime.h:297
uint32_t GetId(void) const
Definition: node.cc:106
double GetSleepPowerW(void) const
Get the sleep state power of the modem.
double GetTxPowerW(void) const
Get the transmission power of the modem.
virtual Ptr< Node > GetNode(void) const
Gets pointer to node.
Time m_lastUpdateTime
Time stamp of previous energy update.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:213
virtual void SetNode(Ptr< Node > node)
Sets pointer to node.
void Nullify(void)
Discard the implementation, set it to null.
Definition: callback.h:1022
void SetSleepPowerW(double sleepPowerW)
Set the sleep power of the modem.
int GetCurrentState(void) const
Get the current state of the modem.
Hold a floating point type.
Definition: double.h:41
double m_txPowerW
The transmitter power, in watts.
double GetIdlePowerW(void) const
Get the idle power of the modem.
a unique identifier for an interface.
Definition: type-id.h:49
TracedValue< double > m_totalEnergyConsumption
The total energy consumed by this model.
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610