scattnlay.pyx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # Copyright (C) 2009-2017 Ovidio Pena <ovidio@bytesfall.com>
  2. # Copyright (C) 2013-2017 Konstantin Ladutenko <kostyfisik@gmail.com>
  3. #
  4. # This file is part of python-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. # distutils: language = c++
  26. # distutils: sources = nmie.cc
  27. from __future__ import division
  28. import numpy as np
  29. cimport numpy as np
  30. from libcpp.vector cimport vector
  31. from libcpp.vector cimport complex
  32. cdef inline double *npy2c(np.ndarray a):
  33. assert a.dtype == np.float64
  34. if not (<object>a).flags["C_CONTIGUOUS"]: # Array is not contiguous, need to make contiguous copy
  35. a = a.copy('C')
  36. # Return data pointer
  37. return <double *>(a.data)
  38. cdef extern from "py_nmie.h":
  39. cdef int ScattCoeffs(int L, int pl, vector[double] x, vector[complex] m, int nmax, double anr[], double ani[], double bnr[], double bni[])
  40. # cdef int ExpansionCoeffs( int L, int pl, vector[double] x, vector[complex] m,
  41. # int nmax,
  42. # vector[vector[double] ]& alnr,
  43. # vector[vector[double] ]& alni,
  44. # vector[vector[double] ]& blnr,
  45. # vector[vector[double] ]& blni,
  46. # vector[vector[double] ]& clnr,
  47. # vector[vector[double] ]& clni,
  48. # vector[vector[double] ]& dlnr,
  49. # vector[vector[double] ]& dlni)
  50. cdef int ExpansionCoeffs(int L, int pl, vector[double] x, vector[complex] m, int nmax, double alnr[], double alni[], double blnr[], double blni[], double clnr[], double clni[], double dlnr[], double dlni[])
  51. cdef int nMie(int L, int pl, vector[double] x, vector[complex] m, int nTheta, vector[double] Theta, int nmax, double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo, double S1r[], double S1i[], double S2r[], double S2i[], int mode_n, int mode_type)
  52. cdef int nField(int L, int pl, vector[double] x, vector[complex] m, int nmax, int mode_n, int mode_type, int nCoords, vector[double] Xp, vector[double] Yp, vector[double] Zp, double Erx[], double Ery[], double Erz[], double Eix[], double Eiy[], double Eiz[], double Hrx[], double Hry[], double Hrz[], double Hix[], double Hiy[], double Hiz[])
  53. def scattcoeffs(np.ndarray[np.float64_t, ndim = 2] x, np.ndarray[np.complex128_t, ndim = 2] m, np.int_t nmax, np.int_t pl = -1):
  54. cdef Py_ssize_t i
  55. cdef np.ndarray[np.int_t, ndim = 1] terms = np.zeros(x.shape[0], dtype = np.int)
  56. cdef np.ndarray[np.complex128_t, ndim = 2] an = np.zeros(
  57. (x.shape[0], nmax), dtype = np.complex128)
  58. cdef np.ndarray[np.complex128_t, ndim = 2] bn = np.zeros(
  59. (x.shape[0], nmax), dtype = np.complex128)
  60. cdef np.ndarray[np.float64_t, ndim = 1] anr
  61. cdef np.ndarray[np.float64_t, ndim = 1] ani
  62. cdef np.ndarray[np.float64_t, ndim = 1] bnr
  63. cdef np.ndarray[np.float64_t, ndim = 1] bni
  64. for i in range(x.shape[0]):
  65. anr = np.zeros(nmax, dtype = np.float64)
  66. ani = np.zeros(nmax, dtype = np.float64)
  67. bnr = np.zeros(nmax, dtype = np.float64)
  68. bni = np.zeros(nmax, dtype = np.float64)
  69. terms[i] = ScattCoeffs(x.shape[1], pl, x[i].copy('C'), m[i].copy('C'), nmax, npy2c(anr), npy2c(ani), npy2c(bnr), npy2c(bni))
  70. an[i] = anr.copy('C') + 1.0j*ani.copy('C')
  71. bn[i] = bnr.copy('C') + 1.0j*bni.copy('C')
  72. return terms, an, bn
  73. def expansioncoeffs(np.ndarray[np.float64_t, ndim = 2] x, np.ndarray[np.complex128_t, ndim = 2] m, np.int_t nmax, np.int_t pl = -1):
  74. cdef Py_ssize_t i
  75. cdef Py_ssize_t l
  76. cdef np.ndarray[np.int_t, ndim = 1] terms = np.zeros(x.shape[0], dtype = np.int)
  77. cdef np.ndarray[np.complex128_t, ndim = 3] aln = np.zeros((x.shape[0], x.shape[1]+1, nmax), dtype = np.complex128)
  78. cdef np.ndarray[np.complex128_t, ndim = 3] bln = np.zeros((x.shape[0], x.shape[1]+1, nmax), dtype = np.complex128)
  79. cdef np.ndarray[np.complex128_t, ndim = 3] cln = np.zeros((x.shape[0], x.shape[1]+1, nmax), dtype = np.complex128)
  80. cdef np.ndarray[np.complex128_t, ndim = 3] dln = np.zeros((x.shape[0], x.shape[1]+1, nmax), dtype = np.complex128)
  81. cdef np.ndarray[np.float64_t, ndim = 1] alnr
  82. cdef np.ndarray[np.float64_t, ndim = 1] alni
  83. cdef np.ndarray[np.float64_t, ndim = 1] blnr
  84. cdef np.ndarray[np.float64_t, ndim = 1] blni
  85. cdef np.ndarray[np.float64_t, ndim = 1] clnr
  86. cdef np.ndarray[np.float64_t, ndim = 1] clni
  87. cdef np.ndarray[np.float64_t, ndim = 1] dlnr
  88. cdef np.ndarray[np.float64_t, ndim = 1] dlni
  89. for i in range(x.shape[0]):
  90. alnr = np.zeros((x.shape[1]+1)*nmax, dtype = np.float64)
  91. alni = np.zeros((x.shape[1]+1)*nmax, dtype = np.float64)
  92. blnr = np.zeros((x.shape[1]+1)*nmax, dtype = np.float64)
  93. blni = np.zeros((x.shape[1]+1)*nmax, dtype = np.float64)
  94. clnr = np.zeros((x.shape[1]+1)*nmax, dtype = np.float64)
  95. clni = np.zeros((x.shape[1]+1)*nmax, dtype = np.float64)
  96. dlnr = np.zeros((x.shape[1]+1)*nmax, dtype = np.float64)
  97. dlni = np.zeros((x.shape[1]+1)*nmax, dtype = np.float64)
  98. #terms[i] = ExpansionCoeffs(x.shape[1], pl, x[i].copy('C'), m[i].copy('C'), nmax, alnr, alni, blnr, blni, clnr, clni, dlnr, dlni)
  99. terms[i] = ExpansionCoeffs(x.shape[1], pl, x[i].copy('C'), m[i].copy('C'), nmax, npy2c(alnr), npy2c(alni), npy2c(blnr), npy2c(blni), npy2c(clnr), npy2c(clni), npy2c(dlnr), npy2c(dlni))
  100. for l in range(x.shape[1]+1):
  101. aln[i][l] = alnr[l*nmax:(l+1)*nmax].copy('C') + 1.0j*alni[l*nmax:(l+1)*nmax].copy('C')
  102. bln[i][l] = blnr[l*nmax:(l+1)*nmax].copy('C') + 1.0j*blni[l*nmax:(l+1)*nmax].copy('C')
  103. cln[i][l] = clnr[l*nmax:(l+1)*nmax].copy('C') + 1.0j*clni[l*nmax:(l+1)*nmax].copy('C')
  104. dln[i][l] = dlnr[l*nmax:(l+1)*nmax].copy('C') + 1.0j*dlni[l*nmax:(l+1)*nmax].copy('C')
  105. return terms, aln, bln, cln, dln
  106. def scattnlay(np.ndarray[np.float64_t, ndim = 2] x, np.ndarray[np.complex128_t, ndim = 2] m, np.ndarray[np.float64_t, ndim = 1] theta = np.zeros(0, dtype = np.float64), np.int_t nmax = -1, np.int_t mode_n = -1, np.int_t mode_type = -1, np.int_t pl = -1):
  107. cdef Py_ssize_t i
  108. cdef np.ndarray[np.int_t, ndim = 1] terms = np.zeros(x.shape[0], dtype = np.int)
  109. cdef np.ndarray[np.float64_t, ndim = 1] Qext = np.zeros(x.shape[0], dtype = np.float64)
  110. cdef np.ndarray[np.float64_t, ndim = 1] Qabs = np.zeros(x.shape[0], dtype = np.float64)
  111. cdef np.ndarray[np.float64_t, ndim = 1] Qsca = np.zeros(x.shape[0], dtype = np.float64)
  112. cdef np.ndarray[np.float64_t, ndim = 1] Qbk = np.zeros(x.shape[0], dtype = np.float64)
  113. cdef np.ndarray[np.float64_t, ndim = 1] Qpr = np.zeros(x.shape[0], dtype = np.float64)
  114. cdef np.ndarray[np.float64_t, ndim = 1] g = np.zeros(x.shape[0], dtype = np.float64)
  115. cdef np.ndarray[np.float64_t, ndim = 1] Albedo = np.zeros(x.shape[0], dtype = np.float64)
  116. cdef np.ndarray[np.complex128_t, ndim = 2] S1 = np.zeros((x.shape[0], theta.shape[0]), dtype = np.complex128)
  117. cdef np.ndarray[np.complex128_t, ndim = 2] S2 = np.zeros((x.shape[0], theta.shape[0]), dtype = np.complex128)
  118. cdef np.ndarray[np.float64_t, ndim = 1] S1r
  119. cdef np.ndarray[np.float64_t, ndim = 1] S1i
  120. cdef np.ndarray[np.float64_t, ndim = 1] S2r
  121. cdef np.ndarray[np.float64_t, ndim = 1] S2i
  122. for i in range(x.shape[0]):
  123. S1r = np.zeros(theta.shape[0], dtype = np.float64)
  124. S1i = np.zeros(theta.shape[0], dtype = np.float64)
  125. S2r = np.zeros(theta.shape[0], dtype = np.float64)
  126. S2i = np.zeros(theta.shape[0], dtype = np.float64)
  127. terms[i] = nMie(x.shape[1], pl, x[i].copy('C'), m[i].copy('C'), theta.shape[0], theta.copy('C'), nmax, &Qext[i], &Qsca[i], &Qabs[i], &Qbk[i], &Qpr[i], &g[i], &Albedo[i], npy2c(S1r), npy2c(S1i), npy2c(S2r), npy2c(S2i), mode_n, mode_type)
  128. S1[i] = S1r.copy('C') + 1.0j*S1i.copy('C')
  129. S2[i] = S2r.copy('C') + 1.0j*S2i.copy('C')
  130. return terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2
  131. def fieldnlay(np.ndarray[np.float64_t, ndim = 2] x, np.ndarray[np.complex128_t, ndim = 2] m, np.ndarray[np.float64_t, ndim = 2] coords, np.int_t nmax = -1, np.int_t mode_n = -1, np.int_t mode_type = -1, np.int_t pl = -1):
  132. cdef Py_ssize_t i
  133. cdef np.ndarray[np.int_t, ndim = 1] terms = np.zeros(x.shape[0], dtype = np.int)
  134. cdef np.ndarray[np.complex128_t, ndim = 3] E = np.zeros((x.shape[0], coords.shape[0], 3), dtype = np.complex128)
  135. cdef np.ndarray[np.complex128_t, ndim = 3] H = np.zeros((x.shape[0], coords.shape[0], 3), dtype = np.complex128)
  136. cdef np.ndarray[np.float64_t, ndim = 1] Erx
  137. cdef np.ndarray[np.float64_t, ndim = 1] Ery
  138. cdef np.ndarray[np.float64_t, ndim = 1] Erz
  139. cdef np.ndarray[np.float64_t, ndim = 1] Eix
  140. cdef np.ndarray[np.float64_t, ndim = 1] Eiy
  141. cdef np.ndarray[np.float64_t, ndim = 1] Eiz
  142. cdef np.ndarray[np.float64_t, ndim = 1] Hrx
  143. cdef np.ndarray[np.float64_t, ndim = 1] Hry
  144. cdef np.ndarray[np.float64_t, ndim = 1] Hrz
  145. cdef np.ndarray[np.float64_t, ndim = 1] Hix
  146. cdef np.ndarray[np.float64_t, ndim = 1] Hiy
  147. cdef np.ndarray[np.float64_t, ndim = 1] Hiz
  148. for i in range(x.shape[0]):
  149. Erx = np.zeros(coords.shape[0], dtype = np.float64)
  150. Ery = np.zeros(coords.shape[0], dtype = np.float64)
  151. Erz = np.zeros(coords.shape[0], dtype = np.float64)
  152. Eix = np.zeros(coords.shape[0], dtype = np.float64)
  153. Eiy = np.zeros(coords.shape[0], dtype = np.float64)
  154. Eiz = np.zeros(coords.shape[0], dtype = np.float64)
  155. Hrx = np.zeros(coords.shape[0], dtype = np.float64)
  156. Hry = np.zeros(coords.shape[0], dtype = np.float64)
  157. Hrz = np.zeros(coords.shape[0], dtype = np.float64)
  158. Hix = np.zeros(coords.shape[0], dtype = np.float64)
  159. Hiy = np.zeros(coords.shape[0], dtype = np.float64)
  160. Hiz = np.zeros(coords.shape[0], dtype = np.float64)
  161. terms[i] = nField(x.shape[1], pl, x[i].copy('C'), m[i].copy('C'), nmax, mode_n, mode_type, coords.shape[0], coords[:, 0].copy('C'), coords[:, 1].copy('C'), coords[:, 2].copy('C'), npy2c(Erx), npy2c(Ery), npy2c(Erz), npy2c(Eix), npy2c(Eiy), npy2c(Eiz), npy2c(Hrx), npy2c(Hry), npy2c(Hrz), npy2c(Hix), npy2c(Hiy), npy2c(Hiz))
  162. E[i] = np.vstack((Erx.copy('C') + 1.0j*Eix.copy('C'), Ery.copy('C') + 1.0j*Eiy.copy('C'), Erz.copy('C') + 1.0j*Eiz.copy('C'))).transpose()
  163. H[i] = np.vstack((Hrx.copy('C') + 1.0j*Hix.copy('C'), Hry.copy('C') + 1.0j*Hiy.copy('C'), Hrz.copy('C') + 1.0j*Hiz.copy('C'))).transpose()
  164. return terms, E, H