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

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