nmie.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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" //Compare with Makefile and setup.py
  34. #include <array>
  35. #include <complex>
  36. #include <cstdlib>
  37. #include <iostream>
  38. #include <vector>
  39. #include "nmie-precision.hpp"
  40. #ifdef MULTI_PRECISION
  41. #include <boost/math/constants/constants.hpp>
  42. #endif
  43. namespace nmie {
  44. int ScattCoeffs(const unsigned int L, const int pl,
  45. const std::vector<double>& x, const std::vector<std::complex<double> >& m,
  46. const int nmax,
  47. std::vector<std::complex<double> >& an,
  48. std::vector<std::complex<double> >& bn);
  49. int ExpanCoeffs(const unsigned int L, const int pl,
  50. const std::vector<double>& x, const std::vector<std::complex<double> >& m,
  51. const int nmax,
  52. std::vector<std::vector<std::complex<double> > >& an,
  53. std::vector<std::vector<std::complex<double> > >& bn,
  54. std::vector<std::vector<std::complex<double> > >& cn,
  55. std::vector<std::vector<std::complex<double> > >& dn);
  56. //helper functions
  57. template<class T> inline T pow2(const T value) {return value*value;}
  58. template<class T> inline T cabs(const std::complex<T> value)
  59. {return nmm::sqrt(pow2(value.real()) + pow2(value.imag()));}
  60. template <typename FloatType>
  61. int newround(FloatType x) {
  62. return x >= 0 ? static_cast<int>(x + 0.5):static_cast<int>(x - 0.5);
  63. //return x >= 0 ? (x + 0.5).convert_to<int>():(x - 0.5).convert_to<int>();
  64. }
  65. template<typename T>
  66. inline std::complex<T> my_exp(const std::complex<T>& x) {
  67. using std::exp; // use ADL
  68. T const& r = exp(x.real());
  69. return std::polar(r, x.imag());
  70. }
  71. // pl, nmax, mode_n, mode_type
  72. int nMie(const unsigned int L,
  73. const int pl,
  74. std::vector<double>& x, std::vector<std::complex<double> >& m,
  75. const unsigned int nTheta, std::vector<double>& Theta,
  76. const int nmax,
  77. double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr,
  78. double *g, double *Albedo,
  79. std::vector<std::complex<double> >& S1, std::vector<std::complex<double> >& S2,
  80. int mode_n, int mode_type);
  81. // pl and nmax
  82. int nMie(const unsigned int L,
  83. const int pl,
  84. std::vector<double>& x, std::vector<std::complex<double> >& m,
  85. const unsigned int nTheta, std::vector<double>& Theta,
  86. const int nmax,
  87. double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr,
  88. double *g, double *Albedo,
  89. std::vector<std::complex<double> >& S1, std::vector<std::complex<double> >& S2);
  90. // no pl and nmax
  91. int nMie(const unsigned int L,
  92. std::vector<double>& x, std::vector<std::complex<double> >& m,
  93. const unsigned int nTheta, std::vector<double>& Theta,
  94. double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr,
  95. double *g, double *Albedo,
  96. std::vector<std::complex<double> >& S1, std::vector<std::complex<double> >& S2);
  97. // pl
  98. int nMie(const unsigned int L,
  99. const int pl,
  100. std::vector<double>& x, std::vector<std::complex<double> >& m,
  101. const unsigned int nTheta, std::vector<double>& Theta,
  102. double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr,
  103. double *g, double *Albedo,
  104. std::vector<std::complex<double> >& S1, std::vector<std::complex<double> >& S2);
  105. // nmax
  106. int nMie(const unsigned int L,
  107. std::vector<double>& x, std::vector<std::complex<double> >& m,
  108. const unsigned int nTheta, std::vector<double>& Theta,
  109. const int nmax,
  110. double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr,
  111. double *g, double *Albedo,
  112. std::vector<std::complex<double> >& S1, std::vector<std::complex<double> >& S2);
  113. int nField(const unsigned int L, const int pl,
  114. const std::vector<double>& x, const std::vector<std::complex<double> >& m, const int nmax,
  115. const int mode_n, const int mode_type, const unsigned int ncoord,
  116. const std::vector<double>& Xp, const std::vector<double>& Yp, const std::vector<double>& Zp,
  117. std::vector<std::vector<std::complex<double> > >& E, std::vector<std::vector<std::complex<double> > >& H);
  118. // constants for per mode evaluation
  119. enum Modes {kAll = -1, kElectric = 0, kMagnetic = 1};
  120. template <typename FloatType = double>
  121. class MultiLayerMie {
  122. public:
  123. const FloatType PI_=3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198938095257201065485863278865936153381827968230301952035301852968995773622599413891249721775283479131515574857242454150695950829533116861727855889075098381754637464939319255060400927701671139009848824012858361603563707660104710181942955596198946767837449448255379774726847104047534646208046684259069491293313677028989152104752162056966024058038150193511253382430035587640247496473263914199272604269922796782354781636009341721641219924586315030286182974555706749838505494588586926995690927210797509302955321165344987202755960236480665499119881834797753566369807426542527862551818417574672890977772793800081647060016145249192173217214772350141441973568548161361157352552133475741849468438523323907394143334547762416862518983569485562099219222184272550254256887671790494601653466804988627232791786085784383827967976681454100953883786360950680064225125205117392984896084128488626945604241965285022210661186306744278622039194945047123713786960956364371917287467764657573962413890865832645995813390478027590099465764078951269468398352595709825822620522489407726719478268482601476990902640136394437455305068203496252451749399651431429809190659250937221696461515709858387410597885959772975498930161753928468138268683868942774155991855925245953959431049972524680845987273644695848653836736222626099124608051243884390451244136549762780797715691435997700129616089441694868555848406353422072225828488648158456028506016842739452267467678895252138522549954666727823986456596116354886230577456498035593634568174324112515076069479451096596094025228879710893145669136867228748940560101503308;
  124. // light speed [m s-1]
  125. const double cc_ = 2.99792458e8;
  126. // assume non-magnetic (MU=MU0=const) [N A-2]
  127. const FloatType mu_ = 4.0*PI_*1.0e-7;
  128. // Run calculation
  129. void RunMieCalculation();
  130. void RunFieldCalculation();
  131. void RunFieldCalculationPolar(const int input_outer_perimeter_points = 1,
  132. const int radius_points=1,
  133. const double from_Rho=0, const double to_Rho=static_cast<double>(1.),
  134. const double from_Theta=0, const double to_Theta=static_cast<double>(3.14159265358979323),
  135. const double from_Phi=0, const double to_Phi=static_cast<double>(3.14159265358979323));
  136. void calcScattCoeffs();
  137. void calcExpanCoeffs();
  138. // Return calculation results
  139. FloatType GetQext();
  140. FloatType GetQsca();
  141. FloatType GetQabs();
  142. FloatType GetQbk();
  143. FloatType GetQpr();
  144. FloatType GetAsymmetryFactor();
  145. FloatType GetAlbedo();
  146. std::vector<std::complex<FloatType> > GetS1();
  147. std::vector<std::complex<FloatType> > GetS2();
  148. std::vector<std::complex<FloatType> > GetAn(){return an_;};
  149. std::vector<std::complex<FloatType> > GetBn(){return bn_;};
  150. std::vector< std::vector<std::complex<FloatType> > > GetLayerAn(){return aln_;};
  151. std::vector< std::vector<std::complex<FloatType> > > GetLayerBn(){return bln_;};
  152. std::vector< std::vector<std::complex<FloatType> > > GetLayerCn(){return cln_;};
  153. std::vector< std::vector<std::complex<FloatType> > > GetLayerDn(){return dln_;};
  154. // Problem definition
  155. // Modify size of all layers
  156. void SetLayersSize(const std::vector<FloatType>& layer_size);
  157. // Modify refractive index of all layers
  158. void SetLayersIndex(const std::vector< std::complex<FloatType> >& index);
  159. // Modify scattering (theta) angles
  160. void SetAngles(const std::vector<FloatType>& angles);
  161. // Modify coordinates for field calculation
  162. void SetFieldCoords(const std::vector< std::vector<FloatType> >& coords);
  163. // Modify index of PEC layer
  164. void SetPECLayer(int layer_position = 0);
  165. // Modify the mode taking into account for evaluation of output variables
  166. void SetModeNmaxAndType(int mode_n, int mode_type){mode_n_ = mode_n; mode_type_ = mode_type;};
  167. // Set a fixed value for the maximun number of terms
  168. void SetMaxTerms(int nmax);
  169. // Get maximum number of terms
  170. int GetMaxTerms() {return nmax_;};
  171. bool isMieCalculated(){return isMieCalculated_;};
  172. // Clear layer information
  173. void ClearLayers();
  174. void MarkUncalculated();
  175. // Read parameters
  176. // Get total size parameter of particle
  177. FloatType GetSizeParameter();
  178. // Returns size of all layers
  179. std::vector<FloatType> GetLayersSize(){return size_param_;};
  180. // Returns refractive index of all layers
  181. std::vector<std::complex<FloatType> > GetLayersIndex(){return refractive_index_;};
  182. // Returns scattering (theta) angles
  183. std::vector<FloatType> GetAngles(){return theta_;};
  184. // Returns coordinates used for field calculation
  185. std::vector<std::vector<FloatType> > GetFieldCoords(){return coords_;};
  186. // Returns index of PEC layer
  187. int GetPECLayer(){return PEC_layer_position_;};
  188. std::vector<std::vector< std::complex<FloatType> > > GetFieldE(){return E_;}; // {X[], Y[], Z[]}
  189. std::vector<std::vector< std::complex<FloatType> > > GetFieldH(){return H_;};
  190. // Get fields in spherical coordinates.
  191. std::vector<std::vector< std::complex<FloatType> > > GetFieldEs(){return E_;}; // {rho[], teha[], phi[]}
  192. std::vector<std::vector< std::complex<FloatType> > > GetFieldHs(){return H_;};
  193. protected:
  194. // Size parameter for all layers
  195. std::vector<FloatType> size_param_;
  196. // Refractive index for all layers
  197. std::vector< std::complex<FloatType> > refractive_index_;
  198. // Scattering coefficients
  199. std::vector<std::complex<FloatType> > an_, bn_;
  200. std::vector< std::vector<std::complex<FloatType> > > aln_, bln_, cln_, dln_;
  201. // Points for field evaluation
  202. std::vector< std::vector<FloatType> > coords_;
  203. private:
  204. unsigned int calcNstop(FloatType xL = -1);
  205. unsigned int calcNmax(FloatType xL = -1);
  206. std::complex<FloatType> calc_an(int n, FloatType XL, std::complex<FloatType> Ha, std::complex<FloatType> mL,
  207. std::complex<FloatType> PsiXL, std::complex<FloatType> ZetaXL,
  208. std::complex<FloatType> PsiXLM1, std::complex<FloatType> ZetaXLM1);
  209. std::complex<FloatType> calc_bn(int n, FloatType XL, std::complex<FloatType> Hb, std::complex<FloatType> mL,
  210. std::complex<FloatType> PsiXL, std::complex<FloatType> ZetaXL,
  211. std::complex<FloatType> PsiXLM1, std::complex<FloatType> ZetaXLM1);
  212. std::complex<FloatType> calc_S1(int n, std::complex<FloatType> an, std::complex<FloatType> bn,
  213. FloatType Pi, FloatType Tau);
  214. std::complex<FloatType> calc_S2(int n, std::complex<FloatType> an, std::complex<FloatType> bn,
  215. FloatType Pi, FloatType Tau);
  216. void calcD1D3(std::complex<FloatType> z,
  217. std::vector<std::complex<FloatType> >& D1,
  218. std::vector<std::complex<FloatType> >& D3);
  219. void calcPsiZeta(std::complex<FloatType> x,
  220. std::vector<std::complex<FloatType> >& Psi,
  221. std::vector<std::complex<FloatType> >& Zeta);
  222. void calcPiTau(const FloatType& costheta,
  223. std::vector<FloatType>& Pi, std::vector<FloatType>& Tau);
  224. void calcSpherHarm(const std::complex<FloatType> Rho, const FloatType Theta, const FloatType Phi,
  225. const std::complex<FloatType>& rn, const std::complex<FloatType>& Dn,
  226. const FloatType& Pi, const FloatType& Tau, const FloatType& n,
  227. std::vector<std::complex<FloatType> >& Mo1n, std::vector<std::complex<FloatType> >& Me1n,
  228. std::vector<std::complex<FloatType> >& No1n, std::vector<std::complex<FloatType> >& Ne1n);
  229. void calcFieldByComponents(const FloatType Rho, const FloatType Theta, const FloatType Phi,
  230. std::vector<std::complex<FloatType> >& E,
  231. std::vector<std::complex<FloatType> >& H);
  232. bool isExpCoeffsCalc_ = false;
  233. bool isScaCoeffsCalc_ = false;
  234. bool isMieCalculated_ = false;
  235. // Scattering angles for scattering pattern in radians
  236. std::vector<FloatType> theta_;
  237. // Should be -1 if there is no PEC.
  238. int PEC_layer_position_ = -1;
  239. int mode_n_ = Modes::kAll;
  240. int mode_type_ = Modes::kAll;
  241. // with calcNmax(int first_layer);
  242. int nmax_ = -1;
  243. int nmax_preset_ = -1;
  244. /// Store result
  245. FloatType Qsca_ = 0.0, Qext_ = 0.0, Qabs_ = 0.0, Qbk_ = 0.0, Qpr_ = 0.0, asymmetry_factor_ = 0.0, albedo_ = 0.0;
  246. std::vector<std::vector< std::complex<FloatType> > > E_, H_; // {X[], Y[], Z[]}
  247. std::vector<std::vector< std::complex<FloatType> > > Es_, Hs_; // {X[], Y[], Z[]}
  248. std::vector<std::complex<FloatType> > S1_, S2_;
  249. }; // end of class MultiLayerMie
  250. } // end of namespace nmie
  251. #endif // SRC_NMIE_HPP_