Main Lemma Repository
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

KernelEM1DReflSpec.h 9.8KB

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