nmie-applied.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #ifndef SRC_NMIE_APPLIED_HPP_
  2. #define SRC_NMIE_APPLIED_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. #include <array>
  34. #include <complex>
  35. #include <cstdlib>
  36. #include <iostream>
  37. #include <vector>
  38. #include "nmie.hpp"
  39. #include "nmie-basic.hpp"
  40. #include "nmie-nearfield.hpp"
  41. #include <emscripten/bind.h>
  42. #include <emscripten/val.h>
  43. namespace nmie {
  44. int nMieApplied(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);
  45. int nMieApplied(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);
  46. int nMieApplied(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);
  47. int nMieApplied(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);
  48. template <typename FloatType = double>
  49. class MultiLayerMieApplied : public MultiLayerMie<FloatType> {
  50. // Will throw for any error!
  51. public:
  52. void RunMieCalculation();
  53. void RunFieldCalculationPolar(const int outer_arc_points,
  54. const int radius_points,
  55. const double from_Rho, const double to_Rho,
  56. const double from_Theta, const double to_Theta,
  57. const double from_Phi, const double to_Phi,
  58. const int isIgnoreAvailableNmax);
  59. emscripten::val GetFieldEabs();
  60. void GetFailed();
  61. long iformat = 0;
  62. bool output = true;
  63. void prn(FloatType var) {
  64. do {
  65. if (!output) break;
  66. ++iformat;
  67. printf("%23.13e",var);
  68. if (iformat%4 == 0) printf("\n");
  69. } while (false);
  70. }
  71. // Set parameters in applied units
  72. void SetWavelength(FloatType wavelength) {
  73. this->MultiLayerMie<FloatType>::MarkUncalculated();
  74. wavelength_ = wavelength;};
  75. // It is possible to set only a multilayer target to run calculaitons.
  76. // For many runs it can be convenient to separate target and coating layers.
  77. // Per layer
  78. void AddTargetLayer(FloatType layer_width, std::complex<FloatType> layer_index);
  79. void AddTargetLayerReIm(FloatType layer_width,
  80. FloatType re_layer_index, FloatType im_layer_index);
  81. void AddCoatingLayer(FloatType layer_width, std::complex<FloatType> layer_index);
  82. // For all layers
  83. void SetTargetWidth(std::vector<FloatType> width);
  84. void SetTargetIndex(std::vector< std::complex<FloatType> > index);
  85. void SetTargetPEC(FloatType radius);
  86. void SetCoatingWidth(std::vector<FloatType> width);
  87. void SetCoatingIndex(std::vector< std::complex<FloatType> > index);
  88. void SetFieldPoints(std::vector< std::array<FloatType,3> > coords);
  89. //Set parameters in size parameter units
  90. void SetWidthSP(const std::vector<FloatType>& width);
  91. void SetIndexSP(const std::vector< std::complex<FloatType> >& index);
  92. void SetFieldPointsSP(const std::vector< std::vector<FloatType> >& coords_sp);
  93. // Set common parameters
  94. void SetModeNmaxAndType(int mode_n, int mode_type) {
  95. this->MultiLayerMie<FloatType>::SetModeNmaxAndType(mode_n, mode_type);};
  96. void SetAnglesForPattern(FloatType from_angle, FloatType to_angle, int samples);
  97. std::vector<FloatType> GetAngles();
  98. void ClearTarget();
  99. void ClearCoating();
  100. void ClearLayers();
  101. void ClearAllDesign(); //Layers + SP + index_
  102. // Applied units requests
  103. FloatType GetTotalRadius();
  104. FloatType GetTargetRadius();
  105. FloatType GetCoatingWidth();
  106. std::vector<FloatType> GetTargetLayersWidth();
  107. std::vector< std::complex<FloatType> > GetTargetLayersIndex();
  108. std::vector<FloatType> GetCoatingLayersWidth();
  109. std::vector< std::complex<FloatType> > GetCoatingLayersIndex();
  110. std::vector< std::vector<FloatType> > GetFieldPoints();
  111. std::vector< std::vector<FloatType> > GetSpectra(FloatType from_WL, FloatType to_WL, int samples); // ext, sca, abs, bk
  112. FloatType GetRCSext();
  113. FloatType GetRCSsca();
  114. FloatType GetRCSabs();
  115. FloatType GetRCSbk();
  116. std::vector<FloatType> GetPatternEk();
  117. std::vector<FloatType> GetPatternHk();
  118. std::vector<FloatType> GetPatternUnpolarized();
  119. // Dimensionless
  120. FloatType GetQsca(){ return this->MultiLayerMie<FloatType>::GetQsca();};
  121. FloatType GetQabs(){ return this->MultiLayerMie<FloatType>::GetQabs();};
  122. FloatType GetQext(){ return this->MultiLayerMie<FloatType>::GetQext();};
  123. // Size parameter units
  124. std::vector<FloatType> GetLayerWidthSP();
  125. // Same as to get target and coating index
  126. std::vector< std::complex<FloatType> > GetLayerIndex();
  127. std::vector< std::array<FloatType,3> > GetFieldPointsSP();
  128. // Do we need normalize field to size parameter?
  129. /* std::vector<std::vector<std::complex<FloatType> > > GetFieldESP(); */
  130. /* std::vector<std::vector<std::complex<FloatType> > > GetFieldHSP(); */
  131. std::vector< std::array<FloatType,5> > GetSpectraSP(FloatType from_SP, FloatType to_SP, int samples); // WL,ext, sca, abs, bk
  132. std::vector<FloatType> GetPatternEkSP();
  133. std::vector<FloatType> GetPatternHkSP();
  134. std::vector<FloatType> GetPatternUnpolarizedSP();
  135. void GetExpanCoeffs
  136. (std::vector< std::vector<std::complex<FloatType> > >& aln,
  137. std::vector< std::vector<std::complex<FloatType> > >& bln,
  138. std::vector< std::vector<std::complex<FloatType> > >& cln,
  139. std::vector< std::vector<std::complex<FloatType> > >& dln);
  140. // Output results (data file + python script to plot it with matplotlib)
  141. void PlotSpectra();
  142. void PlotSpectraSP();
  143. void PlotField();
  144. void PlotFieldSP();
  145. void PlotPattern();
  146. void PlotPatternSP();
  147. private:
  148. void ConvertToSP();
  149. void GenerateSizeParameter();
  150. void GenerateIndex();
  151. void InitMieCalculations();
  152. void sbesjh(std::complex<FloatType> z, std::vector<std::complex<FloatType> >& jn,
  153. std::vector<std::complex<FloatType> >& jnp, std::vector<std::complex<FloatType> >& h1n,
  154. std::vector<std::complex<FloatType> >& h1np);
  155. void sphericalBessel(std::complex<FloatType> z, std::vector<std::complex<FloatType> >& bj,
  156. std::vector<std::complex<FloatType> >& by, std::vector<std::complex<FloatType> >& bd);
  157. std::complex<FloatType> calcD1confra(int N, const std::complex<FloatType> z);
  158. FloatType wavelength_ = 1.0;
  159. FloatType total_radius_ = 0.0;
  160. /// Width and index for each layer of the structure
  161. std::vector<FloatType> target_width_, coating_width_;
  162. std::vector< std::complex<FloatType> > target_index_, coating_index_;
  163. std::vector< std::vector<FloatType> > coords_sp_;
  164. }; // end of class MultiLayerMie
  165. } // end of namespace nmie
  166. #endif // SRC_NMIE_APPLIED_HPP