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.

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