nmie-wrapper.cc 47 KB

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