|
|
|
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/auv-glider-helper.h" |
29 |
#include "ns3/auv-remus-helper.h" |
30 |
#include "ns3/auv-waypoint-mobility-model.h" |
31 |
#include "ns3/uan-helper.h" |
32 |
#include "ns3/basic-energy-source-helper.h" |
33 |
#include "ns3/acoustic-modem-energy-model-helper.h" |
34 |
#include "ns3/acoustic-modem-energy-model.h" |
35 |
#include "ns3/constant-position-mobility-model.h" |
36 |
#include "ns3/uan-channel.h" |
37 |
#include "ns3/uan-noise-model-default.h" |
38 |
#include "ns3/uan-prop-model-ideal.h" |
39 |
#include "ns3/uan-header-common.h" |
40 |
#include "ns3/uan-phy.h" |
41 |
|
42 |
namespace ns3 { |
43 |
|
44 |
NS_LOG_COMPONENT_DEFINE ("AuvEnergyModelTestSuite"); |
45 |
|
46 |
class AcousticModemEnergyTestCase : public TestCase |
47 |
{ |
48 |
public: |
49 |
AcousticModemEnergyTestCase (); |
50 |
~AcousticModemEnergyTestCase (); |
51 |
|
52 |
bool RxPacket (Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address &sender); |
53 |
void SendOnePacket (Ptr<Node> node); |
54 |
|
55 |
void DoRun (void); |
56 |
|
57 |
double m_simTime; |
58 |
uint32_t m_bytesRx; |
59 |
uint32_t m_sentPackets; |
60 |
uint32_t m_packetSize; |
61 |
Ptr<Node> m_node; |
62 |
Ptr<Node> m_gateway; |
63 |
}; |
64 |
|
65 |
AcousticModemEnergyTestCase::AcousticModemEnergyTestCase () |
66 |
: TestCase ("Acoustic Modem energy model test case"), |
67 |
m_simTime (25), |
68 |
m_bytesRx (0), |
69 |
m_sentPackets (0), |
70 |
m_packetSize (17) |
71 |
{ |
72 |
} |
73 |
|
74 |
AcousticModemEnergyTestCase::~AcousticModemEnergyTestCase () |
75 |
{ |
76 |
m_node = NULL; |
77 |
m_gateway = NULL; |
78 |
} |
79 |
|
80 |
void |
81 |
AcousticModemEnergyTestCase::SendOnePacket (Ptr<Node> node) |
82 |
{ |
83 |
// create an empty 17 bytes packet |
84 |
Ptr<Packet> pkt = Create<Packet> (m_packetSize); |
85 |
// send the packet in broadcast |
86 |
Ptr<UanNetDevice> dev = node->GetDevice (0)->GetObject<UanNetDevice> (); |
87 |
dev->Send (pkt, dev->GetBroadcast (), 0); |
88 |
// increase the sent packets number |
89 |
++m_sentPackets; |
90 |
|
91 |
Simulator::Schedule (Seconds (10), |
92 |
&AcousticModemEnergyTestCase::SendOnePacket, |
93 |
this, |
94 |
node); |
95 |
} |
96 |
|
97 |
bool |
98 |
AcousticModemEnergyTestCase::RxPacket (Ptr<NetDevice> dev, Ptr<const Packet> pkt, uint16_t mode, const Address &sender) |
99 |
{ |
100 |
// increase the total bytes received |
101 |
m_bytesRx += pkt->GetSize (); |
102 |
|
103 |
return true; |
104 |
} |
105 |
|
106 |
void |
107 |
AcousticModemEnergyTestCase::DoRun () |
108 |
{ |
109 |
// create a generic node |
110 |
m_node = CreateObject<Node> (); |
111 |
|
112 |
// create a default underwater channel |
113 |
Ptr<UanChannel> channel = CreateObject<UanChannel> (); |
114 |
Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault> (); |
115 |
channel->SetPropagationModel (CreateObject<UanPropModelIdeal> ()); |
116 |
channel->SetNoiseModel (noise); |
117 |
|
118 |
// install the underwater communication stack |
119 |
UanHelper uan; |
120 |
Ptr<UanNetDevice> devNode = uan.Install (m_node, channel); |
121 |
|
122 |
// compute a packet (header + payload) duration |
123 |
uint32_t datarate = devNode->GetPhy ()->GetMode (0).GetDataRateBps (); |
124 |
UanHeaderCommon hd; |
125 |
double packetDuration = (m_packetSize + hd.GetSerializedSize ()) * 8.0 / (double) datarate; |
126 |
|
127 |
// energy source |
128 |
BasicEnergySourceHelper eh; |
129 |
eh.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (10000000.0)); |
130 |
eh.Install (m_node); |
131 |
|
132 |
// mobility model |
133 |
Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> (); |
134 |
mobility->SetPosition (Vector (0,0,-500)); |
135 |
m_node->AggregateObject (mobility); |
136 |
|
137 |
// micro modem energy model |
138 |
AcousticModemEnergyModelHelper modemHelper; |
139 |
Ptr<EnergySource> source = m_node->GetObject<EnergySourceContainer> ()->Get (0); |
140 |
DeviceEnergyModelContainer cont = modemHelper.Install (devNode,source); |
141 |
|
142 |
// Schedule a packet every 10 seconds |
143 |
Simulator::ScheduleNow (&AcousticModemEnergyTestCase::SendOnePacket, |
144 |
this, |
145 |
m_node); |
146 |
|
147 |
// create a gateway node |
148 |
m_gateway = CreateObject<Node> (); |
149 |
|
150 |
// install the underwater communication stack |
151 |
Ptr<UanNetDevice> devGateway = uan.Install (m_gateway, channel); |
152 |
|
153 |
// energy source |
154 |
eh.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (10000000.0)); |
155 |
eh.Install (m_gateway); |
156 |
|
157 |
// mobility model |
158 |
Ptr<ConstantPositionMobilityModel> mobility2 = CreateObject<ConstantPositionMobilityModel> (); |
159 |
mobility2->SetPosition (Vector (0,0,0)); |
160 |
m_gateway->AggregateObject (mobility2); |
161 |
|
162 |
// micro modem energy model |
163 |
Ptr<EnergySource> source2 = m_gateway->GetObject<EnergySourceContainer> ()->Get (0); |
164 |
DeviceEnergyModelContainer cont2 = modemHelper.Install (devGateway, source2); |
165 |
|
166 |
// set the receive callback |
167 |
Ptr<NetDevice> dev = m_gateway->GetDevice (0); |
168 |
dev->SetReceiveCallback (MakeCallback (&AcousticModemEnergyTestCase::RxPacket, |
169 |
this)); |
170 |
|
171 |
// run the simulation |
172 |
Simulator::Stop (Seconds (m_simTime)); |
173 |
Simulator::Run (); |
174 |
Simulator::Destroy (); |
175 |
|
176 |
uint32_t receivedPackets = m_bytesRx / m_packetSize; |
177 |
Ptr<EnergySource> src1 = m_gateway->GetObject<EnergySourceContainer> ()->Get (0); |
178 |
double consumed1 = src1->GetInitialEnergy () - src1->GetRemainingEnergy (); |
179 |
double computed1 = cont2.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetRxPowerW () * packetDuration * receivedPackets + |
180 |
cont2.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetIdlePowerW () * (m_simTime - (double) 2.0 / 3.0 - packetDuration * receivedPackets); |
181 |
|
182 |
NS_TEST_ASSERT_MSG_EQ_TOL (consumed1, computed1, 1.0e-5, |
183 |
"Incorrect gateway consumed energy!"); |
184 |
|
185 |
Ptr<EnergySource> src2 = m_node->GetObject<EnergySourceContainer> ()->Get (0); |
186 |
double consumed2 = src2->GetInitialEnergy () - src2->GetRemainingEnergy (); |
187 |
double computed2 = cont.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetTxPowerW () * packetDuration * m_sentPackets + |
188 |
cont.Get (0)->GetObject<AcousticModemEnergyModel> ()->GetIdlePowerW () * (m_simTime - 1 - packetDuration * m_sentPackets); |
189 |
|
190 |
NS_TEST_ASSERT_MSG_EQ_TOL (consumed2, computed2, 1.0e-5, |
191 |
"Incorrect node consumed energy!"); |
192 |
|
193 |
} |
194 |
|
195 |
class AcousticModemEnergyDepletionTestCase : public TestCase |
196 |
{ |
197 |
public: |
198 |
AcousticModemEnergyDepletionTestCase (); |
199 |
~AcousticModemEnergyDepletionTestCase (); |
200 |
|
201 |
void DepletionHandler (void); |
202 |
void SendOnePacket (Ptr<Node> node); |
203 |
|
204 |
void DoRun (void); |
205 |
|
206 |
double m_simTime; |
207 |
uint32_t m_callbackCount; |
208 |
uint32_t m_packetSize; |
209 |
Ptr<Node> m_node; |
210 |
}; |
211 |
|
212 |
AcousticModemEnergyDepletionTestCase::AcousticModemEnergyDepletionTestCase () |
213 |
: TestCase ("Acoustic Modem energy depletion test case"), |
214 |
m_simTime (25), |
215 |
m_callbackCount (0), |
216 |
m_packetSize (17) |
217 |
{ |
218 |
} |
219 |
|
220 |
AcousticModemEnergyDepletionTestCase::~AcousticModemEnergyDepletionTestCase () |
221 |
{ |
222 |
m_node = NULL; |
223 |
} |
224 |
|
225 |
void |
226 |
AcousticModemEnergyDepletionTestCase::DepletionHandler (void) |
227 |
{ |
228 |
// increase callback count |
229 |
m_callbackCount++; |
230 |
} |
231 |
|
232 |
void |
233 |
AcousticModemEnergyDepletionTestCase::SendOnePacket (Ptr<Node> node) |
234 |
{ |
235 |
// create an empty packet |
236 |
Ptr<Packet> pkt = Create<Packet> (m_packetSize); |
237 |
// send the packet in broadcast |
238 |
Ptr<UanNetDevice> dev = node->GetDevice (0)->GetObject<UanNetDevice> (); |
239 |
dev->Send (pkt, dev->GetBroadcast (), 0); |
240 |
|
241 |
Simulator::Schedule (Seconds (10), |
242 |
&AcousticModemEnergyDepletionTestCase::SendOnePacket, |
243 |
this, |
244 |
node); |
245 |
} |
246 |
|
247 |
void |
248 |
AcousticModemEnergyDepletionTestCase::DoRun (void) |
249 |
{ |
250 |
// create a generic node |
251 |
m_node = CreateObject<Node> (); |
252 |
|
253 |
// create a default underwater channel |
254 |
Ptr<UanChannel> channel = CreateObject<UanChannel> (); |
255 |
Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault> (); |
256 |
channel->SetPropagationModel (CreateObject<UanPropModelIdeal> ()); |
257 |
channel->SetNoiseModel (noise); |
258 |
|
259 |
// install the underwater communication stack |
260 |
UanHelper uan; |
261 |
Ptr<UanNetDevice> devNode = uan.Install (m_node, channel); |
262 |
|
263 |
// set an empty energy source |
264 |
BasicEnergySourceHelper eh; |
265 |
eh.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (0.0)); |
266 |
eh.Install (m_node); |
267 |
|
268 |
// mobility model |
269 |
Ptr<ConstantPositionMobilityModel> mobility = CreateObject<ConstantPositionMobilityModel> (); |
270 |
mobility->SetPosition (Vector (0,0,0)); |
271 |
m_node->AggregateObject (mobility); |
272 |
|
273 |
// micro modem energy model |
274 |
AcousticModemEnergyModelHelper modemHelper; |
275 |
Ptr<EnergySource> source = m_node->GetObject<EnergySourceContainer> ()->Get (0); |
276 |
// set the depletion callback |
277 |
AcousticModemEnergyModel::AcousticModemEnergyDepletionCallback callback = |
278 |
MakeCallback (&AcousticModemEnergyDepletionTestCase::DepletionHandler, this); |
279 |
modemHelper.SetDepletionCallback (callback); |
280 |
DeviceEnergyModelContainer cont = modemHelper.Install (devNode,source); |
281 |
|
282 |
// try to send a packet |
283 |
Simulator::ScheduleNow (&AcousticModemEnergyDepletionTestCase::SendOnePacket, |
284 |
this, |
285 |
m_node); |
286 |
|
287 |
Simulator::Stop (Seconds (m_simTime)); |
288 |
Simulator::Run (); |
289 |
Simulator::Destroy (); |
290 |
|
291 |
NS_TEST_ASSERT_MSG_EQ (m_callbackCount, 1, "Callback not invoked"); |
292 |
|
293 |
} |
294 |
|
295 |
class GliderEnergyTestCase : public TestCase |
296 |
{ |
297 |
public: |
298 |
GliderEnergyTestCase (); |
299 |
~GliderEnergyTestCase (); |
300 |
|
301 |
void DoRun (void); |
302 |
void GetPosition (void); |
303 |
|
304 |
double m_simTime; |
305 |
Ptr<Node> m_node; |
306 |
}; |
307 |
|
308 |
GliderEnergyTestCase::GliderEnergyTestCase () |
309 |
: TestCase ("Glider energy model test case"), |
310 |
m_node (NULL) |
311 |
{ |
312 |
} |
313 |
|
314 |
GliderEnergyTestCase::~GliderEnergyTestCase () |
315 |
{ |
316 |
m_node = NULL; |
317 |
} |
318 |
|
319 |
void |
320 |
GliderEnergyTestCase::GetPosition () |
321 |
{ |
322 |
Ptr<AuvWaypointMobilityModel> model = m_node->GetObject<AuvWaypointMobilityModel> (); |
323 |
|
324 |
Simulator::Schedule (Seconds (1), |
325 |
&GliderEnergyTestCase::GetPosition, |
326 |
this); |
327 |
} |
328 |
|
329 |
void |
330 |
GliderEnergyTestCase::DoRun () |
331 |
{ |
332 |
m_node = CreateObject<Node> (); |
333 |
|
334 |
// create a default underwater channel |
335 |
Ptr<UanChannel> channel = CreateObject<UanChannel> (); |
336 |
Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault> (); |
337 |
channel->SetPropagationModel (CreateObject<UanPropModelIdeal> ()); |
338 |
channel->SetNoiseModel (noise); |
339 |
|
340 |
// install the underwater communication stack |
341 |
UanHelper uan; |
342 |
Ptr<UanNetDevice> devNode = uan.Install (m_node, channel); |
343 |
|
344 |
// install the glider components |
345 |
AuvGliderHelper gh; |
346 |
gh.Install (m_node); |
347 |
|
348 |
Ptr<EnergySource> motorSrc = m_node->GetObject<EnergySourceContainer> ()->Get (0); |
349 |
Ptr<EnergySource> modemSrc = m_node->GetObject<EnergySourceContainer> ()->Get (1); |
350 |
|
351 |
AcousticModemEnergyModelHelper umem; |
352 |
umem.Install (devNode, modemSrc); |
353 |
|
354 |
// move the vehicle somewhere |
355 |
Ptr<AuvWaypointMobilityModel> mob = m_node->GetObject<AuvWaypointMobilityModel> (); |
356 |
mob->AddWaypoint (Waypoint (Seconds (0), Vector (0,0,0))); |
357 |
mob->AddWaypoint (Waypoint (Seconds (20), Vector (4.5,0,-10))); |
358 |
GetPosition (); |
359 |
|
360 |
// run the simulation |
361 |
Simulator::Stop (Seconds (25)); |
362 |
Simulator::Run (); |
363 |
Simulator::Destroy (); |
364 |
|
365 |
double motorConsumption = motorSrc->GetInitialEnergy () - motorSrc->GetRemainingEnergy (); |
366 |
double motorEstimated = 19 * 0.96999; |
367 |
NS_TEST_ASSERT_MSG_EQ_TOL (motorConsumption, motorEstimated, 1.0e-4, |
368 |
"Incorrect motor consumed energy!"); |
369 |
|
370 |
double modemConsumption = modemSrc->GetInitialEnergy () - modemSrc->GetRemainingEnergy (); |
371 |
double modemEstimated = 24 * 0.158; |
372 |
NS_TEST_ASSERT_MSG_EQ_TOL (modemConsumption, modemEstimated, 1.0e-4, |
373 |
"Incorrect modem consumed energy!"); |
374 |
} |
375 |
|
376 |
class RemusEnergyTestCase : public TestCase |
377 |
{ |
378 |
public: |
379 |
RemusEnergyTestCase (); |
380 |
~RemusEnergyTestCase (); |
381 |
|
382 |
void DoRun (void); |
383 |
void GetPosition (void); |
384 |
|
385 |
double m_simTime; |
386 |
Ptr<Node> m_node; |
387 |
}; |
388 |
|
389 |
RemusEnergyTestCase::RemusEnergyTestCase () |
390 |
: TestCase ("Remus energy model test case"), |
391 |
m_node (NULL) |
392 |
{ |
393 |
} |
394 |
|
395 |
RemusEnergyTestCase::~RemusEnergyTestCase () |
396 |
{ |
397 |
m_node = NULL; |
398 |
} |
399 |
|
400 |
void |
401 |
RemusEnergyTestCase::GetPosition () |
402 |
{ |
403 |
Ptr<AuvWaypointMobilityModel> model = m_node->GetObject<AuvWaypointMobilityModel> (); |
404 |
|
405 |
Simulator::Schedule (Seconds (1), |
406 |
&RemusEnergyTestCase::GetPosition, |
407 |
this); |
408 |
} |
409 |
|
410 |
void |
411 |
RemusEnergyTestCase::DoRun () |
412 |
{ |
413 |
m_node = CreateObject<Node> (); |
414 |
|
415 |
// create a default underwater channel |
416 |
Ptr<UanChannel> channel = CreateObject<UanChannel> (); |
417 |
Ptr<UanNoiseModelDefault> noise = CreateObject<UanNoiseModelDefault> (); |
418 |
channel->SetPropagationModel (CreateObject<UanPropModelIdeal> ()); |
419 |
channel->SetNoiseModel (noise); |
420 |
|
421 |
// install the underwater communication stack |
422 |
UanHelper uan; |
423 |
Ptr<UanNetDevice> devNode = uan.Install (m_node, channel); |
424 |
|
425 |
// install the remus components |
426 |
AuvRemusHelper rh; |
427 |
rh.Install (m_node); |
428 |
|
429 |
Ptr<EnergySource> src = m_node->GetObject<EnergySourceContainer> ()->Get (0); |
430 |
|
431 |
AcousticModemEnergyModelHelper umem; |
432 |
umem.Install (devNode, src); |
433 |
|
434 |
// move the vehicle somewhere |
435 |
Ptr<AuvWaypointMobilityModel> mob = m_node->GetObject<AuvWaypointMobilityModel> (); |
436 |
mob->AddWaypoint (Waypoint (Seconds (0), Vector (0,0,0))); |
437 |
mob->AddWaypoint (Waypoint (Seconds (50), Vector (100,0,0))); |
438 |
GetPosition (); |
439 |
|
440 |
// run the simulation |
441 |
Simulator::Stop (Seconds (55)); |
442 |
Simulator::Run (); |
443 |
Simulator::Destroy (); |
444 |
|
445 |
double consumed = src->GetInitialEnergy () - src->GetRemainingEnergy (); |
446 |
// motor + modem idle energy |
447 |
double estimated = 20 * 49 + 0.158 * 54; |
448 |
|
449 |
NS_TEST_ASSERT_MSG_EQ_TOL (consumed, estimated, 1.0e-5, |
450 |
"Incorrect consumed energy!"); |
451 |
} |
452 |
|
453 |
// -------------------------------------------------------------------------- // |
454 |
|
455 |
/** |
456 |
* Unit test suite for underwater energy model. Include test on acoustic modem, |
457 |
* acoustic modem energy depletion, glider energy model, remus energy model. |
458 |
*/ |
459 |
class AuvEnergyModelTestSuite : public TestSuite |
460 |
{ |
461 |
public: |
462 |
AuvEnergyModelTestSuite (); |
463 |
}; |
464 |
|
465 |
AuvEnergyModelTestSuite::AuvEnergyModelTestSuite () |
466 |
: TestSuite ("auv-energy-model", UNIT) |
467 |
{ |
468 |
AddTestCase (new AcousticModemEnergyTestCase, TestCase::QUICK); |
469 |
AddTestCase (new AcousticModemEnergyDepletionTestCase, TestCase::QUICK); |
470 |
AddTestCase (new GliderEnergyTestCase, TestCase::QUICK); |
471 |
AddTestCase (new RemusEnergyTestCase, TestCase::QUICK); |
472 |
} |
473 |
|
474 |
// create an instance of the test suite |
475 |
AuvEnergyModelTestSuite g_auvEnergyModelTestSuite; |
476 |
|
477 |
} // namespace ns3 |