-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgradient_optimization_example.py
More file actions
194 lines (153 loc) · 7.24 KB
/
Copy pathgradient_optimization_example.py
File metadata and controls
194 lines (153 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""Gradient-based ECRH beam steering using jax.grad through the ray tracer.
``trace(..., trim=False)`` returns a TraceResult whose BeamProfile contains the
full padded tensor output of the ODE solve, differentiable w.r.t. beam.position
and beam.direction. Any scalar loss defined from those tensors can be
differentiated with jax.grad.
"""
import jax
jax.config.update("jax_enable_x64", True)
import jax.numpy as jnp # noqa: E402
import numpy as np # noqa: E402
from raytrax import Beam, MagneticConfiguration, RadialProfiles, trace # noqa: E402
# ─── 1. Analytic tokamak equilibrium ──────────────────────────────────────────
#
# Circular cross-section, vacuum toroidal field B_phi = B0*R0/R.
# At R = R0 = 3 m, |B| = B0 = 2.5 T → f_ce ≈ 70 GHz → 2nd harmonic 140 GHz.
R0, a, B0 = 3.0, 1.0, 2.5
n_R, n_Z = 50, 60
R_grid = jnp.linspace(R0 - 1.5 * a, R0 + 1.5 * a, n_R)
Z_grid = jnp.linspace(-1.5 * a, 1.5 * a, n_Z)
R_2d, Z_2d = jnp.meshgrid(R_grid, Z_grid, indexing="ij")
B_phi = B0 * R0 / R_2d
rho_2d = jnp.sqrt((R_2d - R0) ** 2 + Z_2d**2) / a
phi_grid = jnp.array([0.0])
rphiz = jnp.stack(jnp.meshgrid(R_grid, phi_grid, Z_grid, indexing="ij"), axis=-1)
mag_field_grid = jnp.stack(
[jnp.zeros_like(R_2d), B_phi, jnp.zeros_like(R_2d)], axis=-1
)[:, jnp.newaxis, :, :]
rho_grid = rho_2d[:, jnp.newaxis, :]
rho_1d = jnp.linspace(0, 1, 200)
dvolume_drho = 4.0 * jnp.pi**2 * R0 * a**2 * rho_1d
eq_interp = MagneticConfiguration(
rphiz=rphiz,
magnetic_field=mag_field_grid,
rho=rho_grid,
nfp=1,
is_stellarator_symmetric=False,
rho_1d=rho_1d,
dvolume_drho=dvolume_drho,
is_axisymmetric=True,
)
# ─── 2. Parabolic plasma profiles ─────────────────────────────────────────────
rho_prof = jnp.linspace(0, 1, 200)
profiles = RadialProfiles(
rho=rho_prof,
electron_density=0.5 * (1.0 - rho_prof**2),
electron_temperature=3.0 * (1.0 - rho_prof**2),
)
# ─── 3. Loss functions defined on BeamProfile ──────────────────────────────────
#
# trace(..., trim=False) returns a TraceResult whose beam_profile contains padded
# arrays (4097 slots: t0 + up to 4096 ODE steps). All fields are differentiable
# w.r.t. beam.position and beam.direction. Padded entries have
# linear_power_density=0, so jnp.max and weighted jnp.sum are correct without
# any explicit trimming.
#
# Note: each call rebuilds interpolator Python objects (negligible for small
# grids; for W7-X-scale grids build them once and call trace_jitted directly).
def make_beam(position, direction):
return Beam(
position=position,
direction=direction,
frequency=jnp.array(140e9),
mode="O",
power=1e6,
)
def absorbed_fraction(position: jax.Array, direction: jax.Array) -> jax.Array:
"""Total absorbed power fraction 1 − exp(−τ)."""
result = trace(eq_interp, profiles, make_beam(position, direction), trim=False)
# diffrax fills unused buffer slots with inf; ignore them when taking the max.
tau_final = jnp.max(
jnp.where(
jnp.isfinite(result.beam_profile.optical_depth),
result.beam_profile.optical_depth,
0.0,
)
)
return 1.0 - jnp.exp(-tau_final)
def deposition_centroid(
position: jax.Array, direction: jax.Array, target_rho: float = 0.5
) -> jax.Array:
"""Gaussian-weighted linear power density centred at target_rho.
Maximizing this steers the deposition toward the desired flux surface.
"""
result = trace(eq_interp, profiles, make_beam(position, direction), trim=False)
w = jnp.exp(
-(((result.beam_profile.normalized_effective_radius - target_rho) / 0.1) ** 2)
)
return jnp.sum(result.beam_profile.linear_power_density * w)
# ─── 4. Forward pass ──────────────────────────────────────────────────────────
position = jnp.array([R0 + 1.5 * a, 0.0, 0.0]) # outer midplane, ρ > 1
direction = jnp.array([-1.0, 0.0, 0.0]) # inward
print("=" * 60)
print("Forward pass")
print("=" * 60)
print(f" Absorbed fraction: {float(absorbed_fraction(position, direction)):.6f}")
print(
f" Deposition @ ρ=0.5: {float(deposition_centroid(position, direction)):.6f}"
)
# ─── 5. Gradients ─────────────────────────────────────────────────────────────
print("\n" + "=" * 60)
print("Gradients via jax.grad")
print("=" * 60)
grad_pos, grad_dir = jax.grad(absorbed_fraction, argnums=(0, 1))(position, direction)
print(f" ∂(absorbed)/∂position = {np.array(grad_pos)}")
print(f" ∂(absorbed)/∂direction = {np.array(grad_dir)}")
grad_pos2, grad_dir2 = jax.grad(deposition_centroid, argnums=(0, 1))(
position, direction
)
print(f" ∂(centroid)/∂position = {np.array(grad_pos2)}")
print(f" ∂(centroid)/∂direction = {np.array(grad_dir2)}")
# ─── 6. Finite-difference check ───────────────────────────────────────────────
#
# Parametrise beam direction as (−cos θ, 0, sin θ) in the R–Z plane.
# Keeping N_y = 0 exactly avoids the adjoint instability tan(θ) = N_⊥/N_∥ → ∞
# that occurs when N_y drifts to floating-point noise after gradient steps.
@jax.jit
def absorbed_vs_angle(theta: jax.Array) -> jax.Array:
d = jnp.array([-jnp.cos(theta), 0.0, jnp.sin(theta)])
return absorbed_fraction(position, d)
theta_ref = jnp.array(0.3)
eps = 1e-5
fd = (
float(absorbed_vs_angle(theta_ref + eps))
- float(absorbed_vs_angle(theta_ref - eps))
) / (2 * eps)
ad = float(jax.grad(absorbed_vs_angle)(theta_ref))
print("\n" + "=" * 60)
print("Finite-difference check (angle θ, central, ε = 1e-5 rad)")
print("=" * 60)
print(f" dP/dθ (FD) : {fd:.4f}")
print(f" dP/dθ (AD) : {ad:.4f}")
print(f" Relative error : {abs(fd - ad) / (abs(fd) + 1e-12):.4e}")
# ─── 7. Gradient-ascent optimization ──────────────────────────────────────────
#
# Maximize absorbed fraction by steering θ in the R–Z plane.
# N_y = 0 is mandatory: free N_y causes gradient explosion ~1/N_y² in adjoint.
print("\n" + "=" * 60)
print("Gradient ascent: maximize absorbed fraction (poloidal beam angle)")
print("=" * 60)
lr = 0.05
theta = jnp.array(0.3)
value_and_grad_angle = jax.jit(jax.value_and_grad(absorbed_vs_angle))
for step in range(20):
p_val, g_theta = value_and_grad_angle(theta)
if step % 5 == 0 or step == 19:
d_theta = jnp.array([-jnp.cos(theta), 0.0, jnp.sin(theta)])
print(
f" step {step:2d}: P_abs = {float(p_val):.6f}, θ = {float(theta):.4f} rad,"
f" dP/dθ = {float(g_theta):.4f}, dir = {np.array(d_theta)}"
)
theta = theta + lr * g_theta
print(f"\nFinal absorbed fraction: {float(absorbed_vs_angle(theta)):.6f}")
print(f"Final angle: θ = {float(theta):.4f} rad = {float(theta) * 180 / np.pi:.2f}°")