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 namespace ns3 {
40 
41 NS_LOG_COMPONENT_DEFINE ("UanEnergyModelTestSuite")
42  ;
43 
45 {
46 public:
49 
50  bool RxPacket (Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address &sender);
51  void SendOnePacket (Ptr<Node> node);
52 
53  void DoRun (void);
54 
55  double m_simTime;
56  uint32_t m_bytesRx;
57  uint32_t m_sentPackets;
58  uint32_t m_packetSize;
61 };
62 
64  : TestCase ("Acoustic Modem energy model test case"),
65  m_simTime (25),
66  m_bytesRx (0),
67  m_sentPackets (0),
68  m_packetSize (17)
69 {
70 }
71 
73 {
74  m_node = 0;
75  m_gateway = 0;
76 }
77 
78 void
80 {
81  // create an empty 17 bytes packet
82  Ptr<Packet> pkt = Create<Packet> (m_packetSize);
83  // send the packet in broadcast
84  Ptr<UanNetDevice> dev = node->GetDevice (0)->GetObject<UanNetDevice> ();
85  dev->Send (pkt, dev->GetBroadcast (), 0);
86  // increase the sent packets number
87  ++m_sentPackets;
88 
89  Simulator::Schedule (Seconds (10),
91  this,
92  node);
93 }
94 
95 bool
97 {
98  // increase the total bytes received
99  m_bytesRx += pkt->GetSize ();
100 
101  return true;
102 }
103 
104 void
106 {
107  // create a generic node
108  m_node = CreateObject<Node> ();
109 
110  // create a default underwater channel
111  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
112  Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault> ();
113  channel->SetPropagationModel (CreateObject<UanPropModelIdeal> ());
114  channel->SetNoiseModel (noise);
115 
116  // install the underwater communication stack
117  UanHelper uan;
118  Ptr<UanNetDevice> devNode = uan.Install (m_node, channel);
119 
120  // compute a packet (header + payload) duration
121  uint32_t datarate = devNode->GetPhy ()->GetMode (0).GetDataRateBps ();
122  UanHeaderCommon hd;
123  double packetDuration = (m_packetSize + hd.GetSerializedSize ()) * 8.0 / (double) datarate;
124 
125  // energy source
127  eh.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (10000000.0));
128  eh.Install (m_node);
129 
130  // mobility model
131  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
132  mobility->SetPosition (Vector (0,0,-500));
133  m_node->AggregateObject (mobility);
134 
135  // micro modem energy model
136  AcousticModemEnergyModelHelper modemHelper;
138  DeviceEnergyModelContainer cont = modemHelper.Install (devNode,source);
139 
140  // Schedule a packet every 10 seconds
142  this,
143  m_node);
144 
145  // create a gateway node
146  m_gateway = CreateObject<Node> ();
147 
148  // install the underwater communication stack
149  Ptr<UanNetDevice> devGateway = uan.Install (m_gateway, channel);
150 
151  // energy source
152  eh.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (10000000.0));
153  eh.Install (m_gateway);
154 
155  // mobility model
156  Ptr<ConstantPositionMobilityModel> mobility2 = CreateObject<ConstantPositionMobilityModel> ();
157  mobility2->SetPosition (Vector (0,0,0));
158  m_gateway->AggregateObject (mobility2);
159 
160  // micro modem energy model
162  DeviceEnergyModelContainer cont2 = modemHelper.Install (devGateway, source2);
163 
164  // set the receive callback
166  dev->SetReceiveCallback (MakeCallback (&AcousticModemEnergyTestCase::RxPacket,
167  this));
168 
169  // run the simulation
170  Simulator::Stop (Seconds (m_simTime));
171  Simulator::Run ();
172 
173  uint32_t receivedPackets = m_bytesRx / m_packetSize;
175  double consumed1 = src1->GetInitialEnergy () - src1->GetRemainingEnergy ();
176  double computed1 = cont2.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetRxPowerW () * packetDuration * receivedPackets +
177  cont2.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetIdlePowerW () * (m_simTime - (double) 2.0 / 3.0 - packetDuration * receivedPackets);
178 
179  NS_TEST_ASSERT_MSG_EQ_TOL (consumed1, computed1, 1.0e-5,
180  "Incorrect gateway consumed energy!");
181 
183  double consumed2 = src2->GetInitialEnergy () - src2->GetRemainingEnergy ();
184  double computed2 = cont.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetTxPowerW () * packetDuration * m_sentPackets +
185  cont.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetIdlePowerW () * (m_simTime - 1 - packetDuration * m_sentPackets);
186 
187  NS_TEST_ASSERT_MSG_EQ_TOL (consumed2, computed2, 1.0e-5,
188  "Incorrect node consumed energy!");
189 
191 }
192 
194 {
195 public:
198 
199  void DepletionHandler (void);
200  void SendOnePacket (Ptr<Node> node);
201 
202  void DoRun (void);
203 
204  double m_simTime;
205  uint32_t m_callbackCount;
206  uint32_t m_packetSize;
208 };
209 
211  : TestCase ("Acoustic Modem energy depletion test case"),
212  m_simTime (25),
213  m_callbackCount (0),
214  m_packetSize (17)
215 {
216 }
217 
219 {
220  m_node = 0;
221 }
222 
223 void
225 {
226  // increase callback count
227  m_callbackCount++;
228 }
229 
230 void
232 {
233  // create an empty packet
234  Ptr<Packet> pkt = Create<Packet> (m_packetSize);
235  // send the packet in broadcast
236  Ptr<UanNetDevice> dev = node->GetDevice (0)->GetObject<UanNetDevice> ();
237  dev->Send (pkt, dev->GetBroadcast (), 0);
238 
239  Simulator::Schedule (Seconds (10),
241  this,
242  node);
243 }
244 
245 void
247 {
248  // create a generic node
249  m_node = CreateObject<Node> ();
250 
251  // create a default underwater channel
252  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
253  Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault> ();
254  channel->SetPropagationModel (CreateObject<UanPropModelIdeal> ());
255  channel->SetNoiseModel (noise);
256 
257  // install the underwater communication stack
258  UanHelper uan;
259  Ptr<UanNetDevice> devNode = uan.Install (m_node, channel);
260 
261  // set an empty energy source
263  eh.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (0.0));
264  eh.Install (m_node);
265 
266  // mobility model
267  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> ();
268  mobility->SetPosition (Vector (0,0,0));
269  m_node->AggregateObject (mobility);
270 
271  // micro modem energy model
272  AcousticModemEnergyModelHelper modemHelper;
274  // set the depletion callback
277  modemHelper.SetDepletionCallback (callback);
278  DeviceEnergyModelContainer cont = modemHelper.Install (devNode,source);
279 
280  // try to send a packet
282  this,
283  m_node);
284 
285  Simulator::Stop (Seconds (m_simTime));
286  Simulator::Run ();
288 
289  NS_TEST_ASSERT_MSG_EQ (m_callbackCount, 1, "Callback not invoked");
290 }
291 
292 // -------------------------------------------------------------------------- //
293 
299 {
300 public:
302 };
303 
305  : TestSuite ("uan-energy-model", UNIT)
306 {
309 }
310 
311 // create an instance of the test suite
313 
314 } // namespace ns3
UAN configuration helper.
Definition: uan-helper.h:39
DeviceEnergyModelContainer Install(Ptr< NetDevice > device, Ptr< EnergySource > source) const
bool RxPacket(Ptr< NetDevice > dev, Ptr< const Packet > pkt, uint16_t mode, const Address &sender)
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
Holds a vector of ns3::EnergySource pointers.
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
#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:326
A suite of tests to run.
Definition: test.h:1025
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
Unit test suite for underwater energy model.
uint32_t GetSize(void) const
Definition: packet.h:650
Assign AcousticModemEnergyModel to uan devices.
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
encapsulates test code
Definition: test.h:849
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:824
a 3d vector
Definition: vector.h:31
Holds a vector of ns3::DeviceEnergyModel pointers.
a polymophic address class
Definition: address.h:86
WHOI micro-modem energy model.
virtual uint32_t GetSerializedSize(void) const
Creates a BasicEnergySource object.
Ptr< NetDevice > GetDevice(uint32_t index) const
Definition: node.cc:132
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1238
void DoRun(void)
Implementation to actually run this TestCase.
void AggregateObject(Ptr< Object > other)
Definition: object.cc:243
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
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
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:985
void DoRun(void)
Implementation to actually run this TestCase.
void AddTestCase(TestCase *testCase) NS_DEPRECATED
Add an individual child TestCase case to this TestCase.
Definition: test.cc:173
Net device for UAN models.
Fast test.
Definition: test.h:857
static UanEnergyModelTestSuite g_uanEnergyModelTestSuite
static void Stop(void)
If an event invokes this method, it will be the last event scheduled by the Simulator::run method bef...
Definition: simulator.cc:165
void Set(std::string name, const AttributeValue &v)
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:361
#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:137