DESERT 3.6.0
Loading...
Searching...
No Matches
uwphysicalfromdb.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 "uwphysicalfromdb.h"
40#include <climits>
41
42static class UnderwaterPhysicalfromdbClass : public TclClass
43{
44public:
46 : TclClass("Module/UW/PHYSICALFROMDB")
47 {
48 }
49 TclObject *
50 create(int, const char *const *)
51 {
52 return (new UnderwaterPhysicalfromdb);
53 }
55
57 : tau_index(1)
58{
59 bind("tau_index_", &tau_index);
60 path_gainmaps = "";
62}
63
64int
65UnderwaterPhysicalfromdb::command(int argc, const char *const *argv)
66{
67 if (argc == 3) {
68 if (strcasecmp(argv[1], "setPathGainmaps") == 0) {
69 string tmp_ = ((char *) argv[2]);
70 path_gainmaps = new char[tmp_.length() + 1];
71 strcpy(path_gainmaps, tmp_.c_str());
72 if (path_ == NULL) {
74 "Empty string for the path_gainmaps_ file name");
75 return TCL_ERROR;
76 }
77 return TCL_OK;
78 } else if (strcasecmp(argv[1], "setPathSelfInterference") == 0) {
79 string tmp_ = ((char *) argv[2]);
80 path_selfinterference = new char[tmp_.length() + 1];
82 if (path_ == NULL) {
84 "Empty string for the path_selfinterference_ file "
85 "name");
86 return TCL_ERROR;
87 }
88 return TCL_OK;
89 }
90 }
92} /* UnderwaterPhysicalfromdb::command */
93
94double
95UnderwaterPhysicalfromdb::getPER(double _snr, int _nbits, Packet *p)
96{
97 hdr_MPhy *ph = HDR_MPHY(p);
98
99 double x_ = (ph->srcPosition)->getX();
100 double y_ = (ph->srcPosition)->getY();
101 double z_ = (ph->srcPosition)->getZ();
102 double x_dst_ = (ph->dstPosition)->getX();
103 double y_dst_ = (ph->dstPosition)->getY();
104 double z_dst_ = (ph->dstPosition)->getZ();
105 double depth_src_ = z_;
106 double depth_dst_ = z_dst_;
107 double dist_dst_ =
108 sqrt((x_ - x_dst_) * (x_ - x_dst_) + (y_ - y_dst_) * (y_ - y_dst_));
109
110 double gain_ =
112 double self_interference_ = pow(10,
114 // gain_ = gain_ * pow(frequency_correction_factor_, - dist_dst_);
115
116 double snir_;
117 if ((ph->Pn + ph->Pi + self_interference_) != 0) {
118 ph->Pi += self_interference_; // Add the self interference to the
119 // interference.
120 snir_ = (ph->Pt * gain_) / (ph->Pn + ph->Pi);
121 } else {
122 snir_ = -INT_MAX;
123 }
125}
126
127double
129 const double &_source_depth, const double &_destination_depth,
130 const double &_destination_distance)
131{
132 assert(_time >= 0);
133 assert(_source_depth <= 0);
136
137 // Quantize the input values
138 int time_filename_ = static_cast<int>(
139 static_cast<int>(floor(_time) / getTimeRoughness()) *
141#if __cplusplus > 199711L
142 // C++11
143 // The column index corresponds to the depth of the receiver
145 static_cast<int>(round((_source_depth * -1) / getDepthRoughness()) *
147 // The line index is relative to the distance of the receiver
148 int line_index_ = static_cast<int>(
150 // The column index corresponds to the depth of the receiver
151 int column_index_ = static_cast<int>(
153#else
154 // Adding 0.5 and cast is equivalent to use round only because we are
155 // dealing with positive integers
157 static_cast<int>((_source_depth * -1) / getDepthRoughness() + 0.5) *
159 int line_index_ = static_cast<int>(
161 int column_index_ = static_cast<int>(
162 (_destination_depth * -1) / getDepthRoughness() + 0.5);
163#endif
164
165 if (source_depth_filename_ == 0) {
167 }
168
172 getTauIndex());
173
175} /* UnderwaterPhysicalfromdb::getGain */
176
177double
179 const double &_source_depth, const double &_destination_depth,
180 const double &_destination_distance)
181{
182 assert(_time >= 0);
183 assert(_source_depth <= 0);
186
187 // Quantize the input values.
188 int time_filename_ = static_cast<int>(
189 static_cast<int>(ceil(_time) / getTimeRoughness()) *
192 static_cast<int>((_source_depth * -1) / getDepthRoughness()) *
194 // The line index is relative to the distance of the receiver
195 int column_index_ =
196 static_cast<int>((_destination_depth * -1) / getDepthRoughness());
197 // The column index corresponds to the depth of the receiver
198 int line_index_ =
199 static_cast<int>(_destination_distance / getDistanceRoughness());
200
201 if (time_filename_ == 0) {
203 }
204
205 if (source_depth_filename_ == 0) {
207 }
208
212 getTauIndex());
213
215} /* UnderwaterPhysicalfromdb::getSelfInterference */
216
217double
219 const int &_row_index, const int &_column_index) const
220{
221 int row_iterator_ = 0;
222 int column_iterator_ = 0;
225 string line_;
226 string token_;
227 string value_;
228 double return_value_;
229
230 char *tmp_ = new char[_file_name.length() + 1];
231 strcpy(tmp_, _file_name.c_str());
232 if (tmp_ == NULL) {
233 std::cerr << "Impossible to open file " << _file_name << std::endl;
234 fprintf(stderr, "Empty string for the file name");
235 }
236
237 input_file_.open(tmp_);
238 if (input_file_.is_open()) {
239 while (std::getline(input_file_, line_)) {
241 if (row_iterator_ == _row_index) {
242 break;
243 }
244 }
245 } else {
246 std::cerr << "Impossible to open file " << _file_name << std::endl;
247 }
248
253 value_ = token_;
254 }
255 }
256
257 stm.str(value_);
259 // std::cout << "file:" << _file_name << ":column:" << _column_index <<
260 // ":row:" << _row_index << ":gain:" << return_value_ << std::endl;
261
262 delete[] tmp_;
263 if (this->isZero(return_value_)) {
264 return (-INT_MAX);
265 } else {
266 return return_value_;
267 }
268} /* UnderwaterPhysicalfromdb::retriveFromFile */
269
270string
271UnderwaterPhysicalfromdb::createNameFile(const char *_path, const int &_time,
272 const int &_source_depth, const int &_tau_index)
273{
274 osstream_.clear();
275 osstream_.str("");
276 osstream_ << _path << "/" << _time << "_" << _source_depth << "_"
277 << _tau_index;
278 return osstream_.str();
279} /* UnderwaterPhysicalfromdb::createNameFile */
const bool isZero(const double &value) const
Evaluates is the number passed as input is equal to zero.
const int & getDepthRoughness() const
Returns the depth_roughness_ parameter.
char * path_
Name of the trace file writter for the current node.
const int & getDistanceRoughness() const
Returns the distance_roughness_ parameter.
const int & getTimeRoughness() const
Returns the time_roughness_ parameter.
virtual int command(int, const char *const *)
TCL command interpreter.
ostringstream osstream_
Used to create strings.
const int & getTotalTime() const
Returns the total_time_ parameter.
char token_separator_
Token used to parse the elements in a line of the database.
virtual double getPER(double snr, int nbits, Packet *_p)
Returns the packet error rate by using the length of a packet and the information contained in the pa...
TclObject * create(int, const char *const *)
char * path_selfinterference
Name of the trace file writter for the current node.
char * path_gainmaps
Name of the trace file writter for the current node.
virtual double getSelfInterference(const double &_time, const double &_source_depth, const double &_destination_depth, const double &_destination_distance)
Read from the self interference.
virtual double retrieveFromFile(const string &_file_name, const int &_row_index, const int &_column_index) const
Read from a file the value in a specific row - column.
UnderwaterPhysicalfromdb()
Constructor of UnderwaterMPhyBpskDb class.
virtual string createNameFile(const char *_path, const int &_time, const int &_source_depth, const int &_tau_index)
Create the name of the file to read.
virtual double getPER(double _snr, int _nbits, Packet *p)
Return the Packet Error Rate of the packet p passed as input.
const int & getTauIndex() const
Return the value of the tau_index parameter.
int tau_index
Tau index to load in the file.
virtual double getGain(const double &_time, const double &_source_depth, const double &_destination_depth, const double &_destination_distance)
Read from the gain.
virtual int command(int, const char *const *)
TCL command interpreter.
UnderwaterPhysicalfromdbClass class_UnderwaterPhysicalfromdb
Definition of UnderwaterPhysicalfromdb class.