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;
39
40NS_LOG_COMPONENT_DEFINE("UanEnergyModelTestSuite");
41
42/**
43 * \ingroup uan
44 * \defgroup uan-test uan module tests
45 */
46
47/**
48 * \ingroup uan-test
49 * \ingroup tests
50 *
51 * \brief Acoustic Modem Energy Test Case
52 */
54{
55 public:
58
59 /**
60 * Receive packet function
61 * \param dev the device
62 * \param pkt the packet
63 * \param mode the mode
64 * \param sender the address of the sender
65 * \returns true if successful
66 */
67 bool RxPacket(Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address& sender);
68 /**
69 * Send one packet function
70 * \param node the node to send to
71 */
72 void SendOnePacket(Ptr<Node> node);
73
74 void DoRun() override;
75
76 double m_simTime; ///< simulation time
77 uint32_t m_bytesRx; ///< bytes received
78 uint32_t m_sentPackets; ///< number of sent packets
79 uint32_t m_packetSize; ///< packet size
80 Ptr<Node> m_node; ///< node
81 Ptr<Node> m_gateway; ///< the gateway
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
99void
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
109
111}
112
113bool
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
125void
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();
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));
154 m_node->AggregateObject(mobility);
155
156 // micro modem energy model
159 DeviceEnergyModelContainer cont = modemHelper.Install(devNode, source);
160
161 // Schedule a packet every 10 seconds
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
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
211}
212
213/**
214 * \ingroup uan-test
215 * \ingroup tests
216 *
217 * \brief Acoustic Modem Energy Depletion Test Case
218 */
220{
221 public:
224
225 /// Depletion handler function
226 void DepletionHandler();
227 /**
228 * Send one packet function
229 * \param node the node to send to
230 */
231 void SendOnePacket(Ptr<Node> node);
232
233 void DoRun() override;
234
235 double m_simTime; ///< Simulation time
236 uint32_t m_callbackCount; ///< callback count
237 uint32_t m_packetSize; ///< packet size
238 Ptr<Node> m_node; ///< the node
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
254void
256{
257 // increase callback count
259}
260
261void
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
272 this,
273 node);
274}
275
276void
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));
300 m_node->AggregateObject(mobility);
301
302 // micro modem energy model
305 // set the depletion callback
308 modemHelper.SetDepletionCallback(callback);
309 DeviceEnergyModelContainer cont = modemHelper.Install(devNode, source);
310
311 // try to send a packet
313
317
318 NS_TEST_ASSERT_MSG_EQ(m_callbackCount, 1, "Callback not invoked");
319}
320
321// -------------------------------------------------------------------------- //
322
323/**
324 * \ingroup uan-test
325 * \ingroup tests
326 *
327 * \brief Unit test suite for underwater energy model. Include test on acoustic modem,
328 * acoustic modem energy depletion.
329 */
331{
332 public:
334};
335
337 : TestSuite("uan-energy-model", Type::UNIT)
338{
339 AddTestCase(new AcousticModemEnergyTestCase, TestCase::Duration::QUICK);
340 AddTestCase(new AcousticModemEnergyDepletionTestCase, TestCase::Duration::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: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:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
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: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:1326
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
static UanEnergyModelTestSuite g_uanEnergyModelTestSuite