nmie-basic.hpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. #ifndef SRC_NMIE_BASIC_HPP_
  2. #define SRC_NMIE_BASIC_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. //**********************************************************************************//
  34. // This class implements the algorithm for a multilayered sphere described by: //
  35. // [1] W. Yang, "Improved recursive algorithm for light scattering by a //
  36. // multilayered sphere,” Applied Optics, vol. 42, Mar. 2003, pp. 1710-1720. //
  37. // //
  38. // You can find the description of all the used equations in: //
  39. // [2] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  40. // a multilayered sphere," Computer Physics Communications, //
  41. // vol. 180, Nov. 2009, pp. 2348-2354. //
  42. // [3] K. Ladutenko, U. Pal, A. Rivera, and O. Pena-Rodriguez, "Mie //
  43. // calculation of electromagnetic near-field for a multilayered //
  44. // sphere," Computer Physics Communications, vol. 214, May 2017, //
  45. // pp. 225-230. //
  46. // //
  47. // Hereinafter all equations numbers refer to [2] //
  48. //**********************************************************************************//
  49. #include <iostream>
  50. #include <iomanip>
  51. #include <stdexcept>
  52. #include <vector>
  53. #include "special-functions-impl.hpp"
  54. #include "nmie.hpp"
  55. namespace nmie {
  56. //class implementation
  57. // ********************************************************************** //
  58. // Returns previously calculated Qext //
  59. // ********************************************************************** //
  60. template <typename FloatType>
  61. template <typename outputType>
  62. outputType MultiLayerMie<FloatType>::GetQext() {
  63. if (!isMieCalculated_)
  64. throw std::invalid_argument("You should run calculations before result request!");
  65. return static_cast<outputType>(Qext_);
  66. }
  67. // ********************************************************************** //
  68. // Returns previously calculated Qabs //
  69. // ********************************************************************** //
  70. template <typename FloatType>
  71. template <typename outputType>
  72. outputType MultiLayerMie<FloatType>::GetQabs() {
  73. if (!isMieCalculated_)
  74. throw std::invalid_argument("You should run calculations before result request!");
  75. return static_cast<outputType>(Qabs_);
  76. }
  77. // ********************************************************************** //
  78. // Returns previously calculated Qsca //
  79. // ********************************************************************** //
  80. template <typename FloatType>
  81. template <typename outputType>
  82. outputType MultiLayerMie<FloatType>::GetQsca() {
  83. if (!isMieCalculated_)
  84. throw std::invalid_argument("You should run calculations before result request!");
  85. return static_cast<outputType>(Qsca_);
  86. }
  87. // ********************************************************************** //
  88. // Returns previously calculated Qbk //
  89. // ********************************************************************** //
  90. template <typename FloatType>
  91. template <typename outputType>
  92. outputType MultiLayerMie<FloatType>::GetQbk() {
  93. if (!isMieCalculated_)
  94. throw std::invalid_argument("You should run calculations before result request!");
  95. return static_cast<outputType>(Qbk_);
  96. }
  97. // ********************************************************************** //
  98. // Returns previously calculated Qpr //
  99. // ********************************************************************** //
  100. template <typename FloatType>
  101. template <typename outputType>
  102. outputType MultiLayerMie<FloatType>::GetQpr() {
  103. if (!isMieCalculated_)
  104. throw std::invalid_argument("You should run calculations before result request!");
  105. return static_cast<outputType>(Qpr_);
  106. }
  107. // ********************************************************************** //
  108. // Returns previously calculated assymetry factor //
  109. // ********************************************************************** //
  110. template <typename FloatType>
  111. template <typename outputType>
  112. outputType MultiLayerMie<FloatType>::GetAsymmetryFactor() {
  113. if (!isMieCalculated_)
  114. throw std::invalid_argument("You should run calculations before result request!");
  115. return static_cast<outputType>(asymmetry_factor_);
  116. }
  117. // ********************************************************************** //
  118. // Returns previously calculated Albedo //
  119. // ********************************************************************** //
  120. template <typename FloatType>
  121. template <typename outputType>
  122. outputType MultiLayerMie<FloatType>::GetAlbedo() {
  123. if (!isMieCalculated_)
  124. throw std::invalid_argument("You should run calculations before result request!");
  125. return static_cast<outputType>(albedo_);
  126. }
  127. // ********************************************************************** //
  128. // Returns previously calculated S1 //
  129. // ********************************************************************** //
  130. template <typename FloatType>
  131. std::vector<std::complex<FloatType> > MultiLayerMie<FloatType>::GetS1() {
  132. if (!isMieCalculated_)
  133. throw std::invalid_argument("You should run calculations before result request!");
  134. return S1_;
  135. }
  136. // ********************************************************************** //
  137. // Returns previously calculated S2 //
  138. // ********************************************************************** //
  139. template <typename FloatType>
  140. std::vector<std::complex<FloatType> > MultiLayerMie<FloatType>::GetS2() {
  141. if (!isMieCalculated_)
  142. throw std::invalid_argument("You should run calculations before result request!");
  143. return S2_;
  144. }
  145. // ********************************************************************** //
  146. // Modify scattering (theta) angles //
  147. // ********************************************************************** //
  148. template <typename FloatType>
  149. void MultiLayerMie<FloatType>::SetAngles(const std::vector<FloatType> &angles) {
  150. MarkUncalculated();
  151. theta_ = angles;
  152. }
  153. // ********************************************************************** //
  154. // Modify size of all layers //
  155. // ********************************************************************** //
  156. template <typename FloatType>
  157. void MultiLayerMie<FloatType>::SetLayersSize(const std::vector<FloatType> &layer_size) {
  158. MarkUncalculated();
  159. size_param_.clear();
  160. FloatType prev_layer_size = 0.0;
  161. for (auto curr_layer_size : layer_size) {
  162. if (curr_layer_size <= 0.0)
  163. throw std::invalid_argument("Size parameter should be positive!");
  164. if (prev_layer_size > curr_layer_size)
  165. throw std::invalid_argument
  166. ("Size parameter for next layer should be larger than the previous one!");
  167. prev_layer_size = curr_layer_size;
  168. size_param_.push_back(curr_layer_size);
  169. }
  170. }
  171. // ********************************************************************** //
  172. // Modify refractive index of all layers //
  173. // ********************************************************************** //
  174. template <typename FloatType>
  175. void MultiLayerMie<FloatType>::SetLayersIndex(const std::vector< std::complex<FloatType> > &index) {
  176. MarkUncalculated();
  177. refractive_index_ = index;
  178. }
  179. // ********************************************************************** //
  180. // Modify coordinates for field calculation //
  181. // ********************************************************************** //
  182. template <typename FloatType>
  183. void MultiLayerMie<FloatType>::SetFieldCoords(const std::vector< std::vector<FloatType> > &coords) {
  184. if (coords.size() != 3)
  185. throw std::invalid_argument("Error! Wrong dimension of field monitor points!");
  186. if (coords[0].size() != coords[1].size() || coords[0].size() != coords[2].size())
  187. throw std::invalid_argument("Error! Missing coordinates for field monitor points!");
  188. coords_ = coords;
  189. }
  190. // ********************************************************************** //
  191. // Modify index of PEC layer //
  192. // ********************************************************************** //
  193. template <typename FloatType>
  194. void MultiLayerMie<FloatType>::SetPECLayer(int layer_position) {
  195. MarkUncalculated();
  196. if (layer_position < 0 && layer_position != -1)
  197. throw std::invalid_argument("Error! Layers are numbered from 0!");
  198. PEC_layer_position_ = layer_position;
  199. }
  200. // ********************************************************************** //
  201. // Set maximun number of terms to be used //
  202. // ********************************************************************** //
  203. template <typename FloatType>
  204. void MultiLayerMie<FloatType>::SetMaxTerms(int nmax) {
  205. MarkUncalculated();
  206. nmax_preset_ = nmax;
  207. }
  208. // ********************************************************************** //
  209. // Get total size parameter of particle //
  210. // ********************************************************************** //
  211. template <typename FloatType>
  212. FloatType MultiLayerMie<FloatType>::GetSizeParameter() {
  213. if (size_param_.size() > 0)
  214. return size_param_.back();
  215. else
  216. return 0;
  217. }
  218. // ********************************************************************** //
  219. // Mark uncalculated //
  220. // ********************************************************************** //
  221. template <typename FloatType>
  222. void MultiLayerMie<FloatType>::MarkUncalculated() {
  223. isExpCoeffsCalc_ = false;
  224. isScaCoeffsCalc_ = false;
  225. isMieCalculated_ = false;
  226. }
  227. // ********************************************************************** //
  228. // Clear layer information //
  229. // ********************************************************************** //
  230. template <typename FloatType>
  231. void MultiLayerMie<FloatType>::ClearLayers() {
  232. MarkUncalculated();
  233. size_param_.clear();
  234. refractive_index_.clear();
  235. }
  236. // ********************************************************************** //
  237. // ********************************************************************** //
  238. // ********************************************************************** //
  239. // Computational core
  240. // ********************************************************************** //
  241. // ********************************************************************** //
  242. // ********************************************************************** //
  243. template <typename FloatType>
  244. unsigned int LeRu_near_field_cutoff(const std::complex<FloatType> zz) {
  245. std::complex<double> z = ConvertComplex<double>(zz);
  246. auto x = std::abs(z);
  247. return std::round(x + 11 * std::pow(x, (1.0 / 3.0)) + 1);
  248. // return 10000;
  249. }
  250. // ********************************************************************** //
  251. // Calculate calcNstop - equation (17) //
  252. // ********************************************************************** //
  253. template <typename FloatType>
  254. unsigned int MultiLayerMie<FloatType>::calcNstop(FloatType xL) {
  255. unsigned int nmax = 0;
  256. //Wiscombe
  257. if (xL < size_param_.back()) xL = size_param_.back();
  258. if (xL <= 8) {
  259. nmax = newround(xL + 4.0*pow(xL, 1.0/3.0) + 1);
  260. } else if (xL <= 4200) {
  261. nmax = newround(xL + 4.05*pow(xL, 1.0/3.0) + 2);
  262. } else {
  263. nmax = newround(xL + 4.0*pow(xL, 1.0/3.0) + 2);
  264. }
  265. //Use Le Ru cutoff for near field, as a universal one.
  266. auto Nstop = nmie::LeRu_near_field_cutoff(std::complex<FloatType>(xL, 0))+1;
  267. if (Nstop > nmax) nmax = Nstop;
  268. return nmax;
  269. }
  270. // ********************************************************************** //
  271. // Maximum number of terms required for the calculation //
  272. // ********************************************************************** //
  273. template <typename FloatType>
  274. unsigned int MultiLayerMie<FloatType>::calcNmax(FloatType xL) {
  275. const int pl = PEC_layer_position_;
  276. const unsigned int first_layer = (pl > 0) ? pl : 0;
  277. unsigned int ri, riM1, nmax = 0;
  278. const std::vector<FloatType> &x = size_param_;
  279. const std::vector<std::complex<FloatType> > &m = refractive_index_;
  280. nmax = calcNstop(xL);
  281. for (unsigned int i = first_layer; i < x.size(); i++) {
  282. if (static_cast<int>(i) > PEC_layer_position_) // static_cast used to avoid warning
  283. ri = newround(cabs(x[i]*m[i]));
  284. else
  285. ri = 0;
  286. nmax = std::max(nmax, ri);
  287. // first layer is pec, if pec is present
  288. if ((i > first_layer) && (static_cast<int>(i - 1) > PEC_layer_position_))
  289. riM1 = newround(cabs(x[i - 1]* m[i]));
  290. else
  291. riM1 = 0;
  292. nmax = std::max(nmax, riM1);
  293. }
  294. nmax += 15; // Final nmax value
  295. #ifdef MULTI_PRECISION
  296. nmax += MULTI_PRECISION; //TODO we may need to use more terms that this for MP computations.
  297. #endif
  298. // nmax *= nmax;
  299. // printf("using nmax %i\n", nmax);
  300. return nmax;
  301. }
  302. // ********************************************************************** //
  303. // Calculate an - equation (5) //
  304. // ********************************************************************** //
  305. template <typename FloatType>
  306. std::complex<FloatType> MultiLayerMie<FloatType>::
  307. calc_an(int n, FloatType XL, std::complex<FloatType> Ha, std::complex<FloatType> mL,
  308. std::complex<FloatType> PsiXL, std::complex<FloatType> ZetaXL,
  309. std::complex<FloatType> PsiXLM1, std::complex<FloatType> ZetaXLM1) {
  310. std::complex<FloatType> Num = (Ha/mL + n/XL)*PsiXL - PsiXLM1;
  311. std::complex<FloatType> Denom = (Ha/mL + n/XL)*ZetaXL - ZetaXLM1;
  312. // std::cout<< std::setprecision(100)
  313. // << "Ql " << PsiXL
  314. // << std::endl;
  315. return Num/Denom;
  316. }
  317. // ********************************************************************** //
  318. // Calculate bn - equation (6) //
  319. // ********************************************************************** //
  320. template <typename FloatType>
  321. std::complex<FloatType> MultiLayerMie<FloatType>::calc_bn(int n, FloatType XL, std::complex<FloatType> Hb, std::complex<FloatType> mL,
  322. std::complex<FloatType> PsiXL, std::complex<FloatType> ZetaXL,
  323. std::complex<FloatType> PsiXLM1, std::complex<FloatType> ZetaXLM1) {
  324. std::complex<FloatType> Num = (mL*Hb + n/XL)*PsiXL - PsiXLM1;
  325. std::complex<FloatType> Denom = (mL*Hb + n/XL)*ZetaXL - ZetaXLM1;
  326. return Num/Denom;
  327. }
  328. // ********************************************************************** //
  329. // Calculates S1 - equation (25a) //
  330. // ********************************************************************** //
  331. template <typename FloatType>
  332. std::complex<FloatType> MultiLayerMie<FloatType>::calc_S1(int n, std::complex<FloatType> an, std::complex<FloatType> bn,
  333. FloatType Pi, FloatType Tau) {
  334. return FloatType(n + n + 1)*(Pi*an + Tau*bn)/FloatType(n*n + n);
  335. }
  336. // ********************************************************************** //
  337. // Calculates S2 - equation (25b) (it's the same as (25a), just switches //
  338. // Pi and Tau) //
  339. // ********************************************************************** //
  340. template <typename FloatType>
  341. std::complex<FloatType> MultiLayerMie<FloatType>::calc_S2(int n, std::complex<FloatType> an, std::complex<FloatType> bn,
  342. FloatType Pi, FloatType Tau) {
  343. return calc_S1(n, an, bn, Tau, Pi);
  344. }
  345. //**********************************************************************************//
  346. // This function calculates the logarithmic derivatives of the Riccati-Bessel //
  347. // functions (D1 and D3) for a complex argument (z). //
  348. // Equations (16a), (16b) and (18a) - (18d) //
  349. // //
  350. // Input parameters: //
  351. // z: Complex argument to evaluate D1 and D3 //
  352. // nmax_: Maximum number of terms to calculate D1 and D3 //
  353. // //
  354. // Output parameters: //
  355. // D1, D3: Logarithmic derivatives of the Riccati-Bessel functions //
  356. //**********************************************************************************//
  357. template <typename FloatType>
  358. void MultiLayerMie<FloatType>::calcD1D3(const std::complex<FloatType> z,
  359. std::vector<std::complex<FloatType> > &D1,
  360. std::vector<std::complex<FloatType> > &D3) {
  361. std::vector<std::complex<FloatType> > PsiZeta(nmax_+1);
  362. evalDownwardD1(z, D1);
  363. evalUpwardD3 (z, D1, D3, PsiZeta);
  364. }
  365. //**********************************************************************************//
  366. // This function calculates the Riccati-Bessel functions (Psi and Zeta) for a //
  367. // complex argument (z). //
  368. // Equations (20a) - (21b) //
  369. // //
  370. // Input parameters: //
  371. // z: Complex argument to evaluate Psi and Zeta //
  372. // nmax: Maximum number of terms to calculate Psi and Zeta //
  373. // //
  374. // Output parameters: //
  375. // Psi, Zeta: Riccati-Bessel functions //
  376. //**********************************************************************************//
  377. template <typename FloatType>
  378. void MultiLayerMie<FloatType>::calcPsiZeta(std::complex<FloatType> z,
  379. std::vector<std::complex<FloatType> > &Psi,
  380. std::vector<std::complex<FloatType> > &Zeta) {
  381. std::vector<std::complex<FloatType> > D1(nmax_ + 1), D3(nmax_ + 1),
  382. PsiZeta(nmax_+1);
  383. // First, calculate the logarithmic derivatives
  384. evalDownwardD1(z, D1);
  385. // Now, use the upward recurrence to calculate Psi equations (20ab)
  386. evalUpwardPsi(z, D1, Psi);
  387. // Now, use the upward recurrence to calculate Psi*Zeta equations (18ad)
  388. evalUpwardD3 (z, D1, D3, PsiZeta);
  389. for (unsigned int i = 0; i < Zeta.size(); i++) {
  390. Zeta[i] = PsiZeta[i]/Psi[i];
  391. }
  392. // evalUpwardZeta(z, D3, Zeta);
  393. }
  394. template <typename FloatType>
  395. void MultiLayerMie<FloatType>::calcPiTauAllTheta(const double from_Theta, const double to_Theta,
  396. std::vector<std::vector<FloatType> > &Pi,
  397. std::vector<std::vector<FloatType> > &Tau) {
  398. const unsigned int perimeter_points = Pi.size();
  399. for (auto &val:Pi) val.resize(available_maximal_nmax_);
  400. for (auto &val:Tau) val.resize(available_maximal_nmax_);
  401. double delta_Theta = eval_delta<double>(perimeter_points, from_Theta, to_Theta);
  402. for (unsigned int i=0; i < perimeter_points; i++) {
  403. auto Theta = static_cast<FloatType>(from_Theta + i*delta_Theta);
  404. // Calculate angular functions Pi and Tau
  405. calcPiTau(nmm::cos(Theta), Pi[i], Tau[i]);
  406. }
  407. }
  408. //**********************************************************************************//
  409. // This function calculates Pi and Tau for a given value of cos(Theta). //
  410. // Equations (26a) - (26c) //
  411. // //
  412. // Input parameters: //
  413. // nmax_: Maximum number of terms to calculate Pi and Tau //
  414. // nTheta: Number of scattering angles //
  415. // Theta: Array containing all the scattering angles where the scattering //
  416. // amplitudes will be calculated //
  417. // //
  418. // Output parameters: //
  419. // Pi, Tau: Angular functions Pi and Tau, as defined in equations (26a) - (26c) //
  420. //**********************************************************************************//
  421. template <typename FloatType>
  422. void MultiLayerMie<FloatType>::calcPiTau(const FloatType &costheta,
  423. std::vector<FloatType> &Pi, std::vector<FloatType> &Tau) {
  424. int nmax = Pi.size();
  425. if (Pi.size() != Tau.size())
  426. throw std::invalid_argument("Error! Pi and Tau vectors should have the same size!");
  427. //****************************************************//
  428. // Equations (26a) - (26c) //
  429. //****************************************************//
  430. // Initialize Pi and Tau
  431. Pi[0] = 1.0; // n=1
  432. Tau[0] = costheta;
  433. // Calculate the actual values
  434. if (nmax > 1) {
  435. Pi[1] = 3*costheta*Pi[0]; //n=2
  436. Tau[1] = 2*costheta*Pi[1] - 3*Pi[0];
  437. for (int i = 2; i < nmax; i++) { //n=[3..nmax_]
  438. Pi[i] = ((i + i + 1)*costheta*Pi[i - 1] - (i + 1)*Pi[i - 2])/i;
  439. Tau[i] = (i + 1)*costheta*Pi[i] - (i + 2)*Pi[i - 1];
  440. }
  441. }
  442. } // end of MultiLayerMie::calcPiTau(...)
  443. //**********************************************************************************//
  444. // This function calculates vector spherical harmonics (eq. 4.50, p. 95 BH), //
  445. // required to calculate the near-field parameters. //
  446. // //
  447. // Input parameters: //
  448. // Rho: Radial distance //
  449. // Phi: Azimuthal angle //
  450. // Theta: Polar angle //
  451. // rn: Either the spherical Ricatti-Bessel function of first or third kind //
  452. // Dn: Logarithmic derivative of rn //
  453. // Pi, Tau: Angular functions Pi and Tau //
  454. // n: Order of vector spherical harmonics //
  455. // //
  456. // Output parameters: //
  457. // Mo1n, Me1n, No1n, Ne1n: Complex vector spherical harmonics //
  458. //**********************************************************************************//
  459. template <typename FloatType> template <typename evalType>
  460. void MultiLayerMie<FloatType>::calcSpherHarm(const std::complex<evalType> Rho, const evalType Theta, const evalType Phi,
  461. const std::complex<evalType> &rn, const std::complex<evalType> &Dn,
  462. const evalType &Pi, const evalType &Tau, const evalType &n,
  463. std::vector<std::complex<evalType> > &Mo1n, std::vector<std::complex<evalType> > &Me1n,
  464. std::vector<std::complex<evalType> > &No1n, std::vector<std::complex<evalType> > &Ne1n) {
  465. // using eq 4.50 in BH
  466. std::complex<evalType> c_zero(0.0, 0.0);
  467. // using nmm::sin;
  468. // using nmm::cos;
  469. auto sin_Phi = sin_t(Phi);
  470. auto cos_Phi = cos_t(Phi);
  471. auto sin_Theta = sin(Theta);
  472. Mo1n[0] = c_zero;
  473. Mo1n[1] = cos_Phi*Pi*rn/Rho;
  474. Mo1n[2] = -sin_Phi*Tau*rn/Rho;
  475. Me1n[0] = c_zero;
  476. Me1n[1] = -sin_Phi*Pi*rn/Rho;
  477. Me1n[2] = -cos_Phi*Tau*rn/Rho;
  478. No1n[0] = sin_Phi*(n*n + n)*sin_Theta*Pi*rn/Rho/Rho;
  479. No1n[1] = sin_Phi*Tau*Dn*rn/Rho;
  480. No1n[2] = cos_Phi*Pi*Dn*rn/Rho;
  481. Ne1n[0] = cos_Phi*(n*n + n)*sin_Theta*Pi*rn/Rho/Rho;
  482. Ne1n[1] = cos_Phi*Tau*Dn*rn/Rho;
  483. Ne1n[2] = -sin_Phi*Pi*Dn*rn/Rho;
  484. } // end of MultiLayerMie::calcSpherHarm(...)
  485. //**********************************************************************************//
  486. // This function calculates the scattering coefficients required to calculate //
  487. // both the near- and far-field parameters. //
  488. // //
  489. // Input parameters: //
  490. // L: Number of layers //
  491. // pl: Index of PEC layer. If there is none just send -1 //
  492. // x: Array containing the size parameters of the layers [0..L-1] //
  493. // m: Array containing the relative refractive indexes of the layers [0..L-1] //
  494. // nmax: Maximum number of multipolar expansion terms to be used for the //
  495. // calculations. Only use it if you know what you are doing, otherwise //
  496. // set this parameter to -1 and the function will calculate it. //
  497. // //
  498. // Output parameters: //
  499. // an, bn: Complex scattering amplitudes //
  500. // //
  501. // Return value: //
  502. // Number of multipolar expansion terms used for the calculations //
  503. //**********************************************************************************//
  504. template <typename FloatType>
  505. void MultiLayerMie<FloatType>::calcScattCoeffs() {
  506. isScaCoeffsCalc_ = false;
  507. an_.clear();
  508. bn_.clear();
  509. const std::vector<FloatType> &x = size_param_;
  510. const std::vector<std::complex<FloatType> > &m = refractive_index_;
  511. const int &pl = PEC_layer_position_;
  512. const int L = refractive_index_.size();
  513. //************************************************************************//
  514. // Calculate the index of the first layer. It can be either 0 (default) //
  515. // or the index of the outermost PEC layer. In the latter case all layers //
  516. // below the PEC are discarded. //
  517. // ***********************************************************************//
  518. int fl = (pl > 0) ? pl : 0;
  519. if (nmax_preset_ <= 0) nmax_ = calcNmax();
  520. else nmax_ = nmax_preset_;
  521. std::complex<FloatType> z1, z2;
  522. //**************************************************************************//
  523. // Note that since Fri, Nov 14, 2014 all arrays start from 0 (zero), which //
  524. // means that index = layer number - 1 or index = n - 1. The only exception //
  525. // are the arrays for representing D1, D3 and Q because they need a value //
  526. // for the index 0 (zero), hence it is important to consider this shift //
  527. // between different arrays. The change was done to optimize memory usage. //
  528. //**************************************************************************//
  529. // Allocate memory to the arrays
  530. std::vector<std::complex<FloatType> > D1_mlxl(nmax_ + 1), D1_mlxlM1(nmax_ + 1),
  531. D3_mlxl(nmax_ + 1), D3_mlxlM1(nmax_ + 1);
  532. std::vector<std::vector<std::complex<FloatType> > > Q(L), Ha(L), Hb(L);
  533. for (int l = 0; l < L; l++) {
  534. Q[l].resize(nmax_ + 1);
  535. Ha[l].resize(nmax_);
  536. Hb[l].resize(nmax_);
  537. }
  538. an_.resize(nmax_);
  539. bn_.resize(nmax_);
  540. std::vector<std::complex<FloatType> > PsiXL(nmax_ + 1), ZetaXL(nmax_ + 1);
  541. //*************************************************//
  542. // Calculate D1 and D3 for z1 in the first layer //
  543. //*************************************************//
  544. if (fl == pl) { // PEC layer
  545. for (int n = 0; n <= nmax_; n++) {
  546. D1_mlxl[n] = std::complex<FloatType>(0.0, - 1.0);
  547. D3_mlxl[n] = std::complex<FloatType>(0.0, 1.0);
  548. }
  549. } else { // Regular layer
  550. z1 = x[fl]* m[fl];
  551. // Calculate D1 and D3
  552. calcD1D3(z1, D1_mlxl, D3_mlxl);
  553. }
  554. //******************************************************************//
  555. // Calculate Ha and Hb in the first layer - equations (7a) and (8a) //
  556. //******************************************************************//
  557. for (int n = 0; n < nmax_; n++) {
  558. Ha[fl][n] = D1_mlxl[n + 1];
  559. Hb[fl][n] = D1_mlxl[n + 1];
  560. }
  561. //*****************************************************//
  562. // Iteration from the second layer to the last one (L) //
  563. //*****************************************************//
  564. std::complex<FloatType> Temp, Num, Denom;
  565. std::complex<FloatType> G1, G2;
  566. for (int l = fl + 1; l < L; l++) {
  567. //************************************************************//
  568. //Calculate D1 and D3 for z1 and z2 in the layers fl + 1..L //
  569. //************************************************************//
  570. z1 = x[l]*m[l];
  571. z2 = x[l - 1]*m[l];
  572. //Calculate D1 and D3 for z1
  573. calcD1D3(z1, D1_mlxl, D3_mlxl);
  574. //Calculate D1 and D3 for z2
  575. calcD1D3(z2, D1_mlxlM1, D3_mlxlM1);
  576. //*************************************************//
  577. //Calculate Q, Ha and Hb in the layers fl + 1..L //
  578. //*************************************************//
  579. // Upward recurrence for Q - equations (19a) and (19b)
  580. Num = std::complex<FloatType>(nmm::exp(-2.0*(z1.imag() - z2.imag())), 0.0)
  581. *std::complex<FloatType>(nmm::cos(-2.0*z2.real()) - nmm::exp(-2.0*z2.imag()), nmm::sin(-2.0*z2.real()));
  582. Denom = std::complex<FloatType>(nmm::cos(-2.0*z1.real()) - nmm::exp(-2.0*z1.imag()), nmm::sin(-2.0*z1.real()));
  583. Q[l][0] = Num/Denom;
  584. for (int n = 1; n <= nmax_; n++) {
  585. Num = (z1*D1_mlxl[n] + FloatType(n))*(FloatType(n) - z1*D3_mlxl[n - 1]);
  586. Denom = (z2*D1_mlxlM1[n] + FloatType(n))*(FloatType(n) - z2*D3_mlxlM1[n - 1]);
  587. Q[l][n] = ((pow2(x[l - 1]/x[l])* Q[l][n - 1])*Num)/Denom;
  588. }
  589. // Upward recurrence for Ha and Hb - equations (7b), (8b) and (12) - (15)
  590. for (int n = 1; n <= nmax_; n++) {
  591. //Ha
  592. if ((l - 1) == pl) { // The layer below the current one is a PEC layer
  593. G1 = -D1_mlxlM1[n];
  594. G2 = -D3_mlxlM1[n];
  595. } else {
  596. G1 = (m[l]*Ha[l - 1][n - 1]) - (m[l - 1]*D1_mlxlM1[n]);
  597. G2 = (m[l]*Ha[l - 1][n - 1]) - (m[l - 1]*D3_mlxlM1[n]);
  598. } // end of if PEC
  599. Temp = Q[l][n]*G1;
  600. Num = (G2*D1_mlxl[n]) - (Temp*D3_mlxl[n]);
  601. Denom = G2 - Temp;
  602. Ha[l][n - 1] = Num/Denom;
  603. //Hb
  604. if ((l - 1) == pl) { // The layer below the current one is a PEC layer
  605. G1 = Hb[l - 1][n - 1];
  606. G2 = Hb[l - 1][n - 1];
  607. } else {
  608. G1 = (m[l - 1]*Hb[l - 1][n - 1]) - (m[l]*D1_mlxlM1[n]);
  609. G2 = (m[l - 1]*Hb[l - 1][n - 1]) - (m[l]*D3_mlxlM1[n]);
  610. } // end of if PEC
  611. Temp = Q[l][n]*G1;
  612. Num = (G2*D1_mlxl[n]) - (Temp* D3_mlxl[n]);
  613. Denom = (G2- Temp);
  614. Hb[l][n - 1] = (Num/ Denom);
  615. } // end of for Ha and Hb terms
  616. } // end of for layers iteration
  617. //**************************************//
  618. //Calculate Psi and Zeta for XL //
  619. //**************************************//
  620. // Calculate PsiXL and ZetaXL
  621. calcPsiZeta(std::complex<FloatType>(x[L - 1],0.0), PsiXL, ZetaXL);
  622. //*********************************************************************//
  623. // Finally, we calculate the scattering coefficients (an and bn) and //
  624. // the angular functions (Pi and Tau). Note that for these arrays the //
  625. // first layer is 0 (zero), in future versions all arrays will follow //
  626. // this convention to save memory. (13 Nov, 2014) //
  627. //*********************************************************************//
  628. FloatType a0=0, b0=0;
  629. for (int n = 0; n < nmax_; n++) {
  630. //********************************************************************//
  631. //Expressions for calculating an and bn coefficients are not valid if //
  632. //there is only one PEC layer (ie, for a simple PEC sphere). //
  633. //********************************************************************//
  634. if (pl < (L - 1)) {
  635. an_[n] = calc_an(n + 1, x[L - 1], Ha[L - 1][n], m[L - 1], PsiXL[n + 1], ZetaXL[n + 1], PsiXL[n], ZetaXL[n]);
  636. bn_[n] = calc_bn(n + 1, x[L - 1], Hb[L - 1][n], m[L - 1], PsiXL[n + 1], ZetaXL[n + 1], PsiXL[n], ZetaXL[n]);
  637. } else {
  638. an_[n] = calc_an(n + 1, x[L - 1], std::complex<FloatType>(0.0, 0.0), std::complex<FloatType>(1.0, 0.0), PsiXL[n + 1], ZetaXL[n + 1], PsiXL[n], ZetaXL[n]);
  639. bn_[n] = PsiXL[n + 1]/ZetaXL[n + 1];
  640. }
  641. if (n == 0) {a0 = cabs(an_[0]); b0 = cabs(bn_[0]);}
  642. if (n == nmax_ - 1 && nmax_preset_ <= 0) {
  643. std::cout << "Failed to converge in Mie series for nmax="<<nmax_ << std::endl;
  644. std::cout << "convergence threshold: "<< convergence_threshold_ << std::endl;
  645. std::cout << "Mie series a[nmax]/a[1]:" << cabs(an_[n]) / a0 << " and b[nmax]/b[1]:" << cabs(bn_[n]) / b0 << std::endl;
  646. }
  647. if (cabs(an_[n]) / a0 < convergence_threshold_ &&
  648. cabs(bn_[n]) / b0 < convergence_threshold_) {
  649. if (nmax_preset_ <= 0) nmax_ = n;
  650. break;
  651. }
  652. if (nmm::isnan(an_[n].real()) || nmm::isnan(an_[n].imag()) ||
  653. nmm::isnan(bn_[n].real()) || nmm::isnan(bn_[n].imag())
  654. ) {
  655. std::cout << "nmax value was changed due to unexpected error!!! New values is "<< n
  656. << " (was "<<nmax_<<")"<<std::endl;
  657. nmax_ = n;
  658. break;
  659. }
  660. } // end of for an and bn terms
  661. isScaCoeffsCalc_ = true;
  662. } // end of MultiLayerMie::calcScattCoeffs()
  663. //**********************************************************************************//
  664. // This function calculates the actual scattering parameters and amplitudes //
  665. // //
  666. // Input parameters: //
  667. // L: Number of layers //
  668. // pl: Index of PEC layer. If there is none just send -1 //
  669. // x: Array containing the size parameters of the layers [0..L-1] //
  670. // m: Array containing the relative refractive indexes of the layers [0..L-1] //
  671. // nTheta: Number of scattering angles //
  672. // Theta: Array containing all the scattering angles where the scattering //
  673. // amplitudes will be calculated //
  674. // nmax_: Maximum number of multipolar expansion terms to be used for the //
  675. // calculations. Only use it if you know what you are doing, otherwise //
  676. // set this parameter to -1 and the function will calculate it //
  677. // //
  678. // Output parameters: //
  679. // Qext: Efficiency factor for extinction //
  680. // Qsca: Efficiency factor for scattering //
  681. // Qabs: Efficiency factor for absorption (Qabs = Qext - Qsca) //
  682. // Qbk: Efficiency factor for backscattering //
  683. // Qpr: Efficiency factor for the radiation pressure //
  684. // g: Asymmetry factor (g = (Qext-Qpr)/Qsca) //
  685. // Albedo: Single scattering albedo (Albedo = Qsca/Qext) //
  686. // S1, S2: Complex scattering amplitudes //
  687. // //
  688. // Return value: //
  689. // Number of multipolar expansion terms used for the calculations //
  690. //**********************************************************************************//
  691. template <typename FloatType>
  692. void MultiLayerMie<FloatType>::RunMieCalculation() {
  693. if (size_param_.size() != refractive_index_.size())
  694. throw std::invalid_argument("Each size parameter should have only one index!");
  695. if (size_param_.size() == 0)
  696. throw std::invalid_argument("Initialize model first!");
  697. const std::vector<FloatType> &x = size_param_;
  698. //MarkUncalculated();
  699. // Calculate scattering coefficients
  700. if (!isScaCoeffsCalc_) calcScattCoeffs();
  701. // Initialize the scattering parameters
  702. Qext_ = 0.0;
  703. Qsca_ = 0.0;
  704. Qabs_ = 0.0;
  705. Qbk_ = 0.0;
  706. Qpr_ = 0.0;
  707. asymmetry_factor_ = 0.0;
  708. albedo_ = 0.0;
  709. // Initialize the scattering amplitudes
  710. std::vector<std::complex<FloatType> > tmp1(theta_.size(),std::complex<FloatType>(0.0, 0.0));
  711. S1_.swap(tmp1);
  712. S2_ = S1_;
  713. // Precalculate cos(theta) - gives about 5% speed up.
  714. std::vector<FloatType> costheta(theta_.size(), 0.0);
  715. for (unsigned int t = 0; t < theta_.size(); t++) {
  716. costheta[t] = nmm::cos(theta_[t]);
  717. }
  718. std::vector<FloatType> Pi(nmax_), Tau(nmax_);
  719. std::complex<FloatType> Qbktmp(0.0, 0.0);
  720. std::vector< std::complex<FloatType> > Qbktmp_ch(nmax_ - 1, Qbktmp);
  721. // By using downward recurrence we avoid loss of precision due to float rounding errors
  722. // See: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
  723. // http://en.wikipedia.org/wiki/Loss_of_significance
  724. for (int n = nmax_ - 2; n >= 0; n--) {
  725. // for (int n = 0; n < nmax_; n++) {
  726. const int n1 = n + 1;
  727. if (mode_n_ == Modes::kAll) {
  728. // Equation (27)
  729. Qext_ += (n1 + n1 + 1.0) * (an_[n].real() + bn_[n].real());
  730. // Equation (28)
  731. Qsca_ += (n1 + n1 + 1.0) * (an_[n].real() * an_[n].real() + an_[n].imag() * an_[n].imag()
  732. + bn_[n].real() * bn_[n].real() + bn_[n].imag() * bn_[n].imag());
  733. // std::cout<<"n ="<< n1 << " ext:"<<Qext_ <<" sca:"<<Qsca_<<std::endl;
  734. // Equation (29)
  735. Qpr_ += ((n1 * (n1 + 2.0) / (n1 + 1.0)) * ((an_[n] * std::conj(an_[n1]) + bn_[n] * std::conj(bn_[n1])).real())
  736. + ((n1 + n1 + 1.0) / (n1 * (n1 + 1.0))) * (an_[n] * std::conj(bn_[n])).real());
  737. // Equation (33)
  738. Qbktmp += (FloatType) (n1 + n1 + 1.0) * (1.0 - 2.0 * (n1 % 2)) * (an_[n] - bn_[n]);
  739. // Calculate the scattering amplitudes (S1 and S2) Equations (25a) - (25b)
  740. for (unsigned int t = 0; t < theta_.size(); t++) {
  741. calcPiTau(costheta[t], Pi, Tau);
  742. S1_[t] += calc_S1(n1, an_[n], bn_[n], Pi[n], Tau[n]);
  743. S2_[t] += calc_S2(n1, an_[n], bn_[n], Pi[n], Tau[n]);
  744. }
  745. continue;
  746. }
  747. if (n1 == mode_n_) {
  748. if (mode_type_ == Modes::kElectric || mode_type_ == Modes::kAll) {
  749. Qext_ += (n1 + n1 + 1.0) * (an_[n].real());
  750. Qsca_ += (n1 + n1 + 1.0) * (an_[n].real() * an_[n].real() + an_[n].imag() * an_[n].imag());
  751. Qpr_ += std::nan("");
  752. Qbktmp += (FloatType) (n1 + n1 + 1.0) * (1.0 - 2.0 * (n1 % 2)) * (an_[n]);
  753. for (unsigned int t = 0; t < theta_.size(); t++) {
  754. calcPiTau(costheta[t], Pi, Tau);
  755. S1_[t] += calc_S1(n1, an_[n], static_cast<std::complex<FloatType>>(0), Pi[n], Tau[n]);
  756. S2_[t] += calc_S2(n1, an_[n], static_cast<std::complex<FloatType>>(0), Pi[n], Tau[n]);
  757. }
  758. }
  759. if (mode_type_ == Modes::kMagnetic || mode_type_ == Modes::kAll) {
  760. Qext_ += (n1 + n1 + 1.0) * (bn_[n].real());
  761. Qsca_ += (n1 + n1 + 1.0) * (bn_[n].real() * bn_[n].real() + bn_[n].imag() * bn_[n].imag());
  762. Qpr_ += std::nan("");
  763. Qbktmp += (FloatType) (n1 + n1 + 1.0) * (1.0 - 2.0 * (n1 % 2)) * (bn_[n]);
  764. for (unsigned int t = 0; t < theta_.size(); t++) {
  765. calcPiTau(costheta[t], Pi, Tau);
  766. S1_[t] += calc_S1(n1, static_cast<std::complex<FloatType>>(0), bn_[n], Pi[n], Tau[n]);
  767. S2_[t] += calc_S2(n1, static_cast<std::complex<FloatType>>(0), bn_[n], Pi[n], Tau[n]);
  768. }
  769. }
  770. }
  771. }
  772. FloatType x2 = pow2(x.back());
  773. Qext_ = 2.0*(Qext_)/x2; // Equation (27)
  774. Qsca_ = 2.0*(Qsca_)/x2; // Equation (28)
  775. Qpr_ = Qext_ - 4.0*(Qpr_)/x2; // Equation (29)
  776. Qabs_ = Qext_ - Qsca_; // Equation (30)
  777. albedo_ = Qsca_/Qext_; // Equation (31)
  778. asymmetry_factor_ = (Qext_ - Qpr_)/Qsca_; // Equation (32)
  779. Qbk_ = (Qbktmp.real()*Qbktmp.real() + Qbktmp.imag()*Qbktmp.imag())/x2; // Equation (33)
  780. isMieCalculated_ = true;
  781. }
  782. } // end of namespace nmie
  783. #endif // SRC_NMIE_BASIC_HPP_