Main Lemma Repository
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

KernelEM1DReflSpec.h 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. @version $Id: kernelem1dreflspec.h 123 2014-02-05 23:47:20Z tirons $
  10. **/
  11. #ifndef KERNELEM1DREFLSPEC_INC
  12. #define KERNELEM1DREFLSPEC_INC
  13. #include "DipoleSource.h"
  14. #include "LayeredEarthEM.h"
  15. #include "kernelem1dreflbase.h"
  16. namespace Lemma {
  17. // forward declaration for friend
  18. template<EMMODE Mode, int Ikernel, DIPOLE_LOCATION Isource, DIPOLE_LOCATION Irecv>
  19. class KernelEM1DSpec;
  20. // ===================================================================
  21. // Class: KernelEM1DReflSpec
  22. /**
  23. @class
  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. // TODO can we use the manager's key instead to lock for that?
  35. struct ctor_key{};
  36. public:
  37. // what do these template parameters do -TI
  38. template<EMMODE Mode2, int Ikernel2, DIPOLE_LOCATION Isource2, DIPOLE_LOCATION Irecv2>
  39. friend class KernelEM1DSpec;
  40. friend class KernelEM1DManager;
  41. // ==================== LIFECYCLE =======================
  42. /// Default locked constructor.
  43. explicit KernelEM1DReflSpec ( const ctor_key& ) : KernelEM1DReflBase( ) {
  44. }
  45. /// Default protected constructor.
  46. ~KernelEM1DReflSpec () {
  47. }
  48. static std::shared_ptr<KernelEM1DReflSpec<Mode, Isource, Irecv> > NewSP() {
  49. return std::make_shared<KernelEM1DReflSpec<Mode, Isource, Irecv> >( ctor_key() );
  50. }
  51. // ==================== OPERATORS =======================
  52. // ==================== OPERATIONS =======================
  53. // ==================== ACCESS =======================
  54. // ==================== INQUIRY =======================
  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. }; // ----- 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 -----