Lemma is an Electromagnetics API
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

EMEarth1D.cpp 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. /* This file is part of Lemma, a geophysical modelling and inversion API */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /**
  6. @file
  7. @author Trevor Irons
  8. @date 12/02/2009
  9. **/
  10. #include "EMEarth1D.h"
  11. #include "FieldPoints.h"
  12. #include "WireAntenna.h"
  13. #include "PolygonalWireAntenna.h"
  14. #ifdef LEMMAUSEOMP
  15. #include "omp.h"
  16. #endif
  17. namespace Lemma {
  18. std::ostream &operator << (std::ostream &stream, const EMEarth1D &ob) {
  19. stream << ob.Serialize() << "\n";
  20. return stream;
  21. }
  22. #ifdef KIHALEE_EM1D
  23. // Wrapper function for Fortran subroutine Em1D bi kihand
  24. // Returns E or H fields (SLOW)
  25. extern "C" { void em1dcall_(int &itype, // source
  26. int &ipol, // source
  27. int &nlay, // Earth
  28. int &nfreq, // source
  29. int &nfield, // Calculator
  30. int &nres, // Receivers
  31. int &jtype, // N/A
  32. int &jgamma, // Controller
  33. double &acc, // Controller
  34. double *dep, // Earth
  35. std::complex<double> *sig, // Earth
  36. double *susl, // Earth
  37. double *sush, // Earth
  38. double *sustau, // Earth
  39. double *susalp, // Earth
  40. double *eprl, // Earth
  41. double *eprh, // Earth
  42. double *eprtau, // Earth
  43. double *epralp, // Earth
  44. double &finit, // N/A
  45. double &flimit, // N/A
  46. double &dlimit, // N/A
  47. double &lfinc, // N/A
  48. double &tx, // Source
  49. double &ty, // Source
  50. double &tz, // Source
  51. double *rxx, // Receivers
  52. double *rxy, // Receivers
  53. double *rxz, // Receivers
  54. std::complex<double> *ex, // Receivers
  55. std::complex<double> *ey, // |
  56. std::complex<double> *ez, // |
  57. std::complex<double> *hx, // |
  58. std::complex<double> *hy, // V
  59. std::complex<double> *hz ); // ___
  60. }
  61. #endif
  62. // ==================== LIFECYCLE ===================================
  63. // TODO init large arrays here.
  64. EMEarth1D::EMEarth1D( const ctor_key& key ) : LemmaObject( key ),
  65. Dipole(nullptr), Earth(nullptr), Receivers(nullptr), Antenna(nullptr),
  66. FieldsToCalculate(BOTH), HankelType(ANDERSON801), icalcinner(0), icalc(0)
  67. //#ifdef HAVE_BOOST_PROGRESS
  68. // , disp(0)
  69. //#endif
  70. {
  71. }
  72. EMEarth1D::~EMEarth1D() {
  73. }
  74. std::shared_ptr<EMEarth1D> EMEarth1D::NewSP() {
  75. return std::make_shared<EMEarth1D>(ctor_key());
  76. }
  77. YAML::Node EMEarth1D::Serialize() const {
  78. YAML::Node node = LemmaObject::Serialize();
  79. node["FieldsToCalculate"] = enum2String(FieldsToCalculate);
  80. node["HankelType"] = enum2String(HankelType);
  81. //if (Dipole != nullptr) node["Dipole"] = Dipole->Serialize();
  82. if (Earth != nullptr) node["Earth"] = Earth->Serialize();
  83. //if (Receivers != nullptr) node["Receivers"] = Receivers->Serialize(); Can be huge?
  84. if (Antenna != nullptr) node["Antenna"] = Antenna->Serialize();
  85. node.SetTag( this->GetName() );
  86. return node;
  87. }
  88. //--------------------------------------------------------------------------------------
  89. // Class: EMEarth1D
  90. // Method: GetName
  91. // Description: Class identifier
  92. //--------------------------------------------------------------------------------------
  93. inline std::string EMEarth1D::GetName ( ) const {
  94. return CName;
  95. } // ----- end of method EMEarth1D::GetName -----
  96. // ==================== ACCESS ===================================
  97. void EMEarth1D::AttachDipoleSource( std::shared_ptr<DipoleSource> dipoleptr) {
  98. Dipole = dipoleptr;
  99. }
  100. void EMEarth1D::AttachLayeredEarthEM( std::shared_ptr<LayeredEarthEM> earthptr) {
  101. Earth = earthptr;
  102. }
  103. void EMEarth1D::AttachFieldPoints( std::shared_ptr<FieldPoints> recptr) {
  104. Receivers = recptr;
  105. if (Receivers == nullptr) {
  106. std::cout << "nullptr Receivers in emearth1d.cpp " << std::endl;
  107. return;
  108. }
  109. // This has an implicid need to first set a source before receivers, users
  110. // will not expect this. Fix
  111. if (Dipole != nullptr) {
  112. switch (FieldsToCalculate) {
  113. case E:
  114. Receivers->SetNumberOfBinsE(Dipole->GetNumberOfFrequencies());
  115. break;
  116. case H:
  117. Receivers->SetNumberOfBinsH(Dipole->GetNumberOfFrequencies());
  118. break;
  119. case BOTH:
  120. Receivers->SetNumberOfBinsE(Dipole->GetNumberOfFrequencies());
  121. Receivers->SetNumberOfBinsH(Dipole->GetNumberOfFrequencies());
  122. break;
  123. }
  124. } else if (Antenna != nullptr) {
  125. switch (FieldsToCalculate) {
  126. case E:
  127. Receivers->SetNumberOfBinsE(Antenna->GetNumberOfFrequencies());
  128. break;
  129. case H:
  130. Receivers->SetNumberOfBinsH(Antenna->GetNumberOfFrequencies());
  131. break;
  132. case BOTH:
  133. Receivers->SetNumberOfBinsE(Antenna->GetNumberOfFrequencies());
  134. Receivers->SetNumberOfBinsH(Antenna->GetNumberOfFrequencies());
  135. break;
  136. }
  137. }
  138. }
  139. void EMEarth1D::AttachWireAntenna(std::shared_ptr<WireAntenna> antennae) {
  140. this->Antenna = antennae;
  141. }
  142. void EMEarth1D::SetFieldsToCalculate(const FIELDCALCULATIONS &calc) {
  143. FieldsToCalculate = calc;
  144. }
  145. void EMEarth1D::SetHankelTransformMethod( const HANKELTRANSFORMTYPE &type) {
  146. HankelType = type;
  147. }
  148. void EMEarth1D::Query() {
  149. std::cout << "EmEarth1D::Query()" << std::endl;
  150. std::cout << "Dipole " << Dipole;
  151. if (Dipole) std::cout << *Dipole << std::endl;
  152. std::cout << "Earth " << Earth;
  153. if (Earth) std::cout << *Earth << std::endl;
  154. std::cout << "Receivers " << Earth;
  155. if (Earth) std::cout << *Receivers << std::endl;
  156. std::cout << "Antenna " << Earth;
  157. if (Antenna) std::cout << *Antenna << std::endl;
  158. std::cout << "icalc " << icalc << std::endl;
  159. std::cout << "icalcinner " << icalcinner << std::endl;
  160. }
  161. // ==================== OPERATIONS ===================================
  162. void EMEarth1D::CalculateWireAntennaFields(bool progressbar) {
  163. #ifdef HAVE_BOOST_PROGRESS
  164. boost::progress_display *disp;
  165. #endif
  166. if (Earth == nullptr) {
  167. throw NullEarth();
  168. }
  169. if (Receivers == nullptr) {
  170. throw NullReceivers();
  171. }
  172. if (Antenna == nullptr) {
  173. throw NullAntenna();
  174. }
  175. if (Dipole != nullptr) {
  176. throw DipoleSourceSpecifiedForWireAntennaCalc();
  177. }
  178. Receivers->ClearFields();
  179. // Check to make sure Receivers are set up for all calculations
  180. switch(FieldsToCalculate) {
  181. case E:
  182. if (Receivers->NumberOfBinsE != Antenna->GetNumberOfFrequencies())
  183. Receivers->SetNumberOfBinsE(Antenna->GetNumberOfFrequencies());
  184. break;
  185. case H:
  186. if (Receivers->NumberOfBinsH != Antenna->GetNumberOfFrequencies())
  187. Receivers->SetNumberOfBinsH(Antenna->GetNumberOfFrequencies());
  188. break;
  189. case BOTH:
  190. if (Receivers->NumberOfBinsH != Antenna->GetNumberOfFrequencies())
  191. Receivers->SetNumberOfBinsH(Antenna->GetNumberOfFrequencies());
  192. if (Receivers->NumberOfBinsE != Antenna->GetNumberOfFrequencies())
  193. Receivers->SetNumberOfBinsE(Antenna->GetNumberOfFrequencies());
  194. break;
  195. }
  196. if (Antenna->GetName() == std::string("PolygonalWireAntenna") || Antenna->GetName() == std::string("TEMTransmitter") ) {
  197. icalc += 1;
  198. // Check to see if they are all on a plane? If so we can do this fast
  199. if (Antenna->IsHorizontallyPlanar() && ( HankelType == ANDERSON801 || HankelType== FHTKEY201 || HankelType==FHTKEY101 ||
  200. HankelType == FHTKEY51 )) {
  201. #ifdef HAVE_BOOST_PROGRESS
  202. if (progressbar) {
  203. disp = new boost::progress_display( Receivers->GetNumberOfPoints()*Antenna->GetNumberOfFrequencies() );
  204. }
  205. #endif
  206. for (int ifreq=0; ifreq<Antenna->GetNumberOfFrequencies();++ifreq) {
  207. Real wavef = 2.*PI* Antenna->GetFrequency(ifreq);
  208. #ifdef LEMMAUSEOMP
  209. #pragma omp parallel
  210. {
  211. #endif
  212. auto Hankel = HankelTransformFactory::NewSP( HankelType );
  213. #ifdef LEMMAUSEOMP
  214. #pragma omp for schedule(static, 1)
  215. #endif
  216. for (int irec=0; irec<Receivers->GetNumberOfPoints(); ++irec) {
  217. auto AntCopy = static_cast<PolygonalWireAntenna*>(Antenna.get())->ClonePA();
  218. SolveLaggedTxRxPair(irec, Hankel.get(), wavef, ifreq, AntCopy.get());
  219. #ifdef HAVE_BOOST_PROGRESS
  220. if (progressbar) ++(*disp);
  221. #endif
  222. }
  223. #pragma omp barrier
  224. #ifdef LEMMAUSEOMP
  225. }
  226. #endif
  227. }
  228. } else
  229. if (Receivers->GetNumberOfPoints() > Antenna->GetNumberOfFrequencies()) {
  230. //std::cout << "freq parallel #1" << std::endl;
  231. //** Progress display bar for long calculations */
  232. #ifdef HAVE_BOOST_PROGRESS
  233. if (progressbar) {
  234. disp = new boost::progress_display( Receivers->GetNumberOfPoints()*Antenna->GetNumberOfFrequencies() );
  235. }
  236. #endif
  237. // parallelise across receivers
  238. #ifdef LEMMAUSEOMP
  239. #pragma omp parallel
  240. #endif
  241. { // OpenMP Parallel Block
  242. // Since these antennas change we need a local copy for each
  243. // thread.
  244. auto AntCopy = static_cast<PolygonalWireAntenna*>(Antenna.get())->ClonePA();
  245. std::shared_ptr<HankelTransform> Hankel;
  246. switch (HankelType) {
  247. case ANDERSON801:
  248. Hankel = FHTAnderson801::NewSP();
  249. break;
  250. case CHAVE:
  251. Hankel = GQChave::NewSP();
  252. break;
  253. case FHTKEY201:
  254. Hankel = FHTKey201::NewSP();
  255. break;
  256. case FHTKEY101:
  257. Hankel = FHTKey101::NewSP();
  258. break;
  259. case FHTKEY51:
  260. Hankel = FHTKey51::NewSP();
  261. break;
  262. case QWEKEY:
  263. Hankel = QWEKey::NewSP();
  264. break;
  265. default:
  266. std::cerr << "Hankel transform cannot be created\n";
  267. exit(EXIT_FAILURE);
  268. }
  269. //for (int irec=tid; irec<Receivers->GetNumberOfPoints(); irec+=nthreads) {
  270. #ifdef LEMMAUSEOMP
  271. #pragma omp for schedule(static, 1) //nowait
  272. #endif
  273. for (int irec=0; irec<Receivers->GetNumberOfPoints(); ++irec) {
  274. if (!Receivers->GetMask(irec)) {
  275. AntCopy->ApproximateWithElectricDipoles(Receivers->GetLocation(irec));
  276. for (int idip=0; idip<AntCopy->GetNumberOfDipoles(); ++idip) {
  277. auto tDipole = AntCopy->GetDipoleSource(idip);
  278. //#ifdef LEMMAUSEOMP
  279. //#pragma omp for schedule(static, 1)
  280. //#endif
  281. for (int ifreq=0; ifreq<tDipole->GetNumberOfFrequencies();
  282. ++ifreq) {
  283. // Propogation constant in free space
  284. Real wavef = tDipole->GetAngularFrequency(ifreq) *
  285. std::sqrt(MU0*EPSILON0);
  286. SolveSingleTxRxPair(irec, Hankel.get(), wavef, ifreq, tDipole.get());
  287. } // freq loop
  288. } // dipole loop
  289. } // mask
  290. //std::cout << "Normal Path\n";
  291. //std::cout << Receivers->GetHfield(0, irec) << std::endl;
  292. //if (irec == 1) exit(0);
  293. #ifdef HAVE_BOOST_PROGRESS
  294. if (progressbar) ++(*disp);
  295. #endif
  296. } // receiver loop
  297. } // OMP_PARALLEL BLOCK
  298. } else if (Antenna->GetNumberOfFrequencies() > 8) {
  299. // parallel across frequencies
  300. //std::cout << "freq parallel #2" << std::endl;
  301. for (int irec=0; irec<Receivers->GetNumberOfPoints(); ++irec) {
  302. if (!Receivers->GetMask(irec)) {
  303. static_cast<PolygonalWireAntenna*>(Antenna.get())->ApproximateWithElectricDipoles(Receivers->GetLocation(irec));
  304. #ifdef LEMMAUSEOMP
  305. #pragma omp parallel
  306. #endif
  307. { // OpenMP Parallel Block
  308. std::shared_ptr<HankelTransform> Hankel;
  309. switch (HankelType) {
  310. case ANDERSON801:
  311. Hankel = FHTAnderson801::NewSP();
  312. break;
  313. case CHAVE:
  314. Hankel = GQChave::NewSP();
  315. break;
  316. case FHTKEY201:
  317. Hankel = FHTKey201::NewSP();
  318. break;
  319. case FHTKEY101:
  320. Hankel = FHTKey101::NewSP();
  321. break;
  322. case FHTKEY51:
  323. Hankel = FHTKey51::NewSP();
  324. break;
  325. case QWEKEY:
  326. Hankel = QWEKey::NewSP();
  327. break;
  328. default:
  329. std::cerr << "Hankel transform cannot be created\n";
  330. exit(EXIT_FAILURE);
  331. }
  332. #ifdef LEMMAUSEOMP
  333. #pragma omp for schedule(static, 1)
  334. #endif
  335. for (int ifreq=0; ifreq<Antenna->GetNumberOfFrequencies(); ++ifreq) {
  336. for (int idip=0; idip<Antenna->GetNumberOfDipoles(); ++idip) {
  337. auto tDipole = Antenna->GetDipoleSource(idip);
  338. // Propogation constant in free space
  339. Real wavef = tDipole->GetAngularFrequency(ifreq) *
  340. std::sqrt(MU0*EPSILON0);
  341. SolveSingleTxRxPair(irec, Hankel.get(), wavef, ifreq, tDipole.get());
  342. } // dipole loop
  343. } // frequency loop
  344. } // OMP_PARALLEL BLOCK
  345. } // mask loop
  346. #ifdef HAVE_BOOST_PROGRESS
  347. //if (Receivers->GetNumberOfPoints() > 100) {
  348. // ++ disp;
  349. //}
  350. #endif
  351. } // receiver loop
  352. //std::cout << "End freq parallel " << std::endl;
  353. } // Frequency Parallel
  354. else {
  355. //std::cout << "parallel across #3 " << std::endl;
  356. for (int irec=0; irec<Receivers->GetNumberOfPoints(); ++irec) {
  357. if (!Receivers->GetMask(irec)) {
  358. static_cast<PolygonalWireAntenna*>(Antenna.get())->ApproximateWithElectricDipoles(Receivers->GetLocation(irec));
  359. // std::cout << "Not Masked " << std::endl;
  360. // std::cout << "n Freqs " << Antenna->GetNumberOfFrequencies() << std::endl;
  361. // std::cout << "n Dipoles " << Antenna->GetNumberOfDipoles() << std::endl;
  362. // if ( !Antenna->GetNumberOfDipoles() ) {
  363. // std::cout << "NO DIPOLES!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
  364. // // std::cout << "rec location " << Receivers->GetLocation(irec) << std::endl;
  365. // // }
  366. #ifdef LEMMAUSEOMP
  367. #pragma omp parallel
  368. #endif
  369. { // OpenMP Parallel Block
  370. std::shared_ptr<HankelTransform> Hankel;
  371. switch (HankelType) {
  372. case ANDERSON801:
  373. Hankel = FHTAnderson801::NewSP();
  374. break;
  375. case CHAVE:
  376. Hankel = GQChave::NewSP();
  377. break;
  378. case FHTKEY201:
  379. Hankel = FHTKey201::NewSP();
  380. break;
  381. case FHTKEY101:
  382. Hankel = FHTKey101::NewSP();
  383. break;
  384. case FHTKEY51:
  385. Hankel = FHTKey51::NewSP();
  386. break;
  387. case QWEKEY:
  388. Hankel = QWEKey::NewSP();
  389. break;
  390. default:
  391. std::cerr << "Hankel transform cannot be created\n";
  392. exit(EXIT_FAILURE);
  393. }
  394. for (int ifreq=0; ifreq<Antenna->GetNumberOfFrequencies(); ++ifreq) {
  395. #ifdef LEMMAUSEOMP
  396. #pragma omp for schedule(static, 1)
  397. #endif
  398. for (int idip=0; idip<Antenna->GetNumberOfDipoles(); ++idip) {
  399. //#pragma omp critical
  400. //{
  401. //cout << "idip=" << idip << "\tthread num=" << omp_get_thread_num() << '\n';
  402. //}
  403. auto tDipole = Antenna->GetDipoleSource(idip);
  404. // Propogation constant in free space
  405. Real wavef = tDipole->GetAngularFrequency(ifreq) *
  406. std::sqrt(MU0*EPSILON0);
  407. SolveSingleTxRxPair(irec, Hankel.get(), wavef, ifreq, tDipole.get());
  408. } // dipole loop
  409. } // frequency loop
  410. } // OMP_PARALLEL BLOCK
  411. } // mask loop
  412. #ifdef HAVE_BOOST_PROGRESS
  413. //if (Receivers->GetNumberOfPoints() > 100) {
  414. // ++ disp;
  415. //}
  416. #endif
  417. } // receiver loop
  418. } // Polygonal parallel logic
  419. } else {
  420. std::cerr << "Lemma with WireAntenna class is currently broken"
  421. << " fix or use PolygonalWireAntenna\n" << std::endl;
  422. exit(EXIT_FAILURE);
  423. // TODO, getting wrong answer, curiously worKernel->GetKs() with MakeCalc, maybe
  424. // a threading issue, use SolveSingleTxRxPair maype instead of call
  425. // to MakeCalc3? !!!
  426. for (int idip=0; idip<Antenna->GetNumberOfDipoles(); ++idip) {
  427. this->Dipole = Antenna->GetDipoleSource(idip);
  428. MakeCalc3();
  429. //++disp;
  430. }
  431. this->Dipole = nullptr;
  432. }
  433. #ifdef HAVE_BOOST_PROGRESS
  434. if (progressbar) {
  435. delete disp;
  436. }
  437. #endif
  438. }
  439. #ifdef KIHALEE_EM1D
  440. void EMEarth1D::MakeCalc() {
  441. int itype; // 1 = elec, 2 = mag
  442. switch (this->Dipole->GetDipoleSourceType()) {
  443. case (GROUNDEDELECTRICDIPOLE) :
  444. itype = 1;
  445. break;
  446. case (MAGNETICDIPOLE) :
  447. itype = 2;
  448. break;
  449. case (UNGROUNDEDELECTRICDIPOLE) :
  450. std::cerr << "Fortran routine cannot calculate ungrounded"
  451. "electric dipole\n";
  452. default:
  453. throw NonValidDipoleType();
  454. }
  455. int ipol ;
  456. Vector3r Pol = this->Dipole->GetPolarisation();
  457. if (std::abs(Pol[0]-1) < 1e-5) {
  458. ipol = 1;
  459. } else if (std::abs(Pol[1]-1) < 1e-5) {
  460. ipol = 2;
  461. } else if (std::abs(Pol[2]-1) < 1e-5) {
  462. ipol = 3;
  463. } else {
  464. std::cerr << "Fortran routine cannot calculate arbitrary "
  465. "dipole polarisation, set to x, y, or z\n";
  466. }
  467. int nlay = Earth->GetNumberOfNonAirLayers();
  468. if (nlay > MAXLAYERS) {
  469. std::cerr << "FORTRAN CODE CAN ONLY HANDLE " << MAXLAYERS
  470. << " LAYERS\n";
  471. throw EarthModelWithMoreThanMaxLayers();
  472. }
  473. int nfreq = 1; // number of freqs
  474. int nfield; // field output 1 = elec, 2 = mag, 3 = both
  475. switch (FieldsToCalculate) {
  476. case E:
  477. nfield = 1;
  478. break;
  479. case H:
  480. nfield = 2;
  481. break;
  482. case BOTH:
  483. nfield = 3;
  484. break;
  485. default:
  486. throw 7;
  487. }
  488. int nres = Receivers->GetNumberOfPoints();
  489. int jtype = 3; // form ouf output,
  490. // 1 = horizontal,
  491. // 2 = down hole,
  492. // 3 = freq sounding
  493. // 4 = down hole logging
  494. int jgamma = 0; // Units 0 = MKS (H->A/m and E->V/m)
  495. // 1 = h->Gammas E->V/m
  496. double acc = 0.; // Tolerance
  497. // TODO, fix FORTRAN calls so these arrays can be nlay long, not
  498. // MAXLAYERS.
  499. // Model Parameters
  500. double *dep = new double[MAXLAYERS];
  501. dep[0] = 0.; // We always say air starts at 0
  502. for (int ilay=1; ilay<Earth->GetNumberOfLayers(); ++ilay) {
  503. dep[ilay] = dep[ilay-1] + Earth->GetLayerThickness(ilay);
  504. //std::cout << "Depth " << dep[ilay] << std::endl;
  505. }
  506. std::complex<double> *sig = new std::complex<double> [MAXLAYERS];
  507. for (int ilay=1; ilay<=nlay; ++ilay) {
  508. sig[ilay-1] = (std::complex<double>)(Earth->GetLayerConductivity(ilay));
  509. }
  510. // TODO, pass these into Fortran call, and return Cole-Cole model
  511. // parameters. Right now this does nothing
  512. //std::complex<double> *sus = new std::complex<double>[MAXLAYERS];
  513. //std::complex<double> *epr = new std::complex<double>[MAXLAYERS];
  514. // Cole-Cole model stuff
  515. double *susl = new double[MAXLAYERS];
  516. for (int ilay=1; ilay<=nlay; ++ilay) {
  517. susl[ilay-1] = Earth->GetLayerLowFreqSusceptibility(ilay);
  518. }
  519. double *sush = new double[MAXLAYERS];
  520. for (int ilay=1; ilay<=nlay; ++ilay) {
  521. sush[ilay-1] = Earth->GetLayerHighFreqSusceptibility(ilay);
  522. }
  523. double *sustau = new double[MAXLAYERS];
  524. for (int ilay=1; ilay<=nlay; ++ilay) {
  525. sustau[ilay-1] = Earth->GetLayerTauSusceptibility(ilay);
  526. }
  527. double *susalp = new double[MAXLAYERS];
  528. for (int ilay=1; ilay<=nlay; ++ilay) {
  529. susalp[ilay-1] = Earth->GetLayerBreathSusceptibility(ilay);
  530. }
  531. double *eprl = new double[MAXLAYERS];
  532. for (int ilay=1; ilay<=nlay; ++ilay) {
  533. eprl[ilay-1] = Earth->GetLayerLowFreqPermitivity(ilay);
  534. }
  535. double *eprh = new double[MAXLAYERS];
  536. for (int ilay=1; ilay<=nlay; ++ilay) {
  537. eprh[ilay-1] = Earth->GetLayerHighFreqPermitivity(ilay);
  538. }
  539. double *eprtau = new double[MAXLAYERS];
  540. for (int ilay=1; ilay<=nlay; ++ilay) {
  541. eprtau[ilay-1] = Earth->GetLayerTauPermitivity(ilay);
  542. }
  543. double *epralp = new double[MAXLAYERS];
  544. for (int ilay=1; ilay<=nlay; ++ilay) {
  545. epralp[ilay-1] = Earth->GetLayerBreathPermitivity(ilay);
  546. }
  547. // Freq stuff
  548. double finit = Dipole->GetFrequency(0); //(1000); // Starting freq
  549. double flimit = Dipole->GetFrequency(0); //(1000); // max freq
  550. double dlimit = Dipole->GetFrequency(0); //(1000); // difusion limit
  551. double lfinc(1); // no. freq per decade
  552. // tx location jtype != 4
  553. double txx = Dipole->GetLocation(0); // (0.);
  554. double txy = Dipole->GetLocation(1); // (0.);
  555. double txz = Dipole->GetLocation(2); // (0.);
  556. // rx position
  557. // TODO, fix Fortran program to not waste this memory
  558. // maybe
  559. const int MAXREC = 15;
  560. double *rxx = new double [MAXREC];
  561. double *rxy = new double [MAXREC];
  562. double *rxz = new double [MAXREC];
  563. std::complex<double> *ex = new std::complex<double>[MAXREC];
  564. std::complex<double> *ey = new std::complex<double>[MAXREC];
  565. std::complex<double> *ez = new std::complex<double>[MAXREC];
  566. std::complex<double> *hx = new std::complex<double>[MAXREC];
  567. std::complex<double> *hy = new std::complex<double>[MAXREC];
  568. std::complex<double> *hz = new std::complex<double>[MAXREC];
  569. int nres2 = MAXREC;
  570. int ii=0;
  571. for (ii=0; ii<nres-MAXREC; ii+=MAXREC) {
  572. for (int ir=0; ir<MAXREC; ++ir) {
  573. //Vector3r pos = Receivers->GetLocation(ii+ir);
  574. rxx[ir] = Receivers->GetLocation(ii+ir)[0];
  575. rxy[ir] = Receivers->GetLocation(ii+ir)[1];
  576. rxz[ir] = Receivers->GetLocation(ii+ir)[2];
  577. }
  578. em1dcall_(itype, ipol, nlay, nfreq, nfield, nres2, jtype,
  579. jgamma, acc, dep, sig, susl, sush, sustau, susalp,
  580. eprl, eprh, eprtau, epralp, finit, flimit, dlimit,
  581. lfinc, txx, txy, txz, rxx, rxy, rxz, ex, ey, ez,
  582. hx, hy, hz);
  583. // Scale By Moment
  584. for (int ir=0; ir<MAXREC; ++ir) {
  585. ex[ir] *= Dipole->GetMoment();
  586. ey[ir] *= Dipole->GetMoment();
  587. ez[ir] *= Dipole->GetMoment();
  588. hx[ir] *= Dipole->GetMoment();
  589. hy[ir] *= Dipole->GetMoment();
  590. hz[ir] *= Dipole->GetMoment();
  591. // Append values instead of setting them
  592. this->Receivers->AppendEfield(0, ii+ir, (Complex)(ex[ir]),
  593. (Complex)(ey[ir]),
  594. (Complex)(ez[ir]) );
  595. this->Receivers->AppendHfield(0, ii+ir, (Complex)(hx[ir]),
  596. (Complex)(hy[ir]),
  597. (Complex)(hz[ir]) );
  598. }
  599. }
  600. //ii += MAXREC;
  601. nres2 = 0;
  602. // Perform last positions
  603. for (int ir=0; ir<nres-ii; ++ir) {
  604. rxx[ir] = Receivers->GetLocation(ii+ir)[0];
  605. rxy[ir] = Receivers->GetLocation(ii+ir)[1];
  606. rxz[ir] = Receivers->GetLocation(ii+ir)[2];
  607. ++nres2;
  608. }
  609. em1dcall_(itype, ipol, nlay, nfreq, nfield, nres2, jtype,
  610. jgamma, acc, dep, sig, susl, sush, sustau, susalp,
  611. eprl, eprh, eprtau, epralp, finit, flimit, dlimit,
  612. lfinc, txx, txy, txz, rxx, rxy, rxz, ex, ey, ez,
  613. hx, hy, hz);
  614. // Scale By Moment
  615. for (int ir=0; ir<nres-ii; ++ir) {
  616. ex[ir] *= Dipole->GetMoment();
  617. ey[ir] *= Dipole->GetMoment();
  618. ez[ir] *= Dipole->GetMoment();
  619. hx[ir] *= Dipole->GetMoment();
  620. hy[ir] *= Dipole->GetMoment();
  621. hz[ir] *= Dipole->GetMoment();
  622. // Append values instead of setting them
  623. this->Receivers->AppendEfield(0, ii+ir, (Complex)(ex[ir]),
  624. (Complex)(ey[ir]),
  625. (Complex)(ez[ir]) );
  626. this->Receivers->AppendHfield(0, ii+ir, (Complex)(hx[ir]),
  627. (Complex)(hy[ir]),
  628. (Complex)(hz[ir]) );
  629. }
  630. delete [] sig;
  631. delete [] dep;
  632. //delete [] sus;
  633. //delete [] epr;
  634. delete [] susl;
  635. delete [] sush;
  636. delete [] susalp;
  637. delete [] sustau;
  638. delete [] eprl;
  639. delete [] eprh;
  640. delete [] epralp;
  641. delete [] eprtau;
  642. delete [] rxx;
  643. delete [] rxy;
  644. delete [] rxz;
  645. delete [] ex;
  646. delete [] ey;
  647. delete [] ez;
  648. delete [] hx;
  649. delete [] hy;
  650. delete [] hz;
  651. }
  652. #endif
  653. void EMEarth1D::SolveSingleTxRxPair (const int &irec, HankelTransform *Hankel, const Real &wavef, const int &ifreq,
  654. DipoleSource *tDipole) {
  655. ++icalcinner;
  656. Real rho = (Receivers->GetLocation(irec).head<2>() - tDipole->GetLocation().head<2>()).norm();
  657. tDipole->SetKernels(ifreq, FieldsToCalculate, Receivers, irec, Earth);
  658. Hankel->ComputeRelated( rho, tDipole->GetKernelManager() );
  659. tDipole->UpdateFields( ifreq, Hankel, wavef );
  660. }
  661. // void EMEarth1D::SolveSingleTxRxPair (const int &irec, std::shared_ptr<HankelTransform> Hankel, const Real &wavef, const int &ifreq,
  662. // std::shared_ptr<DipoleSource> tDipole) {
  663. // ++icalcinner;
  664. // Real rho = (Receivers->GetLocation(irec).head<2>() - tDipole->GetLocation().head<2>()).norm();
  665. // tDipole->SetKernels(ifreq, FieldsToCalculate, Receivers, irec, Earth);
  666. // //Hankel->ComputeRelated( rho, tDipole->GetKernelManager() );
  667. // //tDipole->UpdateFields( ifreq, Hankel, wavef );
  668. // }
  669. void EMEarth1D::SolveLaggedTxRxPair(const int &irec, HankelTransform* Hankel,
  670. const Real &wavef, const int &ifreq, PolygonalWireAntenna* antenna) {
  671. antenna->ApproximateWithElectricDipoles(Receivers->GetLocation(irec));
  672. // Determine the min and max arguments
  673. Real rhomin = 1e9;
  674. Real rhomax = 1e-9;
  675. for (int idip=0; idip<antenna->GetNumberOfDipoles(); ++idip) {
  676. auto tDipole = antenna->GetDipoleSource(idip);
  677. Real rho = (Receivers->GetLocation(irec).head<2>() - tDipole->GetLocation().head<2>()).norm();
  678. rhomin = std::min(rhomin, rho);
  679. rhomax = std::max(rhomax, rho);
  680. }
  681. // Determine number of lagged convolutions to do
  682. int nlag = 1; // (Key==0) We need an extra for some reason for stability? Maybe in Spline?
  683. Real lrho ( 1.0 * rhomax );
  684. while ( lrho > rhomin ) {
  685. nlag += 1;
  686. lrho *= Hankel->GetABSER();
  687. }
  688. auto tDipole = antenna->GetDipoleSource(0);
  689. tDipole->SetKernels(ifreq, FieldsToCalculate, Receivers, irec, Earth);
  690. // Instead we should pass the antenna into this so that Hankel hass all the rho arguments...
  691. Hankel->ComputeLaggedRelated( 1.0*rhomax, nlag, tDipole->GetKernelManager() );
  692. // Sort the dipoles by rho
  693. for (int idip=0; idip<antenna->GetNumberOfDipoles(); ++idip) {
  694. auto tDipole = antenna->GetDipoleSource(idip);
  695. tDipole->SetKernels(ifreq, FieldsToCalculate, Receivers, irec, Earth);
  696. // Pass Hankel2 a message here so it knows which one to return in Zgauss!
  697. Real rho = (Receivers->GetLocation(irec).head<2>() - tDipole->GetLocation().head<2>()).norm();
  698. Hankel->SetLaggedArg( rho );
  699. tDipole->UpdateFields( ifreq, Hankel, wavef );
  700. }
  701. }
  702. //////////////////////////////////////////////////////////
  703. // Thread safe OO Reimplimentation of KiHand's
  704. // EM1DNEW.for programme
  705. void EMEarth1D::MakeCalc3() {
  706. if ( Dipole == nullptr ) throw NullDipoleSource();
  707. if (Earth == nullptr) throw NullEarth();
  708. if (Receivers == nullptr) throw NullReceivers();
  709. #ifdef LEMMAUSEOMP
  710. #pragma omp parallel
  711. #endif
  712. { // OpenMP Parallel Block
  713. #ifdef LEMMAUSEOMP
  714. int tid = omp_get_thread_num();
  715. int nthreads = omp_get_num_threads();
  716. #else
  717. int tid=0;
  718. int nthreads=1;
  719. #endif
  720. auto tDipole = Dipole->Clone();
  721. std::shared_ptr<HankelTransform> Hankel;
  722. switch (HankelType) {
  723. case ANDERSON801:
  724. Hankel = FHTAnderson801::NewSP();
  725. break;
  726. case CHAVE:
  727. Hankel = GQChave::NewSP();
  728. break;
  729. case FHTKEY201:
  730. Hankel = FHTKey201::NewSP();
  731. break;
  732. case FHTKEY101:
  733. Hankel = FHTKey101::NewSP();
  734. break;
  735. case FHTKEY51:
  736. Hankel = FHTKey51::NewSP();
  737. break;
  738. case QWEKEY:
  739. Hankel = QWEKey::NewSP();
  740. break;
  741. default:
  742. std::cerr << "Hankel transform cannot be created\n";
  743. exit(EXIT_FAILURE);
  744. }
  745. if ( tDipole->GetNumberOfFrequencies() < Receivers->GetNumberOfPoints() ) {
  746. for (int ifreq=0; ifreq<tDipole->GetNumberOfFrequencies(); ++ifreq) {
  747. // Propogation constant in free space being input to Hankel
  748. Real wavef = tDipole->GetAngularFrequency(ifreq) * std::sqrt(MU0*EPSILON0);
  749. for (int irec=tid; irec<Receivers->GetNumberOfPoints(); irec+=nthreads) {
  750. SolveSingleTxRxPair(irec, Hankel.get(), wavef, ifreq, tDipole.get());
  751. }
  752. }
  753. } else {
  754. for (int irec=0; irec<Receivers->GetNumberOfPoints(); ++irec) {
  755. for (int ifreq=tid; ifreq<tDipole->GetNumberOfFrequencies(); ifreq+=nthreads) {
  756. // Propogation constant in free space being input to Hankel
  757. Real wavef = tDipole->GetAngularFrequency(ifreq) * std::sqrt(MU0*EPSILON0);
  758. SolveSingleTxRxPair(irec, Hankel.get(), wavef, ifreq, tDipole.get());
  759. }
  760. }
  761. }
  762. } // OpenMP Parallel Block
  763. }
  764. NullReceivers::NullReceivers() :
  765. runtime_error("nullptr RECEIVERS") {}
  766. NullAntenna::NullAntenna() :
  767. runtime_error("nullptr ANTENNA") {}
  768. NullInstrument::NullInstrument(LemmaObject* ptr) :
  769. runtime_error("nullptr INSTRUMENT") {
  770. std::cout << "Thrown by instance of "
  771. << ptr->GetName() << std::endl;
  772. }
  773. DipoleSourceSpecifiedForWireAntennaCalc::
  774. DipoleSourceSpecifiedForWireAntennaCalc() :
  775. runtime_error("DIPOLE SOURCE SPECIFIED FOR WIRE ANTENNA CALC"){}
  776. } // end of Lemma Namespace