nmie-js-wrapper.cc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //**********************************************************************************//
  2. // Copyright (C) 2009-2019 Ovidio Pena <ovidio@bytesfall.com> //
  3. // Copyright (C) 2013-2019 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. // @brief Wrapper to JS
  32. // //
  33. //**********************************************************************************//
  34. #include "nmie-applied.hpp"
  35. #include "nmie-applied-impl.hpp"
  36. #include "nmie-precision.hpp"
  37. using namespace emscripten;
  38. nmie::MultiLayerMieApplied<double> ml_mie;
  39. EMSCRIPTEN_BINDINGS (c) {
  40. class_<nmie::MultiLayerMieApplied<double>>("nmie")
  41. .constructor<>()
  42. .function("SetWavelength", &nmie::MultiLayerMieApplied<double>::SetWavelength)
  43. .function("AddTargetLayerReIm",&nmie::MultiLayerMieApplied<double>::AddTargetLayerReIm)
  44. .function("SetModeNmaxAndType",&nmie::MultiLayerMieApplied<double>::SetModeNmaxAndType)
  45. .function("ClearTarget",&nmie::MultiLayerMieApplied<double>::ClearTarget)
  46. .function("RunMieCalculation",&nmie::MultiLayerMieApplied<double>::RunMieCalculation)
  47. .function("RunFieldCalculationPolar",&nmie::MultiLayerMieApplied<double>::RunFieldCalculationPolar)
  48. .function("RunFieldCalculationCartesian",&nmie::MultiLayerMieApplied<double>::RunFieldCalculationCartesian)
  49. .function("GetFieldEabs",&nmie::MultiLayerMieApplied<double>::GetFieldEabs)
  50. .function("GetQsca",&nmie::MultiLayerMieApplied<double>::GetQsca)
  51. .function("GetQext",&nmie::MultiLayerMieApplied<double>::GetQext)
  52. .function("GetQabs",&nmie::MultiLayerMieApplied<double>::GetQabs)
  53. // .function("bf",&nmie::MultiLayerMieApplied<double>::bf)
  54. ;
  55. }
  56. //namespace nmie {
  57. //**********************************************************************************//
  58. // This function emulates a C call to calculate the actual scattering parameters //
  59. // and amplitudes. //
  60. // //
  61. // Input parameters: //
  62. // L: Number of layers //
  63. // pl: Index of PEC layer. If there is none just send -1 //
  64. // x: Array containing the size parameters of the layers [0..L-1] //
  65. // m: Array containing the relative refractive indexes of the layers [0..L-1] //
  66. // nTheta: Number of scattering angles //
  67. // Theta: Array containing all the scattering angles where the scattering //
  68. // amplitudes will be calculated //
  69. // nmax: Maximum number of multipolar expansion terms to be used for the //
  70. // calculations. Only use it if you know what you are doing, otherwise //
  71. // set this parameter to -1 and the function will calculate it //
  72. // //
  73. // Output parameters: //
  74. // Qext: Efficiency factor for extinction //
  75. // Qsca: Efficiency factor for scattering //
  76. // Qabs: Efficiency factor for absorption (Qabs = Qext - Qsca) //
  77. // Qbk: Efficiency factor for backscattering //
  78. // Qpr: Efficiency factor for the radiation pressure //
  79. // g: Asymmetry factor (g = (Qext-Qpr)/Qsca) //
  80. // Albedo: Single scattering albedo (Albedo = Qsca/Qext) //
  81. // S1, S2: Complex scattering amplitudes //
  82. // //
  83. // Return value: //
  84. // Number of multipolar expansion terms used for the calculations //
  85. //**********************************************************************************//
  86. // 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) {
  87. //
  88. // if (x.size() != L || m.size() != L)
  89. // throw std::invalid_argument("Declared number of layers do not fit x and m!");
  90. // if (Theta.size() != nTheta)
  91. // throw std::invalid_argument("Declared number of sample for Theta is not correct!");
  92. // try {
  93. // MultiLayerMieApplied<FloatType> ml_mie;
  94. // ml_mie.SetLayersSize(ConvertVector<FloatType>(x));
  95. // ml_mie.SetLayersIndex(ConvertComplexVector<FloatType>(m));
  96. // ml_mie.SetAngles(ConvertVector<FloatType>(Theta));
  97. // ml_mie.SetPECLayer(pl);
  98. // ml_mie.SetMaxTerms(nmax);
  99. //
  100. // ml_mie.RunMieCalculation();
  101. //
  102. // *Qext = static_cast<double>(ml_mie.GetQext());
  103. // *Qsca = static_cast<double>(ml_mie.GetQsca());
  104. // *Qabs = static_cast<double>(ml_mie.GetQabs());
  105. // *Qbk = static_cast<double>(ml_mie.GetQbk());
  106. // *Qpr = static_cast<double>(ml_mie.GetQpr());
  107. // *g = static_cast<double>(ml_mie.GetAsymmetryFactor());
  108. // *Albedo = static_cast<double>(ml_mie.GetAlbedo());
  109. // S1 = ConvertComplexVector<double>(ml_mie.GetS1());
  110. // S2 = ConvertComplexVector<double>(ml_mie.GetS2());
  111. //
  112. // return ml_mie.GetMaxTerms();
  113. // } catch(const std::invalid_argument& ia) {
  114. // // Will catch if ml_mie fails or other errors.
  115. // std::cerr << "Invalid argument: " << ia.what() << std::endl;
  116. // throw std::invalid_argument(ia);
  117. // return -1;
  118. // }
  119. // return 0;
  120. // }
  121. // 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) {
  122. // return nmie::nMieApplied(L, -1, x, m, nTheta, Theta, -1, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2);
  123. // }
  124. // 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) {
  125. // return nmie::nMieApplied(L, pl, x, m, nTheta, Theta, -1, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2);
  126. // }
  127. // 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) {
  128. // return nmie::nMieApplied(L, -1, x, m, nTheta, Theta, nmax, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2);
  129. // }
  130. // ********************************************************************** //
  131. // ********************************************************************** //
  132. // ********************************************************************** //
  133. //} // end of namespace nmie