iitmm 0.1.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.
- iitmm/__init__.py +1 -0
- iitmm/bare_math.py +526 -0
- iitmm/material.py +44 -0
- iitmm/parameters.py +26 -0
- iitmm/solvers.py +656 -0
- iitmm/structures/__init__.py +10 -0
- iitmm/structures/axially_symmetric/__init__.py +24 -0
- iitmm/structures/axially_symmetric/axially_symmetric.py +226 -0
- iitmm/structures/axially_symmetric/pixilated.py +119 -0
- iitmm/structures/axially_symmetric/polygonal.py +316 -0
- iitmm/structures/axially_symmetric/shifted_sphere.py +136 -0
- iitmm/structures/axially_symmetric/spheroid.py +143 -0
- iitmm/structures/axially_symmetric/up_based.py +207 -0
- iitmm/structures/sphere.py +168 -0
- iitmm/structures/sphere_multilayered.py +392 -0
- iitmm/structures/structure.py +74 -0
- iitmm/utils.py +78 -0
- iitmm-0.1.0.dist-info/METADATA +77 -0
- iitmm-0.1.0.dist-info/RECORD +20 -0
- iitmm-0.1.0.dist-info/WHEEL +4 -0
iitmm/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""IITMM - Invariant Imbedding T-Matrix Method package."""
|
iitmm/bare_math.py
ADDED
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
"""Contains the bare mathematical functions used in the IITMM package."""
|
|
2
|
+
|
|
3
|
+
import jax
|
|
4
|
+
from jax.typing import ArrayLike
|
|
5
|
+
import jax.numpy as jnp
|
|
6
|
+
from typing import Literal
|
|
7
|
+
from warnings import warn
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
###########################################################
|
|
11
|
+
# Legendre polynomials
|
|
12
|
+
###########################################################
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@jax.jit(static_argnames=["n"])
|
|
16
|
+
def legendre(n: int, x: complex | ArrayLike) -> jax.Array:
|
|
17
|
+
r"""Calculates the Legendre polynomials up to degree ``n`` at a given point ``x``.
|
|
18
|
+
|
|
19
|
+
Parameters
|
|
20
|
+
----------
|
|
21
|
+
n : int
|
|
22
|
+
The maximum degree of the Legendre polynomial to compute.
|
|
23
|
+
x : complex | jax.typing.ArrayLike
|
|
24
|
+
The point at which to evaluate the Legendre polynomials.
|
|
25
|
+
|
|
26
|
+
Returns
|
|
27
|
+
-------
|
|
28
|
+
jax.Array
|
|
29
|
+
1D array of the values of the Legendre polynomials at the point ``x``
|
|
30
|
+
for degrees :math:`k = 0, 1, \dots, n`.
|
|
31
|
+
|
|
32
|
+
Notes
|
|
33
|
+
-----
|
|
34
|
+
It uses the upward recurrence relation:
|
|
35
|
+
|
|
36
|
+
.. math::
|
|
37
|
+
\begin{aligned}
|
|
38
|
+
P_0(x) &= 1, \\
|
|
39
|
+
P_1(x) &= x, \\
|
|
40
|
+
P_n(x) &= \frac{2n-1}{n} x P_{n-1}(x) - \frac{n-1}{n} P_{n-2}(x), \quad n \geq 2.
|
|
41
|
+
\end{aligned}
|
|
42
|
+
|
|
43
|
+
"""
|
|
44
|
+
# Computation of Special Functions
|
|
45
|
+
# by Shanjie Zhang, Jianming Jin pp. 744 (1996)
|
|
46
|
+
res = jnp.empty(n + 1, dtype=jnp.result_type(x))
|
|
47
|
+
res = res.at[0].set(1)
|
|
48
|
+
res = res.at[1].set(x)
|
|
49
|
+
|
|
50
|
+
def body(n, res):
|
|
51
|
+
res = res.at[n].set((2 * n - 1) / n * x * res[n - 1] - (n - 1) / n * res[n - 2])
|
|
52
|
+
return res
|
|
53
|
+
|
|
54
|
+
return jax.lax.fori_loop(2, n + 1, body, res)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
###########################################################
|
|
58
|
+
# Spherical Bessel and Hankel functions
|
|
59
|
+
###########################################################
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
# Continued fraction
|
|
63
|
+
@jax.jit(static_argnames=("n", "limit_N"))
|
|
64
|
+
def _cf_j(n: int, x: jax.Array, limit_N: int = 10000) -> jax.Array:
|
|
65
|
+
# see section 9.5 "The Programs SBESJY and RICBES"
|
|
66
|
+
# of Computational Atomic Physics (Klaus Bartschat)
|
|
67
|
+
# Lentz-Thompson (L-T) algorithm, (9.24)
|
|
68
|
+
|
|
69
|
+
rtol = 10 * jnp.finfo(x.dtype).eps
|
|
70
|
+
inv_x = jnp.reciprocal(x)
|
|
71
|
+
|
|
72
|
+
def cond_fun(state):
|
|
73
|
+
_, _, _, delta, N = state
|
|
74
|
+
return jnp.logical_and(jnp.abs(delta - 1) > rtol, limit_N > N)
|
|
75
|
+
|
|
76
|
+
def body(state):
|
|
77
|
+
f, c, d, _, N = state
|
|
78
|
+
b = (2 * (n + N) + 1) * inv_x
|
|
79
|
+
c = b - 1.0 / c
|
|
80
|
+
d = 1.0 / (b - d)
|
|
81
|
+
delta = c * d
|
|
82
|
+
f = f * delta
|
|
83
|
+
return f, c, d, delta, N + 1
|
|
84
|
+
|
|
85
|
+
f0 = n * inv_x
|
|
86
|
+
c0 = f0
|
|
87
|
+
d0 = 0.0
|
|
88
|
+
delta0 = jnp.inf
|
|
89
|
+
N0 = 1
|
|
90
|
+
|
|
91
|
+
state = jax.lax.while_loop(cond_fun, body, (f0, c0, d0, delta0, N0))
|
|
92
|
+
return state[0]
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# Downward recurrence for spherical j_n and j_n'
|
|
96
|
+
@jax.jit(static_argnames=("n",))
|
|
97
|
+
def _recurrence_downward_j(n: int, x: jax.Array, f: jax.Array) -> jax.Array:
|
|
98
|
+
inv_x = jnp.reciprocal(x)
|
|
99
|
+
|
|
100
|
+
def body(i, state):
|
|
101
|
+
j1, jp1, j_jp = state
|
|
102
|
+
k = -i # current bessel function order
|
|
103
|
+
|
|
104
|
+
# Bessel function recurrence relations
|
|
105
|
+
# j_{k} = (k+2)/z * j_{k+1} + j'_{k+1}
|
|
106
|
+
# j'_{k} = k/z * j_{k} - j_{k+1}
|
|
107
|
+
j = ((k + 2) * inv_x) * j1 + jp1
|
|
108
|
+
jp = (k * inv_x) * j - j1
|
|
109
|
+
|
|
110
|
+
j_jp = j_jp.at[k, 0].set(j)
|
|
111
|
+
j_jp = j_jp.at[k, 1].set(jp)
|
|
112
|
+
|
|
113
|
+
return j, jp, j_jp
|
|
114
|
+
|
|
115
|
+
jn = 1.0
|
|
116
|
+
jpn = f
|
|
117
|
+
|
|
118
|
+
j_jp = jnp.empty((n + 1, 2), dtype=x.dtype)
|
|
119
|
+
j_jp = j_jp.at[-1, 0].set(jn)
|
|
120
|
+
j_jp = j_jp.at[-1, 1].set(jpn)
|
|
121
|
+
|
|
122
|
+
_, _, j_jp = jax.lax.fori_loop(-n + 1, 1, body, (jn, jpn, j_jp))
|
|
123
|
+
|
|
124
|
+
# We use normalization idea from the paper
|
|
125
|
+
# "On the computation of spherical Bessel functions of complex arguments" by Liang-Wu Cai:
|
|
126
|
+
# Exact values for j0 and j1
|
|
127
|
+
j0 = jnp.sinc(x / jnp.pi)
|
|
128
|
+
j1 = (j0 - jnp.cos(x)) * inv_x
|
|
129
|
+
|
|
130
|
+
condition = jnp.abs(j_jp[0, 0]) >= jnp.abs(j_jp[1, 0])
|
|
131
|
+
c1 = jnp.where(condition, j_jp[0, 0], j_jp[1, 0])
|
|
132
|
+
c2 = jnp.where(condition, j0, j1)
|
|
133
|
+
|
|
134
|
+
return c2 * (j_jp / c1)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
# # Upward recurrence for spherical y_n and y_n'
|
|
138
|
+
# @jax.jit(static_argnames=("n",))
|
|
139
|
+
# def _recurrence_upward_y(n: int, x: jax.Array) -> jax.Array:
|
|
140
|
+
# inv_x = jnp.reciprocal(x)
|
|
141
|
+
|
|
142
|
+
# def body(i, state):
|
|
143
|
+
# y1, yp1, y_yp = state
|
|
144
|
+
|
|
145
|
+
# y = ((i - 1) * inv_x) * y1 - yp1
|
|
146
|
+
# yp = y1 - ((i + 1) * inv_x) * y
|
|
147
|
+
|
|
148
|
+
# y_yp = y_yp.at[i, 0].set(y)
|
|
149
|
+
# y_yp = y_yp.at[i, 1].set(yp)
|
|
150
|
+
|
|
151
|
+
# return y, yp, y_yp
|
|
152
|
+
|
|
153
|
+
# y0 = -jnp.cos(x) * inv_x
|
|
154
|
+
# yp0 = (jnp.sin(x) - y0) * inv_x
|
|
155
|
+
|
|
156
|
+
# y_yp = jnp.empty((n + 1, 2), dtype=x.dtype)
|
|
157
|
+
# y_yp = y_yp.at[0, 0].set(y0)
|
|
158
|
+
# y_yp = y_yp.at[0, 1].set(yp0)
|
|
159
|
+
|
|
160
|
+
# _, _, y_yp = jax.lax.fori_loop(1, n + 1, body, (y0, yp0, y_yp))
|
|
161
|
+
|
|
162
|
+
# return y_yp
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
# Upward recurrence for spherical h_n and h_n'
|
|
166
|
+
@jax.jit(static_argnames=("n",))
|
|
167
|
+
def _recurrence_upward_h(n: int, x: jax.Array, j_jp: jax.Array) -> jax.Array:
|
|
168
|
+
inv_x = jnp.reciprocal(x)
|
|
169
|
+
|
|
170
|
+
def body(k, state):
|
|
171
|
+
h1, hp1, h_hp = state
|
|
172
|
+
|
|
173
|
+
# Hankel function recurrence relations
|
|
174
|
+
# h_{k} = (k-1)/z * h_{k-1} - h'_{k-1}
|
|
175
|
+
# h'_{k} = h_{k-1} - (k+1)/z * h_{k}
|
|
176
|
+
h = (k - 1) * inv_x * h1 - hp1
|
|
177
|
+
hp = h1 - (k + 1) * inv_x * h
|
|
178
|
+
|
|
179
|
+
h_hp = h_hp.at[k, 0].set(h)
|
|
180
|
+
h_hp = h_hp.at[k, 1].set(hp)
|
|
181
|
+
|
|
182
|
+
return h, hp, h_hp
|
|
183
|
+
|
|
184
|
+
# We use idea to calculate h^(1) for Im(z)>=0 and h^(2) for Im(z)<0 from the paper
|
|
185
|
+
# "On the computation of spherical Bessel functions of complex arguments" by Liang-Wu Cai:
|
|
186
|
+
# Exact values for h0 and h0'
|
|
187
|
+
h0 = jnp.where(
|
|
188
|
+
x.imag >= 0,
|
|
189
|
+
(-1j * inv_x) * jnp.exp(1j * x),
|
|
190
|
+
(1j * inv_x) * jnp.exp(-1j * x),
|
|
191
|
+
)
|
|
192
|
+
hp0 = jnp.where(
|
|
193
|
+
x.imag >= 0,
|
|
194
|
+
(1j - inv_x) * h0,
|
|
195
|
+
(-1j - inv_x) * h0,
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
h_hp = jnp.empty((n + 1, 2), dtype=jnp.result_type(x + 0j))
|
|
199
|
+
h_hp = h_hp.at[0, 0].set(h0)
|
|
200
|
+
h_hp = h_hp.at[0, 1].set(hp0)
|
|
201
|
+
|
|
202
|
+
_, _, h_hp = jax.lax.fori_loop(1, n + 1, body, (h0, hp0, h_hp))
|
|
203
|
+
|
|
204
|
+
return jnp.where(x.imag >= 0, h_hp, 2 * j_jp - h_hp)
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def _j_jp_n(n: int, x: complex | ArrayLike) -> jax.Array:
|
|
208
|
+
if n == 0:
|
|
209
|
+
j0 = jnp.sinc(x / jnp.pi)
|
|
210
|
+
j0p = (jnp.cos(x) - j0) * jnp.reciprocal(x)
|
|
211
|
+
return jnp.array([[j0, j0p]], dtype=jnp.result_type(x))
|
|
212
|
+
|
|
213
|
+
f = _cf_j(n=n, x=x)
|
|
214
|
+
return _recurrence_downward_j(n, x, f)
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def _j_jp_n_impl(n: int, x: complex | ArrayLike, N_max: Literal["CF"] | int = "CF") -> jax.Array:
|
|
218
|
+
if N_max == "CF":
|
|
219
|
+
return _j_jp_n(n, x)
|
|
220
|
+
raise NotImplementedError("Only N_max='CF' is implemented.")
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def _j_jp_n_impl_fwd(n, x, N_max: Literal["CF"] | int = "CF"):
|
|
224
|
+
j_jp = _j_jp_n_impl(n, x, N_max)
|
|
225
|
+
|
|
226
|
+
return j_jp, (j_jp, x)
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def _j_jp_n_impl_bwd(n, N_max, res, j_jp_bar):
|
|
230
|
+
j_jp, x = res
|
|
231
|
+
inv_x = jnp.reciprocal(x)
|
|
232
|
+
|
|
233
|
+
j_bar = j_jp_bar[:, 0]
|
|
234
|
+
jp_bar = j_jp_bar[:, 1]
|
|
235
|
+
|
|
236
|
+
# we will use j''_k = [(k+1)k/z^2 - 1] j_{k} - 2/z j'_{k}
|
|
237
|
+
j = j_jp[:, 0]
|
|
238
|
+
jp = j_jp[:, 1]
|
|
239
|
+
k = jnp.arange(n + 1)
|
|
240
|
+
jpp = ((k + 1) * k * inv_x * inv_x - 1) * j - (2 * inv_x) * jp
|
|
241
|
+
|
|
242
|
+
x_bar = jnp.sum(j_bar * jp + jp_bar * jpp)
|
|
243
|
+
|
|
244
|
+
return (x_bar,)
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
# TODO: check gradients
|
|
248
|
+
_j_jp_n_impl = jax.custom_vjp(_j_jp_n_impl, nondiff_argnames=("n", "N_max"))
|
|
249
|
+
_j_jp_n_impl.defvjp(_j_jp_n_impl_fwd, _j_jp_n_impl_bwd)
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
# TODO: add downward recurrence implementation
|
|
253
|
+
def j_jp_n(n: int, x: complex | ArrayLike, N_max: Literal["CF"] | int = "CF") -> jax.Array:
|
|
254
|
+
r"""Calculates the spherical Bessel functions :math:`j_k` and their derivatives.
|
|
255
|
+
|
|
256
|
+
Parameters
|
|
257
|
+
----------
|
|
258
|
+
n : int
|
|
259
|
+
The maximum order of the Bessel functions. Results for
|
|
260
|
+
:math:`k = 0, 1, \dots, n` are returned.
|
|
261
|
+
x : complex | jax.typing.ArrayLike
|
|
262
|
+
The argument of the Bessel functions.
|
|
263
|
+
N_max : Literal["CF"] | int, optional
|
|
264
|
+
The order at which the downward recurrence is started.
|
|
265
|
+
Default is ``"CF"``, which obtains the starting value from a
|
|
266
|
+
continued fraction instead.
|
|
267
|
+
|
|
268
|
+
Returns
|
|
269
|
+
-------
|
|
270
|
+
jax.Array
|
|
271
|
+
2D array where the first column contains :math:`j_k` and the second column
|
|
272
|
+
contains :math:`j_k'` for :math:`k = 0, 1, \dots, n`.
|
|
273
|
+
|
|
274
|
+
Notes
|
|
275
|
+
-----
|
|
276
|
+
For ``N_max="CF"``, the algorithm follows section 9.5 of [#Bartschat]_.
|
|
277
|
+
First, the Lentz-Thompson (L-T) algorithm, described by equation (9.24) of
|
|
278
|
+
[#Bartschat]_, is used to compute the ratio :math:`j_n'/j_n` through a
|
|
279
|
+
continued fraction. Then, the downward recurrence is used to compute
|
|
280
|
+
:math:`j_k` and :math:`j_k'` for :math:`k = n-1, n-2, \dots, 0`.
|
|
281
|
+
Finally, the result is normalized using the exact values of :math:`j_0` or
|
|
282
|
+
:math:`j_1`, which fixes the amplitude of both sequences. For ``n = 0``, the
|
|
283
|
+
exact expressions for :math:`j_0` and :math:`j_0'` are used directly.
|
|
284
|
+
|
|
285
|
+
The continued fraction is iterated until the relative change of the converged
|
|
286
|
+
value is below :math:`10\cdot\epsilon`, where :math:`\epsilon` is machine
|
|
287
|
+
epsilon for the provided ``x`` dtype.
|
|
288
|
+
|
|
289
|
+
.. warning::
|
|
290
|
+
For this function, integer ``N_max`` values are not implemented yet;
|
|
291
|
+
only ``"CF"`` is supported.
|
|
292
|
+
|
|
293
|
+
References
|
|
294
|
+
----------
|
|
295
|
+
.. [#Bartschat] Bartschat, Klaus, ed. Computational Atomic Physics:
|
|
296
|
+
Electron and Positron Collisions with Atoms and Ions.
|
|
297
|
+
Softcover reprint of the original 1st ed. 1996. Springer, 2014.
|
|
298
|
+
"""
|
|
299
|
+
return _j_jp_n_impl(n, x, N_max)
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def h_hp_n(n: int, x: complex | ArrayLike, j_jp: jax.Array) -> jax.Array:
|
|
303
|
+
r"""Calculates the spherical Hankel functions :math:`h^{(1)}_k` and their derivatives.
|
|
304
|
+
|
|
305
|
+
Parameters
|
|
306
|
+
----------
|
|
307
|
+
n : int
|
|
308
|
+
The maximum order of the Hankel functions. Results for
|
|
309
|
+
:math:`k = 0, 1, \dots, n` are returned.
|
|
310
|
+
x : complex | jax.typing.ArrayLike
|
|
311
|
+
The argument of the Hankel functions.
|
|
312
|
+
j_jp : jax.Array
|
|
313
|
+
The 2D array of :math:`j_k` and :math:`j_k'` computed by :func:`j_jp_n`
|
|
314
|
+
for the same ``n`` and ``x``.
|
|
315
|
+
|
|
316
|
+
Returns
|
|
317
|
+
-------
|
|
318
|
+
jax.Array
|
|
319
|
+
2D array where the first column contains :math:`h^{(1)}_k` and the second column
|
|
320
|
+
contains :math:`h^{(1)\prime}_k` for :math:`k = 0, 1, \dots, n`.
|
|
321
|
+
|
|
322
|
+
Notes
|
|
323
|
+
-----
|
|
324
|
+
The calculation uses the upward recurrence. For :math:`\operatorname{Im}(x) \ge 0`,
|
|
325
|
+
it propagates :math:`h^{(1)}_k`. For :math:`\operatorname{Im}(x) < 0`, it propagates
|
|
326
|
+
the numerically stable :math:`h^{(2)}_k` and converts the result back using
|
|
327
|
+
:math:`h^{(1)}_k = 2 j_k - h^{(2)}_k`.
|
|
328
|
+
This approach follows [#Cai]_.
|
|
329
|
+
|
|
330
|
+
References
|
|
331
|
+
----------
|
|
332
|
+
.. [#Cai] Cai, Liang-Wu. "On the Computation of Spherical Bessel Functions
|
|
333
|
+
of Complex Arguments." Computer Physics Communications 182, no. 3 (2011): 663-68.
|
|
334
|
+
https://doi.org/10.1016/j.cpc.2010.11.019.
|
|
335
|
+
|
|
336
|
+
"""
|
|
337
|
+
return _recurrence_upward_h(n, x, j_jp)
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
###########################################################
|
|
341
|
+
# Logarithmic derivative of the Riccati-Bessel function
|
|
342
|
+
###########################################################
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
# Continued fraction
|
|
346
|
+
@jax.jit(static_argnames=("n", "limit_N"))
|
|
347
|
+
def _cf_D1(n: int, z: jax.Array, limit_N: int = 10000):
|
|
348
|
+
"""Continued fraction for :math:`D_n^{(1)}(z)`, using the Lentz-Thompson algorithm."""
|
|
349
|
+
inv_z = 1.0 / z
|
|
350
|
+
rtol = 10 * jnp.finfo(z.dtype).eps
|
|
351
|
+
|
|
352
|
+
# D_n = (n+1)/z - 1/f
|
|
353
|
+
# where f = (2n+3)/z - 1/((2n+5)/z - ...)
|
|
354
|
+
|
|
355
|
+
f0 = (2 * n + 3) * inv_z
|
|
356
|
+
c0 = f0
|
|
357
|
+
d0 = jnp.zeros_like(z)
|
|
358
|
+
delta0 = jnp.asarray(jnp.inf, dtype=z.dtype)
|
|
359
|
+
|
|
360
|
+
def cond(state):
|
|
361
|
+
_, _, _, delta, k = state
|
|
362
|
+
return (jnp.abs(delta - 1) > rtol) & (k < limit_N)
|
|
363
|
+
|
|
364
|
+
def body(state):
|
|
365
|
+
f, c, d, _, k = state
|
|
366
|
+
|
|
367
|
+
b = (2 * (n + k) + 3) * inv_z
|
|
368
|
+
c = b - 1.0 / c
|
|
369
|
+
d = 1.0 / (b - d)
|
|
370
|
+
delta = c * d
|
|
371
|
+
f = f * delta
|
|
372
|
+
|
|
373
|
+
return f, c, d, delta, k + 1
|
|
374
|
+
|
|
375
|
+
f, _, _, _, k_next = jax.lax.while_loop(cond, body, (f0, c0, d0, delta0, 1))
|
|
376
|
+
|
|
377
|
+
return (n + 1) * inv_z - 1.0 / f, k_next - 1
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def _D_n_first_kind(n: int, z: ArrayLike) -> jax.Array:
|
|
381
|
+
dtype = jnp.result_type(z, 0.0)
|
|
382
|
+
out = jnp.zeros((n + 1,), dtype=dtype)
|
|
383
|
+
|
|
384
|
+
inv_z = 1.0 / z
|
|
385
|
+
|
|
386
|
+
# Initialize using the continued fraction at n
|
|
387
|
+
D1, _ = _cf_D1(n, z)
|
|
388
|
+
out = out.at[n].set(D1)
|
|
389
|
+
|
|
390
|
+
# Recur downward from n to 1
|
|
391
|
+
def body(i, out):
|
|
392
|
+
k = n - i
|
|
393
|
+
r = k * inv_z
|
|
394
|
+
D_new = r - 1.0 / (out[k] + r)
|
|
395
|
+
return out.at[k - 1].set(D_new)
|
|
396
|
+
|
|
397
|
+
out = jax.lax.fori_loop(0, n, body, out)
|
|
398
|
+
|
|
399
|
+
return out
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
def _D_n_first_kind_downward(n: int, z: ArrayLike, N_max: ArrayLike) -> jax.Array:
|
|
403
|
+
r"""Calculates :math:`D_n^{(1)}(z)` by downward recurrence.
|
|
404
|
+
|
|
405
|
+
:math:`D_n^{(1)}(z) = \psi_n'(z)/\psi_n(z)` is the logarithmic derivative of the
|
|
406
|
+
Riccati-Bessel function :math:`\psi_n`.
|
|
407
|
+
"""
|
|
408
|
+
dtype = jnp.result_type(z, 0.0)
|
|
409
|
+
out = jnp.zeros(n + 1, dtype=dtype)
|
|
410
|
+
|
|
411
|
+
def cond(state):
|
|
412
|
+
k, _, _ = state
|
|
413
|
+
return k > 0
|
|
414
|
+
|
|
415
|
+
def body(state):
|
|
416
|
+
k, D_k, out = state
|
|
417
|
+
r = k / z
|
|
418
|
+
D_prev = r - 1.0 / (D_k + r)
|
|
419
|
+
|
|
420
|
+
out = out.at[jnp.minimum(k - 1, n)].set(D_prev)
|
|
421
|
+
return k - 1, D_prev, out
|
|
422
|
+
|
|
423
|
+
_, _, out = jax.lax.while_loop(cond, body, (N_max, jnp.zeros((), dtype=dtype), out))
|
|
424
|
+
return out
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
def _D_n_first_kind_impl(n: int, z: complex | ArrayLike, N_max: Literal["CF"] | int) -> jax.Array:
|
|
428
|
+
z = jnp.asarray(z, dtype=jnp.result_type(z, 0.0))
|
|
429
|
+
|
|
430
|
+
if N_max == "CF":
|
|
431
|
+
return _D_n_first_kind(n, z)
|
|
432
|
+
if N_max < n + 1:
|
|
433
|
+
warn(
|
|
434
|
+
f"N_max={N_max} is less than n+1={n + 1}. The downward recurrence may not be accurate.",
|
|
435
|
+
stacklevel=1,
|
|
436
|
+
)
|
|
437
|
+
return _D_n_first_kind_downward(n, z, N_max)
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
def _D_n_first_kind_impl_fwd(n, z, N_max):
|
|
441
|
+
D_n = _D_n_first_kind_impl(n, z, N_max)
|
|
442
|
+
|
|
443
|
+
return D_n, (D_n, z)
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
def _D_n_first_kind_impl_bwd(n, N_max, res, D_n_bar):
|
|
447
|
+
D_n, z = res
|
|
448
|
+
inv_z = jnp.reciprocal(z)
|
|
449
|
+
|
|
450
|
+
# The Riccati-Bessel functions obey psi''_n = [n(n+1)/z^2 - 1] psi_n, hence
|
|
451
|
+
# D'_n = psi''_n/psi_n - (psi'_n/psi_n)^2 = n(n+1)/z^2 - 1 - D_n^2
|
|
452
|
+
n_list = jnp.arange(n + 1)
|
|
453
|
+
D_n_p = n_list * (n_list + 1) * inv_z * inv_z - 1 - D_n * D_n
|
|
454
|
+
|
|
455
|
+
z_bar = jnp.sum(D_n_bar * D_n_p)
|
|
456
|
+
|
|
457
|
+
return (z_bar,)
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
# TODO: check gradients
|
|
461
|
+
_D_n_first_kind_impl = jax.custom_vjp(_D_n_first_kind_impl, nondiff_argnames=("n", "N_max"))
|
|
462
|
+
_D_n_first_kind_impl.defvjp(_D_n_first_kind_impl_fwd, _D_n_first_kind_impl_bwd)
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
def D_n_first_kind(n: int, z: complex | ArrayLike, N_max: Literal["CF"] | int = "CF") -> jax.Array:
|
|
466
|
+
r"""Calculates the logarithmic derivatives :math:`D_n^{(1)}` of the Riccati-Bessel functions.
|
|
467
|
+
|
|
468
|
+
.. math::
|
|
469
|
+
D_n^{(1)}(z) = \frac{\psi_n'(z)}{\psi_n(z)}, \quad \psi_n(z) = z j_n(z)
|
|
470
|
+
|
|
471
|
+
Parameters
|
|
472
|
+
----------
|
|
473
|
+
n : int
|
|
474
|
+
The maximum order of the logarithmic derivatives.
|
|
475
|
+
z : complex | jax.typing.ArrayLike
|
|
476
|
+
The argument of the Riccati-Bessel functions.
|
|
477
|
+
N_max : Literal["CF"] | int, optional
|
|
478
|
+
The order at which the downward recurrence is started.
|
|
479
|
+
Default is ``"CF"``, which obtains the starting value :math:`D_n^{(1)}`
|
|
480
|
+
from a continued fraction instead.
|
|
481
|
+
|
|
482
|
+
Returns
|
|
483
|
+
-------
|
|
484
|
+
jax.Array
|
|
485
|
+
1D array of the values of :math:`D_k^{(1)}` for :math:`k = 0, 1, \dots, n`.
|
|
486
|
+
|
|
487
|
+
Notes
|
|
488
|
+
-----
|
|
489
|
+
If ``N_max="CF"``, :math:`D_n^{(1)}` is obtained from the continued fraction
|
|
490
|
+
|
|
491
|
+
.. math::
|
|
492
|
+
\begin{aligned}
|
|
493
|
+
D_n^{(1)}(z) &= \frac{n+1}{z} - \frac{1}{f}\\
|
|
494
|
+
f &= \frac{2n+3}{z} - \frac{1}{\frac{2n+5}{z} - \dots}
|
|
495
|
+
\end{aligned}
|
|
496
|
+
|
|
497
|
+
evaluated with the Lentz-Thompson (L-T) algorithm of equation (9.24) in
|
|
498
|
+
section 9.5 of [#Bartschat]_, which stops once the relative change of the
|
|
499
|
+
converged value is below :math:`10\cdot\epsilon`, where :math:`\epsilon` is
|
|
500
|
+
machine epsilon for the dtype of ``z``.
|
|
501
|
+
|
|
502
|
+
Then, the downward recurrence is used to compute
|
|
503
|
+
:math:`D_k^{(1)}` for :math:`k = n-1, n-2, \dots, 0`:
|
|
504
|
+
|
|
505
|
+
.. math::
|
|
506
|
+
D_{k-1}^{(1)}(z) = \frac{k}{z} - \frac{1}{D_k^{(1)}(z) + \frac{k}{z}}
|
|
507
|
+
|
|
508
|
+
If :math:`N_\mathrm{max}` is an integer, the function starts the same
|
|
509
|
+
downward recurrence at order :math:`N_\mathrm{max}` from the initial guess
|
|
510
|
+
:math:`D_{N_\mathrm{max}}^{(1)} = 0`; the recurrence converges as it descends.
|
|
511
|
+
|
|
512
|
+
.. warning::
|
|
513
|
+
The downward recurrence requires :math:`N_\mathrm{max} \ge n + 1`,
|
|
514
|
+
otherwise the result may be inaccurate.
|
|
515
|
+
|
|
516
|
+
As a rule of thumb, :math:`N_\mathrm{max} \gtrsim |z| + 4 |z|^{1/3} + 10`
|
|
517
|
+
yields :math:`10^{-10}` relative accuracy, but this should be verified for
|
|
518
|
+
each specific case.
|
|
519
|
+
|
|
520
|
+
References
|
|
521
|
+
----------
|
|
522
|
+
.. [#Bartschat] Bartschat, Klaus, ed. Computational Atomic Physics:
|
|
523
|
+
Electron and Positron Collisions with Atoms and Ions.
|
|
524
|
+
Softcover reprint of the original 1st ed. 1996. Springer, 2014.
|
|
525
|
+
"""
|
|
526
|
+
return _D_n_first_kind_impl(n, z, N_max)
|
iitmm/material.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""Base classes for materials."""
|
|
2
|
+
|
|
3
|
+
from typing import TypeVar, Protocol
|
|
4
|
+
import jax
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
_MaterialParams = TypeVar("_MaterialParams", contravariant=True)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class MaterialProto(Protocol[_MaterialParams]):
|
|
11
|
+
"""Protocol for relative permittivity calculation."""
|
|
12
|
+
|
|
13
|
+
def __call__(
|
|
14
|
+
self,
|
|
15
|
+
params: _MaterialParams,
|
|
16
|
+
k0: complex,
|
|
17
|
+
) -> jax.typing.ArrayLike:
|
|
18
|
+
"""Calculate the relative permittivity at given wavenumber.
|
|
19
|
+
|
|
20
|
+
Parameters
|
|
21
|
+
----------
|
|
22
|
+
params : _MaterialParams
|
|
23
|
+
The parameters of the material.
|
|
24
|
+
k0 : complex
|
|
25
|
+
The vacuum wavenumber.
|
|
26
|
+
|
|
27
|
+
Returns
|
|
28
|
+
-------
|
|
29
|
+
jax.typing.ArrayLike
|
|
30
|
+
The relative permittivity at the given wavenumber.
|
|
31
|
+
"""
|
|
32
|
+
...
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class IsotropicMaterialProto(MaterialProto[_MaterialParams], Protocol[_MaterialParams]):
|
|
36
|
+
"""Base class for isotropic material properties."""
|
|
37
|
+
|
|
38
|
+
pass
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class AnisotropicMaterialProto(MaterialProto[_MaterialParams], Protocol[_MaterialParams]):
|
|
42
|
+
"""Base class for anisotropic material properties."""
|
|
43
|
+
|
|
44
|
+
pass
|
iitmm/parameters.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""Mix-in classes for parameters containers."""
|
|
2
|
+
|
|
3
|
+
from typing import Generic, TypeVar
|
|
4
|
+
from functools import partial
|
|
5
|
+
import jax
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
_StructureParams = TypeVar("_StructureParams")
|
|
10
|
+
_MaterialParams = TypeVar("_MaterialParams")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@partial(jax.tree_util.register_dataclass)
|
|
14
|
+
@dataclass(kw_only=True)
|
|
15
|
+
class WithStructureParams(Generic[_StructureParams]):
|
|
16
|
+
"""Parameters for structures."""
|
|
17
|
+
|
|
18
|
+
structure_params: _StructureParams
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@partial(jax.tree_util.register_dataclass)
|
|
22
|
+
@dataclass(kw_only=True)
|
|
23
|
+
class WithMaterialParams(Generic[_MaterialParams]):
|
|
24
|
+
"""Parameters for materials."""
|
|
25
|
+
|
|
26
|
+
material_params: _MaterialParams
|