nmie.cc 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. //**********************************************************************************//
  2. // Copyright (C) 2009-2015 Ovidio Pena <ovidio@bytesfall.com> //
  3. // //
  4. // This file is part of scattnlay //
  5. // //
  6. // This program is free software: you can redistribute it and/or modify //
  7. // it under the terms of the GNU General Public License as published by //
  8. // the Free Software Foundation, either version 3 of the License, or //
  9. // (at your option) any later version. //
  10. // //
  11. // This program is distributed in the hope that it will be useful, //
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  14. // GNU General Public License for more details. //
  15. // //
  16. // The only additional remark is that we expect that all publications //
  17. // describing work using this software, or all commercial products //
  18. // using it, cite the following reference: //
  19. // [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  20. // a multilayered sphere," Computer Physics Communications, //
  21. // vol. 180, Nov. 2009, pp. 2348-2354. //
  22. // //
  23. // You should have received a copy of the GNU General Public License //
  24. // along with this program. If not, see <http://www.gnu.org/licenses/>. //
  25. //**********************************************************************************//
  26. //**********************************************************************************//
  27. // This library implements the algorithm for a multilayered sphere described by: //
  28. // [1] W. Yang, "Improved recursive algorithm for light scattering by a //
  29. // multilayered sphere,” Applied Optics, vol. 42, Mar. 2003, pp. 1710-1720. //
  30. // //
  31. // You can find the description of all the used equations in: //
  32. // [2] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  33. // a multilayered sphere," Computer Physics Communications, //
  34. // vol. 180, Nov. 2009, pp. 2348-2354. //
  35. // //
  36. // Hereinafter all equations numbers refer to [2] //
  37. //**********************************************************************************//
  38. #include <math.h>
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41. #include "ucomplex.h"
  42. #include "nmie.h"
  43. // TODO should be replaced with std::round() or std::lround()
  44. #define round(x) ((x) >= 0 ? (int)((x) + 0.5):(int)((x) - 0.5))
  45. complex C_ZERO = {0.0, 0.0};
  46. complex C_ONE = {1.0, 0.0};
  47. complex C_I = {0.0, 1.0};
  48. int compare(std::string operation, complex *a, std::vector< std::complex<double> > b) {
  49. for (int i = 0; i < b.size(); ++i) {
  50. //if (i > 50) continue;
  51. double diff_r = std::abs((a[i].r - b[i].real())/a[i].r);
  52. double diff_i = std::abs((a[i].i - b[i].imag())/a[i].i);
  53. double epsilon= 1e-16;
  54. if (diff_r > epsilon ||diff_i > epsilon) {
  55. printf("\n*** WARNING!! Non-zero diff!!! ***\n");
  56. printf("Op: %s at i=%i, diff_r=%g, diff_i=%g, a_r=%g, a_i = %g\n",
  57. operation.c_str(), i, diff_r, diff_i, a[i].r, a[i].i);
  58. }
  59. double factor = 1.0;
  60. if ((diff_r > epsilon * factor && a[i].r/a[0].r > epsilon)
  61. || (diff_i > epsilon*factor && a[i].i/a[0].i > epsilon)) {
  62. printf("\n******** ERROR!! Non-zero diff!!! ********\n");
  63. printf("Op: %s at i=%i, diff_r=%g, diff_i=%g, a_r=%g, a_i = %g\n",
  64. operation.c_str(), i, diff_r, diff_i, a[i].r, a[i].i);
  65. }
  66. }
  67. return 0;
  68. }
  69. int firstLayer(int L, int pl) {
  70. if (pl >= 0) {
  71. return pl;
  72. } else {
  73. return 0;
  74. }
  75. }
  76. // Calculate Nstop - equation (17)
  77. int Nstop(double xL) {
  78. int result;
  79. if (xL <= 8) {
  80. result = std::lround(xL + 4*pow(xL, 1/3) + 1);
  81. } else if (xL <= 4200) {
  82. result = std::lround(xL + 4.05*pow(xL, 1/3) + 2);
  83. } else {
  84. result = std::lround(xL + 4*pow(xL, 1/3) + 2);
  85. }
  86. return result;
  87. }
  88. int Nmax(int L, int fl, int pl, double x[], complex m[]) {
  89. int i, result, ri, riM1;
  90. result = Nstop(x[L - 1]);
  91. for (i = fl; i < L; i++) {
  92. if (i > pl) {
  93. ri = round(Cabs(RCmul(x[i], m[i])));
  94. } else {
  95. ri = 0;
  96. }
  97. if (result < ri) {
  98. result = ri;
  99. }
  100. if ((i > fl) && ((i - 1) > pl)) {
  101. riM1 = round(Cabs(RCmul(x[i - 1], m[i])));
  102. } else {
  103. riM1 = 0;
  104. }
  105. if (result < riM1) {
  106. result = riM1;
  107. }
  108. }
  109. return result + 15;
  110. }
  111. //**********************************************************************************//
  112. int Nmax_std(int L, int fl, int pl, std::vector<double> x,
  113. std::vector<std::complex<double> > m) {
  114. int i, result, ri, riM1;
  115. result = Nstop(x[L - 1]);
  116. for (i = fl; i < L; i++) {
  117. if (i > pl) {
  118. ri = std::lround(std::abs(x[i]*m[i]));
  119. } else {
  120. ri = 0;
  121. }
  122. if (result < ri) {
  123. result = ri;
  124. }
  125. if ((i > fl) && ((i - 1) > pl)) {
  126. riM1 = std::lround(std::abs(x[i - 1]* m[i]));
  127. } else {
  128. riM1 = 0;
  129. }
  130. if (result < riM1) {
  131. result = riM1;
  132. }
  133. }
  134. return result + 15;
  135. }
  136. //**********************************************************************************//
  137. // This function calculates the spherical Bessel functions (jn and hn) for a given //
  138. // value of r. //
  139. // //
  140. // Input parameters: //
  141. // r: Real argument to evaluate jn and hn //
  142. // n_max: Maximum number of terms to calculate jn and hn //
  143. // //
  144. // Output parameters: //
  145. // jn, hn: Spherical Bessel functions (double) //
  146. //**********************************************************************************//
  147. void sphericalBessel(double r, int n_max, double **j, double **h) {
  148. int n;
  149. (*j)[0] = sin(r)/r;
  150. (*j)[1] = sin(r)/r/r - cos(r)/r;
  151. (*h)[0] = -cos(r)/r;
  152. (*h)[1] = -cos(r)/r/r - sin(r)/r;
  153. for (n = 2; n < n_max; n++) {
  154. (*j)[n] = (n + n + 1)*(*j)[n - 1]/r - (*j)[n - 2];
  155. (*h)[n] = (n + n + 1)*(*h)[n - 1]/r - (*h)[n - 2];
  156. }
  157. }
  158. //**********************************************************************************//
  159. // This function calculates the spherical Bessel functions (jn and hn) for a given //
  160. // value of z. //
  161. // //
  162. // Input parameters: //
  163. // z: Real argument to evaluate jn and hn //
  164. // n_max: Maximum number of terms to calculate jn and hn //
  165. // //
  166. // Output parameters: //
  167. // jn, hn: Spherical Bessel functions (complex) //
  168. //**********************************************************************************//
  169. void CsphericalBessel(complex z, int n_max, complex **j, complex **h) {
  170. int n;
  171. (*j)[0] = Cdiv(Csin(z), z);
  172. (*j)[1] = Csub(Cdiv(Cdiv(Csin(z), z), z), Cdiv(Ccos(z), z));
  173. (*h)[0] = Csub(C_ZERO, Cdiv(Ccos(z), z));
  174. (*h)[1] = Csub(C_ZERO, Cadd(Cdiv(Cdiv(Ccos(z), z), z), Cdiv(Csin(z), z)));
  175. for (n = 2; n < n_max; n++) {
  176. (*j)[n] = Csub(RCmul(n + n + 1, Cdiv((*j)[n - 1], z)), (*j)[n - 2]);
  177. (*h)[n] = Csub(RCmul(n + n + 1, Cdiv((*h)[n - 1], z)), (*h)[n - 2]);
  178. }
  179. }
  180. // Calculate an - equation (5)
  181. complex calc_an(int n, double XL, complex Ha, complex mL, complex PsiXL, complex ZetaXL, complex PsiXLM1, complex ZetaXLM1) {
  182. complex Num = Csub(Cmul(Cadd(Cdiv(Ha, mL), Complex(n/XL, 0)), PsiXL), PsiXLM1);
  183. complex Denom = Csub(Cmul(Cadd(Cdiv(Ha, mL), Complex(n/XL, 0)), ZetaXL), ZetaXLM1);
  184. return Cdiv(Num, Denom);
  185. }
  186. /////////////////////////////////////////////
  187. std::complex<double>
  188. calc_an_std(int n, double XL, std::complex<double> Ha, std::complex<double> mL,
  189. std::complex<double> PsiXL, std::complex<double> ZetaXL,
  190. std::complex<double> PsiXLM1, std::complex<double> ZetaXLM1) {
  191. std::complex<double> Num = (( (Ha/mL) + std::complex<double>(n/XL, 0) ) * PsiXL) - PsiXLM1;
  192. std::complex<double> Denom = (( (Ha/mL) + std::complex<double>(n/XL, 0) ) * ZetaXL) - ZetaXLM1;
  193. return Num/Denom;
  194. }
  195. // Calculate bn - equation (6)
  196. complex calc_bn(int n, double XL, complex Hb, complex mL, complex PsiXL, complex ZetaXL, complex PsiXLM1, complex ZetaXLM1) {
  197. complex Num = Csub(Cmul(Cadd(Cmul(Hb, mL), Complex(n/XL, 0)), PsiXL), PsiXLM1);
  198. complex Denom = Csub(Cmul(Cadd(Cmul(Hb, mL), Complex(n/XL, 0)), ZetaXL), ZetaXLM1);
  199. return Cdiv(Num, Denom);
  200. }
  201. // Calculate bn - equation (6)
  202. std::complex<double>
  203. calc_bn_std(int n, double XL, std::complex<double> Hb, std::complex<double> mL,
  204. std::complex<double> PsiXL, std::complex<double> ZetaXL,
  205. std::complex<double> PsiXLM1, std::complex<double> ZetaXLM1) {
  206. std::complex<double> Num = (( (Hb*mL) + std::complex<double>(n/XL,0) ) * PsiXL) - PsiXLM1;
  207. std::complex<double> Denom = (( (Hb*mL) + std::complex<double>(n/XL,0) ) * ZetaXL) - ZetaXLM1;
  208. return Num/Denom;
  209. }
  210. // Calculates S1_n - equation (25a)
  211. complex calc_S1_n(int n, complex an, complex bn, double Pin, double Taun) {
  212. return RCmul((double)(n + n + 1)/(double)(n*n + n), Cadd(RCmul(Pin, an), RCmul(Taun, bn)));
  213. }
  214. //////////////////////
  215. std::complex<double> calc_S1_n_std(int n, std::complex<double> an, std::complex<double> bn,
  216. double Pin, double Taun) {
  217. return (double)(n + n + 1)/(double)(n*n + n) * ((Pin*an) + (Taun*bn));
  218. }
  219. // Calculates S2_n - equation (25b) (it's the same as (25a), just switches Pin and Taun)
  220. complex calc_S2_n(int n, complex an, complex bn, double Pin, double Taun) {
  221. return calc_S1_n(n, an, bn, Taun, Pin);
  222. }
  223. //////////////////////
  224. std::complex<double> calc_S2_n_std(int n, std::complex<double> an, std::complex<double> bn,
  225. double Pin, double Taun) {
  226. return calc_S1_n_std(n, an, bn, Taun, Pin);
  227. }
  228. //**********************************************************************************//
  229. // This function calculates the Riccati-Bessel functions (Psi and Zeta) for a //
  230. // given value of z. //
  231. // //
  232. // Input parameters: //
  233. // z: Complex argument to evaluate Psi and Zeta //
  234. // n_max: Maximum number of terms to calculate Psi and Zeta //
  235. // //
  236. // Output parameters: //
  237. // Psi, Zeta: Riccati-Bessel functions //
  238. //**********************************************************************************//
  239. void calcPsiZeta(complex z, int n_max, complex *D1, complex *D3, complex **Psi, complex **Zeta) {
  240. int n;
  241. complex cn;
  242. //Upward recurrence for Psi and Zeta - equations (20a) - (21b)
  243. (*Psi)[0] = Complex(sin(z.r), 0);
  244. (*Zeta)[0] = Complex(sin(z.r), -cos(z.r));
  245. for (n = 1; n <= n_max; n++) {
  246. cn = Complex(n, 0);
  247. (*Psi)[n] = Cmul((*Psi)[n - 1], Csub(Cdiv(cn, z), D1[n - 1]));
  248. (*Zeta)[n] = Cmul((*Zeta)[n - 1], Csub(Cdiv(cn, z), D3[n - 1]));
  249. }
  250. }
  251. //**********************************************************************************//
  252. void calcPsiZeta_std(std::complex<double> z, int n_max,
  253. std::vector< std::complex<double> > D1,
  254. std::vector< std::complex<double> > D3,
  255. std::vector< std::complex<double> > &Psi,
  256. std::vector< std::complex<double> > &Zeta) {
  257. int n;
  258. std::complex<double> cn;
  259. //Upward recurrence for Psi and Zeta - equations (20a) - (21b)
  260. Psi[0] = std::complex<double>(sin(z.real()), 0);
  261. Zeta[0] = std::complex<double>(sin(z.real()), -cos(z.real()));
  262. for (n = 1; n <= n_max; n++) {
  263. cn = std::complex<double>(n, 0);
  264. Psi[n] = Psi[n-1] * ( (cn/z) - D1[n-1] );
  265. Zeta[n] = Zeta[n-1] * ( (cn/z) - D3[n-1] );
  266. }
  267. }
  268. //**********************************************************************************//
  269. // This function calculates the logarithmic derivatives of the Riccati-Bessel //
  270. // functions (D1 and D3) for a given value of z. //
  271. // //
  272. // Input parameters: //
  273. // z: Complex argument to evaluate D1 and D3 //
  274. // n_max: Maximum number of terms to calculate D1 and D3 //
  275. // //
  276. // Output parameters: //
  277. // D1, D3: Logarithmic derivatives of the Riccati-Bessel functions //
  278. //**********************************************************************************//
  279. void calcD1D3(complex z, int n_max, complex **D1, complex **D3) {
  280. int n;
  281. complex cn;
  282. complex *PsiZeta = (complex *) malloc((n_max + 1)*sizeof(complex));
  283. // Downward recurrence for D1 - equations (16a) and (16b)
  284. (*D1)[n_max] = C_ZERO;
  285. for (n = n_max; n > 0; n--) {
  286. cn = Complex(n, 0);
  287. (*D1)[n - 1] = Csub(Cdiv(cn, z), Cdiv(C_ONE, Cadd((*D1)[n], Cdiv(cn, z))));
  288. }
  289. // Upward recurrence for PsiZeta and D3 - equations (18a) - (18d)
  290. PsiZeta[0] = RCmul(0.5, Csub(C_ONE, Cmul(Complex(cos(2*z.r), sin(2*z.r)), Complex(exp(-2*z.i), 0))));
  291. (*D3)[0] = C_I;
  292. for (n = 1; n <= n_max; n++) {
  293. cn = Complex(n, 0);
  294. PsiZeta[n] = Cmul(PsiZeta[n - 1], Cmul(Csub(Cdiv(cn, z), (*D1)[n - 1]), Csub(Cdiv(cn, z), (*D3)[n - 1])));
  295. (*D3)[n] = Cadd((*D1)[n], Cdiv(C_I, PsiZeta[n]));
  296. }
  297. free(PsiZeta);
  298. }
  299. //**********************************************************************************//
  300. void calcD1D3_std(std::complex<double> z, int n_max,
  301. std::vector< std::complex<double> > &D1,
  302. std::vector< std::complex<double> > &D3) {
  303. int n;
  304. std::complex<double> cn;
  305. //complex *PsiZeta = (complex *) malloc((n_max + 1)*sizeof(complex));
  306. std::vector< std::complex<double> > PsiZeta;
  307. PsiZeta.resize(n_max + 1);
  308. // Downward recurrence for D1 - equations (16a) and (16b)
  309. D1[n_max] = std::complex<double>(0.0, 0.0);
  310. for (n = n_max; n > 0; n--) {
  311. cn = std::complex<double>(n, 0.0);
  312. D1[n - 1] = cn/z - 1.0/(D1[n] + (cn/z));
  313. }
  314. // Upward recurrence for PsiZeta and D3 - equations (18a) - (18d)
  315. PsiZeta[0] = 0.5 * (1.0 - (std::complex<double>(cos(2*z.real()), sin(2*z.real()))
  316. * std::complex<double>(exp(-2*z.imag()), 0)));
  317. D3[0] = std::complex<double>(0.0, 1.0);
  318. for (n = 1; n <= n_max; n++) {
  319. cn = std::complex<double>(n, 0.0);
  320. PsiZeta[n] = PsiZeta[n-1] * (((cn/ z) - D1[n - 1]) * ((cn/ z)- D3[n - 1]));
  321. D3[n] = D1[n]+ (std::complex<double>(0.0, 1.0)/ PsiZeta[n]);
  322. }
  323. // free(PsiZeta);
  324. }
  325. //**********************************************************************************//
  326. // This function calculates Pi and Tau for all values of Theta. //
  327. // //
  328. // Input parameters: //
  329. // n_max: Maximum number of terms to calculate Pi and Tau //
  330. // nTheta: Number of scattering angles //
  331. // Theta: Array containing all the scattering angles where the scattering //
  332. // amplitudes will be calculated //
  333. // //
  334. // Output parameters: //
  335. // Pi, Tau: Angular functions Pi and Tau, as defined in equations (26a) - (26c) //
  336. //**********************************************************************************//
  337. void calcPiTau(int n_max, int nTheta, double Theta[], double ***Pi, double ***Tau) {
  338. int n, t;
  339. for (n = 0; n < n_max; n++) {
  340. //****************************************************//
  341. // Equations (26a) - (26c) //
  342. //****************************************************//
  343. for (t = 0; t < nTheta; t++) {
  344. if (n == 0) {
  345. // Initialize Pi and Tau
  346. (*Pi)[n][t] = 1.0;
  347. (*Tau)[n][t] = (n + 1)*cos(Theta[t]);
  348. } else {
  349. // Calculate the actual values
  350. (*Pi)[n][t] = ((n == 1) ? ((n + n + 1)*cos(Theta[t])*(*Pi)[n - 1][t]/n)
  351. : (((n + n + 1)*cos(Theta[t])*(*Pi)[n - 1][t] - (n + 1)*(*Pi)[n - 2][t])/n));
  352. (*Tau)[n][t] = (n + 1)*cos(Theta[t])*(*Pi)[n][t] - (n + 2)*(*Pi)[n - 1][t];
  353. }
  354. }
  355. }
  356. }
  357. //**********************************************************************************//
  358. void calcPiTau_std(int n_max, int nTheta, std::vector<double> Theta,
  359. std::vector< std::vector<double> > &Pi,
  360. std::vector< std::vector<double> > &Tau) {
  361. int n, t;
  362. for (n = 0; n < n_max; n++) {
  363. //****************************************************//
  364. // Equations (26a) - (26c) //
  365. //****************************************************//
  366. for (t = 0; t < nTheta; t++) {
  367. if (n == 0) {
  368. // Initialize Pi and Tau
  369. Pi[n][t] = 1.0;
  370. Tau[n][t] = (n + 1)*cos(Theta[t]);
  371. } else {
  372. // Calculate the actual values
  373. Pi[n][t] = ((n == 1) ? ((n + n + 1)*cos(Theta[t])*Pi[n - 1][t]/n)
  374. :
  375. (((n + n + 1)*cos(Theta[t])*Pi[n - 1][t] - (n + 1)*Pi[n - 2][t])/n));
  376. Tau[n][t] = (n + 1)*cos(Theta[t])*Pi[n][t] - (n + 2)*Pi[n - 1][t];
  377. }
  378. }
  379. }
  380. }
  381. //**********************************************************************************//
  382. // This function calculates the scattering coefficients required to calculate //
  383. // both the near- and far-field parameters. //
  384. // //
  385. // Input parameters: //
  386. // L: Number of layers //
  387. // pl: Index of PEC layer. If there is none just send -1 //
  388. // x: Array containing the size parameters of the layers [0..L-1] //
  389. // m: Array containing the relative refractive indexes of the layers [0..L-1] //
  390. // n_max: Maximum number of multipolar expansion terms to be used for the //
  391. // calculations. Only used if you know what you are doing, otherwise set //
  392. // this parameter to -1 and the function will calculate it. //
  393. // //
  394. // Output parameters: //
  395. // an, bn: Complex scattering amplitudes //
  396. // //
  397. // Return value: //
  398. // Number of multipolar expansion terms used for the calculations //
  399. //**********************************************************************************//
  400. int ScattCoeff(int L, int pl, double x[], complex m[], int n_max, complex **an, complex **bn){
  401. //************************************************************************//
  402. // Calculate the index of the first layer. It can be either 0 (default) //
  403. // or the index of the outermost PEC layer. In the latter case all layers //
  404. // below the PEC are discarded. //
  405. //************************************************************************//
  406. int fl = firstLayer(L, pl);
  407. if (n_max <= 0) {
  408. n_max = Nmax(L, fl, pl, x, m);
  409. }
  410. complex z1, z2, cn;
  411. complex Num, Denom;
  412. complex G1, G2;
  413. complex Temp;
  414. double Tmp;
  415. int n, l, t;
  416. //**************************************************************************//
  417. // Note that since Fri, Nov 14, 2014 all arrays start from 0 (zero), which //
  418. // means that index = layer number - 1 or index = n - 1. The only exception //
  419. // are the arrays for representing D1, D3 and Q because they need a value //
  420. // for the index 0 (zero), hence it is important to consider this shift //
  421. // between different arrays. The change was done to optimize memory usage. //
  422. //**************************************************************************//
  423. // Allocate memory to the arrays
  424. complex **D1_mlxl = (complex **) malloc(L*sizeof(complex *));
  425. complex **D1_mlxlM1 = (complex **) malloc(L*sizeof(complex *));
  426. complex **D3_mlxl = (complex **) malloc(L*sizeof(complex *));
  427. complex **D3_mlxlM1 = (complex **) malloc(L*sizeof(complex *));
  428. complex **Q = (complex **) malloc(L*sizeof(complex *));
  429. complex **Ha = (complex **) malloc(L*sizeof(complex *));
  430. complex **Hb = (complex **) malloc(L*sizeof(complex *));
  431. for (l = 0; l < L; l++) {
  432. D1_mlxl[l] = (complex *) malloc((n_max + 1)*sizeof(complex));
  433. D1_mlxlM1[l] = (complex *) malloc((n_max + 1)*sizeof(complex));
  434. D3_mlxl[l] = (complex *) malloc((n_max + 1)*sizeof(complex));
  435. D3_mlxlM1[l] = (complex *) malloc((n_max + 1)*sizeof(complex));
  436. Q[l] = (complex *) malloc((n_max + 1)*sizeof(complex));
  437. Ha[l] = (complex *) malloc(n_max*sizeof(complex));
  438. Hb[l] = (complex *) malloc(n_max*sizeof(complex));
  439. }
  440. (*an) = (complex *) malloc(n_max*sizeof(complex));
  441. (*bn) = (complex *) malloc(n_max*sizeof(complex));
  442. complex *D1XL = (complex *) malloc((n_max + 1)*sizeof(complex));
  443. complex *D3XL = (complex *) malloc((n_max + 1)*sizeof(complex));
  444. complex *PsiXL = (complex *) malloc((n_max + 1)*sizeof(complex));
  445. complex *ZetaXL = (complex *) malloc((n_max + 1)*sizeof(complex));
  446. //*************************************************//
  447. // Calculate D1 and D3 for z1 in the first layer //
  448. //*************************************************//
  449. if (fl == pl) { // PEC layer
  450. for (n = 0; n <= n_max; n++) {
  451. D1_mlxl[fl][n] = Complex(0, -1);
  452. D3_mlxl[fl][n] = C_I;
  453. }
  454. } else { // Regular layer
  455. z1 = RCmul(x[fl], m[fl]);
  456. // Calculate D1 and D3
  457. calcD1D3(z1, n_max, &(D1_mlxl[fl]), &(D3_mlxl[fl]));
  458. }
  459. //******************************************************************//
  460. // Calculate Ha and Hb in the first layer - equations (7a) and (8a) //
  461. //******************************************************************//
  462. for (n = 0; n < n_max; n++) {
  463. Ha[fl][n] = D1_mlxl[fl][n + 1];
  464. Hb[fl][n] = D1_mlxl[fl][n + 1];
  465. }
  466. //*****************************************************//
  467. // Iteration from the second layer to the last one (L) //
  468. //*****************************************************//
  469. for (l = fl + 1; l < L; l++) {
  470. //************************************************************//
  471. //Calculate D1 and D3 for z1 and z2 in the layers fl+1..L //
  472. //************************************************************//
  473. z1 = RCmul(x[l], m[l]);
  474. z2 = RCmul(x[l - 1], m[l]);
  475. //Calculate D1 and D3 for z1
  476. calcD1D3(z1, n_max, &(D1_mlxl[l]), &(D3_mlxl[l]));
  477. //Calculate D1 and D3 for z2
  478. calcD1D3(z2, n_max, &(D1_mlxlM1[l]), &(D3_mlxlM1[l]));
  479. //*********************************************//
  480. //Calculate Q, Ha and Hb in the layers fl+1..L //
  481. //*********************************************//
  482. // Upward recurrence for Q - equations (19a) and (19b)
  483. Num = RCmul(exp(-2*(z1.i - z2.i)), Complex(cos(-2*z2.r) - exp(-2*z2.i), sin(-2*z2.r)));
  484. Denom = Complex(cos(-2*z1.r) - exp(-2*z1.i), sin(-2*z1.r));
  485. Q[l][0] = Cdiv(Num, Denom);
  486. for (n = 1; n <= n_max; n++) {
  487. cn = Complex(n, 0);
  488. Num = Cmul(Cadd(Cmul(z1, D1_mlxl[l][n]), cn), Csub(cn, Cmul(z1, D3_mlxl[l][n - 1])));
  489. Denom = Cmul(Cadd(Cmul(z2, D1_mlxlM1[l][n]), cn), Csub(cn, Cmul(z2, D3_mlxlM1[l][n - 1])));
  490. Q[l][n] = Cdiv(Cmul(RCmul((x[l - 1]*x[l - 1])/(x[l]*x[l]), Q[l][n - 1]), Num), Denom);
  491. }
  492. // Upward recurrence for Ha and Hb - equations (7b), (8b) and (12) - (15)
  493. for (n = 1; n <= n_max; n++) {
  494. //Ha
  495. if ((l - 1) == pl) { // The layer below the current one is a PEC layer
  496. G1 = RCmul(-1.0, D1_mlxlM1[l][n]);
  497. G2 = RCmul(-1.0, D3_mlxlM1[l][n]);
  498. } else {
  499. G1 = Csub(Cmul(m[l], Ha[l - 1][n - 1]), Cmul(m[l - 1], D1_mlxlM1[l][n]));
  500. G2 = Csub(Cmul(m[l], Ha[l - 1][n - 1]), Cmul(m[l - 1], D3_mlxlM1[l][n]));
  501. }
  502. Temp = Cmul(Q[l][n], G1);
  503. Num = Csub(Cmul(G2, D1_mlxl[l][n]), Cmul(Temp, D3_mlxl[l][n]));
  504. Denom = Csub(G2, Temp);
  505. Ha[l][n - 1] = Cdiv(Num, Denom);
  506. //Hb
  507. if ((l - 1) == pl) { // The layer below the current one is a PEC layer
  508. G1 = Hb[l - 1][n - 1];
  509. G2 = Hb[l - 1][n - 1];
  510. } else {
  511. G1 = Csub(Cmul(m[l - 1], Hb[l - 1][n - 1]), Cmul(m[l], D1_mlxlM1[l][n]));
  512. G2 = Csub(Cmul(m[l - 1], Hb[l - 1][n - 1]), Cmul(m[l], D3_mlxlM1[l][n]));
  513. }
  514. Temp = Cmul(Q[l][n], G1);
  515. Num = Csub(Cmul(G2, D1_mlxl[l][n]), Cmul(Temp, D3_mlxl[l][n]));
  516. Denom = Csub(G2, Temp);
  517. Hb[l][n - 1] = Cdiv(Num, Denom);
  518. }
  519. }
  520. //**************************************//
  521. //Calculate D1, D3, Psi and Zeta for XL //
  522. //**************************************//
  523. z1 = Complex(x[L - 1], 0);
  524. // Calculate D1XL and D3XL
  525. calcD1D3(z1, n_max, &D1XL, &D3XL);
  526. // Calculate PsiXL and ZetaXL
  527. calcPsiZeta(z1, n_max, D1XL, D3XL, &PsiXL, &ZetaXL);
  528. //*********************************************************************//
  529. // Finally, we calculate the scattering coefficients (an and bn) and //
  530. // the angular functions (Pi and Tau). Note that for these arrays the //
  531. // first layer is 0 (zero), in future versions all arrays will follow //
  532. // this convention to save memory. (13 Nov, 2014) //
  533. //*********************************************************************//
  534. for (n = 0; n < n_max; n++) {
  535. //********************************************************************//
  536. //Expressions for calculating an and bn coefficients are not valid if //
  537. //there is only one PEC layer (ie, for a simple PEC sphere). //
  538. //********************************************************************//
  539. if (pl < (L - 1)) {
  540. (*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]);
  541. (*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]);
  542. } else {
  543. (*an)[n] = calc_an(n + 1, x[L - 1], C_ZERO, C_ONE, PsiXL[n + 1], ZetaXL[n + 1], PsiXL[n], ZetaXL[n]);
  544. (*bn)[n] = Cdiv(PsiXL[n + 1], ZetaXL[n + 1]);
  545. }
  546. }
  547. // Free the memory used for the arrays
  548. for (l = 0; l < L; l++) {
  549. free(D1_mlxl[l]);
  550. free(D1_mlxlM1[l]);
  551. free(D3_mlxl[l]);
  552. free(D3_mlxlM1[l]);
  553. free(Q[l]);
  554. free(Ha[l]);
  555. free(Hb[l]);
  556. }
  557. free(D1_mlxl);
  558. free(D1_mlxlM1);
  559. free(D3_mlxl);
  560. free(D3_mlxlM1);
  561. free(Q);
  562. free(Ha);
  563. free(Hb);
  564. free(D1XL);
  565. free(D3XL);
  566. free(PsiXL);
  567. free(ZetaXL);
  568. return n_max;
  569. }
  570. //**********************************************************************************//
  571. //**********************************************************************************//
  572. //**********************************************************************************//
  573. int ScattCoeff_std(double x[], complex m[],
  574. int L, int pl, std::vector<double> x_std,
  575. std::vector<std::complex<double> > m_std, int n_max,
  576. std::vector< std::complex<double> > &an_std,
  577. std::vector< std::complex<double> > &bn_std){
  578. //************************************************************************//
  579. // Calculate the index of the first layer. It can be either 0 (default) //
  580. // or the index of the outermost PEC layer. In the latter case all layers //
  581. // below the PEC are discarded. //
  582. //************************************************************************//
  583. //TODO: Why?
  584. // int fl = firstLayer(L, pl);
  585. // instead of
  586. int fl = (pl > 0) ? pl : 0;
  587. // fl - first layer, pl - pec layer.
  588. if (n_max <= 0) {
  589. int tmp_n_max = Nmax(L, fl, pl, x, m);
  590. n_max = Nmax_std(L, fl, pl, x_std, m_std);
  591. if (n_max != tmp_n_max) printf("n_max mismatch 2\n");
  592. }
  593. // complex z1, z2, cn;
  594. // complex Num, Denom;
  595. // complex G1, G2;
  596. // complex Temp;
  597. std::complex<double> z1, z2, cn;
  598. std::complex<double> Num, Denom;
  599. std::complex<double> G1, G2;
  600. std::complex<double> Temp;
  601. double Tmp;
  602. int n, l, t;
  603. //**************************************************************************//
  604. // Note that since Fri, Nov 14, 2014 all arrays start from 0 (zero), which //
  605. // means that index = layer number - 1 or index = n - 1. The only exception //
  606. // are the arrays for representing D1, D3 and Q because they need a value //
  607. // for the index 0 (zero), hence it is important to consider this shift //
  608. // between different arrays. The change was done to optimize memory usage. //
  609. //**************************************************************************//
  610. // Allocate memory to the arrays
  611. //complex **D1_mlxl = (complex **) malloc(L*sizeof(complex *));
  612. //complex **D1_mlxlM1 = (complex **) malloc(L*sizeof(complex *));
  613. std::vector< std::vector< std::complex<double> > > D1_mlxl;
  614. D1_mlxl.resize(L);
  615. std::vector< std::vector< std::complex<double> > >D1_mlxlM1;
  616. D1_mlxlM1.resize(L);
  617. // complex **D3_mlxl = (complex **) malloc(L*sizeof(complex *));
  618. // complex **D3_mlxlM1 = (complex **) malloc(L*sizeof(complex *));
  619. std::vector< std::vector< std::complex<double> > > D3_mlxl;
  620. D3_mlxl.resize(L);
  621. std::vector< std::vector< std::complex<double> > > D3_mlxlM1;
  622. D3_mlxlM1.resize(L);
  623. //complex **Q = (complex **) malloc(L*sizeof(complex *));
  624. std::vector< std::vector< std::complex<double> > > Q;
  625. Q.resize(L);
  626. //complex **Ha = (complex **) malloc(L*sizeof(complex *));
  627. std::vector< std::vector< std::complex<double> > > Ha;
  628. Ha.resize(L);
  629. //complex **Hb = (complex **) malloc(L*sizeof(complex *));
  630. std::vector< std::vector< std::complex<double> > > Hb;
  631. Hb.resize(L);
  632. for (l = 0; l < L; l++) {
  633. // D1_mlxl[l] = (complex *) malloc((n_max + 1)*sizeof(complex));
  634. // D1_mlxlM1[l] = (complex *) malloc((n_max + 1)*sizeof(complex));
  635. D1_mlxl[l].resize(n_max +1);
  636. D1_mlxlM1[l].resize(n_max +1);
  637. // D3_mlxl[l] = (complex *) malloc((n_max + 1)*sizeof(complex));
  638. // D3_mlxlM1[l] = (complex *) malloc((n_max + 1)*sizeof(complex));
  639. D3_mlxl[l].resize(n_max +1);
  640. D3_mlxlM1[l].resize(n_max +1);
  641. //Q[l] = (complex *) malloc((n_max + 1)*sizeof(complex));
  642. Q[l].resize(n_max + 1);
  643. // Ha[l] = (complex *) malloc(n_max*sizeof(complex));
  644. // Hb[l] = (complex *) malloc(n_max*sizeof(complex));
  645. Ha[l].resize(n_max);
  646. Hb[l].resize(n_max);
  647. }
  648. // (*an) = (complex *) malloc(n_max*sizeof(complex));
  649. // (*bn) = (complex *) malloc(n_max*sizeof(complex));
  650. an_std.resize(n_max);
  651. bn_std.resize(n_max);
  652. // complex *D1XL = (complex *) malloc((n_max + 1)*sizeof(complex));
  653. // complex *D3XL = (complex *) malloc((n_max + 1)*sizeof(complex));
  654. std::vector< std::complex<double> > D1XL;
  655. D1XL.resize(n_max+1);
  656. std::vector< std::complex<double> > D3XL;
  657. D3XL.resize(n_max+1);
  658. // complex *PsiXL = (complex *) malloc((n_max + 1)*sizeof(complex));
  659. // complex *ZetaXL = (complex *) malloc((n_max + 1)*sizeof(complex));
  660. std::vector< std::complex<double> > PsiXL;
  661. PsiXL.resize(n_max+1);
  662. std::vector< std::complex<double> > ZetaXL;
  663. ZetaXL.resize(n_max+1);
  664. //*************************************************//
  665. // Calculate D1 and D3 for z1 in the first layer //
  666. //*************************************************//
  667. if (fl == pl) { // PEC layer
  668. for (n = 0; n <= n_max; n++) {
  669. D1_mlxl[fl][n] = std::complex<double>(0, -1);
  670. D3_mlxl[fl][n] = std::complex<double>(0, 1);
  671. }
  672. } else { // Regular layer
  673. z1 = x_std[fl]* m_std[fl];
  674. // Calculate D1 and D3
  675. calcD1D3_std(z1, n_max, D1_mlxl[fl], D3_mlxl[fl]);
  676. }
  677. //******************************************************************//
  678. // Calculate Ha and Hb in the first layer - equations (7a) and (8a) //
  679. //******************************************************************//
  680. for (n = 0; n < n_max; n++) {
  681. Ha[fl][n] = D1_mlxl[fl][n + 1];
  682. Hb[fl][n] = D1_mlxl[fl][n + 1];
  683. }
  684. //*****************************************************//
  685. // Iteration from the second layer to the last one (L) //
  686. //*****************************************************//
  687. for (l = fl + 1; l < L; l++) {
  688. //************************************************************//
  689. //Calculate D1 and D3 for z1 and z2 in the layers fl+1..L //
  690. //************************************************************//
  691. z1 = x_std[l] * m_std[l];
  692. z2 = x_std[l - 1] * m_std[l];
  693. //Calculate D1 and D3 for z1
  694. calcD1D3_std(z1, n_max, D1_mlxl[l], D3_mlxl[l]);
  695. //Calculate D1 and D3 for z2
  696. calcD1D3_std(z2, n_max, D1_mlxlM1[l], D3_mlxlM1[l]);
  697. //*********************************************//
  698. //Calculate Q, Ha and Hb in the layers fl+1..L //
  699. //*********************************************//
  700. // Upward recurrence for Q - equations (19a) and (19b)
  701. //Num = RCmul(exp(-2*(z1.i - z2.i)), Complex(cos(-2*z2.r) - exp(-2*z2.i), sin(-2*z2.r)));
  702. Num = exp( -2.0 * ( z1.imag() - z2.imag() ) ) *
  703. std::complex<double>(cos(-2.0*z2.real()) - exp(-2.0*z2.imag()), sin(-2.0*z2.real()));
  704. Denom = std::complex<double>(cos(-2.0*z1.real()) - exp(-2.0*z1.imag()),
  705. sin(-2.0*z1.real()));
  706. Q[l][0] = Num/Denom;
  707. for (n = 1; n <= n_max; n++) {
  708. cn = std::complex<double>(n, 0);
  709. Num = ( (z1*D1_mlxl[l][n]) + cn) * (cn - (z1*D3_mlxl[l][n - 1]) );
  710. Denom = ( (z2*D1_mlxlM1[l][n]) + cn) * (cn- (z2* D3_mlxlM1[l][n - 1]) );
  711. Q[l][n] = (((x[l - 1]*x[l - 1])/(x[l]*x[l])* Q[l][n - 1]) * Num)/Denom;
  712. }
  713. // Upward recurrence for Ha and Hb - equations (7b), (8b) and (12) - (15)
  714. for (n = 1; n <= n_max; n++) {
  715. //Ha
  716. if ((l - 1) == pl) { // The layer below the current one is a PEC layer
  717. G1 = -1.0 * D1_mlxlM1[l][n];
  718. G2 = -1.0 * D3_mlxlM1[l][n];
  719. } else {
  720. G1 = (m_std[l] * Ha[l - 1][n - 1]) - (m_std[l - 1] * D1_mlxlM1[l][n]);
  721. G2 = (m_std[l] * Ha[l - 1][n - 1]) - (m_std[l - 1] * D3_mlxlM1[l][n]);
  722. }
  723. Temp = Q[l][n] * G1;
  724. Num = (G2*D1_mlxl[l][n]) - (Temp*D3_mlxl[l][n]);
  725. Denom = G2 - Temp;
  726. Ha[l][n - 1] = Num / Denom;
  727. //Hb
  728. if ((l - 1) == pl) { // The layer below the current one is a PEC layer
  729. G1 = Hb[l - 1][n - 1];
  730. G2 = Hb[l - 1][n - 1];
  731. } else {
  732. G1 = (m_std[l - 1] * Hb[l - 1][n - 1]) - (m_std[l] * D1_mlxlM1[l][n]);
  733. G2 = (m_std[l - 1] * Hb[l - 1][n - 1]) - (m_std[l] * D3_mlxlM1[l][n]);
  734. }
  735. Temp = Q[l][n] * G1;
  736. Num = (G2*D1_mlxl[l][n]) - (Temp* D3_mlxl[l][n]);
  737. Denom = (G2- Temp);
  738. Hb[l][n - 1] = (Num/ Denom);
  739. }
  740. }
  741. //**************************************//
  742. //Calculate D1, D3, Psi and Zeta for XL //
  743. //**************************************//
  744. z1 = std::complex<double>(x_std[L - 1], 0);
  745. // Calculate D1XL and D3XL
  746. calcD1D3_std(z1, n_max, D1XL, D3XL);
  747. // Calculate PsiXL and ZetaXL
  748. calcPsiZeta_std(z1, n_max, D1XL, D3XL, PsiXL, ZetaXL);
  749. //*********************************************************************//
  750. // Finally, we calculate the scattering coefficients (an and bn) and //
  751. // the angular functions (Pi and Tau). Note that for these arrays the //
  752. // first layer is 0 (zero), in future versions all arrays will follow //
  753. // this convention to save memory. (13 Nov, 2014) //
  754. //*********************************************************************//
  755. for (n = 0; n < n_max; n++) {
  756. //********************************************************************//
  757. //Expressions for calculating an and bn coefficients are not valid if //
  758. //there is only one PEC layer (ie, for a simple PEC sphere). //
  759. //********************************************************************//
  760. if (pl < (L - 1)) {
  761. an_std[n] = calc_an_std(n + 1, x_std[L-1], Ha[L-1][n], m_std[L-1],
  762. PsiXL[n + 1], ZetaXL[n+1], PsiXL[n], ZetaXL[n]);
  763. bn_std[n] = calc_bn_std(n + 1, x_std[L-1], Hb[L-1][n], m_std[L-1],
  764. PsiXL[n+1], ZetaXL[n+1], PsiXL[n], ZetaXL[n]);
  765. } else {
  766. an_std[n] = calc_an_std(n+1, x_std[L-1], std::complex<double>(0.0,0.0),
  767. std::complex<double>(1.0,0.0),
  768. PsiXL[n+1], ZetaXL[n+1], PsiXL[n], ZetaXL[n]);
  769. bn_std[n] = PsiXL[n+1] / ZetaXL[n+1];
  770. }
  771. }
  772. return n_max;
  773. }
  774. //**********************************************************************************//
  775. // This function is just a wrapper to call the function 'nMieScatt' with fewer //
  776. // parameters, it is here mainly for compatibility with older versions of the //
  777. // program. Also, you can use it if you neither have a PEC layer nor want to define //
  778. // any limit for the maximum number of terms. //
  779. //**********************************************************************************//
  780. int nMie(int L, double x[], complex m[], int nTheta, double Theta[], double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo, complex S1[], complex S2[]) {
  781. return nMieScatt(L, -1, x, m, nTheta, Theta, -1, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2);
  782. }
  783. int nMie_std(double x[], complex m[], double Theta[], complex S1[], complex S2[],
  784. int L, std::vector<double> &x_std, std::vector<std::complex<double> > &m_std, int nTheta, std::vector<double> &Theta_std, double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo, std::vector< std::complex<double> > &S1_std, std::vector< std::complex<double> > &S2_std) {
  785. return nMieScatt_std(x, m, Theta, S1, S2, L, -1, x_std, m_std, nTheta, Theta_std, -1, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1_std, S2_std);
  786. }
  787. //**********************************************************************************//
  788. // This function is just a wrapper to call the function 'nMieScatt' with fewer //
  789. // parameters, it is useful if you want to include a PEC layer but not a limit //
  790. // for the maximum number of terms. //
  791. // //
  792. // Input parameters: //
  793. // L: Number of layers //
  794. // pl: Index of PEC layer. If there is none just send -1 //
  795. // x: Array containing the size parameters of the layers [0..L-1] //
  796. // m: Array containing the relative refractive indexes of the layers [0..L-1] //
  797. // nTheta: Number of scattering angles //
  798. // Theta: Array containing all the scattering angles where the scattering //
  799. // amplitudes will be calculated //
  800. // //
  801. // Output parameters: //
  802. // Qext: Efficiency factor for extinction //
  803. // Qsca: Efficiency factor for scattering //
  804. // Qabs: Efficiency factor for absorption (Qabs = Qext - Qsca) //
  805. // Qbk: Efficiency factor for backscattering //
  806. // Qpr: Efficiency factor for the radiation pressure //
  807. // g: Asymmetry factor (g = (Qext-Qpr)/Qsca) //
  808. // Albedo: Single scattering albedo (Albedo = Qsca/Qext) //
  809. // S1, S2: Complex scattering amplitudes //
  810. // //
  811. // Return value: //
  812. // Number of multipolar expansion terms used for the calculations //
  813. //**********************************************************************************//
  814. int nMiePEC(int L, int pl, double x[], complex m[], int nTheta, double Theta[], double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo, complex S1[], complex S2[]) {
  815. return nMieScatt(L, pl, x, m, nTheta, Theta, -1, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2);
  816. }
  817. //**********************************************************************************//
  818. // This function is just a wrapper to call the function 'nMieScatt' with fewer //
  819. // parameters, it is useful if you want to include a limit for the maximum number //
  820. // of terms but not a PEC layer. //
  821. // //
  822. // Input parameters: //
  823. // L: Number of layers //
  824. // x: Array containing the size parameters of the layers [0..L-1] //
  825. // m: Array containing the relative refractive indexes of the layers [0..L-1] //
  826. // nTheta: Number of scattering angles //
  827. // Theta: Array containing all the scattering angles where the scattering //
  828. // amplitudes will be calculated //
  829. // n_max: Maximum number of multipolar expansion terms to be used for the //
  830. // calculations. Only used if you know what you are doing, otherwise set //
  831. // this parameter to -1 and the function will calculate it //
  832. // //
  833. // Output parameters: //
  834. // Qext: Efficiency factor for extinction //
  835. // Qsca: Efficiency factor for scattering //
  836. // Qabs: Efficiency factor for absorption (Qabs = Qext - Qsca) //
  837. // Qbk: Efficiency factor for backscattering //
  838. // Qpr: Efficiency factor for the radiation pressure //
  839. // g: Asymmetry factor (g = (Qext-Qpr)/Qsca) //
  840. // Albedo: Single scattering albedo (Albedo = Qsca/Qext) //
  841. // S1, S2: Complex scattering amplitudes //
  842. // //
  843. // Return value: //
  844. // Number of multipolar expansion terms used for the calculations //
  845. //**********************************************************************************//
  846. int nMieMax(int L, double x[], complex m[], int nTheta, double Theta[], int n_max, double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo, complex S1[], complex S2[]) {
  847. return nMieScatt(L, -1, x, m, nTheta, Theta, n_max, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2);
  848. }
  849. //**********************************************************************************//
  850. // This function calculates the actual scattering parameters and amplitudes //
  851. // //
  852. // Input parameters: //
  853. // L: Number of layers //
  854. // pl: Index of PEC layer. If there is none just send -1 //
  855. // x: Array containing the size parameters of the layers [0..L-1] //
  856. // m: Array containing the relative refractive indexes of the layers [0..L-1] //
  857. // nTheta: Number of scattering angles //
  858. // Theta: Array containing all the scattering angles where the scattering //
  859. // amplitudes will be calculated //
  860. // n_max: Maximum number of multipolar expansion terms to be used for the //
  861. // calculations. Only used if you know what you are doing, otherwise set //
  862. // this parameter to -1 and the function will calculate it //
  863. // //
  864. // Output parameters: //
  865. // Qext: Efficiency factor for extinction //
  866. // Qsca: Efficiency factor for scattering //
  867. // Qabs: Efficiency factor for absorption (Qabs = Qext - Qsca) //
  868. // Qbk: Efficiency factor for backscattering //
  869. // Qpr: Efficiency factor for the radiation pressure //
  870. // g: Asymmetry factor (g = (Qext-Qpr)/Qsca) //
  871. // Albedo: Single scattering albedo (Albedo = Qsca/Qext) //
  872. // S1, S2: Complex scattering amplitudes //
  873. // //
  874. // Return value: //
  875. // Number of multipolar expansion terms used for the calculations //
  876. //**********************************************************************************//
  877. int nMieScatt(int L, int pl, double x[], complex m[], int nTheta, double Theta[], int n_max, double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo, complex S1[], complex S2[]) {
  878. int i, n, t;
  879. double **Pi, **Tau;
  880. complex *an, *bn;
  881. complex Qbktmp;
  882. n_max = ScattCoeff(L, pl, x, m, n_max, &an, &bn);
  883. Pi = (double **) malloc(n_max*sizeof(double *));
  884. Tau = (double **) malloc(n_max*sizeof(double *));
  885. for (n = 0; n < n_max; n++) {
  886. Pi[n] = (double *) malloc(nTheta*sizeof(double));
  887. Tau[n] = (double *) malloc(nTheta*sizeof(double));
  888. }
  889. calcPiTau(n_max, nTheta, Theta, &Pi, &Tau);
  890. double x2 = x[L - 1]*x[L - 1];
  891. // Initialize the scattering parameters
  892. *Qext = 0;
  893. *Qsca = 0;
  894. *Qabs = 0;
  895. *Qbk = 0;
  896. Qbktmp = C_ZERO;
  897. *Qpr = 0;
  898. *g = 0;
  899. *Albedo = 0;
  900. // Initialize the scattering amplitudes
  901. for (t = 0; t < nTheta; t++) {
  902. S1[t] = C_ZERO;
  903. S2[t] = C_ZERO;
  904. }
  905. // By using downward recurrence we avoid loss of precision due to float rounding errors
  906. // See: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
  907. // http://en.wikipedia.org/wiki/Loss_of_significance
  908. for (i = n_max - 2; i >= 0; i--) {
  909. n = i + 1;
  910. // Equation (27)
  911. *Qext = *Qext + (double)(n + n + 1)*(an[i].r + bn[i].r);
  912. // Equation (28)
  913. *Qsca = *Qsca + (double)(n + n + 1)*(an[i].r*an[i].r + an[i].i*an[i].i + bn[i].r*bn[i].r + bn[i].i*bn[i].i);
  914. // Equation (29)
  915. *Qpr = *Qpr + ((n*(n + 2)/(n + 1))*((Cadd(Cmul(an[i], Conjg(an[n])), Cmul(bn[i], Conjg(bn[n])))).r) + ((double)(n + n + 1)/(n*(n + 1)))*(Cmul(an[i], Conjg(bn[i])).r));
  916. // Equation (33)
  917. Qbktmp = Cadd(Qbktmp, RCmul((double)((n + n + 1)*(1 - 2*(n % 2))), Csub(an[i], bn[i])));
  918. //****************************************************//
  919. // Calculate the scattering amplitudes (S1 and S2) //
  920. // Equations (25a) - (25b) //
  921. //****************************************************//
  922. for (t = 0; t < nTheta; t++) {
  923. S1[t] = Cadd(S1[t], calc_S1_n(n, an[i], bn[i], Pi[i][t], Tau[i][t]));
  924. S2[t] = Cadd(S2[t], calc_S2_n(n, an[i], bn[i], Pi[i][t], Tau[i][t]));
  925. }
  926. }
  927. *Qext = 2*(*Qext)/x2; // Equation (27)
  928. *Qsca = 2*(*Qsca)/x2; // Equation (28)
  929. *Qpr = *Qext - 4*(*Qpr)/x2; // Equation (29)
  930. *Qabs = *Qext - *Qsca; // Equation (30)
  931. *Albedo = *Qsca / *Qext; // Equation (31)
  932. *g = (*Qext - *Qpr) / *Qsca; // Equation (32)
  933. *Qbk = (Qbktmp.r*Qbktmp.r + Qbktmp.i*Qbktmp.i)/x2; // Equation (33)
  934. // Free the memory used for the arrays
  935. for (n = 0; n < n_max; n++) {
  936. free(Pi[n]);
  937. free(Tau[n]);
  938. }
  939. free(Pi);
  940. free(Tau);
  941. free(an);
  942. free(bn);
  943. return n_max;
  944. }
  945. int nMieScatt_std(double x[], complex m[], double Theta[], complex S1[], complex S2[],
  946. int L, int pl,
  947. std::vector<double> &x_std, std::vector<std::complex<double> > &m_std,
  948. int nTheta, std::vector<double> &Theta_std,
  949. int n_max, double *Qext, double *Qsca, double *Qabs, double *Qbk,
  950. double *Qpr, double *g, double *Albedo,
  951. std::vector< std::complex<double> > &S1_std,
  952. std::vector< std::complex<double> > &S2_std) {
  953. int i, n, t;
  954. // double **Pi, **Tau;
  955. std::vector< std::vector<double> > Pi_std, Tau_std;
  956. complex *an, *bn;
  957. std::vector< std::complex<double> > an_std, bn_std;
  958. //complex Qbktmp;
  959. std::complex<double> Qbktmp;
  960. {
  961. int tmp_n_max = ScattCoeff(L, pl, x, m, n_max, &an, &bn);
  962. n_max = ScattCoeff_std(x, m, L, pl, x_std, m_std, n_max, an_std, bn_std);
  963. if (n_max != tmp_n_max) printf("n_max mismatch\n");
  964. compare("an vs an_std: ", an, an_std);
  965. compare("bn vs bn_std: ", an, an_std);
  966. }
  967. // Pi = (double **) malloc(n_max*sizeof(double *));
  968. // Tau = (double **) malloc(n_max*sizeof(double *));
  969. std::vector< std::vector<double> > Pi;
  970. Pi.resize(n_max);
  971. std::vector< std::vector<double> > Tau;
  972. Tau.resize(n_max);
  973. for (n = 0; n < n_max; n++) {
  974. // Pi[n] = (double *) malloc(nTheta*sizeof(double));
  975. // Tau[n] = (double *) malloc(nTheta*sizeof(double));
  976. Pi[n].resize(nTheta);
  977. Tau[n].resize(nTheta);
  978. }
  979. calcPiTau_std(n_max, nTheta, Theta_std, Pi, Tau);
  980. double x2 = x[L - 1]*x[L - 1];
  981. // Initialize the scattering parameters
  982. *Qext = 0;
  983. *Qsca = 0;
  984. *Qabs = 0;
  985. *Qbk = 0;
  986. Qbktmp = std::complex<double>(0.0, 0.0);
  987. *Qpr = 0;
  988. *g = 0;
  989. *Albedo = 0;
  990. // Initialize the scattering amplitudes
  991. for (t = 0; t < nTheta; t++) {
  992. S1_std[t] = std::complex<double>(0.0, 0.0);
  993. S2_std[t] = std::complex<double>(0.0, 0.0);
  994. }
  995. // By using downward recurrence we avoid loss of precision due to float rounding errors
  996. // See: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
  997. // http://en.wikipedia.org/wiki/Loss_of_significance
  998. for (i = n_max - 2; i >= 0; i--) {
  999. n = i + 1;
  1000. // Equation (27)
  1001. *Qext = *Qext + (double)(n + n + 1)*(an_std[i].real() + bn_std[i].real());
  1002. // Equation (28)
  1003. *Qsca = *Qsca + (double)(n + n + 1)*(an_std[i].real()*an_std[i].real() + an_std[i].imag()*an_std[i].imag() + bn_std[i].real()*bn_std[i].real() + bn_std[i].imag()*bn_std[i].imag());
  1004. // Equation (29)
  1005. // Qpr = *Qpr + ((n*(n + 2)/(n + 1))*((Cadd(Cmul(an[i], Conjg(an[n])), Cmul(bn[i], Conjg(bn[n])))).r) + ((double)(n + n + 1)/(n*(n + 1)))*(Cmul(an[i], Conjg(bn[i])).r));
  1006. *Qpr = *Qpr +
  1007. (
  1008. (n*(n + 2)/(n + 1))
  1009. *(
  1010. (
  1011. (an_std[i]*std::conj(an_std[n]))
  1012. + (bn_std[i]*std::conj(bn_std[n]))
  1013. ).real()
  1014. )
  1015. + ((double)(n + n + 1)/(n*(n + 1)))
  1016. *(
  1017. (an_std[i] * std::conj(bn_std[i])
  1018. ).real()
  1019. )
  1020. );
  1021. // Equation (33)
  1022. Qbktmp = Qbktmp + ((double)((n + n + 1)*(1 - 2*(n % 2))) * (an_std[i]- bn_std[i]));
  1023. //****************************************************//
  1024. // Calculate the scattering amplitudes (S1 and S2) //
  1025. // Equations (25a) - (25b) //
  1026. //****************************************************//
  1027. for (t = 0; t < nTheta; t++) {
  1028. S1_std[t] = S1_std[t] + calc_S1_n_std(n, an_std[i], bn_std[i], Pi[i][t], Tau[i][t]);
  1029. S2_std[t] = S2_std[t] + calc_S2_n_std(n, an_std[i], bn_std[i], Pi[i][t], Tau[i][t]);
  1030. }
  1031. }
  1032. *Qext = 2*(*Qext)/x2; // Equation (27)
  1033. *Qsca = 2*(*Qsca)/x2; // Equation (28)
  1034. *Qpr = *Qext - 4*(*Qpr)/x2; // Equation (29)
  1035. *Qabs = *Qext - *Qsca; // Equation (30)
  1036. *Albedo = *Qsca / *Qext; // Equation (31)
  1037. *g = (*Qext - *Qpr) / *Qsca; // Equation (32)
  1038. *Qbk = (Qbktmp.real()*Qbktmp.real() + Qbktmp.imag()*Qbktmp.imag())/x2; // Equation (33)
  1039. // Free the memory used for the arrays
  1040. // for (n = 0; n < n_max; n++) {
  1041. // free(Pi[n]);
  1042. // free(Tau[n]);
  1043. // }
  1044. // free(Pi);
  1045. // free(Tau);
  1046. free(an);
  1047. free(bn);
  1048. return n_max;
  1049. }
  1050. //**********************************************************************************//
  1051. // This function calculates complex electric and magnetic field in the surroundings //
  1052. // and inside (TODO) the particle. //
  1053. // //
  1054. // Input parameters: //
  1055. // L: Number of layers //
  1056. // pl: Index of PEC layer. If there is none just send 0 (zero) //
  1057. // x: Array containing the size parameters of the layers [0..L-1] //
  1058. // m: Array containing the relative refractive indexes of the layers [0..L-1] //
  1059. // n_max: Maximum number of multipolar expansion terms to be used for the //
  1060. // calculations. Only used if you know what you are doing, otherwise set //
  1061. // this parameter to 0 (zero) and the function will calculate it. //
  1062. // nCoords: Number of coordinate points //
  1063. // Coords: Array containing all coordinates where the complex electric and //
  1064. // magnetic fields will be calculated //
  1065. // //
  1066. // Output parameters: //
  1067. // E, H: Complex electric and magnetic field at the provided coordinates //
  1068. // //
  1069. // Return value: //
  1070. // Number of multipolar expansion terms used for the calculations //
  1071. //**********************************************************************************//
  1072. int nMieField(int L, int pl, double x[], complex m[], int n_max, int nCoords, double Xp[], double Yp[], double Zp[], complex E[], complex H[]){
  1073. int i, n, c;
  1074. double **Pi, **Tau;
  1075. complex *an, *bn;
  1076. double *Rho = (double *) malloc(nCoords*sizeof(double));
  1077. double *Phi = (double *) malloc(nCoords*sizeof(double));
  1078. double *Theta = (double *) malloc(nCoords*sizeof(double));
  1079. for (c = 0; c < nCoords; c++) {
  1080. Rho[c] = sqrt(Xp[c]*Xp[c] + Yp[c]*Yp[c] + Zp[c]*Zp[c]);
  1081. if (Rho[c] < 1e-3) {
  1082. Rho[c] = 1e-3;
  1083. }
  1084. Phi[c] = acos(Xp[c]/sqrt(Xp[c]*Xp[c] + Yp[c]*Yp[c]));
  1085. Theta[c] = acos(Xp[c]/Rho[c]);
  1086. }
  1087. n_max = ScattCoeff(L, pl, x, m, n_max, &an, &bn);
  1088. Pi = (double **) malloc(n_max*sizeof(double *));
  1089. Tau = (double **) malloc(n_max*sizeof(double *));
  1090. for (n = 0; n < n_max; n++) {
  1091. Pi[n] = (double *) malloc(nCoords*sizeof(double));
  1092. Tau[n] = (double *) malloc(nCoords*sizeof(double));
  1093. }
  1094. calcPiTau(n_max, nCoords, Theta, &Pi, &Tau);
  1095. double x2 = x[L - 1]*x[L - 1];
  1096. // Initialize the fields
  1097. for (c = 0; c < nCoords; c++) {
  1098. E[c] = C_ZERO;
  1099. H[c] = C_ZERO;
  1100. }
  1101. //*******************************************************//
  1102. // external scattering field = incident + scattered //
  1103. // BH p.92 (4.37), 94 (4.45), 95 (4.50) //
  1104. // assume: medium is non-absorbing; refim = 0; Uabs = 0 //
  1105. //*******************************************************//
  1106. // Firstly the easiest case, we want the field outside the particle
  1107. if (Rho[c] >= x[L - 1]) {
  1108. }
  1109. // for (i = 1; i < (n_max - 1); i++) {
  1110. // n = i - 1;
  1111. /* // Equation (27)
  1112. *Qext = *Qext + (double)(n + n + 1)*(an[i].r + bn[i].r);
  1113. // Equation (28)
  1114. *Qsca = *Qsca + (double)(n + n + 1)*(an[i].r*an[i].r + an[i].i*an[i].i + bn[i].r*bn[i].r + bn[i].i*bn[i].i);
  1115. // Equation (29)
  1116. *Qpr = *Qpr + ((n*(n + 2)/(n + 1))*((Cadd(Cmul(an[i], Conjg(an[n])), Cmul(bn[i], Conjg(bn[n])))).r) + ((double)(n + n + 1)/(n*(n + 1)))*(Cmul(an[i], Conjg(bn[i])).r));
  1117. // Equation (33)
  1118. Qbktmp = Cadd(Qbktmp, RCmul((double)((n + n + 1)*(1 - 2*(n % 2))), Csub(an[i], bn[i])));
  1119. */
  1120. //****************************************************//
  1121. // Calculate the scattering amplitudes (S1 and S2) //
  1122. // Equations (25a) - (25b) //
  1123. //****************************************************//
  1124. /* for (t = 0; t < nTheta; t++) {
  1125. S1[t] = Cadd(S1[t], calc_S1_n(n, an[i], bn[i], Pi[i][t], Tau[i][t]));
  1126. S2[t] = Cadd(S2[t], calc_S2_n(n, an[i], bn[i], Pi[i][t], Tau[i][t]));
  1127. }*/
  1128. // }
  1129. // Free the memory used for the arrays
  1130. for (n = 0; n < n_max; n++) {
  1131. free(Pi[n]);
  1132. free(Tau[n]);
  1133. }
  1134. free(Pi);
  1135. free(Tau);
  1136. free(an);
  1137. free(bn);
  1138. free(Rho);
  1139. free(Phi);
  1140. free(Theta);
  1141. return n_max;
  1142. }