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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. IntegrateOnOctreeGrid( 1e-2 );
  95. // EmEarth->AttachFieldPoints(receivers);
  96. // //EmEarth->SetHankelTransformMethod(FHTKEY101);
  97. // EmEarth->CalculateWireAntennaFields();
  98. // Vector3Xcr Rx1 = receivers->GetHfield(0);
  99. // //receivers->ClearFields();
  100. //
  101. // //EmEarth->AttachWireAntenna(Tx2);
  102. // EmEarth->CalculateWireAntennaFields();
  103. // Rx1 += receivers->GetHfield(0);
  104. }
  105. }
  106. //--------------------------------------------------------------------------------------
  107. // Class: KernelV0
  108. // Method: IntegrateOnOctreeGrid
  109. //--------------------------------------------------------------------------------------
  110. void KernelV0::IntegrateOnOctreeGrid( const Real& tolerance) {
  111. this->tol = tolerance;
  112. Vector3r Size;
  113. Size << 100,100,100;
  114. //Vector3r Origin;
  115. Vector3r cpos;
  116. cpos << 50,50,50;
  117. int maxlevel;
  118. SUM = 0;
  119. nleaves = 0;
  120. EvaluateKids( Size, 0, cpos, -1e2 );
  121. std::cout << "SUM\t" << SUM << "\t" << 100*100*100 << "\t" << SUM - Complex(100.*100.*100.) << std::endl;
  122. std::cout << "nleaves\t" << nleaves << std::endl;
  123. }
  124. //--------------------------------------------------------------------------------------
  125. // Class: KernelV0
  126. // Method: f
  127. //--------------------------------------------------------------------------------------
  128. Complex KernelV0::f( const Vector3r& r, const Real& volume ) {
  129. return Complex(volume);
  130. }
  131. //--------------------------------------------------------------------------------------
  132. // Class: KernelV0
  133. // Method: EvaluateKids
  134. //--------------------------------------------------------------------------------------
  135. bool KernelV0::EvaluateKids( const Vector3r& size, const int& level, const Vector3r& cpos,
  136. const Complex& parentVal ) {
  137. // Next level step, interested in one level below
  138. // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
  139. Vector3r step = size.array() / (Real)(1 << (level+2) );
  140. Real vol = step(0)*step(1)*step(2); // volume of each child
  141. Vector3r pos = cpos - step/2.;
  142. Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
  143. 0, 0, 0,
  144. step[0], 0, 0,
  145. 0, step[1], 0,
  146. step[0], step[1], 0,
  147. 0, 0, step[2],
  148. step[0], 0, step[2],
  149. 0, step[1], step[2],
  150. step[0], step[1], step[2] ).finished();
  151. VectorXcr kvals(8); // individual kernel vals
  152. for (int ichild=0; ichild<8; ++ichild) {
  153. Vector3r cp = pos; // Eigen complains about combining these
  154. cp += posadd.row(ichild);
  155. kvals(ichild) = f(cp, vol);
  156. }
  157. Complex ksum = kvals.sum(); // Kernel sum
  158. // Evaluate whether or not furthur splitting is needed
  159. if ( std::abs(ksum - parentVal) > tol || level < 5 ) {
  160. for (int ichild=0; ichild<8; ++ichild) {
  161. Vector3r cp = pos; // Eigen complains about combining these
  162. cp += posadd.row(ichild);
  163. bool isleaf = EvaluateKids( size, level+1, pos, kvals(ichild) );
  164. if (isleaf) { // Include result in final integral
  165. // Id = curse.GetLeafId() // VTK
  166. // LeafDict[Id] = vals[child] // VTK
  167. SUM += ksum;
  168. nleaves += 1;
  169. }
  170. }
  171. return false; // not leaf
  172. }
  173. return true; // leaf
  174. }
  175. } // ---- end of namespace Lemma ----
  176. /* vim: set tabstop=4 expandtab */
  177. /* vim: set filetype=cpp */