47 : TclClass(
"Module/UW/PHYSICALDB")
59 interference_(nullptr),
68 interf_val({0.0,0.0}),
73 bind_error(
"token_separator_", &token_separator);
75 token_separator =
'\t';
82 if (strcasecmp(argv[1],
"addr") == 0) {
83 ipAddr_ =
static_cast<uint8_t
>(atoi(argv[2]));
85 fprintf(stderr,
"0 is not a valid IP address");
89 }
else if (strcasecmp(argv[1],
"setCountry") == 0) {
92 }
else if (strcasecmp(argv[1],
"setModulation") == 0) {
95 }
else if (strcasecmp(argv[1],
"addSnr") == 0) {
96 snr.insert(
snr.end(), strtod(argv[2], NULL));
98 }
else if (strcasecmp(argv[1],
"addSir") == 0) {
99 sir.insert(
sir.end(), strtod(argv[2], NULL));
101 }
else if (strcasecmp(argv[1],
"addOverlap") == 0) {
104 }
else if (strcasecmp(argv[1],
"setPath") == 0) {
105 path_ = (
char *) (argv[2]);
107 }
else if (strcasecmp(argv[1],
"setInterference") == 0) {
114 }
else if (argc == 4) {
115 if (strcasecmp(argv[1],
"addRange") == 0) {
116 uint8_t node_id_ = atoi(argv[2]);
117 double range_ = atof(argv[3]);
118 std::map<uint8_t, std::set<double> >::iterator it =
119 range.find(node_id_);
120 if (it ==
range.end()) {
121 std::set<double> tmp_set_;
122 tmp_set_.insert(range_);
124 pair<uint8_t, std::set<double> >(node_id_, tmp_set_));
126 it->second.insert(range_);
129 }
else if (strcasecmp(argv[1],
"addTypeOfNode") == 0) {
130 uint8_t node_id_ = atoi(argv[2]);
131 string node_type_ = (
char *) (argv[3]);
132 std::map<uint8_t, string>::iterator it =
135 type_of_node.insert(pair<double, string>(node_id_, node_type_));
137 }
else if (it !=
type_of_node.end() && it->second != node_type_) {
139 type_of_node.insert(pair<double, string>(node_id_, node_type_));
143 "You added the same node with the same type twice.");
146 }
else if (strcasecmp(argv[1],
"addRangeNum") == 0) {
147 string pathtype_ = (
char *) (argv[2]);
148 uint8_t rangenum_ = atoi(argv[3]);
149 std::map<string, uint8_t>::iterator it =
range_nums.find(pathtype_);
151 range_nums.insert(pair<string, uint8_t>(pathtype_, rangenum_));
153 }
else if (it !=
range_nums.end() && it->second != rangenum_) {
155 range_nums.insert(pair<string, uint8_t>(pathtype_, rangenum_));
158 std::fprintf(stderr,
"You added the same range_num already");
169 hdr_cmn *ch = HDR_CMN(p);
170 hdr_MPhy *ph = HDR_MPHY(p);
172 if (ch->direction() == hdr_cmn::UP) {
173 ph->dstSpectralMask = getRxSpectralMask(p);
174 ph->dstPosition = getPosition();
175 ph->dstAntenna = getRxAntenna(p);
177 assert(ph->dstSpectralMask);
178 assert(ph->dstPosition);
180 ph->Pr = getRxPower(p);
183 p->txinfo_.CPThresh = 0;
186 ph->Pn = getNoisePower(p);
193 ph->worth_tracing =
true;
196 PacketEvent *pe =
new PacketEvent(p);
197 Scheduler::instance().schedule(&rxtimer, pe, ph->duration);
213 ph->rxtime = ph->txtime;
215 ph->worth_tracing =
false;
217 ph->srcSpectralMask = getTxSpectralMask(p);
218 ph->srcAntenna = getTxAntenna(p);
219 ph->srcPosition = getPosition();
220 ph->dstSpectralMask = 0;
223 ph->modulationType = getModulationType(p);
224 ph->duration = getTxDuration(p);
226 ph->Pt = getTxPower(p);
228 assert(ph->srcSpectralMask);
229 assert(ph->srcPosition);
230 assert(ph->duration > 0);
236 PacketEvent *pe =
new PacketEvent(p->copy());
237 Scheduler::instance().schedule(&txtimer, pe, ph->duration);
246 hdr_cmn *ch = HDR_CMN(p);
247 hdr_MPhy *ph = HDR_MPHY(p);
254 int nbits = ch->size() * 8;
255 double x = RNG::defaultrng()->uniform_double();
257 bool error_n = x <= per_n;
261 double recv_power = ph->Pr;
262 double duration = ph->duration;
263 double endrx_time = ph->rxtime + duration;
265 double interference_power_ =
267 double interference_start_time_ =
269 if (interference_power_ > 0) {
270 interf_val.first = recv_power / interference_power_;
274 assert(duration > 0);
276 ((endrx_time - interference_start_time_) /
282 x = RNG::defaultrng()->uniform_double();
283 error_ni = x <= per_ni;
285 std::cerr <<
"No interference model provided!" << std::endl;
294 Scheduler::instance().clock();
299 Scheduler::instance().clock() + ph->duration;
302 ch->error() = error_ni || error_n;
305 incrErrorPktsNoise();
306 }
else if (error_ni) {
307 incrErrorPktsInterf();
321 const double &_snr,
const int &_nbits,
const Packet *p)
323 hdr_cmn *ch = HDR_CMN(p);
324 hdr_MPhy *ph = HDR_MPHY(p);
328 std::map<uint8_t, string>::const_iterator it =
331 const string type_prev_ = it->second;
334 const string type_me_ = it->second;
335 string type_ = type_prev_ + type_me_;
336 string type_reversed_ = type_me_ + type_prev_;
339 std::map<string, uint8_t>::const_iterator it_r =
range_nums.find(type_);
340 std::map<string, uint8_t>::const_iterator it_rr =
342 if ((it_r !=
range_nums.end() && it_r->second == 0) ||
343 (it_rr !=
range_nums.end() && it_rr->second == 0)) {
351 if ((type_ ==
"BG") || (type_ ==
"GB") || (type_ ==
"GG")) {
357 if ((type_ ==
"AB") || (type_ ==
"BA")) {
368 const double x_ = (ph->srcPosition)->getX();
369 const double y_ = (ph->srcPosition)->getY();
370 const double z_ = (ph->srcPosition)->getZ();
371 const double x_dst_ = (ph->dstPosition)->getX();
372 const double y_dst_ = (ph->dstPosition)->getY();
373 const double z_dst_ = (ph->dstPosition)->getZ();
374 const double distance_ = sqrt((x_ - x_dst_) * (x_ - x_dst_) +
375 (y_ - y_dst_) * (y_ - y_dst_) + (z_ - z_dst_) * (z_ - z_dst_));
376 const double distance_miles_ = this->
fromKmToMiles(distance_ / 1000);
378 std::map<uint8_t, std::set<double> >::const_iterator it2 =
379 range.find(ch->prev_hop_);
381 if (it2 !=
range.end()) {
414 const string file_name_ =
osstream.str();
438 const string file_name_ =
osstream.str();
451 const std::set<double> &_set,
const double &_value)
455 std::set<double>::iterator it_ = _set.begin();
456 if (_value <= *it_) {
460 if (_value >= *it_) {
463 for (std::set<double>::const_iterator it_ = _set.begin();
466 std::set<double>::const_iterator it_next_ = it_;
468 if (_value >= *it_next_) {
471 if (abs(_value - *it_) <= abs(_value - *it_next_)) {
484 const std::string &_file_name,
const double &_snr)
const
486 std::ifstream input_file_;
491 char *tmp_ =
new char[_file_name.length() + 1];
492 strcpy(tmp_, _file_name.c_str());
494 fprintf(stderr,
"Empty string for the file name");
498 input_file_.open(tmp_);
499 if (input_file_.is_open()) {
500 while (std::getline(input_file_, line_)) {
501 std::istringstream iss_(line_);
503 std::stringstream ss_(token_);
504 double token_double_;
505 ss_ >> token_double_;
506 if (token_double_ == _snr) {
509 std::stringstream ss_(token_);
510 ss_ >> token_double_;
511 return token_double_;
515 cerr <<
"Impossible to open file " << _file_name << endl;
virtual int command(int, const char *const *) override
TCL command interpreter.
double Energy_Rx_
Energy (in Joule) spent by the node in transmission.
virtual void startRx(Packet *p) override
Handles the start of a reception.
double time_ready_to_end_rx_
Used to keep track of the rx time.
virtual double consumedEnergyRx(const double &_duration) const
Compute the energy (in Joule) spent by the modem in reception.
double Rx_Time_
Time (in seconds) spent by the node in reception.
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.