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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "EMEarth1D.h"
  19. #include "FieldPoints.h"
  20. namespace Lemma {
  21. // ==================== FRIEND METHODS =====================
  22. std::ostream &operator << (std::ostream &stream, const KernelV0 &ob) {
  23. stream << ob.Serialize() << "\n---\n"; // End of doc ---
  24. return stream;
  25. }
  26. // ==================== LIFECYCLE =======================
  27. //--------------------------------------------------------------------------------------
  28. // Class: KernelV0
  29. // Method: KernelV0
  30. // Description: constructor (locked)
  31. //--------------------------------------------------------------------------------------
  32. KernelV0::KernelV0 (const ctor_key&) : LemmaObject( ) {
  33. } // ----- end of method KernelV0::KernelV0 (constructor) -----
  34. //--------------------------------------------------------------------------------------
  35. // Class: KernelV0
  36. // Method: KernelV0
  37. // Description: DeSerializing constructor (locked)
  38. //--------------------------------------------------------------------------------------
  39. KernelV0::KernelV0 (const YAML::Node& node, const ctor_key&) : LemmaObject(node) {
  40. } // ----- end of method KernelV0::KernelV0 (constructor) -----
  41. //--------------------------------------------------------------------------------------
  42. // Class: KernelV0
  43. // Method: NewSP()
  44. // Description: public constructor returing a shared_ptr
  45. //--------------------------------------------------------------------------------------
  46. std::shared_ptr< KernelV0 > KernelV0::NewSP() {
  47. return std::make_shared< KernelV0 >( ctor_key() );
  48. }
  49. //--------------------------------------------------------------------------------------
  50. // Class: KernelV0
  51. // Method: ~KernelV0
  52. // Description: destructor (protected)
  53. //--------------------------------------------------------------------------------------
  54. KernelV0::~KernelV0 () {
  55. } // ----- end of method KernelV0::~KernelV0 (destructor) -----
  56. //--------------------------------------------------------------------------------------
  57. // Class: KernelV0
  58. // Method: Serialize
  59. //--------------------------------------------------------------------------------------
  60. YAML::Node KernelV0::Serialize ( ) const {
  61. YAML::Node node = LemmaObject::Serialize();
  62. node.SetTag( GetName() );
  63. // Coils Transmitters & Receivers
  64. for ( auto txm : TxRx) {
  65. node[txm.first] = txm.second->Serialize();
  66. }
  67. // LayeredEarthEM
  68. node["SigmaModel"] = SigmaModel->Serialize();
  69. return node;
  70. } // ----- end of method KernelV0::Serialize -----
  71. //--------------------------------------------------------------------------------------
  72. // Class: KernelV0
  73. // Method: DeSerialize
  74. //--------------------------------------------------------------------------------------
  75. std::shared_ptr<KernelV0> KernelV0::DeSerialize ( const YAML::Node& node ) {
  76. if (node.Tag() != "KernelV0" ) {
  77. throw DeSerializeTypeMismatch( "KernelV0", node.Tag());
  78. }
  79. return std::make_shared< KernelV0 > ( node, ctor_key() );
  80. } // ----- end of method KernelV0::DeSerialize -----
  81. //--------------------------------------------------------------------------------------
  82. // Class: KernelV0
  83. // Method: DeSerialize
  84. //--------------------------------------------------------------------------------------
  85. void KernelV0::CalculateK0 (const std::vector< std::string>& Tx, const std::vector<std::string >& Rx ) {
  86. for (auto tx : Tx) {
  87. // Set up EMEarth
  88. auto EmEarth = EMEarth1D::NewSP();
  89. EmEarth->AttachWireAntenna(TxRx[tx]);
  90. EmEarth->AttachLayeredEarthEM(SigmaModel);
  91. EmEarth->SetFieldsToCalculate(H);
  92. // TODO query for method, altough with flat antennae, this is fastest
  93. EmEarth->SetHankelTransformMethod(ANDERSON801);
  94. // EmEarth->AttachFieldPoints(receivers);
  95. // //EmEarth->SetHankelTransformMethod(FHTKEY101);
  96. // EmEarth->CalculateWireAntennaFields();
  97. // Vector3Xcr Rx1 = receivers->GetHfield(0);
  98. // //receivers->ClearFields();
  99. //
  100. // //EmEarth->AttachWireAntenna(Tx2);
  101. // EmEarth->CalculateWireAntennaFields();
  102. // Rx1 += receivers->GetHfield(0);
  103. }
  104. }
  105. //--------------------------------------------------------------------------------------
  106. // Class: KernelV0
  107. // Method: IntegrateOnOctreeGrid
  108. //--------------------------------------------------------------------------------------
  109. void KernelV0::IntegrateOnOctreeGrid( const Real& tolerance) {
  110. Vector3r Size;
  111. Vector3r Origin;
  112. Vector3r step;
  113. Vector3r cpos;
  114. int level;
  115. int maxlevel;
  116. int index;
  117. int counter;
  118. Real cvol;
  119. Real tvol;
  120. Real tol;
  121. Complex KernelSum;
  122. //this->tol = tolerance;
  123. Real KernelSum = 0.;
  124. //Cursor->ToRoot();
  125. //Cubes->SetNumberOfReceivers(8);
  126. EvaluateKids( 1e9 ); // Large initial number don't waste time actually computing
  127. //EvaluateKids();
  128. //std::cout << "Kernel Sum from Generate Mesh "
  129. // << std::real(KernelSum) << "\t" << std::imag(KernelSum) << std::endl;
  130. // old VTK thingy
  131. //SetLeafDataFromGridCreation();
  132. }
  133. //--------------------------------------------------------------------------------------
  134. // Class: KernelV0
  135. // Method: EvaluateKids
  136. //--------------------------------------------------------------------------------------
  137. void KernelV0::EvaluateKids(const Complex& kval) {
  138. assert("Evaluate Kids pre" && Cursor->CurrentIsLeaf());
  139. vtkHyperOctreeCursor *tcurse = Cursor->Clone();
  140. Real p[3];
  141. Octree->SubdivideLeaf(Cursor);
  142. tcurse->ToSameNode(Cursor);
  143. std::cout << "\rPredivide Leaf count: " << Octree->GetNumberOfLeaves();
  144. //std::cout.flush();
  145. for (int child=0; child<8; ++child) {
  146. Cursor->ToChild(child);
  147. assert(Cursor->CurrentIsLeaf());
  148. // Build cube
  149. GetPosition(p);
  150. cpos << p[0], p[1], p[2];
  151. step = ((Size).array() / std::pow(2.,Cursor->GetCurrentLevel()));
  152. Cubes->SetLocation(child, cpos);
  153. Cubes->SetLength(child, step);
  154. //std::cout << "child " << child << " cpos\t" << cpos.transpose() << std::endl;
  155. //std::cout << "child " << child << " step\t" << step.transpose() << std::endl;
  156. Cursor->ToSameNode(tcurse);
  157. }
  158. // make calculation
  159. Cubes->ClearFields();
  160. VectorXcr f = SenseKernel->ComputeSensitivity();
  161. if ( std::abs(std::abs(kval) - std::abs(f.array().abs().sum())) <= tol ||
  162. Cursor->GetCurrentLevel() >= maxlevel ) {
  163. // stop subdividing, save result
  164. for (int child=0; child < 8; ++ child) {
  165. Cursor->ToChild(child);
  166. leafdata.push_back( std::abs(f(child)) / Cubes->GetVolume(child) );
  167. // TODO fval is just a test
  168. //leafdata.push_back( fval );
  169. leafids.push_back(Cursor->GetLeafId());
  170. KernelSum += f(child);
  171. Cursor->ToParent();
  172. }
  173. return;
  174. }
  175. //std::cout << std::abs(kval) << "\t" <<
  176. // std::abs(f.array().abs().sum()) << "\t" << tol << std::endl;
  177. for (int child=0; child < 8; ++ child) {
  178. //std::cout << "Down the rabit hole " <<std::endl;
  179. Cursor->ToChild(child);
  180. EvaluateKids( f(child) );
  181. //Cursor->ToParent();
  182. Cursor->ToSameNode(tcurse);
  183. }
  184. tcurse->Delete();
  185. }
  186. //--------------------------------------------------------------------------------------
  187. // Class: KernelV0
  188. // Method: EvaluateKids
  189. //--------------------------------------------------------------------------------------
  190. void OctreeGrid::GetPosition( Real* p ) {
  191. Real ratio=1.0/(1<<(Cursor->GetCurrentLevel()));
  192. //step = ((Size).array() / std::pow(2.,Cursor->GetCurrentLevel()));
  193. p[0]=(Cursor->GetIndex(0)+.5)*ratio*Size[0]+Origin[0] ;//+ .5*step[0];
  194. p[1]=(Cursor->GetIndex(1)+.5)*ratio*Size[1]+Origin[1] ;//+ .5*step[1];
  195. p[2]=(Cursor->GetIndex(2)+.5)*ratio*Size[2]+Origin[2] ;//+ .5*step[2];
  196. }
  197. } // ---- end of namespace Lemma ----
  198. /* vim: set tabstop=4 expandtab */
  199. /* vim: set filetype=cpp */