A Discrete-Event Network Simulator
API
uan-energy-model-test.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/test.h"
23 #include "ns3/simple-device-energy-model.h"
24 #include "ns3/uan-net-device.h"
25 #include "ns3/simulator.h"
26 #include "ns3/packet.h"
27 #include "ns3/node.h"
28 #include "ns3/uan-helper.h"
29 #include "ns3/basic-energy-source-helper.h"
30 #include "ns3/acoustic-modem-energy-model-helper.h"
31 #include "ns3/acoustic-modem-energy-model.h"
32 #include "ns3/constant-position-mobility-model.h"
33 #include "ns3/uan-channel.h"
34 #include "ns3/uan-noise-model-default.h"
35 #include "ns3/uan-prop-model-ideal.h"
36 #include "ns3/uan-header-common.h"
37 #include "ns3/uan-phy.h"
38 
39 using namespace ns3;
40 
41 NS_LOG_COMPONENT_DEFINE ("UanEnergyModelTestSuite");
42 
56 {
57 public:
60 
69  bool RxPacket (Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address &sender);
74  void SendOnePacket (Ptr<Node> node);
75 
76  void DoRun (void);
77 
78  double m_simTime;
79  uint32_t m_bytesRx;
80  uint32_t m_sentPackets;
81  uint32_t m_packetSize;
84 };
85 
87  : TestCase ("Acoustic Modem energy model test case"),
88  m_simTime (25),
89  m_bytesRx (0),
90  m_sentPackets (0),
91  m_packetSize (17)
92 {
93 }
94 
96 {
97  m_node = 0;
98  m_gateway = 0;
99 }
100 
101 void
103 {
104  // create an empty 17 bytes packet
105  Ptr<Packet> pkt = Create<Packet> (m_packetSize);
106  // send the packet in broadcast
107  Ptr<UanNetDevice> dev = node->GetDevice (0)->GetObject<UanNetDevice> ();
108  dev->Send (pkt, dev->GetBroadcast (), 0);
109  // increase the sent packets number
110  ++m_sentPackets;
111 
112  Simulator::Schedule (Seconds (10),
114  this,
115  node);
116 }
117 
118 bool
120 {
121  // increase the total bytes received
122  m_bytesRx += pkt->GetSize ();
123 
124  return true;
125 }
126 
127 void
129 {
130  // create a generic node
131  m_node = CreateObject<Node> ();
132 
133  // create a default underwater channel
134  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
135  Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault> ();
136  channel->SetPropagationModel (CreateObject<UanPropModelIdeal> ());
137  channel->SetNoiseModel (noise);
138 
139  // install the underwater communication stack
140  UanHelper uan;
141  Ptr<UanNetDevice> devNode = uan.Install (m_node, channel);
142 
143  // compute a packet (header + payload) duration
144  uint32_t datarate = devNode->GetPhy ()->GetMode (0).GetDataRateBps ();
145  UanHeaderCommon hd;
146  double packetDuration = (m_packetSize + hd.GetSerializedSize ()) * 8.0 / (double) datarate;
147 
148  // energy source
150  eh.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (10000000.0));
151  eh.Install (m_node);
152 
153  // mobility model
154  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
155  mobility->SetPosition (Vector (0,0,-500));
156  m_node->AggregateObject (mobility);
157 
158  // micro modem energy model
159  AcousticModemEnergyModelHelper modemHelper;
161  DeviceEnergyModelContainer cont = modemHelper.Install (devNode,source);
162 
163  // Schedule a packet every 10 seconds
164  Simulator::ScheduleNow (&AcousticModemEnergyTestCase::SendOnePacket,
165  this,
166  m_node);
167 
168  // create a gateway node
169  m_gateway = CreateObject<Node> ();
170 
171  // install the underwater communication stack
172  Ptr<UanNetDevice> devGateway = uan.Install (m_gateway, channel);
173 
174  // energy source
175  eh.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (10000000.0));
176  eh.Install (m_gateway);
177 
178  // mobility model
179  Ptr<ConstantPositionMobilityModel> mobility2 = CreateObject<ConstantPositionMobilityModel> ();
180  mobility2->SetPosition (Vector (0,0,0));
181  m_gateway->AggregateObject (mobility2);
182 
183  // micro modem energy model
185  DeviceEnergyModelContainer cont2 = modemHelper.Install (devGateway, source2);
186 
187  // set the receive callback
190  this));
191 
192  // run the simulation
193  Simulator::Stop (Seconds (m_simTime));
194  Simulator::Run ();
195 
196  uint32_t receivedPackets = m_bytesRx / m_packetSize;
198  double consumed1 = src1->GetInitialEnergy () - src1->GetRemainingEnergy ();
199  double computed1 = cont2.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetRxPowerW () * packetDuration * receivedPackets +
200  cont2.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetIdlePowerW () * (m_simTime - (double) 2.0 / 3.0 - packetDuration * receivedPackets);
201 
202  NS_TEST_ASSERT_MSG_EQ_TOL (consumed1, computed1, 1.0e-5,
203  "Incorrect gateway consumed energy!");
204 
206  double consumed2 = src2->GetInitialEnergy () - src2->GetRemainingEnergy ();
207  double computed2 = cont.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetTxPowerW () * packetDuration * m_sentPackets +
208  cont.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetIdlePowerW () * (m_simTime - 1 - packetDuration * m_sentPackets);
209 
210  NS_TEST_ASSERT_MSG_EQ_TOL (consumed2, computed2, 1.0e-5,
211  "Incorrect node consumed energy!");
212 
213  Simulator::Destroy ();
214 }
215 
223 {
224 public:
227 
229  void DepletionHandler (void);
234  void SendOnePacket (Ptr<Node> node);
235 
236  void DoRun (void);
237 
238  double m_simTime;
239  uint32_t m_callbackCount;
240  uint32_t m_packetSize;
242 };
243 
245  : TestCase ("Acoustic Modem energy depletion test case"),
246  m_simTime (25),
247  m_callbackCount (0),
248  m_packetSize (17)
249 {
250 }
251 
253 {
254  m_node = 0;
255 }
256 
257 void
259 {
260  // increase callback count
261  m_callbackCount++;
262 }
263 
264 void
266 {
267  // create an empty packet
268  Ptr<Packet> pkt = Create<Packet> (m_packetSize);
269  // send the packet in broadcast
270  Ptr<UanNetDevice> dev = node->GetDevice (0)->GetObject<UanNetDevice> ();
271  dev->Send (pkt, dev->GetBroadcast (), 0);
272 
273  Simulator::Schedule (Seconds (10),
275  this,
276  node);
277 }
278 
279 void
281 {
282  // create a generic node
283  m_node = CreateObject<Node> ();
284 
285  // create a default underwater channel
286  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
287  Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault> ();
288  channel->SetPropagationModel (CreateObject<UanPropModelIdeal> ());
289  channel->SetNoiseModel (noise);
290 
291  // install the underwater communication stack
292  UanHelper uan;
293  Ptr<UanNetDevice> devNode = uan.Install (m_node, channel);
294 
295  // set an empty energy source
297  eh.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (0.0));
298  eh.Install (m_node);
299 
300  // mobility model
301  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
302  mobility->SetPosition (Vector (0,0,0));
303  m_node->AggregateObject (mobility);
304 
305  // micro modem energy model
306  AcousticModemEnergyModelHelper modemHelper;
308  // set the depletion callback
311  modemHelper.SetDepletionCallback (callback);
312  DeviceEnergyModelContainer cont = modemHelper.Install (devNode,source);
313 
314  // try to send a packet
316  this,
317  m_node);
318 
319  Simulator::Stop (Seconds (m_simTime));
320  Simulator::Run ();
321  Simulator::Destroy ();
322 
323  NS_TEST_ASSERT_MSG_EQ (m_callbackCount, 1, "Callback not invoked");
324 }
325 
326 // -------------------------------------------------------------------------- //
327 
336 {
337 public:
339 };
340 
342  : TestSuite ("uan-energy-model", UNIT)
343 {
344  AddTestCase (new AcousticModemEnergyTestCase, TestCase::QUICK);
345  AddTestCase (new AcousticModemEnergyDepletionTestCase, TestCase::QUICK);
346 }
347 
348 // create an instance of the test suite
UAN configuration helper.
Definition: uan-helper.h:40
tuple channel
Definition: third.py:85
DeviceEnergyModelContainer Install(Ptr< NetDevice > device, Ptr< EnergySource > source) const
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
Holds a vector of ns3::EnergySource pointers.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Acoustic Modem Energy Depletion Test Case.
A suite of tests to run.
Definition: test.h:1342
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
void DoRun(void)
Implementation to actually run this TestCase.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:796
Assign AcousticModemEnergyModel to uan devices.
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
encapsulates test code
Definition: test.h:1155
Holds a vector of ns3::DeviceEnergyModel pointers.
double m_simTime
simulation time
a polymophic address class
Definition: address.h:90
void SendOnePacket(Ptr< Node > node)
Send one packet function.
virtual double GetInitialEnergy(void) const =0
virtual void SetReceiveCallback(ReceiveCallback cb)=0
WHOI micro-modem energy model.
tuple mobility
Definition: third.py:101
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
virtual uint32_t GetSerializedSize(void) const
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:168
Creates a BasicEnergySource object.
bool RxPacket(Ptr< NetDevice > dev, Ptr< const Packet > pkt, uint16_t mode, const Address &sender)
Receive packet function.
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:142
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
Unit test suite for underwater energy model.
Acoustic Modem Energy Test Case.
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
Definition: test.h:380
uint32_t m_sentPackets
number of sent packets
Ptr< DeviceEnergyModel > Get(uint32_t i) const
Get the i-th Ptr stored in this container.
void DepletionHandler(void)
Depletion handler function.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Common packet header fields.
EnergySourceContainer Install(Ptr< Node > node) const
void SendOnePacket(Ptr< Node > node)
Send one packet function.
void SendOnePacket(Ptr< LrWpanPhy > sender, Ptr< LrWpanPhy > receiver)
void SetPosition(const Vector &position)
Net device for UAN models.
Ptr< UanPhy > GetPhy(void) const
Get the Phy used by this device.
virtual double GetRemainingEnergy(void)=0
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
void Set(std::string name, const AttributeValue &v)
void DoRun(void)
Implementation to actually run this TestCase.
void SetDepletionCallback(AcousticModemEnergyModel::AcousticModemEnergyDepletionCallback callback)
Sets the callback to be invoked when energy is depleted.
uint32_t m_bytesRx
bytes received
NetDeviceContainer Install(NodeContainer c) const
This method creates a simple ns3::UanChannel (with a default ns3::UanNoiseModelDefault and ns3::UanPr...
Definition: uan-helper.cc:210
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
static UanEnergyModelTestSuite g_uanEnergyModelTestSuite
Ptr< Node > m_gateway
the gateway