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

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