nmie-js-wrapper.cc 9.8 KB

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