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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Andrea Sacco <andrea.sacco85@gmail.com>
7 */
8
9#include "ns3/acoustic-modem-energy-model-helper.h"
10#include "ns3/acoustic-modem-energy-model.h"
11#include "ns3/basic-energy-source-helper.h"
12#include "ns3/constant-position-mobility-model.h"
13#include "ns3/log.h"
14#include "ns3/node.h"
15#include "ns3/packet.h"
16#include "ns3/simple-device-energy-model.h"
17#include "ns3/simulator.h"
18#include "ns3/test.h"
19#include "ns3/uan-channel.h"
20#include "ns3/uan-header-common.h"
21#include "ns3/uan-helper.h"
22#include "ns3/uan-net-device.h"
23#include "ns3/uan-noise-model-default.h"
24#include "ns3/uan-phy.h"
25#include "ns3/uan-prop-model-ideal.h"
26
27using namespace ns3;
28using namespace ns3::energy;
29
30NS_LOG_COMPONENT_DEFINE("UanEnergyModelTestSuite");
31
32/**
33 * \ingroup uan
34 * \defgroup uan-test uan module tests
35 */
36
37/**
38 * \ingroup uan-test
39 * \ingroup tests
40 *
41 * \brief Acoustic Modem Energy Test Case
42 */
44{
45 public:
48
49 /**
50 * Receive packet function
51 * \param dev the device
52 * \param pkt the packet
53 * \param mode the mode
54 * \param sender the address of the sender
55 * \returns true if successful
56 */
57 bool RxPacket(Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address& sender);
58 /**
59 * Send one packet function
60 * \param node the node to send to
61 */
62 void SendOnePacket(Ptr<Node> node);
63
64 void DoRun() override;
65
66 double m_simTime; ///< simulation time
67 uint32_t m_bytesRx; ///< bytes received
68 uint32_t m_sentPackets; ///< number of sent packets
69 uint32_t m_packetSize; ///< packet size
70 Ptr<Node> m_node; ///< node
71 Ptr<Node> m_gateway; ///< the gateway
72};
73
75 : TestCase("Acoustic Modem energy model test case"),
76 m_simTime(25),
77 m_bytesRx(0),
78 m_sentPackets(0),
79 m_packetSize(17)
80{
81}
82
88
89void
91{
92 // create an empty 17 bytes packet
94 // send the packet in broadcast
95 Ptr<UanNetDevice> dev = node->GetDevice(0)->GetObject<UanNetDevice>();
96 dev->Send(pkt, dev->GetBroadcast(), 0);
97 // increase the sent packets number
99
101}
102
103bool
106 uint16_t /* mode */,
107 const Address& /* sender */)
108{
109 // increase the total bytes received
110 m_bytesRx += pkt->GetSize();
111
112 return true;
113}
114
115void
117{
118 // create a generic node
120
121 // create a default underwater channel
124 channel->SetPropagationModel(CreateObject<UanPropModelIdeal>());
125 channel->SetNoiseModel(noise);
126
127 // install the underwater communication stack
128 UanHelper uan;
129 Ptr<UanNetDevice> devNode = uan.Install(m_node, channel);
130
131 // compute a packet (header + payload) duration
132 uint32_t datarate = devNode->GetPhy()->GetMode(0).GetDataRateBps();
134 double packetDuration = (m_packetSize + hd.GetSerializedSize()) * 8.0 / (double)datarate;
135
136 // energy source
138 eh.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(10000000.0));
139 eh.Install(m_node);
140
141 // mobility model
143 mobility->SetPosition(Vector(0, 0, -500));
144 m_node->AggregateObject(mobility);
145
146 // micro modem energy model
149 DeviceEnergyModelContainer cont = modemHelper.Install(devNode, source);
150
151 // Schedule a packet every 10 seconds
153
154 // create a gateway node
156
157 // install the underwater communication stack
158 Ptr<UanNetDevice> devGateway = uan.Install(m_gateway, channel);
159
160 // energy source
161 eh.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(10000000.0));
162 eh.Install(m_gateway);
163
164 // mobility model
166 mobility2->SetPosition(Vector(0, 0, 0));
167 m_gateway->AggregateObject(mobility2);
168
169 // micro modem energy model
171 DeviceEnergyModelContainer cont2 = modemHelper.Install(devGateway, source2);
172
173 // set the receive callback
175 dev->SetReceiveCallback(MakeCallback(&AcousticModemEnergyTestCase::RxPacket, this));
176
177 // run the simulation
180
181 uint32_t receivedPackets = m_bytesRx / m_packetSize;
183 double consumed1 = src1->GetInitialEnergy() - src1->GetRemainingEnergy();
184 double computed1 = cont2.Get(0)->GetObject<AcousticModemEnergyModel>()->GetRxPowerW() *
185 packetDuration * receivedPackets +
186 cont2.Get(0)->GetObject<AcousticModemEnergyModel>()->GetIdlePowerW() *
187 (m_simTime - packetDuration * receivedPackets);
188
189 NS_TEST_ASSERT_MSG_EQ_TOL(consumed1, computed1, 1.0e-5, "Incorrect gateway consumed energy!");
190
192 double consumed2 = src2->GetInitialEnergy() - src2->GetRemainingEnergy();
193 double computed2 = cont.Get(0)->GetObject<AcousticModemEnergyModel>()->GetTxPowerW() *
194 packetDuration * m_sentPackets +
195 cont.Get(0)->GetObject<AcousticModemEnergyModel>()->GetIdlePowerW() *
196 (m_simTime - packetDuration * m_sentPackets);
197
198 NS_TEST_ASSERT_MSG_EQ_TOL(consumed2, computed2, 1.0e-5, "Incorrect node consumed energy!");
199
201}
202
203/**
204 * \ingroup uan-test
205 * \ingroup tests
206 *
207 * \brief Acoustic Modem Energy Depletion Test Case
208 */
210{
211 public:
214
215 /// Depletion handler function
216 void DepletionHandler();
217 /**
218 * Send one packet function
219 * \param node the node to send to
220 */
221 void SendOnePacket(Ptr<Node> node);
222
223 void DoRun() override;
224
225 double m_simTime; ///< Simulation time
226 uint32_t m_callbackCount; ///< callback count
227 uint32_t m_packetSize; ///< packet size
228 Ptr<Node> m_node; ///< the node
229};
230
232 : TestCase("Acoustic Modem energy depletion test case"),
233 m_simTime(25),
234 m_callbackCount(0),
235 m_packetSize(17)
236{
237}
238
243
244void
246{
247 // increase callback count
249}
250
251void
253{
254 // create an empty packet
256 // send the packet in broadcast
257 Ptr<UanNetDevice> dev = node->GetDevice(0)->GetObject<UanNetDevice>();
258 dev->Send(pkt, dev->GetBroadcast(), 0);
259
262 this,
263 node);
264}
265
266void
268{
269 // create a generic node
271
272 // create a default underwater channel
275 channel->SetPropagationModel(CreateObject<UanPropModelIdeal>());
276 channel->SetNoiseModel(noise);
277
278 // install the underwater communication stack
279 UanHelper uan;
280 Ptr<UanNetDevice> devNode = uan.Install(m_node, channel);
281
282 // set an empty energy source
284 eh.Set("BasicEnergySourceInitialEnergyJ", DoubleValue(0.0));
285 eh.Install(m_node);
286
287 // mobility model
289 mobility->SetPosition(Vector(0, 0, 0));
290 m_node->AggregateObject(mobility);
291
292 // micro modem energy model
295 // set the depletion callback
298 modemHelper.SetDepletionCallback(callback);
299 DeviceEnergyModelContainer cont = modemHelper.Install(devNode, source);
300
301 // try to send a packet
303
307
308 NS_TEST_ASSERT_MSG_EQ(m_callbackCount, 1, "Callback not invoked");
309}
310
311// -------------------------------------------------------------------------- //
312
313/**
314 * \ingroup uan-test
315 * \ingroup tests
316 *
317 * \brief Unit test suite for underwater energy model. Include test on acoustic modem,
318 * acoustic modem energy depletion.
319 */
321{
322 public:
324};
325
327 : TestSuite("uan-energy-model", Type::UNIT)
328{
329 AddTestCase(new AcousticModemEnergyTestCase, TestCase::Duration::QUICK);
330 AddTestCase(new AcousticModemEnergyDepletionTestCase, TestCase::Duration::QUICK);
331}
332
333// 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.
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.
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:90
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:31
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:138
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition object.cc:298
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static EventId ScheduleNow(FUNC f, Ts &&... args)
Schedule an event to expire Now.
Definition simulator.h:594
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
Common packet header fields.
uint32_t GetSerializedSize() const override
UAN configuration helper.
Definition uan-helper.h:31
NetDeviceContainer Install(NodeContainer c) const
This method creates a simple ns3::UanChannel (with a default ns3::UanNoiseModelDefault and ns3::UanPr...
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:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
#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:134
#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:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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:684
static UanEnergyModelTestSuite g_uanEnergyModelTestSuite