nmie-applied-impl.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #ifndef SRC_NMIE_APPLIED_IMPL_HPP_
  2. #define SRC_NMIE_APPLIED_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. // @brief Wrapper class around nMie function for ease of use //
  34. // //
  35. //**********************************************************************************//
  36. #include "nmie-applied.hpp"
  37. #include "nmie-precision.hpp"
  38. #include <array>
  39. #include <algorithm>
  40. #include <cstdio>
  41. #include <cstdlib>
  42. #include <stdexcept>
  43. #include <vector>
  44. namespace nmie {
  45. // ********************************************************************** //
  46. // ********************************************************************** //
  47. // ********************************************************************** //
  48. template <typename FloatType>
  49. void MultiLayerMieApplied<FloatType>::GetFailed() {
  50. FloatType faild_x = 9.42477796076938;
  51. //FloatType faild_x = 9.42477796076937;
  52. std::complex<FloatType> z(faild_x, 0.0);
  53. std::vector<int> nmax_local_array = {20, 100, 500, 2500};
  54. for (auto nmax_local : nmax_local_array) {
  55. std::vector<std::complex<FloatType> > D1_failed(nmax_local + 1);
  56. // Downward recurrence for D1 - equations (16a) and (16b)
  57. D1_failed[nmax_local] = std::complex<FloatType>(0.0, 0.0);
  58. const std::complex<FloatType> zinv = std::complex<FloatType>(1.0, 0.0)/z;
  59. for (int n = nmax_local; n > 0; n--) {
  60. D1_failed[n - 1] = FloatType(n)*zinv - 1.0/(D1_failed[n] + FloatType(n)*zinv);
  61. }
  62. printf("Faild D1[0] from reccurence (z = %16.14f, nmax = %d): %g\n",
  63. faild_x, nmax_local, D1_failed[0].real());
  64. }
  65. printf("Faild D1[0] from continued fraction (z = %16.14f): %g\n", faild_x,
  66. calcD1confra(0,z).real());
  67. //D1[nmax_] = calcD1confra(nmax_, z);
  68. }
  69. // ********************************************************************** //
  70. // ********************************************************************** //
  71. // ********************************************************************** //
  72. template <typename FloatType>
  73. void MultiLayerMieApplied<FloatType>::AddTargetLayer(FloatType width, std::complex<FloatType> layer_index) {
  74. this->MarkUncalculated();
  75. if (width <= 0)
  76. throw std::invalid_argument("Layer width should be positive!");
  77. target_width_.push_back(width);
  78. target_index_.push_back(layer_index);
  79. } // end of void MultiLayerMieApplied<FloatType>::AddTargetLayer(...)
  80. // ********************************************************************** //
  81. // ********************************************************************** //
  82. // ********************************************************************** //
  83. template <typename FloatType>
  84. void MultiLayerMieApplied<FloatType>::SetTargetPEC(FloatType radius) {
  85. this->MarkUncalculated();
  86. if (target_width_.size() != 0 || target_index_.size() != 0)
  87. throw std::invalid_argument("Error! Define PEC target radius before any other layers!");
  88. // Add layer of any index...
  89. AddTargetLayer(radius, std::complex<FloatType>(0.0, 0.0));
  90. // ... and mark it as PEC
  91. this->SetPECLayer(0);
  92. }
  93. // ********************************************************************** //
  94. // ********************************************************************** //
  95. // ********************************************************************** //
  96. template <typename FloatType>
  97. void MultiLayerMieApplied<FloatType>::SetCoatingIndex(std::vector<std::complex<FloatType> > index) {
  98. this->MarkUncalculated();
  99. coating_index_.clear();
  100. for (auto value : index) coating_index_.push_back(value);
  101. } // end of void MultiLayerMieApplied<FloatType>::SetCoatingIndex(std::vector<complex> index);
  102. // ********************************************************************** //
  103. // ********************************************************************** //
  104. // ********************************************************************** //
  105. template <typename FloatType>
  106. void MultiLayerMieApplied<FloatType>::SetCoatingWidth(std::vector<FloatType> width) {
  107. this->MarkUncalculated();
  108. coating_width_.clear();
  109. for (auto w : width)
  110. if (w <= 0)
  111. throw std::invalid_argument("Coating width should be positive!");
  112. else coating_width_.push_back(w);
  113. }
  114. // end of void MultiLayerMieApplied<FloatType>::SetCoatingWidth(...);
  115. // ********************************************************************** //
  116. // ********************************************************************** //
  117. // ********************************************************************** //
  118. template <typename FloatType>
  119. void MultiLayerMieApplied<FloatType>::SetWidthSP(const std::vector<FloatType>& size_parameter) {
  120. this->MarkUncalculated();
  121. this->size_param_.clear();
  122. FloatType prev_size_parameter = 0.0;
  123. for (auto layer_size_parameter : size_parameter) {
  124. if (layer_size_parameter <= 0.0)
  125. throw std::invalid_argument("Size parameter should be positive!");
  126. if (prev_size_parameter > layer_size_parameter)
  127. throw std::invalid_argument
  128. ("Size parameter for next layer should be larger than the previous one!");
  129. prev_size_parameter = layer_size_parameter;
  130. this->size_param_.push_back(layer_size_parameter);
  131. }
  132. }
  133. // end of void MultiLayerMieApplied<FloatType>::SetWidthSP(...);
  134. // ********************************************************************** //
  135. // ********************************************************************** //
  136. // ********************************************************************** //
  137. template <typename FloatType>
  138. void MultiLayerMieApplied<FloatType>::SetIndexSP(const std::vector< std::complex<FloatType> >& index) {
  139. this->MarkUncalculated();
  140. //refractive_index_.clear();
  141. this->refractive_index_ = index;
  142. // for (auto value : index) refractive_index_.push_back(value);
  143. } // end of void MultiLayerMieApplied<FloatType>::SetIndexSP(...);
  144. // ********************************************************************** //
  145. // ********************************************************************** //
  146. // ********************************************************************** //
  147. template <typename FloatType>
  148. void MultiLayerMieApplied<FloatType>::SetFieldPointsSP(const std::vector< std::vector<FloatType> >& coords_sp) {
  149. if (coords_sp.size() != 3)
  150. throw std::invalid_argument("Error! Wrong dimension of field monitor points!");
  151. if (coords_sp[0].size() != coords_sp[1].size() || coords_sp[0].size() != coords_sp[2].size())
  152. throw std::invalid_argument("Error! Missing coordinates for field monitor points!");
  153. this->coords_ = coords_sp;
  154. // for (int i = 0; i < coords_sp_[0].size(); ++i) {
  155. // printf("%g, %g, %g\n", coords_sp_[0][i], coords_sp_[1][i], coords_sp_[2][i]);
  156. // }
  157. } // end of void MultiLayerMieApplied<FloatType>::SetFieldPointsSP(...)
  158. // ********************************************************************** //
  159. // ********************************************************************** //
  160. // ********************************************************************** //
  161. template <typename FloatType>
  162. void MultiLayerMieApplied<FloatType>::GenerateSizeParameter() {
  163. this->MarkUncalculated();
  164. this->size_param_.clear();
  165. FloatType radius = 0.0;
  166. for (auto width : target_width_) {
  167. radius += width;
  168. this->size_param_.push_back(2*this->PI_*radius/wavelength_);
  169. }
  170. for (auto width : coating_width_) {
  171. radius += width;
  172. this->size_param_.push_back(2*this->PI_*radius/wavelength_);
  173. }
  174. this->total_radius_ = radius;
  175. } // end of void MultiLayerMieApplied<FloatType>::GenerateSizeParameter();
  176. // ********************************************************************** //
  177. // ********************************************************************** //
  178. // ********************************************************************** //
  179. template <typename FloatType>
  180. void MultiLayerMieApplied<FloatType>::GenerateIndex() {
  181. this->MarkUncalculated();
  182. this->refractive_index_.clear();
  183. for (auto index : this->target_index_)
  184. this->refractive_index_.push_back(index);
  185. for (auto index : this->coating_index_)
  186. this->refractive_index_.push_back(index);
  187. } // end of void MultiLayerMieApplied<FloatType>::GenerateIndex();
  188. // ********************************************************************** //
  189. // ********************************************************************** //
  190. // ********************************************************************** //
  191. template <typename FloatType>
  192. FloatType MultiLayerMieApplied<FloatType>::GetTotalRadius() {
  193. if (!this->isMieCalculated()) GenerateSizeParameter();
  194. return this->total_radius_;
  195. } // end of FloatType MultiLayerMieApplied<FloatType>::GetTotalRadius();
  196. // ********************************************************************** //
  197. // ********************************************************************** //
  198. // ********************************************************************** //
  199. template <typename FloatType>
  200. std::vector< std::vector<FloatType> >
  201. MultiLayerMieApplied<FloatType>::GetSpectra(FloatType from_WL, FloatType to_WL, int samples) {
  202. if (!this->isMieCalculated())
  203. throw std::invalid_argument("You should run calculations before result request!");
  204. std::vector< std::vector<FloatType> > spectra;
  205. FloatType step_WL = (to_WL - from_WL)/static_cast<FloatType>(samples);
  206. FloatType wavelength_backup = wavelength_;
  207. long fails = 0;
  208. for (FloatType WL = from_WL; WL < to_WL; WL += step_WL) {
  209. wavelength_ = WL;
  210. try {
  211. RunMieCalculation();
  212. } catch(const std::invalid_argument& ia) {
  213. fails++;
  214. continue;
  215. }
  216. //printf("%3.1f ",WL);
  217. spectra.push_back(std::vector<FloatType>({wavelength_, this->GetQext(),
  218. this->GetQsca(), this->GetQabs(), this->GetQbk()}));
  219. } // end of for each WL in spectra
  220. printf("Spectrum has %li fails\n",fails);
  221. wavelength_ = wavelength_backup;
  222. return spectra;
  223. }
  224. // ********************************************************************** //
  225. // ********************************************************************** //
  226. // ********************************************************************** //
  227. template <typename FloatType>
  228. void MultiLayerMieApplied<FloatType>::ClearTarget() {
  229. this->MarkUncalculated();
  230. this->target_width_.clear();
  231. this->target_index_.clear();
  232. }
  233. // ********************************************************************** //
  234. // ********************************************************************** //
  235. // ********************************************************************** //
  236. template <typename FloatType>
  237. void MultiLayerMieApplied<FloatType>::ClearCoating() {
  238. this->MarkUncalculated();
  239. this->coating_width_.clear();
  240. this->coating_index_.clear();
  241. }
  242. // ********************************************************************** //
  243. // ********************************************************************** //
  244. // ********************************************************************** //
  245. template <typename FloatType>
  246. void MultiLayerMieApplied<FloatType>::ClearLayers() {
  247. this->MarkUncalculated();
  248. this->ClearTarget();
  249. this->ClearCoating();
  250. }
  251. // ********************************************************************** //
  252. // ********************************************************************** //
  253. // ********************************************************************** //
  254. template <typename FloatType>
  255. void MultiLayerMieApplied<FloatType>::ClearAllDesign() {
  256. this->MarkUncalculated();
  257. this->ClearLayers();
  258. this->size_param_.clear();
  259. this->refractive_index_.clear();
  260. }
  261. // ********************************************************************** //
  262. // ********************************************************************** //
  263. // ********************************************************************** //
  264. // Computational core
  265. // ********************************************************************** //
  266. // ********************************************************************** //
  267. // ********************************************************************** //
  268. //**********************************************************************************//
  269. // Function CONFRA ported from MIEV0.f (Wiscombe,1979)
  270. // Ref. to NCAR Technical Notes, Wiscombe, 1979
  271. /*
  272. c Compute Bessel function ratio A-sub-N from its
  273. c continued fraction using Lentz method
  274. c ZINV = Reciprocal of argument of A
  275. c I N T E R N A L V A R I A B L E S
  276. c ------------------------------------
  277. c CAK Term in continued fraction expansion of A (Eq. R25)
  278. c a_k
  279. c CAPT Factor used in Lentz iteration for A (Eq. R27)
  280. c T_k
  281. c CNUMER Numerator in capT (Eq. R28A)
  282. c N_k
  283. c CDENOM Denominator in capT (Eq. R28B)
  284. c D_k
  285. c CDTD Product of two successive denominators of capT factors
  286. c (Eq. R34C)
  287. c xi_1
  288. c CNTN Product of two successive numerators of capT factors
  289. c (Eq. R34B)
  290. c xi_2
  291. c EPS1 Ill-conditioning criterion
  292. c EPS2 Convergence criterion
  293. c KK Subscript k of cAk (Eq. R25B)
  294. c k
  295. c KOUNT Iteration counter (used to prevent infinite looping)
  296. c MAXIT Max. allowed no. of iterations
  297. c MM + 1 and - 1, alternately
  298. */
  299. template <typename FloatType>
  300. std::complex<FloatType> MultiLayerMieApplied<FloatType>::calcD1confra(const int N, const std::complex<FloatType> z) {
  301. // NTMR -> nmax_ - 1 \\TODO nmax_ ?
  302. //int N = nmax_ - 1;
  303. int KK, KOUNT, MAXIT = 10000, MM;
  304. // FloatType EPS1=1.0e-2;
  305. FloatType EPS2=1.0e-8;
  306. std::complex<FloatType> CAK, CAPT, CDENOM, CDTD, CNTN, CNUMER;
  307. std::complex<FloatType> one = std::complex<FloatType>(1.0,0.0);
  308. std::complex<FloatType> ZINV = one/z;
  309. // c ** Eq. R25a
  310. std::complex<FloatType> CONFRA = static_cast<std::complex<FloatType> >(N + 1)*ZINV; //debug ZINV
  311. MM = - 1;
  312. KK = 2*N +3; //debug 3
  313. // c ** Eq. R25b, k=2
  314. CAK = static_cast<std::complex<FloatType> >(MM*KK)*ZINV; //debug -3 ZINV
  315. CDENOM = CAK;
  316. CNUMER = CDENOM + one/CONFRA; //-3zinv+z
  317. KOUNT = 1;
  318. //10 CONTINUE
  319. do { ++KOUNT;
  320. if (KOUNT > MAXIT) {
  321. printf("re(%g):im(%g)\t\n", CONFRA.real(), CONFRA.imag());
  322. throw std::invalid_argument("ConFra--Iteration failed to converge!\n");
  323. }
  324. MM *= - 1; KK += 2; //debug mm=1 kk=5
  325. CAK = static_cast<std::complex<FloatType> >(MM*KK)*ZINV; // ** Eq. R25b //debug 5zinv
  326. // //c ** Eq. R32 Ill-conditioned case -- stride two terms instead of one
  327. // if (std::abs(CNUMER/CAK) >= EPS1 || std::abs(CDENOM/CAK) >= EPS1) {
  328. // //c ** Eq. R34
  329. // CNTN = CAK*CNUMER + 1.0;
  330. // CDTD = CAK*CDENOM + 1.0;
  331. // CONFRA = (CNTN/CDTD)*CONFRA; // ** Eq. R33
  332. // MM *= - 1; KK += 2;
  333. // CAK = static_cast<std::complex<FloatType> >(MM*KK)*ZINV; // ** Eq. R25b
  334. // //c ** Eq. R35
  335. // CNUMER = CAK + CNUMER/CNTN;
  336. // CDENOM = CAK + CDENOM/CDTD;
  337. // ++KOUNT;
  338. // //GO TO 10
  339. // continue;
  340. // } else { //c *** Well-conditioned case
  341. {
  342. CAPT = CNUMER/CDENOM; // ** Eq. R27 //debug (-3zinv + z)/(-3zinv)
  343. // printf("re(%g):im(%g)**\t", CAPT.real(), CAPT.imag());
  344. CONFRA = CAPT*CONFRA; // ** Eq. R26
  345. //if (N == 0) {output=true;printf(" re:");prn(CONFRA.real());printf(" im:"); prn(CONFRA.imag());output=false;};
  346. //c ** Check for convergence; Eq. R31
  347. if (std::abs(CAPT.real() - 1.0) >= EPS2 || std::abs(CAPT.imag()) >= EPS2) {
  348. //c ** Eq. R30
  349. CNUMER = CAK + one/CNUMER;
  350. CDENOM = CAK + one/CDENOM;
  351. continue;
  352. //GO TO 10
  353. } // end of if < eps2
  354. }
  355. break;
  356. } while(1);
  357. //if (N == 0) printf(" return confra for z=(%g,%g)\n", ZINV.real(), ZINV.imag());
  358. return CONFRA;
  359. }
  360. // ********************************************************************** //
  361. // ********************************************************************** //
  362. // ********************************************************************** //
  363. template <typename FloatType>
  364. void MultiLayerMieApplied<FloatType>::ConvertToSP() {
  365. this->MarkUncalculated();
  366. if (target_width_.size() + coating_width_.size() == 0)
  367. return; // Nothing to convert, we suppose that SP was set directly
  368. GenerateSizeParameter();
  369. GenerateIndex();
  370. if (this->size_param_.size() != this->refractive_index_.size())
  371. throw std::invalid_argument("Ivalid conversion of width to size parameter units!/n");
  372. }
  373. // ********************************************************************** //
  374. // ********************************************************************** //
  375. // ********************************************************************** //
  376. template <typename FloatType>
  377. void MultiLayerMieApplied<FloatType>::RunMieCalculation() {
  378. ConvertToSP();
  379. this->MultiLayerMie<FloatType>::RunMieCalculation();
  380. }
  381. // ********************************************************************** //
  382. // ********************************************************************** //
  383. // ********************************************************************** //
  384. template <typename FloatType>
  385. void MultiLayerMieApplied<FloatType>::GetExpanCoeffs( std::vector< std::vector<std::complex<FloatType> > >& aln, std::vector< std::vector<std::complex<FloatType> > >& bln, std::vector< std::vector<std::complex<FloatType> > >& cln, std::vector< std::vector<std::complex<FloatType> > >& dln) {
  386. ConvertToSP(); // Case of call before running full Mie calculation.
  387. // Calculate scattering coefficients an_ and bn_
  388. this->calcScattCoeffs();
  389. // Calculate expansion coefficients aln_, bln_, cln_, and dln_
  390. this->calcExpanCoeffs();
  391. aln = this->aln_;
  392. bln = this->bln_;
  393. cln = this->cln_;
  394. dln = this->dln_;
  395. } // end of void MultiLayerMieApplied<FloatType>::GetExpanCoeffs( ...)
  396. // ********************************************************************** //
  397. // ********************************************************************** //
  398. // ********************************************************************** //
  399. } // end of namespace nmie
  400. #endif // SRC_NMIE_APPLIED_IMPL_HPP_