nmie-wrapper.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #ifndef SRC_NMIE_NMIE_WRAPPER_H_
  2. #define SRC_NMIE_NMIE_WRAPPER_H_
  3. ///
  4. /// @file nmie-wrapper.h
  5. /// @author Ladutenko Konstantin <kostyfisik at gmail (.) com>
  6. /// @date Tue Sep 3 00:40:47 2013
  7. /// @copyright 2013 Ladutenko Konstantin
  8. ///
  9. /// nmie-wrapper 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. /// nmie-wrapper 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. /// You should have received a copy of the GNU General Public License
  20. /// along with nmie-wrapper. If not, see <http://www.gnu.org/licenses/>.
  21. ///
  22. /// nmie-wrapper uses nmie.c from scattnlay by Ovidio Pena
  23. /// <ovidio@bytesfall.com> as a linked library. He has an additional condition to
  24. /// his library:
  25. // The only additional condition is that we expect that all publications //
  26. // describing work using this software , or all commercial products //
  27. // using it, cite the following reference: //
  28. // [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  29. // a multilayered sphere," Computer Physics Communications, //
  30. // vol. 180, Nov. 2009, pp. 2348-2354. //
  31. ///
  32. /// @brief Wrapper class around nMie function for ease of use
  33. ///
  34. ///
  35. #include <array>
  36. #include <complex>
  37. #include <cstdlib>
  38. #include <iostream>
  39. #include <vector>
  40. #ifndef NDEBUG
  41. # define ASSERT(condition, message) \
  42. do { \
  43. if (! (condition)) { \
  44. std::cerr << "Assertion `" #condition "` failed in " << __FILE__ \
  45. << " line " << __LINE__ << ": " << message << std::endl; \
  46. std::exit(EXIT_FAILURE); \
  47. } \
  48. } while (false)
  49. #else
  50. # define ASSERT(condition, message) do { } while (false)
  51. #endif
  52. namespace nmie {
  53. int nMie_wrapper(int L, std::vector<double> x, std::vector<std::complex<double> > m,
  54. int nTheta, std::vector<double> Theta,
  55. double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo,
  56. std::vector<std::complex<double> >& S1, std::vector<std::complex<double> >& S2);
  57. class MultiLayerMie {
  58. // Will throw for any error!
  59. // SP stands for size parameter units.
  60. public:
  61. // Set parameters in applied units
  62. void SetWavelength(double wavelength) {wavelength_ = wavelength;};
  63. // It is possible to set only a multilayer target to run calculaitons.
  64. // For many runs it can be convenient to separate target and coating layers.
  65. // Per layer
  66. void AddTargetLayer(double layer_width, std::complex<double> layer_index);
  67. void AddCoatingLayer(double layer_width, std::complex<double> layer_index);
  68. // For all layers
  69. void SetTargetWidth(std::vector<double> width);
  70. void SetTargetIndex(std::vector< std::complex<double> > index);
  71. void SetCoatingWidth(std::vector<double> width);
  72. void SetCoatingIndex(std::vector< std::complex<double> > index);
  73. void SetFieldPoints(std::vector< std::array<double,3> > coords);
  74. //Set parameters in size parameter units
  75. void SetWidthSP(std::vector<double> width);
  76. void SetIndexSP(std::vector< std::complex<double> > index);
  77. void SetFieldPointsSP(std::vector< std::array<double,3> > coords);
  78. // Set common parameters
  79. void SetAnglesForPattern(double from_angle, double to_angle, int samples);
  80. void SetAngles(std::vector<double> angles);
  81. std::vector<double> GetAngles();
  82. void ClearTarget();
  83. void ClearCoating();
  84. void ClearLayers();
  85. // Applied units requests
  86. double GetTotalRadius();
  87. double GetTargetRadius();
  88. double GetCoatingWidth();
  89. std::vector<double> GetTargetLayersWidth();
  90. std::vector< std::complex<double> > GetTargetLayersIndex();
  91. std::vector<double> GetCoatingLayersWidth();
  92. std::vector< std::complex<double> > GetCoatingLayersIndex();
  93. std::vector< std::array<double,3> > GetFieldPoints();
  94. std::vector<std::array< std::complex<double>,3 > > GetFieldE();
  95. std::vector<std::array< std::complex<double>,3 > > GetFieldH();
  96. std::vector< std::array<double,5> > GetSpectra(double from_WL, double to_WL,
  97. int samples); // ext, sca, abs, bk
  98. double GetRCSext();
  99. double GetRCSsca();
  100. double GetRCSabs();
  101. double GetRCSbk();
  102. std::vector<double> GetPatternEk();
  103. std::vector<double> GetPatternHk();
  104. std::vector<double> GetPatternUnpolarized();
  105. // Size parameter units
  106. std::vector<double> GetLayerWidthSP();
  107. // Same as to get target and coating index
  108. std::vector< std::complex<double> > GetLayerIndex();
  109. std::vector< std::array<double,3> > GetFieldPointsSP();
  110. // Do we need normalize field to size parameter?
  111. /* std::vector<std::vector<std::complex<double> > > GetFieldESP(); */
  112. /* std::vector<std::vector<std::complex<double> > > GetFieldHSP(); */
  113. std::vector< std::array<double,5> > GetSpectraSP(double from_SP, double to_SP,
  114. int samples); // WL,ext, sca, abs, bk
  115. double GetQext();
  116. double GetQsca();
  117. double GetQabs();
  118. double GetQbk();
  119. double GetQpr();
  120. double GetAsymmetryFactor();
  121. double GetAlbedo();
  122. std::vector<std::complex<double> > GetS1();
  123. std::vector<std::complex<double> > GetS2();
  124. std::vector<double> GetPatternEkSP();
  125. std::vector<double> GetPatternHkSP();
  126. std::vector<double> GetPatternUnpolarizedSP();
  127. // Run calculation
  128. void RunMieCalculations();
  129. void RunFieldCalculations();
  130. // Output results (data file + python script to plot it with matplotlib)
  131. void PlotSpectra();
  132. void PlotSpectraSP();
  133. void PlotField();
  134. void PlotFieldSP();
  135. void PlotPattern();
  136. void PlotPatternSP();
  137. private:
  138. const double PI=3.14159265358979323846;
  139. void GenerateSizeParameter();
  140. void GenerateIndex();
  141. double wavelength_ = 1.0;
  142. double total_radius_ = 0.0;
  143. /// Width and index for each layer of the structure
  144. std::vector<double> target_width_, coating_width_;
  145. std::vector< std::complex<double> > target_index_, coating_index_;
  146. /// Size parameters for all layers
  147. std::vector<double> size_parameter_;
  148. /// Complex index values for each layers.
  149. std::vector< std::complex<double> > index_;
  150. double Qsca_ = 0.0, Qext_ = 0.0, Qabs_ = 0.0, Qbk_ = 0.0;
  151. }; // end of class MultiLayerMie
  152. } // end of namespace nmie
  153. #endif // SRC_NMIE_NMIE_WRAPPER_H_