Lemma is an Electromagnetics API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EMEarth1D.cpp 34KB

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