Lemma is an Electromagnetics API
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DipoleSource.h 12KB

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