A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
acoustic-modem-energy-model.cc
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"
28 #include "acoustic-modem-energy-model.h"
29 
30 NS_LOG_COMPONENT_DEFINE ("AcousticModemEnergyModel");
31 
32 namespace ns3 {
33 
34 NS_OBJECT_ENSURE_REGISTERED (AcousticModemEnergyModel);
35 
36 TypeId
37 AcousticModemEnergyModel::GetTypeId (void)
38 {
39  static TypeId tid = TypeId ("ns3::AcousticModemEnergyModel")
40  .SetParent<DeviceEnergyModel> ()
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.",
68  MakeTraceSourceAccessor (&AcousticModemEnergyModel::m_totalEnergyConsumption))
69  ;
70  return tid;
71 }
72 
73 AcousticModemEnergyModel::AcousticModemEnergyModel ()
74 {
75  NS_LOG_FUNCTION (this);
76  m_currentState = UanPhy::IDLE; // initially IDLE
77  m_lastUpdateTime = Seconds (0.0);
78  m_energyDepletionCallback.Nullify ();
79  m_node = 0;
80  m_source = 0;
81 }
82 
83 AcousticModemEnergyModel::~AcousticModemEnergyModel ()
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);
113  return m_totalEnergyConsumption;
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
225  m_lastUpdateTime = Simulator::Now ();
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.
245  if (!m_energyDepletionCallback.IsNull ())
246  {
247  m_energyDepletionCallback ();
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;
264  m_energyDepletionCallback.Nullify ();
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
virtual void ChangeState(int newState)
Changes state of the AcousticModemEnergyModel.
keep track of time unit.
Definition: nstime.h:149
virtual void HandleEnergyDepletion(void)
Handles energy depletion.
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
virtual double DoGetCurrentA(void) const
virtual double GetTotalEnergyConsumption(void) const
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
bool IsStateTransitionValid(const int destState)
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
double GetSeconds(void) const
Definition: nstime.h:262
void SetEnergyDepletionCallback(AcousticModemEnergyDepletionCallback callback)
Ptr< NetDevice > GetDevice(uint32_t index) const
Definition: node.cc:133
virtual void SetEnergySource(Ptr< EnergySource > source)
Sets pointer to EnergySouce installed on node.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
static Time Now(void)
Definition: simulator.cc:179
Net device for UAN models.
int64_t GetNanoSeconds(void) const
Definition: nstime.h:287
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
uint32_t GetId(void) const
Definition: node.cc:105
virtual Ptr< Node > GetNode(void) const
Gets pointer to node.
#define NS_LOG_DEBUG(msg)
Definition: log.h:255
virtual void SetNode(Ptr< Node > node)
Sets pointer to node.