import os
from pathlib import Path

# --8<-- [start:code]
"""An inviscid Burgers shock, marched explicitly with ``jno.solve.erk``.

    u_t + (u^2/2)_x = 0,  u = 1 for x < 0.2, 0 beyond: a shock moving at (1 + 0)/2.

Conservation form ``(ui*ui/2).d(x, ...)`` puts the shock where the Rankine-Hugoniot condition says;
SSPRK3 with a minmod limiter keeps it sharp and monotone, and ``cfl=`` chooses the substeps from the
spectral radius of the first-order operator at every step.
"""

import jax

jax.config.update("jax_enable_x64", True)

import numpy as np

import jno

run = jno.setup(__file__)

T = 0.6
d = jno.Path(0.0, 0.0).line_to(1.0, 0.0).curve(size=0.005).domain(time=(0.0, T, 101))
x, t = d.variable("interior", split=True)[:2]
xl = d.variable("left", split=True)[0]
xi = d.variable("initial", split=True)[0]
u = d.unknown()
ui = u.bind(x=x, t=t)

prob = jno.fvm(
    [
        ui.t + (ui * ui / 2).d(x, scheme=jno.fv(limiter="minmod")),
        u(xl) - 1.0,
        u(xi) - (1.0 - jno.np.tanh(1e4 * (xi - 0.2))) / 2,
    ]
)
traj = np.asarray(prob.solve(time=jno.solve.erk("ssprk3", cfl=0.5)))
c = prob.centroids[:, 0]
front = c[np.argmin(np.abs(traj[-1] - 0.5))]
print(f"shock at x = {front:.3f}  (Rankine-Hugoniot: {0.2 + 0.5 * T:.3f});  range [{traj.min():.3f}, {traj.max():.3f}]")
assert abs(front - (0.2 + 0.5 * T)) < 0.01
# --8<-- [end:code]

# ---- data for the figures (tools/tutorial_figures/burgers_shock_fvm.py), only when asked ----
if out := os.environ.get("JNO_FIGURE_DATA"):
    np.savez_compressed(Path(out) / "burgers_shock_fvm.npz", x=c, t=np.linspace(0.0, T, len(traj)), u=traj, x0=0.2)
