# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///
"""Regenerate the portable fixtures on standard output."""
import json
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "reference"))
import spectral as reference


def build():
    cases = [
        {"name":"nonunit length and nonlinear spectral residual","operation":"energy_force","tol":1e-11,
         "inputs":dict(u=[.1,-.6,.7,.2,-.3],length=2.3,alpha=.4,beta=.025,local=.3,spectral=.6)},
        {"name":"pure even-grid Nyquist force","operation":"energy_force","tol":1e-10,
         "inputs":dict(u=[.2,-.2,.2,-.2],length=1.7,alpha=.5,beta=.03,local=0.,spectral=0.)},
        {"name":"coexisting damping and signed transport","operation":"transport","tol":1e-11,
         "inputs":dict(mu=[.6,-.1,.8,-.7,.15],real_head=[.4,-.5,.2],imag_head=[.7,.3,-.4])},
        {"name":"all starts and accumulated two-step loss","operation":"rollout_loss","tol":1e-11,
         "inputs":dict(states=[[.3,-.1,.7,.15],[.25,-.05,.72,.16],[.23,-.06,.69,.17],[.19,-.04,.65,.18]],
                       horizon=2,dt=.04,length=2.8,alpha=.4,beta=.012,local=.7,spectral=.3,
                       real_head=[.7,-.6,.4],imag_head=[.3,.65,-.2])},
        {"name": "two-step wave loss and three weight derivatives", "operation": "wave_loss_gradient", "tol": 1e-10,
         "inputs": dict(amplitudes=[[.6,-.1],[.49,-.19],[.38,-.24],[.28,-.26]],
                        horizon=2, dt=.07, length=3.4, weights=[.13,.82,.68])},
    ]
    for case in cases:
        case["expected"] = getattr(reference, case["operation"])(**case["inputs"])
    return {"format": 1, "method": "spectral-onsagernet", "version": "0.1.0",
            "interface": (
                "Double precision, real length-n arrays, periodic sites j*length/n without duplicated endpoint. Normalized forward DFT c_k=sum_j u_j exp(-2*pi*i*j*k/n)/n, inverse unscaled. "
                "energy_force(u,length,alpha,beta,local,spectral) returns [V,mu in physical coordinates]. V=length/2*sum_k (alpha+beta*(2*pi*k/length)^2)*abs(c_k)^2 + length/n*sum_j local*(1-cos(u_j)) + length*spectral*tanh(P). "
                "P=sum_k e_k*abs(c_k)^2/2, e_k=k^2/(1+k^2) for signed integer k. mu=(n/length)*dV/du. "
                "transport(mu,real_head,imag_head) returns inverse DFT of -(real_head[abs(k)]^2+i*sign(k)*imag_head[abs(k)])*DFT(mu). "
                "Zero the imaginary multiplier at DC and at even-grid Nyquist. rollout_loss(states,horizon,dt,length,alpha,beta,local,spectral,real_head,imag_head) sums squared field errors (no averaging) at offsets 1..horizon for every start that has a complete window. "
                "Reevaluate mu on the predicted state at each forward Euler step. wave_loss_gradient(amplitudes,horizon,dt,length,weights) maps each [a,b] to the field [a,b,-a,-b]. "
                "For weights [beta,r,w] use alpha=1, local=spectral=0 and transport heads [0,r,0] and [0,w,0]. Return [rollout_loss,[d/dbeta,d/dr,d/dw]], differentiating through every Euler step. "
                "Export all four functions from port_adapter.py."
            ), "cases": cases}


if __name__ == "__main__":
    print(json.dumps(build(), indent=2, allow_nan=False))
