nmie.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //**********************************************************************************//
  2. // Copyright (C) 2009-2018 Ovidio Pena <ovidio@bytesfall.com> //
  3. // Copyright (C) 2013-2018 Konstantin Ladutenko <kostyfisik@gmail.com> //
  4. // //
  5. // This file is part of scattnlay //
  6. // //
  7. // This program 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. // This program 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. // The only additional remark is that we expect that all publications //
  18. // describing work using this software, or all commercial products //
  19. // using it, cite at least one of the following references: //
  20. // [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  21. // a multilayered sphere," Computer Physics Communications, //
  22. // vol. 180, Nov. 2009, pp. 2348-2354. //
  23. // [2] K. Ladutenko, U. Pal, A. Rivera, and O. Pena-Rodriguez, "Mie //
  24. // calculation of electromagnetic near-field for a multilayered //
  25. // sphere," Computer Physics Communications, vol. 214, May 2017, //
  26. // pp. 225-230. //
  27. // //
  28. // You should have received a copy of the GNU General Public License //
  29. // along with this program. If not, see <http://www.gnu.org/licenses/>. //
  30. //**********************************************************************************//
  31. //**********************************************************************************//
  32. // This class implements the algorithm for a multilayered sphere described by: //
  33. // [1] W. Yang, "Improved recursive algorithm for light scattering by a //
  34. // multilayered sphere,” Applied Optics, vol. 42, Mar. 2003, pp. 1710-1720. //
  35. // //
  36. // You can find the description of all the used equations in: //
  37. // [2] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  38. // a multilayered sphere," Computer Physics Communications, //
  39. // vol. 180, Nov. 2009, pp. 2348-2354. //
  40. // [3] K. Ladutenko, U. Pal, A. Rivera, and O. Pena-Rodriguez, "Mie //
  41. // calculation of electromagnetic near-field for a multilayered //
  42. // sphere," Computer Physics Communications, vol. 214, May 2017, //
  43. // pp. 225-230. //
  44. // //
  45. // Hereinafter all equations numbers refer to [2] //
  46. //**********************************************************************************//
  47. #include "nmie.hpp"
  48. #include "nmie-impl.hpp"
  49. #include "nmie-precision.hpp"
  50. #include <array>
  51. #include <algorithm>
  52. #include <cstdio>
  53. #include <cstdlib>
  54. #include <stdexcept>
  55. #include <iostream>
  56. #include <iomanip>
  57. #include <vector>
  58. namespace nmie {
  59. //**********************************************************************************//
  60. // This function emulates a C call to calculate the scattering coefficients //
  61. // required to calculate both the near- and far-field parameters. //
  62. // //
  63. // Input parameters: //
  64. // L: Number of layers //
  65. // pl: Index of PEC layer. If there is none just send -1 //
  66. // x: Array containing the size parameters of the layers [0..L-1] //
  67. // m: Array containing the relative refractive indexes of the layers [0..L-1] //
  68. // nmax: Maximum number of multipolar expansion terms to be used for the //
  69. // calculations. Only use it if you know what you are doing, otherwise //
  70. // set this parameter to -1 and the function will calculate it. //
  71. // //
  72. // Output parameters: //
  73. // an, bn: Complex scattering amplitudes //
  74. // //
  75. // Return value: //
  76. // Number of multipolar expansion terms used for the calculations //
  77. //**********************************************************************************//
  78. int ScattCoeffs(const unsigned int L, const int pl, std::vector<double>& x, std::vector<std::complex<double> >& m, const int nmax, std::vector<std::complex<double> >& an, std::vector<std::complex<double> >& bn) {
  79. if (x.size() != L || m.size() != L)
  80. throw std::invalid_argument("Declared number of layers do not fit x and m!");
  81. try {
  82. MultiLayerMie<FloatType> ml_mie;
  83. ml_mie.SetLayersSize(ConvertVector<FloatType>(x));
  84. ml_mie.SetLayersIndex(ConvertComplexVector<FloatType>(m));
  85. ml_mie.SetPECLayer(pl);
  86. ml_mie.SetMaxTerms(nmax);
  87. ml_mie.calcScattCoeffs();
  88. an = ConvertComplexVector<double>(ml_mie.GetAn());
  89. bn = ConvertComplexVector<double>(ml_mie.GetBn());
  90. return ml_mie.GetMaxTerms();
  91. } catch(const std::invalid_argument& ia) {
  92. // Will catch if ml_mie fails or other errors.
  93. std::cerr << "Invalid argument: " << ia.what() << std::endl;
  94. throw std::invalid_argument(ia);
  95. return -1;
  96. }
  97. return 0;
  98. }
  99. //**********************************************************************************//
  100. // This function emulates a C call to calculate the actual scattering parameters //
  101. // and amplitudes. //
  102. // //
  103. // Input parameters: //
  104. // L: Number of layers //
  105. // pl: Index of PEC layer. If there is none just send -1 //
  106. // x: Array containing the size parameters of the layers [0..L-1] //
  107. // m: Array containing the relative refractive indexes of the layers [0..L-1] //
  108. // nTheta: Number of scattering angles //
  109. // Theta: Array containing all the scattering angles where the scattering //
  110. // amplitudes will be calculated //
  111. // nmax: Maximum number of multipolar expansion terms to be used for the //
  112. // calculations. Only use it if you know what you are doing, otherwise //
  113. // set this parameter to -1 and the function will calculate it //
  114. // //
  115. // Output parameters: //
  116. // Qext: Efficiency factor for extinction //
  117. // Qsca: Efficiency factor for scattering //
  118. // Qabs: Efficiency factor for absorption (Qabs = Qext - Qsca) //
  119. // Qbk: Efficiency factor for backscattering //
  120. // Qpr: Efficiency factor for the radiation pressure //
  121. // g: Asymmetry factor (g = (Qext-Qpr)/Qsca) //
  122. // Albedo: Single scattering albedo (Albedo = Qsca/Qext) //
  123. // S1, S2: Complex scattering amplitudes //
  124. // //
  125. // Return value: //
  126. // Number of multipolar expansion terms used for the calculations //
  127. //**********************************************************************************//
  128. int nMie(const unsigned int L, const int pl, std::vector<double>& x, std::vector<std::complex<double> >& m, const unsigned int nTheta, std::vector<double>& Theta, const int nmax, double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo, std::vector<std::complex<double> >& S1, std::vector<std::complex<double> >& S2) {
  129. if (x.size() != L || m.size() != L)
  130. throw std::invalid_argument("Declared number of layers do not fit x and m!");
  131. if (Theta.size() != nTheta)
  132. throw std::invalid_argument("Declared number of sample for Theta is not correct!");
  133. try {
  134. MultiLayerMie<FloatType> ml_mie;
  135. ml_mie.SetLayersSize(ConvertVector<FloatType>(x));
  136. ml_mie.SetLayersIndex(ConvertComplexVector<FloatType>(m));
  137. ml_mie.SetAngles(ConvertVector<FloatType>(Theta));
  138. ml_mie.SetPECLayer(pl);
  139. ml_mie.SetMaxTerms(nmax);
  140. ml_mie.RunMieCalculation();
  141. // std::cout
  142. // << std::setprecision(std::numeric_limits<FloatType>::digits10)
  143. // << "Qext = "
  144. // << ml_mie.GetQext()
  145. // << std::endl;
  146. *Qext = static_cast<double>(ml_mie.GetQext());
  147. *Qsca = static_cast<double>(ml_mie.GetQsca());
  148. *Qabs = static_cast<double>(ml_mie.GetQabs());
  149. *Qbk = static_cast<double>(ml_mie.GetQbk());
  150. *Qpr = static_cast<double>(ml_mie.GetQpr());
  151. *g = static_cast<double>(ml_mie.GetAsymmetryFactor());
  152. *Albedo = static_cast<double>(ml_mie.GetAlbedo());
  153. S1 = ConvertComplexVector<double>(ml_mie.GetS1());
  154. S2 = ConvertComplexVector<double>(ml_mie.GetS2());
  155. return ml_mie.GetMaxTerms();
  156. } catch(const std::invalid_argument& ia) {
  157. // Will catch if ml_mie fails or other errors.
  158. std::cerr << "Invalid argument: " << ia.what() << std::endl;
  159. throw std::invalid_argument(ia);
  160. return -1;
  161. }
  162. return 0;
  163. }
  164. //**********************************************************************************//
  165. // This function is just a wrapper to call the full 'nMie' function with fewer //
  166. // parameters, it is here mainly for compatibility with older versions of the //
  167. // program. Also, you can use it if you neither have a PEC layer nor want to define //
  168. // any limit for the maximum number of terms. //
  169. // //
  170. // Input parameters: //
  171. // L: Number of layers //
  172. // x: Array containing the size parameters of the layers [0..L-1] //
  173. // m: Array containing the relative refractive indexes of the layers [0..L-1] //
  174. // nTheta: Number of scattering angles //
  175. // Theta: Array containing all the scattering angles where the scattering //
  176. // amplitudes will be calculated //
  177. // //
  178. // Output parameters: //
  179. // Qext: Efficiency factor for extinction //
  180. // Qsca: Efficiency factor for scattering //
  181. // Qabs: Efficiency factor for absorption (Qabs = Qext - Qsca) //
  182. // Qbk: Efficiency factor for backscattering //
  183. // Qpr: Efficiency factor for the radiation pressure //
  184. // g: Asymmetry factor (g = (Qext-Qpr)/Qsca) //
  185. // Albedo: Single scattering albedo (Albedo = Qsca/Qext) //
  186. // S1, S2: Complex scattering amplitudes //
  187. // //
  188. // Return value: //
  189. // Number of multipolar expansion terms used for the calculations //
  190. //**********************************************************************************//
  191. int nMie(const unsigned int L, std::vector<double>& x, std::vector<std::complex<double> >& m, const unsigned int nTheta, std::vector<double>& Theta, double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo, std::vector<std::complex<double> >& S1, std::vector<std::complex<double> >& S2) {
  192. return nmie::nMie(L, -1, x, m, nTheta, Theta, -1, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2);
  193. }
  194. //**********************************************************************************//
  195. // This function is just a wrapper to call the full 'nMie' function with fewer //
  196. // parameters, it is useful if you want to include a PEC layer but not a limit //
  197. // for the maximum number of terms. //
  198. // //
  199. // Input parameters: //
  200. // L: Number of layers //
  201. // pl: Index of PEC layer. If there is none just send -1 //
  202. // x: Array containing the size parameters of the layers [0..L-1] //
  203. // m: Array containing the relative refractive indexes of the layers [0..L-1] //
  204. // nTheta: Number of scattering angles //
  205. // Theta: Array containing all the scattering angles where the scattering //
  206. // amplitudes will be calculated //
  207. // //
  208. // Output parameters: //
  209. // Qext: Efficiency factor for extinction //
  210. // Qsca: Efficiency factor for scattering //
  211. // Qabs: Efficiency factor for absorption (Qabs = Qext - Qsca) //
  212. // Qbk: Efficiency factor for backscattering //
  213. // Qpr: Efficiency factor for the radiation pressure //
  214. // g: Asymmetry factor (g = (Qext-Qpr)/Qsca) //
  215. // Albedo: Single scattering albedo (Albedo = Qsca/Qext) //
  216. // S1, S2: Complex scattering amplitudes //
  217. // //
  218. // Return value: //
  219. // Number of multipolar expansion terms used for the calculations //
  220. //**********************************************************************************//
  221. int nMie(const unsigned int L, const int pl, std::vector<double>& x, std::vector<std::complex<double> >& m, const unsigned int nTheta, std::vector<double>& Theta, double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo, std::vector<std::complex<double> >& S1, std::vector<std::complex<double> >& S2) {
  222. return nmie::nMie(L, pl, x, m, nTheta, Theta, -1, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2);
  223. }
  224. //**********************************************************************************//
  225. // This function is just a wrapper to call the full 'nMie' function with fewer //
  226. // parameters, it is useful if you want to include a limit for the maximum number //
  227. // of terms but not a PEC layer. //
  228. // //
  229. // Input parameters: //
  230. // L: Number of layers //
  231. // x: Array containing the size parameters of the layers [0..L-1] //
  232. // m: Array containing the relative refractive indexes of the layers [0..L-1] //
  233. // nTheta: Number of scattering angles //
  234. // Theta: Array containing all the scattering angles where the scattering //
  235. // amplitudes will be calculated //
  236. // nmax: Maximum number of multipolar expansion terms to be used for the //
  237. // calculations. Only use it if you know what you are doing, otherwise //
  238. // set this parameter to -1 and the function will calculate it //
  239. // //
  240. // Output parameters: //
  241. // Qext: Efficiency factor for extinction //
  242. // Qsca: Efficiency factor for scattering //
  243. // Qabs: Efficiency factor for absorption (Qabs = Qext - Qsca) //
  244. // Qbk: Efficiency factor for backscattering //
  245. // Qpr: Efficiency factor for the radiation pressure //
  246. // g: Asymmetry factor (g = (Qext-Qpr)/Qsca) //
  247. // Albedo: Single scattering albedo (Albedo = Qsca/Qext) //
  248. // S1, S2: Complex scattering amplitudes //
  249. // //
  250. // Return value: //
  251. // Number of multipolar expansion terms used for the calculations //
  252. //**********************************************************************************//
  253. int nMie(const unsigned int L, std::vector<double>& x, std::vector<std::complex<double> >& m, const unsigned int nTheta, std::vector<double>& Theta, const int nmax, double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo, std::vector<std::complex<double> >& S1, std::vector<std::complex<double> >& S2) {
  254. return nmie::nMie(L, -1, x, m, nTheta, Theta, nmax, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2);
  255. }
  256. //**********************************************************************************//
  257. // This function emulates a C call to calculate complex electric and magnetic field //
  258. // in the surroundings and inside the particle. //
  259. // //
  260. // Input parameters: //
  261. // L: Number of layers //
  262. // pl: Index of PEC layer. If there is none just send 0 (zero) //
  263. // x: Array containing the size parameters of the layers [0..L-1] //
  264. // m: Array containing the relative refractive indexes of the layers [0..L-1] //
  265. // nmax: Maximum number of multipolar expansion terms to be used for the //
  266. // calculations. Only use it if you know what you are doing, otherwise //
  267. // set this parameter to 0 (zero) and the function will calculate it. //
  268. // ncoord: Number of coordinate points //
  269. // Coords: Array containing all coordinates where the complex electric and //
  270. // magnetic fields will be calculated //
  271. // //
  272. // Output parameters: //
  273. // E, H: Complex electric and magnetic field at the provided coordinates //
  274. // //
  275. // Return value: //
  276. // Number of multipolar expansion terms used for the calculations //
  277. //**********************************************************************************//
  278. int nField(const unsigned int L, const int pl, const std::vector<double>& x, const std::vector<std::complex<double> >& m, const int nmax, const unsigned int ncoord, const std::vector<double>& Xp_vec, const std::vector<double>& Yp_vec, const std::vector<double>& Zp_vec, std::vector<std::vector<std::complex<double> > >& E, std::vector<std::vector<std::complex<double> > >& H) {
  279. if (x.size() != L || m.size() != L)
  280. throw std::invalid_argument("Declared number of layers do not fit x and m!");
  281. if (Xp_vec.size() != ncoord || Yp_vec.size() != ncoord || Zp_vec.size() != ncoord
  282. || E.size() != ncoord || H.size() != ncoord)
  283. throw std::invalid_argument("Declared number of coords do not fit Xp, Yp, Zp, E, or H!");
  284. for (auto f:E)
  285. if (f.size() != 3)
  286. throw std::invalid_argument("Field E is not 3D!");
  287. for (auto f:H)
  288. if (f.size() != 3)
  289. throw std::invalid_argument("Field H is not 3D!");
  290. try {
  291. MultiLayerMie<FloatType> ml_mie;
  292. ml_mie.SetLayersSize(ConvertVector<FloatType>(x));
  293. ml_mie.SetLayersIndex(ConvertComplexVector<FloatType>(m));
  294. ml_mie.SetPECLayer(pl);
  295. ml_mie.SetFieldCoords({ConvertVector<FloatType>(Xp_vec),
  296. ConvertVector<FloatType>(Yp_vec),
  297. ConvertVector<FloatType>(Zp_vec) });
  298. ml_mie.RunFieldCalculation();
  299. E = ConvertComplexVectorVector<double>(ml_mie.GetFieldE());
  300. H = ConvertComplexVectorVector<double>(ml_mie.GetFieldH());
  301. return ml_mie.GetMaxTerms();
  302. } catch(const std::invalid_argument& ia) {
  303. // Will catch if ml_mie fails or other errors.
  304. std::cerr << "Invalid argument: " << ia.what() << std::endl;
  305. throw std::invalid_argument(ia);
  306. return - 1;
  307. }
  308. return 0;
  309. }
  310. } // end of namespace nmie