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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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:34 PM
  11. * @author Trevor Irons (ti)
  12. * @email tirons@egi.utah.edu
  13. * @copyright Copyright (c) 2016, University of Utah
  14. * @copyright Copyright (c) 2016, Lemma Software, LLC
  15. * @copyright Copyright (c) 2008, Colorado School of Mines
  16. */
  17. #ifndef KERNELV0_INC
  18. #define KERNELV0_INC
  19. #pragma once
  20. #include "MerlinObject.h"
  21. #include "LayeredEarthEM.h"
  22. #include "PolygonalWireAntenna.h"
  23. #include "EMEarth1D.h"
  24. #include "ProgressBar.h"
  25. #ifdef LEMMAUSEVTK
  26. //#include "vtkHyperOctree.h"
  27. //#include "vtkHyperOctreeCursor.h"
  28. //#include "vtkXMLHyperOctreeWriter.h"
  29. //#include "vtkXMLHyperOctreeWriter.h"
  30. #include "vtkCellData.h"
  31. #include "vtkPointData.h"
  32. #include "vtkHyperTree.h"
  33. #include "vtkHyperTree.h"
  34. #include "vtkHyperTreeGrid.h"
  35. #include "vtkXMLHyperTreeGridWriter.h"
  36. #include "vtkHyperTreeCursor.h" // not in VTK 8.90
  37. //#include "vtkHyperTreeGridLevelEntry.h" VTK 9
  38. #include "vtkDoubleArray.h"
  39. #endif
  40. namespace Lemma {
  41. // Holds the elliptic field construction of Bperp
  42. // commented out variables are for error checking
  43. struct EllipticB {
  44. Real alpha;
  45. Real beta;
  46. Real zeta;
  47. // Real err;
  48. Complex eizt;
  49. // Complex BperpdotB;
  50. Vector3r bhat;
  51. Vector3r bhatp;
  52. // Vector3cr Bperp;
  53. };
  54. template <typename T> int sgn(T val) {
  55. return (val > T(0)) - (val < T(0));
  56. }
  57. /**
  58. * \ingroup Merlin
  59. * \brief Calculated the initial amplitude imaging kernel of a sNMR experiment
  60. * \details This class calculates the imaging kernel for a free induction decay
  61. * pulse. The methodology follows from Weichman et al., 2000.
  62. */
  63. class KernelV0 : public MerlinObject {
  64. friend std::ostream &operator<<(std::ostream &stream, const KernelV0 &ob);
  65. protected:
  66. /*
  67. * This key is used to lock the constructor. It is protected so that inhereted
  68. * classes also have the key to contruct their base class.
  69. */
  70. public:
  71. // ==================== LIFECYCLE =======================
  72. /**
  73. * Default constructor.
  74. * @note This method is locked, and cannot be called directly.
  75. * The reason that the method is public is to enable the use
  76. * of make_shared whilst enforcing the use of shared_ptr,
  77. * in c++-17, this curiosity may be resolved.
  78. * @see KernelV0::NewSP
  79. */
  80. explicit KernelV0 ( const ctor_key& );
  81. /**
  82. * DeSerializing constructor.
  83. * @note This method is locked, and cannot be called directly.
  84. * The reason that the method is public is to enable the use
  85. * of make_shared whilst enforcing the use of shared_ptr,
  86. * in c++-17, this curiosity may be resolved.
  87. * @see KernelV0::DeSerialize
  88. */
  89. KernelV0 ( const YAML::Node& node, const ctor_key& );
  90. /**
  91. * Default destructor.
  92. * @note This method should never be called due to the mandated
  93. * use of smart pointers. It is necessary to keep the method
  94. * public in order to allow for the use of the more efficient
  95. * make_shared constructor.
  96. */
  97. virtual ~KernelV0 ();
  98. /**
  99. * Uses YAML to serialize this object.
  100. * @return a YAML::Node
  101. * @see KernelV0::DeSerialize
  102. */
  103. virtual YAML::Node Serialize() const;
  104. /**
  105. * Factory method for generating concrete class.
  106. * @return a std::shared_ptr of type KernelV0
  107. */
  108. static std::shared_ptr< KernelV0 > NewSP();
  109. /**
  110. * Constructs an KernelV0 object from a YAML::Node.
  111. * @see KernelV0::Serialize
  112. */
  113. static std::shared_ptr<KernelV0> DeSerialize(const YAML::Node& node);
  114. /**
  115. * Constructs an object from a string representation of a YAML::Node. This is primarily
  116. * used in Python wrapping
  117. */
  118. static std::shared_ptr<KernelV0> DeSerialize( const std::string& node ) {
  119. return KernelV0::DeSerialize(YAML::LoadFile(node));
  120. }
  121. // ==================== OPERATORS =======================
  122. // ==================== OPERATIONS =======================
  123. /**
  124. * Calculates a single imaging kernel, however, phased arrays are supported
  125. * so that more than one transmitter and/or receiver can be specified.
  126. * @param[in] tx is the list of transmitters to use for a kernel, use the same labels as
  127. * used in PushCoil.
  128. * @param[in] rx is the list of receivers to use for a kernel, use the same labels as
  129. * used in PushCoil. @see PushCoil
  130. * @param[in] vtkOutput generates a VTK hyperoctree file as well, useful for visualization.
  131. * requires compilation of Lemma with VTK. The VTK files can become very large.
  132. */
  133. void CalculateK0 (const std::vector< std::string >& tx, const std::vector< std::string >& rx,
  134. bool vtkOutput );
  135. //bool vtkOutput=false );
  136. /**
  137. * Aligns the kernel pulse settings with an Akvo Processed dataset.
  138. */
  139. void AlignWithAkvoDataset( const YAML::Node& node ) ;
  140. /**
  141. * Aligns the kernel pulse settings with an Akvo Processed dataset.
  142. */
  143. void AlignWithAkvoDataset( const std::string& file ) {
  144. AlignWithAkvoDataset( YAML::LoadFile(file) );
  145. }
  146. /**
  147. * Assign transmiter coils
  148. */
  149. inline void PushCoil( const std::string& label, std::shared_ptr<PolygonalWireAntenna> ant ) {
  150. TxRx[label] = ant;
  151. }
  152. // ==================== INQUIRY =======================
  153. /**
  154. * @return std::shared_ptr<LayeredEarthEM>
  155. */
  156. inline std::shared_ptr<LayeredEarthEM> GetSigmaModel ( ) {
  157. return SigmaModel;
  158. } // ----- end of method KernelV0::get_SigmaModel -----
  159. /**
  160. * @return the kernel matrix
  161. */
  162. inline MatrixXcr GetKernel ( ) {
  163. return Kern;
  164. }
  165. /**
  166. * @return the integration tolerance
  167. */
  168. inline Real GetTolerance ( ) {
  169. return tol;
  170. }
  171. /**
  172. * @return the layer interfaces
  173. */
  174. inline VectorXr GetInterfaces ( ) {
  175. return Interfaces;
  176. }
  177. /**
  178. * @return the pulse peak current
  179. */
  180. inline VectorXr GetPulseCurrent ( ) {
  181. return PulseI;
  182. }
  183. /**
  184. * @param[in] value the 1D-EM model used for calculations
  185. */
  186. inline void SetLayeredEarthEM ( std::shared_ptr< LayeredEarthEM > value ) {
  187. SigmaModel = value;
  188. return ;
  189. } // ----- end of method KernelV0::set_SigmaModel -----
  190. /**
  191. * @param[in] size the size of the volume to be integrated
  192. */
  193. inline void SetIntegrationSize ( const Vector3r& size ) {
  194. Size = size;
  195. return ;
  196. } // ----- end of method KernelV0::SetIntegrationSize -----
  197. /**
  198. * @param[in] type The type of Hankel transform that will be used.
  199. */
  200. inline void SetHankelTransformType ( const HANKELTRANSFORMTYPE& type ) {
  201. HankelType = type;
  202. return ;
  203. } // ----- end of method KernelV0::SetIntegrationOrigin -----
  204. /**
  205. * @param[in] min is the minimum leaf level, defaults to 0
  206. */
  207. inline void SetMinLevel ( const int& min ) {
  208. minLevel = min;
  209. return ;
  210. } // ----- end of method KernelV0::SetMinLevel -----
  211. /**
  212. * @param[in] max is the maximum leaf level, defaults to 12
  213. */
  214. inline void SetMaxLevel ( const int& max ) {
  215. maxLevel = max;
  216. return ;
  217. } // ----- end of method KernelV0::SetMaxLevel -----
  218. /**
  219. * @param[in] origin The origin location (corner) for the integration volume
  220. */
  221. inline void SetIntegrationOrigin ( const Vector3r& origin ) {
  222. Origin = origin;
  223. return ;
  224. } // ----- end of method KernelV0::SetIntegrationOrigin -----
  225. /**
  226. * @param[in] Amps is the current for each pulse moment
  227. */
  228. inline void SetPulseCurrent ( const VectorXr& Amps ) {
  229. PulseI = Amps;
  230. return ;
  231. } // ----- end of method KernelV0::SetIntegrationOrigin -----
  232. /**
  233. * Sets the temperature, which has implications in calculation of \f$ M_N^{(0)}\f$. Units in
  234. * Kelvin.
  235. */
  236. inline void SetTemperature(const Real& tempK) {
  237. Temperature = tempK;
  238. }
  239. /**
  240. * Sets the tolerance to use for making the adaptive mesh
  241. * @param[in] ttol is the tolerance to use
  242. */
  243. inline void SetTolerance(const Real& ttol) {
  244. tol = ttol;
  245. }
  246. /**
  247. * @param[in] taup sets the pulse duration
  248. */
  249. inline void SetPulseDuration(const Real& taup) {
  250. Taup = taup;
  251. }
  252. inline Real GetPulseDuration( ) {
  253. return Taup;
  254. }
  255. inline void SetDepthLayerInterfaces( const VectorXr& iface ){
  256. Interfaces = iface;
  257. }
  258. /**
  259. * Returns the name of the underlying class, similiar to Python's type
  260. * @return string of class name
  261. */
  262. virtual inline std::string GetName() const {
  263. return CName;
  264. }
  265. protected:
  266. // ==================== LIFECYCLE =======================
  267. /** Copy is disabled */
  268. KernelV0( const KernelV0& ) = delete;
  269. private:
  270. /**
  271. * Returns the kernel value for an input prism
  272. */
  273. VectorXcr f( const Vector3r& r, const Real& volume , const Vector3cr& Ht, const Vector3cr& Hr);
  274. // Complex ComputeV0Cell(const EllipticB& EBT, const EllipticB& EBR,
  275. // const Real& sintheta, const Real& phase, const Real& Mn0Abs,
  276. // const Real& vol);
  277. EllipticB EllipticFieldRep (const Vector3cr& B, const Vector3r& B0hat);
  278. Vector3r ComputeMn0(const Real& Porosity, const Vector3r& B0);
  279. void IntegrateOnOctreeGrid( bool vtkOutput=false );
  280. /**
  281. * Recursive call to integrate a function on an adaptive Octree Grid.
  282. * For efficiency's sake the octree grid is not stored, as only the
  283. * integral (sum) is of interest. The logic for grid refinement is based
  284. * on an Octree representation of the domain. If an Octree representation
  285. * of the kernel is desired, call alternative version @see EvaluateKids2
  286. * @param[in] size gives the domain size, in metres
  287. * @param[in] level gives the current level of the octree grid, call with 0 initially
  288. * @param[in] cpos is the centre position of the parent cuboid
  289. */
  290. void EvaluateKids( const Vector3r& size, const int& level, const Vector3r& cpos,
  291. const VectorXcr& parentVal );
  292. #ifdef LEMMAUSEVTK
  293. /**
  294. * Same functionality as @see EvaluateKids, but includes generation of a VTK
  295. * HyperOctree, which is useful for visualization.
  296. */
  297. void EvaluateKids2( const Vector3r& size, const int& level, const Vector3r& cpos,
  298. const VectorXcr& parentVal, vtkHyperTreeGrid* octree, vtkHyperTreeCursor* curse );
  299. void GetPosition( vtkHyperTreeCursor* Cursor, Real* p );
  300. #endif
  301. // ==================== DATA MEMBERS =========================
  302. int ilay;
  303. int nleaves;
  304. int minLevel=0;
  305. int maxLevel=12;
  306. Real VOLSUM;
  307. Real tol=1e-11;
  308. Real Temperature=283.;
  309. Real Taup = .020; // Sec
  310. Real Larmor;
  311. Vector3r Size;
  312. Vector3r Origin;
  313. VectorXr PulseI;
  314. VectorXr Interfaces;
  315. MatrixXcr Kern;
  316. HANKELTRANSFORMTYPE HankelType=ANDERSON801;
  317. std::shared_ptr< LayeredEarthEM > SigmaModel = nullptr;
  318. std::shared_ptr< FieldPoints > cpoints = nullptr;
  319. std::map< std::string , std::shared_ptr< PolygonalWireAntenna > > TxRx;
  320. std::map< std::string , std::shared_ptr< EMEarth1D > > EMEarths;
  321. #ifdef LEMMAUSEVTK
  322. std::map< int, VectorXcr > LeafDict; // kernel sum for each q
  323. std::map< int, VectorXcr > LeafHt; // Transmitter field
  324. std::map< int, VectorXcr > LeafHr; // Receiver field
  325. std::map< int, int > LeafDictIdx; // index
  326. std::map< int, Real > LeafDictErr; // error value
  327. #endif
  328. ProgressBar* disp;
  329. int percent_done;
  330. // Physical constants and conversion factors
  331. static constexpr Real GAMMA = 2.67518e8; // MKS units
  332. static constexpr Real INVSQRT2 = 0.70710678118654746; // 1/sqrt(2)
  333. static constexpr Real HBAR = 1.05457148e-34; // m2 kg / s
  334. static constexpr Real NH2O = 6.692e28; // [m^3]
  335. static constexpr Real KB = 1.3805e-23; // m^2 kg s-2 K-1
  336. static constexpr Real CHI_N = 3.29e-3; // MKS units
  337. /** ASCII string representation of the class name */
  338. static constexpr auto CName = "KernelV0";
  339. }; // ----- end of class KernelV0 -----
  340. } // ----- end of namespace Lemma ----
  341. /* vim: set tabstop=4 expandtab */
  342. /* vim: set filetype=cpp */
  343. #endif // ----- #ifndef KERNELV0_INC -----