Lemma is an Electromagnetics API
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 05/18/2012
  9. **/
  10. #ifndef KERNELEM1DREFLSPEC_INC
  11. #define KERNELEM1DREFLSPEC_INC
  12. #include "DipoleSource.h"
  13. #include "KernelEM1DReflBase.h"
  14. #include "LayeredEarthEM.h"
  15. namespace Lemma {
  16. // forward declaration for friend
  17. //template<EMMODE Mode, int Ikernel, DIPOLE_LOCATION Isource, DIPOLE_LOCATION Irecv>
  18. //class KernelEM1DSpec;
  19. // ===================================================================
  20. // Class: KernelEM1DReflSpec
  21. /**
  22. \ingroup FDEM1D
  23. \brief Specialized version of KernelEM1DReflBase
  24. \details Through use of template specialisations, this KernelEm1D
  25. class delivers much better performance. This class is internal
  26. to Lemma, you should never need to instantiate it. The constructors
  27. are public to allow make_shared. Additonally, this class is not
  28. serializable.
  29. */
  30. // ===================================================================
  31. template<EMMODE Mode, DIPOLE_LOCATION Isource, DIPOLE_LOCATION Irecv>
  32. class KernelEM1DReflSpec : public KernelEM1DReflBase {
  33. public:
  34. //template<EMMODE Mode2, int Ikernel2, DIPOLE_LOCATION Isource2, DIPOLE_LOCATION Irecv2>
  35. //friend class KernelEM1DSpec;
  36. friend class KernelEM1DManager;
  37. // ==================== LIFECYCLE =======================
  38. /// Default locked constructor.
  39. explicit KernelEM1DReflSpec ( const ctor_key& key ) : KernelEM1DReflBase( key ) {
  40. }
  41. /// Default protected constructor.
  42. ~KernelEM1DReflSpec () {
  43. }
  44. static std::shared_ptr<KernelEM1DReflSpec<Mode, Isource, Irecv> > NewSP() {
  45. return std::make_shared<KernelEM1DReflSpec<Mode, Isource, Irecv> >( ctor_key() );
  46. }
  47. // ==================== OPERATORS =======================
  48. // ==================== OPERATIONS =======================
  49. // ==================== ACCESS =======================
  50. // ==================== INQUIRY =======================
  51. virtual std::string GetName() const {
  52. return CName;
  53. }
  54. protected:
  55. private:
  56. // ==================== LIFECYCLE =======================
  57. // ==================== OPERATIONS =======================
  58. /** Computes reflection coefficients. Depending on the
  59. * specialisation, this will either be TM or TE mode.
  60. */
  61. void ComputeReflectionCoeffs(const Real &lambda);
  62. /* Computes reflection coefficients. Depending on the
  63. * specialisation, this will either be TM or TE mode. This method
  64. * stores previous results in a struct. For a given index, and
  65. * lambda, the result will be the same. Turned out to be of limited utility.
  66. */
  67. //void ComputeReflectionCoeffs(const Real &lambda, const int& idx);
  68. /** Precomputes expensive calculations that are reused by insances
  69. * of KernelEM1DSpec in the calculation of Related potentials. This
  70. * method is specialised based on template parameters
  71. */
  72. void PreComputePotentialTerms();
  73. /*
  74. * Sets the cache in CACHE to use. Somewhat expensive, avoid calling in tight loops
  75. */
  76. //void SetTCache(const Real& rho0);
  77. // ==================== DATA MEMBERS =========================
  78. static constexpr auto CName = "KernelEM1DSpec";
  79. }; // ----- end of class KernelEM1DReflSpec -----
  80. //template<EMMODE Mode, DIPOLE_LOCATION Isource, DIPOLE_LOCATION Irecv>
  81. //std::unordered_map<Real, cache> KernelEM1DReflSpec<Mode, Isource, Irecv>::CACHE;
  82. //template<EMMODE Mode, DIPOLE_LOCATION Isource, DIPOLE_LOCATION Irecv>
  83. //cache* KernelEM1DReflSpec<Mode, Isource, Irecv>::tcache;
  84. ///////////////////////////////////////////////
  85. // Declarations of specialisations
  86. template<>
  87. void KernelEM1DReflSpec<TM, INAIR, INAIR>::ComputeReflectionCoeffs(const Real& lambda);
  88. template<>
  89. void KernelEM1DReflSpec<TE, INAIR, INAIR>::ComputeReflectionCoeffs(const Real& lambda);
  90. template<>
  91. void KernelEM1DReflSpec<TM, INAIR, INGROUND>::ComputeReflectionCoeffs(const Real& lambda);
  92. template<>
  93. void KernelEM1DReflSpec<TE, INAIR, INGROUND>::ComputeReflectionCoeffs(const Real& lambda);
  94. template<>
  95. void KernelEM1DReflSpec<TM, INAIR, INAIR>::PreComputePotentialTerms( );
  96. template<>
  97. void KernelEM1DReflSpec<TE, INAIR, INAIR>::PreComputePotentialTerms( );
  98. template<>
  99. void KernelEM1DReflSpec<TM, INAIR, INGROUND>::PreComputePotentialTerms( );
  100. template<>
  101. void KernelEM1DReflSpec<TE, INAIR, INGROUND>::PreComputePotentialTerms( );
  102. ///////////////////////////////////////////////
  103. // Default mode definitions
  104. template<EMMODE Mode, DIPOLE_LOCATION Isource, DIPOLE_LOCATION Irecv>
  105. void KernelEM1DReflSpec<Mode, Isource, Irecv>::ComputeReflectionCoeffs(const Real& lambda) {
  106. static bool called = false;
  107. if (!called) {
  108. std::cout << "unspecialised Reflection function KernelEM1DReflSpec<"
  109. << Mode << ", " << Isource << ", "
  110. << Irecv << " >::ComputeReflectionCoeffs( const Real& lambda ) --> SLOW PERFORMANCE EXPECTED\n";
  111. called = true;
  112. }
  113. rams = lambda*lambda;
  114. //////////////////////////////////////////
  115. // Compute TEM stuff
  116. // This call to sqrt takes ~ 15% of execution time
  117. u = (rams-kk.array()).sqrt();
  118. uk = u(lays);
  119. um = u(layr);
  120. switch (Mode) {
  121. // TM mode
  122. case (TM):
  123. Zyu(1) = -u(0)/yh(0);
  124. Zyi = u.array() / yh.array();
  125. break;
  126. // TE mode
  127. case (TE):
  128. Zyu(1) = -u(0)/zh(0);
  129. Zyi = u.array() / zh.array();
  130. break;
  131. default:
  132. throw 11; //IllegalMode(this);
  133. }
  134. Zyd.tail<1>() = Zyi.tail<1>();
  135. for (int ilay=1; ilay<nlay-1; ++ilay) {
  136. cf(ilay) =
  137. std::exp(-(Real)(2.)*u(ilay)*LayerThickness(ilay));
  138. th(ilay) = ((Real)(1.)-cf(ilay)) / ((Real)(1.)+cf(ilay));
  139. }
  140. // Can't use blocks, b/c recursive
  141. for (int N=1; N<lays; ++N){
  142. Zyu(N+1)=Zyi(N)*(Zyu(N)-Zyi(N)*th(N)) /
  143. (Zyi(N)-Zyu(N)*th(N)) ;
  144. }
  145. int ne = nlay-2;
  146. for (int N=ne; N >=lays+1; --N) {
  147. Zyd(N) = Zyi(N)*(Zyd(N+1)+Zyi(N)*th(N)) /
  148. (Zyi(N)+Zyd(N+1)*th(N)) ;
  149. }
  150. rtd(nlay-1) = 0;
  151. if (layr < lays) {
  152. // Receiver above source layer
  153. int ls = layr;
  154. if (ls == 0) {
  155. ls = layr+1;
  156. }
  157. for (int N=ls; N<=lays; ++N) {
  158. rtu(N)= (Zyi(N)+Zyu(N)) /
  159. (Zyi(N)-Zyu(N)) ;
  160. }
  161. if (lays < nlay-1) {
  162. rtd(lays) = (Zyi(lays)-Zyd(lays+1)) /
  163. (Zyi(lays)+Zyd(lays+1)) ;
  164. }
  165. } else {
  166. // RECEIVER IN OR BELOW THE SOURCE LAYER
  167. if (lays == layr) { // Rx In source Layer
  168. if (layr == 0) {
  169. rtd(0) = (Zyu(1)+Zyd(1)) /
  170. (Zyu(1)-Zyd(1)) ;
  171. } else if (layr == nlay-1) {
  172. rtu(nlay-1) = (Zyi(nlay-1)+Zyu(nlay-1)) /
  173. (Zyi(nlay-1)-Zyu(nlay-1)) ;
  174. } else {
  175. rtu(layr) = (Zyi(layr)+Zyu(layr)) /
  176. (Zyi(layr)-Zyu(layr)) ;
  177. rtd(layr) = (Zyi(layr)-Zyd(layr+1)) /
  178. (Zyi(layr)+Zyd(layr+1)) ;
  179. }
  180. } else { // receiver below source layer
  181. if (lays == 0) {
  182. rtd(0) = (Zyu(1)+Zyd(1)) /
  183. (Zyu(1)-Zyd(1)) ;
  184. } else {
  185. rtu(lays) = (Zyi(lays)+Zyu(lays)) /
  186. (Zyi(lays)-Zyu(lays)) ;
  187. }
  188. int le = layr;
  189. if (le == nlay-1) --le;
  190. int ls = lays;
  191. if (lays == 0 ) ++ls;
  192. // TODO use blocks to vectorize maybe?
  193. // This works but gives same to slightly worse
  194. // performance as loop.
  195. // int nn = le-ls+1;
  196. // rtd.segment(ls, nn) =
  197. // (Zyi.segment(ls , nn).array() -
  198. // Zyd.segment(ls+1, nn).array()).array() /
  199. // (Zyi.segment(ls , nn).array() +
  200. // Zyd.segment(ls+1, nn).array()).array() ;
  201. for (int N=ls; N<=le; ++N) {
  202. rtd(N) = (Zyi(N)-Zyd(N+1)) /
  203. (Zyi(N)+Zyd(N+1)) ;
  204. }
  205. }
  206. } // End in or below source layer
  207. return;
  208. }
  209. template<EMMODE Mode, DIPOLE_LOCATION Isource, DIPOLE_LOCATION Irecv>
  210. void KernelEM1DReflSpec<Mode, Isource, Irecv>::PreComputePotentialTerms( ) {
  211. static bool called = false;
  212. if (!called) {
  213. std::cerr << "unspecialised function KernelEM1DReflSpec<"
  214. << Mode << ", " << Isource << ", "
  215. << Irecv << " >::PreComputePotentialTerms\n";
  216. called = true;
  217. }
  218. }
  219. } // ----- end of Lemma name -----
  220. #endif // ----- #ifndef KERNELEM1DREFLSPEC_INC -----