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  [[maybe_unused]] uint16_t mode,
121  [[maybe_unused]] const Address &sender)
122 {
123  // increase the total bytes received
124  m_bytesRx += pkt->GetSize ();
125 
126  return true;
127 }
128 
129 void
131 {
132  // create a generic node
133  m_node = CreateObject<Node> ();
134 
135  // create a default underwater channel
136  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
137  Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault> ();
138  channel->SetPropagationModel (CreateObject<UanPropModelIdeal> ());
139  channel->SetNoiseModel (noise);
140 
141  // install the underwater communication stack
142  UanHelper uan;
143  Ptr<UanNetDevice> devNode = uan.Install (m_node, channel);
144 
145  // compute a packet (header + payload) duration
146  uint32_t datarate = devNode->GetPhy ()->GetMode (0).GetDataRateBps ();
147  UanHeaderCommon hd;
148  double packetDuration = (m_packetSize + hd.GetSerializedSize ()) * 8.0 / (double) datarate;
149 
150  // energy source
152  eh.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (10000000.0));
153  eh.Install (m_node);
154 
155  // mobility model
156  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
157  mobility->SetPosition (Vector (0,0,-500));
159 
160  // micro modem energy model
161  AcousticModemEnergyModelHelper modemHelper;
163  DeviceEnergyModelContainer cont = modemHelper.Install (devNode,source);
164 
165  // Schedule a packet every 10 seconds
166  Simulator::ScheduleNow (&AcousticModemEnergyTestCase::SendOnePacket,
167  this,
168  m_node);
169 
170  // create a gateway node
171  m_gateway = CreateObject<Node> ();
172 
173  // install the underwater communication stack
174  Ptr<UanNetDevice> devGateway = uan.Install (m_gateway, channel);
175 
176  // energy source
177  eh.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (10000000.0));
178  eh.Install (m_gateway);
179 
180  // mobility model
181  Ptr<ConstantPositionMobilityModel> mobility2 = CreateObject<ConstantPositionMobilityModel> ();
182  mobility2->SetPosition (Vector (0,0,0));
183  m_gateway->AggregateObject (mobility2);
184 
185  // micro modem energy model
187  DeviceEnergyModelContainer cont2 = modemHelper.Install (devGateway, source2);
188 
189  // set the receive callback
192  this));
193 
194  // run the simulation
195  Simulator::Stop (Seconds (m_simTime));
196  Simulator::Run ();
197 
198  uint32_t receivedPackets = m_bytesRx / m_packetSize;
200  double consumed1 = src1->GetInitialEnergy () - src1->GetRemainingEnergy ();
201  double computed1 = cont2.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetRxPowerW () * packetDuration * receivedPackets +
202  cont2.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetIdlePowerW () * (m_simTime - packetDuration * receivedPackets);
203 
204  NS_TEST_ASSERT_MSG_EQ_TOL (consumed1, computed1, 1.0e-5,
205  "Incorrect gateway consumed energy!");
206 
208  double consumed2 = src2->GetInitialEnergy () - src2->GetRemainingEnergy ();
209  double computed2 = cont.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetTxPowerW () * packetDuration * m_sentPackets +
210  cont.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetIdlePowerW () * (m_simTime - packetDuration * m_sentPackets);
211 
212  NS_TEST_ASSERT_MSG_EQ_TOL (consumed2, computed2, 1.0e-5,
213  "Incorrect node consumed energy!");
214 
215  Simulator::Destroy ();
216 }
217 
225 {
226 public:
229 
231  void DepletionHandler (void);
236  void SendOnePacket (Ptr<Node> node);
237 
238  void DoRun (void);
239 
240  double m_simTime;
241  uint32_t m_callbackCount;
242  uint32_t m_packetSize;
244 };
245 
247  : TestCase ("Acoustic Modem energy depletion test case"),
248  m_simTime (25),
249  m_callbackCount (0),
250  m_packetSize (17)
251 {
252 }
253 
255 {
256  m_node = 0;
257 }
258 
259 void
261 {
262  // increase callback count
263  m_callbackCount++;
264 }
265 
266 void
268 {
269  // create an empty packet
270  Ptr<Packet> pkt = Create<Packet> (m_packetSize);
271  // send the packet in broadcast
272  Ptr<UanNetDevice> dev = node->GetDevice (0)->GetObject<UanNetDevice> ();
273  dev->Send (pkt, dev->GetBroadcast (), 0);
274 
275  Simulator::Schedule (Seconds (10),
277  this,
278  node);
279 }
280 
281 void
283 {
284  // create a generic node
285  m_node = CreateObject<Node> ();
286 
287  // create a default underwater channel
288  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
289  Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault> ();
290  channel->SetPropagationModel (CreateObject<UanPropModelIdeal> ());
291  channel->SetNoiseModel (noise);
292 
293  // install the underwater communication stack
294  UanHelper uan;
295  Ptr<UanNetDevice> devNode = uan.Install (m_node, channel);
296 
297  // set an empty energy source
299  eh.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (0.0));
300  eh.Install (m_node);
301 
302  // mobility model
303  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
304  mobility->SetPosition (Vector (0,0,0));
306 
307  // micro modem energy model
308  AcousticModemEnergyModelHelper modemHelper;
310  // set the depletion callback
313  modemHelper.SetDepletionCallback (callback);
314  DeviceEnergyModelContainer cont = modemHelper.Install (devNode,source);
315 
316  // try to send a packet
318  this,
319  m_node);
320 
321  Simulator::Stop (Seconds (m_simTime));
322  Simulator::Run ();
323  Simulator::Destroy ();
324 
325  NS_TEST_ASSERT_MSG_EQ (m_callbackCount, 1, "Callback not invoked");
326 }
327 
328 // -------------------------------------------------------------------------- //
329 
338 {
339 public:
341 };
342 
344  : TestSuite ("uan-energy-model", UNIT)
345 {
346  AddTestCase (new AcousticModemEnergyTestCase, TestCase::QUICK);
347  AddTestCase (new AcousticModemEnergyDepletionTestCase, TestCase::QUICK);
348 }
349 
350 // create an instance of the test suite
Acoustic Modem Energy Depletion Test Case.
void SendOnePacket(Ptr< Node > node)
Send one packet function.
void DoRun(void)
Implementation to actually run this TestCase.
void DepletionHandler(void)
Depletion handler function.
Acoustic Modem Energy Test Case.
Ptr< Node > m_gateway
the gateway
uint32_t m_sentPackets
number of sent packets
void SendOnePacket(Ptr< Node > node)
Send one packet function.
bool RxPacket(Ptr< NetDevice > dev, Ptr< const Packet > pkt, uint16_t mode, const Address &sender)
Receive packet function.
void DoRun(void)
Implementation to actually run this TestCase.
uint32_t m_bytesRx
bytes received
Unit test suite for underwater energy model.
Assign AcousticModemEnergyModel to uan devices.
void SetDepletionCallback(AcousticModemEnergyModel::AcousticModemEnergyDepletionCallback callback)
Sets the callback to be invoked when energy is depleted.
WHOI micro-modem energy model.
a polymophic address class
Definition: address.h:91
Creates a BasicEnergySource object.
void Set(std::string name, const AttributeValue &v)
Holds a vector of ns3::DeviceEnergyModel pointers.
Ptr< DeviceEnergyModel > Get(uint32_t i) const
Get the i-th Ptr<DeviceEnergyModel> stored in this container.
DeviceEnergyModelContainer Install(Ptr< NetDevice > device, Ptr< EnergySource > source) const
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
Holds a vector of ns3::EnergySource pointers.
EnergySourceContainer Install(Ptr< Node > node) const
virtual void SetReceiveCallback(ReceiveCallback cb)=0
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:144
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
Common packet header fields.
virtual uint32_t GetSerializedSize(void) const
UAN configuration helper.
Definition: uan-helper.h:41
NetDeviceContainer Install(NodeContainer c) const
This method creates a simple ns3::UanChannel (with a default ns3::UanNoiseModelDefault and ns3::UanPr...
Definition: uan-helper.cc:212
Net device for UAN models.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:141
#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:323
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
void SendOnePacket(Ptr< LrWpanPhy > sender, Ptr< LrWpanPhy > receiver)
Send one packet.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
channel
Definition: third.py:92
mobility
Definition: third.py:108
static UanEnergyModelTestSuite g_uanEnergyModelTestSuite