Surface NMR forward modelling
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /* This file is part of Lemma, a geophysical modelling and inversion API.
  2. * More information is available at http://lemmasoftware.org
  3. */
  4. /* This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file
  10. * @date 11/11/2016 01:47:25 PM
  11. * @author Trevor Irons (ti)
  12. * @email tirons@egi.utah.edu
  13. * @copyright Copyright (c) 2016, University of Utah
  14. * @copyright Copyright (c) 2016, Lemma Software, LLC
  15. * @copyright Copyright (c) 2008, Colorado School of Mines
  16. */
  17. #include "KernelV0.h"
  18. #include "FieldPoints.h"
  19. namespace Lemma {
  20. // ==================== FRIEND METHODS =====================
  21. std::ostream &operator << (std::ostream &stream, const KernelV0 &ob) {
  22. stream << ob.Serialize() << "\n---\n"; // End of doc ---
  23. return stream;
  24. }
  25. // ==================== LIFECYCLE =======================
  26. //--------------------------------------------------------------------------------------
  27. // Class: KernelV0
  28. // Method: KernelV0
  29. // Description: constructor (locked)
  30. //--------------------------------------------------------------------------------------
  31. KernelV0::KernelV0 (const ctor_key&) : LemmaObject( ) {
  32. } // ----- end of method KernelV0::KernelV0 (constructor) -----
  33. //--------------------------------------------------------------------------------------
  34. // Class: KernelV0
  35. // Method: KernelV0
  36. // Description: DeSerializing constructor (locked)
  37. //--------------------------------------------------------------------------------------
  38. KernelV0::KernelV0 (const YAML::Node& node, const ctor_key&) : LemmaObject(node) {
  39. } // ----- end of method KernelV0::KernelV0 (constructor) -----
  40. //--------------------------------------------------------------------------------------
  41. // Class: KernelV0
  42. // Method: NewSP()
  43. // Description: public constructor returing a shared_ptr
  44. //--------------------------------------------------------------------------------------
  45. std::shared_ptr< KernelV0 > KernelV0::NewSP() {
  46. return std::make_shared< KernelV0 >( ctor_key() );
  47. }
  48. //--------------------------------------------------------------------------------------
  49. // Class: KernelV0
  50. // Method: ~KernelV0
  51. // Description: destructor (protected)
  52. //--------------------------------------------------------------------------------------
  53. KernelV0::~KernelV0 () {
  54. } // ----- end of method KernelV0::~KernelV0 (destructor) -----
  55. //--------------------------------------------------------------------------------------
  56. // Class: KernelV0
  57. // Method: Serialize
  58. //--------------------------------------------------------------------------------------
  59. YAML::Node KernelV0::Serialize ( ) const {
  60. YAML::Node node = LemmaObject::Serialize();
  61. node.SetTag( GetName() );
  62. // Coils Transmitters & Receivers
  63. for ( auto txm : TxRx) {
  64. node[txm.first] = txm.second->Serialize();
  65. }
  66. // LayeredEarthEM
  67. node["SigmaModel"] = SigmaModel->Serialize();
  68. node["Larmor"] = Larmor;
  69. node["Temperature"] = Temperature;
  70. node["tol"] = tol;
  71. node["minLevel"] = minLevel;
  72. node["maxLevel"] = maxLevel;
  73. node["Taup"] = Taup;
  74. node["PulseI"] = PulseI;
  75. node["Interfaces"] = Interfaces;
  76. for ( int ilay=0; ilay<Interfaces.size()-1; ++ilay ) {
  77. node["Kern-" + to_string(ilay) ] = static_cast<VectorXcr>(Kern.row(ilay));
  78. }
  79. return node;
  80. } // ----- end of method KernelV0::Serialize -----
  81. //--------------------------------------------------------------------------------------
  82. // Class: KernelV0
  83. // Method: DeSerialize
  84. //--------------------------------------------------------------------------------------
  85. std::shared_ptr<KernelV0> KernelV0::DeSerialize ( const YAML::Node& node ) {
  86. if (node.Tag() != "KernelV0" ) {
  87. throw DeSerializeTypeMismatch( "KernelV0", node.Tag());
  88. }
  89. return std::make_shared< KernelV0 > ( node, ctor_key() );
  90. } // ----- end of method KernelV0::DeSerialize -----
  91. //--------------------------------------------------------------------------------------
  92. // Class: KernelV0
  93. // Method: AlignWithAkvoDataset
  94. //--------------------------------------------------------------------------------------
  95. void KernelV0::AlignWithAkvoDataset( const YAML::Node& node ) {
  96. if (node["processed"].as<std::string>().substr(0,4) == "Akvo") {
  97. std::cout << "Akvo file read\n";
  98. std::cout << node["processed"] << std::endl;
  99. }
  100. if (node["pulseType"].as<std::string>() == "FID") {
  101. PulseI = node["Pulse 1"]["current"].as<VectorXr>();
  102. this->SetPulseDuration( node["pulseLength"].as<double>() );
  103. } else {
  104. std::cerr << "Pulse Type " << node["PulseType"] << "is not supported\n";
  105. }
  106. }
  107. //--------------------------------------------------------------------------------------
  108. // Class: KernelV0
  109. // Method: DeSerialize
  110. //--------------------------------------------------------------------------------------
  111. void KernelV0::CalculateK0 (const std::vector< std::string>& Tx, const std::vector<std::string >& Rx,
  112. bool vtkOutput ) {
  113. // Set up
  114. Larmor = SigmaModel->GetMagneticFieldMagnitude()*GAMMA; // in rad 2246.*2.*PI;
  115. // All EM calculations will share same field points
  116. cpoints = FieldPoints::NewSP();
  117. cpoints->SetNumberOfPoints(8);
  118. for (auto tx : Tx) {
  119. // Set up EMEarth
  120. EMEarths[tx] = EMEarth1D::NewSP();
  121. EMEarths[tx]->AttachWireAntenna(TxRx[tx]);
  122. EMEarths[tx]->AttachLayeredEarthEM(SigmaModel);
  123. EMEarths[tx]->AttachFieldPoints( cpoints );
  124. EMEarths[tx]->SetFieldsToCalculate(H);
  125. // TODO query for method, altough with flat antennae, this is fastest
  126. //EMEarths[tx]->SetHankelTransformMethod(FHTKEY201);
  127. EMEarths[tx]->SetHankelTransformMethod(ANDERSON801);
  128. EMEarths[tx]->SetTxRxMode(TX);
  129. TxRx[tx]->SetCurrent(1.);
  130. }
  131. for (auto rx : Rx) {
  132. if (EMEarths.count(rx)) {
  133. EMEarths[rx]->SetTxRxMode(TXRX);
  134. } else {
  135. EMEarths[rx] = EMEarth1D::NewSP();
  136. EMEarths[rx]->AttachWireAntenna(TxRx[rx]);
  137. EMEarths[rx]->AttachLayeredEarthEM(SigmaModel);
  138. EMEarths[rx]->AttachFieldPoints( cpoints );
  139. EMEarths[rx]->SetFieldsToCalculate(H);
  140. // TODO query for method, altough with flat antennae, this is fastest
  141. //EMEarths[rx]->SetHankelTransformMethod(FHTKEY201);
  142. EMEarths[rx]->SetHankelTransformMethod(ANDERSON801);
  143. EMEarths[rx]->SetTxRxMode(RX);
  144. TxRx[rx]->SetCurrent(1.);
  145. }
  146. }
  147. std::cout << "Calculating K0 kernel\n";
  148. Kern = MatrixXcr::Zero( Interfaces.size()-1, PulseI.size() );
  149. for (ilay=0; ilay<Interfaces.size()-1; ++ilay) {
  150. std::cout << "Layer " << ilay << "\tfrom " << Interfaces(ilay) <<" to "<< Interfaces(ilay+1) << std::endl;
  151. Size(2) = Interfaces(ilay+1) - Interfaces(ilay);
  152. Origin(2) = Interfaces(ilay);
  153. IntegrateOnOctreeGrid( vtkOutput );
  154. }
  155. std::cout << "\nFinished KERNEL\n";
  156. }
  157. //--------------------------------------------------------------------------------------
  158. // Class: KernelV0
  159. // Method: IntegrateOnOctreeGrid
  160. //--------------------------------------------------------------------------------------
  161. void KernelV0::IntegrateOnOctreeGrid( bool vtkOutput) {
  162. Vector3r cpos = Origin + Size/2.;
  163. VOLSUM = 0;
  164. nleaves = 0;
  165. if (!vtkOutput) {
  166. EvaluateKids( Size, 0, cpos, VectorXcr::Ones(PulseI.size()) );
  167. } else {
  168. #ifdef LEMMAUSEVTK
  169. vtkHyperOctree* oct = vtkHyperOctree::New();
  170. oct->SetDimension(3);
  171. oct->SetOrigin( Origin(0), Origin(1), Origin(2) );
  172. oct->SetSize( Size(0), Size(1), Size(2) );
  173. vtkHyperOctreeCursor* curse = oct->NewCellCursor();
  174. curse->ToRoot();
  175. EvaluateKids2( Size, 0, cpos, VectorXcr::Ones(PulseI.size()), oct, curse );
  176. for (int iq=0; iq<PulseI.size(); ++iq) {
  177. // Fill in leaf data
  178. vtkDoubleArray* kr = vtkDoubleArray::New();
  179. kr->SetNumberOfComponents(1);
  180. kr->SetName("Re($\\mathcal{K}_0$)");
  181. kr->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  182. vtkDoubleArray* ki = vtkDoubleArray::New();
  183. ki->SetNumberOfComponents(1);
  184. ki->SetName("Im($\\mathcal{K}_0$)");
  185. ki->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  186. vtkDoubleArray* km = vtkDoubleArray::New();
  187. km->SetNumberOfComponents(1);
  188. km->SetName("mod($\\mathcal{K}_0$)");
  189. km->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  190. vtkIntArray* kid = vtkIntArray::New();
  191. kid->SetNumberOfComponents(1);
  192. kid->SetName("ID");
  193. kid->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  194. vtkIntArray* kerr = vtkIntArray::New();
  195. kerr->SetNumberOfComponents(1);
  196. kerr->SetName("err");
  197. kerr->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  198. // Ht field
  199. vtkDoubleArray* htr = vtkDoubleArray::New();
  200. htr->SetNumberOfComponents(3);
  201. htr->SetName("Re($\\mathbf{\\mathcal{H}}_T$)");
  202. htr->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  203. vtkDoubleArray* hti = vtkDoubleArray::New();
  204. hti->SetNumberOfComponents(3);
  205. hti->SetName("Im($\\mathbf{\\mathcal{H}}_T$)");
  206. hti->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  207. // Hr field
  208. vtkDoubleArray* hrr = vtkDoubleArray::New();
  209. hrr->SetNumberOfComponents(3);
  210. hrr->SetName("Re($\\mathbf{\\mathcal{H}}_R$)");
  211. hrr->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  212. vtkDoubleArray* hri = vtkDoubleArray::New();
  213. hri->SetNumberOfComponents(3);
  214. hri->SetName("Im($\\mathbf{\\mathcal{H}}_R$)");
  215. hri->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  216. //Real LeafVol(0);
  217. for (auto leaf : LeafDict) {
  218. kr->InsertTuple1( leaf.first, std::real(leaf.second(iq)) );
  219. ki->InsertTuple1( leaf.first, std::imag(leaf.second(iq)) );
  220. km->InsertTuple1( leaf.first, std::abs(leaf.second(iq)) );
  221. kid->InsertTuple1( leaf.first, leaf.first );
  222. //LeafVol += std::real(leaf.second);
  223. }
  224. for (auto leaf : LeafHt ) {
  225. htr->InsertTuple( leaf.first, leaf.second.real().data() );
  226. hti->InsertTuple( leaf.first, leaf.second.imag().data() );
  227. }
  228. for (auto leaf : LeafHr ) {
  229. hrr->InsertTuple( leaf.first, leaf.second.real().data() );
  230. hri->InsertTuple( leaf.first, leaf.second.imag().data() );
  231. }
  232. for (auto leaf : LeafDictIdx) {
  233. kerr->InsertTuple1( leaf.first, leaf.second );
  234. }
  235. auto kri = oct->GetLeafData()->AddArray(kr);
  236. auto kii = oct->GetLeafData()->AddArray(ki);
  237. auto kmi = oct->GetLeafData()->AddArray(km);
  238. auto kidi = oct->GetLeafData()->AddArray(kid);
  239. auto keri = oct->GetLeafData()->AddArray(kerr);
  240. auto khtr = oct->GetLeafData()->AddArray(htr);
  241. auto khti = oct->GetLeafData()->AddArray(hti);
  242. auto khrr = oct->GetLeafData()->AddArray(hrr);
  243. auto khri = oct->GetLeafData()->AddArray(hri);
  244. auto write = vtkXMLHyperOctreeWriter::New();
  245. //write.SetDataModeToAscii()
  246. write->SetInputData(oct);
  247. std::string fname = std::string("octree-") + to_string(ilay)
  248. + std::string("-") + to_string(iq) + std::string(".vto");
  249. write->SetFileName(fname.c_str());
  250. write->Write();
  251. write->Delete();
  252. oct->GetLeafData()->RemoveArray( kri );
  253. oct->GetLeafData()->RemoveArray( kii );
  254. oct->GetLeafData()->RemoveArray( kmi );
  255. oct->GetLeafData()->RemoveArray( kidi );
  256. oct->GetLeafData()->RemoveArray( keri );
  257. oct->GetLeafData()->RemoveArray( khtr );
  258. oct->GetLeafData()->RemoveArray( khti );
  259. oct->GetLeafData()->RemoveArray( khrr );
  260. oct->GetLeafData()->RemoveArray( khri );
  261. kerr->Delete();
  262. kid->Delete();
  263. kr->Delete();
  264. ki->Delete();
  265. km->Delete();
  266. htr->Delete();
  267. hti->Delete();
  268. hrr->Delete();
  269. hri->Delete();
  270. }
  271. curse->Delete();
  272. oct->Delete();
  273. #else
  274. throw std::runtime_error("IntegrateOnOctreeGrid with vtkOutput requires Lemma with VTK support");
  275. #endif
  276. }
  277. std::cout << "\nVOLSUM=" << VOLSUM << "\tActual=" << Size(0)*Size(1)*Size(2)
  278. << "\tDifference=" << VOLSUM - (Size(0)*Size(1)*Size(2)) << std::endl;
  279. }
  280. //--------------------------------------------------------------------------------------
  281. // Class: KernelV0
  282. // Method: f
  283. //--------------------------------------------------------------------------------------
  284. VectorXcr KernelV0::f( const Vector3r& r, const Real& volume, const Vector3cr& Ht, const Vector3cr& Hr ) {
  285. // Compute the elliptic fields
  286. Vector3r B0hat = SigmaModel->GetMagneticFieldUnitVector();
  287. Vector3r B0 = SigmaModel->GetMagneticField();
  288. // Elliptic representation
  289. EllipticB EBT = EllipticFieldRep(MU0*Ht, B0hat);
  290. EllipticB EBR = EllipticFieldRep(MU0*Hr, B0hat);
  291. // Compute Mn0
  292. Vector3r Mn0 = ComputeMn0(1.0, B0);
  293. //std::cout << "Mn0\t" << Mn0.transpose() << std::endl;
  294. Real Mn0Abs = Mn0.norm();
  295. // Compute phase delay
  296. // TODO add transmiiter current phase and delay induced apparent time phase!
  297. Complex PhaseTerm = EBR.bhat.dot(EBT.bhat) + Complex(0, (B0hat.dot(EBR.bhat.cross(EBT.bhat))));
  298. Complex ejztr = std::exp(Complex(0, EBR.zeta + EBT.zeta));
  299. // Calcuate vector of all responses
  300. VectorXcr F = VectorXcr::Zero( PulseI.size() );
  301. for (int iq=0; iq<PulseI.size(); ++iq) {
  302. // Compute the tipping angle
  303. Real sintheta = std::sin(0.5*GAMMA*PulseI(iq)*Taup*(EBT.alpha-EBT.beta));
  304. F(iq) = -volume*Complex(0,Larmor)*Mn0Abs*(EBR.alpha+EBR.beta)*ejztr*sintheta*PhaseTerm;
  305. }
  306. return F;
  307. }
  308. // //--------------------------------------------------------------------------------------
  309. // // Class: KernelV0
  310. // // Method: ComputeV0Cell
  311. // //--------------------------------------------------------------------------------------
  312. // Complex KernelV0::ComputeV0Cell(const EllipticB& EBT, const EllipticB& EBR,
  313. // const Real& sintheta, const Real& phase, const Real& Mn0Abs,
  314. // const Real& vol) {
  315. // // earth response of receiver adjoint field
  316. // Vector3r B0hat = SigmaModel->GetMagneticFieldUnitVector();
  317. // Complex ejztr = std::exp(Complex(0, EBR.zeta + EBT.zeta));
  318. // Complex PhaseTerm = EBR.bhat.dot(EBT.bhat) + (B0hat.dot(EBR.bhat.cross(EBT.bhat) ));
  319. // return -vol*Complex(0,Larmor)*Mn0Abs*(EBR.alpha+EBR.beta)*ejztr*sintheta*PhaseTerm;
  320. // }
  321. //--------------------------------------------------------------------------------------
  322. // Class: KernelV0
  323. // Method: ComputeV0Cell
  324. //--------------------------------------------------------------------------------------
  325. Vector3r KernelV0::ComputeMn0(const Real& Porosity, const Vector3r& B0) {
  326. Real chi_n = NH2O*((GAMMA*GAMMA*HBAR*HBAR)/(4.*KB*Temperature));
  327. return chi_n*Porosity*B0;
  328. }
  329. //--------------------------------------------------------------------------------------
  330. // Class: KernelV0
  331. // Method: ComputeV0Cell
  332. //--------------------------------------------------------------------------------------
  333. EllipticB KernelV0::EllipticFieldRep (const Vector3cr& B, const Vector3r& B0hat) {
  334. // This all follows Weichman et al., 2000.
  335. // There are some numerical stability issues that arise when the two terms in the beta
  336. // formulation are nearly equivalent. The current formulation will result in a null-valued
  337. // beta, or can underflow. However, this does not entirely recreate the true value of B perp.
  338. // Error is checked to be below 1%, but reformulating for numeric stability may be welcome
  339. EllipticB ElipB = EllipticB();
  340. Vector3cr Bperp = B - B0hat.dot(B)*B0hat;
  341. Real BperpNorm = Bperp.norm();
  342. // These two are equivalent
  343. //Complex Bp2 = Bperp.transpose() * Bperp;
  344. Complex Bp2 = Bperp.conjugate().dot(Bperp);
  345. VectorXcr iB0 = Complex(0,1)*B0hat.cast<Complex>().array();
  346. ElipB.eizt = std::sqrt(Bp2 / std::abs(Bp2));
  347. ElipB.alpha = INVSQRT2*std::sqrt(BperpNorm*BperpNorm + std::abs(Bp2));
  348. //ElipB.beta = std::copysign(1, std::real(iB0.dot( Bperp.cross(Bperp.conjugate())) )) *
  349. ElipB.beta = sgn( std::real(iB0.dot( Bperp.cross(Bperp.conjugate())) )) *
  350. (INVSQRT2*std::sqrt(BperpNorm*BperpNorm - std::abs(Bp2)));
  351. // Correct underflow in beta calculation
  352. // could use cerrno instead...
  353. // http://en.cppreference.com/w/cpp/numeric/math/sqrt
  354. if (ElipB.beta != ElipB.beta) ElipB.beta = 0;
  355. ElipB.bhat = ((Real)1./ElipB.alpha)*(((Real)1./ElipB.eizt)*Bperp.array()).real().array();
  356. ElipB.bhatp = B0hat.cross(ElipB.bhat);
  357. ElipB.zeta = std::real(std::log(ElipB.eizt)/Complex(0,1));
  358. /* as an error check decomposed field - computed actual */
  359. // Vector3cr Bperp2 = ElipB.eizt * (ElipB.alpha * ElipB.bhat
  360. // + (Complex(0,1) * ElipB.beta * ElipB.bhatp) );
  361. // ElipB.err = (Bperp-Bperp2).norm();
  362. // if (ElipB.err > .01*Bperp.norm() ) { // 1% error
  363. // std::cout << "Elip error\n";
  364. // Real Beta2 = sgn( std::real(iB0.dot( Bperp.cross(Bperp.conjugate())) )) *
  365. // (INVSQRT2*std::sqrt(BperpNorm*BperpNorm - std::abs(Bp2)));
  366. // Vector3cr Bperp3 = ElipB.eizt * (ElipB.alpha * ElipB.bhat
  367. // + (Complex(0,1) * Beta2 * ElipB.bhatp) );
  368. // std::cout << "Beta term0\t" << (INVSQRT2*std::sqrt(BperpNorm*BperpNorm - std::abs(Bp2))) << std::endl;
  369. // std::cout << "Beta term1\t" << BperpNorm*BperpNorm << "\t" << std::abs(Bp2) << std::endl;
  370. // std::cout << "Beta \t" << ElipB.beta << std::endl;
  371. // std::cout << "Beta2 \t" << Beta2 << std::endl;
  372. // std::cout << "Bperp \t" << Bperp.transpose() << std::endl;
  373. // std::cout << "Bperp2\t" << Bperp2.transpose() << std::endl;
  374. // std::cout << "Bperp3\t" << Bperp3.transpose() << std::endl;
  375. // std::cout << "err \t" << ElipB.err << std::endl;
  376. // }
  377. return ElipB;
  378. }
  379. //--------------------------------------------------------------------------------------
  380. // Class: KernelV0
  381. // Method: EvaluateKids
  382. //--------------------------------------------------------------------------------------
  383. void KernelV0::EvaluateKids( const Vector3r& size, const int& level, const Vector3r& cpos,
  384. const VectorXcr& parentVal ) {
  385. std::cout << "\r" << (int)(1e2*VOLSUM/(Size[0]*Size[1]*Size[2])) << "\t" << nleaves;
  386. //std::cout.flush();
  387. // Next level step, interested in one level below
  388. // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
  389. Vector3r step = size.array() / (Real)(1 << (level+1) );
  390. Real vol = (step(0)*step(1)*step(2)); // volume of each child
  391. Vector3r pos = cpos - step/2.;
  392. Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
  393. 0, 0, 0,
  394. step[0], 0, 0,
  395. 0, step[1], 0,
  396. step[0], step[1], 0,
  397. 0, 0, step[2],
  398. step[0], 0, step[2],
  399. 0, step[1], step[2],
  400. step[0], step[1], step[2] ).finished();
  401. cpoints->ClearFields();
  402. for (int ichild=0; ichild<8; ++ichild) {
  403. Vector3r cp = pos; // Eigen complains about combining these
  404. cp += posadd.row(ichild);
  405. cpoints->SetLocation( ichild, cp );
  406. }
  407. Eigen::Matrix<Complex, 3, 8> Ht = Eigen::Matrix<Complex, 3, 8>::Zero();
  408. Eigen::Matrix<Complex, 3, 8> Hr = Eigen::Matrix<Complex, 3, 8>::Zero();
  409. for ( auto EMCalc : EMEarths ) {
  410. EMCalc.second->GetFieldPoints()->ClearFields();
  411. EMCalc.second->CalculateWireAntennaFields();
  412. switch (EMCalc.second->GetTxRxMode()) {
  413. case TX:
  414. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  415. break;
  416. case RX:
  417. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  418. break;
  419. case TXRX:
  420. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  421. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  422. break;
  423. default:
  424. break;
  425. }
  426. }
  427. MatrixXcr kvals(8, PulseI.size()); // individual kernel vals
  428. for (int ichild=0; ichild<8; ++ichild) {
  429. Vector3r cp = pos; // Eigen complains about combining these
  430. cp += posadd.row(ichild);
  431. kvals.row(ichild) = f(cp, vol, Ht.col(ichild), Hr.col(ichild));
  432. }
  433. VectorXcr ksum = kvals.colwise().sum(); // Kernel sum
  434. // Evaluate whether or not furthur splitting is needed
  435. if ( (((ksum - parentVal).array().abs() > tol).any() && level<maxLevel) || level < minLevel ) {
  436. // Not a leaf dive further in
  437. for (int ichild=0; ichild<8; ++ichild) {
  438. Vector3r cp = pos; // Eigen complains about combining these
  439. cp += posadd.row(ichild);
  440. EvaluateKids( size, level+1, cp, kvals.row(ichild) );
  441. }
  442. return; // not leaf
  443. }
  444. // implicit else, is a leaf
  445. Kern.row(ilay) += ksum;
  446. VOLSUM += 8.*vol;
  447. nleaves += 8; // reflects the number of kernel evaluations
  448. return; // is leaf
  449. }
  450. #ifdef LEMMAUSEVTK
  451. //--------------------------------------------------------------------------------------
  452. // Class: KernelV0
  453. // Method: EvaluateKids2 -- same as Evaluate Kids, but include VTK octree generation
  454. //--------------------------------------------------------------------------------------
  455. void KernelV0::EvaluateKids2( const Vector3r& size, const int& level, const Vector3r& cpos,
  456. const VectorXcr& parentVal, vtkHyperOctree* oct, vtkHyperOctreeCursor* curse) {
  457. std::cout << "\r" << (int)(1e2*VOLSUM/(Size[0]*Size[1]*Size[2])) << "\t" << nleaves;
  458. //std::cout.flush();
  459. // Next level step, interested in one level below
  460. // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
  461. Vector3r step = size.array() / (Real)(1 << (level+1) );
  462. Real vol = (step(0)*step(1)*step(2)); // volume of each child
  463. Vector3r pos = cpos - step/2.;
  464. Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
  465. 0, 0, 0,
  466. step[0], 0, 0,
  467. 0, step[1], 0,
  468. step[0], step[1], 0,
  469. 0, 0, step[2],
  470. step[0], 0, step[2],
  471. 0, step[1], step[2],
  472. step[0], step[1], step[2] ).finished();
  473. MatrixXcr kvals(8, PulseI.size()); // individual kernel vals
  474. cpoints->ClearFields();
  475. for (int ichild=0; ichild<8; ++ichild) {
  476. Vector3r cp = pos; // Eigen complains about combining these
  477. cp += posadd.row(ichild);
  478. cpoints->SetLocation( ichild, cp );
  479. }
  480. Eigen::Matrix<Complex, 3, 8> Ht = Eigen::Matrix<Complex, 3, 8>::Zero();
  481. Eigen::Matrix<Complex, 3, 8> Hr = Eigen::Matrix<Complex, 3, 8>::Zero();
  482. for ( auto EMCalc : EMEarths ) {
  483. //EMCalc->GetFieldPoints()->ClearFields();
  484. EMCalc.second->CalculateWireAntennaFields();
  485. switch (EMCalc.second->GetTxRxMode()) {
  486. case TX:
  487. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  488. break;
  489. case RX:
  490. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  491. break;
  492. case TXRX:
  493. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  494. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  495. break;
  496. default:
  497. break;
  498. }
  499. }
  500. for (int ichild=0; ichild<8; ++ichild) {
  501. Vector3r cp = pos; // Eigen complains about combining these
  502. cp += posadd.row(ichild);
  503. kvals.row(ichild) = f(cp, vol, Ht.col(ichild), Hr.col(ichild));
  504. }
  505. VectorXcr ksum = kvals.colwise().sum(); // Kernel sum
  506. // Evaluate whether or not furthur splitting is needed
  507. if ( (((ksum - parentVal).array().abs() > tol).any() && level<maxLevel) || level < minLevel ) {
  508. oct->SubdivideLeaf(curse);
  509. for (int ichild=0; ichild<8; ++ichild) {
  510. curse->ToChild(ichild);
  511. Vector3r cp = pos; // Eigen complains about combining these
  512. cp += posadd.row(ichild);
  513. /* Test for position via alternative means */
  514. /*
  515. Real p[3];
  516. GetPosition(curse, p);
  517. if ( (Vector3r(p) - cp).norm() > 1e-8 ) {
  518. std::cout << "ERROR @ nleaves" << nleaves << "\n" << cp[0] << "\t" << p[0] << "\t" << cp[1] << "\t" << p[1]
  519. << "\t" << cp[2] << "\t" << p[2] << "\t" << vol<< std::endl;
  520. throw std::runtime_error("doom");
  521. }
  522. */
  523. /* End of position test */
  524. EvaluateKids2( size, level+1, cp, kvals.row(ichild), oct, curse );
  525. curse->ToParent();
  526. }
  527. return; // not a leaf
  528. }
  529. /* just stuff with sum of the kids and don't subdivide */
  530. /*
  531. LeafDict[curse->GetLeafId()] = ksum/(8.*vol);
  532. LeafDictIdx[curse->GetLeafId()] = nleaves;
  533. */
  534. /* Alternatively, subdivide the VTK octree here and stuff the children to make better
  535. * visuals, but also 8x the storage...
  536. */
  537. oct->SubdivideLeaf(curse);
  538. for (int ichild=0; ichild<8; ++ichild) {
  539. curse->ToChild(ichild);
  540. LeafDict[curse->GetLeafId()] = ksum/(8.*vol);
  541. LeafHt[curse->GetLeafId()] = Ht.col(ichild);
  542. LeafHr[curse->GetLeafId()] = Hr.col(ichild);
  543. LeafDictIdx[curse->GetLeafId()] = nleaves;
  544. curse->ToParent();
  545. }
  546. Kern.row(ilay) += ksum;
  547. VOLSUM += 8*vol;
  548. nleaves += 8; // good reason to say 1 or 8 here...8 sounds better and reflects kernel evaluations
  549. return; // is a leaf
  550. }
  551. //--------------------------------------------------------------------------------------
  552. // Class: KernelV0
  553. // Method: GetPosition
  554. //--------------------------------------------------------------------------------------
  555. void KernelV0::GetPosition( vtkHyperOctreeCursor* Cursor, Real* p ) {
  556. Real ratio=1.0/(1<<(Cursor->GetCurrentLevel()));
  557. //step = ((Size).array() / std::pow(2.,Cursor->GetCurrentLevel()));
  558. p[0]=(Cursor->GetIndex(0)+.5)*ratio*this->Size[0]+this->Origin[0] ;//+ .5*step[0];
  559. p[1]=(Cursor->GetIndex(1)+.5)*ratio*this->Size[1]+this->Origin[1] ;//+ .5*step[1];
  560. p[2]=(Cursor->GetIndex(2)+.5)*ratio*this->Size[2]+this->Origin[2] ;//+ .5*step[2];
  561. }
  562. #endif
  563. } // ---- end of namespace Lemma ----
  564. /* vim: set tabstop=4 expandtab */
  565. /* vim: set filetype=cpp */