miepython 3.3.0__py3-none-any.whl → 3.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- miepython/__init__.py +24 -4
- miepython/_convention.py +109 -0
- miepython/core.py +122 -12
- miepython/field.py +71 -31
- miepython/mie_jit.py +31 -19
- miepython/mie_nojit.py +29 -20
- miepython/monte_carlo.py +128 -86
- miepython/rayleigh.py +196 -21
- miepython/vsh.py +21 -6
- {miepython-3.3.0.dist-info → miepython-3.4.0.dist-info}/METADATA +7 -9
- miepython-3.4.0.dist-info/RECORD +19 -0
- {miepython-3.3.0.dist-info → miepython-3.4.0.dist-info}/WHEEL +1 -1
- miepython-3.3.0.dist-info/RECORD +0 -18
- {miepython-3.3.0.dist-info → miepython-3.4.0.dist-info}/licenses/LICENSE.txt +0 -0
- {miepython-3.3.0.dist-info → miepython-3.4.0.dist-info}/top_level.txt +0 -0
miepython/__init__.py
CHANGED
|
@@ -22,7 +22,7 @@ If the size parameter is known, then use::
|
|
|
22
22
|
|
|
23
23
|
mie.efficiencies_mx(m, x)
|
|
24
24
|
|
|
25
|
-
Mie scattering amplitudes S1 and S2 (complex numbers)
|
|
25
|
+
Mie scattering amplitudes S1 and S2 (complex numbers)::
|
|
26
26
|
|
|
27
27
|
mie.S1_S2(m, x, mu)
|
|
28
28
|
|
|
@@ -38,10 +38,25 @@ Mie scattering intensities normalized to one when integrated over all angles::
|
|
|
38
38
|
mie.i_par(m, x, mu, norm='one')
|
|
39
39
|
mie.i_unpolarized(m, x, mu, norm='one')
|
|
40
40
|
|
|
41
|
-
The scattering matrix
|
|
41
|
+
The scattering matrix::
|
|
42
42
|
|
|
43
43
|
mie.phase_matrix(m, x, mu)
|
|
44
44
|
|
|
45
|
+
Time convention
|
|
46
|
+
---------------
|
|
47
|
+
|
|
48
|
+
Complex results (coefficients, S1 and S2, fields, the S34 element of the phase
|
|
49
|
+
matrix) depend on whether the time dependence is written exp(+jωt) or exp(-iωt);
|
|
50
|
+
the two are complex conjugates. The default is exp(+jωt), which goes with the
|
|
51
|
+
m = n - ik form of the refractive index and reproduces Wiscombe's MIEV0. Bohren
|
|
52
|
+
& Huffman, Jackson and scattnlay use exp(-iωt)::
|
|
53
|
+
|
|
54
|
+
mie.set_time_convention("-") # for the rest of the session
|
|
55
|
+
with mie.time_convention("-"): # or just for a block
|
|
56
|
+
a, b = mie.coefficients(m, x)
|
|
57
|
+
|
|
58
|
+
Efficiencies, g and intensities are the same either way.
|
|
59
|
+
|
|
45
60
|
Submodules
|
|
46
61
|
----------
|
|
47
62
|
|
|
@@ -56,7 +71,7 @@ the textbook formulas. It is a reference for development and cross-checking rat
|
|
|
56
71
|
than a fast path, it requires the optional ``scipy`` dependency, and so it is bound
|
|
57
72
|
only when scipy is installed::
|
|
58
73
|
|
|
59
|
-
mie.vsh.M_odd(1,
|
|
74
|
+
mie.vsh.M_odd(1, 0.5, 1.0, 1.5, 0.8, 0.7, 0.2)
|
|
60
75
|
|
|
61
76
|
``miepython.monte_carlo`` samples scattering angles from the Mie phase function. It
|
|
62
77
|
imports this package, so importing it from here would be circular; ask for it
|
|
@@ -91,6 +106,8 @@ from ._backend import single_sphere, small_conducting_sphere, small_sphere
|
|
|
91
106
|
from .core import efficiencies, intensities, i_par, i_per, i_unpolarized
|
|
92
107
|
from .core import efficiencies_mx, S1_S2, phase_matrix, coefficients
|
|
93
108
|
|
|
109
|
+
from ._convention import get_time_convention, set_time_convention, time_convention
|
|
110
|
+
|
|
94
111
|
from . import rayleigh
|
|
95
112
|
|
|
96
113
|
_FIELD_IMPORT_ERROR = None
|
|
@@ -148,6 +165,9 @@ else:
|
|
|
148
165
|
# scipy is installed, and a star-import must not depend on that.
|
|
149
166
|
__all__ = (
|
|
150
167
|
"rayleigh",
|
|
168
|
+
"get_time_convention",
|
|
169
|
+
"set_time_convention",
|
|
170
|
+
"time_convention",
|
|
151
171
|
"intensities",
|
|
152
172
|
"i_par",
|
|
153
173
|
"i_per",
|
|
@@ -178,7 +198,7 @@ __all__ = (
|
|
|
178
198
|
"_D_downwards",
|
|
179
199
|
)
|
|
180
200
|
|
|
181
|
-
__version__ = "3.
|
|
201
|
+
__version__ = "3.4.0"
|
|
182
202
|
__author__ = "Scott Prahl"
|
|
183
203
|
__email__ = "scott.prahl@oit.edu"
|
|
184
204
|
__copyright__ = "2017-2026, Scott Prahl"
|
miepython/_convention.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""Time-harmonic sign convention for the complex values miepython returns.
|
|
2
|
+
|
|
3
|
+
A monochromatic field can be written with time dependence exp(+jωt) or
|
|
4
|
+
exp(-iωt). The two descriptions carry the same physics and, for the
|
|
5
|
+
non-magnetic spheres handled here, every complex result in one is the complex
|
|
6
|
+
conjugate of the same result in the other:
|
|
7
|
+
|
|
8
|
+
``"+"`` (the default), exp(+jωt)
|
|
9
|
+
The convention of van de Hulst, Kerker and Wiscombe's MIEV0, natural for the
|
|
10
|
+
m = n - ik form of the refractive index that miepython has always used.
|
|
11
|
+
Outgoing spherical waves go as h_n^(2)(kr), a plane wave travelling along +z
|
|
12
|
+
as exp(-jkz), and ``S1_S2`` reproduces MIEV0's amplitudes directly.
|
|
13
|
+
|
|
14
|
+
``"-"``, exp(-iωt)
|
|
15
|
+
The convention of Bohren & Huffman, Jackson and scattnlay. Outgoing waves go
|
|
16
|
+
as h_n^(1)(kr), a plane wave along +z as exp(+ikz), and the amplitudes,
|
|
17
|
+
coefficients and phase matrix match Bohren & Huffman's equations and tables.
|
|
18
|
+
|
|
19
|
+
Only complex results depend on the choice: the Mie coefficients, S1 and S2, the
|
|
20
|
+
near and far fields, the vector spherical harmonics, and the S34 element of the
|
|
21
|
+
phase matrix (which changes sign, along with the handedness of the Stokes V).
|
|
22
|
+
Efficiencies, g, intensities and the other phase-matrix elements are identical.
|
|
23
|
+
|
|
24
|
+
The refractive index is accepted with either sign of its imaginary part and is
|
|
25
|
+
always taken to be absorbing, whatever convention is active.
|
|
26
|
+
|
|
27
|
+
The setting is global to the process. Library code that needs a particular
|
|
28
|
+
convention should use the context manager, which restores the caller's choice::
|
|
29
|
+
|
|
30
|
+
with miepython.time_convention("-"):
|
|
31
|
+
a, b = miepython.coefficients(m, x)
|
|
32
|
+
|
|
33
|
+
The raw kernels (``an_bn``, ``cn_dn``, ``_S1_S2``, ...) ignore this setting and
|
|
34
|
+
always work in exp(-iωt); the public functions convert on the way out.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
import contextlib
|
|
38
|
+
|
|
39
|
+
import numpy as np
|
|
40
|
+
|
|
41
|
+
__all__ = ("get_time_convention", "set_time_convention", "time_convention")
|
|
42
|
+
|
|
43
|
+
_VALID = ("+", "-")
|
|
44
|
+
_current = "+"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _validate(sign):
|
|
48
|
+
"""Return ``sign`` if it names a convention, else raise ``ValueError``."""
|
|
49
|
+
if not isinstance(sign, str) or sign not in _VALID:
|
|
50
|
+
raise ValueError(f"time convention must be '+' for exp(+jωt) or '-' for exp(-iωt), not {sign!r}")
|
|
51
|
+
return sign
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def get_time_convention():
|
|
55
|
+
"""Return the active time convention.
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
str: ``"+"`` for exp(+jωt) or ``"-"`` for exp(-iωt).
|
|
59
|
+
"""
|
|
60
|
+
return _current
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def set_time_convention(sign):
|
|
64
|
+
"""Choose the time convention for every complex result miepython returns.
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
sign (str): ``"+"`` for exp(+jωt), the default, or ``"-"`` for exp(-iωt).
|
|
68
|
+
|
|
69
|
+
Returns:
|
|
70
|
+
str: The convention that was active before the call, so it can be restored.
|
|
71
|
+
|
|
72
|
+
Raises:
|
|
73
|
+
ValueError: if ``sign`` is neither ``"+"`` nor ``"-"``.
|
|
74
|
+
"""
|
|
75
|
+
global _current # pylint: disable=global-statement
|
|
76
|
+
previous = _current
|
|
77
|
+
_current = _validate(sign)
|
|
78
|
+
return previous
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@contextlib.contextmanager
|
|
82
|
+
def time_convention(sign):
|
|
83
|
+
"""Use a time convention inside a ``with`` block, then restore the previous one.
|
|
84
|
+
|
|
85
|
+
Args:
|
|
86
|
+
sign (str): ``"+"`` for exp(+jωt) or ``"-"`` for exp(-iωt).
|
|
87
|
+
|
|
88
|
+
Yields:
|
|
89
|
+
str: The convention in force inside the block.
|
|
90
|
+
|
|
91
|
+
Raises:
|
|
92
|
+
ValueError: if ``sign`` is neither ``"+"`` nor ``"-"``.
|
|
93
|
+
"""
|
|
94
|
+
previous = set_time_convention(sign)
|
|
95
|
+
try:
|
|
96
|
+
yield sign
|
|
97
|
+
finally:
|
|
98
|
+
set_time_convention(previous)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _convert(value):
|
|
102
|
+
"""Convert between exp(-iωt), used internally, and the active convention.
|
|
103
|
+
|
|
104
|
+
Conjugation is its own inverse, so the same call takes an internal value out
|
|
105
|
+
to the user and a user-supplied value (such as ``abcd``) in.
|
|
106
|
+
"""
|
|
107
|
+
if _current == "+":
|
|
108
|
+
return np.conjugate(value)
|
|
109
|
+
return value
|
miepython/core.py
CHANGED
|
@@ -35,6 +35,7 @@ Normalized Mie scattering intensities for angles mu=cos(theta)::
|
|
|
35
35
|
|
|
36
36
|
import numpy as np
|
|
37
37
|
from ._backend import _S1_S2, an_bn, cn_dn, single_sphere
|
|
38
|
+
from ._convention import _convert
|
|
38
39
|
|
|
39
40
|
# not really needed but included for clarity
|
|
40
41
|
__all__ = (
|
|
@@ -69,6 +70,60 @@ def wiscombe_terms(x):
|
|
|
69
70
|
return int(x + 4.05 * x**0.33333 + 2.0)
|
|
70
71
|
|
|
71
72
|
|
|
73
|
+
def _check_m_x(m, x):
|
|
74
|
+
"""
|
|
75
|
+
Reject an index or size parameter that the kernels would silently misread.
|
|
76
|
+
|
|
77
|
+
The kernels treat any index whose real part fails ``m.real > 0`` as a perfect
|
|
78
|
+
conductor, so a NaN or negative real part gave the answer for a metal sphere
|
|
79
|
+
instead of an error. A NaN or infinite x failed differently on each backend.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
m: refractive index of the sphere, scalar or array
|
|
83
|
+
x: size parameter of the sphere, scalar or array
|
|
84
|
+
|
|
85
|
+
Raises:
|
|
86
|
+
ValueError: if m or x is not finite, x is negative, or m.real is negative
|
|
87
|
+
"""
|
|
88
|
+
m = np.asarray(m)
|
|
89
|
+
x = np.asarray(x)
|
|
90
|
+
if not np.all(np.isfinite(m)):
|
|
91
|
+
raise ValueError(f"refractive index must be finite, got m={m}")
|
|
92
|
+
if np.any(m.real < 0):
|
|
93
|
+
raise ValueError(
|
|
94
|
+
f"real part of the refractive index must not be negative, got m={m}; "
|
|
95
|
+
"use m.real == 0 for a perfect conductor"
|
|
96
|
+
)
|
|
97
|
+
if not np.all(np.isfinite(x)):
|
|
98
|
+
raise ValueError(f"size parameter must be finite, got x={x}")
|
|
99
|
+
if np.any(x < 0):
|
|
100
|
+
raise ValueError(f"size parameter must not be negative, got x={x}")
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _check_one_sphere(m, x):
|
|
104
|
+
"""
|
|
105
|
+
Reject arrays of m or x in functions that describe a single sphere.
|
|
106
|
+
|
|
107
|
+
The angular functions vectorize over mu, so an array of spheres has no
|
|
108
|
+
unambiguous shape to return. Without this check numpy raised "truth value of
|
|
109
|
+
an array is ambiguous" from deep inside.
|
|
110
|
+
|
|
111
|
+
Args:
|
|
112
|
+
m: refractive index of the sphere
|
|
113
|
+
x: size parameter of the sphere
|
|
114
|
+
|
|
115
|
+
Raises:
|
|
116
|
+
ValueError: if m or x is not a scalar
|
|
117
|
+
"""
|
|
118
|
+
if np.ndim(m) > 0 or np.ndim(x) > 0:
|
|
119
|
+
raise ValueError("this function takes one sphere, so m and x must be scalars; loop over spheres for more")
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _scalarize_0d(value):
|
|
123
|
+
"""Pass a zero-dimensional NumPy array to scalar kernels as a scalar."""
|
|
124
|
+
return value.item() if isinstance(value, np.ndarray) and value.ndim == 0 else value
|
|
125
|
+
|
|
126
|
+
|
|
72
127
|
def coefficients(m, x, n_pole=0, internal=False):
|
|
73
128
|
"""
|
|
74
129
|
Computes the Mie coefficients for a sphere.
|
|
@@ -76,18 +131,21 @@ def coefficients(m, x, n_pole=0, internal=False):
|
|
|
76
131
|
This function calculates the Mie coefficients for electromagnetic scattering
|
|
77
132
|
by a sphere. It supports both single values and arrays for the refractive
|
|
78
133
|
index and size parameter. For arrays, the lengths of `m` and `x` must match.
|
|
79
|
-
|
|
134
|
+
Zero-dimensional NumPy arrays are treated as scalar values.
|
|
80
135
|
|
|
81
|
-
If `n_pole > 0`, the function returns
|
|
82
|
-
for each input.
|
|
136
|
+
If `n_pole > 0`, the function returns the coefficients of orders 1 through
|
|
137
|
+
`n_pole` for each input, so the last entry is the order-`n_pole` term. This
|
|
138
|
+
differs from `efficiencies_mx` and `S1_S2`, where a nonzero `n_pole` isolates
|
|
139
|
+
that single order.
|
|
83
140
|
|
|
84
141
|
Args:
|
|
85
142
|
m (complex or array-like): The complex refractive index of the sphere.
|
|
86
143
|
If an array, must match the length of `x`.
|
|
87
144
|
x (float or array-like): The size parameter of the sphere. If an array,
|
|
88
145
|
must match the length of `m`.
|
|
89
|
-
n_pole (int, optional): The
|
|
90
|
-
|
|
146
|
+
n_pole (int, optional): The number of orders to compute, 1 through
|
|
147
|
+
`n_pole`. Defaults to 0, which uses Wiscombe's criterion for the
|
|
148
|
+
number of terms.
|
|
91
149
|
internal (bool, optional): If True then returns Mie coefficients needed to
|
|
92
150
|
calculate fields inside the sphere.
|
|
93
151
|
|
|
@@ -101,8 +159,16 @@ def coefficients(m, x, n_pole=0, internal=False):
|
|
|
101
159
|
Notes:
|
|
102
160
|
- If the imaginary part of the refractive index is positive, it is
|
|
103
161
|
automatically corrected to its conjugate value to ensure a valid input.
|
|
162
|
+
- The coefficients follow the active time convention (see
|
|
163
|
+
``miepython.set_time_convention``). With the default exp(+jωt) they
|
|
164
|
+
are the conjugates of Bohren & Huffman's a_n and b_n; inside
|
|
165
|
+
``miepython.time_convention("-")`` they are Bohren & Huffman's own.
|
|
104
166
|
- Every returned coefficient is a real term; the arrays carry no padding.
|
|
105
167
|
|
|
168
|
+
Raises:
|
|
169
|
+
ValueError: if m or x is not finite, x is negative, or the real part of
|
|
170
|
+
m is negative. A perfect conductor is m.real == 0, not m.real < 0.
|
|
171
|
+
|
|
106
172
|
Examples:
|
|
107
173
|
Compute coefficients for a single sphere:
|
|
108
174
|
|
|
@@ -111,9 +177,9 @@ def coefficients(m, x, n_pole=0, internal=False):
|
|
|
111
177
|
>>> len(a), len(b)
|
|
112
178
|
(3, 3)
|
|
113
179
|
>>> print(f"{a[0]:.6e}")
|
|
114
|
-
3.333700e-05
|
|
180
|
+
3.333700e-05+1.973631e-04j
|
|
115
181
|
>>> print(f"{b[0]:.6e}")
|
|
116
|
-
6.672963e-08
|
|
182
|
+
6.672963e-08+2.754694e-07j
|
|
117
183
|
|
|
118
184
|
Keep only the dipole term for each of several spheres:
|
|
119
185
|
|
|
@@ -121,8 +187,15 @@ def coefficients(m, x, n_pole=0, internal=False):
|
|
|
121
187
|
>>> a.shape
|
|
122
188
|
(2, 1)
|
|
123
189
|
>>> print(f"{a[0, 0]:.6f}")
|
|
124
|
-
0.447946
|
|
190
|
+
0.447946+0.386652j
|
|
125
191
|
"""
|
|
192
|
+
return _convert(_coefficients(m, x, n_pole, internal))
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def _coefficients(m, x, n_pole=0, internal=False):
|
|
196
|
+
"""Return ``coefficients`` in exp(-iωt), whatever the active convention."""
|
|
197
|
+
_check_m_x(m, x)
|
|
198
|
+
m, x = _scalarize_0d(m), _scalarize_0d(x)
|
|
126
199
|
mlen = np.size(m) if np.ndim(m) > 0 else 0
|
|
127
200
|
xlen = np.size(x) if np.ndim(x) > 0 else 0
|
|
128
201
|
|
|
@@ -179,6 +252,7 @@ def efficiencies_mx(m, x, n_pole=0, e_field=True):
|
|
|
179
252
|
Multipole order to compute:
|
|
180
253
|
- If 0 (default), computes contributions from all multipoles.
|
|
181
254
|
- If non-zero, computes contributions from the specified multipole order.
|
|
255
|
+
- A nonzero order beyond ``wiscombe_terms(x)`` raises ValueError.
|
|
182
256
|
e_field (boolean):
|
|
183
257
|
When n_pole>0, selects which multipole of that order contributes:
|
|
184
258
|
True gives the electric multipole a_n, False the magnetic
|
|
@@ -208,6 +282,10 @@ def efficiencies_mx(m, x, n_pole=0, e_field=True):
|
|
|
208
282
|
- This implementation assumes spherical, homogeneous particles in a
|
|
209
283
|
non-absorbing medium.
|
|
210
284
|
|
|
285
|
+
Raises:
|
|
286
|
+
ValueError: if m or x is not finite, x is negative, or the real part of
|
|
287
|
+
m is negative. A perfect conductor is m.real == 0, not m.real < 0.
|
|
288
|
+
|
|
211
289
|
Examples:
|
|
212
290
|
Compute efficiencies for one sphere:
|
|
213
291
|
|
|
@@ -228,6 +306,9 @@ def efficiencies_mx(m, x, n_pole=0, e_field=True):
|
|
|
228
306
|
Use :func:`efficiencies` instead when the diameter and wavelength are
|
|
229
307
|
known rather than the size parameter.
|
|
230
308
|
"""
|
|
309
|
+
_check_m_x(m, x)
|
|
310
|
+
m, x = _scalarize_0d(m), _scalarize_0d(x)
|
|
311
|
+
|
|
231
312
|
# ensure imaginary part of refractive index is negative
|
|
232
313
|
if np.isscalar(m):
|
|
233
314
|
m = np.conj(m) if np.imag(m) > 0 else m
|
|
@@ -289,6 +370,9 @@ def normalization_factor(m, x, norm_str, efficiency_source=_mie_efficiencies):
|
|
|
289
370
|
Returns:
|
|
290
371
|
scaling factor needed for scattering function
|
|
291
372
|
"""
|
|
373
|
+
m, x = _scalarize_0d(m), _scalarize_0d(x)
|
|
374
|
+
if np.imag(m) > 0: # ensure imaginary part of refractive index is negative
|
|
375
|
+
m = np.conj(m)
|
|
292
376
|
factor = None
|
|
293
377
|
norm = norm_str.lower()
|
|
294
378
|
|
|
@@ -299,7 +383,9 @@ def normalization_factor(m, x, norm_str, efficiency_source=_mie_efficiencies):
|
|
|
299
383
|
factor = 1
|
|
300
384
|
|
|
301
385
|
elif norm in ["qsca", "scattering_efficiency"]:
|
|
302
|
-
|
|
386
|
+
# a zero-size sphere has zero amplitudes, so any nonzero factor leaves
|
|
387
|
+
# them zero; x * sqrt(pi) would give 0/0
|
|
388
|
+
factor = x * np.sqrt(np.pi) if x > 0 else 1
|
|
303
389
|
|
|
304
390
|
else:
|
|
305
391
|
qext, qsca, _, _ = efficiency_source(m, x)
|
|
@@ -336,8 +422,9 @@ def S1_S2(m, x, mu, norm="albedo", n_pole=0):
|
|
|
336
422
|
"""
|
|
337
423
|
Calculate the scattering amplitude functions for spheres.
|
|
338
424
|
|
|
339
|
-
|
|
340
|
-
over all 4*pi solid angles
|
|
425
|
+
With the default ``norm='albedo'`` the unpolarized intensity
|
|
426
|
+
``(abs(S1)**2 + abs(S2)**2)/2`` integrated over all 4*pi solid angles equals the
|
|
427
|
+
single-scattering albedo qsca/qext.
|
|
341
428
|
|
|
342
429
|
The normalization is controlled by `norm` and should be one of
|
|
343
430
|
['albedo', 'one', '4pi', 'qext', 'qsca', 'bohren', or 'wiscombe']
|
|
@@ -360,8 +447,25 @@ def S1_S2(m, x, mu, norm="albedo", n_pole=0):
|
|
|
360
447
|
series raise a ValueError.
|
|
361
448
|
|
|
362
449
|
Returns:
|
|
363
|
-
S1, S2: the scattering amplitudes at each angle mu [sr**(-0.5)]
|
|
450
|
+
S1, S2: the scattering amplitudes at each angle mu [sr**(-0.5)]. They
|
|
451
|
+
follow the active time convention: with the default exp(+jωt) they are
|
|
452
|
+
Wiscombe's MIEV0 amplitudes, the conjugates of Bohren & Huffman's, and
|
|
453
|
+
inside ``miepython.time_convention("-")`` they are Bohren & Huffman's.
|
|
454
|
+
|
|
455
|
+
Raises:
|
|
456
|
+
ValueError: if m or x is not finite, x is negative, or the real part of
|
|
457
|
+
m is negative. A perfect conductor is m.real == 0, not m.real < 0.
|
|
458
|
+
Also if m or x is an array, since this describes one sphere.
|
|
364
459
|
"""
|
|
460
|
+
S1, S2 = _S1_S2_normalized(m, x, mu, norm, n_pole)
|
|
461
|
+
return _convert(S1), _convert(S2)
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
def _S1_S2_normalized(m, x, mu, norm="albedo", n_pole=0):
|
|
465
|
+
"""Return ``S1_S2`` in exp(-iωt), whatever the active convention."""
|
|
466
|
+
_check_m_x(m, x)
|
|
467
|
+
_check_one_sphere(m, x)
|
|
468
|
+
m, x = _scalarize_0d(m), _scalarize_0d(x)
|
|
365
469
|
if np.imag(m) > 0: # ensure imaginary part of refractive index is negative
|
|
366
470
|
m = np.conj(m)
|
|
367
471
|
|
|
@@ -395,6 +499,12 @@ def phase_matrix(m, x, mu, norm="albedo", n_pole=0):
|
|
|
395
499
|
Bohren and Huffman, *Absorption and Scattering of Light by Small Particles*,
|
|
396
500
|
JOHN WILEY & SONS, page 112, (1983).
|
|
397
501
|
|
|
502
|
+
S11, S12 and S33 do not depend on the time convention, but S34 does, and so
|
|
503
|
+
does the handedness of the Stokes V it couples to. With exp(-iωt), inside
|
|
504
|
+
``miepython.time_convention("-")``, the matrix is Bohren & Huffman's
|
|
505
|
+
eq. 3.16 with ``p[2, 3] = S34`` and ``p[3, 2] = -S34``. With the default
|
|
506
|
+
exp(+jωt) both elements change sign.
|
|
507
|
+
|
|
398
508
|
The normalization is controlled by `norm` and should be one of
|
|
399
509
|
['albedo', 'one', '4pi', 'qext', 'qsca', 'bohren', or 'wiscombe']
|
|
400
510
|
The normalization describes the integral of the scattering phase
|
miepython/field.py
CHANGED
|
@@ -64,9 +64,15 @@ Reuse precomputed Mie coefficients for repeated field evaluations:
|
|
|
64
64
|
|
|
65
65
|
Conventions
|
|
66
66
|
-----------
|
|
67
|
-
- Phasor time dependence: ``exp(
|
|
67
|
+
- Phasor time dependence: the active time convention, ``exp(+j * omega * t)`` by
|
|
68
|
+
default or ``exp(-i * omega * t)`` inside ``miepython.time_convention("-")``.
|
|
69
|
+
Every field returned here is the complex conjugate of the field in the other
|
|
70
|
+
convention. The calculation itself is done in ``exp(-i * omega * t)`` and
|
|
71
|
+
converted on return, and a caller's ``abcd`` is converted on the way in, so
|
|
72
|
+
``abcd`` must come from ``miepython.coefficients`` under the same convention.
|
|
68
73
|
- Incident plane wave: propagation along ``+z``, ``E`` along ``+x``, ``H`` along ``+y``,
|
|
69
|
-
incident amplitude ``E0 = 1``.
|
|
74
|
+
incident amplitude ``E0 = 1``. It is ``exp(-jkz)`` for exp(+jωt) and
|
|
75
|
+
``exp(+ikz)`` for exp(-iωt).
|
|
70
76
|
- Spherical coordinates: ``theta`` from ``+z`` and ``phi`` from ``+x`` toward ``+y``.
|
|
71
77
|
- ``lambda0`` is vacuum wavelength.
|
|
72
78
|
- ``n_env`` is the surrounding-medium index and ``m_sphere`` is the sphere index;
|
|
@@ -82,7 +88,8 @@ import numpy as np
|
|
|
82
88
|
from scipy.special import factorial2, spherical_jn
|
|
83
89
|
from ._backend import D_calc, pi_tau
|
|
84
90
|
from .bessel import spherical_h1
|
|
85
|
-
from .
|
|
91
|
+
from ._convention import _convert
|
|
92
|
+
from .core import _S1_S2_normalized, _check_m_x, _coefficients, wiscombe_terms
|
|
86
93
|
from .util import cartesian_to_spherical, spherical_vector_to_cartesian
|
|
87
94
|
|
|
88
95
|
__all__ = (
|
|
@@ -208,20 +215,33 @@ def e_far(lambda0, d_sphere, m_sphere, n_env, r, theta, phi):
|
|
|
208
215
|
Returns:
|
|
209
216
|
ndarray: Complex spherical components ``[E_r, E_theta, E_phi]`` with
|
|
210
217
|
shape ``(3, ...)`` following broadcasted input shape.
|
|
218
|
+
|
|
219
|
+
Notes:
|
|
220
|
+
This is Bohren & Huffman eq. 4.74 for a unit x-polarized incident wave,
|
|
221
|
+
``E_theta = S2 cos(phi) exp(ikr)/(-ikr)`` and
|
|
222
|
+
``E_phi = -S1 sin(phi) exp(ikr)/(-ikr)``, evaluated in exp(-iωt) with
|
|
223
|
+
Bohren & Huffman's own S1 and S2 and then converted, like every other
|
|
224
|
+
result, to the active time convention. It is the leading term of
|
|
225
|
+
``e_near(..., include_incident=False)`` and differs from it by O(1/kr).
|
|
211
226
|
"""
|
|
212
227
|
x = np.pi * d_sphere * n_env / lambda0
|
|
213
228
|
m_rel = m_sphere / n_env
|
|
229
|
+
r, theta, phi = np.broadcast_arrays(
|
|
230
|
+
np.asarray(r, dtype=float), np.asarray(theta, dtype=float), np.asarray(phi, dtype=float)
|
|
231
|
+
)
|
|
214
232
|
jkr = 1j * 2 * np.pi * n_env * r / lambda0
|
|
215
233
|
amp = np.exp(jkr) / (-jkr)
|
|
216
234
|
|
|
217
|
-
|
|
235
|
+
# the amplitudes depend on theta alone; evaluate them on the flattened,
|
|
236
|
+
# broadcast grid and restore its shape so every component matches
|
|
237
|
+
S1, S2 = _S1_S2_normalized(m_rel, x, np.cos(theta).ravel(), norm="wiscombe")
|
|
238
|
+
S1 = S1.reshape(theta.shape)
|
|
239
|
+
S2 = S2.reshape(theta.shape)
|
|
218
240
|
|
|
219
241
|
E_r = np.zeros_like(S1, dtype=complex)
|
|
220
242
|
E_theta = S2 * amp * np.cos(phi)
|
|
221
|
-
E_phi = S1 * amp * np.sin(phi)
|
|
222
|
-
|
|
223
|
-
E_phi = np.conjugate(-E_phi)
|
|
224
|
-
return np.array([E_r, E_theta, E_phi])
|
|
243
|
+
E_phi = -S1 * amp * np.sin(phi)
|
|
244
|
+
return _convert(np.array([E_r, E_theta, E_phi]))
|
|
225
245
|
|
|
226
246
|
|
|
227
247
|
def _incident_e_spherical(lambda0, n_env, r, theta, phi, amplitude=1.0):
|
|
@@ -287,6 +307,7 @@ def _coefficients_abcd(lambda0, d_sphere, m_sphere, n_env, n_pole):
|
|
|
287
307
|
"""
|
|
288
308
|
x = np.pi * d_sphere * n_env / lambda0
|
|
289
309
|
m_rel = m_sphere / n_env
|
|
310
|
+
_check_m_x(m_rel, x) # before wiscombe_terms, which cannot take a NaN x
|
|
290
311
|
if n_pole == 0:
|
|
291
312
|
# Wiscombe's criterion truncates the scattered series, which converges
|
|
292
313
|
# faster than the field evaluated right at the surface. Now that an_bn
|
|
@@ -295,10 +316,21 @@ def _coefficients_abcd(lambda0, d_sphere, m_sphere, n_env, n_pole):
|
|
|
295
316
|
# 3.6e-6 with one extra order and 3.2e-6 with two, then flattens out, so
|
|
296
317
|
# two is where it stops being worth more terms.
|
|
297
318
|
n_pole = wiscombe_terms(x) + 2
|
|
298
|
-
a, b, c, d =
|
|
319
|
+
a, b, c, d = _coefficients(m_rel, x, n_pole=n_pole, internal=True)
|
|
299
320
|
return np.array([a, b, c, d])
|
|
300
321
|
|
|
301
322
|
|
|
323
|
+
def _internal_abcd(lambda0, d_sphere, m_sphere, n_env, n_pole, abcd):
|
|
324
|
+
"""Return exp(-iωt) coefficients, computing them or converting the caller's.
|
|
325
|
+
|
|
326
|
+
A caller's ``abcd`` comes from ``miepython.coefficients`` and so is in the
|
|
327
|
+
active convention; everything in this module works in exp(-iωt).
|
|
328
|
+
"""
|
|
329
|
+
if abcd is None:
|
|
330
|
+
return _coefficients_abcd(lambda0, d_sphere, m_sphere, n_env, n_pole)
|
|
331
|
+
return _convert(np.asarray(abcd))
|
|
332
|
+
|
|
333
|
+
|
|
302
334
|
def _near_fields(abcd, lambda0, d_sphere, m_sphere, n_env, r, theta, phi, include_incident, want_e, want_h):
|
|
303
335
|
"""Evaluate the near fields at every requested point.
|
|
304
336
|
|
|
@@ -344,15 +376,17 @@ def _near_fields(abcd, lambda0, d_sphere, m_sphere, n_env, r, theta, phi, includ
|
|
|
344
376
|
e_out = np.empty((3, r_flat.size), dtype=complex) if want_e else None
|
|
345
377
|
h_out = np.empty((3, r_flat.size), dtype=complex) if want_h else None
|
|
346
378
|
|
|
379
|
+
# The coefficient kernels treat either imaginary sign as absorption. Use the
|
|
380
|
+
# same physical index for the interior radial functions and H-field factor.
|
|
381
|
+
m_absorbing = np.conjugate(m_sphere) if np.imag(m_sphere) > 0 else m_sphere
|
|
347
382
|
is_inside = r_flat < d_sphere / 2
|
|
348
383
|
for inside in (True, False):
|
|
349
384
|
group = is_inside if inside else ~is_inside
|
|
350
385
|
if not group.any():
|
|
351
386
|
continue
|
|
352
387
|
|
|
353
|
-
#
|
|
354
|
-
|
|
355
|
-
m_index = np.conjugate(m_sphere) if inside else n_env
|
|
388
|
+
# This module works in exp(-iωt), where an absorbing index is n + ik.
|
|
389
|
+
m_index = np.conjugate(m_absorbing) if inside else n_env
|
|
356
390
|
M_the_base, M_phi_base, N_r_base, N_the_base, N_phi_base = _vsh_components_base(
|
|
357
391
|
n_terms, lambda0, m_index, r_flat[group], theta_flat[group], inside
|
|
358
392
|
)
|
|
@@ -393,7 +427,7 @@ def _near_fields(abcd, lambda0, d_sphere, m_sphere, n_env, r, theta, phi, includ
|
|
|
393
427
|
cos_phi * N_phi_base,
|
|
394
428
|
)
|
|
395
429
|
if inside:
|
|
396
|
-
m_rel =
|
|
430
|
+
m_rel = m_index / n_env
|
|
397
431
|
h_rad = m_rel * _sum_two_scaled_terms(scale, d, M_even_rad, -1.0 + 0.0j, c, N_odd_rad, -1.0j)
|
|
398
432
|
h_the = m_rel * _sum_two_scaled_terms(scale, d, M_even_the, -1.0 + 0.0j, c, N_odd_the, -1.0j)
|
|
399
433
|
h_phi = m_rel * _sum_two_scaled_terms(scale, d, M_even_phi, -1.0 + 0.0j, c, N_odd_phi, -1.0j)
|
|
@@ -444,17 +478,17 @@ def e_near(lambda0, d_sphere, m_sphere, n_env, r, theta, phi, include_incident=T
|
|
|
444
478
|
n_pole (int): Number of multipole terms to keep. ``0`` (the default) keeps
|
|
445
479
|
two more than Wiscombe's criterion, which converges the near field at
|
|
446
480
|
the sphere surface roughly five times closer than the criterion alone.
|
|
447
|
-
abcd (ndarray or None): Optional precomputed coefficients ``[a, b, c, d]
|
|
448
|
-
|
|
481
|
+
abcd (ndarray or None): Optional precomputed coefficients ``[a, b, c, d]``
|
|
482
|
+
from ``miepython.coefficients(..., internal=True)`` under the same time
|
|
483
|
+
convention. If provided, ``n_pole`` is ignored.
|
|
449
484
|
|
|
450
485
|
Returns:
|
|
451
486
|
ndarray: Spherical electric components ``[E_r, E_theta, E_phi]`` with
|
|
452
487
|
shape ``(3, ...)``.
|
|
453
488
|
"""
|
|
454
|
-
|
|
455
|
-
abcd = _coefficients_abcd(lambda0, d_sphere, m_sphere, n_env, n_pole)
|
|
489
|
+
abcd = _internal_abcd(lambda0, d_sphere, m_sphere, n_env, n_pole, abcd)
|
|
456
490
|
|
|
457
|
-
|
|
491
|
+
e_sph = _near_fields(
|
|
458
492
|
abcd,
|
|
459
493
|
lambda0,
|
|
460
494
|
d_sphere,
|
|
@@ -467,6 +501,7 @@ def e_near(lambda0, d_sphere, m_sphere, n_env, r, theta, phi, include_incident=T
|
|
|
467
501
|
want_e=True,
|
|
468
502
|
want_h=False,
|
|
469
503
|
)[0]
|
|
504
|
+
return _convert(e_sph)
|
|
470
505
|
|
|
471
506
|
|
|
472
507
|
def h_near(lambda0, d_sphere, m_sphere, n_env, r, theta, phi, include_incident=True, n_pole=0, abcd=None):
|
|
@@ -484,17 +519,17 @@ def h_near(lambda0, d_sphere, m_sphere, n_env, r, theta, phi, include_incident=T
|
|
|
484
519
|
n_pole (int): Number of multipole terms to keep. ``0`` (the default) keeps
|
|
485
520
|
two more than Wiscombe's criterion, which converges the near field at
|
|
486
521
|
the sphere surface roughly five times closer than the criterion alone.
|
|
487
|
-
abcd (ndarray or None): Optional precomputed coefficients ``[a, b, c, d]
|
|
488
|
-
|
|
522
|
+
abcd (ndarray or None): Optional precomputed coefficients ``[a, b, c, d]``
|
|
523
|
+
from ``miepython.coefficients(..., internal=True)`` under the same time
|
|
524
|
+
convention. If provided, ``n_pole`` is ignored.
|
|
489
525
|
|
|
490
526
|
Returns:
|
|
491
527
|
ndarray: Spherical magnetic components ``[H_r, H_theta, H_phi]`` with
|
|
492
528
|
shape ``(3, ...)``.
|
|
493
529
|
"""
|
|
494
|
-
|
|
495
|
-
abcd = _coefficients_abcd(lambda0, d_sphere, m_sphere, n_env, n_pole)
|
|
530
|
+
abcd = _internal_abcd(lambda0, d_sphere, m_sphere, n_env, n_pole, abcd)
|
|
496
531
|
|
|
497
|
-
|
|
532
|
+
h_sph = _near_fields(
|
|
498
533
|
abcd,
|
|
499
534
|
lambda0,
|
|
500
535
|
d_sphere,
|
|
@@ -507,6 +542,7 @@ def h_near(lambda0, d_sphere, m_sphere, n_env, r, theta, phi, include_incident=T
|
|
|
507
542
|
want_e=False,
|
|
508
543
|
want_h=True,
|
|
509
544
|
)[1]
|
|
545
|
+
return _convert(h_sph)
|
|
510
546
|
|
|
511
547
|
|
|
512
548
|
def eh_near(
|
|
@@ -535,17 +571,17 @@ def eh_near(
|
|
|
535
571
|
n_pole (int): Number of multipole terms to keep. ``0`` (the default) keeps
|
|
536
572
|
two more than Wiscombe's criterion, which converges the near field at
|
|
537
573
|
the sphere surface roughly five times closer than the criterion alone.
|
|
538
|
-
abcd (ndarray or None): Optional precomputed coefficients ``[a, b, c, d]
|
|
539
|
-
|
|
574
|
+
abcd (ndarray or None): Optional precomputed coefficients ``[a, b, c, d]``
|
|
575
|
+
from ``miepython.coefficients(..., internal=True)`` under the same time
|
|
576
|
+
convention. If provided, ``n_pole`` is ignored.
|
|
540
577
|
|
|
541
578
|
Returns:
|
|
542
579
|
tuple[ndarray, ndarray]: Tuple ``(E, H)`` in spherical components,
|
|
543
580
|
each with shape ``(3, ...)``.
|
|
544
581
|
"""
|
|
545
|
-
|
|
546
|
-
abcd = _coefficients_abcd(lambda0, d_sphere, m_sphere, n_env, n_pole)
|
|
582
|
+
abcd = _internal_abcd(lambda0, d_sphere, m_sphere, n_env, n_pole, abcd)
|
|
547
583
|
|
|
548
|
-
|
|
584
|
+
e_sph, h_sph = _near_fields(
|
|
549
585
|
abcd,
|
|
550
586
|
lambda0,
|
|
551
587
|
d_sphere,
|
|
@@ -558,6 +594,7 @@ def eh_near(
|
|
|
558
594
|
want_e=True,
|
|
559
595
|
want_h=True,
|
|
560
596
|
)
|
|
597
|
+
return _convert(e_sph), _convert(h_sph)
|
|
561
598
|
|
|
562
599
|
|
|
563
600
|
def e_near_cartesian(
|
|
@@ -586,7 +623,8 @@ def e_near_cartesian(
|
|
|
586
623
|
n_pole (int): Number of multipole terms to keep. ``0`` (the default) keeps
|
|
587
624
|
two more than Wiscombe's criterion, which converges the near field at
|
|
588
625
|
the sphere surface roughly five times closer than the criterion alone.
|
|
589
|
-
abcd (ndarray or None): Optional precomputed coefficients ``[a, b, c, d]
|
|
626
|
+
abcd (ndarray or None): Optional precomputed coefficients ``[a, b, c, d]``,
|
|
627
|
+
in the active time convention.
|
|
590
628
|
|
|
591
629
|
Returns:
|
|
592
630
|
ndarray: Cartesian electric components ``[E_x, E_y, E_z]``.
|
|
@@ -622,7 +660,8 @@ def h_near_cartesian(
|
|
|
622
660
|
n_pole (int): Number of multipole terms to keep. ``0`` (the default) keeps
|
|
623
661
|
two more than Wiscombe's criterion, which converges the near field at
|
|
624
662
|
the sphere surface roughly five times closer than the criterion alone.
|
|
625
|
-
abcd (ndarray or None): Optional precomputed coefficients ``[a, b, c, d]
|
|
663
|
+
abcd (ndarray or None): Optional precomputed coefficients ``[a, b, c, d]``,
|
|
664
|
+
in the active time convention.
|
|
626
665
|
|
|
627
666
|
Returns:
|
|
628
667
|
ndarray: Cartesian magnetic components ``[H_x, H_y, H_z]``.
|
|
@@ -658,7 +697,8 @@ def eh_near_cartesian(
|
|
|
658
697
|
n_pole (int): Number of multipole terms to keep. ``0`` (the default) keeps
|
|
659
698
|
two more than Wiscombe's criterion, which converges the near field at
|
|
660
699
|
the sphere surface roughly five times closer than the criterion alone.
|
|
661
|
-
abcd (ndarray or None): Optional precomputed coefficients ``[a, b, c, d]
|
|
700
|
+
abcd (ndarray or None): Optional precomputed coefficients ``[a, b, c, d]``,
|
|
701
|
+
in the active time convention.
|
|
662
702
|
|
|
663
703
|
Returns:
|
|
664
704
|
tuple[ndarray, ndarray]: Tuple ``(E_xyz, H_xyz)`` where each array is
|