Surface NMR forward modelling
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.

KernelV0.cpp 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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. #if 1
  285. VectorXcr KernelV0::f( const Vector3r& r, const Real& volume, const Vector3cr& Ht, const Vector3cr& Hr ) {
  286. // Compute the elliptic fields
  287. Vector3r B0hat = SigmaModel->GetMagneticFieldUnitVector();
  288. Vector3r B0 = SigmaModel->GetMagneticField();
  289. // Elliptic representation
  290. EllipticB EBT = EllipticFieldRep(MU0*Ht, B0hat);
  291. EllipticB EBR = EllipticFieldRep(MU0*Hr, B0hat);
  292. // Compute Mn0
  293. Vector3r Mn0 = ComputeMn0(1.0, B0);
  294. //std::cout << "Mn0\t" << Mn0.transpose() << std::endl;
  295. Real Mn0Abs = Mn0.norm();
  296. // Compute phase delay
  297. // TODO add transmiiter current phase and delay induced apparent time phase!
  298. Complex PhaseTerm = EBR.bhat.dot(EBT.bhat) + Complex(0, (B0hat.dot(EBR.bhat.cross(EBT.bhat))));
  299. Complex ejztr = std::exp(Complex(0, EBR.zeta + EBT.zeta));
  300. // Calcuate vector of all responses
  301. VectorXcr F = VectorXcr::Zero( PulseI.size() );
  302. for (int iq=0; iq<PulseI.size(); ++iq) {
  303. // Compute the tipping angle
  304. Real sintheta = std::sin(0.5*GAMMA*PulseI(iq)*Taup*(EBT.alpha-EBT.beta));
  305. F(iq) = -volume*Complex(0,Larmor)*Mn0Abs*(EBR.alpha+EBR.beta)*ejztr*sintheta*PhaseTerm;
  306. //TODO TEST FOR ASYMETRY
  307. //Real sintheta = std::sin(0.5*GAMMA*PulseI(iq)*Taup*(EBT.alpha-EBT.beta));
  308. //F(iq) = volume * Complex(EBT.Bperp.real().norm(), EBT.Bperp.imag().norm());
  309. //Complex(sintheta, EBT.Bperp.norm() );
  310. //F(iq) = volume * Complex(EBT.alpha, EBT.beta);
  311. //F(iq) = volume * MU0*Hr.norm();
  312. //F(iq) = volume * EBT.err;
  313. //F(iq) = volume * sintheta;
  314. }
  315. return F;
  316. }
  317. #endif
  318. #if 0
  319. VectorXcr KernelV0::f( const Vector3r& r, const Real& volume, const Vector3cr& Ht, const Vector3cr& Hr ) {
  320. VectorXcr F = VectorXcr::Ones( PulseI.size() );
  321. F.array() *= volume * Complex(Ht.norm(), Hr.norm()); //*Ht.dot(Hr);
  322. return F;
  323. }
  324. #endif
  325. // //--------------------------------------------------------------------------------------
  326. // // Class: KernelV0
  327. // // Method: ComputeV0Cell
  328. // //--------------------------------------------------------------------------------------
  329. // Complex KernelV0::ComputeV0Cell(const EllipticB& EBT, const EllipticB& EBR,
  330. // const Real& sintheta, const Real& phase, const Real& Mn0Abs,
  331. // const Real& vol) {
  332. // // earth response of receiver adjoint field
  333. // Vector3r B0hat = SigmaModel->GetMagneticFieldUnitVector();
  334. // Complex ejztr = std::exp(Complex(0, EBR.zeta + EBT.zeta));
  335. // Complex PhaseTerm = EBR.bhat.dot(EBT.bhat) + (B0hat.dot(EBR.bhat.cross(EBT.bhat) ));
  336. // return -vol*Complex(0,Larmor)*Mn0Abs*(EBR.alpha+EBR.beta)*ejztr*sintheta*PhaseTerm;
  337. // }
  338. //--------------------------------------------------------------------------------------
  339. // Class: KernelV0
  340. // Method: ComputeV0Cell
  341. //--------------------------------------------------------------------------------------
  342. Vector3r KernelV0::ComputeMn0(const Real& Porosity, const Vector3r& B0) {
  343. Real chi_n = NH2O*((GAMMA*GAMMA*HBAR*HBAR)/(4.*KB*Temperature));
  344. return chi_n*Porosity*B0;
  345. }
  346. //--------------------------------------------------------------------------------------
  347. // Class: KernelV0
  348. // Method: ComputeV0Cell
  349. //--------------------------------------------------------------------------------------
  350. EllipticB KernelV0::EllipticFieldRep (const Vector3cr& B, const Vector3r& B0hat) {
  351. // This all follows Weichman et al., 2000.
  352. // There are some numerical stability issues that arise when the two terms in the beta
  353. // formulation are nearly equivalent. The current formulation will result in a null-valued
  354. // beta, although this does not entirely recreate the true value of B perp. Error is checked
  355. // to be below 1%, but reformulating may be welcome
  356. EllipticB ElipB = EllipticB();
  357. Vector3cr Bperp = B - B0hat.dot(B)*B0hat;
  358. Real BperpNorm = Bperp.norm();
  359. // These two are equivalent
  360. //Complex Bp2 = Bperp.transpose() * Bperp;
  361. Complex Bp2 = Bperp.conjugate().dot(Bperp);
  362. VectorXcr iB0 = Complex(0,1)*B0hat.cast<Complex>().array();
  363. ElipB.eizt = std::sqrt(Bp2 / std::abs(Bp2));
  364. ElipB.alpha = INVSQRT2*std::sqrt(BperpNorm*BperpNorm + std::abs(Bp2));
  365. ElipB.beta = std::copysign(1, std::real(iB0.dot( Bperp.cross(Bperp.conjugate())) )) *
  366. (INVSQRT2*std::sqrt(BperpNorm*BperpNorm - std::abs(Bp2)));
  367. ElipB.bhat = ((Real)1./ElipB.alpha)*(((Real)1./ElipB.eizt)*Bperp.array()).real().array();
  368. ElipB.bhatp = B0hat.cross(ElipB.bhat);
  369. ElipB.zeta = std::real(std::log(ElipB.eizt)/Complex(0,1));
  370. /* as an error check decomposed field - computed actual */
  371. // Vector3cr Bperp2 = ElipB.eizt * (ElipB.alpha * ElipB.bhat
  372. // + (Complex(0,1) * ElipB.beta * ElipB.bhatp) );
  373. // ElipB.err = (Bperp-Bperp2).norm();
  374. // if (ElipB.err > .01*Bperp.norm() ) { // 1% error
  375. // std::cout << "Elip error\n";
  376. // Real Beta2 = sgn( std::real(iB0.dot( Bperp.cross(Bperp.conjugate())) )) *
  377. // (INVSQRT2*std::sqrt(BperpNorm*BperpNorm - std::abs(Bp2)));
  378. // Vector3cr Bperp3 = ElipB.eizt * (ElipB.alpha * ElipB.bhat
  379. // + (Complex(0,1) * Beta2 * ElipB.bhatp) );
  380. // std::cout << "Beta term0\t" << (INVSQRT2*std::sqrt(BperpNorm*BperpNorm - std::abs(Bp2))) << std::endl;
  381. // std::cout << "Beta term1\t" << BperpNorm*BperpNorm << "\t" << std::abs(Bp2) << std::endl;
  382. // std::cout << "Beta \t" << ElipB.beta << std::endl;
  383. // std::cout << "Beta2 \t" << Beta2 << std::endl;
  384. // std::cout << "Bperp \t" << Bperp.transpose() << std::endl;
  385. // std::cout << "Bperp2\t" << Bperp2.transpose() << std::endl;
  386. // std::cout << "Bperp3\t" << Bperp3.transpose() << std::endl;
  387. // std::cout << "err \t" << ElipB.err << std::endl;
  388. // }
  389. return ElipB;
  390. }
  391. //--------------------------------------------------------------------------------------
  392. // Class: KernelV0
  393. // Method: EvaluateKids
  394. //--------------------------------------------------------------------------------------
  395. void KernelV0::EvaluateKids( const Vector3r& size, const int& level, const Vector3r& cpos,
  396. const VectorXcr& parentVal ) {
  397. std::cout << "\r" << (int)(1e2*VOLSUM/(Size[0]*Size[1]*Size[2])) << "\t" << nleaves;
  398. //std::cout.flush();
  399. // Next level step, interested in one level below
  400. // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
  401. Vector3r step = size.array() / (Real)(1 << (level+1) );
  402. Real vol = (step(0)*step(1)*step(2)); // volume of each child
  403. Vector3r pos = cpos - step/2.;
  404. Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
  405. 0, 0, 0,
  406. step[0], 0, 0,
  407. 0, step[1], 0,
  408. step[0], step[1], 0,
  409. 0, 0, step[2],
  410. step[0], 0, step[2],
  411. 0, step[1], step[2],
  412. step[0], step[1], step[2] ).finished();
  413. MatrixXcr kvals(8, PulseI.size()); // individual kernel vals
  414. cpoints->ClearFields();
  415. for (int ichild=0; ichild<8; ++ichild) {
  416. Vector3r cp = pos; // Eigen complains about combining these
  417. cp += posadd.row(ichild);
  418. cpoints->SetLocation( ichild, cp );
  419. }
  420. Eigen::Matrix<Complex, 3, 8> Ht = Eigen::Matrix<Complex, 3, 8>::Zero();
  421. Eigen::Matrix<Complex, 3, 8> Hr = Eigen::Matrix<Complex, 3, 8>::Zero();
  422. for ( auto EMCalc : EMEarths ) {
  423. EMCalc.second->GetFieldPoints()->ClearFields();
  424. EMCalc.second->CalculateWireAntennaFields();
  425. switch (EMCalc.second->GetTxRxMode()) {
  426. case TX:
  427. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  428. break;
  429. case RX:
  430. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  431. break;
  432. case TXRX:
  433. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  434. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  435. break;
  436. default:
  437. break;
  438. }
  439. }
  440. for (int ichild=0; ichild<8; ++ichild) {
  441. Vector3r cp = pos; // Eigen complains about combining these
  442. cp += posadd.row(ichild);
  443. kvals.row(ichild) = f(cp, vol, Ht.col(ichild), Hr.col(ichild));
  444. }
  445. VectorXcr ksum = kvals.colwise().sum(); // Kernel sum
  446. // Evaluate whether or not furthur splitting is needed
  447. if ( (((ksum - parentVal).array().abs() > tol).any() && level<maxLevel) || level < minLevel ) {
  448. // Not a leaf dive further in
  449. for (int ichild=0; ichild<8; ++ichild) {
  450. Vector3r cp = pos; // Eigen complains about combining these
  451. cp += posadd.row(ichild);
  452. EvaluateKids( size, level+1, cp, kvals.row(ichild) );
  453. }
  454. return; // not leaf
  455. }
  456. // implicit else, is a leaf
  457. Kern.row(ilay) += ksum;
  458. VOLSUM += 8.*vol;
  459. nleaves += 8; // reflects the number of kernel evaluations
  460. return; // is leaf
  461. }
  462. #ifdef LEMMAUSEVTK
  463. //--------------------------------------------------------------------------------------
  464. // Class: KernelV0
  465. // Method: EvaluateKids2 -- same as Evaluate Kids, but include VTK octree generation
  466. //--------------------------------------------------------------------------------------
  467. void KernelV0::EvaluateKids2( const Vector3r& size, const int& level, const Vector3r& cpos,
  468. const VectorXcr& parentVal, vtkHyperOctree* oct, vtkHyperOctreeCursor* curse) {
  469. std::cout << "\r" << (int)(1e2*VOLSUM/(Size[0]*Size[1]*Size[2])) << "\t" << nleaves;
  470. //std::cout.flush();
  471. // Next level step, interested in one level below
  472. // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
  473. Vector3r step = size.array() / (Real)(1 << (level+1) );
  474. Real vol = (step(0)*step(1)*step(2)); // volume of each child
  475. Vector3r pos = cpos - step/2.;
  476. Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
  477. 0, 0, 0,
  478. step[0], 0, 0,
  479. 0, step[1], 0,
  480. step[0], step[1], 0,
  481. 0, 0, step[2],
  482. step[0], 0, step[2],
  483. 0, step[1], step[2],
  484. step[0], step[1], step[2] ).finished();
  485. MatrixXcr kvals(8, PulseI.size()); // individual kernel vals
  486. cpoints->ClearFields();
  487. for (int ichild=0; ichild<8; ++ichild) {
  488. Vector3r cp = pos; // Eigen complains about combining these
  489. cp += posadd.row(ichild);
  490. cpoints->SetLocation( ichild, cp );
  491. }
  492. Eigen::Matrix<Complex, 3, 8> Ht = Eigen::Matrix<Complex, 3, 8>::Zero();
  493. Eigen::Matrix<Complex, 3, 8> Hr = Eigen::Matrix<Complex, 3, 8>::Zero();
  494. for ( auto EMCalc : EMEarths ) {
  495. //EMCalc->GetFieldPoints()->ClearFields();
  496. EMCalc.second->CalculateWireAntennaFields();
  497. switch (EMCalc.second->GetTxRxMode()) {
  498. case TX:
  499. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  500. break;
  501. case RX:
  502. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  503. break;
  504. case TXRX:
  505. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  506. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  507. break;
  508. default:
  509. break;
  510. }
  511. }
  512. for (int ichild=0; ichild<8; ++ichild) {
  513. Vector3r cp = pos; // Eigen complains about combining these
  514. cp += posadd.row(ichild);
  515. kvals.row(ichild) = f(cp, vol, Ht.col(ichild), Hr.col(ichild));
  516. }
  517. VectorXcr ksum = kvals.colwise().sum(); // Kernel sum
  518. // Evaluate whether or not furthur splitting is needed
  519. if ( (((ksum - parentVal).array().abs() > tol).any() && level<maxLevel) || level < minLevel ) {
  520. oct->SubdivideLeaf(curse);
  521. for (int ichild=0; ichild<8; ++ichild) {
  522. curse->ToChild(ichild);
  523. Vector3r cp = pos; // Eigen complains about combining these
  524. cp += posadd.row(ichild);
  525. /* Test for position via alternative means */
  526. /*
  527. Real p[3];
  528. GetPosition(curse, p);
  529. if ( (Vector3r(p) - cp).norm() > 1e-8 ) {
  530. std::cout << "ERROR @ nleaves" << nleaves << "\n" << cp[0] << "\t" << p[0] << "\t" << cp[1] << "\t" << p[1]
  531. << "\t" << cp[2] << "\t" << p[2] << "\t" << vol<< std::endl;
  532. throw std::runtime_error("doom");
  533. }
  534. */
  535. /* End of position test */
  536. EvaluateKids2( size, level+1, cp, kvals.row(ichild), oct, curse );
  537. curse->ToParent();
  538. }
  539. return; // not a leaf
  540. }
  541. /* just stuff with sum of the kids and don't subdivide */
  542. /*
  543. LeafDict[curse->GetLeafId()] = ksum/(8.*vol);
  544. LeafDictIdx[curse->GetLeafId()] = nleaves;
  545. */
  546. /* Alternatively, subdivide the VTK octree here and stuff the children to make better
  547. * visuals, but also 8x the storage...
  548. */
  549. oct->SubdivideLeaf(curse);
  550. for (int ichild=0; ichild<8; ++ichild) {
  551. curse->ToChild(ichild);
  552. LeafDict[curse->GetLeafId()] = ksum/(8.*vol);
  553. LeafHt[curse->GetLeafId()] = Ht.col(ichild);
  554. LeafHr[curse->GetLeafId()] = Hr.col(ichild);
  555. LeafDictIdx[curse->GetLeafId()] = nleaves;
  556. curse->ToParent();
  557. }
  558. Kern.row(ilay) += ksum;
  559. VOLSUM += 8*vol;
  560. nleaves += 8; // good reason to say 1 or 8 here...8 sounds better and reflects kernel evaluations
  561. return; // is a leaf
  562. }
  563. //--------------------------------------------------------------------------------------
  564. // Class: KernelV0
  565. // Method: GetPosition
  566. //--------------------------------------------------------------------------------------
  567. void KernelV0::GetPosition( vtkHyperOctreeCursor* Cursor, Real* p ) {
  568. Real ratio=1.0/(1<<(Cursor->GetCurrentLevel()));
  569. //step = ((Size).array() / std::pow(2.,Cursor->GetCurrentLevel()));
  570. p[0]=(Cursor->GetIndex(0)+.5)*ratio*this->Size[0]+this->Origin[0] ;//+ .5*step[0];
  571. p[1]=(Cursor->GetIndex(1)+.5)*ratio*this->Size[1]+this->Origin[1] ;//+ .5*step[1];
  572. p[2]=(Cursor->GetIndex(2)+.5)*ratio*this->Size[2]+this->Origin[2] ;//+ .5*step[2];
  573. }
  574. #endif
  575. } // ---- end of namespace Lemma ----
  576. /* vim: set tabstop=4 expandtab */
  577. /* vim: set filetype=cpp */