special-functions-impl.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #ifndef SRC_SPECIAL_FUNCTIONS_IMPL_HPP_
  2. #define SRC_SPECIAL_FUNCTIONS_IMPL_HPP_
  3. //**********************************************************************************//
  4. // Copyright (C) 2009-2018 Ovidio Pena <ovidio@bytesfall.com> //
  5. // Copyright (C) 2013-2018 Konstantin Ladutenko <kostyfisik@gmail.com> //
  6. // //
  7. // This file is part of scattnlay //
  8. // //
  9. // This program is free software: you can redistribute it and/or modify //
  10. // it under the terms of the GNU General Public License as published by //
  11. // the Free Software Foundation, either version 3 of the License, or //
  12. // (at your option) any later version. //
  13. // //
  14. // This program is distributed in the hope that it will be useful, //
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  17. // GNU General Public License for more details. //
  18. // //
  19. // The only additional remark is that we expect that all publications //
  20. // describing work using this software, or all commercial products //
  21. // using it, cite at least one of the following references: //
  22. // [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  23. // a multilayered sphere," Computer Physics Communications, //
  24. // vol. 180, Nov. 2009, pp. 2348-2354. //
  25. // [2] K. Ladutenko, U. Pal, A. Rivera, and O. Pena-Rodriguez, "Mie //
  26. // calculation of electromagnetic near-field for a multilayered //
  27. // sphere," Computer Physics Communications, vol. 214, May 2017, //
  28. // pp. 225-230. //
  29. // //
  30. // You should have received a copy of the GNU General Public License //
  31. // along with this program. If not, see <http://www.gnu.org/licenses/>. //
  32. //**********************************************************************************//
  33. //**********************************************************************************//
  34. // This class implements the algorithm for a multilayered sphere described by: //
  35. // [1] W. Yang, "Improved recursive algorithm for light scattering by a //
  36. // multilayered sphere,” Applied Optics, vol. 42, Mar. 2003, pp. 1710-1720. //
  37. // //
  38. // You can find the description of all the used equations in: //
  39. // [2] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  40. // a multilayered sphere," Computer Physics Communications, //
  41. // vol. 180, Nov. 2009, pp. 2348-2354. //
  42. // [3] K. Ladutenko, U. Pal, A. Rivera, and O. Pena-Rodriguez, "Mie //
  43. // calculation of electromagnetic near-field for a multilayered //
  44. // sphere," Computer Physics Communications, vol. 214, May 2017, //
  45. // pp. 225-230. //
  46. // //
  47. // Hereinafter all equations numbers refer to [2] //
  48. //**********************************************************************************//
  49. #include <iostream>
  50. #include <iomanip>
  51. #include <stdexcept>
  52. #include <vector>
  53. #include "nmie.hpp"
  54. #include "nmie-precision.hpp"
  55. namespace nmie {
  56. // Note, that Kapteyn seems to be too optimistic (at least by 3 digits
  57. // in some cases) for forward recurrence, see D1test with WYang_data
  58. template <typename FloatType>
  59. int evalKapteynNumberOfLostSignificantDigits(const int ni,
  60. const std::complex<FloatType> zz) {
  61. using std::abs; using std::imag; using std::real; using std::log; using std::sqrt; using std::round;
  62. std::complex<double> z = ConvertComplex<double>(zz);
  63. if (abs(z) == 0.) return 0;
  64. auto n = static_cast<double>(ni);
  65. auto one = std::complex<double> (1, 0);
  66. auto lost_digits = round((
  67. abs(imag(z)) - log(2.) - n * ( real( log( z/n) + sqrt(one
  68. - pow2(z/n)) - log (one + sqrt (one
  69. - pow2(z/n)))
  70. )))/ log(10.));
  71. return lost_digits;
  72. }
  73. template <typename FloatType>
  74. int getNStar(int nmax, std::complex<FloatType> z, const int valid_digits) {
  75. if (nmax == 0) nmax = 1;
  76. int nstar = nmax;
  77. auto z_dp = ConvertComplex<double>(z);
  78. if (std::abs(z_dp) == 0.) return nstar;
  79. int forwardLoss = evalKapteynNumberOfLostSignificantDigits(nmax, z_dp);
  80. int increment = static_cast<int>(std::ceil(
  81. std::max(4* std::pow(std::abs( z_dp), 1/3.0), 5.0)
  82. ));
  83. int backwardLoss =evalKapteynNumberOfLostSignificantDigits(nstar, z_dp);
  84. while ( backwardLoss - forwardLoss < valid_digits) {
  85. nstar += increment;
  86. backwardLoss = evalKapteynNumberOfLostSignificantDigits(nstar,z_dp);
  87. };
  88. return nstar;
  89. }
  90. // Custom implementation of complex cot function to avoid overflow
  91. // if Im(z) < 0, then it evaluates cot(z) as conj(cot(conj(z)))
  92. template <typename FloatType>
  93. std::complex<FloatType> complex_cot(const std::complex<FloatType> z) {
  94. auto Remx = z.real();
  95. auto Immx = z.imag();
  96. int sign = (Immx>0) ? 1: -1; // use complex conj if needed for exp and return
  97. auto exp = nmm::exp(-2*sign*Immx);
  98. auto tan = nmm::tan(Remx);
  99. auto a = tan - exp*tan;
  100. auto b = 1 + exp;
  101. auto c = -1 + exp;
  102. auto d = tan + exp* tan;
  103. auto c_one = std::complex<FloatType>(0, 1);
  104. return (a*c + b*d)/(pow2(c) + pow2(d)) +
  105. c_one * (sign* (b*c - a*d)/(pow2(c) + pow2(d)));
  106. }
  107. // Forward iteration for evaluation of ratio of the Riccati–Bessel functions
  108. template <typename FloatType>
  109. void evalForwardR (const std::complex<FloatType> z,
  110. std::vector<std::complex<FloatType> > &r) {
  111. if (r.size() < 1) throw std::invalid_argument(
  112. "We need non-zero evaluations of ratio of the Riccati–Bessel functions.\n");
  113. // r0 = cot(z)
  114. // r[0] = nmm::cos(z)/nmm::sin(z);
  115. r[0] = complex_cot(z);
  116. for (unsigned int n = 0; n < r.size() - 1; n++) {
  117. r[n+1] = static_cast<FloatType>(1)/( static_cast<FloatType>(2*n+1)/z - r[n] );
  118. }
  119. }
  120. // Backward iteration for evaluation of ratio of the Riccati–Bessel functions
  121. template <typename FloatType>
  122. void evalBackwardR (const std::complex<FloatType> z,
  123. std::vector<std::complex<FloatType> > &r) {
  124. if (r.size() < 1) throw std::invalid_argument(
  125. "We need non-zero evaluations of ratio of the Riccati–Bessel functions.\n");
  126. int nmax = r.size()-1 ;
  127. auto tmp = static_cast<FloatType>(2*nmax + 1)/z;
  128. r[nmax] = tmp;
  129. for (int n = nmax -1; n >= 0; n--) {
  130. r[n] = -static_cast<FloatType>(1)/r[n+1] + static_cast<FloatType>(2*n+1)/z;
  131. }
  132. r[0] = complex_cot(z);
  133. }
  134. template <typename FloatType>
  135. void convertRtoD1(const std::complex<FloatType> z,
  136. std::vector<std::complex<FloatType> > &r,
  137. std::vector<std::complex<FloatType> > &D1) {
  138. if (D1.size() > r.size()) throw std::invalid_argument(
  139. "Not enough elements in array of ratio of the Riccati–Bessel functions to convert it into logarithmic derivative array.\n");
  140. std::complex<FloatType> Dold;
  141. for (unsigned int n = 0; n < D1.size(); n++) {
  142. Dold = D1[n];
  143. D1[n] = r[n] - static_cast<FloatType>(n)/z;
  144. // std::cout << "D1old - D1[n] :" << Dold-D1[n] << '\n';
  145. }
  146. }
  147. // ********************************************************************** //
  148. template <typename FloatType>
  149. void evalForwardD (const std::complex<FloatType> z,
  150. std::vector<std::complex<FloatType> > &D) {
  151. int nmax = D.size();
  152. FloatType one = static_cast<FloatType >(1);
  153. for (int n = 1; n < nmax; n++) {
  154. FloatType nf = static_cast<FloatType > (n);
  155. D[n] = one/ (nf/z - D[n-1]) - nf/z;
  156. }
  157. }
  158. // ********************************************************************** //
  159. template <typename FloatType>
  160. void evalForwardD1 (const std::complex<FloatType> z,
  161. std::vector<std::complex<FloatType> > &D) {
  162. if (D.size()<1) throw std::invalid_argument("Should have a leas one element!\n");
  163. D[0] = nmm::cos(z)/nmm::sin(z);
  164. evalForwardD(z,D);
  165. }
  166. // template <typename FloatType>
  167. // void MultiLayerMie<FloatType>::calcD1D3(const std::complex<FloatType> z,
  168. // std::vector<std::complex<FloatType> > &D1,
  169. // std::vector<std::complex<FloatType> > &D3) {
  170. //
  171. // // if (cabs(D1[0]) > 1.0e15) {
  172. // // throw std::invalid_argument("Unstable D1! Please, try to change input parameters!\n");
  173. // // //printf("Warning: Potentially unstable D1! Please, try to change input parameters!\n");
  174. // // }
  175. //
  176. // // Upward recurrence for PsiZeta and D3 - equations (18a) - (18d)
  177. // PsiZeta_[0] = static_cast<FloatType>(0.5)*(static_cast<FloatType>(1.0) - std::complex<FloatType>(nmm::cos(2.0*z.real()), nmm::sin(2.0*z.real()))
  178. // *static_cast<FloatType>(nmm::exp(-2.0*z.imag())));
  179. // D3[0] = std::complex<FloatType>(0.0, 1.0);
  180. //
  181. // for (int n = 1; n <= nmax_; n++) {
  182. // PsiZeta_[n] = PsiZeta_[n - 1]*(static_cast<FloatType>(n)*zinv - D1[n - 1])
  183. // *(static_cast<FloatType>(n)*zinv - D3[n - 1]);
  184. // D3[n] = D1[n] + std::complex<FloatType>(0.0, 1.0)/PsiZeta_[n];
  185. // }
  186. // }
  187. //
  188. //
  189. //**********************************************************************************//
  190. // This function calculates the Riccati-Bessel functions (Psi and Zeta) for a //
  191. // complex argument (z). //
  192. // Equations (20a) - (21b) //
  193. // //
  194. // Input parameters: //
  195. // z: Complex argument to evaluate Psi and Zeta //
  196. // nmax: Maximum number of terms to calculate Psi and Zeta //
  197. // //
  198. // Output parameters: //
  199. // Psi, Zeta: Riccati-Bessel functions //
  200. //**********************************************************************************//
  201. // template <typename FloatType>
  202. // void MultiLayerMie<FloatType>::calcPsiZeta(std::complex<FloatType> z,
  203. // std::vector<std::complex<FloatType> > &Psi,
  204. // std::vector<std::complex<FloatType> > &Zeta) {
  205. //
  206. // std::complex<FloatType> c_i(0.0, 1.0);
  207. // std::vector<std::complex<FloatType> > D1(nmax_ + 1), D3(nmax_ + 1);
  208. //
  209. // // First, calculate the logarithmic derivatives
  210. // calcD1D3(z, D1, D3);
  211. //
  212. // // Now, use the upward recurrence to calculate Psi and Zeta - equations (20a) - (21b)
  213. // Psi[0] = nmm::sin(z);
  214. // Zeta[0] = nmm::sin(z) - c_i*nmm::cos(z);
  215. // for (int n = 1; n <= nmax_; n++) {
  216. // Psi[n] = Psi[n - 1]*(std::complex<FloatType>(n,0.0)/z - D1[n - 1]);
  217. // Zeta[n] = Zeta[n - 1]*(std::complex<FloatType>(n,0.0)/z - D3[n - 1]);
  218. // }
  219. // }
  220. //**********************************************************************************//
  221. // This function calculates Pi and Tau for a given value of cos(Theta). //
  222. // Equations (26a) - (26c) //
  223. // //
  224. // Input parameters: //
  225. // nmax_: Maximum number of terms to calculate Pi and Tau //
  226. // nTheta: Number of scattering angles //
  227. // Theta: Array containing all the scattering angles where the scattering //
  228. // amplitudes will be calculated //
  229. // //
  230. // Output parameters: //
  231. // Pi, Tau: Angular functions Pi and Tau, as defined in equations (26a) - (26c) //
  232. //**********************************************************************************//
  233. // template <typename FloatType>
  234. // void MultiLayerMie<FloatType>::calcPiTau(const FloatType &costheta,
  235. // std::vector<FloatType> &Pi, std::vector<FloatType> &Tau) {
  236. //
  237. // int i;
  238. // //****************************************************//
  239. // // Equations (26a) - (26c) //
  240. // //****************************************************//
  241. // // Initialize Pi and Tau
  242. // Pi[0] = 1.0; // n=1
  243. // Tau[0] = costheta;
  244. // // Calculate the actual values
  245. // if (nmax_ > 1) {
  246. // Pi[1] = 3*costheta*Pi[0]; //n=2
  247. // Tau[1] = 2*costheta*Pi[1] - 3*Pi[0];
  248. // for (i = 2; i < nmax_; i++) { //n=[3..nmax_]
  249. // Pi[i] = ((i + i + 1)*costheta*Pi[i - 1] - (i + 1)*Pi[i - 2])/i;
  250. // Tau[i] = (i + 1)*costheta*Pi[i] - (i + 2)*Pi[i - 1];
  251. // }
  252. // }
  253. // } // end of MultiLayerMie::calcPiTau(...)
  254. //**********************************************************************************//
  255. // This function calculates vector spherical harmonics (eq. 4.50, p. 95 BH), //
  256. // required to calculate the near-field parameters. //
  257. // //
  258. // Input parameters: //
  259. // Rho: Radial distance //
  260. // Phi: Azimuthal angle //
  261. // Theta: Polar angle //
  262. // rn: Either the spherical Ricatti-Bessel function of first or third kind //
  263. // Dn: Logarithmic derivative of rn //
  264. // Pi, Tau: Angular functions Pi and Tau //
  265. // n: Order of vector spherical harmonics //
  266. // //
  267. // Output parameters: //
  268. // Mo1n, Me1n, No1n, Ne1n: Complex vector spherical harmonics //
  269. //**********************************************************************************//
  270. // template <typename FloatType>
  271. // void MultiLayerMie<FloatType>::calcSpherHarm(const std::complex<FloatType> Rho, const FloatType Theta, const FloatType Phi,
  272. // const std::complex<FloatType> &rn, const std::complex<FloatType> &Dn,
  273. // const FloatType &Pi, const FloatType &Tau, const FloatType &n,
  274. // std::vector<std::complex<FloatType> >& Mo1n, std::vector<std::complex<FloatType> >& Me1n,
  275. // std::vector<std::complex<FloatType> >& No1n, std::vector<std::complex<FloatType> >& Ne1n) {
  276. //
  277. // // using eq 4.50 in BH
  278. // std::complex<FloatType> c_zero(0.0, 0.0);
  279. //
  280. // using nmm::sin;
  281. // using nmm::cos;
  282. // Mo1n[0] = c_zero;
  283. // Mo1n[1] = cos(Phi)*Pi*rn/Rho;
  284. // Mo1n[2] = -sin(Phi)*Tau*rn/Rho;
  285. //
  286. // Me1n[0] = c_zero;
  287. // Me1n[1] = -sin(Phi)*Pi*rn/Rho;
  288. // Me1n[2] = -cos(Phi)*Tau*rn/Rho;
  289. //
  290. // No1n[0] = sin(Phi)*(n*n + n)*sin(Theta)*Pi*rn/Rho/Rho;
  291. // No1n[1] = sin(Phi)*Tau*Dn*rn/Rho;
  292. // No1n[2] = cos(Phi)*Pi*Dn*rn/Rho;
  293. //
  294. // Ne1n[0] = cos(Phi)*(n*n + n)*sin(Theta)*Pi*rn/Rho/Rho;
  295. // Ne1n[1] = cos(Phi)*Tau*Dn*rn/Rho;
  296. // Ne1n[2] = -sin(Phi)*Pi*Dn*rn/Rho;
  297. // } // end of MultiLayerMie::calcSpherHarm(...)
  298. //
  299. //**********************************************************************************//
  300. // This functions calculate the logarithmic derivatives of the Riccati-Bessel //
  301. // functions (D1 and D3) for a complex argument (z). //
  302. // Equations (16a), (16b) and (18a) - (18d) //
  303. // //
  304. // Input parameters: //
  305. // z: Complex argument to evaluate D1 and D3 //
  306. // nmax_: Maximum number of terms to calculate D1 and D3 //
  307. // //
  308. // Output parameters: //
  309. // D1, D3: Logarithmic derivatives of the Riccati-Bessel functions //
  310. //**********************************************************************************//
  311. template <typename FloatType>
  312. void evalDownwardD1 (const std::complex<FloatType> z,
  313. std::vector<std::complex<FloatType> >& D1) {
  314. int nmax = D1.size() - 1;
  315. int valid_digits = 10;
  316. #ifdef MULTI_PRECISION
  317. valid_digits += MULTI_PRECISION;
  318. #endif
  319. int nstar = nmie::getNStar(nmax, z, valid_digits);
  320. D1.resize(nstar+1);
  321. // Downward recurrence for D1 - equations (16a) and (16b)
  322. D1[nstar] = std::complex<FloatType>(0.0, 0.0);
  323. std::complex<FloatType> c_one(1.0, 0.0);
  324. const std::complex<FloatType> zinv = std::complex<FloatType>(1.0, 0.0)/z;
  325. for (unsigned int n = nstar; n > 0; n--) {
  326. D1[n - 1] = static_cast<FloatType>(n)*zinv - c_one/
  327. (D1[n] + static_cast<FloatType>(n)*zinv);
  328. }
  329. // Use D1[0] from upward recurrence
  330. D1[0] = complex_cot(z);
  331. D1.resize(nmax+1);
  332. // printf("D1[0] = (%16.15g, %16.15g) z=(%16.15g,%16.15g)\n", D1[0].real(),D1[0].imag(),
  333. // z.real(),z.imag());
  334. }
  335. template <typename FloatType>
  336. void evalUpwardD3 (const std::complex<FloatType> z,
  337. const std::vector<std::complex<FloatType> >& D1,
  338. std::vector<std::complex<FloatType> >& D3,
  339. std::vector<std::complex<FloatType> >& PsiZeta) {
  340. int nmax = D1.size()-1;
  341. // Upward recurrence for PsiZeta and D3 - equations (18a) - (18d)
  342. PsiZeta[0] = static_cast<FloatType>(0.5)*(static_cast<FloatType>(1.0) - std::complex<FloatType>(nmm::cos(2.0*z.real()), nmm::sin(2.0*z.real()))
  343. *static_cast<FloatType>(nmm::exp(-2.0*z.imag())));
  344. D3[0] = std::complex<FloatType>(0.0, 1.0);
  345. const std::complex<FloatType> zinv = std::complex<FloatType>(1.0, 0.0)/z;
  346. for (int n = 1; n <= nmax; n++) {
  347. PsiZeta[n] = PsiZeta[n - 1]*(static_cast<FloatType>(n)*zinv - D1[n - 1])
  348. *(static_cast<FloatType>(n)*zinv - D3[n - 1]);
  349. D3[n] = D1[n] + std::complex<FloatType>(0.0, 1.0)/PsiZeta[n];
  350. }
  351. }
  352. template <typename FloatType>
  353. std::complex<FloatType> complex_sin (const std::complex<FloatType> z){
  354. auto a = z.real();
  355. auto b = z.imag();
  356. auto i = std::complex<FloatType>(0,1);
  357. using nmm::sin; using nmm::cos; using nmm::exp;
  358. return ((cos(a)+i*sin(a))*exp(-b)
  359. - (cos(-a)+i*sin(-a))*exp(b)
  360. )/(static_cast<FloatType>(2)*i);
  361. }
  362. template <typename FloatType>
  363. void evalUpwardPsi (const std::complex<FloatType> z,
  364. const std::vector<std::complex<FloatType> > D1,
  365. std::vector<std::complex<FloatType> >& Psi) {
  366. int nmax = Psi.size() - 1;
  367. // Now, use the upward recurrence to calculate Psi and Zeta - equations (20a) - (21b)
  368. Psi[0] = complex_sin(z);
  369. for (int n = 1; n <= nmax; n++) {
  370. Psi[n] = Psi[n - 1]*(std::complex<FloatType>(n,0.0)/z - D1[n - 1]);
  371. }
  372. }
  373. // Sometimes in literature Zeta is also denoted as Ksi, it is a Riccati-Bessel function of third kind.
  374. template <typename FloatType>
  375. void evalUpwardZeta (const std::complex<FloatType> z,
  376. const std::vector<std::complex<FloatType> > D3,
  377. std::vector<std::complex<FloatType> >& Zeta) {
  378. int nmax = Zeta.size() - 1;
  379. std::complex<FloatType> c_i(0.0, 1.0);
  380. // Now, use the upward recurrence to calculate Zeta and Zeta - equations (20a) - (21b)
  381. Zeta[0] = nmm::sin(z) - c_i*nmm::cos(z);
  382. for (int n = 1; n <= nmax; n++) {
  383. Zeta[n] = Zeta[n - 1]*(std::complex<FloatType>(n, 0.0)/z - D3[n - 1]);
  384. }
  385. }
  386. //void evalForwardRiccatiBessel(const FloatType x, const FloatType first, const FloatType second,
  387. // std::vector<FloatType> &values) {
  388. // values[0] = first;
  389. // values[1] = second;
  390. // int nmax = values.size();
  391. // for (int i = 1; i < nmax-1; i++) {
  392. // values[i+1] = (1 + 2*i) * values[i]/x - values[i-1];
  393. // }
  394. //}
  395. //
  396. //void evalChi(const FloatType x, std::vector<FloatType> &Chi) {
  397. // auto first = nmm::cos(x);
  398. // auto second = first/x + nmm::sin(x);
  399. // evalForwardRiccatiBessel(x, first, second, Chi);
  400. //}
  401. //
  402. //void evalPsi(const FloatType x, std::vector<FloatType> &Psi) {
  403. // auto first = nmm::sin(x);
  404. // auto second = first/x - nmm::cos(x);
  405. // evalForwardRiccatiBessel(x, first, second, Psi);
  406. //}
  407. //
  408. //void composeZeta(const std::vector<FloatType> &Psi,
  409. // const std::vector<FloatType> &Chi,
  410. // std::vector< std::complex<FloatType>> &Zeta) {
  411. // int nmax = Zeta.size();
  412. // for (int i = 0; i < nmax; i++) {
  413. // Zeta[i] = std::complex<FloatType > (Psi[i], Chi[i]);
  414. // }
  415. //}
  416. } // end of namespace nmie
  417. #endif // SRC_SPECIAL_FUNCTIONS_IMPL_HPP_