DESERT 3.5.1
Loading...
Searching...
No Matches
uwahoi-phy.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2017 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
39#include "uwahoi-phy.h"
40#include <fstream>
41#include <sstream>
42
46static class UwAhoiPhyClass : public TclClass
47{
48public:
50 : TclClass("Module/UW/AHOI/PHY")
51 {
52 }
53 TclObject *
54 create(int, const char *const *)
55 {
56 return (new UwAhoiPhy);
57 }
59/*
60L{ 25, 50, 95, 120, 140, 160, 180, 190 },
61P_SUCC{ 0.923077*39/40, 0.913793*58/60, 0.924528*53/60, 0.876712*73/80,
62 0.61643*73/80, 0.75*28/40, 0.275862*29/40, 0 }*/
65 , pdr_file_name_("dbs/ahoi/default_pdr.csv")
66 , sir_file_name_("dbs/ahoi/default_sir.csv")
67 , pdr_token_separator_(',')
68 , range2pdr_()
69 , sir2pdr_()
70 , initLUT_(false)
71{ // binding to TCL variables
72 Interference_Model = "MEANPOWER";
73}
74
78
79int
80UwAhoiPhy::command(int argc, const char *const *argv)
81{
82 if (argc == 2) {
83 if (strcasecmp(argv[1], "initLUT") == 0) {
85 return TCL_OK;
86 }
87 } else if (argc == 3) {
88 if (strcasecmp(argv[1], "setRangePDRFileName") == 0) {
89 string tmp_ = ((char *) argv[2]);
90 if (tmp_.size() == 0) {
91 fprintf(stderr, "Empty string for the file name");
92 return TCL_ERROR;
93 }
94 pdr_file_name_ = tmp_;
95 return TCL_OK;
96 } else if (strcasecmp(argv[1], "setSIRFileName") == 0) {
97 string tmp_ = ((char *) argv[2]);
98 if (tmp_.size() == 0) {
99 fprintf(stderr, "Empty string for the file name");
100 return TCL_ERROR;
101 }
102 sir_file_name_ = tmp_;
103 return TCL_OK;
104 } else if (strcasecmp(argv[1], "setLUTSeparator") == 0) {
105 string tmp_ = ((char *) argv[2]);
106 if (tmp_.size() == 0) {
107 fprintf(stderr, "Empty char for the file name");
108 return TCL_ERROR;
109 }
110 pdr_token_separator_ = tmp_.at(0);
111 return TCL_OK;
112 }
113 }
114 return UnderwaterPhysical::command(argc, argv);
115}
116
117void
119{
120 ifstream input_file_;
121 string line_;
122 char *tmp_ = new char[pdr_file_name_.length() + 1];
123 strcpy(tmp_, pdr_file_name_.c_str());
124 input_file_.open(tmp_);
125 if (input_file_.is_open()) {
126 // skip first 2 lines
127 if (debug_)
128 std::cout << "UwAhoiPhy::initializeRangeLUT()" << endl;
129 while (std::getline(input_file_, line_)) {
130 // TODO: retrive the right row and break the loop
131 std::istringstream line_stream(line_);
132 std::string token;
133 std::getline(line_stream, token, pdr_token_separator_);
134 double d = stod(token);
135 std::getline(line_stream, token, pdr_token_separator_);
136 double p = stod(token);
137 range2pdr_[d] = p;
138 if (debug_)
139 std::cout << d << " " << p << endl;
140 }
141 } else {
142 cerr << "Impossible to open file " << pdr_file_name_ << endl;
143 }
144
145 ifstream input_file_2;
146 tmp_ = new char[sir_file_name_.length() + 1];
147 strcpy(tmp_, sir_file_name_.c_str());
148 input_file_2.open(tmp_);
149 if (input_file_2.is_open()) {
150 // skip first 2 lines
151 if (debug_)
152 std::cout << "UwAhoiPhy::initializeSIRLUT()" << endl;
153 while (std::getline(input_file_2, line_)) {
154 // TODO: retrive the right row and break the loop
155 std::istringstream line_stream(line_);
156 std::string token;
157 std::getline(line_stream, token, pdr_token_separator_);
158 double sir = stod(token);
159 std::getline(line_stream, token, pdr_token_separator_);
160 double p = stod(token);
161 sir2pdr_[sir] = p;
162 if (debug_)
163 std::cout << sir << " " << p << endl;
164 }
165 } else {
166 cerr << "Impossible to open file " << sir_file_name_ << endl;
167 }
168
169 initLUT_ = true;
170}
171
172void
174{
175 if (!initLUT_)
176 cerr << "UwAhoiPhy ERROR: FIRST INITIALIZE LUT!" << endl;
177 hdr_cmn *ch = HDR_CMN(p);
178 hdr_MPhy *ph = HDR_MPHY(p);
179 hdr_mac *mach = HDR_MAC(p);
180 counter interferent_pkts;
181 static int mac_addr = -1;
182 ClMsgPhy2MacAddr msg;
183 sendSyncClMsg(&msg);
184 mac_addr = msg.getAddr();
185 if (PktRx != 0) {
186 if (PktRx == p) {
187 double per_ni; // packet error rate due to noise and/or interference
188 double per_n; // packet error rate due to noise only
189 double x = RNG::defaultrng()->uniform_double();
190 per_n = getPER(ph->Pr / ph->Pn, p);
191 bool error_n = x <= per_n;
192 error_n = 0;
193 bool error_ni = 0;
194 double interference = interference_ ?
196 if (!error_n) {
197 per_ni = interference?getPER(ph->Pr/interference,p):getPER(0,p);
198 error_ni = x <= per_ni;
199 }
200 if (time_ready_to_end_rx_ > Scheduler::instance().clock()) {
201 Rx_Time_ = Rx_Time_ + ph->duration - time_ready_to_end_rx_ +
202 Scheduler::instance().clock();
203 } else {
204 Rx_Time_ += ph->duration;
205 }
207 Scheduler::instance().clock() + ph->duration;
208 Energy_Rx_ += consumedEnergyRx(ph->duration);
209
210 ch->error() = error_ni || error_n;
211 if (debug_) {
212 if (error_ni == 1) {
213 std::cout
214 << NOW << " UwAhoiPhy(" << mac_addr
215 << ")::endRx() packet " << ch->uid()
216 << " contains errors due to noise and interference."
217 << std::endl;
218 } else if (error_n == 1) {
219 std::cout << NOW << " UwAhoiPhy(" << mac_addr
220 << ")::endRx() packet " << ch->uid()
221 << " contains errors due to noise." << std::endl;
222 }
223 }
224 if (error_n) {
225 incrErrorPktsNoise();
226 if (mach->ftype() != MF_CONTROL) {
228 } else if (mach->ftype() == MF_CONTROL) {
230 }
231 } else if (error_ni) {
232 if (mach->ftype() != MF_CONTROL) {
233 incrErrorPktsInterf();
235 if (interferent_pkts.second >= 1) {
237 } else {
238 if (interferent_pkts.first > 0) {
240 }
241 }
242 } else if (mach->ftype() == MF_CONTROL) {
245 if (interferent_pkts.first > 0) {
247 }
248 }
249 }
250 sendUp(p);
251 PktRx = 0;
252 } else {
253 dropPacket(p);
254 }
255 } else {
256 dropPacket(p);
257 }
258}
259
260double
261UwAhoiPhy::getPER(double _sir, Packet *_p)
262{
263 double pdr = matchDistancePDR(getDistance(_p));
264
265 if(_sir > 0) {
266 pdr = pdr * matchSIR_PDR(_sir);
267 }
268 return 1 - pdr;
269}
270
271double
273{
274 hdr_MPhy *ph = HDR_MPHY(_p);
275 double x_src = (ph->srcPosition)->getX();
276 double y_src = (ph->srcPosition)->getY();
277 double z_src = (ph->srcPosition)->getZ();
278 double x_dst = (ph->dstPosition)->getX();
279 double y_dst = (ph->dstPosition)->getY();
280 double z_dst = (ph->dstPosition)->getZ();
281 return sqrt(pow(x_src - x_dst, 2.0) + pow(y_src - y_dst, 2.0) +
282 pow(z_src - z_dst, 2.0));
283}
284
285double
287{ // success probability of AHOI
288 if (debug_)
289 std::cout << NOW
290 << " UwAhoiPhy()::matchDistancePDR(double distance)"
291 << "distance = " << distance << std::endl;
292 if (distance <= range2pdr_.begin()->first)
293 return range2pdr_.begin()->second;
294
295 PdrLut::iterator it = range2pdr_.lower_bound(distance);
296 if (it == range2pdr_.end())
297 return (--it)->second;
298 double l_sup = it->first;
299 double p_sup = it->second;
300 it--;
301 double l_inf = it->first;
302 double p_inf = it->second;
303 if (debug_) {
304 std::cout << " Distance between " << l_inf << " and " << l_sup;
305 std::cout << " Succ Prob between " << p_inf << " and " << p_sup;
306 }
307 return linearInterpolator(distance, l_inf, p_inf, l_sup, p_sup);
308}
309
310double
312{ // success probability of AHOI
313 double sir_db = 10*log10(sir/10);
314 if (debug_)
315 std::cout << NOW
316 << " UwAhoiPhy()::matchSIR_PDR(double sir)"
317 << "sir = " << sir << std::endl;
318 if (sir_db <= sir2pdr_.begin()->first)
319 return sir2pdr_.begin()->second;
320
321 PdrLut::iterator it = sir2pdr_.lower_bound(sir_db);
322 if (it == sir2pdr_.end())
323 return (--it)->second;
324 double l_sup = it->first;
325 double p_sup = it->second;
326 it--;
327 double l_inf = it->first;
328 double p_inf = it->second;
329 if (debug_) {
330 std::cout << " SIR between " << l_inf << " and " << l_sup;
331 std::cout << " Succ Prob between " << p_inf << " and " << p_sup;
332 }
333 return linearInterpolator(sir_db, l_inf, p_inf, l_sup, p_sup);
334}
335
336double
338 double x, double x1, double y1, double x2, double y2)
339{
340 double m = (y1 - y2) / (x1 - x2);
341 double q = y1 - m * x1;
342 if (debug_)
343 std::cout << NOW << " UwAhoiPhy::linearInterpolator( double x, "
344 "double x1, double y1, double x2, double y2 )"
345 << "m = " << m << " q= " << q << std::endl;
346 return m * x + q;
347}
348
void incrTot_pkts_lost()
Increment the number of packets discarded.
Definition uwphysical.h:295
std::string Interference_Model
Interference calcuation mode chosen: CHUNK model or MEANPOWER model.
Definition uwphysical.h:463
virtual double consumedEnergyRx(const double &_duration)
Compute the energy (in Joule) spent by the modem in reception.
Definition uwphysical.h:200
uwinterference * interference_
Pointer to the interference model module.
Definition uwphysical.h:467
void incrErrorCtrlPktsInterf()
Increment the number of CTRL packets discarded due to interference.
Definition uwphysical.h:313
double Energy_Rx_
Energy (in Joule) spent by the node in transmission.
Definition uwphysical.h:441
void incrCollisionCTRL()
Increment the number of CTRL pkts discarded due to a collision.
Definition uwphysical.h:331
void incrCollisionDATAvsCTRL()
Increment the number of collisions DATA/CTRL.
Definition uwphysical.h:322
double time_ready_to_end_rx_
Used to keep track of the arrival time.
Definition uwphysical.h:430
double Rx_Time_
Time (in seconds) spent by the node in reception.
Definition uwphysical.h:435
virtual int command(int, const char *const *)
TCL command interpreter.
void incrCollisionDATA()
Increment the number of DATA pkts discarded due to a collision.
Definition uwphysical.h:340
void incrTotCrtl_pkts_lost()
Increment the number of CTRL packets discarded.
Definition uwphysical.h:304
Adds the module for UwCbrModuleClass in ns2.
TclObject * create(int, const char *const *)
virtual int command(int, const char *const *)
TCL command interpreter.
virtual double matchSIR_PDR(double sir)
Return the PER via linear interpolation.
virtual double matchDistancePDR(double distance)
Return the PER via linear interpolation.
virtual void endRx(Packet *p)
Handles the end of a packet reception.
PdrLut sir2pdr_
Definition uwahoi-phy.h:138
virtual double getPER(double snr, Packet *)
Returns the packet error rate by using the length of a packet and the information contained in the pa...
string pdr_file_name_
Definition uwahoi-phy.h:134
string sir_file_name_
Definition uwahoi-phy.h:135
UwAhoiPhy()
Constructor of UwAhoiPhy class.
virtual double linearInterpolator(double x, double x1, double y1, double x2, double y2)
Return y via linear interpolation given two points.
virtual double getDistance(Packet *)
Return the distance between source and destination.
char pdr_token_separator_
Definition uwahoi-phy.h:136
bool initLUT_
Definition uwahoi-phy.h:139
PdrLut range2pdr_
Definition uwahoi-phy.h:137
virtual ~UwAhoiPhy()
Destructor of UwAhoiPhy class.
virtual void initializeLUT()
virtual double getInterferencePower(Packet *p)
Compute the average interference power for the given packet.
UwAhoiPhyClass class_module_UwAhoiPhy
Definition of UwAhoiPhy class.
std::pair< int, int > counter
counter of collisions
const double q