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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. * @version $Id$
  12. * @author Trevor Irons (ti)
  13. * @email tirons@egi.utah.edu
  14. * @copyright Copyright (c) 2016, University of Utah
  15. * @copyright Copyright (c) 2016, Lemma Software, LLC
  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. return node;
  69. } // ----- end of method KernelV0::Serialize -----
  70. //--------------------------------------------------------------------------------------
  71. // Class: KernelV0
  72. // Method: DeSerialize
  73. //--------------------------------------------------------------------------------------
  74. std::shared_ptr<KernelV0> KernelV0::DeSerialize ( const YAML::Node& node ) {
  75. if (node.Tag() != "KernelV0" ) {
  76. throw DeSerializeTypeMismatch( "KernelV0", node.Tag());
  77. }
  78. return std::make_shared< KernelV0 > ( node, ctor_key() );
  79. } // ----- end of method KernelV0::DeSerialize -----
  80. //--------------------------------------------------------------------------------------
  81. // Class: KernelV0
  82. // Method: DeSerialize
  83. //--------------------------------------------------------------------------------------
  84. void KernelV0::CalculateK0 (const std::vector< std::string>& Tx, const std::vector<std::string >& Rx,
  85. bool vtkOutput ) {
  86. // All EM calculations will share same field points
  87. auto points = FieldPoints::NewSP();
  88. points->SetNumberOfPoints(8);
  89. for (auto tx : Tx) {
  90. // Set up EMEarth
  91. EMEarths.push_back( EMEarth1D::NewSP() );
  92. EMEarths.back()->AttachWireAntenna(TxRx[tx]);
  93. EMEarths.back()->AttachLayeredEarthEM(SigmaModel);
  94. EMEarths.back()->AttachFieldPoints( points );
  95. EMEarths.back()->SetFieldsToCalculate(H);
  96. // TODO query for method, altough with flat antennae, this is fastest
  97. EMEarths.back()->SetHankelTransformMethod(ANDERSON801);
  98. }
  99. IntegrateOnOctreeGrid( 1e-1, vtkOutput );
  100. }
  101. //--------------------------------------------------------------------------------------
  102. // Class: KernelV0
  103. // Method: IntegrateOnOctreeGrid
  104. //--------------------------------------------------------------------------------------
  105. void KernelV0::IntegrateOnOctreeGrid( const Real& tolerance, bool vtkOutput) {
  106. this->tol = tolerance;
  107. //Vector3r Size;
  108. Size << 200,200,100;
  109. //Vector3r Origin;
  110. Origin << 0,0,0;
  111. Vector3r cpos; // centre position
  112. //cpos << 100,100,50;
  113. cpos = (Size-Origin).array() / 2.;
  114. int maxlevel;
  115. SUM = 0;
  116. VOLSUM = 0;
  117. nleaves = 0;
  118. if (!vtkOutput) {
  119. EvaluateKids( Size, 0, cpos, 1e6 );
  120. } else {
  121. #ifdef LEMMAUSEVTK
  122. vtkHyperOctree* oct = vtkHyperOctree::New();
  123. oct->SetDimension(3);
  124. oct->SetOrigin( Origin(0), Origin(1), Origin(2) );
  125. oct->SetSize( Size(0), Size(1), Size(2) );
  126. vtkHyperOctreeCursor* curse = oct->NewCellCursor();
  127. curse->ToRoot();
  128. EvaluateKids2( Size, 0, cpos, 1e6, oct, curse );
  129. // Fill in leaf data
  130. vtkDoubleArray* kr = vtkDoubleArray::New();
  131. kr->SetNumberOfComponents(1);
  132. kr->SetName("Re($K_0$)");
  133. kr->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  134. vtkDoubleArray* ki = vtkDoubleArray::New();
  135. ki->SetNumberOfComponents(1);
  136. ki->SetName("Im($K_0$)");
  137. ki->SetNumberOfTuples( oct->GetNumberOfLeaves() );
  138. for (auto leaf : LeafDict) {
  139. kr->InsertTuple1( leaf.first, std::real(leaf.second) );
  140. ki->InsertTuple1( leaf.first, std::imag(leaf.second) );
  141. }
  142. oct->GetLeafData()->AddArray(kr);
  143. oct->GetLeafData()->AddArray(ki);
  144. auto write = vtkXMLHyperOctreeWriter::New();
  145. //write.SetDataModeToAscii()
  146. write->SetInputData(oct);
  147. write->SetFileName("octree.vto");
  148. write->Write();
  149. write->Delete();
  150. kr->Delete();
  151. ki->Delete();
  152. curse->Delete();
  153. oct->Delete();
  154. #else
  155. throw std::runtime_error("IntegrateOnOctreeGrid with vtkOutput requires Lemma with VTK support");
  156. #endif
  157. }
  158. std::cout << "\nVOLSUM=" << VOLSUM << "\tActual=" << Size(0)*Size(1)*Size(2) << "\tDifference=" << VOLSUM - (Size(0)*Size(1)*Size(2)) << std::endl;
  159. std::cout << "nleaves\t" << nleaves << std::endl;
  160. std::cout << "KSUM\t" << SUM << std::endl;
  161. }
  162. //--------------------------------------------------------------------------------------
  163. // Class: KernelV0
  164. // Method: f
  165. //--------------------------------------------------------------------------------------
  166. Complex KernelV0::f( const Vector3r& r, const Real& volume, const Vector3cr& Bt ) {
  167. //std::cout << volume*Bt.norm() << std::endl;
  168. //return Complex(volume*Bt.norm());
  169. return Complex(volume*Bt.norm());
  170. //return Complex(volume);
  171. }
  172. //--------------------------------------------------------------------------------------
  173. // Class: KernelV0
  174. // Method: EvaluateKids
  175. //--------------------------------------------------------------------------------------
  176. bool KernelV0::EvaluateKids( const Vector3r& size, const int& level, const Vector3r& cpos,
  177. const Complex& parentVal ) {
  178. // Next level step, interested in one level below
  179. // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
  180. Vector3r step = size.array() / (Real)(1 << (level+1) );
  181. Vector3r step2 = size.array() / (Real)(1 << (level+2) );
  182. Real vol = (step2(0)*step2(1)*step2(2)); // volume of each child
  183. Vector3r pos = cpos - step/2.;
  184. Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
  185. 0, 0, 0,
  186. step[0], 0, 0,
  187. 0, step[1], 0,
  188. step[0], step[1], 0,
  189. 0, 0, step[2],
  190. step[0], 0, step[2],
  191. 0, step[1], step[2],
  192. step[0], step[1], step[2] ).finished();
  193. VectorXcr kvals(8); // individual kernel vals
  194. FieldPoints* cpoints = EMEarths[0]->GetFieldPoints();
  195. cpoints->ClearFields();
  196. for (int ichild=0; ichild<8; ++ichild) {
  197. Vector3r cp = pos; // Eigen complains about combining these
  198. cp += posadd.row(ichild);
  199. cpoints->SetLocation( ichild, cp );
  200. }
  201. Vector3Xcr Bt;
  202. //Eigen::Matrix< Complex, 8, 3 > Bt;
  203. for ( auto EMCalc : EMEarths ) {
  204. //EMCalc->GetFieldPoints()->ClearFields();
  205. EMCalc->CalculateWireAntennaFields();
  206. Bt = EMCalc->GetFieldPoints()->GetHfield(0);
  207. }
  208. for (int ichild=0; ichild<8; ++ichild) {
  209. Vector3r cp = pos; // Eigen complains about combining these
  210. cp += posadd.row(ichild);
  211. kvals(ichild) = f(cp, vol, Bt.col(ichild));
  212. }
  213. Complex ksum = kvals.sum(); // Kernel sum
  214. // Evaluate whether or not furthur splitting is needed
  215. if ( std::abs(ksum - parentVal) > tol || level < 2 ) {
  216. for (int ichild=0; ichild<8; ++ichild) {
  217. Vector3r cp = pos; // Eigen complains about combining these
  218. cp += posadd.row(ichild);
  219. bool isleaf = EvaluateKids( size, level+1, cp, kvals(ichild) );
  220. if (isleaf) { // Include result in final integral
  221. SUM += ksum;
  222. VOLSUM += 8.*vol;
  223. nleaves += 1;
  224. }
  225. }
  226. return false; // not leaf
  227. }
  228. // Save here instead?
  229. return true; // leaf
  230. }
  231. #ifdef LEMMAUSEVTK
  232. //--------------------------------------------------------------------------------------
  233. // Class: KernelV0
  234. // Method: EvaluateKids2 -- same as Evaluate Kids, but include VTK octree generation
  235. //--------------------------------------------------------------------------------------
  236. bool KernelV0::EvaluateKids2( const Vector3r& size, const int& level, const Vector3r& cpos,
  237. const Complex& parentVal, vtkHyperOctree* oct, vtkHyperOctreeCursor* curse) {
  238. //std::cout << "\rlevel " << level << "\t" << nleaves;
  239. //std::cout.flush();
  240. // Next level step, interested in one level below
  241. // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
  242. Vector3r step = size.array() / (Real)(1 << (level+1) );
  243. Vector3r step2 = size.array() / (Real)(1 << (level+2) );
  244. Real vol = (step2(0)*step2(1)*step2(2)); // volume of each child
  245. Vector3r pos = cpos - step/2.;
  246. Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
  247. 0, 0, 0,
  248. step[0], 0, 0,
  249. 0, step[1], 0,
  250. step[0], step[1], 0,
  251. 0, 0, step[2],
  252. step[0], 0, step[2],
  253. 0, step[1], step[2],
  254. step[0], step[1], step[2] ).finished();
  255. VectorXcr kvals(8); // individual kernel vals
  256. FieldPoints* cpoints = EMEarths[0]->GetFieldPoints();
  257. cpoints->ClearFields();
  258. for (int ichild=0; ichild<8; ++ichild) {
  259. Vector3r cp = pos; // Eigen complains about combining these
  260. cp += posadd.row(ichild);
  261. cpoints->SetLocation( ichild, cp );
  262. }
  263. Vector3Xcr Bt;
  264. for ( auto EMCalc : EMEarths ) {
  265. //EMCalc->GetFieldPoints()->ClearFields();
  266. EMCalc->CalculateWireAntennaFields();
  267. Bt = EMCalc->GetFieldPoints()->GetHfield(0);
  268. }
  269. for (int ichild=0; ichild<8; ++ichild) {
  270. Vector3r cp = pos; // Eigen complains about combining these
  271. cp += posadd.row(ichild);
  272. kvals(ichild) = f(cp, vol, Bt.col(ichild));
  273. }
  274. Complex ksum = kvals.sum(); // Kernel sum
  275. // Evaluate whether or not furthur splitting is needed
  276. if ( std::abs(ksum - parentVal) > tol || level < 2 ) {
  277. oct->SubdivideLeaf(curse);
  278. for (int ichild=0; ichild<8; ++ichild) {
  279. curse->ToChild(ichild);
  280. Vector3r cp = pos; // Eigen complains about combining these
  281. cp += posadd.row(ichild);
  282. //Real p[3];
  283. //GetPosition(curse, p);
  284. //std::cout << cp[0] << "\t" << p[0] << "\t" << cp[1] << "\t" << p[1] << "\t" << cp[2] << "\t" << p[2] << "\t" << vol<< std::endl;
  285. bool isleaf = EvaluateKids2( size, level+1, cp, kvals(ichild), oct, curse );
  286. if (isleaf) { // Include result in final integral
  287. LeafDict[curse->GetLeafId()] = kvals(ichild); // VTK
  288. SUM += ksum;
  289. VOLSUM += 8*vol;
  290. nleaves += 1;
  291. }
  292. curse->ToParent();
  293. }
  294. return false; // not leaf
  295. }
  296. return true; // leaf
  297. }
  298. void KernelV0::GetPosition( vtkHyperOctreeCursor* Cursor, Real* p ) {
  299. Real ratio=1.0/(1<<(Cursor->GetCurrentLevel()));
  300. //step = ((Size).array() / std::pow(2.,Cursor->GetCurrentLevel()));
  301. p[0]=(Cursor->GetIndex(0)+.5)*ratio*this->Size[0]+this->Origin[0] ;//+ .5*step[0];
  302. p[1]=(Cursor->GetIndex(1)+.5)*ratio*this->Size[1]+this->Origin[1] ;//+ .5*step[1];
  303. p[2]=(Cursor->GetIndex(2)+.5)*ratio*this->Size[2]+this->Origin[2] ;//+ .5*step[2];
  304. }
  305. #endif
  306. } // ---- end of namespace Lemma ----
  307. /* vim: set tabstop=4 expandtab */
  308. /* vim: set filetype=cpp */