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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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-7, 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 << 100,100,100;
  109. Vector3r Origin;
  110. Origin << 0,0,0;
  111. Vector3r cpos;
  112. cpos << 50,50,50;
  113. int maxlevel;
  114. SUM = 0;
  115. nleaves = 0;
  116. if (!vtkOutput) {
  117. EvaluateKids( Size, 0, cpos, 1e6 );
  118. } else {
  119. #ifdef LEMMAUSEVTK
  120. vtkHyperOctree* oct = vtkHyperOctree::New();
  121. oct->SetDimension(3);
  122. oct->SetOrigin( Origin(0), Origin(1), Origin(2) );
  123. oct->SetSize( Size(0), Size(1), Size(2) );
  124. vtkHyperOctreeCursor* curse = oct->NewCellCursor();
  125. curse->ToRoot();
  126. EvaluateKids2( Size, 0, cpos, 1e6, oct, curse );
  127. auto write = vtkXMLHyperOctreeWriter::New();
  128. //write.SetDataModeToAscii()
  129. write->SetInputData(oct);
  130. write->SetFileName("octree.vto");
  131. write->Write();
  132. write->Delete();
  133. curse->Delete();
  134. oct->Delete();
  135. #else
  136. throw std::runtime_error("IntegrateOnOctreeGrid with vtkOutput requires Lemma with VTK support");
  137. #endif
  138. }
  139. std::cout << "SUM\t" << SUM << "\t" << 100*100*100 << "\t" << SUM - Complex(100.*100.*100.) << std::endl;
  140. std::cout << "nleaves\t" << nleaves << std::endl;
  141. }
  142. //--------------------------------------------------------------------------------------
  143. // Class: KernelV0
  144. // Method: f
  145. //--------------------------------------------------------------------------------------
  146. Complex KernelV0::f( const Vector3r& r, const Real& volume, const Vector3cr& Bt ) {
  147. //std::cout << volume*Bt.norm() << std::endl;
  148. return Complex(volume*Bt.norm());
  149. //return Complex(volume);
  150. }
  151. //--------------------------------------------------------------------------------------
  152. // Class: KernelV0
  153. // Method: EvaluateKids
  154. //--------------------------------------------------------------------------------------
  155. bool KernelV0::EvaluateKids( const Vector3r& size, const int& level, const Vector3r& cpos,
  156. const Complex& parentVal ) {
  157. // Next level step, interested in one level below
  158. // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
  159. Vector3r step = size.array() / (Real)(1 << (level+2) );
  160. Real vol = step(0)*step(1)*step(2); // volume of each child
  161. Vector3r pos = cpos - step/2.;
  162. Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
  163. 0, 0, 0,
  164. step[0], 0, 0,
  165. 0, step[1], 0,
  166. step[0], step[1], 0,
  167. 0, 0, step[2],
  168. step[0], 0, step[2],
  169. 0, step[1], step[2],
  170. step[0], step[1], step[2] ).finished();
  171. VectorXcr kvals(8); // individual kernel vals
  172. FieldPoints* cpoints = EMEarths[0]->GetFieldPoints();
  173. cpoints->ClearFields();
  174. for (int ichild=0; ichild<8; ++ichild) {
  175. Vector3r cp = pos; // Eigen complains about combining these
  176. cp += posadd.row(ichild);
  177. cpoints->SetLocation( ichild, cp );
  178. }
  179. Vector3Xcr Bt;
  180. //Eigen::Matrix< Complex, 8, 3 > Bt;
  181. for ( auto EMCalc : EMEarths ) {
  182. //EMCalc->GetFieldPoints()->ClearFields();
  183. EMCalc->CalculateWireAntennaFields();
  184. Bt = EMCalc->GetFieldPoints()->GetHfield(0);
  185. }
  186. for (int ichild=0; ichild<8; ++ichild) {
  187. Vector3r cp = pos; // Eigen complains about combining these
  188. cp += posadd.row(ichild);
  189. kvals(ichild) = f(cp, vol, Bt.col(ichild));
  190. }
  191. Complex ksum = kvals.sum(); // Kernel sum
  192. // Evaluate whether or not furthur splitting is needed
  193. if ( std::abs(ksum - parentVal) > tol || level < 5 ) {
  194. for (int ichild=0; ichild<8; ++ichild) {
  195. Vector3r cp = pos; // Eigen complains about combining these
  196. cp += posadd.row(ichild);
  197. bool isleaf = EvaluateKids( size, level+1, cp, kvals(ichild) );
  198. if (isleaf) { // Include result in final integral
  199. SUM += ksum;
  200. nleaves += 1;
  201. }
  202. }
  203. return false; // not leaf
  204. }
  205. // Save here instead?
  206. return true; // leaf
  207. }
  208. #ifdef LEMMAUSEVTK
  209. //--------------------------------------------------------------------------------------
  210. // Class: KernelV0
  211. // Method: EvaluateKids2 -- same as Evaluate Kids, but include VTK octree generation
  212. //--------------------------------------------------------------------------------------
  213. bool KernelV0::EvaluateKids2( const Vector3r& size, const int& level, const Vector3r& cpos,
  214. const Complex& parentVal, vtkHyperOctree* oct, vtkHyperOctreeCursor* curse) {
  215. std::cout << "\rlevel " << level << "\t" << nleaves;
  216. std::cout.flush();
  217. // Next level step, interested in one level below
  218. // bitshift requires one extra, faster than, and equivalent to std::pow(2, level+1)
  219. Vector3r step = size.array() / (Real)(1 << (level+2) );
  220. Real vol = step(0)*step(1)*step(2); // volume of each child
  221. Vector3r pos = cpos - step/2.;
  222. Eigen::Matrix<Real, 8, 3> posadd = (Eigen::Matrix<Real, 8, 3>() <<
  223. 0, 0, 0,
  224. step[0], 0, 0,
  225. 0, step[1], 0,
  226. step[0], step[1], 0,
  227. 0, 0, step[2],
  228. step[0], 0, step[2],
  229. 0, step[1], step[2],
  230. step[0], step[1], step[2] ).finished();
  231. VectorXcr kvals(8); // individual kernel vals
  232. FieldPoints* cpoints = EMEarths[0]->GetFieldPoints();
  233. cpoints->ClearFields();
  234. for (int ichild=0; ichild<8; ++ichild) {
  235. Vector3r cp = pos; // Eigen complains about combining these
  236. cp += posadd.row(ichild);
  237. cpoints->SetLocation( ichild, cp );
  238. }
  239. Vector3Xcr Bt;
  240. for ( auto EMCalc : EMEarths ) {
  241. //EMCalc->GetFieldPoints()->ClearFields();
  242. EMCalc->CalculateWireAntennaFields();
  243. Bt = EMCalc->GetFieldPoints()->GetHfield(0);
  244. }
  245. for (int ichild=0; ichild<8; ++ichild) {
  246. Vector3r cp = pos; // Eigen complains about combining these
  247. cp += posadd.row(ichild);
  248. kvals(ichild) = f(cp, vol, Bt.col(ichild));
  249. }
  250. Complex ksum = kvals.sum(); // Kernel sum
  251. // Evaluate whether or not furthur splitting is needed
  252. if ( std::abs(ksum - parentVal) > tol || level < 3 ) {
  253. oct->SubdivideLeaf(curse);
  254. for (int ichild=0; ichild<8; ++ichild) {
  255. curse->ToChild(ichild);
  256. Vector3r cp = pos; // Eigen complains about combining these
  257. cp += posadd.row(ichild);
  258. bool isleaf = EvaluateKids2( size, level+1, cp, kvals(ichild), oct, curse );
  259. if (isleaf) { // Include result in final integral
  260. LeafDict[curse->GetLeafId()] = kvals(ichild); // VTK
  261. SUM += ksum;
  262. nleaves += 1;
  263. }
  264. curse->ToParent();
  265. }
  266. return false; // not leaf
  267. }
  268. return true; // leaf
  269. }
  270. #endif
  271. } // ---- end of namespace Lemma ----
  272. /* vim: set tabstop=4 expandtab */
  273. /* vim: set filetype=cpp */