nmie-basic.hpp 40 KB

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