Main Lemma Repository
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

KernelEM1DReflSpec.h 9.9KB

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