main.py 14 KB

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