drbx 2.0.0.dev0__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.
- drbx/__init__.py +20 -0
- drbx/__main__.py +5 -0
- drbx/cli.py +586 -0
- drbx/config/__init__.py +20 -0
- drbx/config/boutinp.py +556 -0
- drbx/config/model.py +34 -0
- drbx/config/normalization.py +66 -0
- drbx/data/atomic_rates/__init__.py +1 -0
- drbx/data/atomic_rates/iz_AMJUEL_H.x_2.1.5.json +213 -0
- drbx/data/atomic_rates/iz_AMJUEL_H.x_2.3.9a.json +213 -0
- drbx/data/atomic_rates/rec_AMJUEL_H.x_2.1.8.json +213 -0
- drbx/data/atomic_rates/rec_AMJUEL_H.x_2.3.13a.json +213 -0
- drbx/geometry/__init__.py +207 -0
- drbx/geometry/embedding.py +56 -0
- drbx/geometry/essos_import.py +1385 -0
- drbx/geometry/fci_geometry.py +4622 -0
- drbx/geometry/fci_maps.py +85 -0
- drbx/geometry/island_divertor.py +291 -0
- drbx/geometry/metric_tensor.py +99 -0
- drbx/geometry/open_slab.py +150 -0
- drbx/geometry/rotating_ellipse.py +253 -0
- drbx/geometry/shifted_torus.py +225 -0
- drbx/geometry/stellarator.py +287 -0
- drbx/geometry/vmec_extender_import.py +499 -0
- drbx/geometry/vmec_jax_import.py +306 -0
- drbx/linear/__init__.py +37 -0
- drbx/linear/dispersion.py +138 -0
- drbx/linear/eigen.py +91 -0
- drbx/native/__init__.py +224 -0
- drbx/native/array_backend.py +64 -0
- drbx/native/deck_runner.py +779 -0
- drbx/native/electromagnetic.py +250 -0
- drbx/native/expression.py +173 -0
- drbx/native/fci.py +295 -0
- drbx/native/fci_2_field_rhs.py +182 -0
- drbx/native/fci_4_field_rhs.py +1267 -0
- drbx/native/fci_boundaries.py +2494 -0
- drbx/native/fci_differentiable_case.py +304 -0
- drbx/native/fci_drb_EB_rhs.py +1243 -0
- drbx/native/fci_drb_rhs.py +190 -0
- drbx/native/fci_halo.py +1575 -0
- drbx/native/fci_helpers.py +350 -0
- drbx/native/fci_model.py +294 -0
- drbx/native/fci_neutral.py +139 -0
- drbx/native/fci_operators.py +4081 -0
- drbx/native/fci_sharding.py +597 -0
- drbx/native/fci_sheath_recycling.py +206 -0
- drbx/native/fci_time_integrator.py +96 -0
- drbx/native/fci_vorticity.py +198 -0
- drbx/native/fluid_1d.py +330 -0
- drbx/native/hasegawa_wakatani.py +196 -0
- drbx/native/limiters.py +57 -0
- drbx/native/mesh.py +238 -0
- drbx/native/metrics.py +234 -0
- drbx/native/neutrals/__init__.py +58 -0
- drbx/native/neutrals/atomic_rates.py +134 -0
- drbx/native/neutrals/detachment_sol_model.py +221 -0
- drbx/native/neutrals/reactions.py +164 -0
- drbx/native/neutrals/recycling_sol_model.py +197 -0
- drbx/native/sol_flux_tube.py +133 -0
- drbx/native/stellarator_turbulence.py +343 -0
- drbx/native/transport.py +134 -0
- drbx/native/units.py +32 -0
- drbx/native/vorticity.py +252 -0
- drbx/runtime/__init__.py +53 -0
- drbx/runtime/artifacts.py +161 -0
- drbx/runtime/memory.py +144 -0
- drbx/runtime/output.py +374 -0
- drbx/runtime/paths.py +9 -0
- drbx/runtime/performance.py +161 -0
- drbx/runtime/run_config.py +184 -0
- drbx/runtime/scheduler.py +99 -0
- drbx/runtime/state.py +40 -0
- drbx/validation/__init__.py +424 -0
- drbx/validation/autodiff_diffusion.py +329 -0
- drbx/validation/autodiff_diffusion_uncertainty.py +235 -0
- drbx/validation/diverted_tokamak_movie.py +558 -0
- drbx/validation/essos_fieldline_import_campaign.py +181 -0
- drbx/validation/essos_imported_artifact_audit.py +535 -0
- drbx/validation/essos_imported_drb_movie_campaign.py +2826 -0
- drbx/validation/essos_imported_fci_campaign.py +5241 -0
- drbx/validation/essos_imported_pytree_campaign.py +406 -0
- drbx/validation/essos_vmec_closed_field_campaign.py +314 -0
- drbx/validation/essos_vmec_closed_field_transient_campaign.py +629 -0
- drbx/validation/essos_vmec_fieldline_surface_campaign.py +620 -0
- drbx/validation/fluid_1d_mms_convergence.py +250 -0
- drbx/validation/geometry_lineouts.py +136 -0
- drbx/validation/geometry_slices.py +178 -0
- drbx/validation/publication_plotting.py +91 -0
- drbx/validation/stellarator_drb_pytree_campaign.py +621 -0
- drbx/validation/stellarator_fci_geometry_campaign.py +200 -0
- drbx/validation/stellarator_fci_operator_campaign.py +304 -0
- drbx/validation/stellarator_fci_suite_campaign.py +264 -0
- drbx/validation/stellarator_metric_mms_campaign.py +289 -0
- drbx/validation/stellarator_neutral_physics_campaign.py +255 -0
- drbx/validation/stellarator_sheath_recycling_campaign.py +331 -0
- drbx/validation/stellarator_sol_showcase.py +628 -0
- drbx/validation/stellarator_vorticity_campaign.py +304 -0
- drbx/validation/vmec_extender_edge_field_campaign.py +260 -0
- drbx/validation/vmec_extender_sol_smoke_campaign.py +365 -0
- drbx-2.0.0.dev0.dist-info/METADATA +380 -0
- drbx-2.0.0.dev0.dist-info/RECORD +106 -0
- drbx-2.0.0.dev0.dist-info/WHEEL +5 -0
- drbx-2.0.0.dev0.dist-info/entry_points.txt +2 -0
- drbx-2.0.0.dev0.dist-info/licenses/LICENSE +21 -0
- drbx-2.0.0.dev0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import time
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
import jax
|
|
10
|
+
import jax.numpy as jnp
|
|
11
|
+
from matplotlib import pyplot as plt
|
|
12
|
+
import numpy as np
|
|
13
|
+
|
|
14
|
+
from ..geometry import EssosImportedFciGeometry, build_essos_imported_fci_geometry
|
|
15
|
+
from ..native.fci_drb_rhs import FciDrbRhsParameters, FciDrbState, compute_fci_drb_rhs
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True)
|
|
19
|
+
class EssosImportedPytreeCampaignArtifacts:
|
|
20
|
+
report_json_path: Path
|
|
21
|
+
arrays_npz_path: Path
|
|
22
|
+
plot_png_path: Path
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def create_essos_imported_pytree_campaign_package(
|
|
26
|
+
*,
|
|
27
|
+
output_root: str | Path,
|
|
28
|
+
case_label: str = "essos_imported_pytree_campaign",
|
|
29
|
+
coil_json_path: str | Path | None = None,
|
|
30
|
+
vmec_wout_path: str | Path | None = None,
|
|
31
|
+
essos_root: str | Path | None = None,
|
|
32
|
+
map_source: str = "coil",
|
|
33
|
+
nx: int = 4,
|
|
34
|
+
ny: int = 6,
|
|
35
|
+
nz: int = 12,
|
|
36
|
+
rho_min: float = 0.12,
|
|
37
|
+
rho_max: float = 0.34,
|
|
38
|
+
maxtime: float = 60.0,
|
|
39
|
+
times_to_trace: int = 280,
|
|
40
|
+
steps: int = 5,
|
|
41
|
+
) -> EssosImportedPytreeCampaignArtifacts:
|
|
42
|
+
root = Path(output_root)
|
|
43
|
+
data_dir = root / "data"
|
|
44
|
+
images_dir = root / "images"
|
|
45
|
+
data_dir.mkdir(parents=True, exist_ok=True)
|
|
46
|
+
images_dir.mkdir(parents=True, exist_ok=True)
|
|
47
|
+
|
|
48
|
+
report, arrays = build_essos_imported_pytree_campaign(
|
|
49
|
+
coil_json_path=coil_json_path,
|
|
50
|
+
vmec_wout_path=vmec_wout_path,
|
|
51
|
+
essos_root=essos_root,
|
|
52
|
+
map_source=map_source,
|
|
53
|
+
nx=nx,
|
|
54
|
+
ny=ny,
|
|
55
|
+
nz=nz,
|
|
56
|
+
rho_min=rho_min,
|
|
57
|
+
rho_max=rho_max,
|
|
58
|
+
maxtime=maxtime,
|
|
59
|
+
times_to_trace=times_to_trace,
|
|
60
|
+
steps=steps,
|
|
61
|
+
)
|
|
62
|
+
report_json_path = data_dir / f"{case_label}.json"
|
|
63
|
+
report_json_path.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
|
64
|
+
arrays_npz_path = data_dir / f"{case_label}.npz"
|
|
65
|
+
np.savez_compressed(arrays_npz_path, **arrays)
|
|
66
|
+
plot_png_path = images_dir / f"{case_label}.png"
|
|
67
|
+
save_essos_imported_pytree_campaign_plot(report, arrays, plot_png_path)
|
|
68
|
+
return EssosImportedPytreeCampaignArtifacts(
|
|
69
|
+
report_json_path=report_json_path,
|
|
70
|
+
arrays_npz_path=arrays_npz_path,
|
|
71
|
+
plot_png_path=plot_png_path,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def build_essos_imported_pytree_campaign(
|
|
76
|
+
*,
|
|
77
|
+
coil_json_path: str | Path | None = None,
|
|
78
|
+
vmec_wout_path: str | Path | None = None,
|
|
79
|
+
essos_root: str | Path | None = None,
|
|
80
|
+
map_source: str = "coil",
|
|
81
|
+
nx: int = 4,
|
|
82
|
+
ny: int = 6,
|
|
83
|
+
nz: int = 12,
|
|
84
|
+
rho_min: float = 0.12,
|
|
85
|
+
rho_max: float = 0.34,
|
|
86
|
+
maxtime: float = 60.0,
|
|
87
|
+
times_to_trace: int = 280,
|
|
88
|
+
steps: int = 5,
|
|
89
|
+
) -> tuple[dict[str, Any], dict[str, np.ndarray]]:
|
|
90
|
+
geometry = build_essos_imported_fci_geometry(
|
|
91
|
+
coil_json_path=coil_json_path,
|
|
92
|
+
vmec_wout_path=vmec_wout_path,
|
|
93
|
+
essos_root=essos_root,
|
|
94
|
+
map_source=map_source,
|
|
95
|
+
nx=nx,
|
|
96
|
+
ny=ny,
|
|
97
|
+
nz=nz,
|
|
98
|
+
rho_min=rho_min,
|
|
99
|
+
rho_max=rho_max,
|
|
100
|
+
maxtime=maxtime,
|
|
101
|
+
times_to_trace=times_to_trace,
|
|
102
|
+
)
|
|
103
|
+
parameters = FciDrbRhsParameters(
|
|
104
|
+
recycling_fraction=0.965,
|
|
105
|
+
recycled_neutral_energy=0.026,
|
|
106
|
+
potential_iterations=64,
|
|
107
|
+
potential_regularization=1.0,
|
|
108
|
+
)
|
|
109
|
+
dt = 1.0e-5
|
|
110
|
+
run_transient = _build_imported_transient(geometry, parameters=parameters, dt=dt, steps=steps)
|
|
111
|
+
initial = initial_essos_imported_drb_state(geometry, drive_scale=1.0)
|
|
112
|
+
|
|
113
|
+
t0 = time.perf_counter()
|
|
114
|
+
final_state, history = run_transient(initial)
|
|
115
|
+
_block_until_ready((final_state, history))
|
|
116
|
+
compile_and_first_execute_seconds = time.perf_counter() - t0
|
|
117
|
+
|
|
118
|
+
t1 = time.perf_counter()
|
|
119
|
+
final_state, history = run_transient(initial)
|
|
120
|
+
_block_until_ready((final_state, history))
|
|
121
|
+
warm_execute_seconds = time.perf_counter() - t1
|
|
122
|
+
|
|
123
|
+
objective = _build_imported_objective(geometry, parameters=parameters, dt=dt, steps=steps)
|
|
124
|
+
value, jvp_derivative = jax.jvp(
|
|
125
|
+
objective,
|
|
126
|
+
(jnp.asarray(1.0, dtype=jnp.float64),),
|
|
127
|
+
(jnp.asarray(1.0, dtype=jnp.float64),),
|
|
128
|
+
)
|
|
129
|
+
_block_until_ready((value, jvp_derivative))
|
|
130
|
+
eps = 1.0e-3
|
|
131
|
+
finite_difference = (objective(1.0 + eps) - objective(1.0 - eps)) / (2.0 * eps)
|
|
132
|
+
_block_until_ready(finite_difference)
|
|
133
|
+
jvp_relative_error = float(
|
|
134
|
+
jnp.abs(jvp_derivative - finite_difference) / jnp.maximum(jnp.abs(finite_difference), 1.0e-14)
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
batched_objective = jax.jit(jax.vmap(objective))
|
|
138
|
+
serial_scales = np.linspace(0.90, 1.10, 4)
|
|
139
|
+
serial_values = np.asarray([float(objective(scale)) for scale in serial_scales], dtype=np.float64)
|
|
140
|
+
vmap_values = np.asarray(batched_objective(jnp.asarray(serial_scales, dtype=jnp.float64)), dtype=np.float64)
|
|
141
|
+
_block_until_ready(vmap_values)
|
|
142
|
+
vmap_serial_linf = float(np.max(np.abs(serial_values - vmap_values)))
|
|
143
|
+
|
|
144
|
+
batch_sizes = np.asarray([1, 2, 4], dtype=np.int64)
|
|
145
|
+
batch_times = []
|
|
146
|
+
for batch_size in batch_sizes:
|
|
147
|
+
scales = jnp.linspace(0.90, 1.10, int(batch_size), dtype=jnp.float64)
|
|
148
|
+
batched_objective(scales).block_until_ready()
|
|
149
|
+
t_batch = time.perf_counter()
|
|
150
|
+
batched_objective(scales).block_until_ready()
|
|
151
|
+
batch_times.append(time.perf_counter() - t_batch)
|
|
152
|
+
|
|
153
|
+
history_np = np.asarray(history, dtype=np.float64)
|
|
154
|
+
final_ion = np.asarray(final_state.ion_density, dtype=np.float64)
|
|
155
|
+
final_neutral = np.asarray(final_state.neutral_density, dtype=np.float64)
|
|
156
|
+
final_vorticity = np.asarray(final_state.vorticity, dtype=np.float64)
|
|
157
|
+
bmag = np.asarray(geometry.magnetic_field_magnitude, dtype=np.float64)
|
|
158
|
+
endpoint_fraction = float(
|
|
159
|
+
np.mean(np.asarray(geometry.maps.forward_boundary, dtype=bool) | np.asarray(geometry.maps.backward_boundary, dtype=bool))
|
|
160
|
+
)
|
|
161
|
+
actual_map_source = str(geometry.metadata.get("map_source", "coil"))
|
|
162
|
+
endpoint_gate = endpoint_fraction < 1.0e-12 if actual_map_source == "vmec" else 0.05 < endpoint_fraction <= 1.0
|
|
163
|
+
b_modulation_gate = 1.01 if actual_map_source == "vmec" else 1.05
|
|
164
|
+
min_density = float(min(np.min(final_ion), np.min(final_neutral)))
|
|
165
|
+
report: dict[str, Any] = {
|
|
166
|
+
"case": "essos_imported_fci_drb_pytree_transient",
|
|
167
|
+
"source": "ESSOS-imported field-line maps with DRBX fixed-layout PyTree RHS",
|
|
168
|
+
"map_source": actual_map_source,
|
|
169
|
+
"geometry": geometry.metadata,
|
|
170
|
+
"steps": int(steps),
|
|
171
|
+
"dt": float(dt),
|
|
172
|
+
"compile_and_first_execute_seconds": float(compile_and_first_execute_seconds),
|
|
173
|
+
"warm_execute_seconds": float(warm_execute_seconds),
|
|
174
|
+
"jvp_objective_value": float(value),
|
|
175
|
+
"jvp_derivative": float(jvp_derivative),
|
|
176
|
+
"finite_difference_derivative": float(finite_difference),
|
|
177
|
+
"jvp_relative_error": jvp_relative_error,
|
|
178
|
+
"vmap_serial_linf": vmap_serial_linf,
|
|
179
|
+
"batch_sizes": batch_sizes.tolist(),
|
|
180
|
+
"batch_execute_seconds": [float(item) for item in batch_times],
|
|
181
|
+
"batch_throughput_cases_per_second": [
|
|
182
|
+
float(size / max(seconds, 1.0e-30)) for size, seconds in zip(batch_sizes, batch_times, strict=True)
|
|
183
|
+
],
|
|
184
|
+
"endpoint_fraction": endpoint_fraction,
|
|
185
|
+
"magnetic_field_modulation": float(np.max(bmag) / max(float(np.min(bmag)), 1.0e-30)),
|
|
186
|
+
"final_min_density": min_density,
|
|
187
|
+
"final_ion_density_mean": float(np.mean(final_ion)),
|
|
188
|
+
"final_neutral_density_mean": float(np.mean(final_neutral)),
|
|
189
|
+
"final_vorticity_rms": float(np.sqrt(np.mean(final_vorticity * final_vorticity))),
|
|
190
|
+
"final_potential_residual_l2": float(history_np[-1, 4]),
|
|
191
|
+
}
|
|
192
|
+
report["passed"] = (
|
|
193
|
+
np.all(np.isfinite(history_np))
|
|
194
|
+
and min_density > 0.0
|
|
195
|
+
and endpoint_gate
|
|
196
|
+
and report["magnetic_field_modulation"] > b_modulation_gate
|
|
197
|
+
and report["final_vorticity_rms"] > 0.0
|
|
198
|
+
and report["final_potential_residual_l2"] < 2.5
|
|
199
|
+
and jvp_relative_error < 1.0e-2
|
|
200
|
+
and vmap_serial_linf < 1.0e-6
|
|
201
|
+
and report["warm_execute_seconds"] > 0.0
|
|
202
|
+
)
|
|
203
|
+
arrays = {
|
|
204
|
+
"time_index": np.arange(history_np.shape[0], dtype=np.int64),
|
|
205
|
+
"history": history_np.astype(np.float32),
|
|
206
|
+
"final_ion_density_section": final_ion[:, 0, :].astype(np.float32),
|
|
207
|
+
"final_neutral_density_section": final_neutral[:, 0, :].astype(np.float32),
|
|
208
|
+
"final_vorticity_section": final_vorticity[:, 0, :].astype(np.float32),
|
|
209
|
+
"endpoint_count_toroidal": (
|
|
210
|
+
np.asarray(geometry.maps.forward_boundary, dtype=np.float64)
|
|
211
|
+
+ np.asarray(geometry.maps.backward_boundary, dtype=np.float64)
|
|
212
|
+
).sum(axis=0).astype(np.float32),
|
|
213
|
+
"magnetic_field_section": bmag[:, 0, :].astype(np.float32),
|
|
214
|
+
"serial_scales_4": serial_scales.astype(np.float32),
|
|
215
|
+
"serial_values_4": serial_values.astype(np.float32),
|
|
216
|
+
"vmap_values_4": vmap_values.astype(np.float32),
|
|
217
|
+
"batch_sizes": batch_sizes.astype(np.float32),
|
|
218
|
+
"batch_execute_seconds": np.asarray(batch_times, dtype=np.float32),
|
|
219
|
+
"jvp_summary": np.asarray([float(jvp_derivative), float(finite_difference), jvp_relative_error], dtype=np.float32),
|
|
220
|
+
}
|
|
221
|
+
return report, arrays
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def initial_essos_imported_drb_state(
|
|
225
|
+
geometry: EssosImportedFciGeometry,
|
|
226
|
+
*,
|
|
227
|
+
drive_scale: jax.Array | float = 1.0,
|
|
228
|
+
) -> FciDrbState:
|
|
229
|
+
rho = geometry.minor_radius
|
|
230
|
+
rho_min = jnp.min(rho)
|
|
231
|
+
rho_span = jnp.maximum(jnp.max(rho) - rho_min, 1.0e-12)
|
|
232
|
+
radial = (rho - rho_min) / rho_span
|
|
233
|
+
theta = geometry.poloidal_angle
|
|
234
|
+
phi = geometry.toroidal_angle
|
|
235
|
+
bnorm = geometry.magnetic_field_magnitude / jnp.maximum(jnp.mean(geometry.magnetic_field_magnitude), 1.0e-12)
|
|
236
|
+
helical = jnp.cos(2.0 * theta - phi)
|
|
237
|
+
sideband = jnp.sin(theta + 2.0 * phi)
|
|
238
|
+
scale = jnp.asarray(drive_scale, dtype=jnp.float64)
|
|
239
|
+
ion_density = 0.55 + scale * 0.70 * jnp.exp(-jnp.square((radial - 0.46) / 0.26)) * (1.0 + 0.05 * helical)
|
|
240
|
+
neutral_density = 0.11 + scale * 0.25 * jnp.exp(-jnp.square((radial - 0.88) / 0.15)) * (1.0 + 0.10 * helical)
|
|
241
|
+
electron_density = ion_density * (1.0 + 0.005 * sideband)
|
|
242
|
+
ion_temperature = (0.058 + 0.10 * (1.0 - radial)) / jnp.maximum(bnorm, 0.15) ** 0.20
|
|
243
|
+
electron_temperature = (0.075 + 0.14 * (1.0 - radial)) / jnp.maximum(bnorm, 0.15) ** 0.35
|
|
244
|
+
neutral_temperature = 0.020 + 0.018 * radial
|
|
245
|
+
return FciDrbState(
|
|
246
|
+
ion_density=jnp.maximum(ion_density, 1.0e-6),
|
|
247
|
+
electron_density=jnp.maximum(electron_density, 1.0e-6),
|
|
248
|
+
neutral_density=jnp.maximum(neutral_density, 1.0e-8),
|
|
249
|
+
ion_pressure=ion_density * ion_temperature,
|
|
250
|
+
electron_pressure=electron_density * electron_temperature,
|
|
251
|
+
neutral_pressure=neutral_density * neutral_temperature,
|
|
252
|
+
ion_momentum=0.018 * ion_density * helical,
|
|
253
|
+
neutral_momentum=0.010 * neutral_density * jnp.sin(theta - phi),
|
|
254
|
+
vorticity=0.030 * jnp.sin(2.0 * theta - phi) + 0.010 * sideband,
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def save_essos_imported_pytree_campaign_plot(
|
|
259
|
+
report: dict[str, Any],
|
|
260
|
+
arrays: dict[str, np.ndarray],
|
|
261
|
+
path: str | Path,
|
|
262
|
+
) -> Path:
|
|
263
|
+
resolved = Path(path)
|
|
264
|
+
resolved.parent.mkdir(parents=True, exist_ok=True)
|
|
265
|
+
fig, axes = plt.subplots(2, 3, figsize=(15.8, 8.8), constrained_layout=True)
|
|
266
|
+
history = arrays["history"]
|
|
267
|
+
for column, label in ((0, "mean ion density"), (1, "mean neutral density"), (3, "vorticity RMS")):
|
|
268
|
+
reference = max(abs(float(history[0, column])), 1.0e-30)
|
|
269
|
+
axes[0, 0].plot(
|
|
270
|
+
arrays["time_index"],
|
|
271
|
+
1.0e6 * (history[:, column] - history[0, column]) / reference,
|
|
272
|
+
lw=2.0,
|
|
273
|
+
label=label,
|
|
274
|
+
)
|
|
275
|
+
axes[0, 0].set_title("imported-map PyTree transient")
|
|
276
|
+
axes[0, 0].set_xlabel("step")
|
|
277
|
+
axes[0, 0].set_ylabel("change from step 0 [ppm]")
|
|
278
|
+
axes[0, 0].grid(alpha=0.25)
|
|
279
|
+
axes[0, 0].legend(frameon=False, fontsize=8)
|
|
280
|
+
|
|
281
|
+
image1 = axes[0, 1].imshow(arrays["endpoint_count_toroidal"].T, origin="lower", aspect="auto", cmap="magma")
|
|
282
|
+
axes[0, 1].set_title("imported endpoint count")
|
|
283
|
+
axes[0, 1].set_xlabel("toroidal angle")
|
|
284
|
+
axes[0, 1].set_ylabel("poloidal angle")
|
|
285
|
+
fig.colorbar(image1, ax=axes[0, 1])
|
|
286
|
+
|
|
287
|
+
image2 = axes[0, 2].imshow(arrays["final_ion_density_section"], origin="lower", aspect="auto", cmap="viridis")
|
|
288
|
+
axes[0, 2].set_title("final ion density section")
|
|
289
|
+
fig.colorbar(image2, ax=axes[0, 2])
|
|
290
|
+
|
|
291
|
+
image3 = axes[1, 0].imshow(arrays["final_neutral_density_section"], origin="lower", aspect="auto", cmap="magma")
|
|
292
|
+
axes[1, 0].set_title("final neutral density section")
|
|
293
|
+
fig.colorbar(image3, ax=axes[1, 0])
|
|
294
|
+
|
|
295
|
+
axes[1, 1].plot(arrays["serial_scales_4"], arrays["serial_values_4"], "o-", lw=2.0, label="serial")
|
|
296
|
+
axes[1, 1].plot(arrays["serial_scales_4"], arrays["vmap_values_4"], "s--", lw=2.0, label="vmap")
|
|
297
|
+
axes[1, 1].set_title("batched objective parity")
|
|
298
|
+
axes[1, 1].set_xlabel("drive scale")
|
|
299
|
+
axes[1, 1].grid(alpha=0.25)
|
|
300
|
+
axes[1, 1].legend(frameon=False)
|
|
301
|
+
|
|
302
|
+
labels = ["JVP", "finite diff", "rel. error"]
|
|
303
|
+
axes[1, 2].bar(np.arange(3), np.abs(arrays["jvp_summary"]), color=["#005f73", "#9b2226", "#ee9b00"])
|
|
304
|
+
axes[1, 2].set_xticks(np.arange(3), labels, rotation=18, ha="right")
|
|
305
|
+
axes[1, 2].set_yscale("log")
|
|
306
|
+
axes[1, 2].grid(axis="y", alpha=0.25)
|
|
307
|
+
axes[1, 2].set_title("JVP derivative gate")
|
|
308
|
+
axes[1, 2].text(
|
|
309
|
+
0.03,
|
|
310
|
+
0.96,
|
|
311
|
+
"\n".join(
|
|
312
|
+
[
|
|
313
|
+
f"warm run = {report['warm_execute_seconds']:.3f} s",
|
|
314
|
+
f"JVP rel. err = {report['jvp_relative_error']:.1e}",
|
|
315
|
+
f"endpoint fraction = {report['endpoint_fraction']:.2f}",
|
|
316
|
+
]
|
|
317
|
+
),
|
|
318
|
+
transform=axes[1, 2].transAxes,
|
|
319
|
+
va="top",
|
|
320
|
+
ha="left",
|
|
321
|
+
fontsize=8,
|
|
322
|
+
bbox={"facecolor": "white", "alpha": 0.82, "edgecolor": "0.8"},
|
|
323
|
+
)
|
|
324
|
+
for axis in (axes[0, 2], axes[1, 0]):
|
|
325
|
+
axis.set_xlabel("poloidal index")
|
|
326
|
+
axis.set_ylabel("radial index")
|
|
327
|
+
fig.suptitle(
|
|
328
|
+
"Imported field-line PyTree/JVP gate: external FCI maps with DRBX transformable RHS",
|
|
329
|
+
fontsize=14,
|
|
330
|
+
)
|
|
331
|
+
fig.savefig(resolved, dpi=190)
|
|
332
|
+
plt.close(fig)
|
|
333
|
+
return resolved
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def _build_imported_transient(
|
|
337
|
+
geometry: EssosImportedFciGeometry,
|
|
338
|
+
*,
|
|
339
|
+
parameters: FciDrbRhsParameters,
|
|
340
|
+
dt: float,
|
|
341
|
+
steps: int,
|
|
342
|
+
):
|
|
343
|
+
def run(initial_state: FciDrbState) -> tuple[FciDrbState, jax.Array]:
|
|
344
|
+
def step(state: FciDrbState, _unused: None) -> tuple[FciDrbState, jax.Array]:
|
|
345
|
+
result = compute_fci_drb_rhs(state, maps=geometry.maps, metric=geometry.metric, parameters=parameters)
|
|
346
|
+
next_state = _clip_state(_add_scaled_state(state, result.rhs, dt))
|
|
347
|
+
diagnostics = jnp.asarray(
|
|
348
|
+
[
|
|
349
|
+
jnp.mean(next_state.ion_density),
|
|
350
|
+
jnp.mean(next_state.neutral_density),
|
|
351
|
+
jnp.mean(next_state.electron_pressure),
|
|
352
|
+
jnp.sqrt(jnp.mean(jnp.square(next_state.vorticity))),
|
|
353
|
+
result.potential_residual_l2,
|
|
354
|
+
],
|
|
355
|
+
dtype=jnp.float64,
|
|
356
|
+
)
|
|
357
|
+
return next_state, diagnostics
|
|
358
|
+
|
|
359
|
+
return jax.lax.scan(step, initial_state, None, length=int(steps))
|
|
360
|
+
|
|
361
|
+
return jax.jit(run)
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
def _build_imported_objective(
|
|
365
|
+
geometry: EssosImportedFciGeometry,
|
|
366
|
+
*,
|
|
367
|
+
parameters: FciDrbRhsParameters,
|
|
368
|
+
dt: float,
|
|
369
|
+
steps: int,
|
|
370
|
+
):
|
|
371
|
+
run_transient = _build_imported_transient(geometry, parameters=parameters, dt=dt, steps=steps)
|
|
372
|
+
|
|
373
|
+
def objective(drive_scale: jax.Array) -> jax.Array:
|
|
374
|
+
final_state, history = run_transient(initial_essos_imported_drb_state(geometry, drive_scale=drive_scale))
|
|
375
|
+
return (
|
|
376
|
+
jnp.mean(final_state.ion_density)
|
|
377
|
+
+ 0.25 * jnp.mean(final_state.neutral_density)
|
|
378
|
+
+ 0.05 * jnp.sqrt(jnp.mean(jnp.square(final_state.vorticity)))
|
|
379
|
+
+ 0.0 * history[-1, 4]
|
|
380
|
+
)
|
|
381
|
+
|
|
382
|
+
return objective
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
def _add_scaled_state(state: FciDrbState, rhs: FciDrbState, scale: float) -> FciDrbState:
|
|
386
|
+
return jax.tree_util.tree_map(lambda value, increment: value + float(scale) * increment, state, rhs)
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
def _clip_state(state: FciDrbState) -> FciDrbState:
|
|
390
|
+
return FciDrbState(
|
|
391
|
+
ion_density=jnp.maximum(state.ion_density, 1.0e-6),
|
|
392
|
+
electron_density=jnp.maximum(state.electron_density, 1.0e-6),
|
|
393
|
+
neutral_density=jnp.maximum(state.neutral_density, 1.0e-8),
|
|
394
|
+
ion_pressure=jnp.maximum(state.ion_pressure, 1.0e-8),
|
|
395
|
+
electron_pressure=jnp.maximum(state.electron_pressure, 1.0e-8),
|
|
396
|
+
neutral_pressure=jnp.maximum(state.neutral_pressure, 1.0e-10),
|
|
397
|
+
ion_momentum=state.ion_momentum,
|
|
398
|
+
neutral_momentum=state.neutral_momentum,
|
|
399
|
+
vorticity=state.vorticity,
|
|
400
|
+
)
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def _block_until_ready(value: object) -> None:
|
|
404
|
+
for leaf in jax.tree_util.tree_leaves(value):
|
|
405
|
+
if hasattr(leaf, "block_until_ready"):
|
|
406
|
+
leaf.block_until_ready()
|