nmie.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #ifndef SRC_NMIE_HPP_
  2. #define SRC_NMIE_HPP_
  3. //**********************************************************************************//
  4. // Copyright (C) 2009-2018 Ovidio Pena <ovidio@bytesfall.com> //
  5. // Copyright (C) 2013-2018 Konstantin Ladutenko <kostyfisik@gmail.com> //
  6. // //
  7. // This file is part of scattnlay //
  8. // //
  9. // This program is free software: you can redistribute it and/or modify //
  10. // it under the terms of the GNU General Public License as published by //
  11. // the Free Software Foundation, either version 3 of the License, or //
  12. // (at your option) any later version. //
  13. // //
  14. // This program is distributed in the hope that it will be useful, //
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  17. // GNU General Public License for more details. //
  18. // //
  19. // The only additional remark is that we expect that all publications //
  20. // describing work using this software, or all commercial products //
  21. // using it, cite at least one of the following references: //
  22. // [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  23. // a multilayered sphere," Computer Physics Communications, //
  24. // vol. 180, Nov. 2009, pp. 2348-2354. //
  25. // [2] K. Ladutenko, U. Pal, A. Rivera, and O. Pena-Rodriguez, "Mie //
  26. // calculation of electromagnetic near-field for a multilayered //
  27. // sphere," Computer Physics Communications, vol. 214, May 2017, //
  28. // pp. 225-230. //
  29. // //
  30. // You should have received a copy of the GNU General Public License //
  31. // along with this program. If not, see <http://www.gnu.org/licenses/>. //
  32. //**********************************************************************************//
  33. #define VERSION "2.2"
  34. #include <array>
  35. #include <complex>
  36. #include <cstdlib>
  37. #include <iostream>
  38. #include <vector>
  39. #include <boost/math/constants/constants.hpp>
  40. namespace nmie {
  41. int ScattCoeffs(const unsigned int L, const int pl, std::vector<double>& x, std::vector<std::complex<double> >& m, const int nmax, std::vector<std::complex<double> >& an, std::vector<std::complex<double> >& bn);
  42. int nMie(const unsigned int L, const int pl, std::vector<double>& x, std::vector<std::complex<double> >& m, const unsigned int nTheta, std::vector<double>& Theta, const int nmax, double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo, std::vector<std::complex<double> >& S1, std::vector<std::complex<double> >& S2);
  43. int nMie(const unsigned int L, std::vector<double>& x, std::vector<std::complex<double> >& m, const unsigned int nTheta, std::vector<double>& Theta, double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo, std::vector<std::complex<double> >& S1, std::vector<std::complex<double> >& S2);
  44. int nMie(const unsigned int L, const int pl, std::vector<double>& x, std::vector<std::complex<double> >& m, const unsigned int nTheta, std::vector<double>& Theta, double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo, std::vector<std::complex<double> >& S1, std::vector<std::complex<double> >& S2);
  45. int nMie(const unsigned int L, std::vector<double>& x, std::vector<std::complex<double> >& m, const unsigned int nTheta, std::vector<double>& Theta, const int nmax, double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo, std::vector<std::complex<double> >& S1, std::vector<std::complex<double> >& S2);
  46. int nField(const unsigned int L, const int pl, const std::vector<double>& x, const std::vector<std::complex<double> >& m, const int nmax, const unsigned int ncoord, const std::vector<double>& Xp, const std::vector<double>& Yp, const std::vector<double>& Zp, std::vector<std::vector<std::complex<double> > >& E, std::vector<std::vector<std::complex<double> > >& H);
  47. template <typename FloatType = double>
  48. class MultiLayerMie {
  49. public:
  50. //Used constants TODO! Change to boost PI
  51. const double PI_=3.14159265358979323846;
  52. // light speed [m s-1]
  53. const double cc_ = 2.99792458e8;
  54. // assume non-magnetic (MU=MU0=const) [N A-2]
  55. const double mu_ = 4.0*PI_*1.0e-7;
  56. // Run calculation
  57. void RunMieCalculation();
  58. void RunFieldCalculation();
  59. void calcScattCoeffs();
  60. // Return calculation results
  61. FloatType GetQext();
  62. FloatType GetQsca();
  63. FloatType GetQabs();
  64. FloatType GetQbk();
  65. FloatType GetQpr();
  66. FloatType GetAsymmetryFactor();
  67. FloatType GetAlbedo();
  68. std::vector<std::complex<FloatType> > GetS1();
  69. std::vector<std::complex<FloatType> > GetS2();
  70. std::vector<std::complex<FloatType> > GetAn(){return an_;};
  71. std::vector<std::complex<FloatType> > GetBn(){return bn_;};
  72. // Problem definition
  73. // Modify size of all layers
  74. void SetLayersSize(const std::vector<FloatType>& layer_size);
  75. // Modify refractive index of all layers
  76. void SetLayersIndex(const std::vector< std::complex<FloatType> >& index);
  77. // Modify scattering (theta) angles
  78. void SetAngles(const std::vector<FloatType>& angles);
  79. // Modify coordinates for field calculation
  80. void SetFieldCoords(const std::vector< std::vector<FloatType> >& coords);
  81. // Modify index of PEC layer
  82. void SetPECLayer(int layer_position = 0);
  83. // Set a fixed value for the maximun number of terms
  84. void SetMaxTerms(int nmax);
  85. // Get maximun number of terms
  86. int GetMaxTerms() {return nmax_;};
  87. bool isMieCalculated(){return isMieCalculated_;};
  88. // Clear layer information
  89. void ClearLayers();
  90. void MarkUncalculated();
  91. // Read parameters
  92. // Get total size parameter of particle
  93. FloatType GetSizeParameter();
  94. // Returns size of all layers
  95. std::vector<FloatType> GetLayersSize(){return size_param_;};
  96. // Returns refractive index of all layers
  97. std::vector<std::complex<FloatType> > GetLayersIndex(){return refractive_index_;};
  98. // Returns scattering (theta) angles
  99. std::vector<FloatType> GetAngles(){return theta_;};
  100. // Returns coordinates used for field calculation
  101. std::vector<std::vector<FloatType> > GetFieldCoords(){return coords_;};
  102. // Returns index of PEC layer
  103. int GetPECLayer(){return PEC_layer_position_;};
  104. std::vector<std::vector< std::complex<FloatType> > > GetFieldE(){return E_;}; // {X[], Y[], Z[]}
  105. std::vector<std::vector< std::complex<FloatType> > > GetFieldH(){return H_;};
  106. // Get fields in spherical coordinates.
  107. std::vector<std::vector< std::complex<FloatType> > > GetFieldEs(){return E_;}; // {rho[], teha[], phi[]}
  108. std::vector<std::vector< std::complex<FloatType> > > GetFieldHs(){return H_;};
  109. protected:
  110. // Size parameter for all layers
  111. std::vector<FloatType> size_param_;
  112. // Refractive index for all layers
  113. std::vector< std::complex<FloatType> > refractive_index_;
  114. // Scattering coefficients
  115. std::vector<std::complex<FloatType> > an_, bn_;
  116. std::vector< std::vector<std::complex<FloatType> > > aln_, bln_, cln_, dln_;
  117. void calcExpanCoeffs();
  118. // Points for field evaluation
  119. std::vector< std::vector<FloatType> > coords_;
  120. private:
  121. void calcNstop();
  122. void calcNmax(unsigned int first_layer);
  123. std::complex<FloatType> calc_an(int n, FloatType XL, std::complex<FloatType> Ha, std::complex<FloatType> mL,
  124. std::complex<FloatType> PsiXL, std::complex<FloatType> ZetaXL,
  125. std::complex<FloatType> PsiXLM1, std::complex<FloatType> ZetaXLM1);
  126. std::complex<FloatType> calc_bn(int n, FloatType XL, std::complex<FloatType> Hb, std::complex<FloatType> mL,
  127. std::complex<FloatType> PsiXL, std::complex<FloatType> ZetaXL,
  128. std::complex<FloatType> PsiXLM1, std::complex<FloatType> ZetaXLM1);
  129. std::complex<FloatType> calc_S1(int n, std::complex<FloatType> an, std::complex<FloatType> bn,
  130. FloatType Pi, FloatType Tau);
  131. std::complex<FloatType> calc_S2(int n, std::complex<FloatType> an, std::complex<FloatType> bn,
  132. FloatType Pi, FloatType Tau);
  133. void calcD1D3(std::complex<FloatType> z,
  134. std::vector<std::complex<FloatType> >& D1,
  135. std::vector<std::complex<FloatType> >& D3);
  136. void calcPsiZeta(std::complex<FloatType> x,
  137. std::vector<std::complex<FloatType> >& Psi,
  138. std::vector<std::complex<FloatType> >& Zeta);
  139. void calcPiTau(const FloatType& costheta,
  140. std::vector<FloatType>& Pi, std::vector<FloatType>& Tau);
  141. void calcSpherHarm(const std::complex<FloatType> Rho, const FloatType Theta, const FloatType Phi,
  142. const std::complex<FloatType>& rn, const std::complex<FloatType>& Dn,
  143. const FloatType& Pi, const FloatType& Tau, const FloatType& n,
  144. std::vector<std::complex<FloatType> >& Mo1n, std::vector<std::complex<FloatType> >& Me1n,
  145. std::vector<std::complex<FloatType> >& No1n, std::vector<std::complex<FloatType> >& Ne1n);
  146. void calcField(const FloatType Rho, const FloatType Theta, const FloatType Phi,
  147. std::vector<std::complex<FloatType> >& E, std::vector<std::complex<FloatType> >& H);
  148. bool isExpCoeffsCalc_ = false;
  149. bool isScaCoeffsCalc_ = false;
  150. bool isMieCalculated_ = false;
  151. // Scattering angles for scattering pattern in radians
  152. std::vector<FloatType> theta_;
  153. // Should be -1 if there is no PEC.
  154. int PEC_layer_position_ = -1;
  155. // with calcNmax(int first_layer);
  156. int nmax_ = -1;
  157. int nmax_preset_ = -1;
  158. /// Store result
  159. FloatType Qsca_ = 0.0, Qext_ = 0.0, Qabs_ = 0.0, Qbk_ = 0.0, Qpr_ = 0.0, asymmetry_factor_ = 0.0, albedo_ = 0.0;
  160. std::vector<std::vector< std::complex<FloatType> > > E_, H_; // {X[], Y[], Z[]}
  161. std::vector<std::vector< std::complex<FloatType> > > Es_, Hs_; // {X[], Y[], Z[]}
  162. std::vector<std::complex<FloatType> > S1_, S2_;
  163. //Temporary variables
  164. std::vector<std::complex<FloatType> > PsiZeta_;
  165. }; // end of class MultiLayerMie
  166. } // end of namespace nmie
  167. #endif // SRC_NMIE_HPP_