main.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #
  4. # Copyright (C) 2009-2019 Ovidio Peña Rodríguez <ovidio@bytesfall.com>
  5. # Copyright (C) 2013-2019 Konstantin Ladutenko <kostyfisik@gmail.com>
  6. #
  7. # This file is part of scattnlay
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # The only additional remark is that we expect that all publications
  20. # describing work using this software, or all commercial products
  21. # using it, cite at least one of the following references:
  22. # [1] O. Peña and U. Pal, "Scattering of electromagnetic radiation by
  23. # a multilayered sphere," Computer Physics Communications,
  24. # vol. 180, Nov. 2009, pp. 2348-2354.
  25. # [2] K. Ladutenko, U. Pal, A. Rivera, and O. Peña-Rodríguez, "Mie
  26. # calculation of electromagnetic near-field for a multilayered
  27. # sphere," Computer Physics Communications, vol. 214, May 2017,
  28. # pp. 225-230.
  29. #
  30. # You should have received a copy of the GNU General Public License
  31. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. import numpy as np
  33. def scattcoeffs(x, m, nmax=-1, pl=-1, mp=False):
  34. """
  35. scattcoeffs(x, m[, nmax, pl, mp])
  36. Calculate the scattering coefficients required to calculate both the
  37. near- and far-field parameters.
  38. x: Size parameters (1D or 2D ndarray)
  39. m: Relative refractive indices (1D or 2D ndarray)
  40. nmax: Maximum number of multipolar expansion terms to be used for the
  41. calculations. Only use it if you know what you are doing, otherwise
  42. set this parameter to -1 and the function will calculate it.
  43. pl: Index of PEC layer. If there is none just send -1.
  44. mp: Use multiple (True) or double (False) precision.
  45. Returns: (terms, an, bn)
  46. with
  47. terms: Number of multipolar expansion terms used for the calculations
  48. an, bn: Complex scattering coefficients
  49. """
  50. if mp:
  51. from scattnlay_mp import scattcoeffs as scattcoeffs_
  52. else:
  53. from scattnlay_dp import scattcoeffs as scattcoeffs_
  54. if len(m.shape) != 1 and len(m.shape) != 2:
  55. raise ValueError('The relative refractive index (m) should be a 1-D or 2-D NumPy array.')
  56. if len(x.shape) == 1:
  57. if len(m.shape) == 1:
  58. return scattcoeffs_(x, m, nmax=nmax, pl=pl)
  59. else:
  60. raise ValueError('The number of of dimensions for the relative refractive index (m) and for the size parameter (x) must be equal.')
  61. elif len(x.shape) != 2:
  62. raise ValueError('The size parameter (x) should be a 1-D or 2-D NumPy array.')
  63. # Repeat the same m for all wavelengths
  64. if len(m.shape) == 1:
  65. m = np.repeat(m[np.newaxis, :], x.shape[0], axis=0)
  66. if nmax == -1:
  67. nstore = 0
  68. else:
  69. nstore = nmax
  70. terms = np.zeros((x.shape[0]), dtype=int)
  71. an = np.zeros((0, nstore), dtype=complex)
  72. bn = np.zeros((0, nstore), dtype=complex)
  73. for i, xi in enumerate(x):
  74. terms[i], a, b = scattcoeffs_(xi, m[i], nmax=nmax, pl=pl)
  75. if terms[i] > nstore:
  76. nstore = terms[i]
  77. an.resize((an.shape[0], nstore))
  78. bn.resize((bn.shape[0], nstore))
  79. an = np.vstack((an, a))
  80. bn = np.vstack((bn, b))
  81. return terms, an, bn
  82. #scattcoeffs()
  83. def expancoeffs(x, m, nmax=-1, pl=-1, mp=False):
  84. """
  85. expancoeffs(x, m[, nmax, pl, mp])
  86. Calculate the scattering coefficients required to calculate both the
  87. near- and far-field parameters.
  88. x: Size parameters (1D or 2D ndarray)
  89. m: Relative refractive indices (1D or 2D ndarray)
  90. nmax: Maximum number of multipolar expansion terms to be used for the
  91. calculations. Only use it if you know what you are doing, otherwise
  92. set this parameter to -1 and the function will calculate it.
  93. pl: Index of PEC layer. If there is none just send -1.
  94. mp: Use multiple (True) or double (False) precision.
  95. Returns: (terms, an, bn, cn, dn)
  96. with
  97. terms: Number of multipolar expansion terms used for the calculations
  98. an, bn, cn, dn: Complex expansion coefficients of each layer
  99. """
  100. if mp:
  101. from scattnlay_mp import expancoeffs as expancoeffs_
  102. else:
  103. from scattnlay_dp import expancoeffs as expancoeffs_
  104. if len(m.shape) != 1 and len(m.shape) != 2:
  105. raise ValueError('The relative refractive index (m) should be a 1-D or 2-D NumPy array.')
  106. if len(x.shape) == 1:
  107. if len(m.shape) == 1:
  108. return expancoeffs_(x, m, nmax=nmax, pl=pl)
  109. else:
  110. raise ValueError('The number of of dimensions for the relative refractive index (m) and for the size parameter (x) must be equal.')
  111. elif len(x.shape) != 2:
  112. raise ValueError('The size parameter (x) should be a 1-D or 2-D NumPy array.')
  113. # Repeat the same m for all wavelengths
  114. if len(m.shape) == 1:
  115. m = np.repeat(m[np.newaxis, :], x.shape[0], axis=0)
  116. if nmax == -1:
  117. nstore = 0
  118. else:
  119. nstore = nmax
  120. terms = np.zeros((x.shape[0]), dtype=int)
  121. an = np.zeros((0, x.shape[1], nstore), dtype=complex)
  122. bn = np.zeros((0, x.shape[1], nstore), dtype=complex)
  123. cn = np.zeros((0, x.shape[1], nstore), dtype=complex)
  124. dn = np.zeros((0, x.shape[1], nstore), dtype=complex)
  125. for i, xi in enumerate(x):
  126. terms[i], a, b, c, d = expancoeffs_(xi, m[i], nmax=nmax, pl=pl)
  127. if terms[i] > nstore:
  128. nstore = terms[i]
  129. an.resize((an.shape[0], an.shape[1], nstore))
  130. bn.resize((bn.shape[0], bn.shape[1], nstore))
  131. cn.resize((cn.shape[0], cn.shape[1], nstore))
  132. dn.resize((dn.shape[0], dn.shape[1], nstore))
  133. an = np.vstack((an, a))
  134. bn = np.vstack((bn, b))
  135. cn = np.vstack((cn, c))
  136. dn = np.vstack((dn, d))
  137. return terms, an, bn, cn, dn
  138. #expancoeffs()
  139. def scattnlay(x, m, theta=np.zeros(0, dtype=float), nmax=-1, pl=-1, mp=False):
  140. """
  141. scattnlay(x, m[, theta, nmax, pl, mp])
  142. Calculate the actual scattering parameters and amplitudes.
  143. x: Size parameters (1D or 2D ndarray)
  144. m: Relative refractive indices (1D or 2D ndarray)
  145. theta: Scattering angles where the scattering amplitudes will be
  146. calculated (optional, 1D ndarray)
  147. nmax: Maximum number of multipolar expansion terms to be used for the
  148. calculations. Only use it if you know what you are doing.
  149. pl: Index of PEC layer. If there is none just send -1.
  150. mp: Use multiple (True) or double (False) precision.
  151. Returns: (terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2)
  152. with
  153. terms: Number of multipolar expansion terms used for the calculations
  154. Qext: Efficiency factor for extinction
  155. Qsca: Efficiency factor for scattering
  156. Qabs: Efficiency factor for absorption (Qabs = Qext - Qsca)
  157. Qbk: Efficiency factor for backscattering
  158. Qpr: Efficiency factor for the radiation pressure
  159. g: Asymmetry factor (g = (Qext-Qpr)/Qsca)
  160. Albedo: Single scattering albedo (Albedo = Qsca/Qext)
  161. S1, S2: Complex scattering amplitudes
  162. """
  163. if mp:
  164. from scattnlay_mp import scattnlay as scattnlay_
  165. else:
  166. from scattnlay_dp import scattnlay as scattnlay_
  167. if len(m.shape) != 1 and len(m.shape) != 2:
  168. raise ValueError('The relative refractive index (m) should be a 1-D or 2-D NumPy array.')
  169. if len(x.shape) == 1:
  170. if len(m.shape) == 1:
  171. return scattnlay_(x, m, theta, nmax=nmax, pl=pl)
  172. else:
  173. raise ValueError('The number of of dimensions for the relative refractive index (m) and for the size parameter (x) must be equal.')
  174. elif len(x.shape) != 2:
  175. raise ValueError('The size parameter (x) should be a 1-D or 2-D NumPy array.')
  176. if len(theta.shape) != 1:
  177. raise ValueError('The scattering angles (theta) should be a 1-D NumPy array.')
  178. # Repeat the same m for all wavelengths
  179. if len(m.shape) == 1:
  180. m = np.repeat(m[np.newaxis, :], x.shape[0], axis=0)
  181. terms = np.zeros((x.shape[0]), dtype=int)
  182. Qext = np.zeros((x.shape[0]), dtype=float)
  183. Qsca = np.zeros((x.shape[0]), dtype=float)
  184. Qabs = np.zeros((x.shape[0]), dtype=float)
  185. Qbk = np.zeros((x.shape[0]), dtype=float)
  186. Qpr = np.zeros((x.shape[0]), dtype=float)
  187. g = np.zeros((x.shape[0]), dtype=float)
  188. Albedo = np.zeros((x.shape[0]), dtype=float)
  189. S1 = np.zeros((x.shape[0], theta.shape[0]), dtype=complex)
  190. S2 = np.zeros((x.shape[0], theta.shape[0]), dtype=complex)
  191. for i, xi in enumerate(x):
  192. terms[i], Qext[i], Qsca[i], Qabs[i], Qbk[i], Qpr[i], g[i], Albedo[i], S1[i], S2[i] = scattnlay_(xi, m[i], theta, nmax=nmax, pl=pl)
  193. return terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2
  194. #scattnlay()
  195. def fieldnlay(x, m, xp, yp, zp, nmax=-1, pl=-1, mp=False):
  196. """
  197. fieldnlay(x, m, xp, yp, zp[, nmax, pl, mp])
  198. Calculate the actual scattering parameters and amplitudes.
  199. x: Size parameters (1D or 2D ndarray)
  200. m: Relative refractive indices (1D or 2D ndarray)
  201. xp: Array containing all X coordinates to calculate the complex
  202. electric and magnetic fields (1D* ndarray)
  203. yp: Array containing all Y coordinates to calculate the complex
  204. electric and magnetic fields (1D* ndarray)
  205. zp: Array containing all Z coordinates to calculate the complex
  206. electric and magnetic fields (1D* ndarray)
  207. nmax: Maximum number of multipolar expansion terms to be used for the
  208. calculations. Only use it if you know what you are doing.
  209. pl: Index of PEC layer. If there is none just send -1.
  210. mp: Use multiple (True) or double (False) precision.
  211. Returns: (terms, E, H)
  212. with
  213. terms: Number of multipolar expansion terms used for the calculations
  214. E, H: Complex electric and magnetic field at the provided coordinates
  215. *Note: We assume that the coordinates are referred to the first wavelength
  216. (or structure) and correct it for the following ones
  217. """
  218. if mp:
  219. from scattnlay_mp import fieldnlay as fieldnlay_
  220. else:
  221. from scattnlay_dp import fieldnlay as fieldnlay_
  222. if len(m.shape) != 1 and len(m.shape) != 2:
  223. raise ValueError('The relative refractive index (m) should be a 1-D or 2-D NumPy array.')
  224. if len(x.shape) == 1:
  225. if len(m.shape) == 1:
  226. return fieldnlay_(x, m, xp, yp, zp, nmax=nmax, pl=pl)
  227. else:
  228. raise ValueError('The number of of dimensions for the relative refractive index (m) and for the size parameter (x) must be equal.')
  229. elif len(x.shape) != 2:
  230. raise ValueError('The size parameter (x) should be a 1-D or 2-D NumPy array.')
  231. # Repeat the same m for all wavelengths
  232. if len(m.shape) == 1:
  233. m = np.repeat(m[np.newaxis, :], x.shape[0], axis=0)
  234. terms = np.zeros((x.shape[0]), dtype=int)
  235. E = np.zeros((x.shape[0], xp.shape[0], 3), dtype=complex)
  236. H = np.zeros((x.shape[0], xp.shape[0], 3), dtype=complex)
  237. for i, xi in enumerate(x):
  238. # (2020/05/12) We assume that the coordinates are referred to the first wavelength
  239. # (or structure) and correct it for the following ones
  240. terms[i], E[i], H[i] = fieldnlay_(xi, m[i], xp*xi[-1]/x[0, -1], yp*xi[-1]/x[0, -1], zp*xi[-1]/x[0, -1], nmax=nmax, pl=pl)
  241. return terms, E, H
  242. #fieldnlay()