Inspect a gyrokinetic distribution¶
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.
This example uses committed TCV output: ion Hamiltonian moments from
rt_gk_tcv_iwl_adapt_source_1x2v_p1 and an electron distribution plus geometry
from rt_gk_tcv_iwl_1x2v_p1. These are separate runs; do not combine their
moments into one conservation calculation. Frame 250 identifies saved output,
not a time in seconds.
load_quantity resolves named quantities from available files.
load_distf reconstructs the distribution using its matching Jacobians.
Coordinates remain computational in this example; the optional velocity
mapping is not enabled. Inspect the returned grid before assigning units or
interpreting a velocity-space slice.
Python script
python examples/scripts/04_gyrokinetics.py
"""Gyrokinetic diagnostics: named quantities and distribution functions.
``postgkyl.diagnostics.gk`` is another equation-specific module in
the diagnostics layer (like ``five_moment`` in
``examples/scripts/03_diagnostics_five_moment.py``), but for gyrokinetic
simulations it owns its own *loading* too -- a simulation's files follow a
naming convention (``<name>-<species>_<quantity>_<frame>.gkyl``), so instead
of calling ``pg.load`` on individual filenames, you ask for a quantity by
name and the loader resolves which files it needs:
* ``pg.gk.available_quantities()`` lists the registered quantity names.
* ``pg.gk.load_quantity(quantity, species, name, frame, path=...)`` resolves
the source file(s) for that quantity, computes it, and returns one
``GData`` per requested species and frame, preserving modal DG data.
Use ``.interpolate()`` when point samples are needed.
* ``pg.gk.load_distf(name=..., species=..., frame=..., ...)`` reconstructs a
full distribution function from the saved ``Jf``-times-Jacobian(s) files
(what the CLI's ``gk_load_distf`` command wraps).
This uses the ``rt_gk_tcv_iwl*`` fixtures staged in ``tests/test_data`` --
two related simulations: ``rt_gk_tcv_iwl_adapt_source_1x2v_p1`` wrote ion
Hamiltonian moments (``M0``/``M1`` are derivable from those), and
``rt_gk_tcv_iwl_1x2v_p1`` wrote the electron distribution function plus its
geometry (``geo_int_jacobtot_inv``).
Run directly:
MPLBACKEND=Agg PYTHONPATH=src python examples/scripts/04_gyrokinetics.py
"""
from __future__ import annotations
import matplotlib
matplotlib.use("Agg")
import postgkyl as pg
from _example_paths import TEST_DATA, prepare_output_dir
OUTPUT_DIR = prepare_output_dir()
HMOM_NAME = "rt_gk_tcv_iwl_adapt_source_1x2v_p1" # wrote ion Hamiltonian moments
GK_NAME = "rt_gk_tcv_iwl_1x2v_p1" # wrote elc distf + geometry
print("registered quantities:", pg.gk.available_quantities())
# 1. A moment quantity ("M0", the density) resolved straight from the
# HamiltonianMoments file the registry knows how to read, retaining
# its modal representation. Plotting evaluates the field for display.
m0, = pg.gk.load_quantity("M0", "ion", HMOM_NAME, "250", path=str(TEST_DATA))
print("M0:", repr(m0), " label:", m0.get_label())
fig = m0.plot(title="Ion density from Hamiltonian moments",
xlabel="Field-aligned computational coordinate",
ylabel=m0.get_label(),
no_show=True)
fig.savefig(OUTPUT_DIR / "04_gyrokinetics_M0.png")
# M1 is a density-weighted parallel velocity moment, not velocity itself.
# Its Hamiltonian conversion requires the simulation's species mass in the
# same unit system. This fixture has no input deck establishing that mass,
# so this example limits itself to M0 rather than assigning an arbitrary mass.
# 3. A species-independent geometric factor, from the other simulation --
# ``species=None`` since geometry isn't per-species.
jacobtot_inv, = pg.gk.load_quantity("geo_int_jacobtot_inv",
None,
GK_NAME,
path=str(TEST_DATA))
print("(J B)^-1:", repr(jacobtot_inv), " label:", jacobtot_inv.get_label())
fig = jacobtot_inv.plot(title=jacobtot_inv.get_label(), no_show=True)
fig.savefig(OUTPUT_DIR / "04_gyrokinetics_jacobtot_inv.png")
# 4. The full electron distribution function: 3D (x, vpar, mu), built from
# the saved Jf-times-Jacobian(s) file plus the geometry factor above.
distf = pg.gk.load_distf(name=str(TEST_DATA / GK_NAME),
species="elc",
frame=250,
jacobtot_inv_file=TEST_DATA /
f"{GK_NAME}-geo_int_jacobtot_inv.gkyl")
print("distf:", repr(distf))
# It's a regular GData from here on -- e.g. select a fixed-mu slice down to
# the (x, vpar) plane and plot it, same as any other 2D field.
slice_2d = distf.select(z2=0.0)
fig = slice_2d.plot(title="Electron distribution: lowest mu slice",
xlabel="Field-aligned computational coordinate",
ylabel="Computational parallel velocity",
clabel="Distribution [simulation units]",
no_show=True)
fig.savefig(OUTPUT_DIR / "04_gyrokinetics_distf_slice.png")
print("04_gyrokinetics: OK")
Equivalent CLI commands
Run from the extracted example bundle (or repository root). Create an output directory first:
mkdir -p output
pgkyl gk_load_quantity --quantity M0 --species ion --name \
rt_gk_tcv_iwl_adapt_source_1x2v_p1 --frame 250 --path tests/test_data/ plot --title \
'Ion density from Hamiltonian moments' --xlabel \
'Field-aligned computational coordinate' --ylabel '$M_{0i}$ (m$^{-3}$)' --no_show --dpi \
100 --saveas output/04_gyrokinetics_M0.png
04_gyrokinetics_M0.png: Identical pixels (1 frame(s)) and timing.
|
|
pgkyl gk_load_quantity --quantity geo_int_jacobtot_inv --species '' --name \
rt_gk_tcv_iwl_1x2v_p1 --path tests/test_data/ plot --title '$(J B)^{-1}$' --no_show \
--dpi 100 --saveas output/04_gyrokinetics_jacobtot_inv.png
04_gyrokinetics_jacobtot_inv.png: Identical pixels (1 frame(s)) and timing.
|
|
pgkyl gk_load_distf --name tests/test_data/rt_gk_tcv_iwl_1x2v_p1 --species elc --frame 250 \
--jacobtot_inv_file tests/test_data/rt_gk_tcv_iwl_1x2v_p1-geo_int_jacobtot_inv.gkyl \
select --z2 0.0 plot --title 'Electron distribution: lowest mu slice' --xlabel \
'Field-aligned computational coordinate' --ylabel 'Computational parallel velocity' \
--clabel 'Distribution [simulation units]' --no_show --dpi 100 --saveas \
output/04_gyrokinetics_distf_slice.png
04_gyrokinetics_distf_slice.png: Identical pixels (1 frame(s)) and timing.
|
|