nmie-wrapper.cc 46 KB

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