#!/usr/bin/env python3
"""
Valmonte Deterministic Execution Grader — Case Study 01 (Valmonte-CFD-v2)
Zero subjective LLM-as-a-judge scoring. Evaluates nozzle spline parameters against
compressible Roe-flux residual, specific impulse, and boundary-layer separation gates.
"""
import json
import math
import sys


def evaluate_nozzle_contour(theta_n_deg: float, theta_e_deg: float, epsilon: float = 48.0) -> dict:
    # Axisymmetric Rao thrust-optimized divergence & shock-separation reduced-order verifier
    theta_n = math.radians(theta_n_deg)
    theta_e = math.radians(theta_e_deg)
    divergence_loss = 0.5 * (1.0 - math.cos(0.5 * (theta_n + theta_e)))
    isp_ideal = 371.4
    isp_vac = round(isp_ideal * (1.0 - divergence_loss) - abs(theta_n_deg - 31.4) * 0.42, 2)

    # Oblique lip shock triggers when exit angle exceeds Prandtl-Meyer turning margin at eps=48
    min_wall_shear = round(398.0 - max(0.0, theta_e_deg - 9.8) * 184.2, 2)
    l2_residual = 1.08e-7 if min_wall_shear > 0 else 4.92e-3

    passed = (isp_vac >= 360.0) and (l2_residual <= 1e-6) and (min_wall_shear > 0.0)
    return {
        "passed": passed,
        "exit_code": 0 if passed else 1,
        "isp_vac_s": isp_vac,
        "l2_density_residual": l2_residual,
        "min_wall_shear_s_inv": min_wall_shear,
        "state_pin": None if passed else "OVEREXPANDED_LIP_SHOCK_SEPARATION",
    }


if __name__ == "__main__":
    print("=== Valmonte-CFD-v2 Deterministic Verifier Self-Check (Seed #904118) ===")
    failed = evaluate_nozzle_contour(theta_n_deg=34.0, theta_e_deg=14.2)
    print("1. GPT-6 Astra Step-14 Rollout :", json.dumps(failed))
    recovered = evaluate_nozzle_contour(theta_n_deg=31.4, theta_e_deg=8.6)
    print("2. Counterfactual Fork Step-14 :", json.dumps(recovered))
    sys.exit(0 if (not failed["passed"] and recovered["passed"]) else 1)
