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

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