nmie-wrapper.cc 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. ///
  2. /// @file nmie-wrapper.cc
  3. /// @author Ladutenko Konstantin <kostyfisik at gmail (.) com>
  4. /// @date Tue Sep 3 00:38:27 2013
  5. /// @copyright 2013 Ladutenko Konstantin
  6. ///
  7. /// nmie-wrapper is free software: you can redistribute it and/or modify
  8. /// it under the terms of the GNU General Public License as published by
  9. /// the Free Software Foundation, either version 3 of the License, or
  10. /// (at your option) any later version.
  11. ///
  12. /// nmie-wrapper is distributed in the hope that it will be useful,
  13. /// but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. /// GNU General Public License for more details.
  16. ///
  17. /// You should have received a copy of the GNU General Public License
  18. /// along with nmie-wrapper. If not, see <http://www.gnu.org/licenses/>.
  19. ///
  20. /// nmie-wrapper uses nmie.c from scattnlay by Ovidio Pena
  21. /// <ovidio@bytesfall.com> as a linked library. He has an additional condition to
  22. /// his library:
  23. // The only additional condition is that we expect that all publications //
  24. // describing work using this software , or all commercial products //
  25. // using it, cite the following reference: //
  26. // [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  27. // a multilayered sphere," Computer Physics Communications, //
  28. // vol. 180, Nov. 2009, pp. 2348-2354. //
  29. ///
  30. /// @brief Wrapper class around nMie function for ease of use
  31. ///
  32. #include "nmie-wrapper.h"
  33. //#include "nmie.h"
  34. #include <array>
  35. #include <algorithm>
  36. #include <cstdio>
  37. #include <cstdlib>
  38. #include <stdexcept>
  39. #include <vector>
  40. namespace nmie {
  41. //helpers
  42. template<class T> inline T pow2(const T value) {return value*value;}
  43. #define round(x) ((x) >= 0 ? (int)((x) + 0.5):(int)((x) - 0.5))
  44. // ********************************************************************** //
  45. // ********************************************************************** //
  46. // ********************************************************************** //
  47. //emulate C call.
  48. int nMie_wrapper(int L, std::vector<double> x, std::vector<std::complex<double> > m,
  49. int nTheta, std::vector<double> Theta,
  50. double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo,
  51. std::vector<std::complex<double> >& S1, std::vector<std::complex<double> >& S2) {
  52. if (x.size() != L || m.size() != L)
  53. throw std::invalid_argument("Declared number of layers do not fit x and m!");
  54. if (Theta.size() != nTheta)
  55. throw std::invalid_argument("Declared number of sample for Theta is not correct!");
  56. try {
  57. MultiLayerMie multi_layer_mie;
  58. multi_layer_mie.SetWidthSP(x);
  59. multi_layer_mie.SetIndexSP(m);
  60. multi_layer_mie.SetAngles(Theta);
  61. multi_layer_mie.RunMieCalculations();
  62. *Qext = multi_layer_mie.GetQext();
  63. *Qsca = multi_layer_mie.GetQsca();
  64. *Qabs = multi_layer_mie.GetQabs();
  65. *Qbk = multi_layer_mie.GetQbk();
  66. *Qpr = multi_layer_mie.GetQpr();
  67. *g = multi_layer_mie.GetAsymmetryFactor();
  68. *Albedo = multi_layer_mie.GetAlbedo();
  69. S1 = multi_layer_mie.GetS1();
  70. S2 = multi_layer_mie.GetS2();
  71. } catch( const std::invalid_argument& ia ) {
  72. // Will catch if multi_layer_mie fails or other errors.
  73. std::cerr << "Invalid argument: " << ia.what() << std::endl;
  74. return -1;
  75. }
  76. return 0;
  77. }
  78. // ********************************************************************** //
  79. // ********************************************************************** //
  80. // ********************************************************************** //
  81. double MultiLayerMie::GetQext() {
  82. if (!isMieCalculated_)
  83. throw std::invalid_argument("You should run calculations before result reques!");
  84. return Qext_;
  85. }
  86. // ********************************************************************** //
  87. // ********************************************************************** //
  88. // ********************************************************************** //
  89. double MultiLayerMie::GetQabs() {
  90. if (!isMieCalculated_)
  91. throw std::invalid_argument("You should run calculations before result reques!");
  92. return Qabs_;
  93. }
  94. // ********************************************************************** //
  95. // ********************************************************************** //
  96. // ********************************************************************** //
  97. double MultiLayerMie::GetQsca() {
  98. if (!isMieCalculated_)
  99. throw std::invalid_argument("You should run calculations before result reques!");
  100. return Qsca_;
  101. }
  102. // ********************************************************************** //
  103. // ********************************************************************** //
  104. // ********************************************************************** //
  105. double MultiLayerMie::GetQbk() {
  106. if (!isMieCalculated_)
  107. throw std::invalid_argument("You should run calculations before result reques!");
  108. return Qbk_;
  109. }
  110. // ********************************************************************** //
  111. // ********************************************************************** //
  112. // ********************************************************************** //
  113. double MultiLayerMie::GetQpr() {
  114. if (!isMieCalculated_)
  115. throw std::invalid_argument("You should run calculations before result reques!");
  116. return Qpr_;
  117. }
  118. // ********************************************************************** //
  119. // ********************************************************************** //
  120. // ********************************************************************** //
  121. double MultiLayerMie::GetAsymmetryFactor() {
  122. if (!isMieCalculated_)
  123. throw std::invalid_argument("You should run calculations before result reques!");
  124. return asymmetry_factor_;
  125. }
  126. // ********************************************************************** //
  127. // ********************************************************************** //
  128. // ********************************************************************** //
  129. double MultiLayerMie::GetAlbedo() {
  130. if (!isMieCalculated_)
  131. throw std::invalid_argument("You should run calculations before result reques!");
  132. return albedo_;
  133. }
  134. // ********************************************************************** //
  135. // ********************************************************************** //
  136. // ********************************************************************** //
  137. std::vector<std::complex<double> > MultiLayerMie::GetS1() {
  138. return S1_;
  139. }
  140. // ********************************************************************** //
  141. // ********************************************************************** //
  142. // ********************************************************************** //
  143. std::vector<std::complex<double> > MultiLayerMie::GetS2() {
  144. return S2_;
  145. }
  146. // ********************************************************************** //
  147. // ********************************************************************** //
  148. // ********************************************************************** //
  149. void MultiLayerMie::SetAngles(std::vector<double> angles) {
  150. isMieCalculated_ = false;
  151. theta_.clear();
  152. for (auto value : angles) theta_.push_back(value);
  153. } // end of SetAngles()
  154. // ********************************************************************** //
  155. // ********************************************************************** //
  156. // ********************************************************************** //
  157. void MultiLayerMie::SetWidthSP(std::vector<double> size_parameter) {
  158. isMieCalculated_ = false;
  159. size_parameter_.clear();
  160. double prev_size_parameter = 0.0;
  161. for (auto layer_size_parameter : size_parameter) {
  162. if (layer_size_parameter <= 0.0)
  163. throw std::invalid_argument("Size parameter should be positive!");
  164. if (prev_size_parameter > layer_size_parameter)
  165. throw std::invalid_argument
  166. ("Size parameter for next layer should be larger than the previous one!");
  167. prev_size_parameter = layer_size_parameter;
  168. size_parameter_.push_back(layer_size_parameter);
  169. }
  170. }
  171. // end of void MultiLayerMie::SetWidthSP(...);
  172. // ********************************************************************** //
  173. // ********************************************************************** //
  174. // ********************************************************************** //
  175. void MultiLayerMie::SetIndexSP(std::vector< std::complex<double> > index) {
  176. isMieCalculated_ = false;
  177. index_.clear();
  178. for (auto value : index) index_.push_back(value);
  179. } // end of void MultiLayerMie::SetIndexSP(...);
  180. // ********************************************************************** //
  181. // ********************************************************************** //
  182. // ********************************************************************** //
  183. void MultiLayerMie::SetPEC(int layer_position) {
  184. if (layer_position < 0)
  185. throw std::invalid_argument("Error! Layers are numbered from 0!");
  186. PEC_layer_position_ = layer_position;
  187. }
  188. // ********************************************************************** //
  189. // ********************************************************************** //
  190. // ********************************************************************** //
  191. void MultiLayerMie::SetMaxTermsNumber(int nmax) {
  192. nmax_ = nmax;
  193. }
  194. // ********************************************************************** //
  195. // ********************************************************************** //
  196. // ********************************************************************** //
  197. void MultiLayerMie::GenerateSizeParameter() {
  198. size_parameter_.clear();
  199. double radius = 0.0;
  200. for (auto width : target_width_) {
  201. radius += width;
  202. size_parameter_.push_back(2*PI*radius / wavelength_);
  203. }
  204. for (auto width : coating_width_) {
  205. radius += width;
  206. size_parameter_.push_back(2*PI*radius / wavelength_);
  207. }
  208. total_radius_ = radius;
  209. } // end of void MultiLayerMie::GenerateSizeParameter();
  210. // ********************************************************************** //
  211. // ********************************************************************** //
  212. // ********************************************************************** //
  213. double MultiLayerMie::GetTotalRadius() {
  214. if (total_radius_ == 0) GenerateSizeParameter();
  215. return total_radius_;
  216. } // end of double MultiLayerMie::GetTotalRadius();
  217. // ********************************************************************** //
  218. // ********************************************************************** //
  219. // ********************************************************************** //
  220. std::vector< std::array<double,5> >
  221. MultiLayerMie::GetSpectra(double from_WL, double to_WL, int samples) {
  222. std::vector< std::array<double,5> > spectra;
  223. double step_WL = (to_WL - from_WL)/ static_cast<double>(samples);
  224. double wavelength_backup = wavelength_;
  225. long fails = 0;
  226. for (double WL = from_WL; WL < to_WL; WL += step_WL) {
  227. double Qext, Qsca, Qabs, Qbk;
  228. wavelength_ = WL;
  229. try {
  230. RunMieCalculations();
  231. } catch( const std::invalid_argument& ia ) {
  232. fails++;
  233. continue;
  234. }
  235. //printf("%3.1f ",WL);
  236. spectra.push_back({wavelength_, Qext, Qsca, Qabs, Qbk});
  237. } // end of for each WL in spectra
  238. printf("fails %li\n",fails);
  239. wavelength_ = wavelength_backup;
  240. return spectra;
  241. }
  242. // ********************************************************************** //
  243. // ********************************************************************** //
  244. // ********************************************************************** //
  245. // Calculate Nstop - equation (17)
  246. //
  247. void MultiLayerMie::Nstop() {
  248. const double& xL = size_parameter_.back();
  249. if (xL <= 8) {
  250. nmax_ = round(xL + 4*pow(xL, 1/3) + 1);
  251. } else if (xL <= 4200) {
  252. nmax_ = round(xL + 4.05*pow(xL, 1/3) + 2);
  253. } else {
  254. nmax_ = round(xL + 4*pow(xL, 1/3) + 2);
  255. }
  256. }
  257. // ********************************************************************** //
  258. // ********************************************************************** //
  259. // ********************************************************************** //
  260. void MultiLayerMie::Nmax(int first_layer) {
  261. int ri, riM1;
  262. const std::vector<double>& x = size_parameter_;
  263. const std::vector<std::complex<double> >& m = index_;
  264. const int& pl = PEC_layer_position_;
  265. Nstop(); // Set initial nmax_ value
  266. for (int i = first_layer; i < x.size(); i++) {
  267. if (i > PEC_layer_position_)
  268. ri = round(std::abs(x[i]*m[i]));
  269. else
  270. ri = 0;
  271. nmax_ = std::max(nmax_, ri);
  272. // first layer is pec, if pec is present
  273. if ((i > first_layer) && ((i - 1) > PEC_layer_position_))
  274. riM1 = round(std::abs(x[i - 1]* m[i]));
  275. else
  276. riM1 = 0;
  277. nmax_ = std::max(nmax_, riM1);
  278. }
  279. nmax_ += 15; // Final nmax_ value
  280. }
  281. //**********************************************************************************//
  282. // This function calculates the spherical Bessel (jn) and Hankel (h1n) functions //
  283. // and their derivatives for a given complex value z. See pag. 87 B&H. //
  284. // //
  285. // Input parameters: //
  286. // z: Real argument to evaluate jn and h1n //
  287. // nmax_: Maximum number of terms to calculate jn and h1n //
  288. // //
  289. // Output parameters: //
  290. // jn, h1n: Spherical Bessel and Hankel functions //
  291. // jnp, h1np: Derivatives of the spherical Bessel and Hankel functions //
  292. // //
  293. // The implementation follows the algorithm by I.J. Thompson and A.R. Barnett, //
  294. // Comp. Phys. Comm. 47 (1987) 245-257. //
  295. // //
  296. // Complex spherical Bessel functions from n=0..nmax_-1 for z in the upper half //
  297. // plane (Im(z) > -3). //
  298. // //
  299. // j[n] = j/n(z) Regular solution: j[0]=sin(z)/z //
  300. // j'[n] = d[j/n(z)]/dz //
  301. // h1[n] = h[0]/n(z) Irregular Hankel function: //
  302. // h1'[n] = d[h[0]/n(z)]/dz h1[0] = j0(z) + i*y0(z) //
  303. // = (sin(z)-i*cos(z))/z //
  304. // = -i*exp(i*z)/z //
  305. // Using complex CF1, and trigonometric forms for n=0 solutions. //
  306. //**********************************************************************************//
  307. void MultiLayerMie::sbesjh(std::complex<double> z,
  308. std::vector<std::complex<double> >& jn,
  309. std::vector<std::complex<double> >& jnp,
  310. std::vector<std::complex<double> >& h1n,
  311. std::vector<std::complex<double> >& h1np) {
  312. const int limit = 20000;
  313. const double accur = 1.0e-12;
  314. const double tm30 = 1e-30;
  315. double absc;
  316. std::complex<double> zi, w;
  317. std::complex<double> pl, f, b, d, c, del, jn0, jndb, h1nldb, h1nbdb;
  318. absc = std::abs(std::real(z)) + std::abs(std::imag(z));
  319. if ((absc < accur) || (std::imag(z) < -3.0)) {
  320. throw std::invalid_argument("TODO add error description for condition if ((absc < accur) || (std::imag(z) < -3.0))");
  321. }
  322. zi = 1.0/z;
  323. w = zi + zi;
  324. pl = double(nmax_)*zi;
  325. f = pl + zi;
  326. b = f + f + zi;
  327. d = 0.0;
  328. c = f;
  329. for (int n = 0; n < limit; n++) {
  330. d = b - d;
  331. c = b - 1.0/c;
  332. absc = std::abs(std::real(d)) + std::abs(std::imag(d));
  333. if (absc < tm30) {
  334. d = tm30;
  335. }
  336. absc = std::abs(std::real(c)) + std::abs(std::imag(c));
  337. if (absc < tm30) {
  338. c = tm30;
  339. }
  340. d = 1.0/d;
  341. del = d*c;
  342. f = f*del;
  343. b += w;
  344. absc = std::abs(std::real(del - 1.0)) + std::abs(std::imag(del - 1.0));
  345. if (absc < accur) {
  346. // We have obtained the desired accuracy
  347. break;
  348. }
  349. }
  350. if (absc > accur) {
  351. throw std::invalid_argument("We were not able to obtain the desired accuracy");
  352. }
  353. jn[nmax_ - 1] = tm30;
  354. jnp[nmax_ - 1] = f*jn[nmax_ - 1];
  355. // Downward recursion to n=0 (N.B. Coulomb Functions)
  356. for (int n = nmax_ - 2; n >= 0; n--) {
  357. jn[n] = pl*jn[n + 1] + jnp[n + 1];
  358. jnp[n] = pl*jn[n] - jn[n + 1];
  359. pl = pl - zi;
  360. }
  361. // Calculate the n=0 Bessel Functions
  362. jn0 = zi*std::sin(z);
  363. h1n[0] = std::exp(std::complex<double>(0.0, 1.0)*z)*zi*(-std::complex<double>(0.0, 1.0));
  364. h1np[0] = h1n[0]*(std::complex<double>(0.0, 1.0) - zi);
  365. // Rescale j[n], j'[n], converting to spherical Bessel functions.
  366. // Recur h1[n], h1'[n] as spherical Bessel functions.
  367. w = 1.0/jn[0];
  368. pl = zi;
  369. for (int n = 0; n < nmax_; n++) {
  370. jn[n] = jn0*(w*jn[n]);
  371. jnp[n] = jn0*(w*jnp[n]) - zi*jn[n];
  372. if (n != 0) {
  373. h1n[n] = (pl - zi)*h1n[n - 1] - h1np[n - 1];
  374. // check if hankel is increasing (upward stable)
  375. if (std::abs(h1n[n]) < std::abs(h1n[n - 1])) {
  376. jndb = z;
  377. h1nldb = h1n[n];
  378. h1nbdb = h1n[n - 1];
  379. }
  380. pl += zi;
  381. h1np[n] = -(pl*h1n[n]) + h1n[n - 1];
  382. }
  383. }
  384. }
  385. //**********************************************************************************//
  386. // This function calculates the spherical Bessel functions (bj and by) and the //
  387. // logarithmic derivative (bd) for a given complex value z. See pag. 87 B&H. //
  388. // //
  389. // Input parameters: //
  390. // z: Complex argument to evaluate bj, by and bd //
  391. // nmax_: Maximum number of terms to calculate bj, by and bd //
  392. // //
  393. // Output parameters: //
  394. // bj, by: Spherical Bessel functions //
  395. // bd: Logarithmic derivative //
  396. //**********************************************************************************//
  397. void MultiLayerMie::sphericalBessel(std::complex<double> z,
  398. std::vector<std::complex<double> >& bj,
  399. std::vector<std::complex<double> >& by,
  400. std::vector<std::complex<double> >& bd) {
  401. std::vector<std::complex<double> > jn(nmax_), jnp(nmax_), h1n(nmax_), h1np(nmax_);
  402. sbesjh(z, jn, jnp, h1n, h1np);
  403. for (int n = 0; n < nmax_; n++) {
  404. bj[n] = jn[n];
  405. by[n] = (h1n[n] - jn[n])/std::complex<double>(0.0, 1.0);
  406. bd[n] = jnp[n]/jn[n] + 1.0/z;
  407. }
  408. }
  409. // ********************************************************************** //
  410. // ********************************************************************** //
  411. // ********************************************************************** //
  412. // Calculate an - equation (5)
  413. std::complex<double> MultiLayerMie::calc_an(int n, double XL, std::complex<double> Ha, std::complex<double> mL,
  414. std::complex<double> PsiXL, std::complex<double> ZetaXL,
  415. std::complex<double> PsiXLM1, std::complex<double> ZetaXLM1) {
  416. std::complex<double> Num = (Ha/mL + n/XL)*PsiXL - PsiXLM1;
  417. std::complex<double> Denom = (Ha/mL + n/XL)*ZetaXL - ZetaXLM1;
  418. return Num/Denom;
  419. }
  420. // ********************************************************************** //
  421. // ********************************************************************** //
  422. // ********************************************************************** //
  423. // Calculate bn - equation (6)
  424. std::complex<double> MultiLayerMie::calc_bn(int n, double XL, std::complex<double> Hb, std::complex<double> mL,
  425. std::complex<double> PsiXL, std::complex<double> ZetaXL,
  426. std::complex<double> PsiXLM1, std::complex<double> ZetaXLM1) {
  427. std::complex<double> Num = (mL*Hb + n/XL)*PsiXL - PsiXLM1;
  428. std::complex<double> Denom = (mL*Hb + n/XL)*ZetaXL - ZetaXLM1;
  429. return Num/Denom;
  430. }
  431. // ********************************************************************** //
  432. // ********************************************************************** //
  433. // ********************************************************************** //
  434. // Calculates S1 - equation (25a)
  435. std::complex<double> MultiLayerMie::calc_S1(int n, std::complex<double> an, std::complex<double> bn,
  436. double Pi, double Tau) {
  437. return double(n + n + 1)*(Pi*an + Tau*bn)/double(n*n + n);
  438. }
  439. // ********************************************************************** //
  440. // ********************************************************************** //
  441. // ********************************************************************** //
  442. // Calculates S2 - equation (25b) (it's the same as (25a), just switches Pi and Tau)
  443. std::complex<double> MultiLayerMie::calc_S2(int n, std::complex<double> an, std::complex<double> bn,
  444. double Pi, double Tau) {
  445. return calc_S1(n, an, bn, Tau, Pi);
  446. }
  447. //**********************************************************************************//
  448. // This function calculates the Riccati-Bessel functions (Psi and Zeta) for a //
  449. // real argument (x). //
  450. // Equations (20a) - (21b) //
  451. // //
  452. // Input parameters: //
  453. // x: Real argument to evaluate Psi and Zeta //
  454. // nmax: Maximum number of terms to calculate Psi and Zeta //
  455. // //
  456. // Output parameters: //
  457. // Psi, Zeta: Riccati-Bessel functions //
  458. //**********************************************************************************//
  459. void MultiLayerMie::calcPsiZeta(double x,
  460. std::vector<std::complex<double> > D1,
  461. std::vector<std::complex<double> > D3,
  462. std::vector<std::complex<double> >& Psi,
  463. std::vector<std::complex<double> >& Zeta) {
  464. //Upward recurrence for Psi and Zeta - equations (20a) - (21b)
  465. Psi[0] = std::complex<double>(sin(x), 0);
  466. Zeta[0] = std::complex<double>(sin(x), -cos(x));
  467. for (int n = 1; n <= nmax_; n++) {
  468. Psi[n] = Psi[n - 1]*(n/x - D1[n - 1]);
  469. Zeta[n] = Zeta[n - 1]*(n/x - D3[n - 1]);
  470. }
  471. }
  472. //**********************************************************************************//
  473. // This function calculates the logarithmic derivatives of the Riccati-Bessel //
  474. // functions (D1 and D3) for a complex argument (z). //
  475. // Equations (16a), (16b) and (18a) - (18d) //
  476. // //
  477. // Input parameters: //
  478. // z: Complex argument to evaluate D1 and D3 //
  479. // nmax_: Maximum number of terms to calculate D1 and D3 //
  480. // //
  481. // Output parameters: //
  482. // D1, D3: Logarithmic derivatives of the Riccati-Bessel functions //
  483. //**********************************************************************************//
  484. void MultiLayerMie::calcD1D3(std::complex<double> z,
  485. std::vector<std::complex<double> >& D1,
  486. std::vector<std::complex<double> >& D3) {
  487. std::vector<std::complex<double> > PsiZeta;
  488. PsiZeta.resize(nmax_ + 1);
  489. // Downward recurrence for D1 - equations (16a) and (16b)
  490. D1[nmax_] = std::complex<double>(0.0, 0.0);
  491. for (int n = nmax_; n > 0; n--) {
  492. D1[n - 1] = double(n)/z - 1.0/(D1[n] + double(n)/z);
  493. }
  494. // Upward recurrence for PsiZeta and D3 - equations (18a) - (18d)
  495. PsiZeta[0] = 0.5*(1.0 - std::complex<double>(cos(2.0*z.real()), sin(2.0*z.real()))*exp(-2.0*z.imag()));
  496. D3[0] = std::complex<double>(0.0, 1.0);
  497. for (int n = 1; n <= nmax_; n++) {
  498. PsiZeta[n] = PsiZeta[n - 1]*(double(n)/z - D1[n - 1])*(double(n)/z- D3[n - 1]);
  499. D3[n] = D1[n] + std::complex<double>(0.0, 1.0)/PsiZeta[n];
  500. }
  501. }
  502. //**********************************************************************************//
  503. // This function calculates Pi and Tau for all values of Theta. //
  504. // Equations (26a) - (26c) //
  505. // //
  506. // Input parameters: //
  507. // nmax_: Maximum number of terms to calculate Pi and Tau //
  508. // nTheta: Number of scattering angles //
  509. // Theta: Array containing all the scattering angles where the scattering //
  510. // amplitudes will be calculated //
  511. // //
  512. // Output parameters: //
  513. // Pi, Tau: Angular functions Pi and Tau, as defined in equations (26a) - (26c) //
  514. //**********************************************************************************//
  515. void MultiLayerMie::calcPiTau(std::vector< std::vector<double> >& Pi,
  516. std::vector< std::vector<double> >& Tau) {
  517. //****************************************************//
  518. // Equations (26a) - (26c) //
  519. //****************************************************//
  520. // // // Assume nmax_ > 0;
  521. // // int n = 0;
  522. // // double Pin, Pinm1, Pinm2;
  523. // // Pi[n] = 1.0;
  524. // // Pinm2 = 1.0;
  525. // // Tau[n] = (n + 1)*cos(Theta);
  526. // // if (nmax_ == 1) return;
  527. // // n = 1;
  528. // // Pinm1 = (n + n + 1)*cos(Theta)*Pi[n - 1]/n;
  529. // // Pi[n] = Pinm1;
  530. // // Tau[n] = (n + 1)*cos(Theta)*Pi[n] - (n + 2)*Pi[n - 1];
  531. // // if (nmax_ == 2) return;
  532. // // double nf = 2.0, cosTheta = std::cos(Theta);
  533. // // for (n = 2; n < nmax_; n++) {
  534. // // // Calculate the actual values
  535. // // //Pin = ((nf + nf + 1.0)*cosTheta*Pinm1 - (nf + 1)*Pinm2)/nf;
  536. // // Pi[n] = ((nf + nf + 1.0)*std::cos(Theta)*Pi[n - 1] - (nf + 1)*Pi[n - 2])/nf;
  537. // // // Pi[n] = Pin;
  538. // // Tau[n] = (nf + 1.0)*cosTheta*Pin - (nf + 2)*Pinm1;
  539. // // // Tau[n] = (nf + 1.0)*cos(Theta)*Pi[n] - (nf + 2)*Pi[n - 1];
  540. // // nf+=1.0;
  541. // // // Pinm2 = Pinm1;
  542. // // // Pinm1 = Pin;
  543. // // }
  544. // Unoptimized
  545. for (int n = 0; n < nmax_; n++) {
  546. for (int t = 0; t < theta_.size(); t++) {
  547. if (n == 0) {
  548. // Initialize Pi and Tau
  549. Pi[n][t] = 1.0;
  550. Tau[n][t] = (n + 1)*cos(theta_[t]);
  551. } else {
  552. // Calculate the actual values
  553. Pi[n][t] = ((n == 1) ? ((n + n + 1)*cos(theta_[t])*Pi[n - 1][t]/n)
  554. : (((n + n + 1)*cos(theta_[t])*Pi[n - 1][t]
  555. - (n + 1)*Pi[n - 2][t])/n));
  556. Tau[n][t] = (n + 1)*cos(theta_[t])*Pi[n][t] - (n + 2)*Pi[n - 1][t];
  557. }
  558. }
  559. }
  560. }
  561. //**********************************************************************************//
  562. // This function calculates the scattering coefficients required to calculate //
  563. // both the near- and far-field parameters. //
  564. // //
  565. // Input parameters: //
  566. // L: Number of layers //
  567. // pl: Index of PEC layer. If there is none just send -1 //
  568. // x: Array containing the size parameters of the layers [0..L-1] //
  569. // m: Array containing the relative refractive indexes of the layers [0..L-1] //
  570. // nmax: Maximum number of multipolar expansion terms to be used for the //
  571. // calculations. Only use it if you know what you are doing, otherwise //
  572. // set this parameter to -1 and the function will calculate it. //
  573. // //
  574. // Output parameters: //
  575. // an, bn: Complex scattering amplitudes //
  576. // //
  577. // Return value: //
  578. // Number of multipolar expansion terms used for the calculations //
  579. //**********************************************************************************//
  580. void MultiLayerMie::ScattCoeffs(std::vector<std::complex<double> >& an,
  581. std::vector<std::complex<double> >& bn) {
  582. //************************************************************************//
  583. // Calculate the index of the first layer. It can be either 0 (default) //
  584. // or the index of the outermost PEC layer. In the latter case all layers //
  585. // below the PEC are discarded. //
  586. //************************************************************************//
  587. const std::vector<double>& x = size_parameter_;
  588. const std::vector<std::complex<double> >& m = index_;
  589. const int& pl = PEC_layer_position_;
  590. const int L = index_.size();
  591. // TODO, is it possible for PEC to have a zero index? If yes than is should be:
  592. // int fl = (pl > -1) ? pl : 0;
  593. int fl = (pl > 0) ? pl : 0;
  594. if (nmax_ <= 0) Nmax(fl);
  595. std::complex<double> z1, z2;
  596. //**************************************************************************//
  597. // Note that since Fri, Nov 14, 2014 all arrays start from 0 (zero), which //
  598. // means that index = layer number - 1 or index = n - 1. The only exception //
  599. // are the arrays for representing D1, D3 and Q because they need a value //
  600. // for the index 0 (zero), hence it is important to consider this shift //
  601. // between different arrays. The change was done to optimize memory usage. //
  602. //**************************************************************************//
  603. // Allocate memory to the arrays
  604. std::vector<std::vector<std::complex<double> > > D1_mlxl(L), D1_mlxlM1(L),
  605. D3_mlxl(L), D3_mlxlM1(L), Q(L), Ha(L), Hb(L);
  606. for (int l = 0; l < L; l++) {
  607. D1_mlxl[l].resize(nmax_ + 1);
  608. D1_mlxlM1[l].resize(nmax_ + 1);
  609. D3_mlxl[l].resize(nmax_ + 1);
  610. D3_mlxlM1[l].resize(nmax_ + 1);
  611. Q[l].resize(nmax_ + 1);
  612. Ha[l].resize(nmax_);
  613. Hb[l].resize(nmax_);
  614. }
  615. an.resize(nmax_);
  616. bn.resize(nmax_);
  617. std::vector<std::complex<double> > D1XL(nmax_ + 1), D3XL(nmax_ + 1),
  618. PsiXL(nmax_ + 1), ZetaXL(nmax_ + 1);
  619. //*************************************************//
  620. // Calculate D1 and D3 for z1 in the first layer //
  621. //*************************************************//
  622. if (fl == pl) { // PEC layer
  623. for (int n = 0; n <= nmax_; n++) {
  624. D1_mlxl[fl][n] = std::complex<double>(0.0, -1.0);
  625. D3_mlxl[fl][n] = std::complex<double>(0.0, 1.0);
  626. }
  627. } else { // Regular layer
  628. z1 = x[fl]* m[fl];
  629. // Calculate D1 and D3
  630. calcD1D3(z1, D1_mlxl[fl], D3_mlxl[fl]);
  631. }
  632. //******************************************************************//
  633. // Calculate Ha and Hb in the first layer - equations (7a) and (8a) //
  634. //******************************************************************//
  635. for (int n = 0; n < nmax_; n++) {
  636. Ha[fl][n] = D1_mlxl[fl][n + 1];
  637. Hb[fl][n] = D1_mlxl[fl][n + 1];
  638. }
  639. //*****************************************************//
  640. // Iteration from the second layer to the last one (L) //
  641. //*****************************************************//
  642. for (int l = fl + 1; l < L; l++) {
  643. //************************************************************//
  644. //Calculate D1 and D3 for z1 and z2 in the layers fl+1..L //
  645. //************************************************************//
  646. z1 = x[l]*m[l];
  647. z2 = x[l - 1]*m[l];
  648. //Calculate D1 and D3 for z1
  649. calcD1D3(z1, D1_mlxl[l], D3_mlxl[l]);
  650. //Calculate D1 and D3 for z2
  651. calcD1D3(z2, D1_mlxlM1[l], D3_mlxlM1[l]);
  652. //*********************************************//
  653. //Calculate Q, Ha and Hb in the layers fl+1..L //
  654. //*********************************************//
  655. // Upward recurrence for Q - equations (19a) and (19b)
  656. std::complex<double> Num, Denom;
  657. Num = exp(-2.0*(z1.imag() - z2.imag()))
  658. * std::complex<double>(cos(-2.0*z2.real()) - exp(-2.0*z2.imag()), sin(-2.0*z2.real()));
  659. Denom = std::complex<double>(cos(-2.0*z1.real()) - exp(-2.0*z1.imag()), sin(-2.0*z1.real()));
  660. Q[l][0] = Num/Denom;
  661. for (int n = 1; n <= nmax_; n++) {
  662. std::complex<double> Num, Denom;
  663. Num = (z1*D1_mlxl[l][n] + double(n))*(double(n) - z1*D3_mlxl[l][n - 1]);
  664. Denom = (z2*D1_mlxlM1[l][n] + double(n))*(double(n) - z2*D3_mlxlM1[l][n - 1]);
  665. Q[l][n] = (((x[l - 1]*x[l - 1])/(x[l]*x[l])* Q[l][n - 1])*Num)/Denom;
  666. }
  667. // Upward recurrence for Ha and Hb - equations (7b), (8b) and (12) - (15)
  668. for (int n = 1; n <= nmax_; n++) {
  669. std::complex<double> G1, G2;
  670. //Ha
  671. if ((l - 1) == pl) { // The layer below the current one is a PEC layer
  672. G1 = -D1_mlxlM1[l][n];
  673. G2 = -D3_mlxlM1[l][n];
  674. } else {
  675. G1 = (m[l]*Ha[l - 1][n - 1]) - (m[l - 1]*D1_mlxlM1[l][n]);
  676. G2 = (m[l]*Ha[l - 1][n - 1]) - (m[l - 1]*D3_mlxlM1[l][n]);
  677. }
  678. std::complex<double> Temp, Num, Denom;
  679. Temp = Q[l][n]*G1;
  680. Num = (G2*D1_mlxl[l][n]) - (Temp*D3_mlxl[l][n]);
  681. Denom = G2 - Temp;
  682. Ha[l][n - 1] = Num/Denom;
  683. //Hb
  684. if ((l - 1) == pl) { // The layer below the current one is a PEC layer
  685. G1 = Hb[l - 1][n - 1];
  686. G2 = Hb[l - 1][n - 1];
  687. } else {
  688. G1 = (m[l - 1]*Hb[l - 1][n - 1]) - (m[l]*D1_mlxlM1[l][n]);
  689. G2 = (m[l - 1]*Hb[l - 1][n - 1]) - (m[l]*D3_mlxlM1[l][n]);
  690. }
  691. Temp = Q[l][n]*G1;
  692. Num = (G2*D1_mlxl[l][n]) - (Temp* D3_mlxl[l][n]);
  693. Denom = (G2- Temp);
  694. Hb[l][n - 1] = (Num/ Denom);
  695. }
  696. }
  697. //**************************************//
  698. //Calculate D1, D3, Psi and Zeta for XL //
  699. //**************************************//
  700. // Calculate D1XL and D3XL
  701. calcD1D3(x[L - 1], D1XL, D3XL);
  702. // Calculate PsiXL and ZetaXL
  703. calcPsiZeta(x[L - 1], D1XL, D3XL, PsiXL, ZetaXL);
  704. //*********************************************************************//
  705. // Finally, we calculate the scattering coefficients (an and bn) and //
  706. // the angular functions (Pi and Tau). Note that for these arrays the //
  707. // first layer is 0 (zero), in future versions all arrays will follow //
  708. // this convention to save memory. (13 Nov, 2014) //
  709. //*********************************************************************//
  710. for (int n = 0; n < nmax_; n++) {
  711. //********************************************************************//
  712. //Expressions for calculating an and bn coefficients are not valid if //
  713. //there is only one PEC layer (ie, for a simple PEC sphere). //
  714. //********************************************************************//
  715. if (pl < (L - 1)) {
  716. 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]);
  717. 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]);
  718. } else {
  719. an[n] = calc_an(n + 1, x[L - 1], std::complex<double>(0.0, 0.0), std::complex<double>(1.0, 0.0), PsiXL[n + 1], ZetaXL[n + 1], PsiXL[n], ZetaXL[n]);
  720. bn[n] = PsiXL[n + 1]/ZetaXL[n + 1];
  721. }
  722. }
  723. }
  724. // ********************************************************************** //
  725. // ********************************************************************** //
  726. // ********************************************************************** //
  727. void MultiLayerMie::InitMieCalculations() {
  728. // Initialize the scattering parameters
  729. Qext_ = 0;
  730. Qsca_ = 0;
  731. Qabs_ = 0;
  732. Qbk_ = 0;
  733. Qpr_ = 0;
  734. asymmetry_factor_ = 0;
  735. albedo_ = 0;
  736. // Initialize the scattering amplitudes
  737. std::vector<std::complex<double> > tmp1(theta_.size(),std::complex<double>(0.0, 0.0));
  738. S1_.swap(tmp1);
  739. S2_ = S1_;
  740. }
  741. // ********************************************************************** //
  742. // ********************************************************************** //
  743. // ********************************************************************** //
  744. //**********************************************************************************//
  745. // This function calculates the actual scattering parameters and amplitudes //
  746. // //
  747. // Input parameters: //
  748. // L: Number of layers //
  749. // pl: Index of PEC layer. If there is none just send -1 //
  750. // x: Array containing the size parameters of the layers [0..L-1] //
  751. // m: Array containing the relative refractive indexes of the layers [0..L-1] //
  752. // nTheta: Number of scattering angles //
  753. // Theta: Array containing all the scattering angles where the scattering //
  754. // amplitudes will be calculated //
  755. // nmax_: Maximum number of multipolar expansion terms to be used for the //
  756. // calculations. Only use it if you know what you are doing, otherwise //
  757. // set this parameter to -1 and the function will calculate it //
  758. // //
  759. // Output parameters: //
  760. // Qext: Efficiency factor for extinction //
  761. // Qsca: Efficiency factor for scattering //
  762. // Qabs: Efficiency factor for absorption (Qabs = Qext - Qsca) //
  763. // Qbk: Efficiency factor for backscattering //
  764. // Qpr: Efficiency factor for the radiation pressure //
  765. // g: Asymmetry factor (g = (Qext-Qpr)/Qsca) //
  766. // Albedo: Single scattering albedo (Albedo = Qsca/Qext) //
  767. // S1, S2: Complex scattering amplitudes //
  768. // //
  769. // Return value: //
  770. // Number of multipolar expansion terms used for the calculations //
  771. //**********************************************************************************//
  772. void MultiLayerMie::RunMieCalculations() {
  773. if (size_parameter_.size() != index_.size())
  774. throw std::invalid_argument("Each size parameter should have only one index!");
  775. std::vector<std::complex<double> > an, bn;
  776. std::complex<double> Qbktmp(0.0, 0.0);
  777. const std::vector<double>& x = size_parameter_;
  778. const std::vector<std::complex<double> >& m = index_;
  779. // Calculate scattering coefficients
  780. ScattCoeffs(an, bn);
  781. std::vector< std::vector<double> > Pi(nmax_), Tau(nmax_);
  782. for (int i =0; i< nmax_; ++i) {
  783. Pi[i].resize(theta_.size());
  784. Tau[i].resize(theta_.size());
  785. }
  786. calcPiTau(Pi, Tau);
  787. InitMieCalculations();
  788. // By using downward recurrence we avoid loss of precision due to float rounding errors
  789. // See: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
  790. // http://en.wikipedia.org/wiki/Loss_of_significance
  791. for (int i = nmax_ - 2; i >= 0; i--) {
  792. int n = i + 1;
  793. // Equation (27)
  794. Qext_ += (n + n + 1)*(an[i].real() + bn[i].real());
  795. // Equation (28)
  796. Qsca_ += (n + n + 1)*(an[i].real()*an[i].real() + an[i].imag()*an[i].imag()
  797. + bn[i].real()*bn[i].real() + bn[i].imag()*bn[i].imag());
  798. // Equation (29) TODO We must check carefully this equation. If we
  799. // remove the typecast to double then the result changes. Which is
  800. // the correct one??? Ovidio (2014/12/10) With cast ratio will
  801. // give double, without cast (n + n + 1)/(n*(n + 1)) will be
  802. // rounded to integer. Tig (2015/02/24)
  803. Qpr_ += ((n*(n + 2)/(n + 1))*((an[i]*std::conj(an[n]) + bn[i]*std::conj(bn[n])).real())
  804. + ((double)(n + n + 1)/(n*(n + 1)))*(an[i]*std::conj(bn[i])).real());
  805. // Equation (33)
  806. Qbktmp = Qbktmp + (double)(n + n + 1)*(1 - 2*(n % 2))*(an[i]- bn[i]);
  807. // Calculate the scattering amplitudes (S1 and S2) //
  808. // Equations (25a) - (25b) //
  809. for (int t = 0; t < theta_.size(); t++) {
  810. S1_[t] += calc_S1(n, an[i], bn[i], Pi[i][t], Tau[i][t]);
  811. S2_[t] += calc_S2(n, an[i], bn[i], Pi[i][t], Tau[i][t]);
  812. }
  813. }
  814. double x2 = pow2(x.back());
  815. Qext_ = 2*(Qext_)/x2; // Equation (27)
  816. Qsca_ = 2*(Qsca_)/x2; // Equation (28)
  817. Qpr_ = Qext_ - 4*(Qpr_)/x2; // Equation (29)
  818. Qabs_ = Qext_ - Qsca_; // Equation (30)
  819. albedo_ = Qsca_ / Qext_; // Equation (31)
  820. asymmetry_factor_ = (Qext_ - Qpr_) / Qsca_; // Equation (32)
  821. Qbk_ = (Qbktmp.real()*Qbktmp.real() + Qbktmp.imag()*Qbktmp.imag())/x2; // Equation (33)
  822. isMieCalculated_ = true;
  823. //return nmax;
  824. }
  825. // ********************************************************************** //
  826. // ********************************************************************** //
  827. // ********************************************************************** //
  828. // external scattering field = incident + scattered
  829. // BH p.92 (4.37), 94 (4.45), 95 (4.50)
  830. // assume: medium is non-absorbing; refim = 0; Uabs = 0
  831. void MultiLayerMie::fieldExt(double Rho, double Phi, double Theta, std::vector<double> Pi, std::vector<double> Tau,
  832. std::vector<std::complex<double> > an, std::vector<std::complex<double> > bn,
  833. std::vector<std::complex<double> >& E, std::vector<std::complex<double> >& H) {
  834. double rn = 0.0;
  835. std::complex<double> zn, xxip, encap;
  836. std::vector<std::complex<double> > vm3o1n, vm3e1n, vn3o1n, vn3e1n;
  837. vm3o1n.resize(3);
  838. vm3e1n.resize(3);
  839. vn3o1n.resize(3);
  840. vn3e1n.resize(3);
  841. std::vector<std::complex<double> > Ei, Hi, Es, Hs;
  842. Ei.resize(3);
  843. Hi.resize(3);
  844. Es.resize(3);
  845. Hs.resize(3);
  846. for (int i = 0; i < 3; i++) {
  847. Ei[i] = std::complex<double>(0.0, 0.0);
  848. Hi[i] = std::complex<double>(0.0, 0.0);
  849. Es[i] = std::complex<double>(0.0, 0.0);
  850. Hs[i] = std::complex<double>(0.0, 0.0);
  851. }
  852. std::vector<std::complex<double> > bj, by, bd;
  853. bj.resize(nmax_);
  854. by.resize(nmax_);
  855. bd.resize(nmax_);
  856. // Calculate spherical Bessel and Hankel functions
  857. sphericalBessel(Rho, bj, by, bd);
  858. for (int n = 0; n < nmax_; n++) {
  859. rn = double(n + 1);
  860. zn = bj[n] + std::complex<double>(0.0, 1.0)*by[n];
  861. xxip = Rho*(bj[n] + std::complex<double>(0.0, 1.0)*by[n]) - rn*zn;
  862. vm3o1n[0] = std::complex<double>(0.0, 0.0);
  863. vm3o1n[1] = std::cos(Phi)*Pi[n]*zn;
  864. vm3o1n[2] = -(std::sin(Phi)*Tau[n]*zn);
  865. vm3e1n[0] = std::complex<double>(0.0, 0.0);
  866. vm3e1n[1] = -(std::sin(Phi)*Pi[n]*zn);
  867. vm3e1n[2] = -(std::cos(Phi)*Tau[n]*zn);
  868. vn3o1n[0] = std::sin(Phi)*rn*(rn + 1.0)*std::sin(Theta)*Pi[n]*zn/Rho;
  869. vn3o1n[1] = std::sin(Phi)*Tau[n]*xxip/Rho;
  870. vn3o1n[2] = std::cos(Phi)*Pi[n]*xxip/Rho;
  871. vn3e1n[0] = std::cos(Phi)*rn*(rn + 1.0)*std::sin(Theta)*Pi[n]*zn/Rho;
  872. vn3e1n[1] = std::cos(Phi)*Tau[n]*xxip/Rho;
  873. vn3e1n[2] = -(std::sin(Phi)*Pi[n]*xxip/Rho);
  874. // scattered field: BH p.94 (4.45)
  875. encap = std::pow(std::complex<double>(0.0, 1.0), rn)*(2.0*rn + 1.0)/(rn*(rn + 1.0));
  876. for (int i = 0; i < 3; i++) {
  877. Es[i] = Es[i] + encap*(std::complex<double>(0.0, 1.0)*an[n]*vn3e1n[i] - bn[n]*vm3o1n[i]);
  878. Hs[i] = Hs[i] + encap*(std::complex<double>(0.0, 1.0)*bn[n]*vn3o1n[i] + an[n]*vm3e1n[i]);
  879. }
  880. }
  881. // incident E field: BH p.89 (4.21); cf. p.92 (4.37), p.93 (4.38)
  882. // basis unit vectors = er, etheta, ephi
  883. std::complex<double> eifac = std::exp(std::complex<double>(0.0, 1.0)*Rho*std::cos(Theta));
  884. Ei[0] = eifac*std::sin(Theta)*std::cos(Phi);
  885. Ei[1] = eifac*std::cos(Theta)*std::cos(Phi);
  886. Ei[2] = -(eifac*std::sin(Phi));
  887. // magnetic field
  888. double hffact = 1.0/(cc*mu);
  889. for (int i = 0; i < 3; i++) {
  890. Hs[i] = hffact*Hs[i];
  891. }
  892. // incident H field: BH p.26 (2.43), p.89 (4.21)
  893. std::complex<double> hffacta = hffact;
  894. std::complex<double> hifac = eifac*hffacta;
  895. Hi[0] = hifac*std::sin(Theta)*std::sin(Phi);
  896. Hi[1] = hifac*std::cos(Theta)*std::sin(Phi);
  897. Hi[2] = hifac*std::cos(Phi);
  898. for (int i = 0; i < 3; i++) {
  899. // electric field E [V m-1] = EF*E0
  900. E[i] = Ei[i] + Es[i];
  901. H[i] = Hi[i] + Hs[i];
  902. }
  903. }
  904. // ********************************************************************** //
  905. // ********************************************************************** //
  906. // ********************************************************************** //
  907. //**********************************************************************************//
  908. // This function calculates complex electric and magnetic field in the surroundings //
  909. // and inside (TODO) the particle. //
  910. // //
  911. // Input parameters: //
  912. // L: Number of layers //
  913. // pl: Index of PEC layer. If there is none just send 0 (zero) //
  914. // x: Array containing the size parameters of the layers [0..L-1] //
  915. // m: Array containing the relative refractive indexes of the layers [0..L-1] //
  916. // nmax: Maximum number of multipolar expansion terms to be used for the //
  917. // calculations. Only use it if you know what you are doing, otherwise //
  918. // set this parameter to 0 (zero) and the function will calculate it. //
  919. // ncoord: Number of coordinate points //
  920. // Coords: Array containing all coordinates where the complex electric and //
  921. // magnetic fields will be calculated //
  922. // //
  923. // Output parameters: //
  924. // E, H: Complex electric and magnetic field at the provided coordinates //
  925. // //
  926. // Return value: //
  927. // Number of multipolar expansion terms used for the calculations //
  928. //**********************************************************************************//
  929. // int MultiLayerMie::nField(int L, int pl, std::vector<double> x, std::vector<std::complex<double> > m, int nmax,
  930. // int ncoord, std::vector<double> Xp, std::vector<double> Yp, std::vector<double> Zp,
  931. // std::vector<std::vector<std::complex<double> > >& E, std::vector<std::vector<std::complex<double> > >& H) {
  932. // double Rho, Phi, Theta;
  933. // std::vector<std::complex<double> > an, bn;
  934. // // This array contains the fields in spherical coordinates
  935. // std::vector<std::complex<double> > Es, Hs;
  936. // Es.resize(3);
  937. // Hs.resize(3);
  938. // // Calculate scattering coefficients
  939. // ScattCoeffs(L, pl, an, bn);
  940. // std::vector<double> Pi, Tau;
  941. // Pi.resize(nmax_);
  942. // Tau.resize(nmax_);
  943. // for (int c = 0; c < ncoord; c++) {
  944. // // Convert to spherical coordinates
  945. // Rho = sqrt(Xp[c]*Xp[c] + Yp[c]*Yp[c] + Zp[c]*Zp[c]);
  946. // if (Rho < 1e-3) {
  947. // // Avoid convergence problems
  948. // Rho = 1e-3;
  949. // }
  950. // Phi = acos(Xp[c]/sqrt(Xp[c]*Xp[c] + Yp[c]*Yp[c]));
  951. // Theta = acos(Xp[c]/Rho);
  952. // calcPiTau(Theta, Pi, Tau);
  953. // //*******************************************************//
  954. // // external scattering field = incident + scattered //
  955. // // BH p.92 (4.37), 94 (4.45), 95 (4.50) //
  956. // // assume: medium is non-absorbing; refim = 0; Uabs = 0 //
  957. // //*******************************************************//
  958. // // Firstly the easiest case: the field outside the particle
  959. // if (Rho >= x[L - 1]) {
  960. // fieldExt(Rho, Phi, Theta, Pi, Tau, an, bn, Es, Hs);
  961. // } else {
  962. // // TODO, for now just set all the fields to zero
  963. // for (int i = 0; i < 3; i++) {
  964. // Es[i] = std::complex<double>(0.0, 0.0);
  965. // Hs[i] = std::complex<double>(0.0, 0.0);
  966. // }
  967. // }
  968. // //Now, convert the fields back to cartesian coordinates
  969. // E[c][0] = std::sin(Theta)*std::cos(Phi)*Es[0] + std::cos(Theta)*std::cos(Phi)*Es[1] - std::sin(Phi)*Es[2];
  970. // E[c][1] = std::sin(Theta)*std::sin(Phi)*Es[0] + std::cos(Theta)*std::sin(Phi)*Es[1] + std::cos(Phi)*Es[2];
  971. // E[c][2] = std::cos(Theta)*Es[0] - std::sin(Theta)*Es[1];
  972. // H[c][0] = std::sin(Theta)*std::cos(Phi)*Hs[0] + std::cos(Theta)*std::cos(Phi)*Hs[1] - std::sin(Phi)*Hs[2];
  973. // H[c][1] = std::sin(Theta)*std::sin(Phi)*Hs[0] + std::cos(Theta)*std::sin(Phi)*Hs[1] + std::cos(Phi)*Hs[2];
  974. // H[c][2] = std::cos(Theta)*Hs[0] - std::sin(Theta)*Hs[1];
  975. // }
  976. // return nmax;
  977. // } // end of int nField()
  978. } // end of namespace nmie