nmie.cc 51 KB

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