nmie-wrapper.cc 50 KB

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