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 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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(ANDERSON801);
  127. EMEarths[tx]->SetTxRxMode(TX);
  128. TxRx[tx]->SetCurrent(1.);
  129. }
  130. for (auto rx : Rx) {
  131. if (EMEarths.count(rx)) {
  132. EMEarths[rx]->SetTxRxMode(TXRX);
  133. } else {
  134. EMEarths[rx] = EMEarth1D::NewSP();
  135. EMEarths[rx]->AttachWireAntenna(TxRx[rx]);
  136. EMEarths[rx]->AttachLayeredEarthEM(SigmaModel);
  137. EMEarths[rx]->AttachFieldPoints( cpoints );
  138. EMEarths[rx]->SetFieldsToCalculate(H);
  139. // TODO query for method, altough with flat antennae, this is fastest
  140. EMEarths[rx]->SetHankelTransformMethod(ANDERSON801);
  141. EMEarths[rx]->SetTxRxMode(RX);
  142. TxRx[rx]->SetCurrent(1.);
  143. }
  144. }
  145. std::cout << "Calculating K0 kernel\n";
  146. Kern = MatrixXcr::Zero( Interfaces.size()-1, PulseI.size() );
  147. for (ilay=0; ilay<Interfaces.size()-1; ++ilay) {
  148. std::cout << "Layer " << ilay << "\tfrom " << Interfaces(ilay) <<" to "<< Interfaces(ilay+1) << std::endl;
  149. Size(2) = Interfaces(ilay+1) - Interfaces(ilay);
  150. Origin(2) = Interfaces(ilay);
  151. IntegrateOnOctreeGrid( vtkOutput );
  152. }
  153. std::cout << "\nFinished KERNEL\n";
  154. }
  155. //--------------------------------------------------------------------------------------
  156. // Class: KernelV0
  157. // Method: IntegrateOnOctreeGrid
  158. //--------------------------------------------------------------------------------------
  159. void KernelV0::IntegrateOnOctreeGrid( bool vtkOutput) {
  160. Vector3r cpos = Origin + Size/2.;
  161. VOLSUM = 0;
  162. nleaves = 0;
  163. if (!vtkOutput) {
  164. EvaluateKids( Size, 0, cpos, VectorXcr::Ones(PulseI.size()) );
  165. } else {
  166. #ifdef LEMMAUSEVTK
  167. vtkHyperOctree* oct = vtkHyperOctree::New();
  168. oct->SetDimension(3);
  169. oct->SetOrigin( Origin(0), Origin(1), Origin(2) );
  170. oct->SetSize( Size(0), Size(1), Size(2) );
  171. vtkHyperOctreeCursor* curse = oct->NewCellCursor();
  172. curse->ToRoot();
  173. EvaluateKids2( Size, 0, cpos, VectorXcr::Ones(PulseI.size()), oct, curse );
  174. for (int iq=0; iq<PulseI.size(); ++iq) {
  175. // Fill in leaf data
  176. vtkDoubleArray* kr = vtkDoubleArray::New();
  177. kr->SetNumberOfComponents(1);
  178. kr->SetName("Re($K_0$)");
  179. kr->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  180. vtkDoubleArray* ki = vtkDoubleArray::New();
  181. ki->SetNumberOfComponents(1);
  182. ki->SetName("Im($K_0$)");
  183. ki->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  184. vtkDoubleArray* km = vtkDoubleArray::New();
  185. km->SetNumberOfComponents(1);
  186. km->SetName("mod($K_0$)");
  187. km->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  188. vtkIntArray* kid = vtkIntArray::New();
  189. kid->SetNumberOfComponents(1);
  190. kid->SetName("ID");
  191. kid->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  192. vtkIntArray* kerr = vtkIntArray::New();
  193. kerr->SetNumberOfComponents(1);
  194. kerr->SetName("nleaf");
  195. //Real LeafVol(0);
  196. for (auto leaf : LeafDict) {
  197. kr->InsertTuple1( leaf.first, std::real(leaf.second(iq)) );
  198. ki->InsertTuple1( leaf.first, std::imag(leaf.second(iq)) );
  199. km->InsertTuple1( leaf.first, std::abs(leaf.second(iq)) );
  200. kid->InsertTuple1( leaf.first, leaf.first );
  201. //LeafVol += std::real(leaf.second);
  202. }
  203. //std::cout << "\n\nLeafVol=" << LeafVol << std::endl;
  204. for (auto leaf : LeafDictIdx) {
  205. kerr->InsertTuple1( leaf.first, leaf.second );
  206. }
  207. auto kri = oct->GetLeafData()->AddArray(kr);
  208. auto kii = oct->GetLeafData()->AddArray(ki);
  209. auto kmi = oct->GetLeafData()->AddArray(km);
  210. auto kidi = oct->GetLeafData()->AddArray(kid);
  211. auto keri = oct->GetLeafData()->AddArray(kerr);
  212. auto write = vtkXMLHyperOctreeWriter::New();
  213. //write.SetDataModeToAscii()
  214. write->SetInputData(oct);
  215. std::string fname = std::string("octree-") + to_string(ilay)
  216. + std::string("-") + to_string(iq) + std::string(".vto");
  217. write->SetFileName(fname.c_str());
  218. write->Write();
  219. write->Delete();
  220. oct->GetLeafData()->RemoveArray( kri );
  221. oct->GetLeafData()->RemoveArray( kii );
  222. oct->GetLeafData()->RemoveArray( kmi );
  223. oct->GetLeafData()->RemoveArray( kidi );
  224. oct->GetLeafData()->RemoveArray( keri );
  225. kerr->Delete();
  226. kid->Delete();
  227. kr->Delete();
  228. ki->Delete();
  229. km->Delete();
  230. }
  231. curse->Delete();
  232. oct->Delete();
  233. #else
  234. throw std::runtime_error("IntegrateOnOctreeGrid with vtkOutput requires Lemma with VTK support");
  235. #endif
  236. }
  237. std::cout << "\nVOLSUM=" << VOLSUM << "\tActual=" << Size(0)*Size(1)*Size(2)
  238. << "\tDifference=" << VOLSUM - (Size(0)*Size(1)*Size(2)) << std::endl;
  239. }
  240. //--------------------------------------------------------------------------------------
  241. // Class: KernelV0
  242. // Method: f
  243. //--------------------------------------------------------------------------------------
  244. #if 1
  245. VectorXcr KernelV0::f( const Vector3r& r, const Real& volume, const Vector3cr& Ht, const Vector3cr& Hr ) {
  246. // Compute the elliptic fields
  247. Vector3r B0hat = SigmaModel->GetMagneticFieldUnitVector();
  248. Vector3r B0 = SigmaModel->GetMagneticField();
  249. // Elliptic representation
  250. EllipticB EBT = EllipticFieldRep(MU0*Ht, B0hat);
  251. EllipticB EBR = EllipticFieldRep(MU0*Hr, B0hat);
  252. // Compute Mn0
  253. Vector3r Mn0 = ComputeMn0(1.0, B0);
  254. //std::cout << "Mn0\t" << Mn0.transpose() << std::endl;
  255. Real Mn0Abs = Mn0.norm();
  256. // Compute phase delay
  257. // TODO add transmiiter current phase and delay induced apparent time phase!
  258. Complex PhaseTerm = EBR.bhat.dot(EBT.bhat) + Complex(0, (B0hat.dot(EBR.bhat.cross(EBT.bhat))));
  259. Complex ejztr = std::exp(Complex(0, EBR.zeta + EBT.zeta));
  260. // Calcuate vector of all responses
  261. VectorXcr F = VectorXcr::Zero( PulseI.size() );
  262. for (int iq=0; iq<PulseI.size(); ++iq) {
  263. // Compute the tipping angle
  264. Real sintheta = std::sin(0.5*GAMMA*PulseI(iq)*Taup*(EBT.alpha-EBT.beta));
  265. F(iq) = -volume*Complex(0,Larmor)*Mn0Abs*(EBR.alpha+EBR.beta)*ejztr*sintheta*PhaseTerm;
  266. //TODO TEST FOR ASYMETRY
  267. //Real sintheta = std::sin(0.5*GAMMA*PulseI(iq)*Taup*(EBT.alpha-EBT.beta));
  268. //F(iq) = volume * Complex(EBT.Bperp.real().norm(), EBT.Bperp.imag().norm()); //Complex(sintheta, EBT.Bperp.norm() );
  269. //F(iq) = volume * Complex(EBT.alpha, EBT.beta);
  270. //F(iq) = volume * EBT.err;
  271. //F(iq) = volume * sintheta;
  272. }
  273. return F;
  274. }
  275. #endif
  276. #if 0
  277. VectorXcr KernelV0::f( const Vector3r& r, const Real& volume, const Vector3cr& Ht, const Vector3cr& Hr ) {
  278. VectorXcr F = VectorXcr::Ones( PulseI.size() );
  279. F.array() *= volume * Complex(Ht.norm(), Hr.norm()); //*Ht.dot(Hr);
  280. return F;
  281. }
  282. #endif
  283. // //--------------------------------------------------------------------------------------
  284. // // Class: KernelV0
  285. // // Method: ComputeV0Cell
  286. // //--------------------------------------------------------------------------------------
  287. // Complex KernelV0::ComputeV0Cell(const EllipticB& EBT, const EllipticB& EBR,
  288. // const Real& sintheta, const Real& phase, const Real& Mn0Abs,
  289. // const Real& vol) {
  290. // // earth response of receiver adjoint field
  291. // Vector3r B0hat = SigmaModel->GetMagneticFieldUnitVector();
  292. // Complex ejztr = std::exp(Complex(0, EBR.zeta + EBT.zeta));
  293. // Complex PhaseTerm = EBR.bhat.dot(EBT.bhat) + (B0hat.dot(EBR.bhat.cross(EBT.bhat) ));
  294. // return -vol*Complex(0,Larmor)*Mn0Abs*(EBR.alpha+EBR.beta)*ejztr*sintheta*PhaseTerm;
  295. // }
  296. //--------------------------------------------------------------------------------------
  297. // Class: KernelV0
  298. // Method: ComputeV0Cell
  299. //--------------------------------------------------------------------------------------
  300. Vector3r KernelV0::ComputeMn0(const Real& Porosity, const Vector3r& B0) {
  301. Real chi_n = NH2O*((GAMMA*GAMMA*HBAR*HBAR)/(4.*KB*Temperature));
  302. return chi_n*Porosity*B0;
  303. }
  304. //--------------------------------------------------------------------------------------
  305. // Class: KernelV0
  306. // Method: ComputeV0Cell
  307. //--------------------------------------------------------------------------------------
  308. EllipticB KernelV0::EllipticFieldRep (const Vector3cr& B, const Vector3r& B0hat) {
  309. // This all follows Weichman et. al., 2000. There are some numerical stability isseus
  310. // below. Reformulating may be welcome, may be in B field calculation too.
  311. EllipticB ElipB = EllipticB();
  312. Vector3cr Bperp = B - B0hat.dot(B)*B0hat; // complex - real??
  313. //ElipB.BperpdotB = Bperp.dot(B0hat); // TODO remove
  314. Real BperpNorm = Bperp.norm();
  315. Complex Bp2 = Bperp.transpose() * Bperp;
  316. VectorXcr iB0 = Complex(0,1)*B0hat.cast<Complex>().array();
  317. ElipB.eizt = std::sqrt(Bp2 / std::abs(Bp2));
  318. ElipB.alpha = INVSQRT2*std::sqrt(BperpNorm*BperpNorm + std::abs(Bp2));
  319. ElipB.beta = std::copysign(1, std::real(iB0.dot( Bperp.cross(Bperp.conjugate())) )) *
  320. (INVSQRT2*std::sqrt(BperpNorm*BperpNorm - std::abs(Bp2)));
  321. ElipB.bhat = ((Real)1./ElipB.alpha)*(((Real)1./ElipB.eizt)*Bperp.array()).real().array();
  322. ElipB.bhatp = B0hat.cross(ElipB.bhat);
  323. ElipB.zeta = std::real(std::log(ElipB.eizt)/Complex(0,1));
  324. /* use as an error check decomposed field - computed actual */
  325. Vector3cr Bperp2 = ElipB.eizt * (ElipB.alpha * ElipB.bhat
  326. + (Complex(0,1) * ElipB.beta * ElipB.bhatp) );
  327. ElipB.err = (Bperp-Bperp2).norm();
  328. if (ElipB.err > 1e-12) {
  329. std::cout << "Elip error\n";
  330. std::cout << "Bperp \t" << Bperp.transpose() << std::endl;
  331. std::cout << "Bperp2\t" << Bperp2.transpose() << std::endl;
  332. std::cout << "err \t" << ElipB.err << std::endl;
  333. }
  334. //std::cout << "B0\t" << B0hat.transpose() << std::endl;
  335. return ElipB;
  336. }
  337. //--------------------------------------------------------------------------------------
  338. // Class: KernelV0
  339. // Method: EvaluateKids
  340. //--------------------------------------------------------------------------------------
  341. void KernelV0::EvaluateKids( const Vector3r& size, const int& level, const Vector3r& cpos,
  342. const VectorXcr& parentVal ) {
  343. std::cout << "\r" << (int)(1e2*VOLSUM/(Size[0]*Size[1]*Size[2])) << "\t" << nleaves;
  344. //std::cout.flush();
  345. // Next level step, interested in one level below
  346. // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
  347. Vector3r step = size.array() / (Real)(1 << (level+1) );
  348. Real vol = (step(0)*step(1)*step(2)); // volume of each child
  349. Vector3r pos = cpos - step/2.;
  350. Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
  351. 0, 0, 0,
  352. step[0], 0, 0,
  353. 0, step[1], 0,
  354. step[0], step[1], 0,
  355. 0, 0, step[2],
  356. step[0], 0, step[2],
  357. 0, step[1], step[2],
  358. step[0], step[1], step[2] ).finished();
  359. MatrixXcr kvals(8, PulseI.size()); // individual kernel vals
  360. cpoints->ClearFields();
  361. for (int ichild=0; ichild<8; ++ichild) {
  362. Vector3r cp = pos; // Eigen complains about combining these
  363. cp += posadd.row(ichild);
  364. cpoints->SetLocation( ichild, cp );
  365. }
  366. Eigen::Matrix<Complex, 3, 8> Ht = Eigen::Matrix<Complex, 3, 8>::Zero();
  367. Eigen::Matrix<Complex, 3, 8> Hr = Eigen::Matrix<Complex, 3, 8>::Zero();
  368. for ( auto EMCalc : EMEarths ) {
  369. EMCalc.second->GetFieldPoints()->ClearFields();
  370. EMCalc.second->CalculateWireAntennaFields();
  371. switch (EMCalc.second->GetTxRxMode()) {
  372. case TX:
  373. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  374. break;
  375. case RX:
  376. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  377. break;
  378. case TXRX:
  379. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  380. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  381. break;
  382. default:
  383. break;
  384. }
  385. }
  386. for (int ichild=0; ichild<8; ++ichild) {
  387. Vector3r cp = pos; // Eigen complains about combining these
  388. cp += posadd.row(ichild);
  389. kvals.row(ichild) = f(cp, vol, Ht.col(ichild), Hr.col(ichild));
  390. }
  391. VectorXcr ksum = kvals.colwise().sum(); // Kernel sum
  392. // Evaluate whether or not furthur splitting is needed
  393. if ( (((ksum - parentVal).array().abs() > tol).any() && level<maxLevel) || level < minLevel ) {
  394. // Not a leaf dive further in
  395. for (int ichild=0; ichild<8; ++ichild) {
  396. Vector3r cp = pos; // Eigen complains about combining these
  397. cp += posadd.row(ichild);
  398. EvaluateKids( size, level+1, cp, kvals.row(ichild) );
  399. }
  400. return; // not leaf
  401. }
  402. // implicit else, is a leaf
  403. Kern.row(ilay) += ksum;
  404. VOLSUM += 8.*vol;
  405. nleaves += 8; // reflects the number of kernel evaluations
  406. return; // is leaf
  407. }
  408. #ifdef LEMMAUSEVTK
  409. //--------------------------------------------------------------------------------------
  410. // Class: KernelV0
  411. // Method: EvaluateKids2 -- same as Evaluate Kids, but include VTK octree generation
  412. //--------------------------------------------------------------------------------------
  413. void KernelV0::EvaluateKids2( const Vector3r& size, const int& level, const Vector3r& cpos,
  414. const VectorXcr& parentVal, vtkHyperOctree* oct, vtkHyperOctreeCursor* curse) {
  415. std::cout << "\r" << (int)(1e2*VOLSUM/(Size[0]*Size[1]*Size[2])) << "\t" << nleaves;
  416. //std::cout.flush();
  417. // Next level step, interested in one level below
  418. // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
  419. Vector3r step = size.array() / (Real)(1 << (level+1) );
  420. Real vol = (step(0)*step(1)*step(2)); // volume of each child
  421. Vector3r pos = cpos - step/2.;
  422. Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
  423. 0, 0, 0,
  424. step[0], 0, 0,
  425. 0, step[1], 0,
  426. step[0], step[1], 0,
  427. 0, 0, step[2],
  428. step[0], 0, step[2],
  429. 0, step[1], step[2],
  430. step[0], step[1], step[2] ).finished();
  431. MatrixXcr kvals(8, PulseI.size()); // individual kernel vals
  432. cpoints->ClearFields();
  433. for (int ichild=0; ichild<8; ++ichild) {
  434. Vector3r cp = pos; // Eigen complains about combining these
  435. cp += posadd.row(ichild);
  436. cpoints->SetLocation( ichild, cp );
  437. }
  438. Eigen::Matrix<Complex, 3, 8> Ht = Eigen::Matrix<Complex, 3, 8>::Zero();
  439. Eigen::Matrix<Complex, 3, 8> Hr = Eigen::Matrix<Complex, 3, 8>::Zero();
  440. for ( auto EMCalc : EMEarths ) {
  441. //EMCalc->GetFieldPoints()->ClearFields();
  442. EMCalc.second->CalculateWireAntennaFields();
  443. switch (EMCalc.second->GetTxRxMode()) {
  444. case TX:
  445. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  446. break;
  447. case RX:
  448. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  449. break;
  450. case TXRX:
  451. Ht += EMCalc.second->GetFieldPoints()->GetHfield(0);
  452. Hr += EMCalc.second->GetFieldPoints()->GetHfield(0);
  453. break;
  454. default:
  455. break;
  456. }
  457. }
  458. for (int ichild=0; ichild<8; ++ichild) {
  459. Vector3r cp = pos; // Eigen complains about combining these
  460. cp += posadd.row(ichild);
  461. kvals.row(ichild) = f(cp, vol, Ht.col(ichild), Hr.col(ichild));
  462. }
  463. VectorXcr ksum = kvals.colwise().sum(); // Kernel sum
  464. // Evaluate whether or not furthur splitting is needed
  465. if ( (((ksum - parentVal).array().abs() > tol).any() && level<maxLevel) || level < minLevel ) {
  466. oct->SubdivideLeaf(curse);
  467. for (int ichild=0; ichild<8; ++ichild) {
  468. curse->ToChild(ichild);
  469. Vector3r cp = pos; // Eigen complains about combining these
  470. cp += posadd.row(ichild);
  471. /* Test for position via alternative means */
  472. /*
  473. Real p[3];
  474. GetPosition(curse, p);
  475. if ( (Vector3r(p) - cp).norm() > 1e-8 ) {
  476. std::cout << "ERROR @ nleaves" << nleaves << "\n" << cp[0] << "\t" << p[0] << "\t" << cp[1] << "\t" << p[1]
  477. << "\t" << cp[2] << "\t" << p[2] << "\t" << vol<< std::endl;
  478. throw std::runtime_error("doom");
  479. }
  480. */
  481. /* End of position test */
  482. EvaluateKids2( size, level+1, cp, kvals.row(ichild), oct, curse );
  483. curse->ToParent();
  484. }
  485. return; // not a leaf
  486. }
  487. /* just stuff with sum of the kids and don't subdivide */
  488. /*
  489. LeafDict[curse->GetLeafId()] = ksum/(8.*vol);
  490. LeafDictIdx[curse->GetLeafId()] = nleaves;
  491. */
  492. /* Alternatively, subdivide the VTK octree here and stuff the children to make better
  493. * visuals, but also 8x the storage...
  494. */
  495. oct->SubdivideLeaf(curse);
  496. for (int ichild=0; ichild<8; ++ichild) {
  497. curse->ToChild(ichild);
  498. LeafDict[curse->GetLeafId()] = ksum/(8.*vol);
  499. LeafDictIdx[curse->GetLeafId()] = nleaves;
  500. curse->ToParent();
  501. }
  502. Kern.row(ilay) += ksum;
  503. VOLSUM += 8*vol;
  504. nleaves += 8; // good reason to say 1 or 8 here...8 sounds better and reflects kernel evaluations
  505. return; // is a leaf
  506. }
  507. //--------------------------------------------------------------------------------------
  508. // Class: KernelV0
  509. // Method: GetPosition
  510. //--------------------------------------------------------------------------------------
  511. void KernelV0::GetPosition( vtkHyperOctreeCursor* Cursor, Real* p ) {
  512. Real ratio=1.0/(1<<(Cursor->GetCurrentLevel()));
  513. //step = ((Size).array() / std::pow(2.,Cursor->GetCurrentLevel()));
  514. p[0]=(Cursor->GetIndex(0)+.5)*ratio*this->Size[0]+this->Origin[0] ;//+ .5*step[0];
  515. p[1]=(Cursor->GetIndex(1)+.5)*ratio*this->Size[1]+this->Origin[1] ;//+ .5*step[1];
  516. p[2]=(Cursor->GetIndex(2)+.5)*ratio*this->Size[2]+this->Origin[2] ;//+ .5*step[2];
  517. }
  518. #endif
  519. } // ---- end of namespace Lemma ----
  520. /* vim: set tabstop=4 expandtab */
  521. /* vim: set filetype=cpp */