45 : TclClass(
"Module/UW/PHYSICALDB")
57 interference_(nullptr),
66 interf_val({0.0,0.0}),
71 bind_error(
"token_separator_", &token_separator);
73 token_separator =
'\t';
80 if (strcasecmp(argv[1],
"addr") == 0) {
81 ipAddr_ =
static_cast<uint8_t
>(atoi(argv[2]));
83 fprintf(stderr,
"0 is not a valid IP address");
87 }
else if (strcasecmp(argv[1],
"setCountry") == 0) {
90 }
else if (strcasecmp(argv[1],
"setModulation") == 0) {
93 }
else if (strcasecmp(argv[1],
"addSnr") == 0) {
94 snr.insert(
snr.end(), strtod(argv[2], NULL));
96 }
else if (strcasecmp(argv[1],
"addSir") == 0) {
97 sir.insert(
sir.end(), strtod(argv[2], NULL));
99 }
else if (strcasecmp(argv[1],
"addOverlap") == 0) {
102 }
else if (strcasecmp(argv[1],
"setPath") == 0) {
103 path_ = (
char *) (argv[2]);
105 }
else if (strcasecmp(argv[1],
"setInterference") == 0) {
112 }
else if (argc == 4) {
113 if (strcasecmp(argv[1],
"addRange") == 0) {
114 uint8_t node_id_ = atoi(argv[2]);
115 double range_ = atof(argv[3]);
116 std::map<uint8_t, std::set<double> >::iterator it =
117 range.find(node_id_);
118 if (it ==
range.end()) {
119 std::set<double> tmp_set_;
120 tmp_set_.insert(range_);
122 pair<uint8_t, std::set<double> >(node_id_, tmp_set_));
124 it->second.insert(range_);
127 }
else if (strcasecmp(argv[1],
"addTypeOfNode") == 0) {
128 uint8_t node_id_ = atoi(argv[2]);
129 string node_type_ = (
char *) (argv[3]);
130 std::map<uint8_t, string>::iterator it =
133 type_of_node.insert(pair<double, string>(node_id_, node_type_));
135 }
else if (it !=
type_of_node.end() && it->second != node_type_) {
137 type_of_node.insert(pair<double, string>(node_id_, node_type_));
141 "You added the same node with the same type twice.");
144 }
else if (strcasecmp(argv[1],
"addRangeNum") == 0) {
145 string pathtype_ = (
char *) (argv[2]);
146 uint8_t rangenum_ = atoi(argv[3]);
147 std::map<string, uint8_t>::iterator it =
range_nums.find(pathtype_);
149 range_nums.insert(pair<string, uint8_t>(pathtype_, rangenum_));
151 }
else if (it !=
range_nums.end() && it->second != rangenum_) {
153 range_nums.insert(pair<string, uint8_t>(pathtype_, rangenum_));
156 std::fprintf(stderr,
"You added the same range_num already");
167 hdr_cmn *ch = HDR_CMN(p);
168 hdr_MPhy *ph = HDR_MPHY(p);
170 if (ch->direction() == hdr_cmn::UP) {
171 ph->dstSpectralMask = getRxSpectralMask(p);
172 ph->dstPosition = getPosition();
173 ph->dstAntenna = getRxAntenna(p);
175 assert(ph->dstSpectralMask);
176 assert(ph->dstPosition);
178 ph->Pr = getRxPower(p);
181 p->txinfo_.CPThresh = 0;
184 ph->Pn = getNoisePower(p);
191 ph->worth_tracing =
true;
194 PacketEvent *pe =
new PacketEvent(p);
195 Scheduler::instance().schedule(&rxtimer, pe, ph->duration);
211 ph->rxtime = ph->txtime;
213 ph->worth_tracing =
false;
215 ph->srcSpectralMask = getTxSpectralMask(p);
216 ph->srcAntenna = getTxAntenna(p);
217 ph->srcPosition = getPosition();
218 ph->dstSpectralMask = 0;
221 ph->modulationType = getModulationType(p);
222 ph->duration = getTxDuration(p);
224 ph->Pt = getTxPower(p);
226 assert(ph->srcSpectralMask);
227 assert(ph->srcPosition);
228 assert(ph->duration > 0);
234 PacketEvent *pe =
new PacketEvent(p->copy());
235 Scheduler::instance().schedule(&txtimer, pe, ph->duration);
244 hdr_cmn *ch = HDR_CMN(p);
245 hdr_MPhy *ph = HDR_MPHY(p);
252 int nbits = ch->size() * 8;
253 double x = RNG::defaultrng()->uniform_double();
255 bool error_n = x <= per_n;
259 double recv_power = ph->Pr;
260 double duration = ph->duration;
261 double endrx_time = ph->rxtime + duration;
263 double interference_power_ =
265 double interference_start_time_ =
267 if (interference_power_ > 0) {
268 interf_val.first = recv_power / interference_power_;
272 assert(duration > 0);
274 ((endrx_time - interference_start_time_) /
280 x = RNG::defaultrng()->uniform_double();
281 error_ni = x <= per_ni;
283 std::cerr <<
"No interference model provided!" << std::endl;
292 Scheduler::instance().clock();
297 Scheduler::instance().clock() + ph->duration;
300 ch->error() = error_ni || error_n;
303 incrErrorPktsNoise();
304 }
else if (error_ni) {
305 incrErrorPktsInterf();
319 const double &_snr,
const int &_nbits,
const Packet *p)
321 hdr_cmn *ch = HDR_CMN(p);
322 hdr_MPhy *ph = HDR_MPHY(p);
326 std::map<uint8_t, string>::const_iterator it =
329 const string type_prev_ = it->second;
332 const string type_me_ = it->second;
333 string type_ = type_prev_ + type_me_;
334 string type_reversed_ = type_me_ + type_prev_;
337 std::map<string, uint8_t>::const_iterator it_r =
range_nums.find(type_);
338 std::map<string, uint8_t>::const_iterator it_rr =
340 if ((it_r !=
range_nums.end() && it_r->second == 0) ||
341 (it_rr !=
range_nums.end() && it_rr->second == 0)) {
349 if ((type_ ==
"BG") || (type_ ==
"GB") || (type_ ==
"GG")) {
355 if ((type_ ==
"AB") || (type_ ==
"BA")) {
366 const double x_ = (ph->srcPosition)->getX();
367 const double y_ = (ph->srcPosition)->getY();
368 const double z_ = (ph->srcPosition)->getZ();
369 const double x_dst_ = (ph->dstPosition)->getX();
370 const double y_dst_ = (ph->dstPosition)->getY();
371 const double z_dst_ = (ph->dstPosition)->getZ();
372 const double distance_ = sqrt((x_ - x_dst_) * (x_ - x_dst_) +
373 (y_ - y_dst_) * (y_ - y_dst_) + (z_ - z_dst_) * (z_ - z_dst_));
374 const double distance_miles_ = this->
fromKmToMiles(distance_ / 1000);
376 std::map<uint8_t, std::set<double> >::const_iterator it2 =
377 range.find(ch->prev_hop_);
379 if (it2 !=
range.end()) {
412 const string file_name_ =
osstream.str();
436 const string file_name_ =
osstream.str();
449 const std::set<double> &_set,
const double &_value)
453 std::set<double>::iterator it_ = _set.begin();
454 if (_value <= *it_) {
458 if (_value >= *it_) {
461 for (std::set<double>::const_iterator it_ = _set.begin();
464 std::set<double>::const_iterator it_next_ = it_;
466 if (_value >= *it_next_) {
469 if (abs(_value - *it_) <= abs(_value - *it_next_)) {
482 const std::string &_file_name,
const double &_snr)
const
484 std::ifstream input_file_;
489 char *tmp_ =
new char[_file_name.length() + 1];
490 strcpy(tmp_, _file_name.c_str());
492 fprintf(stderr,
"Empty string for the file name");
496 input_file_.open(tmp_);
497 if (input_file_.is_open()) {
498 while (std::getline(input_file_, line_)) {
499 std::istringstream iss_(line_);
501 std::stringstream ss_(token_);
502 double token_double_;
503 ss_ >> token_double_;
504 if (token_double_ == _snr) {
507 std::stringstream ss_(token_);
508 ss_ >> token_double_;
509 return token_double_;
513 cerr <<
"Impossible to open file " << _file_name << endl;
virtual double consumedEnergyRx(const double &_duration)
Compute the energy (in Joule) spent by the modem in reception.
double Energy_Rx_
Energy (in Joule) spent by the node in transmission.
virtual void startRx(Packet *p)
Handles the start of a reception.
double time_ready_to_end_rx_
Used to keep track of the arrival time.
double Rx_Time_
Time (in seconds) spent by the node in reception.
virtual int command(int, const char *const *)
TCL command interpreter.
UnderwaterPhysicaldbClass()
TclObject * create(int, const char *const *)
UnderwaterPhysicaldb()
Constructor of UnderwaterMPhyBpskDb class.
virtual double getPERfromSIR(const double &, const double &)
Packet error rate based on Overlap and SIR.
string modulation
Name of the Modulation.
virtual void recv(Packet *)
recv method.
char token_separator
Token used to parse the elements in a line of the database.
std::pair< double, double > interf_val
(SIR, Overlap)
virtual double getNearestNeighbor(const std::set< double > &, const double &)
Nearest neighbor of a value contained in a set.
virtual double getPERfromSNR(const double &, const int &, const Packet *)
Packet error rate based on SNR.
std::set< double > overlap
Set of the available Overlaps.
uwinterference * interference_
Interference Model.
virtual void endRx(Packet *p)
Handles the end of a packet reception.
uint8_t ipAddr_
IP of the node.
string country
Name of the Country.
std::map< string, uint8_t > range_nums
Set of the number of ranges for a given pathtype, e.g "AA".
virtual int command(int, const char *const *)
TCL command interpreter.
std::set< double > snr
Set of the available SNRs.
char * path_
Name of the trace file writter for the current node.
virtual const double retrievePerFromFile(const std::string &, const double &) const
const double fromKmToMiles(const double &_km) const
Converts from kilometers to miles.
std::map< uint8_t, std::set< double > > range
Set of the available Ranges.
std::map< uint8_t, string > type_of_node
Set of the available type of nodes.
std::set< double > sir
Set of the available SIRs.
virtual void addToInterference(Packet *p)
Add a packet to the interference calculation.
virtual double getInterferencePower(Packet *p)
Compute the average interference power for the given packet.
double getInitialInterferenceTime()
Get the timestamp of the begin of reception of the first interferer packet.
UnderwaterPhysicaldbClass class_UnderwaterPhysicaldb
Definition of UnderwaterPhysicaldb class.