Plot in physical R–Z coordinates¶
All scripts below run against the example bundle. Unzip it, activate the environment where
Postgkyl is installed, and run the commands from the extracted directory.
The Python and CLI outputs below are both generated and compared when this website is built.
Computational coordinates are useful for analysis, but a poloidal view places
the field in its physical geometry. Keep the companion
rt_gk_tcv_nt_iwl_3x2v_p1-geo_int_mapc2p.gkyl beside the electron density
file so pg.map_to_rz can resolve it by the simulation prefix. The mapping
operation uses the same geometry filenames for every Gkeyll equation system.
The matching CLI verb is map_to_rz.
Load reusable geometry with pg.resolve_geometry(data.file_name) or construct
pg.Geometry from coordinate arrays, build
pg.resolve_rz_projection(data, geometry), and apply it with
data.map_to_rz(projection=projection). The projection can be reused for
fields on the same computational grid. Three-dimensional reconstruction
assumes periodic field-aligned coordinates and twist-and-shift boundaries.
Related toroidal surface sampling is available through
data.extract_flux_surface(...) and the extract_flux_surface CLI verb.
Python script
python examples/scripts/05_map_to_rz.py
"""R-Z mapping through fluent, functional, and reusable APIs.
Run directly:
MPLBACKEND=Agg PYTHONPATH=src python examples/scripts/05_map_to_rz.py
"""
from __future__ import annotations
import matplotlib
matplotlib.use("Agg")
import numpy as np
import postgkyl as pg
from _example_paths import TEST_DATA, prepare_output_dir
OUTPUT_DIR = prepare_output_dir()
data = pg.load(TEST_DATA / "rt_gk_tcv_nt_iwl_3x2v_p1-elc_M0_5.gkyl")
# Geometry is inferred from the dataset's simulation prefix.
mapped = pg.map_to_rz(data, z_axis=0.0, phi_tor=0.0, nz_interp=2)
fig = mapped.plot(title="Electron density in the poloidal plane",
xlabel="R [m]",
ylabel="Z [m]",
clabel=r"$n_e$ [m$^{-3}$]",
fixaspect=True,
no_show=True)
fig.savefig(OUTPUT_DIR / "05_map_to_rz.png")
# Reuse geometry and projection when several fields/frames or toroidal angles
# share one computational grid.
geometry = pg.resolve_geometry(data.file_name)
projection = pg.resolve_rz_projection(data, geometry, z_axis=0.0, nz_interp=2)
at_zero = data.map_to_rz(projection=projection, phi_tor=0.0)
at_quarter_turn = pg.map_to_rz(data, projection=projection, phi_tor=np.pi / 2)
np.testing.assert_allclose(at_zero.values, mapped.values)
assert not np.allclose(
at_zero.values, at_quarter_turn.values, rtol=1e-12, atol=0.0)
print("05_map_to_rz: OK")
Equivalent CLI commands
Run from the extracted example bundle (or repository root). Create an output directory first:
mkdir -p output
pgkyl tests/test_data/rt_gk_tcv_nt_iwl_3x2v_p1-elc_M0_5.gkyl map_to_rz --z_axis 0.0 \
--phi_tor 0.0 --nz_interp 2 plot --title 'Electron density in the poloidal plane' \
--xlabel 'R [m]' --ylabel 'Z [m]' --clabel '$n_e$ [m$^{-3}$]' --fixaspect --no_show \
--dpi 100 --saveas output/05_map_to_rz.png
05_map_to_rz.png: Identical pixels (1 frame(s)) and timing.
|
|