py_nmie.cc 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //**********************************************************************************//
  2. // Copyright (C) 2009-2015 Ovidio Pena <ovidio@bytesfall.com> //
  3. // //
  4. // This file is part of scattnlay //
  5. // //
  6. // This program is free software: you can redistribute it and/or modify //
  7. // it under the terms of the GNU General Public License as published by //
  8. // the Free Software Foundation, either version 3 of the License, or //
  9. // (at your option) any later version. //
  10. // //
  11. // This program is distributed in the hope that it will be useful, //
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  14. // GNU General Public License for more details. //
  15. // //
  16. // The only additional remark is that we expect that all publications //
  17. // describing work using this software, or all commercial products //
  18. // using it, cite the following reference: //
  19. // [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  20. // a multilayered sphere," Computer Physics Communications, //
  21. // vol. 180, Nov. 2009, pp. 2348-2354. //
  22. // //
  23. // You should have received a copy of the GNU General Public License //
  24. // along with this program. If not, see <http://www.gnu.org/licenses/>. //
  25. //**********************************************************************************//
  26. #include <math.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include "nmie.h"
  30. #include "py_nmie.h"
  31. // Same as nMie in 'nmie.h' but uses double arrays to return the results (useful for python).
  32. // This is a workaround because I have not been able to return the results using
  33. // std::vector<std::complex<double> >
  34. int nMie(int L, int pl, std::vector<double> x, std::vector<std::complex<double> > m,
  35. int nTheta, std::vector<double> Theta, int nmax,
  36. double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo,
  37. double S1r[], double S1i[], double S2r[], double S2i[]) {
  38. int i, result;
  39. std::vector<std::complex<double> > S1, S2;
  40. S1.resize(nTheta);
  41. S2.resize(nTheta);
  42. result = nMie(L, pl, x, m, nTheta, Theta, nmax, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2);
  43. for (i = 0; i < nTheta; i++) {
  44. S1r[i] = S1[i].real();
  45. S1i[i] = S1[i].imag();
  46. S2r[i] = S2[i].real();
  47. S2i[i] = S2[i].imag();
  48. }
  49. return result;
  50. }
  51. // Same as nField in 'nmie.h' but uses double arrays to return the results (useful for python).
  52. // This is a workaround because I have not been able to return the results using
  53. // std::vector<std::complex<double> >
  54. int nField(int L, int pl, std::vector<double> x, std::vector<std::complex<double> > m, int nmax,
  55. int nCoords, std::vector<double> Xp, std::vector<double> Yp, std::vector<double> Zp,
  56. double Erx[], double Ery[], double Erz[], double Eix[], double Eiy[], double Eiz[],
  57. double Hrx[], double Hry[], double Hrz[], double Hix[], double Hiy[], double Hiz[]) {
  58. int i, result;
  59. std::vector<std::vector<std::complex<double> > > E, H;
  60. E.resize(nCoords);
  61. H.resize(nCoords);
  62. for (i = 0; i < nCoords; i++) {
  63. E[i].resize(3);
  64. H[i].resize(3);
  65. }
  66. result = nField(L, pl, x, m, nmax, nCoords, Xp, Yp, Zp, E, H);
  67. for (i = 0; i < nCoords; i++) {
  68. Erx[i] = E[i][0].real();
  69. Ery[i] = E[i][1].real();
  70. Erz[i] = E[i][2].real();
  71. Eix[i] = E[i][0].imag();
  72. Eiy[i] = E[i][1].imag();
  73. Eiz[i] = E[i][2].imag();
  74. Hrx[i] = H[i][0].real();
  75. Hry[i] = H[i][1].real();
  76. Hrz[i] = H[i][2].real();
  77. Hix[i] = H[i][0].imag();
  78. Hiy[i] = H[i][1].imag();
  79. Hiz[i] = H[i][2].imag();
  80. }
  81. return result;
  82. }