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

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