nmie-wrapper.cc 46 KB

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