Main Lemma Repository
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.

DipoleSource.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* This file is part of Lemma, a geophysical modelling and inversion API */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /**
  6. @file
  7. @author Trevor Irons
  8. @date 12/02/2009
  9. **/
  10. #ifndef __DIPOLESOURCE_H
  11. #define __DIPOLESOURCE_H
  12. #include <memory>
  13. #include "LemmaObject.h"
  14. #include "LayeredEarthEM.h"
  15. #ifdef LEMMAUSEVTK
  16. #include "vtkActor.h"
  17. #include "vtkLineSource.h"
  18. #include "vtkSphereSource.h"
  19. #include "vtkPolyDataMapper.h"
  20. #include "vtkTubeFilter.h"
  21. #include "vtkRegularPolygonSource.h"
  22. #include "vtkProperty.h"
  23. #endif
  24. namespace Lemma {
  25. // Forward declarations
  26. class KernelEM1DManager;
  27. class FieldPoints;
  28. class HankelTransform;
  29. // ==========================================================================
  30. // Class: DipoleSource
  31. /// \ingroup FDEM1D
  32. /// \brief Dipole sources form the backbone of Lemma.
  33. /// \details More complex sources are constructed from a superposition of
  34. /// dipoles.
  35. // ==========================================================================
  36. //class DipoleSource : public std::enable_shared_from_this<DipoleSource>, LemmaObject {
  37. class DipoleSource : public LemmaObject, std::enable_shared_from_this<DipoleSource> {
  38. //class DipoleSource : public LemmaObject, private std::enable_shared_from_this<DipoleSource> {
  39. // ==================== FRIENDS ======================
  40. friend std::ostream &operator<<(std::ostream &stream, const DipoleSource &ob);
  41. friend class EMEarth1D;
  42. public:
  43. //bool operator==(DipoleSource& rhs)const;
  44. // ==================== LIFECYCLE ======================
  45. /** Default locked constructor. */
  46. explicit DipoleSource ( const ctor_key& );
  47. /** Locked deserializing constructor */
  48. DipoleSource ( const YAML::Node& node, const ctor_key& );
  49. /** Default locked constructor. */
  50. ~DipoleSource ();
  51. /**
  52. * Returns shared_ptr to new DipoleSource. Location is
  53. * initialized to (0,0,0) type and polarization are
  54. * initialized to nonworking values that will throw
  55. * exceptions if used.
  56. */
  57. static std::shared_ptr< DipoleSource > NewSP();
  58. /**
  59. * YAML Serializing method
  60. */
  61. YAML::Node Serialize() const;
  62. /**
  63. * Constructs an object from a YAML::Node.
  64. */
  65. static std::shared_ptr< DipoleSource > DeSerialize(const YAML::Node& node);
  66. /** Returns a deep copy of the dipole. Used to make thread safe methods. Does not
  67. copy attachments.
  68. */
  69. std::shared_ptr< DipoleSource > Clone();
  70. // ==================== OPERATORS ======================
  71. // ==================== ACCESS ======================
  72. /** Sets the position.
  73. * @param [in] posin
  74. */
  75. void SetLocation(const Vector3r &posin);
  76. /** Sets the location using three Real coordinate arguments.
  77. * @param[in] xp is the x coordinate of the dipole
  78. * @param[in] yp is the y coordinate of the dipole
  79. * @param[in] zp is the z coordinate of the dipole
  80. */
  81. void SetLocation(const Real &xp, const Real &yp, const Real &zp);
  82. /** Sets the dipole direction (polarisation). This method
  83. * replaced SetPolarisation(DipoleSourcePolarisation) and allows for general dipole
  84. * directionality.
  85. * @param[in] dir is the direction of the dipole. This will be normalised.
  86. */
  87. void SetPolarisation(const Vector3r &dir);
  88. /** Sets the polarisation of the dipole. Conveneince method that calls
  89. * SetPolarisation(const Vector3r &dir), constructing the normalized Vector | <x, y, z> |
  90. */
  91. void SetPolarisation(const Real& x, const Real& y, const Real& z );
  92. /// Sets the dipole polarisation
  93. /// @param[in] pol is the enumerated polarisation
  94. void SetPolarisation(const DipoleSourcePolarisation &pol);
  95. /// Sets the dipole source type
  96. /// @param[in] stype is one of the enerated values taking either
  97. /// ELECTRICDIPOLE or MAGNETICDIPOLE
  98. void SetType(const DIPOLESOURCETYPE &stype);
  99. /// Sets the dipole moment
  100. void SetMoment(const Real &moment);
  101. /// Sets the dipole phse
  102. void SetPhase(const Real &phase);
  103. /// Sets the polarity
  104. void SetPolarity(const DipoleSourcePolarity& pol);
  105. /// Sets number of frequencies
  106. void SetNumberOfFrequencies(const int &nfreq);
  107. /// Sets a specific frequency.
  108. /// @param[in] ifreq is the frequency bin number
  109. /// @param[in] freq is the frequency to set, in Hz
  110. void SetFrequency(const int &ifreq, const Real &freq);
  111. /// Sets the frequencies of the dipole.
  112. /// @param[in] freqs is a vector of the frequencies. Also sets
  113. /// number of frequencies
  114. void SetFrequencies(const VectorXr& freqs);
  115. // ==================== INQUIRY ======================
  116. /** Accessor to polarisation vector.
  117. @return returns the unit polarisation vector.
  118. */
  119. Vector3r GetPolarisation();
  120. /// Returns Vector3r position of the dipole
  121. Vector3r GetLocation();
  122. /// Returns a specific coordinate of the dipole
  123. /// @param coordinate 0=x, 1=y, 2=z
  124. Real GetLocation(const int &coordinate);
  125. /// Returns enumerated of DIPOLESOURCETYPE
  126. DIPOLESOURCETYPE GetDipoleSourceType();
  127. /// Returns the dipole type
  128. DIPOLESOURCETYPE GetType();
  129. /// Returns pointer to KernelEM1DManager
  130. std::shared_ptr<KernelEM1DManager> GetKernelManager();
  131. // Returns enumerated DipoleSourcePolarization
  132. //DipoleSourcePolarisation GetDipoleSourcePolarisation();
  133. /// Returns the dipole moment
  134. Real GetMoment();
  135. /// Returns the angular frequency of the dipole
  136. Real GetAngularFrequency(const int &ifreq);
  137. /// Returns the frequency of the dipole (Hz)
  138. Real GetFrequency(const int &ifreq);
  139. /// Returns the frequency of the dipole (Hz)
  140. VectorXr GetFrequencies( );
  141. /// Returns the phase offset of the dipole
  142. Real GetPhase();
  143. /// Returns the number of frequencies
  144. int GetNumberOfFrequencies();
  145. #ifdef LEMMAUSEVTK
  146. /// Returns an actor that can be placed into a vtk scene easily
  147. /// Note that this function throws a pointer, it is the receivers
  148. /// job to manage this memory!
  149. vtkActor* GetVtkActor();
  150. #endif
  151. /** Returns the name of the underlying class, similiar to Python's type */
  152. virtual std::string GetName() const ;
  153. protected:
  154. // ==================== OPERATIONS ======================
  155. /** Determines if kernels have been loaded already, and if so if they can be reused
  156. */
  157. void SetKernels(const int& ifreq, const FIELDCALCULATIONS& Fields,
  158. std::shared_ptr<FieldPoints> Receivers, const int& irec,
  159. std::shared_ptr<LayeredEarthEM> Earth );
  160. /** resets the kernels if they cannot be reused */
  161. virtual void ReSetKernels(const int& ifreq, const FIELDCALCULATIONS& Fields,
  162. std::shared_ptr<FieldPoints> Receivers,
  163. const int& irec, std::shared_ptr<LayeredEarthEM> Earth );
  164. /** Updates the receiver fields */
  165. virtual void UpdateFields(const int& ifreq, HankelTransform* Hankel, const Real& wavef);
  166. private:
  167. // ==================== DATA MEMBERS ======================
  168. /// Defines the type of source (magnetic or electric)
  169. DIPOLESOURCETYPE Type;
  170. // Polarization of the dipole, (x, y or z)
  171. //DipoleSourcePolarisation Polarisation;
  172. // Dipole polarity
  173. //DipoleSourcePolarity Polarity;
  174. /// Which receiver index should Kernels be configured for
  175. int irec;
  176. int lays;
  177. int layr;
  178. /// Phase offset of the dipole, referenced from 0
  179. Real Phase;
  180. /// Dipole Moment
  181. Real Moment;
  182. Real xxp;
  183. Real yyp;
  184. Real rho;
  185. Real sp;
  186. Real cp;
  187. Real scp;
  188. Real sps;
  189. Real cps;
  190. Real c2p;
  191. Real kernelFreq;
  192. FIELDCALCULATIONS FieldsToCalculate = BOTH;
  193. VectorXcr f;
  194. VectorXi ik;
  195. /// Central location of the dipole
  196. Vector3r Location;
  197. /// Unit vector defining directionality of the dipole
  198. Vector3r Phat;
  199. /// Freqencies of the source, in Hz
  200. VectorXr Freqs;
  201. /// Storage of the EM1D kernels used by this dipole
  202. std::shared_ptr<KernelEM1DManager> KernelManager;
  203. /// Receiver points, keep track if these have changed
  204. std::shared_ptr<FieldPoints> Receivers;
  205. /// Layered Earth used by Kernels
  206. std::shared_ptr<LayeredEarthEM> Earth;
  207. /** ASCII string representation of the class name */
  208. static constexpr auto CName = "DipoleSource";
  209. }; // ----- end of class DipoleSource -----
  210. /** If no dipole source has been specified, throw this error.
  211. */
  212. class NullDipoleSource : public std::runtime_error {
  213. public:
  214. /** Thrown when a DipoleSource pointer is NULL
  215. */
  216. NullDipoleSource ( );
  217. };
  218. /** Error class for assignment of a dipole source that did not connect properly.
  219. */
  220. class NonValidDipoleTypeAssignment : public std::runtime_error {
  221. public:
  222. NonValidDipoleTypeAssignment( );
  223. };
  224. /** Error class for a non-valid dipole type. Generally thrown if this was not
  225. * set properly.
  226. */
  227. class NonValidDipoleType : public std::runtime_error {
  228. public:
  229. /** Throws error. This is a deprecated function. Call the method with
  230. * the pointer address instead.
  231. */
  232. NonValidDipoleType( );
  233. /** Throws error with information on the class throwing the error.
  234. */
  235. NonValidDipoleType( LemmaObject *ptr );
  236. };
  237. /** Error class for non valid dipole polarisation
  238. */
  239. class NonValidDipolePolarisation : public std::runtime_error {
  240. public:
  241. NonValidDipolePolarisation( );
  242. };
  243. /** Error class for non valid dipole polarity
  244. */
  245. class NonValidDipolePolarity : public std::runtime_error {
  246. public:
  247. NonValidDipolePolarity( );
  248. };
  249. /** Error class for non valid dipole polarisation
  250. */
  251. class NonValidDipolePolarisationAssignment : public std::runtime_error {
  252. public:
  253. NonValidDipolePolarisationAssignment( );
  254. };
  255. /** Error class for non valid location coordinate.
  256. */
  257. class NonValidLocationCoordinate : public std::runtime_error {
  258. public:
  259. NonValidLocationCoordinate( );
  260. };
  261. }
  262. #endif // __DIPOLESOURCE_H