A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
38using namespace ns3;
39using namespace ns3::energy;
40
41NS_LOG_COMPONENT_DEFINE("UanEnergyModelTestSuite");
42
43/**
44 * \ingroup uan
45 * \defgroup uan-test uan module tests
46 */
47
48/**
49 * \ingroup uan-test
50 * \ingroup tests
51 *
52 * \brief Acoustic Modem Energy Test Case
53 */
55{
56 public:
59
60 /**
61 * Receive packet function
62 * \param dev the device
63 * \param pkt the packet
64 * \param mode the mode
65 * \param sender the address of the sender
66 * \returns true if successful
67 */
68 bool RxPacket(Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address& sender);
69 /**
70 * Send one packet function
71 * \param node the node to send to
72 */
73 void SendOnePacket(Ptr<Node> node);
74
75 void DoRun() override;
76
77 double m_simTime; ///< simulation time
78 uint32_t m_bytesRx; ///< bytes received
79 uint32_t m_sentPackets; ///< number of sent packets
80 uint32_t m_packetSize; ///< packet size
81 Ptr<Node> m_node; ///< node
82 Ptr<Node> m_gateway; ///< the gateway
83};
84
86 : TestCase("Acoustic Modem energy model test case"),
87 m_simTime(25),
88 m_bytesRx(0),
89 m_sentPackets(0),
90 m_packetSize(17)
91{
92}
93
95{
96 m_node = nullptr;
97 m_gateway = nullptr;
98}
99
100void
102{
103 // create an empty 17 bytes packet
104 Ptr<Packet> pkt = Create<Packet>(m_packetSize);
105 // send the packet in broadcast
106 Ptr<UanNetDevice> dev = node->GetDevice(0)->GetObject<UanNetDevice>();
107 dev->Send(pkt, dev->GetBroadcast(), 0);
108 // increase the sent packets number
110
112}
113
114bool
117 uint16_t /* mode */,
118 const Address& /* sender */)
119{
120 // increase the total bytes received
121 m_bytesRx += pkt->GetSize();
122
123 return true;
124}
125
126void
128{
129 // create a generic node
130 m_node = CreateObject<Node>();
131
132 // create a default underwater channel
133 Ptr<UanChannel> channel = CreateObject<UanChannel>();
134 Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault>();
135 channel->SetPropagationModel(CreateObject<UanPropModelIdeal>());
136 channel->SetNoiseModel(noise);
137
138 // install the underwater communication stack
139 UanHelper uan;
140 Ptr<UanNetDevice> devNode = uan.Install(m_node, channel);
141
142 // compute a packet (header + payload) duration
143 uint32_t datarate = devNode->GetPhy()->GetMode(0).GetDataRateBps();
145 double packetDuration = (m_packetSize + hd.GetSerializedSize()) * 8.0 / (double)datarate;
146
147 // energy source
149 eh.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(10000000.0));
150 eh.Install(m_node);
151
152 // mobility model
153 Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel>();
154 mobility->SetPosition(Vector(0, 0, -500));
155 m_node->AggregateObject(mobility);
156
157 // micro modem energy model
160 DeviceEnergyModelContainer cont = modemHelper.Install(devNode, source);
161
162 // Schedule a packet every 10 seconds
164
165 // create a gateway node
166 m_gateway = CreateObject<Node>();
167
168 // install the underwater communication stack
169 Ptr<UanNetDevice> devGateway = uan.Install(m_gateway, channel);
170
171 // energy source
172 eh.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(10000000.0));
173 eh.Install(m_gateway);
174
175 // mobility model
176 Ptr<ConstantPositionMobilityModel> mobility2 = CreateObject<ConstantPositionMobilityModel>();
177 mobility2->SetPosition(Vector(0, 0, 0));
178 m_gateway->AggregateObject(mobility2);
179
180 // micro modem energy model
182 DeviceEnergyModelContainer cont2 = modemHelper.Install(devGateway, source2);
183
184 // set the receive callback
186 dev->SetReceiveCallback(MakeCallback(&AcousticModemEnergyTestCase::RxPacket, this));
187
188 // run the simulation
191
192 uint32_t receivedPackets = m_bytesRx / m_packetSize;
194 double consumed1 = src1->GetInitialEnergy() - src1->GetRemainingEnergy();
195 double computed1 = cont2.Get(0)->GetObject<AcousticModemEnergyModel>()->GetRxPowerW() *
196 packetDuration * receivedPackets +
197 cont2.Get(0)->GetObject<AcousticModemEnergyModel>()->GetIdlePowerW() *
198 (m_simTime - packetDuration * receivedPackets);
199
200 NS_TEST_ASSERT_MSG_EQ_TOL(consumed1, computed1, 1.0e-5, "Incorrect gateway consumed energy!");
201
203 double consumed2 = src2->GetInitialEnergy() - src2->GetRemainingEnergy();
204 double computed2 = cont.Get(0)->GetObject<AcousticModemEnergyModel>()->GetTxPowerW() *
205 packetDuration * m_sentPackets +
206 cont.Get(0)->GetObject<AcousticModemEnergyModel>()->GetIdlePowerW() *
207 (m_simTime - packetDuration * m_sentPackets);
208
209 NS_TEST_ASSERT_MSG_EQ_TOL(consumed2, computed2, 1.0e-5, "Incorrect node consumed energy!");
210
212}
213
214/**
215 * \ingroup uan-test
216 * \ingroup tests
217 *
218 * \brief Acoustic Modem Energy Depletion Test Case
219 */
221{
222 public:
225
226 /// Depletion handler function
227 void DepletionHandler();
228 /**
229 * Send one packet function
230 * \param node the node to send to
231 */
232 void SendOnePacket(Ptr<Node> node);
233
234 void DoRun() override;
235
236 double m_simTime; ///< Simulation time
237 uint32_t m_callbackCount; ///< callback count
238 uint32_t m_packetSize; ///< packet size
239 Ptr<Node> m_node; ///< the node
240};
241
243 : TestCase("Acoustic Modem energy depletion test case"),
244 m_simTime(25),
245 m_callbackCount(0),
246 m_packetSize(17)
247{
248}
249
251{
252 m_node = nullptr;
253}
254
255void
257{
258 // increase callback count
260}
261
262void
264{
265 // create an empty packet
266 Ptr<Packet> pkt = Create<Packet>(m_packetSize);
267 // send the packet in broadcast
268 Ptr<UanNetDevice> dev = node->GetDevice(0)->GetObject<UanNetDevice>();
269 dev->Send(pkt, dev->GetBroadcast(), 0);
270
273 this,
274 node);
275}
276
277void
279{
280 // create a generic node
281 m_node = CreateObject<Node>();
282
283 // create a default underwater channel
284 Ptr<UanChannel> channel = CreateObject<UanChannel>();
285 Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault>();
286 channel->SetPropagationModel(CreateObject<UanPropModelIdeal>());
287 channel->SetNoiseModel(noise);
288
289 // install the underwater communication stack
290 UanHelper uan;
291 Ptr<UanNetDevice> devNode = uan.Install(m_node, channel);
292
293 // set an empty energy source
295 eh.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(0.0));
296 eh.Install(m_node);
297
298 // mobility model
299 Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel>();
300 mobility->SetPosition(Vector(0, 0, 0));
301 m_node->AggregateObject(mobility);
302
303 // micro modem energy model
306 // set the depletion callback
309 modemHelper.SetDepletionCallback(callback);
310 DeviceEnergyModelContainer cont = modemHelper.Install(devNode, source);
311
312 // try to send a packet
314
318
319 NS_TEST_ASSERT_MSG_EQ(m_callbackCount, 1, "Callback not invoked");
320}
321
322// -------------------------------------------------------------------------- //
323
324/**
325 * \ingroup uan-test
326 * \ingroup tests
327 *
328 * \brief Unit test suite for underwater energy model. Include test on acoustic modem,
329 * acoustic modem energy depletion.
330 */
332{
333 public:
335};
336
338 : TestSuite("uan-energy-model", Type::UNIT)
339{
340 AddTestCase(new AcousticModemEnergyTestCase, TestCase::Duration::QUICK);
341 AddTestCase(new AcousticModemEnergyDepletionTestCase, TestCase::Duration::QUICK);
342}
343
344// 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
energy::DeviceEnergyModelContainer Install(Ptr< NetDevice > device, Ptr< energy::EnergySource > source) const
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
energy::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:149
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:522
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:309
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition: simulator.h:605
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:302
A suite of tests to run.
Definition: test.h:1273
Type
Type of test.
Definition: test.h:1280
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.
Holds a vector of ns3::DeviceEnergyModel pointers.
Ptr< DeviceEnergyModel > Get(uint32_t i) const
Get the i-th Ptr<DeviceEnergyModel> stored in this container.
Holds a vector of ns3::EnergySource pointers.
#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:145
#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:338
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
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:700
static UanEnergyModelTestSuite g_uanEnergyModelTestSuite