Lemma is an Electromagnetics API
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

KernelEM1DReflSpec.h 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 "kernelem1dreflbase.h"
  15. #include "LayeredEarthEM.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. //template<EMMODE Mode2, int Ikernel2, DIPOLE_LOCATION Isource2, DIPOLE_LOCATION Irecv2>
  38. //friend class KernelEM1DSpec;
  39. friend class KernelEM1DManager;
  40. // ==================== LIFECYCLE =======================
  41. /// Default locked constructor.
  42. explicit KernelEM1DReflSpec ( const ctor_key& ) : KernelEM1DReflBase( ) {
  43. }
  44. /// Default protected constructor.
  45. ~KernelEM1DReflSpec () {
  46. }
  47. static std::shared_ptr<KernelEM1DReflSpec<Mode, Isource, Irecv> > NewSP() {
  48. return std::make_shared<KernelEM1DReflSpec<Mode, Isource, Irecv> >( ctor_key() );
  49. }
  50. // ==================== OPERATORS =======================
  51. // ==================== OPERATIONS =======================
  52. // ==================== ACCESS =======================
  53. // ==================== INQUIRY =======================
  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. }; // ----- end of class KernelEM1DReflSpec -----
  79. //template<EMMODE Mode, DIPOLE_LOCATION Isource, DIPOLE_LOCATION Irecv>
  80. //std::unordered_map<Real, cache> KernelEM1DReflSpec<Mode, Isource, Irecv>::CACHE;
  81. //template<EMMODE Mode, DIPOLE_LOCATION Isource, DIPOLE_LOCATION Irecv>
  82. //cache* KernelEM1DReflSpec<Mode, Isource, Irecv>::tcache;
  83. ///////////////////////////////////////////////
  84. // Declarations of specialisations
  85. template<>
  86. void KernelEM1DReflSpec<TM, INAIR, INAIR>::ComputeReflectionCoeffs(const Real& lambda);
  87. template<>
  88. void KernelEM1DReflSpec<TE, INAIR, INAIR>::ComputeReflectionCoeffs(const Real& lambda);
  89. template<>
  90. void KernelEM1DReflSpec<TM, INAIR, INGROUND>::ComputeReflectionCoeffs(const Real& lambda);
  91. template<>
  92. void KernelEM1DReflSpec<TE, INAIR, INGROUND>::ComputeReflectionCoeffs(const Real& lambda);
  93. template<>
  94. void KernelEM1DReflSpec<TM, INAIR, INAIR>::PreComputePotentialTerms( );
  95. template<>
  96. void KernelEM1DReflSpec<TE, INAIR, INAIR>::PreComputePotentialTerms( );
  97. template<>
  98. void KernelEM1DReflSpec<TM, INAIR, INGROUND>::PreComputePotentialTerms( );
  99. template<>
  100. void KernelEM1DReflSpec<TE, INAIR, INGROUND>::PreComputePotentialTerms( );
  101. ///////////////////////////////////////////////
  102. // Default mode definitions
  103. template<EMMODE Mode, DIPOLE_LOCATION Isource, DIPOLE_LOCATION Irecv>
  104. void KernelEM1DReflSpec<Mode, Isource, Irecv>::ComputeReflectionCoeffs(const Real& lambda) {
  105. static bool called = false;
  106. if (!called) {
  107. std::cout << "unspecialised Reflection function KernelEM1DReflSpec<"
  108. << Mode << ", " << Isource << ", "
  109. << Irecv << " >::ComputeReflectionCoeffs( const Real& lambda ) --> SLOW PERFORMANCE EXPECTED\n";
  110. called = true;
  111. }
  112. rams = lambda*lambda;
  113. //////////////////////////////////////////
  114. // Compute TEM stuff
  115. // This call to sqrt takes ~ 15% of execution time
  116. u = (rams-kk.array()).sqrt();
  117. uk = u(lays);
  118. um = u(layr);
  119. switch (Mode) {
  120. // TM mode
  121. case (TM):
  122. Zyu(1) = -u(0)/yh(0);
  123. Zyi = u.array() / yh.array();
  124. break;
  125. // TE mode
  126. case (TE):
  127. Zyu(1) = -u(0)/zh(0);
  128. Zyi = u.array() / zh.array();
  129. break;
  130. default:
  131. throw 11; //IllegalMode(this);
  132. }
  133. Zyd.tail<1>() = Zyi.tail<1>();
  134. for (int ilay=1; ilay<nlay-1; ++ilay) {
  135. cf(ilay) =
  136. std::exp(-(Real)(2.)*u(ilay)*LayerThickness(ilay));
  137. th(ilay) = ((Real)(1.)-cf(ilay)) / ((Real)(1.)+cf(ilay));
  138. }
  139. // Can't use blocks, b/c recursive
  140. for (int N=1; N<lays; ++N){
  141. Zyu(N+1)=Zyi(N)*(Zyu(N)-Zyi(N)*th(N)) /
  142. (Zyi(N)-Zyu(N)*th(N)) ;
  143. }
  144. int ne = nlay-2;
  145. for (int N=ne; N >=lays+1; --N) {
  146. Zyd(N) = Zyi(N)*(Zyd(N+1)+Zyi(N)*th(N)) /
  147. (Zyi(N)+Zyd(N+1)*th(N)) ;
  148. }
  149. rtd(nlay-1) = 0;
  150. if (layr < lays) {
  151. // Receiver above source layer
  152. int ls = layr;
  153. if (ls == 0) {
  154. ls = layr+1;
  155. }
  156. for (int N=ls; N<=lays; ++N) {
  157. rtu(N)= (Zyi(N)+Zyu(N)) /
  158. (Zyi(N)-Zyu(N)) ;
  159. }
  160. if (lays < nlay-1) {
  161. rtd(lays) = (Zyi(lays)-Zyd(lays+1)) /
  162. (Zyi(lays)+Zyd(lays+1)) ;
  163. }
  164. } else {
  165. // RECEIVER IN OR BELOW THE SOURCE LAYER
  166. if (lays == layr) { // Rx In source Layer
  167. if (layr == 0) {
  168. rtd(0) = (Zyu(1)+Zyd(1)) /
  169. (Zyu(1)-Zyd(1)) ;
  170. } else if (layr == nlay-1) {
  171. rtu(nlay-1) = (Zyi(nlay-1)+Zyu(nlay-1)) /
  172. (Zyi(nlay-1)-Zyu(nlay-1)) ;
  173. } else {
  174. rtu(layr) = (Zyi(layr)+Zyu(layr)) /
  175. (Zyi(layr)-Zyu(layr)) ;
  176. rtd(layr) = (Zyi(layr)-Zyd(layr+1)) /
  177. (Zyi(layr)+Zyd(layr+1)) ;
  178. }
  179. } else { // receiver below source layer
  180. if (lays == 0) {
  181. rtd(0) = (Zyu(1)+Zyd(1)) /
  182. (Zyu(1)-Zyd(1)) ;
  183. } else {
  184. rtu(lays) = (Zyi(lays)+Zyu(lays)) /
  185. (Zyi(lays)-Zyu(lays)) ;
  186. }
  187. int le = layr;
  188. if (le == nlay-1) --le;
  189. int ls = lays;
  190. if (lays == 0 ) ++ls;
  191. // TODO use blocks to vectorize maybe?
  192. // This works but gives same to slightly worse
  193. // performance as loop.
  194. // int nn = le-ls+1;
  195. // rtd.segment(ls, nn) =
  196. // (Zyi.segment(ls , nn).array() -
  197. // Zyd.segment(ls+1, nn).array()).array() /
  198. // (Zyi.segment(ls , nn).array() +
  199. // Zyd.segment(ls+1, nn).array()).array() ;
  200. for (int N=ls; N<=le; ++N) {
  201. rtd(N) = (Zyi(N)-Zyd(N+1)) /
  202. (Zyi(N)+Zyd(N+1)) ;
  203. }
  204. }
  205. } // End in or below source layer
  206. return;
  207. }
  208. template<EMMODE Mode, DIPOLE_LOCATION Isource, DIPOLE_LOCATION Irecv>
  209. void KernelEM1DReflSpec<Mode, Isource, Irecv>::PreComputePotentialTerms( ) {
  210. static bool called = false;
  211. if (!called) {
  212. std::cerr << "unspecialised function KernelEM1DReflSpec<"
  213. << Mode << ", " << Isource << ", "
  214. << Irecv << " >::PreComputePotentialTerms\n";
  215. called = true;
  216. }
  217. }
  218. } // ----- end of Lemma name -----
  219. #endif // ----- #ifndef KERNELEM1DREFLSPEC_INC -----