A Discrete-Event Network Simulator
API
uan-energy-model-test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Andrea Sacco
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Andrea Sacco <andrea.sacco85@gmail.com>
18  */
19 
20 #include "ns3/acoustic-modem-energy-model-helper.h"
21 #include "ns3/acoustic-modem-energy-model.h"
22 #include "ns3/basic-energy-source-helper.h"
23 #include "ns3/constant-position-mobility-model.h"
24 #include "ns3/log.h"
25 #include "ns3/node.h"
26 #include "ns3/packet.h"
27 #include "ns3/simple-device-energy-model.h"
28 #include "ns3/simulator.h"
29 #include "ns3/test.h"
30 #include "ns3/uan-channel.h"
31 #include "ns3/uan-header-common.h"
32 #include "ns3/uan-helper.h"
33 #include "ns3/uan-net-device.h"
34 #include "ns3/uan-noise-model-default.h"
35 #include "ns3/uan-phy.h"
36 #include "ns3/uan-prop-model-ideal.h"
37 
38 using namespace ns3;
39 
40 NS_LOG_COMPONENT_DEFINE("UanEnergyModelTestSuite");
41 
54 {
55  public:
57  ~AcousticModemEnergyTestCase() override;
58 
67  bool RxPacket(Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address& sender);
72  void SendOnePacket(Ptr<Node> node);
73 
74  void DoRun() override;
75 
76  double m_simTime;
77  uint32_t m_bytesRx;
78  uint32_t m_sentPackets;
79  uint32_t m_packetSize;
82 };
83 
85  : TestCase("Acoustic Modem energy model test case"),
86  m_simTime(25),
87  m_bytesRx(0),
88  m_sentPackets(0),
89  m_packetSize(17)
90 {
91 }
92 
94 {
95  m_node = nullptr;
96  m_gateway = nullptr;
97 }
98 
99 void
101 {
102  // create an empty 17 bytes packet
103  Ptr<Packet> pkt = Create<Packet>(m_packetSize);
104  // send the packet in broadcast
105  Ptr<UanNetDevice> dev = node->GetDevice(0)->GetObject<UanNetDevice>();
106  dev->Send(pkt, dev->GetBroadcast(), 0);
107  // increase the sent packets number
108  ++m_sentPackets;
109 
110  Simulator::Schedule(Seconds(10), &AcousticModemEnergyTestCase::SendOnePacket, this, node);
111 }
112 
113 bool
115  Ptr<const Packet> pkt,
116  uint16_t /* mode */,
117  const Address& /* sender */)
118 {
119  // increase the total bytes received
120  m_bytesRx += pkt->GetSize();
121 
122  return true;
123 }
124 
125 void
127 {
128  // create a generic node
129  m_node = CreateObject<Node>();
130 
131  // create a default underwater channel
132  Ptr<UanChannel> channel = CreateObject<UanChannel>();
133  Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault>();
134  channel->SetPropagationModel(CreateObject<UanPropModelIdeal>());
135  channel->SetNoiseModel(noise);
136 
137  // install the underwater communication stack
138  UanHelper uan;
139  Ptr<UanNetDevice> devNode = uan.Install(m_node, channel);
140 
141  // compute a packet (header + payload) duration
142  uint32_t datarate = devNode->GetPhy()->GetMode(0).GetDataRateBps();
143  UanHeaderCommon hd;
144  double packetDuration = (m_packetSize + hd.GetSerializedSize()) * 8.0 / (double)datarate;
145 
146  // energy source
148  eh.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(10000000.0));
149  eh.Install(m_node);
150 
151  // mobility model
152  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel>();
153  mobility->SetPosition(Vector(0, 0, -500));
155 
156  // micro modem energy model
157  AcousticModemEnergyModelHelper modemHelper;
159  DeviceEnergyModelContainer cont = modemHelper.Install(devNode, source);
160 
161  // Schedule a packet every 10 seconds
162  Simulator::ScheduleNow(&AcousticModemEnergyTestCase::SendOnePacket, this, m_node);
163 
164  // create a gateway node
165  m_gateway = CreateObject<Node>();
166 
167  // install the underwater communication stack
168  Ptr<UanNetDevice> devGateway = uan.Install(m_gateway, channel);
169 
170  // energy source
171  eh.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(10000000.0));
172  eh.Install(m_gateway);
173 
174  // mobility model
175  Ptr<ConstantPositionMobilityModel> mobility2 = CreateObject<ConstantPositionMobilityModel>();
176  mobility2->SetPosition(Vector(0, 0, 0));
177  m_gateway->AggregateObject(mobility2);
178 
179  // micro modem energy model
181  DeviceEnergyModelContainer cont2 = modemHelper.Install(devGateway, source2);
182 
183  // set the receive callback
185  dev->SetReceiveCallback(MakeCallback(&AcousticModemEnergyTestCase::RxPacket, this));
186 
187  // run the simulation
188  Simulator::Stop(Seconds(m_simTime));
189  Simulator::Run();
190 
191  uint32_t receivedPackets = m_bytesRx / m_packetSize;
193  double consumed1 = src1->GetInitialEnergy() - src1->GetRemainingEnergy();
194  double computed1 = cont2.Get(0)->GetObject<AcousticModemEnergyModel>()->GetRxPowerW() *
195  packetDuration * receivedPackets +
196  cont2.Get(0)->GetObject<AcousticModemEnergyModel>()->GetIdlePowerW() *
197  (m_simTime - packetDuration * receivedPackets);
198 
199  NS_TEST_ASSERT_MSG_EQ_TOL(consumed1, computed1, 1.0e-5, "Incorrect gateway consumed energy!");
200 
202  double consumed2 = src2->GetInitialEnergy() - src2->GetRemainingEnergy();
203  double computed2 = cont.Get(0)->GetObject<AcousticModemEnergyModel>()->GetTxPowerW() *
204  packetDuration * m_sentPackets +
205  cont.Get(0)->GetObject<AcousticModemEnergyModel>()->GetIdlePowerW() *
206  (m_simTime - packetDuration * m_sentPackets);
207 
208  NS_TEST_ASSERT_MSG_EQ_TOL(consumed2, computed2, 1.0e-5, "Incorrect node consumed energy!");
209 
210  Simulator::Destroy();
211 }
212 
220 {
221  public:
224 
226  void DepletionHandler();
231  void SendOnePacket(Ptr<Node> node);
232 
233  void DoRun() override;
234 
235  double m_simTime;
236  uint32_t m_callbackCount;
237  uint32_t m_packetSize;
239 };
240 
242  : TestCase("Acoustic Modem energy depletion test case"),
243  m_simTime(25),
244  m_callbackCount(0),
245  m_packetSize(17)
246 {
247 }
248 
250 {
251  m_node = nullptr;
252 }
253 
254 void
256 {
257  // increase callback count
258  m_callbackCount++;
259 }
260 
261 void
263 {
264  // create an empty packet
265  Ptr<Packet> pkt = Create<Packet>(m_packetSize);
266  // send the packet in broadcast
267  Ptr<UanNetDevice> dev = node->GetDevice(0)->GetObject<UanNetDevice>();
268  dev->Send(pkt, dev->GetBroadcast(), 0);
269 
270  Simulator::Schedule(Seconds(10),
272  this,
273  node);
274 }
275 
276 void
278 {
279  // create a generic node
280  m_node = CreateObject<Node>();
281 
282  // create a default underwater channel
283  Ptr<UanChannel> channel = CreateObject<UanChannel>();
284  Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault>();
285  channel->SetPropagationModel(CreateObject<UanPropModelIdeal>());
286  channel->SetNoiseModel(noise);
287 
288  // install the underwater communication stack
289  UanHelper uan;
290  Ptr<UanNetDevice> devNode = uan.Install(m_node, channel);
291 
292  // set an empty energy source
294  eh.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(0.0));
295  eh.Install(m_node);
296 
297  // mobility model
298  Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel>();
299  mobility->SetPosition(Vector(0, 0, 0));
301 
302  // micro modem energy model
303  AcousticModemEnergyModelHelper modemHelper;
305  // set the depletion callback
308  modemHelper.SetDepletionCallback(callback);
309  DeviceEnergyModelContainer cont = modemHelper.Install(devNode, source);
310 
311  // try to send a packet
312  Simulator::ScheduleNow(&AcousticModemEnergyDepletionTestCase::SendOnePacket, this, m_node);
313 
314  Simulator::Stop(Seconds(m_simTime));
315  Simulator::Run();
316  Simulator::Destroy();
317 
318  NS_TEST_ASSERT_MSG_EQ(m_callbackCount, 1, "Callback not invoked");
319 }
320 
321 // -------------------------------------------------------------------------- //
322 
331 {
332  public:
334 };
335 
337  : TestSuite("uan-energy-model", UNIT)
338 {
339  AddTestCase(new AcousticModemEnergyTestCase, TestCase::QUICK);
341 }
342 
343 // create an instance of the test suite
Acoustic Modem Energy Depletion Test Case.
void DepletionHandler()
Depletion handler function.
void SendOnePacket(Ptr< Node > node)
Send one packet function.
void DoRun() override
Implementation to actually run this TestCase.
Acoustic Modem Energy Test Case.
Ptr< Node > m_gateway
the gateway
void DoRun() override
Implementation to actually run this TestCase.
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.
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:101
Creates a BasicEnergySource object.
void Set(std::string name, const AttributeValue &v) override
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:42
Holds a vector of ns3::EnergySource pointers.
EnergySourceContainer Install(Ptr< Node > node) const
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:152
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:259
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
Common packet header fields.
uint32_t GetSerializedSize() const override
UAN configuration helper.
Definition: uan-helper.h:42
NetDeviceContainer Install(NodeContainer c) const
This method creates a simple ns3::UanChannel (with a default ns3::UanNoiseModelDefault and ns3::UanPr...
Definition: uan-helper.cc:145
Net device for UAN models.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:144
#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:337
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
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, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
channel
Definition: third.py:88
mobility
Definition: third.py:105
static UanEnergyModelTestSuite g_uanEnergyModelTestSuite