A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
44 {
45 public:
48 
49  bool RxPacket (Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address &sender);
50  void SendOnePacket (Ptr<Node> node);
51 
52  void DoRun (void);
53 
54  double m_simTime;
55  uint32_t m_bytesRx;
56  uint32_t m_sentPackets;
57  uint32_t m_packetSize;
60 };
61 
63  : TestCase ("Acoustic Modem energy model test case"),
64  m_simTime (25),
65  m_bytesRx (0),
66  m_sentPackets (0),
67  m_packetSize (17)
68 {
69 }
70 
72 {
73  m_node = 0;
74  m_gateway = 0;
75 }
76 
77 void
79 {
80  // create an empty 17 bytes packet
81  Ptr<Packet> pkt = Create<Packet> (m_packetSize);
82  // send the packet in broadcast
83  Ptr<UanNetDevice> dev = node->GetDevice (0)->GetObject<UanNetDevice> ();
84  dev->Send (pkt, dev->GetBroadcast (), 0);
85  // increase the sent packets number
86  ++m_sentPackets;
87 
88  Simulator::Schedule (Seconds (10),
90  this,
91  node);
92 }
93 
94 bool
96 {
97  // increase the total bytes received
98  m_bytesRx += pkt->GetSize ();
99 
100  return true;
101 }
102 
103 void
105 {
106  // create a generic node
107  m_node = CreateObject<Node> ();
108 
109  // create a default underwater channel
110  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
111  Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault> ();
112  channel->SetPropagationModel (CreateObject<UanPropModelIdeal> ());
113  channel->SetNoiseModel (noise);
114 
115  // install the underwater communication stack
116  UanHelper uan;
117  Ptr<UanNetDevice> devNode = uan.Install (m_node, channel);
118 
119  // compute a packet (header + payload) duration
120  uint32_t datarate = devNode->GetPhy ()->GetMode (0).GetDataRateBps ();
121  UanHeaderCommon hd;
122  double packetDuration = (m_packetSize + hd.GetSerializedSize ()) * 8.0 / (double) datarate;
123 
124  // energy source
126  eh.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (10000000.0));
127  eh.Install (m_node);
128 
129  // mobility model
130  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
131  mobility->SetPosition (Vector (0,0,-500));
132  m_node->AggregateObject (mobility);
133 
134  // micro modem energy model
135  AcousticModemEnergyModelHelper modemHelper;
137  DeviceEnergyModelContainer cont = modemHelper.Install (devNode,source);
138 
139  // Schedule a packet every 10 seconds
140  Simulator::ScheduleNow (&AcousticModemEnergyTestCase::SendOnePacket,
141  this,
142  m_node);
143 
144  // create a gateway node
145  m_gateway = CreateObject<Node> ();
146 
147  // install the underwater communication stack
148  Ptr<UanNetDevice> devGateway = uan.Install (m_gateway, channel);
149 
150  // energy source
151  eh.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (10000000.0));
152  eh.Install (m_gateway);
153 
154  // mobility model
155  Ptr<ConstantPositionMobilityModel> mobility2 = CreateObject<ConstantPositionMobilityModel> ();
156  mobility2->SetPosition (Vector (0,0,0));
157  m_gateway->AggregateObject (mobility2);
158 
159  // micro modem energy model
161  DeviceEnergyModelContainer cont2 = modemHelper.Install (devGateway, source2);
162 
163  // set the receive callback
166  this));
167 
168  // run the simulation
169  Simulator::Stop (Seconds (m_simTime));
170  Simulator::Run ();
171 
172  uint32_t receivedPackets = m_bytesRx / m_packetSize;
174  double consumed1 = src1->GetInitialEnergy () - src1->GetRemainingEnergy ();
175  double computed1 = cont2.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetRxPowerW () * packetDuration * receivedPackets +
176  cont2.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetIdlePowerW () * (m_simTime - (double) 2.0 / 3.0 - packetDuration * receivedPackets);
177 
178  NS_TEST_ASSERT_MSG_EQ_TOL (consumed1, computed1, 1.0e-5,
179  "Incorrect gateway consumed energy!");
180 
182  double consumed2 = src2->GetInitialEnergy () - src2->GetRemainingEnergy ();
183  double computed2 = cont.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetTxPowerW () * packetDuration * m_sentPackets +
184  cont.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetIdlePowerW () * (m_simTime - 1 - packetDuration * m_sentPackets);
185 
186  NS_TEST_ASSERT_MSG_EQ_TOL (consumed2, computed2, 1.0e-5,
187  "Incorrect node consumed energy!");
188 
189  Simulator::Destroy ();
190 }
191 
193 {
194 public:
197 
198  void DepletionHandler (void);
199  void SendOnePacket (Ptr<Node> node);
200 
201  void DoRun (void);
202 
203  double m_simTime;
204  uint32_t m_callbackCount;
205  uint32_t m_packetSize;
207 };
208 
210  : TestCase ("Acoustic Modem energy depletion test case"),
211  m_simTime (25),
212  m_callbackCount (0),
213  m_packetSize (17)
214 {
215 }
216 
218 {
219  m_node = 0;
220 }
221 
222 void
224 {
225  // increase callback count
226  m_callbackCount++;
227 }
228 
229 void
231 {
232  // create an empty packet
233  Ptr<Packet> pkt = Create<Packet> (m_packetSize);
234  // send the packet in broadcast
235  Ptr<UanNetDevice> dev = node->GetDevice (0)->GetObject<UanNetDevice> ();
236  dev->Send (pkt, dev->GetBroadcast (), 0);
237 
238  Simulator::Schedule (Seconds (10),
240  this,
241  node);
242 }
243 
244 void
246 {
247  // create a generic node
248  m_node = CreateObject<Node> ();
249 
250  // create a default underwater channel
251  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
252  Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault> ();
253  channel->SetPropagationModel (CreateObject<UanPropModelIdeal> ());
254  channel->SetNoiseModel (noise);
255 
256  // install the underwater communication stack
257  UanHelper uan;
258  Ptr<UanNetDevice> devNode = uan.Install (m_node, channel);
259 
260  // set an empty energy source
262  eh.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (0.0));
263  eh.Install (m_node);
264 
265  // mobility model
266  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
267  mobility->SetPosition (Vector (0,0,0));
268  m_node->AggregateObject (mobility);
269 
270  // micro modem energy model
271  AcousticModemEnergyModelHelper modemHelper;
273  // set the depletion callback
276  modemHelper.SetDepletionCallback (callback);
277  DeviceEnergyModelContainer cont = modemHelper.Install (devNode,source);
278 
279  // try to send a packet
281  this,
282  m_node);
283 
284  Simulator::Stop (Seconds (m_simTime));
285  Simulator::Run ();
286  Simulator::Destroy ();
287 
288  NS_TEST_ASSERT_MSG_EQ (m_callbackCount, 1, "Callback not invoked");
289 }
290 
291 // -------------------------------------------------------------------------- //
292 
298 {
299 public:
301 };
302 
304  : TestSuite ("uan-energy-model", UNIT)
305 {
306  AddTestCase (new AcousticModemEnergyTestCase, TestCase::QUICK);
307  AddTestCase (new AcousticModemEnergyDepletionTestCase, TestCase::QUICK);
308 }
309 
310 // create an instance of the test suite
UAN configuration helper.
Definition: uan-helper.h:39
DeviceEnergyModelContainer Install(Ptr< NetDevice > device, Ptr< EnergySource > source) const
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
Holds a vector of ns3::EnergySource pointers.
A suite of tests to run.
Definition: test.h:1105
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
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:744
Assign AcousticModemEnergyModel to uan devices.
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
encapsulates test code
Definition: test.h:929
a 3d vector
Definition: vector.h:31
Holds a vector of ns3::DeviceEnergyModel pointers.
a polymophic address class
Definition: address.h:86
virtual double GetInitialEnergy(void) const =0
virtual void SetReceiveCallback(ReceiveCallback cb)=0
WHOI micro-modem energy model.
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:148
Creates a BasicEnergySource object.
bool RxPacket(Ptr< NetDevice > dev, Ptr< const Packet > pkt, uint16_t mode, const Address &sender)
Ptr< NetDevice > GetDevice(uint32_t index) const
Definition: node.cc:134
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
Unit test suite for underwater energy model.
void AggregateObject(Ptr< Object > other)
Definition: object.cc:242
#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:355
Ptr< DeviceEnergyModel > Get(uint32_t i) const
Get the i-th Ptr stored in this container.
Common packet header fields.
EnergySourceContainer Install(Ptr< Node > node) const
void SendOnePacket(Ptr< Node > node)
void SendOnePacket(Ptr< LrWpanPhy > sender, Ptr< LrWpanPhy > receiver)
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:184
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
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.
NetDeviceContainer Install(NodeContainer c) const
This method creates a simple ns3::UanChannel (with a default ns3::UanNoiseModelDefault and ns3::UanPr...
Definition: uan-helper.cc:208
Hold a floating point type.
Definition: double.h:41
Ptr< T > GetObject(void) const
Definition: object.h:362
static UanEnergyModelTestSuite g_uanEnergyModelTestSuite