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 
37 TypeId
39 {
40  static TypeId tid = TypeId ("ns3::AcousticModemEnergyModel")
42  .AddConstructor<AcousticModemEnergyModel> ()
43  .AddAttribute ("TxPowerW",
44  "The modem Tx power in Watts",
45  DoubleValue (50),
46  MakeDoubleAccessor (&AcousticModemEnergyModel::SetTxPowerW,
48  MakeDoubleChecker<double> ())
49  .AddAttribute ("RxPowerW",
50  "The modem Rx power in Watts",
51  DoubleValue (0.158),
52  MakeDoubleAccessor (&AcousticModemEnergyModel::SetRxPowerW,
54  MakeDoubleChecker<double> ())
55  .AddAttribute ("IdlePowerW",
56  "The modem Idle power in Watts",
57  DoubleValue (0.158),
58  MakeDoubleAccessor (&AcousticModemEnergyModel::SetIdlePowerW,
60  MakeDoubleChecker<double> ())
61  .AddAttribute ("SleepPowerW",
62  "The modem Sleep power in Watts",
63  DoubleValue (0.0058),
64  MakeDoubleAccessor (&AcousticModemEnergyModel::SetSleepPowerW,
66  MakeDoubleChecker<double> ())
67  .AddTraceSource ("TotalEnergyConsumption",
68  "Total energy consumption of the modem device.",
70  ;
71  return tid;
72 }
73 
75 {
76  NS_LOG_FUNCTION (this);
77  m_currentState = UanPhy::IDLE; // initially IDLE
78  m_lastUpdateTime = Seconds (0.0);
80  m_node = 0;
81  m_source = 0;
82 }
83 
85 {
86 }
87 
88 void
90 {
91  NS_LOG_FUNCTION (this << node);
92  NS_ASSERT (node != 0);
93  m_node = node;
94 }
95 
98 {
99  return m_node;
100 }
101 
102 void
104 {
105  NS_LOG_FUNCTION (this << source);
106  NS_ASSERT (source != 0);
107  m_source = source;
108 }
109 
110 double
112 {
113  NS_LOG_FUNCTION (this);
115 }
116 
117 double
119 {
120  NS_LOG_FUNCTION (this);
121  return m_txPowerW;
122 }
123 
124 void
126 {
127  NS_LOG_FUNCTION (this << txPowerW);
128  m_txPowerW = txPowerW;
129 }
130 
131 double
133 {
134  NS_LOG_FUNCTION (this);
135  return m_rxPowerW;
136 }
137 
138 void
140 {
141  NS_LOG_FUNCTION (this << rxPowerW);
142  m_rxPowerW = rxPowerW;
143 }
144 
145 double
147 {
148  NS_LOG_FUNCTION (this);
149  return m_idlePowerW;
150 }
151 
152 void
154 {
155  NS_LOG_FUNCTION (this << idlePowerW);
156  m_idlePowerW = idlePowerW;
157 }
158 
159 double
161 {
162  NS_LOG_FUNCTION (this);
163  return m_sleepPowerW;
164 }
165 
166 void
168 {
169  NS_LOG_FUNCTION (this << sleepPowerW);
170  m_sleepPowerW = sleepPowerW;
171 }
172 
173 int
175 {
176  NS_LOG_FUNCTION (this);
177  return m_currentState;
178 }
179 
180 void
183 {
184  NS_LOG_FUNCTION (this);
185  if (callback.IsNull ())
186  {
187  NS_LOG_DEBUG ("AcousticModemEnergyModel:Setting NULL energy depletion callback!");
188  }
189  m_energyDepletionCallback = callback;
190 }
191 
192 void
194 {
195  NS_LOG_FUNCTION (this << newState);
196  // NS_ASSERT (IsStateTransitionValid ((MicroModemState) newState));
197 
198  Time duration = Simulator::Now () - m_lastUpdateTime;
199  NS_ASSERT (duration.GetNanoSeconds () >= 0); // check if duration is valid
200 
201  // energy to decrease = current * voltage * time
202  double energyToDecrease = 0.0;
203  double supplyVoltage = m_source->GetSupplyVoltage ();
204  switch (m_currentState)
205  {
206  case UanPhy::TX:
207  energyToDecrease = duration.GetSeconds () * m_txPowerW * supplyVoltage;
208  break;
209  case UanPhy::RX:
210  energyToDecrease = duration.GetSeconds () * m_rxPowerW * supplyVoltage;
211  break;
212  case UanPhy::IDLE:
213  energyToDecrease = duration.GetSeconds () * m_idlePowerW * supplyVoltage;
214  break;
215  case UanPhy::SLEEP:
216  energyToDecrease = duration.GetSeconds () * m_sleepPowerW * supplyVoltage;
217  break;
218  default:
219  NS_FATAL_ERROR ("AcousticModemEnergyModel:Undefined radio state!");
220  }
221 
222  // update total energy consumption
223  m_totalEnergyConsumption += energyToDecrease;
224 
225  // update last update time stamp
227 
228  // notify energy source
229  m_source->UpdateEnergySource ();
230 
231  // update current state & last update time stamp
232  SetMicroModemState (newState);
233 
234  // some debug message
235  NS_LOG_DEBUG ("AcousticModemEnergyModel:Total energy consumption at node #" <<
236  m_node->GetId () << " is " << m_totalEnergyConsumption << "J");
237 }
238 
239 void
241 {
242  NS_LOG_FUNCTION (this);
243  NS_LOG_DEBUG ("AcousticModemEnergyModel:Energy is depleted at node #" <<
244  m_node->GetId ());
245  // invoke energy depletion callback, if set.
247  {
249  }
250  // invoke the phy energy depletion handler
251  Ptr<UanNetDevice> dev = m_node->GetDevice (0)->GetObject<UanNetDevice> ();
252  dev->GetPhy ()->EnergyDepletionHandler ();
253 }
254 
255 /*
256  * Private functions start here.
257  */
258 
259 void
261 {
262  NS_LOG_FUNCTION (this);
263  m_node = 0;
264  m_source = 0;
266 }
267 
268 double
270 {
271  NS_LOG_FUNCTION (this);
272 
273  double supplyVoltage = m_source->GetSupplyVoltage ();
274  NS_ASSERT (supplyVoltage != 0.0);
275  double stateCurrent = 0.0;
276  switch (m_currentState)
277  {
278  case UanPhy::TX:
279  stateCurrent = m_txPowerW / supplyVoltage;
280  break;
281  case UanPhy::RX:
282  stateCurrent = m_rxPowerW / supplyVoltage;
283  break;
284  case UanPhy::IDLE:
285  stateCurrent = m_idlePowerW / supplyVoltage;
286  break;
287  case UanPhy::SLEEP:
288  stateCurrent = m_sleepPowerW / supplyVoltage;
289  break;
290  default:
291  NS_FATAL_ERROR ("AcousticModemEnergyModel:Undefined radio state!");
292  }
293 
294  return stateCurrent;
295 }
296 
297 bool
299 {
300  NS_LOG_FUNCTION (this << destState);
301  return true;
302 }
303 
304 void
306 {
307  NS_LOG_FUNCTION (this);
308  if (IsStateTransitionValid (state))
309  {
310  m_currentState = state;
311  std::string stateName;
312  switch (state)
313  {
314  case UanPhy::TX:
315  stateName = "TX";
316  break;
317  case UanPhy::RX:
318  stateName = "RX";
319  break;
320  case UanPhy::IDLE:
321  stateName = "IDLE";
322  break;
323  case UanPhy::SLEEP:
324  stateName = "SLEEP";
325  break;
326  }
327  NS_LOG_DEBUG ("AcousticModemEnergyModel:Switching to state: " << stateName <<
328  " at time = " << Simulator::Now ());
329  }
330  else
331  {
332  NS_FATAL_ERROR ("AcousticModemEnergyModel:Invalid state transition!");
333  }
334 }
335 
336 } // namespace ns3
double GetRxPowerW(void) const
Get the recieving power.
virtual void ChangeState(int newState)
Changes state of the AcousticModemEnergyModel.
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
virtual ~AcousticModemEnergyModel()
Dummy destructor, see DoDispose.
virtual void HandleEnergyDepletion(void)
Handles energy depletion.
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
virtual double DoGetCurrentA(void) const
double m_idlePowerW
The idle power, in watts.
Transmitting.
Definition: uan-phy.h:184
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:1014
#define NS_ASSERT(condition)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
AcousticModemEnergyDepletionCallback m_energyDepletionCallback
Energy depletion callback.
void SetIdlePowerW(double idlePowerW)
Set the idle state power of the modem.
NS_LOG_COMPONENT_DEFINE("AcousticModemEnergyModel")
bool IsStateTransitionValid(const int destState)
Ptr< Node > m_node
The node hosting this transducer.
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
double GetSeconds(void) const
Definition: nstime.h:274
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:132
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:299
uint32_t GetId(void) const
Definition: node.cc:104
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)
Definition: log.h:289
virtual void SetNode(Ptr< Node > node)
Sets pointer to node.
void Nullify(void)
Discard the implementation, set it to null.
Definition: callback.h:1018
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:611