Lemma is an Electromagnetics API
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

EMEarth1D.cpp 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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 )) {
  200. #ifdef HAVE_BOOST_PROGRESS
  201. if (progressbar) {
  202. disp = new boost::progress_display( Receivers->GetNumberOfPoints()*Antenna->GetNumberOfFrequencies() );
  203. }
  204. #endif
  205. for (int ifreq=0; ifreq<Antenna->GetNumberOfFrequencies();++ifreq) {
  206. Real wavef = 2.*PI* Antenna->GetFrequency(ifreq);
  207. #ifdef LEMMAUSEOMP
  208. #pragma omp parallel
  209. {
  210. #endif
  211. auto Hankel = HankelTransformFactory::NewSP( HankelType );
  212. #ifdef LEMMAUSEOMP
  213. #pragma omp for schedule(static, 1)
  214. #endif
  215. for (int irec=0; irec<Receivers->GetNumberOfPoints(); ++irec) {
  216. auto AntCopy = static_cast<PolygonalWireAntenna*>(Antenna.get())->ClonePA();
  217. SolveLaggedTxRxPair(irec, Hankel.get(), wavef, ifreq, AntCopy.get());
  218. #ifdef HAVE_BOOST_PROGRESS
  219. if (progressbar) ++(*disp);
  220. #endif
  221. }
  222. #pragma omp barrier
  223. #ifdef LEMMAUSEOMP
  224. }
  225. #endif
  226. }
  227. } else
  228. if (Receivers->GetNumberOfPoints() > Antenna->GetNumberOfFrequencies()) {
  229. //std::cout << "freq parallel #1" << std::endl;
  230. //** Progress display bar for long calculations */
  231. #ifdef HAVE_BOOST_PROGRESS
  232. if (progressbar) {
  233. disp = new boost::progress_display( Receivers->GetNumberOfPoints()*Antenna->GetNumberOfFrequencies() );
  234. }
  235. #endif
  236. // parallelise across receivers
  237. #ifdef LEMMAUSEOMP
  238. #pragma omp parallel
  239. #endif
  240. { // OpenMP Parallel Block
  241. // Since these antennas change we need a local copy for each
  242. // thread.
  243. auto AntCopy = static_cast<PolygonalWireAntenna*>(Antenna.get())->ClonePA();
  244. std::shared_ptr<HankelTransform> Hankel;
  245. switch (HankelType) {
  246. case ANDERSON801:
  247. Hankel = FHTAnderson801::NewSP();
  248. break;
  249. case CHAVE:
  250. Hankel = GQChave::NewSP();
  251. break;
  252. case FHTKEY201:
  253. Hankel = FHTKey201::NewSP();
  254. break;
  255. case FHTKEY101:
  256. Hankel = FHTKey101::NewSP();
  257. break;
  258. case FHTKEY51:
  259. Hankel = FHTKey51::NewSP();
  260. break;
  261. case QWEKEY:
  262. Hankel = QWEKey::NewSP();
  263. break;
  264. default:
  265. std::cerr << "Hankel transform cannot be created\n";
  266. exit(EXIT_FAILURE);
  267. }
  268. //for (int irec=tid; irec<Receivers->GetNumberOfPoints(); irec+=nthreads) {
  269. #ifdef LEMMAUSEOMP
  270. #pragma omp for schedule(static, 1) //nowait
  271. #endif
  272. for (int irec=0; irec<Receivers->GetNumberOfPoints(); ++irec) {
  273. if (!Receivers->GetMask(irec)) {
  274. AntCopy->ApproximateWithElectricDipoles(Receivers->GetLocation(irec));
  275. for (int idip=0; idip<AntCopy->GetNumberOfDipoles(); ++idip) {
  276. auto tDipole = AntCopy->GetDipoleSource(idip);
  277. //#ifdef LEMMAUSEOMP
  278. //#pragma omp for schedule(static, 1)
  279. //#endif
  280. for (int ifreq=0; ifreq<tDipole->GetNumberOfFrequencies();
  281. ++ifreq) {
  282. // Propogation constant in free space
  283. Real wavef = tDipole->GetAngularFrequency(ifreq) *
  284. std::sqrt(MU0*EPSILON0);
  285. SolveSingleTxRxPair(irec, Hankel.get(), wavef, ifreq, tDipole.get());
  286. } // freq loop
  287. } // dipole loop
  288. } // mask
  289. //std::cout << "Normal Path\n";
  290. //std::cout << Receivers->GetHfield(0, irec) << std::endl;
  291. //if (irec == 1) exit(0);
  292. #ifdef HAVE_BOOST_PROGRESS
  293. if (progressbar) ++(*disp);
  294. #endif
  295. } // receiver loop
  296. } // OMP_PARALLEL BLOCK
  297. } else if (Antenna->GetNumberOfFrequencies() > 8) {
  298. // parallel across frequencies
  299. //std::cout << "freq parallel #2" << std::endl;
  300. for (int irec=0; irec<Receivers->GetNumberOfPoints(); ++irec) {
  301. if (!Receivers->GetMask(irec)) {
  302. static_cast<PolygonalWireAntenna*>(Antenna.get())->ApproximateWithElectricDipoles(Receivers->GetLocation(irec));
  303. #ifdef LEMMAUSEOMP
  304. #pragma omp parallel
  305. #endif
  306. { // OpenMP Parallel Block
  307. std::shared_ptr<HankelTransform> Hankel;
  308. switch (HankelType) {
  309. case ANDERSON801:
  310. Hankel = FHTAnderson801::NewSP();
  311. break;
  312. case CHAVE:
  313. Hankel = GQChave::NewSP();
  314. break;
  315. case FHTKEY201:
  316. Hankel = FHTKey201::NewSP();
  317. break;
  318. case FHTKEY101:
  319. Hankel = FHTKey101::NewSP();
  320. break;
  321. case FHTKEY51:
  322. Hankel = FHTKey51::NewSP();
  323. break;
  324. case QWEKEY:
  325. Hankel = QWEKey::NewSP();
  326. break;
  327. default:
  328. std::cerr << "Hankel transform cannot be created\n";
  329. exit(EXIT_FAILURE);
  330. }
  331. #ifdef LEMMAUSEOMP
  332. #pragma omp for schedule(static, 1)
  333. #endif
  334. for (int ifreq=0; ifreq<Antenna->GetNumberOfFrequencies(); ++ifreq) {
  335. for (int idip=0; idip<Antenna->GetNumberOfDipoles(); ++idip) {
  336. auto tDipole = Antenna->GetDipoleSource(idip);
  337. // Propogation constant in free space
  338. Real wavef = tDipole->GetAngularFrequency(ifreq) *
  339. std::sqrt(MU0*EPSILON0);
  340. SolveSingleTxRxPair(irec, Hankel.get(), wavef, ifreq, tDipole.get());
  341. } // dipole loop
  342. } // frequency loop
  343. } // OMP_PARALLEL BLOCK
  344. } // mask loop
  345. #ifdef HAVE_BOOST_PROGRESS
  346. //if (Receivers->GetNumberOfPoints() > 100) {
  347. // ++ disp;
  348. //}
  349. #endif
  350. } // receiver loop
  351. //std::cout << "End freq parallel " << std::endl;
  352. } // Frequency Parallel
  353. else {
  354. //std::cout << "parallel across #3 " << std::endl;
  355. for (int irec=0; irec<Receivers->GetNumberOfPoints(); ++irec) {
  356. if (!Receivers->GetMask(irec)) {
  357. static_cast<PolygonalWireAntenna*>(Antenna.get())->ApproximateWithElectricDipoles(Receivers->GetLocation(irec));
  358. // std::cout << "Not Masked " << std::endl;
  359. // std::cout << "n Freqs " << Antenna->GetNumberOfFrequencies() << std::endl;
  360. // std::cout << "n Dipoles " << Antenna->GetNumberOfDipoles() << std::endl;
  361. // if ( !Antenna->GetNumberOfDipoles() ) {
  362. // std::cout << "NO DIPOLES!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
  363. // // std::cout << "rec location " << Receivers->GetLocation(irec) << std::endl;
  364. // // }
  365. #ifdef LEMMAUSEOMP
  366. #pragma omp parallel
  367. #endif
  368. { // OpenMP Parallel Block
  369. std::shared_ptr<HankelTransform> Hankel;
  370. switch (HankelType) {
  371. case ANDERSON801:
  372. Hankel = FHTAnderson801::NewSP();
  373. break;
  374. case CHAVE:
  375. Hankel = GQChave::NewSP();
  376. break;
  377. case FHTKEY201:
  378. Hankel = FHTKey201::NewSP();
  379. break;
  380. case FHTKEY101:
  381. Hankel = FHTKey101::NewSP();
  382. break;
  383. case FHTKEY51:
  384. Hankel = FHTKey51::NewSP();
  385. break;
  386. case QWEKEY:
  387. Hankel = QWEKey::NewSP();
  388. break;
  389. default:
  390. std::cerr << "Hankel transform cannot be created\n";
  391. exit(EXIT_FAILURE);
  392. }
  393. for (int ifreq=0; ifreq<Antenna->GetNumberOfFrequencies(); ++ifreq) {
  394. #ifdef LEMMAUSEOMP
  395. #pragma omp for schedule(static, 1)
  396. #endif
  397. for (int idip=0; idip<Antenna->GetNumberOfDipoles(); ++idip) {
  398. //#pragma omp critical
  399. //{
  400. //cout << "idip=" << idip << "\tthread num=" << omp_get_thread_num() << '\n';
  401. //}
  402. auto tDipole = Antenna->GetDipoleSource(idip);
  403. // Propogation constant in free space
  404. Real wavef = tDipole->GetAngularFrequency(ifreq) *
  405. std::sqrt(MU0*EPSILON0);
  406. SolveSingleTxRxPair(irec, Hankel.get(), wavef, ifreq, tDipole.get());
  407. } // dipole loop
  408. } // frequency loop
  409. } // OMP_PARALLEL BLOCK
  410. } // mask loop
  411. #ifdef HAVE_BOOST_PROGRESS
  412. //if (Receivers->GetNumberOfPoints() > 100) {
  413. // ++ disp;
  414. //}
  415. #endif
  416. } // receiver loop
  417. } // Polygonal parallel logic
  418. } else {
  419. std::cerr << "Lemma with WireAntenna class is currently broken"
  420. << " fix or use PolygonalWireAntenna\n" << std::endl;
  421. exit(EXIT_FAILURE);
  422. // TODO, getting wrong answer, curiously worKernel->GetKs() with MakeCalc, maybe
  423. // a threading issue, use SolveSingleTxRxPair maype instead of call
  424. // to MakeCalc3? !!!
  425. for (int idip=0; idip<Antenna->GetNumberOfDipoles(); ++idip) {
  426. this->Dipole = Antenna->GetDipoleSource(idip);
  427. MakeCalc3();
  428. //++disp;
  429. }
  430. this->Dipole = nullptr;
  431. }
  432. #ifdef HAVE_BOOST_PROGRESS
  433. if (progressbar) {
  434. delete disp;
  435. }
  436. #endif
  437. }
  438. #ifdef KIHALEE_EM1D
  439. void EMEarth1D::MakeCalc() {
  440. int itype; // 1 = elec, 2 = mag
  441. switch (this->Dipole->GetDipoleSourceType()) {
  442. case (GROUNDEDELECTRICDIPOLE) :
  443. itype = 1;
  444. break;
  445. case (MAGNETICDIPOLE) :
  446. itype = 2;
  447. break;
  448. case (UNGROUNDEDELECTRICDIPOLE) :
  449. std::cerr << "Fortran routine cannot calculate ungrounded"
  450. "electric dipole\n";
  451. default:
  452. throw NonValidDipoleType();
  453. }
  454. int ipol ;
  455. Vector3r Pol = this->Dipole->GetPolarisation();
  456. if (std::abs(Pol[0]-1) < 1e-5) {
  457. ipol = 1;
  458. } else if (std::abs(Pol[1]-1) < 1e-5) {
  459. ipol = 2;
  460. } else if (std::abs(Pol[2]-1) < 1e-5) {
  461. ipol = 3;
  462. } else {
  463. std::cerr << "Fortran routine cannot calculate arbitrary "
  464. "dipole polarisation, set to x, y, or z\n";
  465. }
  466. int nlay = Earth->GetNumberOfNonAirLayers();
  467. if (nlay > MAXLAYERS) {
  468. std::cerr << "FORTRAN CODE CAN ONLY HANDLE " << MAXLAYERS
  469. << " LAYERS\n";
  470. throw EarthModelWithMoreThanMaxLayers();
  471. }
  472. int nfreq = 1; // number of freqs
  473. int nfield; // field output 1 = elec, 2 = mag, 3 = both
  474. switch (FieldsToCalculate) {
  475. case E:
  476. nfield = 1;
  477. break;
  478. case H:
  479. nfield = 2;
  480. break;
  481. case BOTH:
  482. nfield = 3;
  483. break;
  484. default:
  485. throw 7;
  486. }
  487. int nres = Receivers->GetNumberOfPoints();
  488. int jtype = 3; // form ouf output,
  489. // 1 = horizontal,
  490. // 2 = down hole,
  491. // 3 = freq sounding
  492. // 4 = down hole logging
  493. int jgamma = 0; // Units 0 = MKS (H->A/m and E->V/m)
  494. // 1 = h->Gammas E->V/m
  495. double acc = 0.; // Tolerance
  496. // TODO, fix FORTRAN calls so these arrays can be nlay long, not
  497. // MAXLAYERS.
  498. // Model Parameters
  499. double *dep = new double[MAXLAYERS];
  500. dep[0] = 0.; // We always say air starts at 0
  501. for (int ilay=1; ilay<Earth->GetNumberOfLayers(); ++ilay) {
  502. dep[ilay] = dep[ilay-1] + Earth->GetLayerThickness(ilay);
  503. //std::cout << "Depth " << dep[ilay] << std::endl;
  504. }
  505. std::complex<double> *sig = new std::complex<double> [MAXLAYERS];
  506. for (int ilay=1; ilay<=nlay; ++ilay) {
  507. sig[ilay-1] = (std::complex<double>)(Earth->GetLayerConductivity(ilay));
  508. }
  509. // TODO, pass these into Fortran call, and return Cole-Cole model
  510. // parameters. Right now this does nothing
  511. //std::complex<double> *sus = new std::complex<double>[MAXLAYERS];
  512. //std::complex<double> *epr = new std::complex<double>[MAXLAYERS];
  513. // Cole-Cole model stuff
  514. double *susl = new double[MAXLAYERS];
  515. for (int ilay=1; ilay<=nlay; ++ilay) {
  516. susl[ilay-1] = Earth->GetLayerLowFreqSusceptibility(ilay);
  517. }
  518. double *sush = new double[MAXLAYERS];
  519. for (int ilay=1; ilay<=nlay; ++ilay) {
  520. sush[ilay-1] = Earth->GetLayerHighFreqSusceptibility(ilay);
  521. }
  522. double *sustau = new double[MAXLAYERS];
  523. for (int ilay=1; ilay<=nlay; ++ilay) {
  524. sustau[ilay-1] = Earth->GetLayerTauSusceptibility(ilay);
  525. }
  526. double *susalp = new double[MAXLAYERS];
  527. for (int ilay=1; ilay<=nlay; ++ilay) {
  528. susalp[ilay-1] = Earth->GetLayerBreathSusceptibility(ilay);
  529. }
  530. double *eprl = new double[MAXLAYERS];
  531. for (int ilay=1; ilay<=nlay; ++ilay) {
  532. eprl[ilay-1] = Earth->GetLayerLowFreqPermitivity(ilay);
  533. }
  534. double *eprh = new double[MAXLAYERS];
  535. for (int ilay=1; ilay<=nlay; ++ilay) {
  536. eprh[ilay-1] = Earth->GetLayerHighFreqPermitivity(ilay);
  537. }
  538. double *eprtau = new double[MAXLAYERS];
  539. for (int ilay=1; ilay<=nlay; ++ilay) {
  540. eprtau[ilay-1] = Earth->GetLayerTauPermitivity(ilay);
  541. }
  542. double *epralp = new double[MAXLAYERS];
  543. for (int ilay=1; ilay<=nlay; ++ilay) {
  544. epralp[ilay-1] = Earth->GetLayerBreathPermitivity(ilay);
  545. }
  546. // Freq stuff
  547. double finit = Dipole->GetFrequency(0); //(1000); // Starting freq
  548. double flimit = Dipole->GetFrequency(0); //(1000); // max freq
  549. double dlimit = Dipole->GetFrequency(0); //(1000); // difusion limit
  550. double lfinc(1); // no. freq per decade
  551. // tx location jtype != 4
  552. double txx = Dipole->GetLocation(0); // (0.);
  553. double txy = Dipole->GetLocation(1); // (0.);
  554. double txz = Dipole->GetLocation(2); // (0.);
  555. // rx position
  556. // TODO, fix Fortran program to not waste this memory
  557. // maybe
  558. const int MAXREC = 15;
  559. double *rxx = new double [MAXREC];
  560. double *rxy = new double [MAXREC];
  561. double *rxz = new double [MAXREC];
  562. std::complex<double> *ex = new std::complex<double>[MAXREC];
  563. std::complex<double> *ey = new std::complex<double>[MAXREC];
  564. std::complex<double> *ez = new std::complex<double>[MAXREC];
  565. std::complex<double> *hx = new std::complex<double>[MAXREC];
  566. std::complex<double> *hy = new std::complex<double>[MAXREC];
  567. std::complex<double> *hz = new std::complex<double>[MAXREC];
  568. int nres2 = MAXREC;
  569. int ii=0;
  570. for (ii=0; ii<nres-MAXREC; ii+=MAXREC) {
  571. for (int ir=0; ir<MAXREC; ++ir) {
  572. //Vector3r pos = Receivers->GetLocation(ii+ir);
  573. rxx[ir] = Receivers->GetLocation(ii+ir)[0];
  574. rxy[ir] = Receivers->GetLocation(ii+ir)[1];
  575. rxz[ir] = Receivers->GetLocation(ii+ir)[2];
  576. }
  577. em1dcall_(itype, ipol, nlay, nfreq, nfield, nres2, jtype,
  578. jgamma, acc, dep, sig, susl, sush, sustau, susalp,
  579. eprl, eprh, eprtau, epralp, finit, flimit, dlimit,
  580. lfinc, txx, txy, txz, rxx, rxy, rxz, ex, ey, ez,
  581. hx, hy, hz);
  582. // Scale By Moment
  583. for (int ir=0; ir<MAXREC; ++ir) {
  584. ex[ir] *= Dipole->GetMoment();
  585. ey[ir] *= Dipole->GetMoment();
  586. ez[ir] *= Dipole->GetMoment();
  587. hx[ir] *= Dipole->GetMoment();
  588. hy[ir] *= Dipole->GetMoment();
  589. hz[ir] *= Dipole->GetMoment();
  590. // Append values instead of setting them
  591. this->Receivers->AppendEfield(0, ii+ir, (Complex)(ex[ir]),
  592. (Complex)(ey[ir]),
  593. (Complex)(ez[ir]) );
  594. this->Receivers->AppendHfield(0, ii+ir, (Complex)(hx[ir]),
  595. (Complex)(hy[ir]),
  596. (Complex)(hz[ir]) );
  597. }
  598. }
  599. //ii += MAXREC;
  600. nres2 = 0;
  601. // Perform last positions
  602. for (int ir=0; ir<nres-ii; ++ir) {
  603. rxx[ir] = Receivers->GetLocation(ii+ir)[0];
  604. rxy[ir] = Receivers->GetLocation(ii+ir)[1];
  605. rxz[ir] = Receivers->GetLocation(ii+ir)[2];
  606. ++nres2;
  607. }
  608. em1dcall_(itype, ipol, nlay, nfreq, nfield, nres2, jtype,
  609. jgamma, acc, dep, sig, susl, sush, sustau, susalp,
  610. eprl, eprh, eprtau, epralp, finit, flimit, dlimit,
  611. lfinc, txx, txy, txz, rxx, rxy, rxz, ex, ey, ez,
  612. hx, hy, hz);
  613. // Scale By Moment
  614. for (int ir=0; ir<nres-ii; ++ir) {
  615. ex[ir] *= Dipole->GetMoment();
  616. ey[ir] *= Dipole->GetMoment();
  617. ez[ir] *= Dipole->GetMoment();
  618. hx[ir] *= Dipole->GetMoment();
  619. hy[ir] *= Dipole->GetMoment();
  620. hz[ir] *= Dipole->GetMoment();
  621. // Append values instead of setting them
  622. this->Receivers->AppendEfield(0, ii+ir, (Complex)(ex[ir]),
  623. (Complex)(ey[ir]),
  624. (Complex)(ez[ir]) );
  625. this->Receivers->AppendHfield(0, ii+ir, (Complex)(hx[ir]),
  626. (Complex)(hy[ir]),
  627. (Complex)(hz[ir]) );
  628. }
  629. delete [] sig;
  630. delete [] dep;
  631. //delete [] sus;
  632. //delete [] epr;
  633. delete [] susl;
  634. delete [] sush;
  635. delete [] susalp;
  636. delete [] sustau;
  637. delete [] eprl;
  638. delete [] eprh;
  639. delete [] epralp;
  640. delete [] eprtau;
  641. delete [] rxx;
  642. delete [] rxy;
  643. delete [] rxz;
  644. delete [] ex;
  645. delete [] ey;
  646. delete [] ez;
  647. delete [] hx;
  648. delete [] hy;
  649. delete [] hz;
  650. }
  651. #endif
  652. void EMEarth1D::SolveSingleTxRxPair (const int &irec, HankelTransform *Hankel, const Real &wavef, const int &ifreq,
  653. DipoleSource *tDipole) {
  654. ++icalcinner;
  655. Real rho = (Receivers->GetLocation(irec).head<2>() - tDipole->GetLocation().head<2>()).norm();
  656. tDipole->SetKernels(ifreq, FieldsToCalculate, Receivers, irec, Earth);
  657. Hankel->ComputeRelated( rho, tDipole->GetKernelManager() );
  658. tDipole->UpdateFields( ifreq, Hankel, wavef );
  659. }
  660. // void EMEarth1D::SolveSingleTxRxPair (const int &irec, std::shared_ptr<HankelTransform> Hankel, const Real &wavef, const int &ifreq,
  661. // std::shared_ptr<DipoleSource> tDipole) {
  662. // ++icalcinner;
  663. // Real rho = (Receivers->GetLocation(irec).head<2>() - tDipole->GetLocation().head<2>()).norm();
  664. // tDipole->SetKernels(ifreq, FieldsToCalculate, Receivers, irec, Earth);
  665. // //Hankel->ComputeRelated( rho, tDipole->GetKernelManager() );
  666. // //tDipole->UpdateFields( ifreq, Hankel, wavef );
  667. // }
  668. void EMEarth1D::SolveLaggedTxRxPair(const int &irec, HankelTransform* Hankel,
  669. const Real &wavef, const int &ifreq, PolygonalWireAntenna* antenna) {
  670. antenna->ApproximateWithElectricDipoles(Receivers->GetLocation(irec));
  671. // Determine the min and max arguments
  672. Real rhomin = 1e9;
  673. Real rhomax = 1e-9;
  674. for (int idip=0; idip<antenna->GetNumberOfDipoles(); ++idip) {
  675. auto tDipole = antenna->GetDipoleSource(idip);
  676. Real rho = (Receivers->GetLocation(irec).head<2>() - tDipole->GetLocation().head<2>()).norm();
  677. rhomin = std::min(rhomin, rho);
  678. rhomax = std::max(rhomax, rho);
  679. }
  680. // Determine number of lagged convolutions to do
  681. int nlag = 1; // (Key==0) We need an extra for some reason for stability? Maybe in Spline?
  682. Real lrho ( 1.0 * rhomax );
  683. while ( lrho > rhomin ) {
  684. nlag += 1;
  685. lrho *= Hankel->GetABSER();
  686. }
  687. auto tDipole = antenna->GetDipoleSource(0);
  688. tDipole->SetKernels(ifreq, FieldsToCalculate, Receivers, irec, Earth);
  689. // Instead we should pass the antenna into this so that Hankel hass all the rho arguments...
  690. Hankel->ComputeLaggedRelated( 1.0*rhomax, nlag, tDipole->GetKernelManager() );
  691. // Sort the dipoles by rho
  692. for (int idip=0; idip<antenna->GetNumberOfDipoles(); ++idip) {
  693. auto tDipole = antenna->GetDipoleSource(idip);
  694. tDipole->SetKernels(ifreq, FieldsToCalculate, Receivers, irec, Earth);
  695. // Pass Hankel2 a message here so it knows which one to return in Zgauss!
  696. Real rho = (Receivers->GetLocation(irec).head<2>() - tDipole->GetLocation().head<2>()).norm();
  697. Hankel->SetLaggedArg( rho );
  698. tDipole->UpdateFields( ifreq, Hankel, wavef );
  699. }
  700. }
  701. //////////////////////////////////////////////////////////
  702. // Thread safe OO Reimplimentation of KiHand's
  703. // EM1DNEW.for programme
  704. void EMEarth1D::MakeCalc3() {
  705. if ( Dipole == nullptr ) throw NullDipoleSource();
  706. if (Earth == nullptr) throw NullEarth();
  707. if (Receivers == nullptr) throw NullReceivers();
  708. #ifdef LEMMAUSEOMP
  709. #pragma omp parallel
  710. #endif
  711. { // OpenMP Parallel Block
  712. #ifdef LEMMAUSEOMP
  713. int tid = omp_get_thread_num();
  714. int nthreads = omp_get_num_threads();
  715. #else
  716. int tid=0;
  717. int nthreads=1;
  718. #endif
  719. auto tDipole = Dipole->Clone();
  720. std::shared_ptr<HankelTransform> Hankel;
  721. switch (HankelType) {
  722. case ANDERSON801:
  723. Hankel = FHTAnderson801::NewSP();
  724. break;
  725. case CHAVE:
  726. Hankel = GQChave::NewSP();
  727. break;
  728. case FHTKEY201:
  729. Hankel = FHTKey201::NewSP();
  730. break;
  731. case FHTKEY101:
  732. Hankel = FHTKey101::NewSP();
  733. break;
  734. case FHTKEY51:
  735. Hankel = FHTKey51::NewSP();
  736. break;
  737. case QWEKEY:
  738. Hankel = QWEKey::NewSP();
  739. break;
  740. default:
  741. std::cerr << "Hankel transform cannot be created\n";
  742. exit(EXIT_FAILURE);
  743. }
  744. if ( tDipole->GetNumberOfFrequencies() < Receivers->GetNumberOfPoints() ) {
  745. for (int ifreq=0; ifreq<tDipole->GetNumberOfFrequencies(); ++ifreq) {
  746. // Propogation constant in free space being input to Hankel
  747. Real wavef = tDipole->GetAngularFrequency(ifreq) * std::sqrt(MU0*EPSILON0);
  748. for (int irec=tid; irec<Receivers->GetNumberOfPoints(); irec+=nthreads) {
  749. SolveSingleTxRxPair(irec, Hankel.get(), wavef, ifreq, tDipole.get());
  750. }
  751. }
  752. } else {
  753. for (int irec=0; irec<Receivers->GetNumberOfPoints(); ++irec) {
  754. for (int ifreq=tid; ifreq<tDipole->GetNumberOfFrequencies(); ifreq+=nthreads) {
  755. // Propogation constant in free space being input to Hankel
  756. Real wavef = tDipole->GetAngularFrequency(ifreq) * std::sqrt(MU0*EPSILON0);
  757. SolveSingleTxRxPair(irec, Hankel.get(), wavef, ifreq, tDipole.get());
  758. }
  759. }
  760. }
  761. } // OpenMP Parallel Block
  762. }
  763. NullReceivers::NullReceivers() :
  764. runtime_error("nullptr RECEIVERS") {}
  765. NullAntenna::NullAntenna() :
  766. runtime_error("nullptr ANTENNA") {}
  767. NullInstrument::NullInstrument(LemmaObject* ptr) :
  768. runtime_error("nullptr INSTRUMENT") {
  769. std::cout << "Thrown by instance of "
  770. << ptr->GetName() << std::endl;
  771. }
  772. DipoleSourceSpecifiedForWireAntennaCalc::
  773. DipoleSourceSpecifiedForWireAntennaCalc() :
  774. runtime_error("DIPOLE SOURCE SPECIFIED FOR WIRE ANTENNA CALC"){}
  775. } // end of Lemma Namespace