py_nmie.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. using namespace std;
  33. // Same as ScattCoeffs in 'nmie.h' but uses double arrays to return the results (useful for python).
  34. // This is a workaround because I have not been able to return the results using
  35. // vector<complex<double> >
  36. int ScattCoeffs(const unsigned int L, const int pl, vector<double>& x, vector<complex<double> >& m,
  37. const int nmax, double anr[], double ani[], double bnr[], double bni[]) {
  38. int i, result;
  39. vector<complex<double> > an, bn;
  40. an.resize(nmax);
  41. bn.resize(nmax);
  42. result = nmie::ScattCoeffs(L, pl, x, m, nmax, an, bn);
  43. for (i = 0; i < result; i++) {
  44. anr[i] = an[i].real();
  45. ani[i] = an[i].imag();
  46. bnr[i] = bn[i].real();
  47. bni[i] = bn[i].imag();
  48. }
  49. return result;
  50. }
  51. // Same as nMie 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. // vector<complex<double> >
  54. int nMie(const int L, const int pl, vector<double>& x, vector<complex<double> >& m,
  55. const int nTheta, vector<double>& Theta, const int nmax,
  56. double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo,
  57. double S1r[], double S1i[], double S2r[], double S2i[]) {
  58. int i, result;
  59. vector<complex<double> > S1, S2;
  60. S1.resize(nTheta);
  61. S2.resize(nTheta);
  62. result = nmie::nMie(L, pl, x, m, nTheta, Theta, nmax, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2);
  63. for (i = 0; i < nTheta; i++) {
  64. S1r[i] = S1[i].real();
  65. S1i[i] = S1[i].imag();
  66. S2r[i] = S2[i].real();
  67. S2i[i] = S2[i].imag();
  68. }
  69. return result;
  70. }
  71. // Same as nField in 'nmie.h' but uses double arrays to return the results (useful for python).
  72. // This is a workaround because I have not been able to return the results using
  73. // vector<complex<double> >
  74. int nField(const int L, const int pl, vector<double>& x, vector<complex<double> >& m, const int nmax,
  75. const int nCoords, vector<double>& Xp, vector<double>& Yp, vector<double>& Zp,
  76. double Erx[], double Ery[], double Erz[], double Eix[], double Eiy[], double Eiz[],
  77. double Hrx[], double Hry[], double Hrz[], double Hix[], double Hiy[], double Hiz[]) {
  78. int i, result;
  79. vector<vector<complex<double> > > E, H;
  80. E.resize(nCoords);
  81. H.resize(nCoords);
  82. for (i = 0; i < nCoords; i++) {
  83. E[i].resize(3);
  84. H[i].resize(3);
  85. }
  86. result = nmie::nField(L, pl, x, m, nmax, nCoords, Xp, Yp, Zp, E, H);
  87. for (i = 0; i < nCoords; i++) {
  88. Erx[i] = E[i][0].real();
  89. Ery[i] = E[i][1].real();
  90. Erz[i] = E[i][2].real();
  91. Eix[i] = E[i][0].imag();
  92. Eiy[i] = E[i][1].imag();
  93. Eiz[i] = E[i][2].imag();
  94. Hrx[i] = H[i][0].real();
  95. Hry[i] = H[i][1].real();
  96. Hrz[i] = H[i][2].real();
  97. Hix[i] = H[i][0].imag();
  98. Hiy[i] = H[i][1].imag();
  99. Hiz[i] = H[i][2].imag();
  100. }
  101. return result;
  102. }