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

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