main.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #
  4. # Copyright (C) 2009-2021 Ovidio Peña Rodríguez <ovidio@bytesfall.com>
  5. # Copyright (C) 2013-2021 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. mie_mp = None
  34. try:
  35. from scattnlay_mp import mie_mp as mie_mp_
  36. mie_mp = mie_mp_()
  37. from scattnlay_dp import mie_dp
  38. mie = mie_dp()
  39. def scattcoeffs_(x, m, nmax=-1, pl=-1, mp=False):
  40. if mp and mie_mp:
  41. from scattnlay_mp import mie_mp as mie_
  42. else:
  43. from scattnlay_dp import mie_dp as mie_
  44. # from scattnlay_mp import mie_mp as mie_
  45. mie = mie_()
  46. mie.SetLayersSize(x)
  47. mie.SetLayersIndex(m)
  48. mie.SetPECLayer(pl)
  49. mie.SetMaxTerms(nmax)
  50. mie.calcScattCoeffs()
  51. terms = mie.GetMaxTerms()
  52. a = mie.GetAn()
  53. b = mie.GetBn()
  54. return terms, a, b
  55. def scattcoeffs(x, m, nmax=-1, pl=-1, mp=False):
  56. """
  57. scattcoeffs(x, m[, nmax, pl, mp])
  58. Calculate the scattering coefficients required to calculate both the
  59. near- and far-field parameters.
  60. x: Size parameters (1D or 2D ndarray)
  61. m: Relative refractive indices (1D or 2D ndarray)
  62. nmax: Maximum number of multipolar expansion terms to be used for the
  63. calculations. Only use it if you know what you are doing, otherwise
  64. set this parameter to -1 and the function will calculate it.
  65. pl: Index of PEC layer. If there is none just send -1.
  66. mp: Use multiple (True) or double (False) precision.
  67. Returns: (terms, an, bn)
  68. with
  69. terms: Number of multipolar expansion terms used for the calculations
  70. an, bn: Complex scattering coefficients
  71. """
  72. if len(m.shape) != 1 and len(m.shape) != 2:
  73. raise ValueError('The relative refractive index (m) should be a 1-D or 2-D NumPy array.')
  74. if len(x.shape) == 1:
  75. if len(m.shape) == 1:
  76. return scattcoeffs_(x, m, nmax=nmax, pl=pl, mp=mp)
  77. else:
  78. raise ValueError('The number of of dimensions for the relative refractive index (m) and for the size parameter (x) must be equal.')
  79. elif len(x.shape) != 2:
  80. raise ValueError('The size parameter (x) should be a 1-D or 2-D NumPy array.')
  81. # Repeat the same m for all wavelengths
  82. if len(m.shape) == 1:
  83. m = np.repeat(m[np.newaxis, :], x.shape[0], axis=0)
  84. if nmax == -1:
  85. nstore = 0
  86. else:
  87. nstore = nmax
  88. terms = np.zeros((x.shape[0]), dtype=int)
  89. an = np.zeros((0, nstore), dtype=complex)
  90. bn = np.zeros((0, nstore), dtype=complex)
  91. for i, xi in enumerate(x):
  92. terms[i], a, b = scattcoeffs_(xi, m[i], nmax=nmax, pl=pl, mp=mp)
  93. if terms[i] > nstore:
  94. nstore = terms[i]
  95. an.resize((an.shape[0], nstore))
  96. bn.resize((bn.shape[0], nstore))
  97. an = np.vstack((an, a))
  98. bn = np.vstack((bn, b))
  99. return terms, an, bn
  100. def expancoeffs_(x, m, nmax=-1, pl=-1, mp=False):
  101. if mp and mie_mp:
  102. from scattnlay_mp import mie_mp as mie_
  103. else:
  104. from scattnlay_dp import mie_dp as mie_
  105. # from scattnlay_mp import mie_mp as mie_
  106. mie = mie_()
  107. mie.SetLayersSize(x)
  108. mie.SetLayersIndex(m)
  109. mie.SetPECLayer(pl)
  110. mie.SetMaxTerms(nmax)
  111. mie.calcScattCoeffs()
  112. mie.calcExpanCoeffs()
  113. terms = mie.GetMaxTerms()
  114. an = mie.GetLayerAn()
  115. bn = mie.GetLayerBn()
  116. cn = mie.GetLayerCn()
  117. dn = mie.GetLayerDn()
  118. return terms, an, bn, cn, dn
  119. # TODO verify that expancoeffs() is really working
  120. def expancoeffs(x, m, nmax=-1, pl=-1, mp=False):
  121. """
  122. expancoeffs(x, m[, nmax, pl, mp])
  123. Calculate the scattering coefficients required to calculate both the
  124. near- and far-field parameters.
  125. x: Size parameters (1D or 2D ndarray)
  126. m: Relative refractive indices (1D or 2D ndarray)
  127. nmax: Maximum number of multipolar expansion terms to be used for the
  128. calculations. Only use it if you know what you are doing, otherwise
  129. set this parameter to -1 and the function will calculate it.
  130. pl: Index of PEC layer. If there is none just send -1.
  131. mp: Use multiple (True) or double (False) precision.
  132. Returns: (terms, an, bn, cn, dn)
  133. with
  134. terms: Number of multipolar expansion terms used for the calculations
  135. an, bn, cn, dn: Complex expansion coefficients of each layer
  136. """
  137. if len(m.shape) != 1 and len(m.shape) != 2:
  138. raise ValueError('The relative refractive index (m) should be a 1-D or 2-D NumPy array.')
  139. if len(x.shape) == 1:
  140. if len(m.shape) == 1:
  141. return expancoeffs_(x, m, nmax=nmax, pl=pl, mp=mp)
  142. else:
  143. raise ValueError('The number of of dimensions for the relative refractive index (m) and for the size parameter (x) must be equal.')
  144. elif len(x.shape) != 2:
  145. raise ValueError('The size parameter (x) should be a 1-D or 2-D NumPy array.')
  146. # Repeat the same m for all wavelengths
  147. if len(m.shape) == 1:
  148. m = np.repeat(m[np.newaxis, :], x.shape[0], axis=0)
  149. if nmax == -1:
  150. nstore = 0
  151. else:
  152. nstore = nmax
  153. terms = np.zeros((x.shape[0]), dtype=int)
  154. an = np.zeros((0, x.shape[1]+1, nstore), dtype=complex)
  155. bn = np.zeros((0, x.shape[1]+1, nstore), dtype=complex)
  156. cn = np.zeros((0, x.shape[1]+1, nstore), dtype=complex)
  157. dn = np.zeros((0, x.shape[1]+1, nstore), dtype=complex)
  158. for i, xi in enumerate(x):
  159. terms[i], a, b, c, d = expancoeffs_(xi, m[i], nmax=nmax, pl=pl, mp=mp)
  160. if terms[i] > nstore:
  161. nstore = terms[i]
  162. an.resize((an.shape[0], an.shape[1], nstore))
  163. bn.resize((bn.shape[0], bn.shape[1], nstore))
  164. cn.resize((cn.shape[0], cn.shape[1], nstore))
  165. dn.resize((dn.shape[0], dn.shape[1], nstore))
  166. an = np.vstack((an, [a]))
  167. bn = np.vstack((bn, [b]))
  168. cn = np.vstack((cn, [c]))
  169. dn = np.vstack((dn, [d]))
  170. return terms, an, bn, cn, dn
  171. def scattnlay_(x, m, theta=np.zeros(0, dtype=float), nmax=-1, pl=-1, mp=False):
  172. if mp and mie_mp:
  173. from scattnlay_mp import mie_mp as mie_
  174. else:
  175. from scattnlay_dp import mie_dp as mie_
  176. mie = mie_()
  177. mie.SetLayersSize(x)
  178. mie.SetLayersIndex(m)
  179. mie.SetAngles(theta)
  180. mie.SetPECLayer(pl)
  181. mie.SetMaxTerms(nmax)
  182. mie.RunMieCalculation()
  183. Qext = mie.GetQext()
  184. Qsca = mie.GetQsca()
  185. Qabs = mie.GetQabs()
  186. Qbk = mie.GetQbk()
  187. Qpr = mie.GetQpr()
  188. g = mie.GetAsymmetryFactor()
  189. Albedo = mie.GetAlbedo()
  190. terms = mie.GetMaxTerms()
  191. S1 = mie.GetS1()
  192. S2 = mie.GetS2()
  193. return terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2
  194. def scattnlay(x, m, theta=np.zeros(0, dtype=float), nmax=-1, pl=-1, mp=False):
  195. """
  196. scattnlay(x, m[, theta, nmax, pl, mp])
  197. Calculate the actual scattering parameters and amplitudes.
  198. x: Size parameters (1D or 2D ndarray)
  199. m: Relative refractive indices (1D or 2D ndarray)
  200. theta: Scattering angles where the scattering amplitudes will be
  201. calculated (optional, 1D ndarray)
  202. nmax: Maximum number of multipolar expansion terms to be used for the
  203. calculations. Only use it if you know what you are doing.
  204. pl: Index of PEC layer. If there is none just send -1.
  205. mp: Use multiple (True) or double (False) precision.
  206. Returns: (terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2)
  207. with
  208. terms: Number of multipolar expansion terms used for the calculations
  209. Qext: Efficiency factor for extinction
  210. Qsca: Efficiency factor for scattering
  211. Qabs: Efficiency factor for absorption (Qabs = Qext - Qsca)
  212. Qbk: Efficiency factor for backscattering
  213. Qpr: Efficiency factor for the radiation pressure
  214. g: Asymmetry factor (g = (Qext-Qpr)/Qsca)
  215. Albedo: Single scattering albedo (Albedo = Qsca/Qext)
  216. S1, S2: Complex scattering amplitudes
  217. """
  218. if len(m.shape) != 1 and len(m.shape) != 2:
  219. raise ValueError('The relative refractive index (m) should be a 1-D or 2-D NumPy array.')
  220. if len(x.shape) == 1:
  221. if len(m.shape) == 1:
  222. return scattnlay_(x, m, theta, nmax=nmax, pl=pl, mp=mp)
  223. else:
  224. raise ValueError('The number of of dimensions for the relative refractive index (m) and for the size parameter (x) must be equal.')
  225. elif len(x.shape) != 2:
  226. raise ValueError('The size parameter (x) should be a 1-D or 2-D NumPy array.')
  227. if len(theta.shape) != 1:
  228. raise ValueError('The scattering angles (theta) should be a 1-D NumPy array.')
  229. # Repeat the same m for all wavelengths
  230. if len(m.shape) == 1:
  231. m = np.repeat(m[np.newaxis, :], x.shape[0], axis=0)
  232. terms = np.zeros((x.shape[0]), dtype=int)
  233. Qext = np.zeros((x.shape[0]), dtype=float)
  234. Qsca = np.zeros((x.shape[0]), dtype=float)
  235. Qabs = np.zeros((x.shape[0]), dtype=float)
  236. Qbk = np.zeros((x.shape[0]), dtype=float)
  237. Qpr = np.zeros((x.shape[0]), dtype=float)
  238. g = np.zeros((x.shape[0]), dtype=float)
  239. Albedo = np.zeros((x.shape[0]), dtype=float)
  240. S1 = np.zeros((x.shape[0], theta.shape[0]), dtype=complex)
  241. S2 = np.zeros((x.shape[0], theta.shape[0]), dtype=complex)
  242. for i, xi in enumerate(x):
  243. 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, mp=mp)
  244. return terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2
  245. def fieldnlay_(x, m, xp, yp, zp, nmax=-1, pl=-1, mp=False):
  246. if mp and mie_mp:
  247. from scattnlay_mp import mie_mp as mie_
  248. else:
  249. from scattnlay_dp import mie_dp as mie_
  250. # from scattnlay_mp import mie_mp as mie_
  251. mie = mie_()
  252. mie.SetLayersSize(x)
  253. mie.SetLayersIndex(m)
  254. mie.SetPECLayer(pl)
  255. mie.SetMaxTerms(nmax)
  256. mie.SetFieldCoords(xp, yp, zp)
  257. mie.RunFieldCalculation()
  258. terms = mie.GetMaxTerms()
  259. E = mie.GetFieldE()
  260. H = mie.GetFieldH()
  261. return terms, E, H
  262. def fieldnlay(x, m, xp, yp, zp, nmax=-1, pl=-1, mp=False):
  263. """
  264. fieldnlay(x, m, xp, yp, zp[, nmax, pl, mp])
  265. Calculate the actual scattering parameters and amplitudes.
  266. x: Size parameters (1D or 2D ndarray)
  267. m: Relative refractive indices (1D or 2D ndarray)
  268. xp: Array containing all X coordinates to calculate the complex
  269. electric and magnetic fields (1D* ndarray)
  270. yp: Array containing all Y coordinates to calculate the complex
  271. electric and magnetic fields (1D* ndarray)
  272. zp: Array containing all Z coordinates to calculate the complex
  273. electric and magnetic fields (1D* ndarray)
  274. nmax: Maximum number of multipolar expansion terms to be used for the
  275. calculations. Only use it if you know what you are doing.
  276. pl: Index of PEC layer. If there is none just send -1.
  277. mp: Use multiple (True) or double (False) precision.
  278. Returns: (terms, E, H)
  279. with
  280. terms: Number of multipolar expansion terms used for the calculations
  281. E, H: Complex electric and magnetic field at the provided coordinates
  282. *Note: We assume that the coordinates are referred to the first wavelength
  283. (or structure) and correct it for the following ones
  284. """
  285. if len(m.shape) != 1 and len(m.shape) != 2:
  286. raise ValueError('The relative refractive index (m) should be a 1-D or 2-D NumPy array.')
  287. if len(x.shape) == 1:
  288. if len(m.shape) == 1:
  289. return fieldnlay_(x, m, xp, yp, zp, nmax=nmax, pl=pl, mp=mp)
  290. else:
  291. raise ValueError('The number of of dimensions for the relative refractive index (m) and for the size parameter (x) must be equal.')
  292. elif len(x.shape) != 2:
  293. raise ValueError('The size parameter (x) should be a 1-D or 2-D NumPy array.')
  294. # Repeat the same m for all wavelengths
  295. if len(m.shape) == 1:
  296. m = np.repeat(m[np.newaxis, :], x.shape[0], axis=0)
  297. terms = np.zeros((x.shape[0]), dtype=int)
  298. E = np.zeros((x.shape[0], xp.shape[0], 3), dtype=complex)
  299. H = np.zeros((x.shape[0], xp.shape[0], 3), dtype=complex)
  300. for i, xi in enumerate(x):
  301. # (2020/05/12) We assume that the coordinates are referred to the first wavelength
  302. # (or structure) and correct it for the following ones
  303. 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, mp=mp)
  304. return terms, E, H