Measure exponential growth¶
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.
The generator supplies an analytic energy history with a known exponential rate. Fit the logarithm of positive energy to time; its slope is the energy growth rate. If energy is proportional to squared mode amplitude, the amplitude growth rate is half this slope. This exact example checks the calculation; real data require selecting a justified growth interval and examining residuals before saturation dominates. The script checks the known rate and residuals numerically, then plots the reconstructed fitted energy.
Python script
python examples/scripts/06_growth.py
"""Recover a known energy growth rate from a generated diagnostic history."""
import matplotlib
matplotlib.use("Agg")
import numpy as np
import postgkyl as pg
from _example_paths import TEST_DATA, prepare_output_dir
data = pg.load(TEST_DATA / "generated/exponential_energy.gkyl")
log_energy = pg.evaluate("f log", data)
fit = log_energy.fit("linear")
time = data.grid[0]
energy_rate = np.polyfit(time, fit.values.ravel(), 1)[0]
np.testing.assert_allclose(energy_rate, 0.4, rtol=1e-10)
np.testing.assert_allclose(fit.values, log_energy.values, atol=1e-12)
# Plot the reconstructed fitted energy with the public plotting function.
# The CLI uses evaluate "f log", fit linear, and evaluate "f1 exp" (fit appends its result).
pg.evaluate("f exp", fit).plot(title="Fitted energy growth",
xlabel="Time [normalized]",
ylabel="Energy [normalized]",
logy=True,
no_show=True,
saveas=prepare_output_dir() / "06_growth.png",
dpi=100)
Equivalent CLI commands
Run from the extracted example bundle (or repository root). Create an output directory first:
mkdir -p output
pgkyl tests/test_data/generated/exponential_energy.gkyl evaluate 'f log' fit --fit_type \
linear evaluate 'f1 exp' plot --title 'Fitted energy growth' --xlabel \
'Time [normalized]' --ylabel 'Energy [normalized]' --logy --no_show --dpi 100 --saveas \
output/06_growth.png
06_growth.png: Identical pixels (1 frame(s)) and timing.
|
|