Surface NMR forward modelling
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

KernelV0.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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-5, 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,200;
  109. //Vector3r Origin;
  110. Origin << 0,0,1.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. // Vn(ir) = ComputeV0Cell(Bt, Br, volume, 1.0);
  172. }
  173. //--------------------------------------------------------------------------------------
  174. // Class: KernelV0
  175. // Method: EvaluateKids
  176. //--------------------------------------------------------------------------------------
  177. bool KernelV0::EvaluateKids( const Vector3r& size, const int& level, const Vector3r& cpos,
  178. const Complex& parentVal ) {
  179. std::cout << "\r" << (int)(1e2*VOLSUM/(Size[0]*Size[1]*Size[2])) << "\t" << nleaves;
  180. std::cout.flush();
  181. // Next level step, interested in one level below
  182. // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
  183. Vector3r step = size.array() / (Real)(1 << (level+1) );
  184. Vector3r step2 = size.array() / (Real)(1 << (level+2) );
  185. Real vol = (step2(0)*step2(1)*step2(2)); // volume of each child
  186. Vector3r pos = cpos - step/2.;
  187. Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
  188. 0, 0, 0,
  189. step[0], 0, 0,
  190. 0, step[1], 0,
  191. step[0], step[1], 0,
  192. 0, 0, step[2],
  193. step[0], 0, step[2],
  194. 0, step[1], step[2],
  195. step[0], step[1], step[2] ).finished();
  196. VectorXcr kvals(8); // individual kernel vals
  197. FieldPoints* cpoints = EMEarths[0]->GetFieldPoints();
  198. cpoints->ClearFields();
  199. for (int ichild=0; ichild<8; ++ichild) {
  200. Vector3r cp = pos; // Eigen complains about combining these
  201. cp += posadd.row(ichild);
  202. cpoints->SetLocation( ichild, cp );
  203. }
  204. Vector3Xcr Bt;
  205. //Eigen::Matrix< Complex, 8, 3 > Bt;
  206. for ( auto EMCalc : EMEarths ) {
  207. //EMCalc->GetFieldPoints()->ClearFields();
  208. EMCalc->CalculateWireAntennaFields();
  209. Bt = EMCalc->GetFieldPoints()->GetHfield(0);
  210. }
  211. for (int ichild=0; ichild<8; ++ichild) {
  212. Vector3r cp = pos; // Eigen complains about combining these
  213. cp += posadd.row(ichild);
  214. kvals(ichild) = f(cp, vol, Bt.col(ichild));
  215. }
  216. Complex ksum = kvals.sum(); // Kernel sum
  217. // Evaluate whether or not furthur splitting is needed
  218. if ( std::abs(ksum - parentVal) > tol || level < 2 ) {
  219. for (int ichild=0; ichild<8; ++ichild) {
  220. Vector3r cp = pos; // Eigen complains about combining these
  221. cp += posadd.row(ichild);
  222. bool isleaf = EvaluateKids( size, level+1, cp, kvals(ichild) );
  223. if (isleaf) { // Include result in final integral
  224. SUM += ksum;
  225. VOLSUM += 8.*vol;
  226. nleaves += 1;
  227. }
  228. }
  229. return false; // not leaf
  230. }
  231. // Save here instead?
  232. return true; // leaf
  233. }
  234. #ifdef LEMMAUSEVTK
  235. //--------------------------------------------------------------------------------------
  236. // Class: KernelV0
  237. // Method: EvaluateKids2 -- same as Evaluate Kids, but include VTK octree generation
  238. //--------------------------------------------------------------------------------------
  239. bool KernelV0::EvaluateKids2( const Vector3r& size, const int& level, const Vector3r& cpos,
  240. const Complex& parentVal, vtkHyperOctree* oct, vtkHyperOctreeCursor* curse) {
  241. std::cout << "\r" << (int)(1e2*VOLSUM/(Size[0]*Size[1]*Size[2])) << "\t" << nleaves;
  242. std::cout.flush();
  243. // Next level step, interested in one level below
  244. // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
  245. Vector3r step = size.array() / (Real)(1 << (level+1) );
  246. Vector3r step2 = size.array() / (Real)(1 << (level+2) );
  247. Real vol = (step2(0)*step2(1)*step2(2)); // volume of each child
  248. Vector3r pos = cpos - step/2.;
  249. Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
  250. 0, 0, 0,
  251. step[0], 0, 0,
  252. 0, step[1], 0,
  253. step[0], step[1], 0,
  254. 0, 0, step[2],
  255. step[0], 0, step[2],
  256. 0, step[1], step[2],
  257. step[0], step[1], step[2] ).finished();
  258. VectorXcr kvals(8); // individual kernel vals
  259. FieldPoints* cpoints = EMEarths[0]->GetFieldPoints();
  260. cpoints->ClearFields();
  261. for (int ichild=0; ichild<8; ++ichild) {
  262. Vector3r cp = pos; // Eigen complains about combining these
  263. cp += posadd.row(ichild);
  264. cpoints->SetLocation( ichild, cp );
  265. }
  266. Vector3Xcr Bt;
  267. for ( auto EMCalc : EMEarths ) {
  268. //EMCalc->GetFieldPoints()->ClearFields();
  269. EMCalc->CalculateWireAntennaFields();
  270. Bt = EMCalc->GetFieldPoints()->GetHfield(0);
  271. }
  272. for (int ichild=0; ichild<8; ++ichild) {
  273. Vector3r cp = pos; // Eigen complains about combining these
  274. cp += posadd.row(ichild);
  275. kvals(ichild) = f(cp, vol, Bt.col(ichild));
  276. }
  277. Complex ksum = kvals.sum(); // Kernel sum
  278. // Evaluate whether or not furthur splitting is needed
  279. if ( std::abs(ksum - parentVal) > tol || level < 2 ) {
  280. oct->SubdivideLeaf(curse);
  281. for (int ichild=0; ichild<8; ++ichild) {
  282. curse->ToChild(ichild);
  283. Vector3r cp = pos; // Eigen complains about combining these
  284. cp += posadd.row(ichild);
  285. // Testing for position via alternative means
  286. //Real p[3];
  287. //GetPosition(curse, p);
  288. //std::cout << cp[0] << "\t" << p[0] << "\t" << cp[1] << "\t" << p[1] << "\t" << cp[2] << "\t" << p[2] << "\t" << vol<< std::endl;
  289. bool isleaf = EvaluateKids2( size, level+1, cp, kvals(ichild), oct, curse );
  290. if (isleaf) { // Include result in final integral
  291. LeafDict[curse->GetLeafId()] = kvals(ichild); // VTK
  292. SUM += ksum;
  293. VOLSUM += 8*vol;
  294. nleaves += 1;
  295. }
  296. curse->ToParent();
  297. }
  298. return false; // not leaf
  299. }
  300. return true; // leaf
  301. }
  302. //--------------------------------------------------------------------------------------
  303. // Class: KernelV0
  304. // Method: GetPosition
  305. //--------------------------------------------------------------------------------------
  306. void KernelV0::GetPosition( vtkHyperOctreeCursor* Cursor, Real* p ) {
  307. Real ratio=1.0/(1<<(Cursor->GetCurrentLevel()));
  308. //step = ((Size).array() / std::pow(2.,Cursor->GetCurrentLevel()));
  309. p[0]=(Cursor->GetIndex(0)+.5)*ratio*this->Size[0]+this->Origin[0] ;//+ .5*step[0];
  310. p[1]=(Cursor->GetIndex(1)+.5)*ratio*this->Size[1]+this->Origin[1] ;//+ .5*step[1];
  311. p[2]=(Cursor->GetIndex(2)+.5)*ratio*this->Size[2]+this->Origin[2] ;//+ .5*step[2];
  312. }
  313. #endif
  314. } // ---- end of namespace Lemma ----
  315. /* vim: set tabstop=4 expandtab */
  316. /* vim: set filetype=cpp */