Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | |
2 /* | |
3 * Copyright (c) 2008 University of Washington | |
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 as published by | |
7 * the Free Software Foundation, either version 3 of the License, or | |
8 * (at your option) any later version. | |
Andrey Mazo
2009/11/15 13:32:11
ns-3 is licensed under GPLv2.
| |
9 * | |
10 * This program is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 * GNU General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 * | |
18 * Author: Leonard Tracy <lentracy@u.washington.edu> | |
19 * | |
20 * | |
21 */ | |
22 | |
23 | |
craigdo1
2009/07/11 00:10:52
This file is in a directory under examples. I thi
faker.moatamri
2009/10/21 10:24:09
Agree with Craig
| |
24 //TODO: Implement pertubations and averaging. | |
25 //NOTE: ^^^ THIS HAS NOT YET BEEN DONE | |
faker.moatamri
2009/10/21 10:24:09
So what does this file do? any Doxygen
| |
26 | |
27 #include "ns3/common-module.h" | |
28 #include "ns3/core-module.h" | |
29 #include "ns3/uan-module.h" | |
30 //#include "ns3/assert.h" | |
31 | |
32 //#include "ns3/log.h" | |
faker.moatamri
2009/10/21 10:24:09
Please remove ALL commented code in all files!
| |
33 | |
34 //#define USE_SQL_DAT | |
35 | |
36 #include <iostream> | |
37 #include <fstream> | |
38 #include <algorithm> | |
39 #include <cctype> | |
40 #include <string> | |
41 #include <sstream> | |
42 #include <set> | |
43 #include <vector> | |
44 #include <iomanip> | |
45 #ifdef USE_SQL_DAT | |
46 #include <sqlite3.h> | |
47 #endif | |
48 | |
49 using namespace ns3; | |
50 using namespace std; | |
51 | |
52 | |
53 | |
54 | |
55 NS_LOG_COMPONENT_DEFINE("BhCreateDat2"); | |
56 | |
57 #ifdef USE_SQL_DAT | |
58 static int | |
59 sqlCallback(void *vd, int32_t argc, char **argv, char **cname) | |
60 { | |
61 for(int32_t i=0;i<argc;i++) | |
62 { | |
63 std::cout << "Column: " << cname[i] << " is " << argv[i] << std::endl; | |
64 } | |
65 return 0; | |
66 } | |
67 #endif //USE_SQL_DAT | |
68 | |
69 //bool | |
70 //operator < (std::pair<double, complex<double> > a, std::pair<double, complex<d ouble> > b) | |
71 //{ | |
72 // return a.first < b.first; | |
73 //} | |
74 | |
75 struct TapCmp | |
76 { | |
77 bool operator () (pair<double, complex<double> > a, pair<double, complex<doubl e> > b) | |
78 { | |
79 return a.first < b.first; | |
80 } | |
81 }; | |
82 | |
83 struct RunResult | |
84 { | |
85 BhIndex index; | |
86 double pathLossDb; | |
87 vector<double> amps; | |
88 double propDelay; | |
89 }; | |
90 | |
91 class DatMaker : public BhConfig | |
faker.moatamri
2009/10/21 10:24:09
Can you please create a header file and put all of
| |
92 { | |
93 | |
94 public: | |
95 void PrintHelp(void); | |
faker.moatamri
2009/10/21 10:24:09
Coding style for all files, please review the codi
| |
96 void MakeDatFiles(void); | |
97 virtual bool Config(const char *cfgFile); | |
98 | |
99 private: | |
100 typedef std::list<pair<double, double> > SspList; | |
101 typedef std::set<pair<double, complex<double> >, TapCmp > TapPairSet; | |
102 | |
103 //pair < delay in seconds, amplitude > | |
104 //typedef std::vector<pair<double, complex<double> > > TapVec; | |
105 string m_bhPath; | |
106 uint32_t m_nmedia; | |
107 string m_ssOpt; | |
108 uint32_t m_nmesh; | |
109 uint32_t m_topSigma; | |
110 double m_zdepth; | |
111 | |
112 SspList m_sspVals; | |
113 string m_botSigma; | |
114 double m_botRough; | |
115 | |
116 string m_botArgs; | |
117 string m_runType; | |
118 | |
119 uint32_t m_numBeams; | |
120 double m_minBeamAngle; | |
121 double m_maxBeamAngle; | |
122 | |
123 double m_step; | |
124 double m_zbox; | |
125 double m_rbox; | |
126 | |
127 double m_perturbMean; | |
128 double m_perturbVariance; | |
129 uint32_t m_numAverages; | |
130 | |
131 uint32_t m_preTaps; | |
132 uint32_t m_totalTaps; | |
133 double m_resolution; | |
134 | |
135 void WriteEnvFile(string filename, uint32_t freqHz); | |
136 void WriteEnvFile(string filename, uint32_t freqHz, SspList ssp); | |
137 void ReadArrivals(list<RunResult> &results); | |
138 set<string> FillParams(void); | |
139 | |
140 #ifdef USE_SQL_DAT | |
141 void WriteResults(list<BellhopResp> &results, sqlite3 *sqldb); | |
142 sqlite3 *CreateDb(string dbfile); | |
143 #else | |
144 void WriteResults(list<BellhopResp> &results); | |
145 #endif | |
146 //list<TapVec> SumTapVecList(list<TapVec> &a, list<TapVec> &b); | |
147 vector<double> ProcessTaps (TapPairSet pairs); | |
148 SspList PerturbSsp(void); | |
149 | |
150 void DoAverage (list<BellhopResp> &out, vector<list<RunResult> > runs); | |
151 | |
152 }; | |
153 | |
154 void | |
155 DatMaker::PrintHelp(void) | |
156 { | |
157 cout << "Usage: create-dat <CFG_FILE>" << endl; | |
158 cout << "\tCFG_FILE=Configuration file describing frequency, depth, and ranges to include" << endl; | |
159 cout << "See documentation for details" << endl << endl; | |
160 } | |
161 | |
162 void | |
163 DatMaker::WriteEnvFile(string filename, uint32_t freqHz) | |
164 { | |
165 WriteEnvFile(filename, freqHz, m_sspVals); | |
166 } | |
167 void | |
168 DatMaker::WriteEnvFile(string filename, uint32_t freqHz, SspList ssp) | |
169 { | |
170 | |
171 ofstream env(filename.c_str()); | |
172 | |
173 env << "'CreateDatEnv'" << endl; | |
174 env << freqHz << endl << m_nmedia << endl << m_ssOpt << endl; | |
175 env << m_nmesh << " " << m_topSigma << " " << m_zdepth << endl; | |
176 | |
177 list<pair<double, double> >::iterator it = ssp.begin(); | |
178 | |
179 for(;it != ssp.end(); it++) | |
180 { | |
181 env << (*it).first << " " << (*it).second << " /" << endl; | |
182 } | |
183 | |
184 env << m_botSigma << " " << m_botRough << endl; | |
185 env << m_botArgs << endl; | |
186 | |
187 //Source depths" | |
188 env << m_numSrcDepth << endl; | |
189 for(uint32_t i=0;i<m_numSrcDepth;i++) | |
190 { | |
191 env << m_minSrcDepthM + m_srcDepthStepM*i << " "; | |
192 } | |
193 env << endl; | |
194 //Rx depths | |
195 env << m_numRecDepth << endl; | |
196 for(uint32_t i=0;i<m_numRecDepth;i++) | |
197 { | |
198 env << m_minRecDepthM + m_recDepthStepM*i << " "; | |
199 } | |
200 env << endl; | |
201 | |
202 //Ranges | |
203 env << m_numRanges << endl; | |
204 for(uint32_t i=0;i<m_numRanges;i++) | |
205 { | |
206 env << m_minRangeKm + i*m_rangeStepKm << " "; | |
207 } | |
208 env << endl; | |
209 | |
210 env << m_runType << endl; | |
211 env << m_numBeams << endl << m_minBeamAngle << " " << m_maxBeamAngle << " /" << endl; | |
212 env << m_step << " " << m_zbox << " " << m_rbox << endl; | |
213 env << endl; | |
214 env.close(); | |
215 } | |
216 | |
217 set<string> | |
218 DatMaker::FillParams(void) | |
219 { | |
220 | |
221 set<string> params; | |
222 | |
223 params.insert("bellhoppath"); | |
224 params.insert("SSP"); | |
225 params.insert("nmedia"); | |
226 params.insert("ssopt"); | |
227 params.insert("nmesh"); | |
228 params.insert("topsigma"); | |
229 params.insert("sspbottomdepth"); | |
230 params.insert("bottomsigma"); | |
231 params.insert("bottomroughness"); | |
232 params.insert("runtype"); | |
233 params.insert("numbeams"); | |
234 params.insert("minbeamangle"); | |
235 params.insert("maxbeamangle"); | |
236 params.insert("step"); | |
237 params.insert("raybottomdepth"); | |
238 params.insert("raymaxrange"); | |
239 params.insert("perturbmean"); | |
240 params.insert("perturbvariance"); | |
241 params.insert("numavgs"); | |
242 params.insert("tapsbeforemax"); | |
243 params.insert("totaltaps"); | |
244 params.insert("bottomargument"); | |
245 params.insert("resolution"); | |
246 | |
247 return params; | |
248 } | |
249 | |
250 | |
251 bool | |
252 DatMaker::Config(const char *cfgFileName) | |
253 { | |
254 | |
255 ifstream inf(cfgFileName); | |
256 ostringstream buf; | |
257 int (*tl)(int) = tolower; | |
258 | |
259 if(!BhConfig::Config(cfgFileName)) | |
260 { | |
261 NS_FATAL_ERROR("Bad configuration file: Missing basic params"); | |
262 return false; | |
263 } | |
264 | |
265 | |
266 set<string> params = FillParams(); | |
267 | |
268 | |
269 if(!inf.good()) | |
270 { | |
271 return false; //Bad environment file | |
272 } | |
273 m_configFile = string(cfgFileName); | |
274 | |
275 string line; | |
276 uint32_t lineNo=0; | |
277 | |
278 while(getline(inf, line)) | |
279 { | |
280 lineNo++; | |
281 trim(line); | |
282 //NS_LOG_DEBUG("Line " << lineNo << ": " << line); | |
283 if(line == "SSP:") | |
284 { | |
285 params.erase("SSP"); | |
286 buf.str(""); | |
287 while(1) | |
288 { | |
289 | |
290 if(!getline(inf, line)) | |
291 { | |
292 NS_FATAL_ERROR("Found no ENDSSP: after SSP: in config file " < < m_configFile); | |
293 } | |
294 if(line == "ENDSSP:") | |
295 { | |
296 break; | |
297 } | |
298 NS_LOG_DEBUG(line); | |
299 buf << line << endl; | |
300 string::size_type comPos = line.find_first_of(","); | |
301 if(comPos == string::npos) | |
302 { | |
303 NS_FATAL_ERROR("Invalid SSP entry: " << line); | |
304 } | |
305 string firstArg = trim(line.substr(0,comPos)); | |
306 string scdArg = trim(line.substr(comPos+1, line.length() - comPos + 1)); | |
307 m_sspVals.push_back(make_pair(atof(firstArg.c_str()), atof(scdArg. c_str()))); | |
308 } | |
309 } | |
310 else | |
311 { | |
312 if(line.empty() || line.at(0) == '#') //comment | |
313 continue; | |
314 | |
315 std::string::size_type colonPos = line.find_first_of(':'); | |
316 if(colonPos == string::npos) | |
317 { | |
318 NS_LOG_WARN("WARNING: Unrecognized configuration line at line numb er " << lineNo << ": " << line); | |
319 continue; | |
320 } | |
321 | |
322 | |
323 string configParam = trim(line.substr(0, colonPos)); | |
324 transform(configParam.begin(), configParam.end(), configParam.begin(), tl); | |
325 | |
326 | |
327 string::size_type endPos = line.find_first_of("# ", colonPos+1); | |
328 string::size_type valSize = (endPos == string::npos)?(line.length() - colonPos + 1):(colonPos - endPos - 1); | |
329 string configValue = trim(line.substr(colonPos+1, valSize)); | |
330 | |
331 params.erase(configParam); | |
332 if(configParam == "bellhoppath") | |
333 { | |
334 m_bhPath = configValue; | |
335 NS_LOG_DEBUG("Set BH path: " << m_bhPath); | |
336 } | |
337 else if(configParam == "nmedia") | |
338 { | |
339 m_nmedia = atoi(configValue.c_str()); | |
340 NS_LOG_DEBUG("Set NMedia: " << m_nmedia); | |
341 } | |
342 else if(configParam == "ssopt") | |
343 { | |
344 m_ssOpt = configValue; | |
345 NS_LOG_DEBUG("Set SSOPT: " << m_ssOpt); | |
346 } | |
347 else if(configParam == "nmesh") | |
348 { | |
349 m_nmesh = atoi(configValue.c_str()); | |
350 NS_LOG_DEBUG("Set NMesh:" << m_nmesh); | |
351 } | |
352 else if(configParam == "topsigma") | |
353 { | |
354 m_topSigma = atoi(configValue.c_str()); | |
355 NS_LOG_DEBUG("Set top SIGMA: " << m_topSigma); | |
356 } | |
357 else if(configParam == "sspbottomdepth") | |
358 { | |
359 m_zdepth = atof(configValue.c_str()); | |
360 NS_LOG_DEBUG("Set SSP bottom depth: " << m_zdepth << " m"); | |
361 } | |
362 else if(configParam == "bottomsigma") | |
363 { | |
364 m_botSigma = configValue; | |
365 NS_LOG_DEBUG("Set bottom sigma: " << m_botSigma); | |
366 } | |
367 else if(configParam == "bottomroughness") | |
368 { | |
369 m_botRough = atof(configValue.c_str()); | |
370 NS_LOG_DEBUG("Set bottom roughness: " << m_botRough); | |
371 } | |
372 else if(configParam == "bottomargument") | |
373 { | |
374 m_botArgs = configValue; | |
375 NS_LOG_DEBUG("Set bottom arguments"); | |
376 } | |
377 else if(configParam == "runtype") | |
378 { | |
379 m_runType = configValue; | |
380 NS_LOG_DEBUG("Set run type: " << m_runType); | |
381 } | |
382 else if(configParam == "numbeams") | |
383 { | |
384 m_numBeams = atoi(configValue.c_str()); | |
385 NS_LOG_DEBUG("Set number of beams: " << m_numBeams); | |
386 } | |
387 else if(configParam == "minbeamangle") | |
388 { | |
389 m_minBeamAngle = atof(configValue.c_str()); | |
390 NS_LOG_DEBUG("Set minimum beam angle: " << m_minBeamAngle << " deg rees"); | |
391 } | |
392 else if(configParam == "maxbeamangle") | |
393 { | |
394 m_maxBeamAngle = atof(configValue.c_str()); | |
395 NS_LOG_DEBUG("Set maximum beam angle: " << m_maxBeamAngle << " deg rees"); | |
396 } | |
397 else if(configParam == "step") | |
398 { | |
399 m_step = atof(configValue.c_str()); | |
400 NS_LOG_DEBUG("Set step: " << m_step << " m"); | |
401 } | |
402 else if(configParam == "raybottomdepth") | |
403 { | |
404 m_zbox = atof(configValue.c_str()); | |
405 NS_LOG_DEBUG("Set ray bottom depth: " << m_zbox << "m"); | |
406 } | |
407 else if(configParam == "raymaxrange") | |
408 { | |
409 m_rbox = atof(configValue.c_str()); | |
410 NS_LOG_DEBUG("Set max range to trace a ray: " << m_rbox << " km"); | |
411 } | |
412 else if(configParam == "perturbmean") | |
413 { | |
414 m_perturbMean = atof(configValue.c_str()); | |
415 NS_LOG_DEBUG("Set mean for SSP petrubations: " << m_perturbMean); | |
416 } | |
417 else if(configParam == "perturbvariance") | |
418 { | |
419 m_perturbVariance = atof(configValue.c_str()); | |
420 NS_LOG_DEBUG("Set variance for SSP pertubations: " << m_perturbVar iance); | |
421 } | |
422 else if(configParam == "numavgs") | |
423 { | |
424 m_numAverages = atoi(configValue.c_str()); | |
425 NS_LOG_DEBUG("Set number of averages: " << m_numAverages); | |
426 } | |
427 else if(configParam == "tapsbeforemax") | |
428 { | |
429 m_preTaps = atoi(configValue.c_str()); | |
430 NS_LOG_DEBUG("Set taps before max to keep: " << m_preTaps << " tap s"); | |
431 } | |
432 else if(configParam == "totaltaps") | |
433 { | |
434 m_totalTaps = atoi(configValue.c_str()); | |
435 NS_LOG_DEBUG("Set total taps: " << m_totalTaps << " taps"); | |
436 } | |
437 else if(configParam == "resolution") | |
438 { | |
439 m_resolution = atof(configValue.c_str()); | |
440 } | |
441 else | |
442 continue; | |
443 | |
444 params.erase(configParam); | |
445 | |
446 } | |
447 } | |
448 | |
449 if(!params.empty()) | |
450 { | |
451 NS_LOG_WARN("Config file " << m_configFile << " is incomplete"); | |
452 NS_LOG_WARN("Missing parameters:"); | |
453 set<string>::iterator it = params.begin(); | |
454 for(; it != params.end(); it++) | |
455 { | |
456 NS_LOG_WARN(*it); | |
457 } | |
458 NS_FATAL_ERROR("Missing parameters"); | |
459 return false; | |
460 } | |
461 return true; | |
462 } | |
463 int main(int argc, char *argv[]) | |
464 { | |
465 LogComponentEnable("BhCreateDat2", LOG_LEVEL_ALL); | |
466 LogComponentEnable("UanPropBh", LOG_LEVEL_ALL); | |
467 DatMaker bhc; | |
468 | |
469 if(argc != 2) | |
470 { | |
471 bhc.PrintHelp(); | |
472 return 1; | |
473 } | |
474 | |
475 bhc.Config(argv[1]); | |
476 //bhc.WriteEnvFile("TestEnv.env"); | |
477 //list<BellhopResp> results; | |
478 //bhc.ReadArrivals(results); | |
479 bhc.MakeDatFiles(); | |
480 | |
481 } | |
482 | |
483 //list<DatMaker::TapVec> | |
484 //DatMaker::SumTapVecList(list<TapVec> &a, list<TapVec> &b) | |
485 //{ | |
486 // NS_ASSERT(a.size() == b.size()); | |
487 // list<TapVec> out; | |
488 // | |
489 // list<TapVec>::iterator ait = a.begin(); | |
490 // list<TapVec>::iterator bit = b.begin(); | |
491 // for(;ait != a.end();ait++, bit++) | |
492 // { | |
493 // NS_ASSERT(ait->size() == bit->size()); | |
494 // TapVec outvec(ait->size()); | |
495 // for(uint32_t i=0;i < ait->size();i++) | |
496 // { | |
497 // TapVec &av = *ait; | |
498 // TapVec &bv = *bit; | |
499 // outvec[i] = make_pair(av[i].first, abs(av[i].second) + abs(bv[i].sec ond)); | |
500 // } | |
501 // out.push_back(outvec); | |
502 // } | |
503 // return out; | |
504 //} | |
505 | |
506 DatMaker::SspList | |
507 DatMaker::PerturbSsp(void) | |
508 { | |
509 NormalVariable nrv(m_perturbMean, m_perturbVariance); | |
510 NS_LOG_DEBUG("Created RV with mean " << m_perturbMean << " and variance " << m _perturbVariance); | |
511 SspList outlist; | |
512 for(SspList::iterator it = m_sspVals.begin();it != m_sspVals.end();it++) | |
513 { | |
514 double n; | |
515 do | |
516 { | |
517 n = nrv.GetValue(); | |
518 } while(n != n); | |
519 | |
520 double newValue = it->second + n; | |
521 NS_LOG_DEBUG("Perturbed SSP Pair: " << it->first << ", " << newValue << " From " << it->first << ", " << it->second << " (nrv = " << n << ")"); | |
522 | |
523 outlist.push_back(make_pair(it->first, newValue)); | |
524 } | |
525 return outlist; | |
526 } | |
527 | |
528 | |
529 | |
530 void | |
531 DatMaker::MakeDatFiles(void) | |
532 { | |
533 string envFile("make-dat-envfile.env"); | |
534 ostringstream bhcommand; | |
535 bhcommand << m_bhPath << " < " << envFile << " > /dev/null"; | |
536 | |
537 #ifdef USE_SQL_DAT | |
538 sqlite3 *sqldb = CreateDb("sqldbtest.db"); | |
539 #endif | |
540 | |
541 for(uint32_t fr = 0; fr < m_numFreqs; fr++) | |
542 { | |
543 | |
544 vector<list<RunResult> > runs(m_numAverages); | |
545 | |
546 for(uint32_t avg = 0; avg < m_numAverages;avg++) | |
547 { | |
548 //Do run | |
549 if(m_numAverages > 1) | |
550 WriteEnvFile(envFile, m_minFreqHz + m_freqStepHz * fr, PerturbSsp()) ; | |
551 else | |
552 WriteEnvFile(envFile, m_minFreqHz + m_freqStepHz * fr); | |
553 int32_t result = system(bhcommand.str().c_str()); | |
554 if(result) | |
555 { | |
556 NS_FATAL_ERROR("Bellhop ended with non-zero error!"); | |
557 } | |
558 | |
559 ReadArrivals(runs[avg]); | |
560 | |
561 } | |
562 list<BellhopResp> results; | |
563 DoAverage(results, runs); | |
564 NS_LOG_DEBUG("Size of results: " << results.size()); | |
565 #ifdef USE_SQL_DAT | |
566 WriteResults(results, sqldb); | |
567 #else | |
568 WriteResults(results); | |
569 #endif | |
570 } | |
571 | |
572 } | |
573 | |
574 void | |
575 DatMaker::DoAverage (list<BellhopResp> &out, vector<list<RunResult> > runs) | |
576 { | |
577 | |
578 uint32_t numRuns = runs[0].size(); | |
579 vector<list<RunResult>::iterator> its(numRuns); | |
580 for(uint32_t i=0;i<m_numAverages;i++) | |
581 { | |
582 NS_ASSERT_MSG(runs[i].size() == numRuns, "In do Average: Lengths of runs unequal!"); | |
583 its[i] = runs[i].begin(); | |
584 for(uint32_t j=0;j<numRuns;j++) | |
585 { | |
586 NS_ASSERT_MSG(its[i]->amps.size() == m_totalTaps, "Not " << m_totalTap s << " taps in run " << j << " of run set (average) " << i); | |
587 its[i]++; | |
588 } | |
589 | |
590 its[i] = runs[i].begin(); | |
591 | |
592 } | |
593 vector<vector<double> > finals(numRuns, vector<double>(m_totalTaps)); | |
594 | |
595 for(uint32_t i=0; i< numRuns;i++) | |
596 { | |
597 //finals[i] = vector<double>(m_totalTaps, 0); | |
598 for(uint32_t j=0;j<m_numAverages;j++) | |
599 { | |
600 for(uint32_t k=0;k<m_totalTaps;k++) | |
601 { | |
602 double current = finals[i][k]; | |
603 double next = its[j]->amps[k]; | |
604 next /= m_numAverages; | |
605 finals[i][k] = current + next; | |
606 if(k==5) | |
607 NS_LOG_DEBUG("finals[" << i << "][5] == " << finals[i][5] << " --- current=" << current << " and next=" << next); | |
608 } | |
609 its[j]++; | |
610 } | |
611 } | |
612 //Calculate pathlosses | |
613 vector<double> pLoss; | |
614 for(uint32_t i=0;i<numRuns;i++) | |
615 { | |
616 double sum=0; | |
617 for(uint32_t j=0;j<m_totalTaps;j++) | |
618 { | |
619 sum += finals[i][j]; | |
620 } | |
621 pLoss.push_back(20*log10(sum)); | |
622 NS_LOG_DEBUG("Calculated pathloss: " << sum); | |
623 //Normalize PDP | |
624 for(uint32_t j=0;j<m_totalTaps;j++) | |
625 { | |
626 finals[i][j] = finals[i][j] / sum; | |
627 } | |
628 } | |
629 out.clear(); | |
630 list<RunResult>::iterator fit = runs[0].begin(); | |
631 for(uint32_t i=0;i<numRuns;i++, fit++) | |
632 { | |
633 //NS_LOG_DEBUG("Creating final index: " << fit->index.m_srcDepthM << ", " << fit->index.m_recDepthM << ", " << fit->index.m_rangeM << ", " << fit->index.m _freqHz << " With pLoss=" << pLoss[i]); | |
634 BellhopResp resp(fit->index, UanPdp(finals[i], Seconds(m_resolution)), pLo ss[i]); | |
635 out.push_back(resp); | |
636 } | |
637 } | |
638 | |
639 | |
640 vector<double> | |
641 DatMaker::ProcessTaps (TapPairSet pairs) | |
642 { | |
643 | |
644 | |
645 vector<double> tempTap; | |
646 TapPairSet::iterator cit = pairs.begin(); | |
647 for(;cit != pairs.end();cit++) | |
648 { | |
649 double runTime = 0; | |
650 complex<double> tapSum = 0; | |
651 while(cit != pairs.end() && runTime < m_resolution) | |
652 { | |
653 runTime += cit->first; | |
654 tapSum += cit->second; | |
655 } | |
656 tempTap.push_back(abs(tapSum)); | |
657 } | |
658 | |
659 vector<double> out(m_totalTaps); | |
660 | |
661 //Find max index | |
662 vector<double>::iterator mit = max_element(tempTap.begin(), tempTap.end()); | |
663 vector<double>::iterator it = mit; | |
664 it--; | |
665 bool atbegin = false; | |
666 for(uint32_t i=m_preTaps;i>0;i--) | |
667 { | |
668 if(atbegin) | |
669 { | |
670 out[i-1] = 0; | |
671 } | |
672 else | |
673 { | |
674 out[i-1] = *it; | |
675 if(it == tempTap.begin()) | |
676 atbegin = true; | |
677 it--; | |
678 } | |
679 } | |
680 for(uint32_t i=m_preTaps;i<m_totalTaps;i++) | |
681 { | |
682 if(mit != tempTap.end()) | |
683 { | |
684 out[i] = *mit; | |
685 mit++; | |
686 } | |
687 else | |
688 { | |
689 out[i] = 0; | |
690 } | |
691 } | |
692 return out; | |
693 } | |
694 | |
695 void | |
696 DatMaker::ReadArrivals(list<RunResult> &results) | |
697 { | |
698 ifstream inf("ARRFIL"); | |
699 NS_ASSERT(inf.is_open()); | |
700 | |
701 //NS_ASSERT_MSG(results.size() == 0, "DatMaker::ReadArrivals expects empty lis t to fill with results. Got a populated list"); | |
702 results.clear(); | |
703 | |
704 uint32_t numSrcDepths, numRecDepths, numRanges, numArrivals; | |
705 double freq; | |
706 | |
707 inf >> freq >> numSrcDepths >> numRecDepths >> numRanges;// >> sd2; | |
708 //inf >> numArrivals; | |
709 NS_LOG_DEBUG("Freq: " << freq << endl | |
710 << "Number src depths: " << numSrcDepths << endl | |
711 << "Number rec depths: " << numRecDepths << endl | |
712 << "Number ranges: " << numRanges << endl); | |
713 // << "sd2?: " << sd2); | |
714 | |
715 vector<uint32_t> srcDepths, recDepths; | |
716 vector<double> ranges; | |
717 double depthBuf; | |
718 double rangeBuf; | |
719 | |
720 for(uint32_t i=0;i<numSrcDepths;i++) | |
721 { | |
722 inf >> depthBuf; | |
723 srcDepths.push_back((uint32_t) depthBuf); | |
724 NS_LOG_DEBUG("Src depth: " << depthBuf); | |
725 } | |
726 for(uint32_t i=0;i<numRecDepths;i++) | |
727 { | |
728 inf >> depthBuf; | |
729 recDepths.push_back((uint32_t) depthBuf); | |
730 NS_LOG_DEBUG("Rec Depth: " << depthBuf); | |
731 } | |
732 for(uint32_t i=0;i<numRanges;i++) | |
733 { | |
734 inf >> rangeBuf; | |
735 ranges.push_back(rangeBuf); | |
736 NS_LOG_DEBUG("Range: " << rangeBuf); | |
737 } | |
738 | |
739 vector<uint32_t>::iterator srcDepIt, recDepIt; | |
740 vector<double>::iterator rangeIt; | |
741 | |
742 | |
743 for(srcDepIt = srcDepths.begin();srcDepIt != srcDepths.end();srcDepIt++) | |
744 { | |
745 inf >> numArrivals; | |
746 NS_LOG_DEBUG("Max arrivals for src depth " << *srcDepIt << " = " << numAr rivals); | |
747 for(recDepIt=recDepths.begin();recDepIt != recDepths.end();recDepIt++) | |
748 for(rangeIt=ranges.begin();rangeIt != ranges.end(); rangeIt++) | |
749 { | |
750 | |
751 inf >> numArrivals; | |
752 //NS_LOG_DEBUG("Looking for " << numArrivals << " arrivals at range " << *rangeIt << " and frequency " << freq << " (NOTE: These are converted in i ntegers in next line)"); | |
753 | |
754 BhIndex newIndex((uint32_t) freq, *srcDepIt, *recDepIt, (uint32_t) * rangeIt); | |
755 | |
756 double aMag, aArg, aDelay, aSrcAngle, aRecAngle; | |
757 uint32_t aTopBounces, aBotBounces; | |
758 double pathLoss = 0; | |
759 double pDelay = 1e30; | |
760 TapPairSet tapPairs; | |
761 for(uint32_t i=0;i<numArrivals;i++) | |
762 { | |
763 inf >> aMag >> aArg >> aDelay >> aSrcAngle >> aRecAngle >> aTopB ounces >> aBotBounces; | |
764 NS_ASSERT(inf.good()); | |
765 complex<double> amplitude(aMag*cos(aArg), aMag*sin(aArg)); | |
766 pathLoss += abs(amplitude); | |
767 //NS_LOG_DEBUG("Got tap. Delay = " << aDelay << " Amp = " << amplitude); | |
768 tapPairs.insert(make_pair(aDelay, amplitude)); | |
769 if(aDelay < pDelay) | |
770 pDelay = aDelay; | |
771 } | |
772 | |
773 pathLoss = 20*log10(pathLoss); | |
774 BhIndex bhi((uint32_t) freq, *srcDepIt, *recDepIt, (uint32_t) *range It); | |
775 //NS_LOG_DEBUG("Creating index with rd=" << *recDepIt << " and rr=" << *rangeIt); | |
776 vector<double> amps = ProcessTaps(tapPairs); | |
777 | |
778 RunResult run; | |
779 run.amps = amps; | |
780 run.index = bhi; | |
781 run.pathLossDb = pathLoss; | |
782 run.propDelay = pDelay; | |
783 results.push_back(run); | |
784 | |
785 //NS_LOG_DEBUG("Read " << tapPairs.size() << " taps for SD=" << bhi. m_srcDepthM << ", RD=" << bhi.m_recDepthM << " Range=" << bhi.m_rangeM << " P athloss=" << pathLoss); | |
786 | |
787 } | |
788 } | |
789 } | |
790 | |
791 | |
792 #ifdef USE_SQL_DAT | |
793 void | |
794 DatMaker::WriteResults(list<BellhopResp> &results, sqlite3 *sqldb) | |
795 { | |
796 ostringstream sqlStr; | |
797 //expecting results to be in the order read into in ReadArrivals. | |
798 //i.e. sd1 rd1 rr1 .. rrn rd2 rr1 .. rrn .. rdn rr1 rrn sd2 rd1 rr1 ... sdn | |
799 | |
800 char *err; | |
801 int32_t rc; | |
802 static uint32_t tapid = 0; | |
803 | |
804 //list<BellhopResp>::iterator it = results.front(); | |
805 for (uint32_t sd = 0; sd < m_NumSrcDepth; sd++) | |
806 { | |
807 NS_ASSERT(!results.empty()); | |
808 uint32_t srcDepth = results.front().GetSrcDepthM(); | |
809 uint32_t freq = results.front().GetFreqHz(); | |
810 | |
811 for (uint32_t rd = 0; rd < m_NumRecDepth; rd++) | |
812 { | |
813 uint32_t recDepth = m_MinRecDepthM + rd*m_RecDepthStepM; | |
814 | |
815 for (uint32_t rr = 0; rr < m_NumRanges; rr++) | |
816 { | |
817 tapid++; | |
818 BellhopResp resp = results.front(); | |
819 results.pop_front(); | |
820 NS_LOG_DEBUG("Results source depth: " << resp.GetSrcDepthM() << " Expected depth: " << m_MinSrcDepthM + sd*m_SrcDepthStepM); | |
821 NS_LOG_DEBUG("Results receiver depth: " << resp.GetRecDepthM() << " Expected depth: " << m_MinRecDepthM + rd*m_RecDepthStepM); | |
822 NS_LOG_DEBUG("Results range: " << resp.GetRangeM() << " Expected r ange: " << 1000*(m_MinRangeKm + rr*m_RangeStepKm)); | |
823 NS_ASSERT(resp.GetSrcDepthM() == m_MinSrcDepthM + sd* m_SrcDepthSt epM); | |
824 NS_ASSERT(resp.GetRecDepthM() == m_MinRecDepthM + rd*m_RecDepthSte pM); | |
825 uint32_t range = 1000*(m_MinRangeKm + rr*m_RangeStepKm); | |
826 | |
827 UanPdp pdp = resp.GetPdp(); | |
828 sqlStr.str(""); | |
829 sqlStr << "INSERT INTO indextable (srcdepth, recdepth, range, freq , ploss) VALUES (" | |
830 << srcDepth << ", " << recDepth << ", " << range << ", " << freq << ", " << resp.GetPathLossDb() << ");"; | |
831 rc = sqlite3_exec(sqldb, sqlStr.str().c_str(), &sqlCallback, 0, &e rr); | |
832 if(rc) | |
833 { | |
834 sqlite3_close(sqldb); | |
835 NS_FATAL_ERROR("SQL error inserting index into indextable.\nEr ror: " << err << "\nQuery: " << sqlStr.str()); | |
836 } | |
837 | |
838 UanPdp::Iterator tapit = pdp.GetBegin(); | |
839 for(;tapit != pdp.GetEnd();tapit++) | |
840 { | |
841 sqlStr.str(""); | |
842 sqlStr << "INSERT INTO taptable (tapid, delay, realamp, imagam p) VALUES " << | |
843 "(" << tapid << ", " << tapit->GetDelay().GetSeconds () << | |
844 ", " << real(tapit->GetAmp()) << ", " << imag(tapit- >GetAmp()) << ");"; | |
845 std::cout << sqlStr.str() << endl; | |
846 rc = sqlite3_exec(sqldb, sqlStr.str().c_str(), &sqlCallback, 0 , &err); | |
847 if(rc) | |
848 { | |
849 sqlite3_close(sqldb); | |
850 NS_FATAL_ERROR("SQL error inserting tap into taptable in.\ nError: " << err << "\nQuery: " << sqlStr.str()); | |
851 } | |
852 | |
853 } | |
854 } | |
855 } | |
856 | |
857 } | |
858 } | |
859 | |
860 sqlite3 * | |
861 DatMaker::CreateDb(string dbfile) | |
862 { | |
863 | |
864 char *err; | |
865 int32_t rc; | |
866 ostringstream sqlStr; | |
867 | |
868 std::ifstream tstfile(dbfile.c_str()); | |
869 if(tstfile.is_open()) | |
870 { | |
871 NS_FATAL_ERROR("DB file " << dbfile << " already exists!"); | |
872 } | |
873 sqlite3 *sqldb; | |
874 rc = sqlite3_open(dbfile.c_str(), &sqldb); | |
875 if(rc) | |
876 { | |
877 sqlite3_close(sqldb); | |
878 NS_FATAL_ERROR("Could not open DB: " << dbfile); | |
879 } | |
880 sqlStr << "CREATE TABLE indextable (tapid INTEGER PRIMARY KEY AUTOINCREMENT, " | |
881 << "srcdepth INTEGER, recdepth INTEGER, range INTEGER, freq INTEGER, pl oss DECIMAL);"; | |
882 | |
883 rc = sqlite3_exec(sqldb, sqlStr.str().c_str(), &sqlCallback, 0, &err); | |
884 if(rc) | |
885 { | |
886 sqlite3_close(sqldb); | |
887 NS_FATAL_ERROR("SQL Error creating table, indextable, in " << dbfile << ". \nError: " << err); | |
888 } | |
889 | |
890 sqlStr.str(""); | |
891 sqlStr << "CREATE TABLE taptable (tapid INTEGER, " | |
892 << "delay DECIMAL, realamp DECIMAL, imagamp DECIMAL);"; | |
893 | |
894 rc = sqlite3_exec(sqldb, sqlStr.str().c_str(), &sqlCallback, 0, &err); | |
895 if(rc) | |
896 { | |
897 sqlite3_close(sqldb); | |
898 NS_FATAL_ERROR("SQL Error creating table, taptable, in " << dbfile << ".\n Error: " << err); | |
899 } | |
900 return sqldb; | |
901 } | |
902 #else | |
903 | |
904 void | |
905 DatMaker::WriteResults(list<BellhopResp> &results) | |
906 { | |
907 ostringstream datFileName; | |
908 //expecting results to be in the order read into in ReadArrivals. | |
909 //i.e. sd1 rd1 rr1 .. rrn rd2 rr1 .. rrn .. rdn rr1 rrn sd2 rd1 rr1 ... sdn | |
910 | |
911 ofstream outf; | |
912 //list<BellhopResp>::iterator it = results.front(); | |
913 for (uint32_t sd = 0; sd < m_numSrcDepth; sd++) | |
914 { | |
915 NS_ASSERT(!results.empty()); | |
916 datFileName.str(""); | |
917 datFileName << fixed << setprecision(1) << m_dataPath << results.front().G etSrcDepthM() << "-" << results.front().GetFreqHz() << ".dat"; | |
918 outf.open(datFileName.str().c_str()); | |
919 if(!outf.good()) | |
920 { | |
921 NS_FATAL_ERROR("Fatal error opening file: " << datFileName.str()); | |
922 } | |
923 for (uint32_t rd = 0; rd < m_numRecDepth; rd++) | |
924 { | |
925 outf << "rd " << m_minRecDepthM + rd*m_recDepthStepM << endl; | |
926 for (uint32_t rr = 0; rr < m_numRanges; rr++) | |
927 { | |
928 BellhopResp resp = results.front(); | |
929 results.pop_front(); | |
930 // NS_LOG_DEBUG("Results source depth: " << resp.GetSrcDepthM() << " Expected depth: " << m_minSrcDepthM + sd*m_srcDepthStepM); | |
931 // NS_LOG_DEBUG("Results receiver depth: " << resp.GetRecDepthM() < < " Expected depth: " << m_minRecDepthM + rd*m_recDepthStepM); | |
932 // NS_LOG_DEBUG("Results range: " << resp.GetRangeM() << " Expected range: " << 1000*(m_minRangeKm + rr*m_rangeStepKm)); | |
933 NS_ASSERT(resp.GetSrcDepthM() == m_minSrcDepthM + sd* m_srcDepthSt epM); | |
934 //NS_ASSERT(resp.GetRangeM() == uint32_t (1000*(m_MinRangeKm+ rr * m_RangeStepKm))); | |
935 NS_ASSERT(resp.GetRecDepthM() == m_minRecDepthM + rd*m_recDepthSte pM); | |
936 | |
937 outf << "rr " << resp.GetRangeM() << endl; | |
938 UanPdp pdp = resp.GetPdp(); | |
939 outf << resp.GetPathLossDb() << endl; | |
940 outf << pdp; | |
941 outf << endl; | |
942 | |
943 } | |
944 } | |
945 outf.close(); | |
946 } | |
947 | |
948 } | |
949 | |
950 #endif //USQ_SQL_DAT | |
OLD | NEW |