Index: examples/uan-create-dat.cc |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/examples/uan-create-dat.cc |
@@ -0,0 +1,950 @@ |
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
+/* |
+ * Copyright (c) 2008 University of Washington |
+ * |
+ * This program is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License as published by |
+ * the Free Software Foundation, either version 3 of the License, or |
+ * (at your option) any later version. |
Andrey Mazo
2009/11/15 13:32:11
ns-3 is licensed under GPLv2.
|
+ * |
+ * This program is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. |
+ * |
+ * Author: Leonard Tracy <lentracy@u.washington.edu> |
+ * |
+ * |
+ */ |
+ |
+ |
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
|
+//TODO: Implement pertubations and averaging. |
+//NOTE: ^^^ THIS HAS NOT YET BEEN DONE |
faker.moatamri
2009/10/21 10:24:09
So what does this file do? any Doxygen
|
+ |
+#include "ns3/common-module.h" |
+#include "ns3/core-module.h" |
+#include "ns3/uan-module.h" |
+//#include "ns3/assert.h" |
+ |
+//#include "ns3/log.h" |
faker.moatamri
2009/10/21 10:24:09
Please remove ALL commented code in all files!
|
+ |
+//#define USE_SQL_DAT |
+ |
+#include <iostream> |
+#include <fstream> |
+#include <algorithm> |
+#include <cctype> |
+#include <string> |
+#include <sstream> |
+#include <set> |
+#include <vector> |
+#include <iomanip> |
+#ifdef USE_SQL_DAT |
+#include <sqlite3.h> |
+#endif |
+ |
+using namespace ns3; |
+using namespace std; |
+ |
+ |
+ |
+ |
+NS_LOG_COMPONENT_DEFINE("BhCreateDat2"); |
+ |
+#ifdef USE_SQL_DAT |
+static int |
+sqlCallback(void *vd, int32_t argc, char **argv, char **cname) |
+{ |
+ for(int32_t i=0;i<argc;i++) |
+ { |
+ std::cout << "Column: " << cname[i] << " is " << argv[i] << std::endl; |
+ } |
+ return 0; |
+} |
+#endif //USE_SQL_DAT |
+ |
+//bool |
+//operator < (std::pair<double, complex<double> > a, std::pair<double, complex<double> > b) |
+//{ |
+// return a.first < b.first; |
+//} |
+ |
+struct TapCmp |
+{ |
+ bool operator () (pair<double, complex<double> > a, pair<double, complex<double> > b) |
+ { |
+ return a.first < b.first; |
+ } |
+}; |
+ |
+struct RunResult |
+{ |
+ BhIndex index; |
+ double pathLossDb; |
+ vector<double> amps; |
+ double propDelay; |
+}; |
+ |
+class DatMaker : public BhConfig |
faker.moatamri
2009/10/21 10:24:09
Can you please create a header file and put all of
|
+{ |
+ |
+public: |
+ void PrintHelp(void); |
faker.moatamri
2009/10/21 10:24:09
Coding style for all files, please review the codi
|
+ void MakeDatFiles(void); |
+ virtual bool Config(const char *cfgFile); |
+ |
+private: |
+ typedef std::list<pair<double, double> > SspList; |
+ typedef std::set<pair<double, complex<double> >, TapCmp > TapPairSet; |
+ |
+ //pair < delay in seconds, amplitude > |
+ //typedef std::vector<pair<double, complex<double> > > TapVec; |
+ string m_bhPath; |
+ uint32_t m_nmedia; |
+ string m_ssOpt; |
+ uint32_t m_nmesh; |
+ uint32_t m_topSigma; |
+ double m_zdepth; |
+ |
+ SspList m_sspVals; |
+ string m_botSigma; |
+ double m_botRough; |
+ |
+ string m_botArgs; |
+ string m_runType; |
+ |
+ uint32_t m_numBeams; |
+ double m_minBeamAngle; |
+ double m_maxBeamAngle; |
+ |
+ double m_step; |
+ double m_zbox; |
+ double m_rbox; |
+ |
+ double m_perturbMean; |
+ double m_perturbVariance; |
+ uint32_t m_numAverages; |
+ |
+ uint32_t m_preTaps; |
+ uint32_t m_totalTaps; |
+ double m_resolution; |
+ |
+ void WriteEnvFile(string filename, uint32_t freqHz); |
+ void WriteEnvFile(string filename, uint32_t freqHz, SspList ssp); |
+ void ReadArrivals(list<RunResult> &results); |
+ set<string> FillParams(void); |
+ |
+#ifdef USE_SQL_DAT |
+ void WriteResults(list<BellhopResp> &results, sqlite3 *sqldb); |
+ sqlite3 *CreateDb(string dbfile); |
+#else |
+ void WriteResults(list<BellhopResp> &results); |
+#endif |
+ //list<TapVec> SumTapVecList(list<TapVec> &a, list<TapVec> &b); |
+ vector<double> ProcessTaps (TapPairSet pairs); |
+ SspList PerturbSsp(void); |
+ |
+ void DoAverage (list<BellhopResp> &out, vector<list<RunResult> > runs); |
+ |
+}; |
+ |
+void |
+DatMaker::PrintHelp(void) |
+{ |
+ cout << "Usage: create-dat <CFG_FILE>" << endl; |
+ cout << "\tCFG_FILE=Configuration file describing frequency, depth, and ranges to include" << endl; |
+ cout << "See documentation for details" << endl << endl; |
+} |
+ |
+void |
+DatMaker::WriteEnvFile(string filename, uint32_t freqHz) |
+{ |
+ WriteEnvFile(filename, freqHz, m_sspVals); |
+} |
+void |
+DatMaker::WriteEnvFile(string filename, uint32_t freqHz, SspList ssp) |
+{ |
+ |
+ ofstream env(filename.c_str()); |
+ |
+ env << "'CreateDatEnv'" << endl; |
+ env << freqHz << endl << m_nmedia << endl << m_ssOpt << endl; |
+ env << m_nmesh << " " << m_topSigma << " " << m_zdepth << endl; |
+ |
+ list<pair<double, double> >::iterator it = ssp.begin(); |
+ |
+ for(;it != ssp.end(); it++) |
+ { |
+ env << (*it).first << " " << (*it).second << " /" << endl; |
+ } |
+ |
+ env << m_botSigma << " " << m_botRough << endl; |
+ env << m_botArgs << endl; |
+ |
+ //Source depths" |
+ env << m_numSrcDepth << endl; |
+ for(uint32_t i=0;i<m_numSrcDepth;i++) |
+ { |
+ env << m_minSrcDepthM + m_srcDepthStepM*i << " "; |
+ } |
+ env << endl; |
+ //Rx depths |
+ env << m_numRecDepth << endl; |
+ for(uint32_t i=0;i<m_numRecDepth;i++) |
+ { |
+ env << m_minRecDepthM + m_recDepthStepM*i << " "; |
+ } |
+ env << endl; |
+ |
+ //Ranges |
+ env << m_numRanges << endl; |
+ for(uint32_t i=0;i<m_numRanges;i++) |
+ { |
+ env << m_minRangeKm + i*m_rangeStepKm << " "; |
+ } |
+ env << endl; |
+ |
+ env << m_runType << endl; |
+ env << m_numBeams << endl << m_minBeamAngle << " " << m_maxBeamAngle << " /" << endl; |
+ env << m_step << " " << m_zbox << " " << m_rbox << endl; |
+ env << endl; |
+ env.close(); |
+} |
+ |
+set<string> |
+DatMaker::FillParams(void) |
+{ |
+ |
+ set<string> params; |
+ |
+ params.insert("bellhoppath"); |
+ params.insert("SSP"); |
+ params.insert("nmedia"); |
+ params.insert("ssopt"); |
+ params.insert("nmesh"); |
+ params.insert("topsigma"); |
+ params.insert("sspbottomdepth"); |
+ params.insert("bottomsigma"); |
+ params.insert("bottomroughness"); |
+ params.insert("runtype"); |
+ params.insert("numbeams"); |
+ params.insert("minbeamangle"); |
+ params.insert("maxbeamangle"); |
+ params.insert("step"); |
+ params.insert("raybottomdepth"); |
+ params.insert("raymaxrange"); |
+ params.insert("perturbmean"); |
+ params.insert("perturbvariance"); |
+ params.insert("numavgs"); |
+ params.insert("tapsbeforemax"); |
+ params.insert("totaltaps"); |
+ params.insert("bottomargument"); |
+ params.insert("resolution"); |
+ |
+ return params; |
+} |
+ |
+ |
+bool |
+DatMaker::Config(const char *cfgFileName) |
+{ |
+ |
+ ifstream inf(cfgFileName); |
+ ostringstream buf; |
+ int (*tl)(int) = tolower; |
+ |
+ if(!BhConfig::Config(cfgFileName)) |
+ { |
+ NS_FATAL_ERROR("Bad configuration file: Missing basic params"); |
+ return false; |
+ } |
+ |
+ |
+ set<string> params = FillParams(); |
+ |
+ |
+ if(!inf.good()) |
+ { |
+ return false; //Bad environment file |
+ } |
+ m_configFile = string(cfgFileName); |
+ |
+ string line; |
+ uint32_t lineNo=0; |
+ |
+ while(getline(inf, line)) |
+ { |
+ lineNo++; |
+ trim(line); |
+ //NS_LOG_DEBUG("Line " << lineNo << ": " << line); |
+ if(line == "SSP:") |
+ { |
+ params.erase("SSP"); |
+ buf.str(""); |
+ while(1) |
+ { |
+ |
+ if(!getline(inf, line)) |
+ { |
+ NS_FATAL_ERROR("Found no ENDSSP: after SSP: in config file " << m_configFile); |
+ } |
+ if(line == "ENDSSP:") |
+ { |
+ break; |
+ } |
+ NS_LOG_DEBUG(line); |
+ buf << line << endl; |
+ string::size_type comPos = line.find_first_of(","); |
+ if(comPos == string::npos) |
+ { |
+ NS_FATAL_ERROR("Invalid SSP entry: " << line); |
+ } |
+ string firstArg = trim(line.substr(0,comPos)); |
+ string scdArg = trim(line.substr(comPos+1, line.length() - comPos + 1)); |
+ m_sspVals.push_back(make_pair(atof(firstArg.c_str()), atof(scdArg.c_str()))); |
+ } |
+ } |
+ else |
+ { |
+ if(line.empty() || line.at(0) == '#') //comment |
+ continue; |
+ |
+ std::string::size_type colonPos = line.find_first_of(':'); |
+ if(colonPos == string::npos) |
+ { |
+ NS_LOG_WARN("WARNING: Unrecognized configuration line at line number " << lineNo << ": " << line); |
+ continue; |
+ } |
+ |
+ |
+ string configParam = trim(line.substr(0, colonPos)); |
+ transform(configParam.begin(), configParam.end(), configParam.begin(), tl); |
+ |
+ |
+ string::size_type endPos = line.find_first_of("# ", colonPos+1); |
+ string::size_type valSize = (endPos == string::npos)?(line.length() - colonPos + 1):(colonPos - endPos - 1); |
+ string configValue = trim(line.substr(colonPos+1, valSize)); |
+ |
+ params.erase(configParam); |
+ if(configParam == "bellhoppath") |
+ { |
+ m_bhPath = configValue; |
+ NS_LOG_DEBUG("Set BH path: " << m_bhPath); |
+ } |
+ else if(configParam == "nmedia") |
+ { |
+ m_nmedia = atoi(configValue.c_str()); |
+ NS_LOG_DEBUG("Set NMedia: " << m_nmedia); |
+ } |
+ else if(configParam == "ssopt") |
+ { |
+ m_ssOpt = configValue; |
+ NS_LOG_DEBUG("Set SSOPT: " << m_ssOpt); |
+ } |
+ else if(configParam == "nmesh") |
+ { |
+ m_nmesh = atoi(configValue.c_str()); |
+ NS_LOG_DEBUG("Set NMesh:" << m_nmesh); |
+ } |
+ else if(configParam == "topsigma") |
+ { |
+ m_topSigma = atoi(configValue.c_str()); |
+ NS_LOG_DEBUG("Set top SIGMA: " << m_topSigma); |
+ } |
+ else if(configParam == "sspbottomdepth") |
+ { |
+ m_zdepth = atof(configValue.c_str()); |
+ NS_LOG_DEBUG("Set SSP bottom depth: " << m_zdepth << " m"); |
+ } |
+ else if(configParam == "bottomsigma") |
+ { |
+ m_botSigma = configValue; |
+ NS_LOG_DEBUG("Set bottom sigma: " << m_botSigma); |
+ } |
+ else if(configParam == "bottomroughness") |
+ { |
+ m_botRough = atof(configValue.c_str()); |
+ NS_LOG_DEBUG("Set bottom roughness: " << m_botRough); |
+ } |
+ else if(configParam == "bottomargument") |
+ { |
+ m_botArgs = configValue; |
+ NS_LOG_DEBUG("Set bottom arguments"); |
+ } |
+ else if(configParam == "runtype") |
+ { |
+ m_runType = configValue; |
+ NS_LOG_DEBUG("Set run type: " << m_runType); |
+ } |
+ else if(configParam == "numbeams") |
+ { |
+ m_numBeams = atoi(configValue.c_str()); |
+ NS_LOG_DEBUG("Set number of beams: " << m_numBeams); |
+ } |
+ else if(configParam == "minbeamangle") |
+ { |
+ m_minBeamAngle = atof(configValue.c_str()); |
+ NS_LOG_DEBUG("Set minimum beam angle: " << m_minBeamAngle << " degrees"); |
+ } |
+ else if(configParam == "maxbeamangle") |
+ { |
+ m_maxBeamAngle = atof(configValue.c_str()); |
+ NS_LOG_DEBUG("Set maximum beam angle: " << m_maxBeamAngle << " degrees"); |
+ } |
+ else if(configParam == "step") |
+ { |
+ m_step = atof(configValue.c_str()); |
+ NS_LOG_DEBUG("Set step: " << m_step << " m"); |
+ } |
+ else if(configParam == "raybottomdepth") |
+ { |
+ m_zbox = atof(configValue.c_str()); |
+ NS_LOG_DEBUG("Set ray bottom depth: " << m_zbox << "m"); |
+ } |
+ else if(configParam == "raymaxrange") |
+ { |
+ m_rbox = atof(configValue.c_str()); |
+ NS_LOG_DEBUG("Set max range to trace a ray: " << m_rbox << " km"); |
+ } |
+ else if(configParam == "perturbmean") |
+ { |
+ m_perturbMean = atof(configValue.c_str()); |
+ NS_LOG_DEBUG("Set mean for SSP petrubations: " << m_perturbMean); |
+ } |
+ else if(configParam == "perturbvariance") |
+ { |
+ m_perturbVariance = atof(configValue.c_str()); |
+ NS_LOG_DEBUG("Set variance for SSP pertubations: " << m_perturbVariance); |
+ } |
+ else if(configParam == "numavgs") |
+ { |
+ m_numAverages = atoi(configValue.c_str()); |
+ NS_LOG_DEBUG("Set number of averages: " << m_numAverages); |
+ } |
+ else if(configParam == "tapsbeforemax") |
+ { |
+ m_preTaps = atoi(configValue.c_str()); |
+ NS_LOG_DEBUG("Set taps before max to keep: " << m_preTaps << " taps"); |
+ } |
+ else if(configParam == "totaltaps") |
+ { |
+ m_totalTaps = atoi(configValue.c_str()); |
+ NS_LOG_DEBUG("Set total taps: " << m_totalTaps << " taps"); |
+ } |
+ else if(configParam == "resolution") |
+ { |
+ m_resolution = atof(configValue.c_str()); |
+ } |
+ else |
+ continue; |
+ |
+ params.erase(configParam); |
+ |
+ } |
+ } |
+ |
+ if(!params.empty()) |
+ { |
+ NS_LOG_WARN("Config file " << m_configFile << " is incomplete"); |
+ NS_LOG_WARN("Missing parameters:"); |
+ set<string>::iterator it = params.begin(); |
+ for(; it != params.end(); it++) |
+ { |
+ NS_LOG_WARN(*it); |
+ } |
+ NS_FATAL_ERROR("Missing parameters"); |
+ return false; |
+ } |
+ return true; |
+} |
+int main(int argc, char *argv[]) |
+{ |
+ LogComponentEnable("BhCreateDat2", LOG_LEVEL_ALL); |
+ LogComponentEnable("UanPropBh", LOG_LEVEL_ALL); |
+ DatMaker bhc; |
+ |
+ if(argc != 2) |
+ { |
+ bhc.PrintHelp(); |
+ return 1; |
+ } |
+ |
+ bhc.Config(argv[1]); |
+ //bhc.WriteEnvFile("TestEnv.env"); |
+ //list<BellhopResp> results; |
+ //bhc.ReadArrivals(results); |
+ bhc.MakeDatFiles(); |
+ |
+} |
+ |
+//list<DatMaker::TapVec> |
+//DatMaker::SumTapVecList(list<TapVec> &a, list<TapVec> &b) |
+//{ |
+// NS_ASSERT(a.size() == b.size()); |
+// list<TapVec> out; |
+// |
+// list<TapVec>::iterator ait = a.begin(); |
+// list<TapVec>::iterator bit = b.begin(); |
+// for(;ait != a.end();ait++, bit++) |
+// { |
+// NS_ASSERT(ait->size() == bit->size()); |
+// TapVec outvec(ait->size()); |
+// for(uint32_t i=0;i < ait->size();i++) |
+// { |
+// TapVec &av = *ait; |
+// TapVec &bv = *bit; |
+// outvec[i] = make_pair(av[i].first, abs(av[i].second) + abs(bv[i].second)); |
+// } |
+// out.push_back(outvec); |
+// } |
+// return out; |
+//} |
+ |
+DatMaker::SspList |
+DatMaker::PerturbSsp(void) |
+{ |
+ NormalVariable nrv(m_perturbMean, m_perturbVariance); |
+ NS_LOG_DEBUG("Created RV with mean " << m_perturbMean << " and variance " << m_perturbVariance); |
+ SspList outlist; |
+ for(SspList::iterator it = m_sspVals.begin();it != m_sspVals.end();it++) |
+ { |
+ double n; |
+ do |
+ { |
+ n = nrv.GetValue(); |
+ } while(n != n); |
+ |
+ double newValue = it->second + n; |
+ NS_LOG_DEBUG("Perturbed SSP Pair: " << it->first << ", " << newValue << " From " << it->first << ", " << it->second << " (nrv = " << n << ")"); |
+ |
+ outlist.push_back(make_pair(it->first, newValue)); |
+ } |
+ return outlist; |
+} |
+ |
+ |
+ |
+void |
+DatMaker::MakeDatFiles(void) |
+{ |
+ string envFile("make-dat-envfile.env"); |
+ ostringstream bhcommand; |
+ bhcommand << m_bhPath << " < " << envFile << " > /dev/null"; |
+ |
+#ifdef USE_SQL_DAT |
+ sqlite3 *sqldb = CreateDb("sqldbtest.db"); |
+#endif |
+ |
+ for(uint32_t fr = 0; fr < m_numFreqs; fr++) |
+ { |
+ |
+ vector<list<RunResult> > runs(m_numAverages); |
+ |
+ for(uint32_t avg = 0; avg < m_numAverages;avg++) |
+ { |
+ //Do run |
+ if(m_numAverages > 1) |
+ WriteEnvFile(envFile, m_minFreqHz + m_freqStepHz * fr, PerturbSsp()); |
+ else |
+ WriteEnvFile(envFile, m_minFreqHz + m_freqStepHz * fr); |
+ int32_t result = system(bhcommand.str().c_str()); |
+ if(result) |
+ { |
+ NS_FATAL_ERROR("Bellhop ended with non-zero error!"); |
+ } |
+ |
+ ReadArrivals(runs[avg]); |
+ |
+ } |
+ list<BellhopResp> results; |
+ DoAverage(results, runs); |
+ NS_LOG_DEBUG("Size of results: " << results.size()); |
+#ifdef USE_SQL_DAT |
+ WriteResults(results, sqldb); |
+#else |
+ WriteResults(results); |
+#endif |
+ } |
+ |
+} |
+ |
+void |
+DatMaker::DoAverage (list<BellhopResp> &out, vector<list<RunResult> > runs) |
+{ |
+ |
+ uint32_t numRuns = runs[0].size(); |
+ vector<list<RunResult>::iterator> its(numRuns); |
+ for(uint32_t i=0;i<m_numAverages;i++) |
+ { |
+ NS_ASSERT_MSG(runs[i].size() == numRuns, "In do Average: Lengths of runs unequal!"); |
+ its[i] = runs[i].begin(); |
+ for(uint32_t j=0;j<numRuns;j++) |
+ { |
+ NS_ASSERT_MSG(its[i]->amps.size() == m_totalTaps, "Not " << m_totalTaps << " taps in run " << j << " of run set (average) " << i); |
+ its[i]++; |
+ } |
+ |
+ its[i] = runs[i].begin(); |
+ |
+ } |
+ vector<vector<double> > finals(numRuns, vector<double>(m_totalTaps)); |
+ |
+ for(uint32_t i=0; i< numRuns;i++) |
+ { |
+ //finals[i] = vector<double>(m_totalTaps, 0); |
+ for(uint32_t j=0;j<m_numAverages;j++) |
+ { |
+ for(uint32_t k=0;k<m_totalTaps;k++) |
+ { |
+ double current = finals[i][k]; |
+ double next = its[j]->amps[k]; |
+ next /= m_numAverages; |
+ finals[i][k] = current + next; |
+ if(k==5) |
+ NS_LOG_DEBUG("finals[" << i << "][5] == " << finals[i][5] << " --- current=" << current << " and next=" << next); |
+ } |
+ its[j]++; |
+ } |
+ } |
+ //Calculate pathlosses |
+ vector<double> pLoss; |
+ for(uint32_t i=0;i<numRuns;i++) |
+ { |
+ double sum=0; |
+ for(uint32_t j=0;j<m_totalTaps;j++) |
+ { |
+ sum += finals[i][j]; |
+ } |
+ pLoss.push_back(20*log10(sum)); |
+ NS_LOG_DEBUG("Calculated pathloss: " << sum); |
+ //Normalize PDP |
+ for(uint32_t j=0;j<m_totalTaps;j++) |
+ { |
+ finals[i][j] = finals[i][j] / sum; |
+ } |
+ } |
+ out.clear(); |
+ list<RunResult>::iterator fit = runs[0].begin(); |
+ for(uint32_t i=0;i<numRuns;i++, fit++) |
+ { |
+ //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]); |
+ BellhopResp resp(fit->index, UanPdp(finals[i], Seconds(m_resolution)), pLoss[i]); |
+ out.push_back(resp); |
+ } |
+} |
+ |
+ |
+vector<double> |
+DatMaker::ProcessTaps (TapPairSet pairs) |
+{ |
+ |
+ |
+ vector<double> tempTap; |
+ TapPairSet::iterator cit = pairs.begin(); |
+ for(;cit != pairs.end();cit++) |
+ { |
+ double runTime = 0; |
+ complex<double> tapSum = 0; |
+ while(cit != pairs.end() && runTime < m_resolution) |
+ { |
+ runTime += cit->first; |
+ tapSum += cit->second; |
+ } |
+ tempTap.push_back(abs(tapSum)); |
+ } |
+ |
+ vector<double> out(m_totalTaps); |
+ |
+ //Find max index |
+ vector<double>::iterator mit = max_element(tempTap.begin(), tempTap.end()); |
+ vector<double>::iterator it = mit; |
+ it--; |
+ bool atbegin = false; |
+ for(uint32_t i=m_preTaps;i>0;i--) |
+ { |
+ if(atbegin) |
+ { |
+ out[i-1] = 0; |
+ } |
+ else |
+ { |
+ out[i-1] = *it; |
+ if(it == tempTap.begin()) |
+ atbegin = true; |
+ it--; |
+ } |
+ } |
+ for(uint32_t i=m_preTaps;i<m_totalTaps;i++) |
+ { |
+ if(mit != tempTap.end()) |
+ { |
+ out[i] = *mit; |
+ mit++; |
+ } |
+ else |
+ { |
+ out[i] = 0; |
+ } |
+ } |
+ return out; |
+} |
+ |
+void |
+DatMaker::ReadArrivals(list<RunResult> &results) |
+{ |
+ ifstream inf("ARRFIL"); |
+ NS_ASSERT(inf.is_open()); |
+ |
+ //NS_ASSERT_MSG(results.size() == 0, "DatMaker::ReadArrivals expects empty list to fill with results. Got a populated list"); |
+ results.clear(); |
+ |
+ uint32_t numSrcDepths, numRecDepths, numRanges, numArrivals; |
+ double freq; |
+ |
+ inf >> freq >> numSrcDepths >> numRecDepths >> numRanges;// >> sd2; |
+ //inf >> numArrivals; |
+ NS_LOG_DEBUG("Freq: " << freq << endl |
+ << "Number src depths: " << numSrcDepths << endl |
+ << "Number rec depths: " << numRecDepths << endl |
+ << "Number ranges: " << numRanges << endl); |
+ // << "sd2?: " << sd2); |
+ |
+ vector<uint32_t> srcDepths, recDepths; |
+ vector<double> ranges; |
+ double depthBuf; |
+ double rangeBuf; |
+ |
+ for(uint32_t i=0;i<numSrcDepths;i++) |
+ { |
+ inf >> depthBuf; |
+ srcDepths.push_back((uint32_t) depthBuf); |
+ NS_LOG_DEBUG("Src depth: " << depthBuf); |
+ } |
+ for(uint32_t i=0;i<numRecDepths;i++) |
+ { |
+ inf >> depthBuf; |
+ recDepths.push_back((uint32_t) depthBuf); |
+ NS_LOG_DEBUG("Rec Depth: " << depthBuf); |
+ } |
+ for(uint32_t i=0;i<numRanges;i++) |
+ { |
+ inf >> rangeBuf; |
+ ranges.push_back(rangeBuf); |
+ NS_LOG_DEBUG("Range: " << rangeBuf); |
+ } |
+ |
+ vector<uint32_t>::iterator srcDepIt, recDepIt; |
+ vector<double>::iterator rangeIt; |
+ |
+ |
+ for(srcDepIt = srcDepths.begin();srcDepIt != srcDepths.end();srcDepIt++) |
+ { |
+ inf >> numArrivals; |
+ NS_LOG_DEBUG("Max arrivals for src depth " << *srcDepIt << " = " << numArrivals); |
+ for(recDepIt=recDepths.begin();recDepIt != recDepths.end();recDepIt++) |
+ for(rangeIt=ranges.begin();rangeIt != ranges.end(); rangeIt++) |
+ { |
+ |
+ inf >> numArrivals; |
+ //NS_LOG_DEBUG("Looking for " << numArrivals << " arrivals at range " << *rangeIt << " and frequency " << freq << " (NOTE: These are converted in integers in next line)"); |
+ |
+ BhIndex newIndex((uint32_t) freq, *srcDepIt, *recDepIt, (uint32_t) *rangeIt); |
+ |
+ double aMag, aArg, aDelay, aSrcAngle, aRecAngle; |
+ uint32_t aTopBounces, aBotBounces; |
+ double pathLoss = 0; |
+ double pDelay = 1e30; |
+ TapPairSet tapPairs; |
+ for(uint32_t i=0;i<numArrivals;i++) |
+ { |
+ inf >> aMag >> aArg >> aDelay >> aSrcAngle >> aRecAngle >> aTopBounces >> aBotBounces; |
+ NS_ASSERT(inf.good()); |
+ complex<double> amplitude(aMag*cos(aArg), aMag*sin(aArg)); |
+ pathLoss += abs(amplitude); |
+ //NS_LOG_DEBUG("Got tap. Delay = " << aDelay << " Amp = " << amplitude); |
+ tapPairs.insert(make_pair(aDelay, amplitude)); |
+ if(aDelay < pDelay) |
+ pDelay = aDelay; |
+ } |
+ |
+ pathLoss = 20*log10(pathLoss); |
+ BhIndex bhi((uint32_t) freq, *srcDepIt, *recDepIt, (uint32_t) *rangeIt); |
+ //NS_LOG_DEBUG("Creating index with rd=" << *recDepIt << " and rr=" << *rangeIt); |
+ vector<double> amps = ProcessTaps(tapPairs); |
+ |
+ RunResult run; |
+ run.amps = amps; |
+ run.index = bhi; |
+ run.pathLossDb = pathLoss; |
+ run.propDelay = pDelay; |
+ results.push_back(run); |
+ |
+ //NS_LOG_DEBUG("Read " << tapPairs.size() << " taps for SD=" << bhi.m_srcDepthM << ", RD=" << bhi.m_recDepthM << " Range=" << bhi.m_rangeM << " Pathloss=" << pathLoss); |
+ |
+ } |
+ } |
+} |
+ |
+ |
+#ifdef USE_SQL_DAT |
+void |
+DatMaker::WriteResults(list<BellhopResp> &results, sqlite3 *sqldb) |
+{ |
+ ostringstream sqlStr; |
+ //expecting results to be in the order read into in ReadArrivals. |
+ //i.e. sd1 rd1 rr1 .. rrn rd2 rr1 .. rrn .. rdn rr1 rrn sd2 rd1 rr1 ... sdn |
+ |
+ char *err; |
+ int32_t rc; |
+ static uint32_t tapid = 0; |
+ |
+ //list<BellhopResp>::iterator it = results.front(); |
+ for (uint32_t sd = 0; sd < m_NumSrcDepth; sd++) |
+ { |
+ NS_ASSERT(!results.empty()); |
+ uint32_t srcDepth = results.front().GetSrcDepthM(); |
+ uint32_t freq = results.front().GetFreqHz(); |
+ |
+ for (uint32_t rd = 0; rd < m_NumRecDepth; rd++) |
+ { |
+ uint32_t recDepth = m_MinRecDepthM + rd*m_RecDepthStepM; |
+ |
+ for (uint32_t rr = 0; rr < m_NumRanges; rr++) |
+ { |
+ tapid++; |
+ BellhopResp resp = results.front(); |
+ results.pop_front(); |
+ NS_LOG_DEBUG("Results source depth: " << resp.GetSrcDepthM() << " Expected depth: " << m_MinSrcDepthM + sd*m_SrcDepthStepM); |
+ NS_LOG_DEBUG("Results receiver depth: " << resp.GetRecDepthM() << " Expected depth: " << m_MinRecDepthM + rd*m_RecDepthStepM); |
+ NS_LOG_DEBUG("Results range: " << resp.GetRangeM() << " Expected range: " << 1000*(m_MinRangeKm + rr*m_RangeStepKm)); |
+ NS_ASSERT(resp.GetSrcDepthM() == m_MinSrcDepthM + sd* m_SrcDepthStepM); |
+ NS_ASSERT(resp.GetRecDepthM() == m_MinRecDepthM + rd*m_RecDepthStepM); |
+ uint32_t range = 1000*(m_MinRangeKm + rr*m_RangeStepKm); |
+ |
+ UanPdp pdp = resp.GetPdp(); |
+ sqlStr.str(""); |
+ sqlStr << "INSERT INTO indextable (srcdepth, recdepth, range, freq, ploss) VALUES (" |
+ << srcDepth << ", " << recDepth << ", " << range << ", " << freq << ", " << resp.GetPathLossDb() << ");"; |
+ rc = sqlite3_exec(sqldb, sqlStr.str().c_str(), &sqlCallback, 0, &err); |
+ if(rc) |
+ { |
+ sqlite3_close(sqldb); |
+ NS_FATAL_ERROR("SQL error inserting index into indextable.\nError: " << err << "\nQuery: " << sqlStr.str()); |
+ } |
+ |
+ UanPdp::Iterator tapit = pdp.GetBegin(); |
+ for(;tapit != pdp.GetEnd();tapit++) |
+ { |
+ sqlStr.str(""); |
+ sqlStr << "INSERT INTO taptable (tapid, delay, realamp, imagamp) VALUES " << |
+ "(" << tapid << ", " << tapit->GetDelay().GetSeconds() << |
+ ", " << real(tapit->GetAmp()) << ", " << imag(tapit->GetAmp()) << ");"; |
+ std::cout << sqlStr.str() << endl; |
+ rc = sqlite3_exec(sqldb, sqlStr.str().c_str(), &sqlCallback, 0, &err); |
+ if(rc) |
+ { |
+ sqlite3_close(sqldb); |
+ NS_FATAL_ERROR("SQL error inserting tap into taptable in.\nError: " << err << "\nQuery: " << sqlStr.str()); |
+ } |
+ |
+ } |
+ } |
+ } |
+ |
+ } |
+} |
+ |
+sqlite3 * |
+DatMaker::CreateDb(string dbfile) |
+{ |
+ |
+ char *err; |
+ int32_t rc; |
+ ostringstream sqlStr; |
+ |
+ std::ifstream tstfile(dbfile.c_str()); |
+ if(tstfile.is_open()) |
+ { |
+ NS_FATAL_ERROR("DB file " << dbfile << " already exists!"); |
+ } |
+ sqlite3 *sqldb; |
+ rc = sqlite3_open(dbfile.c_str(), &sqldb); |
+ if(rc) |
+ { |
+ sqlite3_close(sqldb); |
+ NS_FATAL_ERROR("Could not open DB: " << dbfile); |
+ } |
+ sqlStr << "CREATE TABLE indextable (tapid INTEGER PRIMARY KEY AUTOINCREMENT, " |
+ << "srcdepth INTEGER, recdepth INTEGER, range INTEGER, freq INTEGER, ploss DECIMAL);"; |
+ |
+ rc = sqlite3_exec(sqldb, sqlStr.str().c_str(), &sqlCallback, 0, &err); |
+ if(rc) |
+ { |
+ sqlite3_close(sqldb); |
+ NS_FATAL_ERROR("SQL Error creating table, indextable, in " << dbfile << ".\nError: " << err); |
+ } |
+ |
+ sqlStr.str(""); |
+ sqlStr << "CREATE TABLE taptable (tapid INTEGER, " |
+ << "delay DECIMAL, realamp DECIMAL, imagamp DECIMAL);"; |
+ |
+ rc = sqlite3_exec(sqldb, sqlStr.str().c_str(), &sqlCallback, 0, &err); |
+ if(rc) |
+ { |
+ sqlite3_close(sqldb); |
+ NS_FATAL_ERROR("SQL Error creating table, taptable, in " << dbfile << ".\nError: " << err); |
+ } |
+ return sqldb; |
+} |
+#else |
+ |
+void |
+DatMaker::WriteResults(list<BellhopResp> &results) |
+{ |
+ ostringstream datFileName; |
+ //expecting results to be in the order read into in ReadArrivals. |
+ //i.e. sd1 rd1 rr1 .. rrn rd2 rr1 .. rrn .. rdn rr1 rrn sd2 rd1 rr1 ... sdn |
+ |
+ ofstream outf; |
+ //list<BellhopResp>::iterator it = results.front(); |
+ for (uint32_t sd = 0; sd < m_numSrcDepth; sd++) |
+ { |
+ NS_ASSERT(!results.empty()); |
+ datFileName.str(""); |
+ datFileName << fixed << setprecision(1) << m_dataPath << results.front().GetSrcDepthM() << "-" << results.front().GetFreqHz() << ".dat"; |
+ outf.open(datFileName.str().c_str()); |
+ if(!outf.good()) |
+ { |
+ NS_FATAL_ERROR("Fatal error opening file: " << datFileName.str()); |
+ } |
+ for (uint32_t rd = 0; rd < m_numRecDepth; rd++) |
+ { |
+ outf << "rd " << m_minRecDepthM + rd*m_recDepthStepM << endl; |
+ for (uint32_t rr = 0; rr < m_numRanges; rr++) |
+ { |
+ BellhopResp resp = results.front(); |
+ results.pop_front(); |
+// NS_LOG_DEBUG("Results source depth: " << resp.GetSrcDepthM() << " Expected depth: " << m_minSrcDepthM + sd*m_srcDepthStepM); |
+// NS_LOG_DEBUG("Results receiver depth: " << resp.GetRecDepthM() << " Expected depth: " << m_minRecDepthM + rd*m_recDepthStepM); |
+// NS_LOG_DEBUG("Results range: " << resp.GetRangeM() << " Expected range: " << 1000*(m_minRangeKm + rr*m_rangeStepKm)); |
+ NS_ASSERT(resp.GetSrcDepthM() == m_minSrcDepthM + sd* m_srcDepthStepM); |
+ //NS_ASSERT(resp.GetRangeM() == uint32_t (1000*(m_MinRangeKm+ rr * m_RangeStepKm))); |
+ NS_ASSERT(resp.GetRecDepthM() == m_minRecDepthM + rd*m_recDepthStepM); |
+ |
+ outf << "rr " << resp.GetRangeM() << endl; |
+ UanPdp pdp = resp.GetPdp(); |
+ outf << resp.GetPathLossDb() << endl; |
+ outf << pdp; |
+ outf << endl; |
+ |
+ } |
+ } |
+ outf.close(); |
+ } |
+ |
+} |
+ |
+#endif //USQ_SQL_DAT |