dreams 0.2.0__tar.gz
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.
- dreams-0.2.0/LICENSE +21 -0
- dreams-0.2.0/PKG-INFO +70 -0
- dreams-0.2.0/README.md +36 -0
- dreams-0.2.0/dreams/__init__.py +3 -0
- dreams-0.2.0/dreams/jax_coeffs.py +381 -0
- dreams-0.2.0/dreams/jax_coord.py +125 -0
- dreams-0.2.0/dreams/jax_la_bounded.py +514 -0
- dreams-0.2.0/dreams/jax_misc.py +325 -0
- dreams-0.2.0/dreams/jax_op.py +388 -0
- dreams-0.2.0/dreams/jax_primitive.py +325 -0
- dreams-0.2.0/dreams/jax_recursive.py +469 -0
- dreams-0.2.0/dreams/jax_smat.py +931 -0
- dreams-0.2.0/dreams/jax_tmat.py +534 -0
- dreams-0.2.0/dreams/jax_tr.py +536 -0
- dreams-0.2.0/dreams/jax_waves.py +896 -0
- dreams-0.2.0/pyproject.toml +148 -0
dreams-0.2.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Nigar Asadova and Jan David Fischbach
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
dreams-0.2.0/PKG-INFO
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dreams
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Differentiable T-matrix based electromagnetic scattering framework.
|
|
5
|
+
Keywords: python
|
|
6
|
+
Author: Jan David Fischbach
|
|
7
|
+
Author-email: Nigar Asadova <nigar.asadova@kit.edu>
|
|
8
|
+
Requires-Python: >=3.9
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: matplotlib
|
|
16
|
+
Requires-Dist: treams
|
|
17
|
+
Requires-Dist: jax
|
|
18
|
+
Requires-Dist: nlopt
|
|
19
|
+
Requires-Dist: equinox>=0.11.10
|
|
20
|
+
Requires-Dist: pre-commit ; extra == "dev"
|
|
21
|
+
Requires-Dist: pytest ; extra == "dev"
|
|
22
|
+
Requires-Dist: pytest-cov ; extra == "dev"
|
|
23
|
+
Requires-Dist: pytest-regressions ; extra == "dev"
|
|
24
|
+
Requires-Dist: pandas ; extra == "dev"
|
|
25
|
+
Requires-Dist: line-profiler>=4.2.0 ; extra == "dev"
|
|
26
|
+
Requires-Dist: jupytext ; extra == "docs"
|
|
27
|
+
Requires-Dist: autodoc_pydantic ; extra == "docs"
|
|
28
|
+
Requires-Dist: matplotlib ; extra == "docs"
|
|
29
|
+
Requires-Dist: jupyter-book==0.15.1 ; extra == "docs"
|
|
30
|
+
Requires-Dist: sphinx-click ; extra == "docs"
|
|
31
|
+
Provides-Extra: dev
|
|
32
|
+
Provides-Extra: docs
|
|
33
|
+
|
|
34
|
+
# dreams
|
|
35
|
+
|
|
36
|
+
A differentiable T-matrix-based framework for
|
|
37
|
+
multiple-scattering problems in nanophotonics.
|
|
38
|
+
|
|
39
|
+
It implements the T-matrix formalism in
|
|
40
|
+
[jax](https://github.com/google/jax), following the implementation of
|
|
41
|
+
[treams](https://github.com/tfp-photonics/treams).
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
To install the package with pip, use
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pip install dreams
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
For the development version, clone the repository:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
git clone https://github.com/tfp-photonics/dreams.git
|
|
55
|
+
cd dreams
|
|
56
|
+
pip install -e ".[dev]"
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Usage
|
|
60
|
+
|
|
61
|
+
The tutorial is available in the documentation:
|
|
62
|
+
https://tfp-photonics.github.io/dreams/
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
## Features
|
|
66
|
+
|
|
67
|
+
- inverse design of multiple-scattering systems
|
|
68
|
+
- optimization of scatterer positions and radii
|
|
69
|
+
- support for finite and periodic arrangements
|
|
70
|
+
|
dreams-0.2.0/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# dreams
|
|
2
|
+
|
|
3
|
+
A differentiable T-matrix-based framework for
|
|
4
|
+
multiple-scattering problems in nanophotonics.
|
|
5
|
+
|
|
6
|
+
It implements the T-matrix formalism in
|
|
7
|
+
[jax](https://github.com/google/jax), following the implementation of
|
|
8
|
+
[treams](https://github.com/tfp-photonics/treams).
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
To install the package with pip, use
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install dreams
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
For the development version, clone the repository:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
git clone https://github.com/tfp-photonics/dreams.git
|
|
22
|
+
cd dreams
|
|
23
|
+
pip install -e ".[dev]"
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
The tutorial is available in the documentation:
|
|
29
|
+
https://tfp-photonics.github.io/dreams/
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
## Features
|
|
33
|
+
|
|
34
|
+
- inverse design of multiple-scattering systems
|
|
35
|
+
- optimization of scatterer positions and radii
|
|
36
|
+
- support for finite and periodic arrangements
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
import jax
|
|
3
|
+
import jax.numpy as np
|
|
4
|
+
from jax import vmap, grad, jacrev
|
|
5
|
+
from dreams.jax_primitive import spherical_jn, spherical_hankel1
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def innermost_interface(l: int, x: np.ndarray, z: np.ndarray) -> np.ndarray:
|
|
9
|
+
# x: (2,2), z: (2,), complex allowed
|
|
10
|
+
sb = spherical_jn(l, x)
|
|
11
|
+
sh = spherical_hankel1(l, x)
|
|
12
|
+
d_jn = vmap(jacrev(spherical_jn, argnums=1, holomorphic=True), in_axes=(None, 0))(
|
|
13
|
+
l, x.flatten()
|
|
14
|
+
).reshape(x.shape)
|
|
15
|
+
d_h1 = vmap(
|
|
16
|
+
jacrev(spherical_hankel1, argnums=1, holomorphic=True), in_axes=(None, 0)
|
|
17
|
+
)(l, x.flatten()).reshape(x.shape)
|
|
18
|
+
psi = d_jn + sb / x
|
|
19
|
+
chi = d_h1 + sh / x
|
|
20
|
+
|
|
21
|
+
z0, z1 = z[0], z[1]
|
|
22
|
+
zs = (z1 + z0) / (2j * z0)
|
|
23
|
+
zd = (z1 - z0) / (2j * z0)
|
|
24
|
+
|
|
25
|
+
x10, x11 = x[1, 0], x[1, 1]
|
|
26
|
+
x10_2, x11_2 = x10 * x10, x11 * x11
|
|
27
|
+
c0 = np.array(
|
|
28
|
+
[
|
|
29
|
+
(chi[1, 0] * sb[0, 0] - sh[1, 0] * psi[0, 0]) * zs * x10_2,
|
|
30
|
+
(chi[1, 0] * sb[0, 1] + sh[1, 0] * psi[0, 1]) * zd * x10_2,
|
|
31
|
+
],
|
|
32
|
+
dtype=np.complex128,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
c1 = np.array(
|
|
36
|
+
[
|
|
37
|
+
(chi[1, 1] * sb[0, 0] + sh[1, 1] * psi[0, 0]) * zd * x11_2,
|
|
38
|
+
(chi[1, 1] * sb[0, 1] - sh[1, 1] * psi[0, 1]) * zs * x11_2,
|
|
39
|
+
],
|
|
40
|
+
dtype=np.complex128,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
c2 = np.array(
|
|
44
|
+
[
|
|
45
|
+
(-psi[1, 0] * sb[0, 0] + sb[1, 0] * psi[0, 0]) * zs * x10_2,
|
|
46
|
+
(-psi[1, 0] * sb[0, 1] - sb[1, 0] * psi[0, 1]) * zd * x10_2,
|
|
47
|
+
],
|
|
48
|
+
dtype=np.complex128,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
c3 = np.array(
|
|
52
|
+
[
|
|
53
|
+
(-psi[1, 1] * sb[0, 0] - sb[1, 1] * psi[0, 0]) * zd * x11_2,
|
|
54
|
+
(-psi[1, 1] * sb[0, 1] + sb[1, 1] * psi[0, 1]) * zs * x11_2,
|
|
55
|
+
],
|
|
56
|
+
dtype=np.complex128,
|
|
57
|
+
)
|
|
58
|
+
return np.stack([c0, c1, c2, c3], axis=1)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def interface_matrix(l: int, x: np.ndarray, z: np.ndarray) -> np.ndarray:
|
|
62
|
+
# x: (2,2), z: (2,), complex allowed
|
|
63
|
+
sb = spherical_jn(l, x)
|
|
64
|
+
sh = spherical_hankel1(l, x)
|
|
65
|
+
d_jn = vmap(jacrev(spherical_jn, argnums=1, holomorphic=True), in_axes=(None, 0))(
|
|
66
|
+
l, x.flatten()
|
|
67
|
+
).reshape(x.shape)
|
|
68
|
+
d_h1 = vmap(
|
|
69
|
+
jacrev(spherical_hankel1, argnums=1, holomorphic=True), in_axes=(None, 0)
|
|
70
|
+
)(l, x.flatten()).reshape(x.shape)
|
|
71
|
+
psi = d_jn + sb / x
|
|
72
|
+
chi = d_h1 + sh / x
|
|
73
|
+
|
|
74
|
+
z0, z1 = z[0], z[1]
|
|
75
|
+
zs = (z1 + z0) / (2j * z0)
|
|
76
|
+
zd = (z1 - z0) / (2j * z0)
|
|
77
|
+
|
|
78
|
+
x10, x11 = x[1, 0], x[1, 1]
|
|
79
|
+
x10_2, x11_2 = x10 * x10, x11 * x11
|
|
80
|
+
|
|
81
|
+
c0 = np.array(
|
|
82
|
+
[
|
|
83
|
+
(chi[1, 0] * sb[0, 0] - sh[1, 0] * psi[0, 0]) * zs * x10_2,
|
|
84
|
+
(chi[1, 0] * sb[0, 1] + sh[1, 0] * psi[0, 1]) * zd * x10_2,
|
|
85
|
+
(chi[1, 0] * sh[0, 0] - sh[1, 0] * chi[0, 0]) * zs * x10_2,
|
|
86
|
+
(chi[1, 0] * sh[0, 1] + sh[1, 0] * chi[0, 1]) * zd * x10_2,
|
|
87
|
+
]
|
|
88
|
+
)
|
|
89
|
+
c1 = np.array(
|
|
90
|
+
[
|
|
91
|
+
(chi[1, 1] * sb[0, 0] + sh[1, 1] * psi[0, 0]) * zd * x11_2,
|
|
92
|
+
(chi[1, 1] * sb[0, 1] - sh[1, 1] * psi[0, 1]) * zs * x11_2,
|
|
93
|
+
(chi[1, 1] * sh[0, 0] + sh[1, 1] * chi[0, 0]) * zd * x11_2,
|
|
94
|
+
(chi[1, 1] * sh[0, 1] - sh[1, 1] * chi[0, 1]) * zs * x11_2,
|
|
95
|
+
]
|
|
96
|
+
)
|
|
97
|
+
c2 = np.array(
|
|
98
|
+
[
|
|
99
|
+
(-psi[1, 0] * sb[0, 0] + sb[1, 0] * psi[0, 0]) * zs * x10_2,
|
|
100
|
+
(-psi[1, 0] * sb[0, 1] - sb[1, 0] * psi[0, 1]) * zd * x10_2,
|
|
101
|
+
(-psi[1, 0] * sh[0, 0] + sb[1, 0] * chi[0, 0]) * zs * x10_2,
|
|
102
|
+
(-psi[1, 0] * sh[0, 1] - sb[1, 0] * chi[0, 1]) * zd * x10_2,
|
|
103
|
+
]
|
|
104
|
+
)
|
|
105
|
+
c3 = np.array(
|
|
106
|
+
[
|
|
107
|
+
(-psi[1, 1] * sb[0, 0] - sb[1, 1] * psi[0, 0]) * zd * x11_2,
|
|
108
|
+
(-psi[1, 1] * sb[0, 1] + sb[1, 1] * psi[0, 1]) * zs * x11_2,
|
|
109
|
+
(-psi[1, 1] * sh[0, 0] - sb[1, 1] * chi[0, 0]) * zd * x11_2,
|
|
110
|
+
(-psi[1, 1] * sh[0, 1] + sb[1, 1] * chi[0, 1]) * zs * x11_2,
|
|
111
|
+
]
|
|
112
|
+
)
|
|
113
|
+
return np.stack([c0, c1, c2, c3], axis=1)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def layer_params(i, x, eps, mu, kappa):
|
|
117
|
+
nr_in, nr_out = np.sqrt(eps[i] * mu[i]), np.sqrt(eps[i + 1] * mu[i + 1])
|
|
118
|
+
z_in, z_out = np.sqrt(mu[i] / eps[i]), np.sqrt(mu[i + 1] / eps[i + 1])
|
|
119
|
+
kap_in, kap_out = kappa[i], kappa[i + 1]
|
|
120
|
+
xi = x[i]
|
|
121
|
+
x_2x2 = np.array(
|
|
122
|
+
[
|
|
123
|
+
[xi * (nr_in - kap_in), xi * (nr_in + kap_in)],
|
|
124
|
+
[xi * (nr_out - kap_out), xi * (nr_out + kap_out)],
|
|
125
|
+
],
|
|
126
|
+
dtype=np.result_type(x, eps, mu, kappa),
|
|
127
|
+
)
|
|
128
|
+
z_pair = np.array([z_in, z_out], dtype=np.result_type(eps, mu))
|
|
129
|
+
return x_2x2, z_pair
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def mie_one_l(l: int, k0: float, radii, eps, mu, kappa):
|
|
133
|
+
"""
|
|
134
|
+
radii: (n,) # radii from inner to outer shell boundaries
|
|
135
|
+
eps, mu, kappa: (n+1,) # materials from core .. embedding
|
|
136
|
+
returns: (2,2) T_l in helicity basis
|
|
137
|
+
"""
|
|
138
|
+
x = (k0 * np.asarray(radii)).astype(np.complex128) # (n,)
|
|
139
|
+
# eps = anp.asarray(eps); mu = anp.asarray(mu); kappa = anp.asarray(kappa)
|
|
140
|
+
eps = np.asarray(eps)
|
|
141
|
+
mu = np.asarray(mu)
|
|
142
|
+
kappa = np.asarray(kappa)
|
|
143
|
+
n = x.shape[0]
|
|
144
|
+
# i = 0 → innermost interface → initial 4x2 'state'
|
|
145
|
+
x0, z0 = layer_params(0, x, eps, mu, kappa)
|
|
146
|
+
state = innermost_interface(l, x0, z0).T # (4,2)
|
|
147
|
+
|
|
148
|
+
# i = 1..n-1 → general 4x4 interfaces
|
|
149
|
+
def body(state, i):
|
|
150
|
+
xi, zi = layer_params(i, x, eps, mu, kappa)
|
|
151
|
+
M = interface_matrix(l, xi, zi).T # (4,4)
|
|
152
|
+
return M @ state, None
|
|
153
|
+
|
|
154
|
+
state, _ = jax.lax.scan(body, state, np.arange(1, n))
|
|
155
|
+
# split and solve: A^{-1} B
|
|
156
|
+
A = state[:2, :].T # (2,2) (top rows)
|
|
157
|
+
B = state[2:, :].T # (2,2) (bottom rows)
|
|
158
|
+
T = np.linalg.solve(A, B) # (2,2)
|
|
159
|
+
return T
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def mie(l, mu_a, epsilon, r, k0):
|
|
163
|
+
"""
|
|
164
|
+
Compute Mie coefficients a_l, b_l in the parity basis for a homogeneous sphere.
|
|
165
|
+
|
|
166
|
+
Args:
|
|
167
|
+
l (int or array_like of int):
|
|
168
|
+
Multipole orders.
|
|
169
|
+
mu_a (array_like, shape (2,)):
|
|
170
|
+
Relative permeabilities (mu_medium, mu_sphere).
|
|
171
|
+
epsilon (array_like, shape (2,)):
|
|
172
|
+
Relative permittivities (eps_sphere, eps_medium).
|
|
173
|
+
r (float or array_like):
|
|
174
|
+
Sphere radius. Broadcasts against `l`.
|
|
175
|
+
k0 (float):
|
|
176
|
+
Vacuum wavenumber.
|
|
177
|
+
|
|
178
|
+
Returns:
|
|
179
|
+
jax.Array:
|
|
180
|
+
Flattened coefficients of shape (2 * N,), where N = len(l).
|
|
181
|
+
For each entry l[i], the output is ordered as
|
|
182
|
+
coeffs[2*i] = a_l[i]
|
|
183
|
+
coeffs[2*i+1] = b_l[i].
|
|
184
|
+
"""
|
|
185
|
+
l = np.asarray([l]).flatten()
|
|
186
|
+
r = np.asarray([r]).flatten().astype("float64")
|
|
187
|
+
|
|
188
|
+
epsilon_m = epsilon[1] # medium
|
|
189
|
+
epsilon_p = epsilon[0] # particle
|
|
190
|
+
|
|
191
|
+
rho = (k0 * r * epsilon_m**0.5 + 0.0j).astype(
|
|
192
|
+
complex
|
|
193
|
+
) # medium # COMPLEX TO AVOID NANS for bessel & hankel
|
|
194
|
+
rho1 = (k0 * r * epsilon_p**0.5 + 0.0j).astype(complex)
|
|
195
|
+
mu = mu_a[0]
|
|
196
|
+
mu1 = mu_a[1]
|
|
197
|
+
an_n = mu * epsilon_p * (
|
|
198
|
+
rho
|
|
199
|
+
* vmap(jacrev(spherical_jn, argnums=1, holomorphic=True), in_axes=(0, 0))(
|
|
200
|
+
l, rho
|
|
201
|
+
)
|
|
202
|
+
+ spherical_jn(l, rho)
|
|
203
|
+
) * spherical_jn(l, rho1) - mu1 * epsilon_m * (
|
|
204
|
+
rho1
|
|
205
|
+
* vmap(jacrev(spherical_jn, argnums=1, holomorphic=True), in_axes=(0, 0))(
|
|
206
|
+
l, rho1
|
|
207
|
+
)
|
|
208
|
+
+ spherical_jn(l, rho1)
|
|
209
|
+
) * spherical_jn(
|
|
210
|
+
l, rho
|
|
211
|
+
)
|
|
212
|
+
an_d = mu * epsilon_p * (
|
|
213
|
+
rho
|
|
214
|
+
* vmap(grad(spherical_hankel1, argnums=1, holomorphic=True), in_axes=(0, 0))(
|
|
215
|
+
l, rho
|
|
216
|
+
)
|
|
217
|
+
+ spherical_hankel1(l, rho)
|
|
218
|
+
) * vmap(spherical_jn, in_axes=(0, 0))(l, rho1) - mu1 * epsilon_m * (
|
|
219
|
+
rho1
|
|
220
|
+
* vmap(grad(spherical_jn, argnums=1, holomorphic=True), in_axes=(0, 0))(l, rho1)
|
|
221
|
+
+ spherical_jn(l, rho1)
|
|
222
|
+
) * spherical_hankel1(
|
|
223
|
+
l, rho
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
an = an_n / an_d
|
|
227
|
+
bn_n = mu1 * (
|
|
228
|
+
rho
|
|
229
|
+
* vmap(grad(spherical_jn, argnums=1, holomorphic=True), in_axes=(0, 0))(l, rho)
|
|
230
|
+
+ spherical_jn(l, rho)
|
|
231
|
+
) * spherical_jn(l, rho1) - mu * (
|
|
232
|
+
rho1
|
|
233
|
+
* vmap(grad(spherical_jn, argnums=1, holomorphic=True), in_axes=(0, 0))(l, rho1)
|
|
234
|
+
+ spherical_jn(l, rho1)
|
|
235
|
+
) * spherical_jn(
|
|
236
|
+
l, rho
|
|
237
|
+
)
|
|
238
|
+
bn_d = mu1 * (
|
|
239
|
+
rho
|
|
240
|
+
* vmap(grad(spherical_hankel1, argnums=1, holomorphic=True), in_axes=(0, 0))(
|
|
241
|
+
l, rho
|
|
242
|
+
)
|
|
243
|
+
+ spherical_hankel1(l, rho)
|
|
244
|
+
) * spherical_jn(l, rho1) - mu * (
|
|
245
|
+
rho1
|
|
246
|
+
* vmap(grad(spherical_jn, argnums=1, holomorphic=True), in_axes=(0, 0))(l, rho1)
|
|
247
|
+
+ spherical_jn(l, rho1)
|
|
248
|
+
) * spherical_hankel1(
|
|
249
|
+
l, rho
|
|
250
|
+
)
|
|
251
|
+
bn = bn_n / bn_d
|
|
252
|
+
return np.vstack((an, bn)).T.flatten()
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def fresnel(ks, kzss, zs):
|
|
256
|
+
r"""fresnel(ks, kzs, zs)
|
|
257
|
+
|
|
258
|
+
Fresnel coefficient for a planar interface.
|
|
259
|
+
|
|
260
|
+
The first dimension contains the numbers for the two media, the second dimenison
|
|
261
|
+
indexes the polarizations.
|
|
262
|
+
|
|
263
|
+
The result is an array relating incoming light of negative (index `0`) and
|
|
264
|
+
positive (index `1`) helicity with the outgoing modes, which are indexed in the same
|
|
265
|
+
way. The first dimension of the array are the outgoing and the second dimension the
|
|
266
|
+
incoming modes.
|
|
267
|
+
|
|
268
|
+
Args:
|
|
269
|
+
ks (float or complex): Wave numbers
|
|
270
|
+
kzss (float): Z component of the waves
|
|
271
|
+
zs (float or complex): Impedances
|
|
272
|
+
|
|
273
|
+
Returns:
|
|
274
|
+
complex (2, 2, 2, 2)-array
|
|
275
|
+
"""
|
|
276
|
+
final = []
|
|
277
|
+
for _i, kzs in enumerate(kzss):
|
|
278
|
+
ap = [
|
|
279
|
+
ks[0][0] * kzs[0][1] + ks[0][1] * kzs[0][0],
|
|
280
|
+
ks[1][0] * kzs[1][1] + ks[1][1] * kzs[1][0],
|
|
281
|
+
]
|
|
282
|
+
bp = ks[0][1] * kzs[1][1] + ks[1][1] * kzs[0][1]
|
|
283
|
+
cp = ks[0][0] * kzs[1][0] + ks[1][0] * kzs[0][0]
|
|
284
|
+
am = [
|
|
285
|
+
ks[0][0] * kzs[0][1] - ks[0][1] * kzs[0][0],
|
|
286
|
+
ks[1][0] * kzs[1][1] - ks[1][1] * kzs[1][0],
|
|
287
|
+
]
|
|
288
|
+
|
|
289
|
+
zs_diff = zs[0] - zs[1]
|
|
290
|
+
zs_prod = 4 * zs[0] * zs[1]
|
|
291
|
+
pref = 1 / (zs_diff * zs_diff * ap[0] * ap[1] + zs_prod * bp * cp)
|
|
292
|
+
a000 = [
|
|
293
|
+
(zs[0] + zs[1]) * ks[1][0] * kzs[0][0] * bp * zs[1] * 4 * pref,
|
|
294
|
+
-(
|
|
295
|
+
(zs[0] - zs[1])
|
|
296
|
+
* ks[1][0]
|
|
297
|
+
* kzs[0][1]
|
|
298
|
+
* (ks[1][1] * kzs[0][0] - ks[0][0] * kzs[1][1])
|
|
299
|
+
)
|
|
300
|
+
* zs[1]
|
|
301
|
+
* 4
|
|
302
|
+
* pref,
|
|
303
|
+
]
|
|
304
|
+
a001 = [
|
|
305
|
+
-(
|
|
306
|
+
(zs[0] - zs[1])
|
|
307
|
+
* ks[1][1]
|
|
308
|
+
* kzs[0][0]
|
|
309
|
+
* (ks[1][0] * kzs[0][1] - ks[0][1] * kzs[1][0])
|
|
310
|
+
)
|
|
311
|
+
* zs[1]
|
|
312
|
+
* 4
|
|
313
|
+
* pref,
|
|
314
|
+
(zs[0] + zs[1]) * ks[1][1] * kzs[0][1] * cp * zs[1] * 4 * pref,
|
|
315
|
+
]
|
|
316
|
+
a010 = [
|
|
317
|
+
(
|
|
318
|
+
-zs_diff * zs_diff * am[1] * ap[0]
|
|
319
|
+
- zs_prod * bp * (ks[1][0] * kzs[0][0] - ks[0][0] * kzs[1][0])
|
|
320
|
+
)
|
|
321
|
+
* pref,
|
|
322
|
+
-(2 * (zs[0] * zs[0] - zs[1] * zs[1]) * ks[1][0] * kzs[1][1] * ap[0])
|
|
323
|
+
* pref,
|
|
324
|
+
]
|
|
325
|
+
a011 = [
|
|
326
|
+
-(2 * (zs[0] * zs[0] - zs[1] * zs[1]) * ks[1][1] * kzs[1][0] * ap[0])
|
|
327
|
+
* pref,
|
|
328
|
+
(
|
|
329
|
+
zs_diff * zs_diff * am[1] * ap[0]
|
|
330
|
+
- zs_prod * cp * (ks[1][1] * kzs[0][1] - ks[0][1] * kzs[1][1])
|
|
331
|
+
)
|
|
332
|
+
* pref,
|
|
333
|
+
]
|
|
334
|
+
a100 = [
|
|
335
|
+
(
|
|
336
|
+
-zs_diff * zs_diff * am[0] * ap[1]
|
|
337
|
+
- zs_prod * bp * (ks[0][0] * kzs[1][0] - ks[1][0] * kzs[0][0])
|
|
338
|
+
)
|
|
339
|
+
* pref,
|
|
340
|
+
-(2 * (zs[1] * zs[1] - zs[0] * zs[0]) * ks[0][0] * kzs[0][1] * ap[1])
|
|
341
|
+
* pref,
|
|
342
|
+
]
|
|
343
|
+
a101 = [
|
|
344
|
+
-(2 * (zs[1] * zs[1] - zs[0] * zs[0]) * ks[0][1] * kzs[0][0] * ap[1])
|
|
345
|
+
* pref,
|
|
346
|
+
(
|
|
347
|
+
zs_diff * zs_diff * am[0] * ap[1]
|
|
348
|
+
- zs_prod * cp * (ks[0][1] * kzs[1][1] - ks[1][1] * kzs[0][1])
|
|
349
|
+
)
|
|
350
|
+
* pref,
|
|
351
|
+
]
|
|
352
|
+
a110 = [
|
|
353
|
+
(zs[0] + zs[1]) * ks[0][0] * kzs[1][0] * bp * zs[0] * 4 * pref,
|
|
354
|
+
-(
|
|
355
|
+
(zs[1] - zs[0])
|
|
356
|
+
* ks[0][0]
|
|
357
|
+
* kzs[1][1]
|
|
358
|
+
* (ks[0][1] * kzs[1][0] - ks[1][0] * kzs[0][1])
|
|
359
|
+
)
|
|
360
|
+
* zs[0]
|
|
361
|
+
* 4
|
|
362
|
+
* pref,
|
|
363
|
+
]
|
|
364
|
+
a111 = [
|
|
365
|
+
-(
|
|
366
|
+
(zs[1] - zs[0])
|
|
367
|
+
* ks[0][1]
|
|
368
|
+
* kzs[1][0]
|
|
369
|
+
* (ks[0][0] * kzs[1][1] - ks[1][1] * kzs[0][0])
|
|
370
|
+
)
|
|
371
|
+
* zs[0]
|
|
372
|
+
* 4
|
|
373
|
+
* pref,
|
|
374
|
+
(zs[0] + zs[1]) * ks[0][1] * kzs[1][1] * cp * zs[0] * 4 * pref,
|
|
375
|
+
]
|
|
376
|
+
res = np.array([[[a000, a001], [a010, a011]], [[a100, a101], [a110, a111]]])
|
|
377
|
+
|
|
378
|
+
final.append(res)
|
|
379
|
+
final = np.stack(final, axis=0)
|
|
380
|
+
|
|
381
|
+
return final
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import numpy as anp
|
|
2
|
+
import jax
|
|
3
|
+
import jax.numpy as np
|
|
4
|
+
from jax import config
|
|
5
|
+
|
|
6
|
+
config.update("jax_enable_x64", True)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def car2pol(car):
|
|
10
|
+
"""
|
|
11
|
+
Convert Cartesian to polar coordinates.
|
|
12
|
+
|
|
13
|
+
This works on arrays of shape (..., 2), where the last axis is [x, y].
|
|
14
|
+
|
|
15
|
+
Args:
|
|
16
|
+
car (array_like): Cartesian coordinates [..., 2] with components [x, y].
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
array: Polar coordinates [..., 2] with components [r, phi],
|
|
20
|
+
r >= 0 and phi in [-pi, pi].
|
|
21
|
+
"""
|
|
22
|
+
car = np.asarray(car)
|
|
23
|
+
x = car[..., 0]
|
|
24
|
+
y = car[..., 1]
|
|
25
|
+
r = np.hypot(x, y)
|
|
26
|
+
phi = np.arctan2(y, x)
|
|
27
|
+
return np.stack([r, phi], axis=-1)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def subc2s(th):
|
|
31
|
+
"""
|
|
32
|
+
th: (x, y, z) -> (r, theta, phi)
|
|
33
|
+
double where trick:avoid nans even in the branch that is not chosen
|
|
34
|
+
"""
|
|
35
|
+
cond0 = (th[0] == 0.0) & (th[1] == 0.0)
|
|
36
|
+
denomx = np.where(cond0, 1.0, th[0])
|
|
37
|
+
denomz = np.where(cond0, 1.0, th[2])
|
|
38
|
+
xy_safe = np.hypot(th[1], denomx)
|
|
39
|
+
a1 = np.hypot(xy_safe, denomz)
|
|
40
|
+
a2 = np.abs(np.arctan2(xy_safe, denomz))
|
|
41
|
+
a3 = np.arctan2(th[1], denomx)
|
|
42
|
+
a = np.array([a1, a2, a3])
|
|
43
|
+
anulp = np.array([np.abs(th[2]), 0.0, 0])
|
|
44
|
+
# Note: this explicit z-axis handling (x = y = 0) differs from treams.car2sph,
|
|
45
|
+
# so values on the z-axis are not bitwise-identical to treams. It is required
|
|
46
|
+
# to avoid NaNs / gradient issues with JAX at the origin.
|
|
47
|
+
anuln = np.array([np.abs(th[2]), np.pi, 0])
|
|
48
|
+
conds = [(th[2] < 0) & cond0, (th[2] >= 0) & cond0]
|
|
49
|
+
ans = np.select(conds, [anuln, anulp], a)
|
|
50
|
+
return ans
|
|
51
|
+
|
|
52
|
+
@jax.jit
|
|
53
|
+
def car2sph(thg):
|
|
54
|
+
r"""
|
|
55
|
+
Vectorized Cartesian → spherical coordinate transform.
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
thg (ndarray):
|
|
59
|
+
Coordinates in Cartesian form. Accepted shapes:
|
|
60
|
+
|
|
61
|
+
- ``(3,)`` for a single point,
|
|
62
|
+
- ``(N, 3)`` for a list of points,
|
|
63
|
+
- ``(M, N, 3)`` for a grid of points.
|
|
64
|
+
thg[..., 0] = x
|
|
65
|
+
thg[..., 1] = y
|
|
66
|
+
thg[..., 2] = z
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
ndarray:
|
|
70
|
+
Same shape as ``thg``, but with the last axis holding
|
|
71
|
+
spherical coordinates ``(r, theta, phi)`` instead of
|
|
72
|
+
``(x, y, z)``.
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
x = thg[..., 0]
|
|
76
|
+
y = thg[..., 1]
|
|
77
|
+
z = thg[..., 2]
|
|
78
|
+
|
|
79
|
+
cond0 = (x == 0.0) & (y == 0.0)
|
|
80
|
+
|
|
81
|
+
denomx = np.where(cond0, 1.0, x)
|
|
82
|
+
denomz = np.where(cond0, 1.0, z)
|
|
83
|
+
|
|
84
|
+
xy_safe = np.hypot(y, denomx)
|
|
85
|
+
a1 = np.hypot(xy_safe, denomz)
|
|
86
|
+
a2 = np.abs(np.arctan2(xy_safe, denomz))
|
|
87
|
+
a3 = np.arctan2(y, denomx)
|
|
88
|
+
|
|
89
|
+
a = np.stack([a1, a2, a3], axis=-1)
|
|
90
|
+
|
|
91
|
+
zeros = np.zeros_like(z)
|
|
92
|
+
anulp = np.stack([np.abs(z), zeros, zeros], axis=-1)
|
|
93
|
+
anuln = np.stack([np.abs(z), np.full_like(z, np.pi), zeros], axis=-1)
|
|
94
|
+
|
|
95
|
+
ans = np.where((cond0 & (z >= 0))[..., None], anulp, a)
|
|
96
|
+
ans = np.where((cond0 & (z < 0))[..., None], anuln, ans)
|
|
97
|
+
|
|
98
|
+
return ans
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def vsph2car(iv, ip):
|
|
102
|
+
"""Convert vector from spherical to cartesian basis"""
|
|
103
|
+
|
|
104
|
+
# def v2c(iv, ip)
|
|
105
|
+
ip1 = ip[:, :, 1]
|
|
106
|
+
ip0 = ip[:, :, 0]
|
|
107
|
+
ip2 = ip[:, :, 2]
|
|
108
|
+
iv1 = iv[:, :, 1]
|
|
109
|
+
iv0 = iv[:, :, 0]
|
|
110
|
+
iv2 = iv[:, :, 2]
|
|
111
|
+
vy = (
|
|
112
|
+
np.sin(ip1) * np.sin(ip2) * iv0
|
|
113
|
+
+ np.cos(ip1) * np.sin(ip2) * iv1
|
|
114
|
+
+ np.cos(ip2) * iv2
|
|
115
|
+
)
|
|
116
|
+
vz = np.cos(ip1) * iv0 - np.sin(ip1) * iv1
|
|
117
|
+
return np.array(
|
|
118
|
+
[
|
|
119
|
+
np.sin(ip1) * np.cos(ip2) * iv0
|
|
120
|
+
+ np.cos(ip1) * np.cos(ip2) * iv1
|
|
121
|
+
- np.sin(ip2) * iv2,
|
|
122
|
+
vy,
|
|
123
|
+
vz,
|
|
124
|
+
]
|
|
125
|
+
).transpose(1, 2, 0)
|