py_nmie.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //**********************************************************************************//
  2. // Copyright (C) 2009-2015 Ovidio Pena <ovidio@bytesfall.com> //
  3. // Copyright (C) 2013-2015 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 the following reference: //
  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. // //
  24. // You should have received a copy of the GNU General Public License //
  25. // along with this program. If not, see <http://www.gnu.org/licenses/>. //
  26. //**********************************************************************************//
  27. #include <math.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include "nmie.h"
  31. #include "py_nmie.h"
  32. // Same as ScattCoeffs in 'nmie.h' but uses double arrays to return the results (useful for python).
  33. // This is a workaround because I have not been able to return the results using
  34. // std::vector<std::complex<double> >
  35. int ScattCoeffs(const unsigned int L, const int pl, std::vector<double>& x, std::vector<std::complex<double> >& m,
  36. const int nmax, double anr[], double ani[], double bnr[], double bni[]) {
  37. int i, result;
  38. std::vector<std::complex<double> > an, bn;
  39. an.resize(nmax);
  40. bn.resize(nmax);
  41. result = nmie::ScattCoeffs(L, pl, x, m, nmax, an, bn);
  42. for (i = 0; i < result; i++) {
  43. anr[i] = an[i].real();
  44. ani[i] = an[i].imag();
  45. bnr[i] = bn[i].real();
  46. bni[i] = bn[i].imag();
  47. }
  48. return result;
  49. }
  50. // Same as nMie in 'nmie.h' but uses double arrays to return the results (useful for python).
  51. // This is a workaround because I have not been able to return the results using
  52. // std::vector<std::complex<double> >
  53. int nMie(const int L, const int pl, std::vector<double>& x, std::vector<std::complex<double> >& m,
  54. const int nTheta, std::vector<double>& Theta, const int nmax,
  55. double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo,
  56. double S1r[], double S1i[], double S2r[], double S2i[]) {
  57. int i, result;
  58. std::vector<std::complex<double> > S1, S2;
  59. S1.resize(nTheta);
  60. S2.resize(nTheta);
  61. result = nmie::nMie(L, pl, x, m, nTheta, Theta, nmax, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2);
  62. for (i = 0; i < nTheta; i++) {
  63. S1r[i] = S1[i].real();
  64. S1i[i] = S1[i].imag();
  65. S2r[i] = S2[i].real();
  66. S2i[i] = S2[i].imag();
  67. }
  68. return result;
  69. }
  70. // Same as nField in 'nmie.h' but uses double arrays to return the results (useful for python).
  71. // This is a workaround because I have not been able to return the results using
  72. // std::vector<std::complex<double> >
  73. int nField(const int L, const int pl, std::vector<double>& x, std::vector<std::complex<double> >& m, const int nmax,
  74. const int nCoords, std::vector<double>& Xp, std::vector<double>& Yp, std::vector<double>& Zp,
  75. double Erx[], double Ery[], double Erz[], double Eix[], double Eiy[], double Eiz[],
  76. double Hrx[], double Hry[], double Hrz[], double Hix[], double Hiy[], double Hiz[]) {
  77. int i, result;
  78. std::vector<std::vector<std::complex<double> > > E, H;
  79. E.resize(nCoords);
  80. H.resize(nCoords);
  81. for (i = 0; i < nCoords; i++) {
  82. E[i].resize(3);
  83. H[i].resize(3);
  84. }
  85. result = nmie::nField(L, pl, x, m, nmax, nCoords, Xp, Yp, Zp, E, H);
  86. for (i = 0; i < nCoords; i++) {
  87. Erx[i] = E[i][0].real();
  88. Ery[i] = E[i][1].real();
  89. Erz[i] = E[i][2].real();
  90. Eix[i] = E[i][0].imag();
  91. Eiy[i] = E[i][1].imag();
  92. Eiz[i] = E[i][2].imag();
  93. Hrx[i] = H[i][0].real();
  94. Hry[i] = H[i][1].real();
  95. Hrz[i] = H[i][2].real();
  96. Hix[i] = H[i][0].imag();
  97. Hiy[i] = H[i][1].imag();
  98. Hiz[i] = H[i][2].imag();
  99. }
  100. return result;
  101. }