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

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