nmie.hpp 18 KB

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