import os
from pathlib import Path

# --8<-- [start:code]
"""Convection-dominated transport: numerical fluxes and limiters with ``jno.fv``.

    u' - eps u'' = 0 on (0, 1), u(0) = 0, u(1) = 1,  eps = 0.01: a boundary layer at x = 1.

Write the convection in divergence form, ``ui.d(x, scheme=jno.fv(...))``: the scheme rides on the outer
derivative, as a ``jno.fd(...)`` stencil does. At a cell Peclet number of 2.5 the unlimited second-order
reconstruction oscillates; every limiter keeps the solution in [0, 1].
"""

import jax

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

import numpy as np

import jno

run = jno.setup(__file__)

EPS = 0.01
d = jno.Path(0.0, 0.0).line_to(1.0, 0.0).curve(size=0.05).domain()
x = d.variable("interior", split=True)[0]
xl, xr = d.variable("left", split=True)[0], d.variable("right", split=True)[0]
u = d.unknown()
ui = u.bind(x=x)

schemes = {
    "first order": jno.fv(recon="none"),
    "unlimited": jno.fv(limiter=None),
    "van Albada": jno.fv(),
    "minmod": jno.fv(limiter="minmod"),
    "HLL + van Leer": jno.fv(flux="hll", limiter="vanleer"),
}
sols = {}
for name, scheme in schemes.items():
    prob = jno.fvm([ui.d(x, scheme=scheme) - EPS * ui.xx, u(xl) - 0.0, u(xr) - 1.0])
    sols[name] = sol = np.asarray(prob.solve())
    print(f"{name:15s} range [{sol.min():+.3f}, {sol.max():.3f}]")
    if name != "unlimited":
        assert sol.min() > -1e-3 and sol.max() < 1 + 1e-3
# --8<-- [end:code]

# ---- data for the figures (tools/tutorial_figures/advection_diffusion_fvm.py), only when asked ----
if out := os.environ.get("JNO_FIGURE_DATA"):
    np.savez_compressed(
        Path(out) / "advection_diffusion_fvm.npz",
        x=prob.centroids[:, 0],
        names=np.array(list(sols)),
        u=np.stack(list(sols.values())),
        eps=EPS,
    )
