OpenIslet

scripts/verify_iip0043_wholegraft_gate0v_and_calibration.py

IIP-0043 / DEC-122 (CAND-034 / EXP-031 — Bucket 1 Closure, Stage S3-R2 Remediated)

script Release v0.8.0 gen-eb955f8266035aba 1008 KB · 76 sections

Use this file in any AI chat (paste-only or attachment-capable):

Download as text (.txt) Download section (.txt)

Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0043_wholegraft_gate0v_and_calibration.py · SHA-256: 20706180ed1ed6a2…

            "policy_version": NW08_MESH_CONVERGENCE_POLICY_VERSION,
            "near_floor_trigger_band_mmhg": eff_band,
            "safety_factor_fs": fs,
            "admissible_asymptotic_order_range_p": [
                NW08_MESH_CONVERGENCE_MIN_ADMISSIBLE_ORDER_P,
                NW08_MESH_CONVERGENCE_MAX_ADMISSIBLE_ORDER_P,
            ],
            "formal_order_cap_p": NW08_MESH_CONVERGENCE_FORMAL_ORDER_P,
            "triggered_by_near_floor_band": near_floor,
            "executed": True,
            "grids_run": 3,
            "fine_grid_skipped_already_straddling": False,
            "grid_ladder": [
                {"level": c_lvl, "grid": [c_nx, c_nx, c_nz], "h_rel": round(h3 / h1, 6), "face_min_mmhg_unrounded": f3},
                {"level": m_lvl, "grid": [m_nx, m_nx, m_nz], "h_rel": round(h2 / h1, 6), "face_min_mmhg_unrounded": f2},
                {"level": f_lvl, "grid": [f_nx, f_nx, f_nz], "h_rel": 1.0, "face_min_mmhg_unrounded": f1},
            ],
            "refinement_ratios": {"r32": round(r32, 6), "r21": round(r21, 6)},
            "convergence_regime": "INVALID_OR_NONFINITE_GRID_VALUES_HOLD",
            "mesh_uncertainty_status": NW08_MESH_STATUS_THREE_GRID_INVALID_HOLD,
            "observed_order_p_raw": None,
            "apparent_order_p": None,
            "order_qualified": False,
            "interval_valid": False,
            "richardson_extrapolated_mmhg": None,
            "grid_min_mmhg": None,
            "grid_max_mmhg": None,
            "grid_spread_mmhg": None,
            "fine_grid_face_min_mmhg_unrounded": f1 if math.isfinite(f1) else None,
            "discretization_uncertainty_mmhg": None,
            "conservative_po2_lower_bound_mmhg": None,
            "conservative_po2_upper_bound_mmhg": None,
            "straddles_canonical_15mmhg_floor": None,
            "mesh_convergence_resolved": False,
            "hold_reason_token": NW08_HOLD_TOKEN_UNQUALIFIED_ORDER,
            "verdict_from_mesh_policy": "HOLD",
        }

    f_min = min(f3, f2, f1)
    f_max = max(f3, f2, f1)
    spread = f_max - f_min
    eps21 = f1 - f2
    eps32 = f2 - f3
    direct_straddle = bool(f_min < floor_val <= f_max)

    p_obs_raw: float | None = None
    p_app: float | None = None
    f_ext: float | None = None
    u_num: float | None = None
    l_bound: float | None = None
    u_bound: float | None = None
    interval_valid: bool = False
    order_qualified: bool = False
    mesh_uncertainty_status: str = NW08_MESH_STATUS_THREE_GRID_UNQUALIFIED_ORDER_HOLD

    if spread == 0.0:
        regime = "CONVERGED_EXACT"
        u_num = 0.0
        l_bound = f1
        u_bound = f1
        f_ext = f1
        interval_valid = True
        order_qualified = True
        mesh_uncertainty_status = NW08_MESH_STATUS_THREE_GRID_EXACT_ZERO_SPREAD
    elif eps21 * eps32 < 0.0:
        regime = "OSCILLATORY_ASME_VV20_HALF_RANGE"
        f_mid = 0.5 * (f_min + f_max)
        u_num = fs * 0.5 * spread
        l_bound = f_mid - u_num
        u_bound = f_mid + u_num
        interval_valid = True
        order_qualified = False
        mesh_uncertainty_status = NW08_MESH_STATUS_THREE_GRID_OSCILLATORY_BOUND
    elif eps21 * eps32 > 0.0 and abs(eps21) < abs(eps32):
        target_log = math.log(abs(eps32) / abs(eps21))
        ln_r32 = math.log(r32)
        ln_r21 = math.log(r21)

        def _gci_order_lhs(p_val: float) -> float:
            # Numerically stable for arbitrarily small p_val > 0 via expm1 (avoids r^p - 1 cancellation).
            return p_val * ln_r21 + math.log(math.expm1(p_val * ln_r32) / math.expm1(p_val * ln_r21))

        lhs_zero = math.log(ln_r32 / ln_r21)
        lhs_min = _gci_order_lhs(NW08_MESH_CONVERGENCE_MIN_ADMISSIBLE_ORDER_P)
        lhs_max = _gci_order_lhs(NW08_MESH_CONVERGENCE_MAX_ADMISSIBLE_ORDER_P)

        if target_log <= lhs_zero:
            # No positive algebraic convergence order p > 0 exists on (r32, r21) (e.g., logarithmic/sub-power drift).
            regime = "MONOTONE_SUB_ASYMPTOTIC_NONPOSITIVE_ORDER_HOLD"
            p_obs_raw = None
            p_app = None
            interval_valid = False
            order_qualified = False
            mesh_uncertainty_status = NW08_MESH_STATUS_THREE_GRID_UNQUALIFIED_ORDER_HOLD
        elif target_log < lhs_min:
            # R15-B2: Sub-asymptotic slow convergence (0 < p_obs < 0.5). NEVER clamp p UP to 0.5!
            # Solve the true observed order for audit transparency and fail closed to HOLD.
            p_tiny = 1e-12
            if target_log <= _gci_order_lhs(p_tiny):
                p_obs_raw = 0.0
            else:
                p_lo, p_hi = p_tiny, NW08_MESH_CONVERGENCE_MIN_ADMISSIBLE_ORDER_P
                for _ in range(80):
                    p_mid = 0.5 * (p_lo + p_hi)
                    if _gci_order_lhs(p_mid) < target_log:
                        p_lo = p_mid
                    else:
                        p_hi = p_mid
                p_obs_raw = 0.5 * (p_lo + p_hi)
            p_app = p_obs_raw
            regime = "MONOTONE_SUB_ASYMPTOTIC_SLOW_ORDER_HOLD"
            interval_valid = False
            order_qualified = False
            mesh_uncertainty_status = NW08_MESH_STATUS_THREE_GRID_UNQUALIFIED_ORDER_HOLD
        elif target_log <= lhs_max:
            p_lo = NW08_MESH_CONVERGENCE_MIN_ADMISSIBLE_ORDER_P
            p_hi = NW08_MESH_CONVERGENCE_MAX_ADMISSIBLE_ORDER_P
            for _ in range(64):
                p_mid = 0.5 * (p_lo + p_hi)
                if _gci_order_lhs(p_mid) < target_log:
                    p_lo = p_mid
                else:
                    p_hi = p_mid
            p_obs_raw = 0.5 * (p_lo + p_hi)
            if p_obs_raw <= NW08_MESH_CONVERGENCE_FORMAL_ORDER_P:
                p_eff = p_obs_raw
                regime = "MONOTONE_CONVERGING_GCI"
            else:
                # Conservatively cap DOWN to formal 2nd-order accuracy (widens GCI; never shrinks).
                p_eff = NW08_MESH_CONVERGENCE_FORMAL_ORDER_P
                regime = "MONOTONE_CONVERGING_GCI_FORMAL_ORDER_CAPPED"
            p_app = p_eff
            denom = math.expm1(p_eff * ln_r21)
            gci_fine = fs * abs(eps21) / denom
            f_ext = f1 + eps21 / denom
            u_num = gci_fine
            l_bound = min(f_min, f1 - gci_fine)
            u_bound = max(f_max, f1 + gci_fine)
            interval_valid = True
            order_qualified = True
            mesh_uncertainty_status = NW08_MESH_STATUS_THREE_GRID_ASYMPTOTIC_GCI_QUALIFIED
        else:
            # Extreme super-convergence (p_obs > 2.5): potential pre-asymptotic error cancellation.
            if target_log <= _gci_order_lhs(100.0):
                p_lo, p_hi = NW08_MESH_CONVERGENCE_MAX_ADMISSIBLE_ORDER_P, 100.0
                for _ in range(64):
                    p_mid = 0.5 * (p_lo + p_hi)
                    if _gci_order_lhs(p_mid) < target_log:
                        p_lo = p_mid
                    else:
                        p_hi = p_mid
                p_obs_raw = 0.5 * (p_lo + p_hi)
            else:
                p_obs_raw = None
            p_app = p_obs_raw
            regime = "MONOTONE_SUPERCONVERGENT_NON_ASYMPTOTIC_HOLD"
            interval_valid = False
            order_qualified = False
            mesh_uncertainty_status = NW08_MESH_STATUS_THREE_GRID_UNQUALIFIED_ORDER_HOLD
    else:
        if direct_straddle:
            regime = "MONOTONE_NON_ASYMPTOTIC_STRADDLES_FLOOR"
            u_num = fs * spread
            l_bound = min(f_min, f1 - u_num)
            u_bound = max(f_max, f1 + u_num)
            interval_valid = False
            order_qualified = False
            mesh_uncertainty_status = NW08_MESH_STATUS_THREE_GRID_NON_ASYMPTOTIC_HOLD
        elif (f_min >= floor_val and eps32 > 0.0 and eps21 > 0.0) or (
            f_max < floor_val and eps32 < 0.0 and eps21 < 0.0
        ):
            # Monotone strictly moving AWAY from 15.0 mmHg with conservative full-spread envelope
            # (`f1 - Fs*spread = f3 - 0.25*(f1-f3) < f3` when moving upward above 15.0 mmHg).
            regime = "MONOTONE_NON_ASYMPTOTIC_AWAY_FROM_FLOOR_CONSERVATIVE"
            u_num = fs * spread
            l_bound = min(f_min, f1 - u_num)
            u_bound = max(f_max, f1 + u_num)
            interval_valid = True
            order_qualified = False
            mesh_uncertainty_status = NW08_MESH_STATUS_THREE_GRID_MONOTONE_FALLBACK
        else:
            # Non-shrinking / accelerating drift TOWARD 15.0 mmHg or stalled zero step with spread > 0:
            # fail closed to HOLD with no claimed clearance bound.
            regime = "MONOTONE_NON_ASYMPTOTIC_TOWARD_FLOOR_HOLD"
            interval_valid = False
            order_qualified = False
            mesh_uncertainty_status = NW08_MESH_STATUS_THREE_GRID_NON_ASYMPTOTIC_HOLD

    hold_tok: str | None = None
    straddles: bool | None
    if (not interval_valid) or (l_bound is None) or (u_bound is None):
        verdict = "HOLD"
        if direct_straddle:
            straddles = True
            hold_tok = NW08_HOLD_TOKEN_STRADDLES_FLOOR
        else:
            straddles = None
            hold_tok = NW08_HOLD_TOKEN_UNQUALIFIED_ORDER
    elif l_bound >= floor_val and f_min >= floor_val:
        verdict = "PASS"
        straddles = False
    elif u_bound < floor_val and f_max < floor_val:
        verdict = "FAIL"
        straddles = False
    else:
        verdict = "HOLD"
        straddles = True
        hold_tok = NW08_HOLD_TOKEN_STRADDLES_FLOOR

    return {
        "policy_version": NW08_MESH_CONVERGENCE_POLICY_VERSION,
        "near_floor_trigger_band_mmhg": eff_band,
        "safety_factor_fs": fs,
        "admissible_asymptotic_order_range_p": [
            NW08_MESH_CONVERGENCE_MIN_ADMISSIBLE_ORDER_P,
            NW08_MESH_CONVERGENCE_MAX_ADMISSIBLE_ORDER_P,
        ],
        "formal_order_cap_p": NW08_MESH_CONVERGENCE_FORMAL_ORDER_P,
        "triggered_by_near_floor_band": near_floor,
        "executed": True,
        "grids_run": 3,
        "fine_grid_skipped_already_straddling": False,
        "grid_ladder": [
            {"level": c_lvl, "grid": [c_nx, c_nx, c_nz], "h_rel": round(h3 / h1, 6), "face_min_mmhg_unrounded": f3},
            {"level": m_lvl, "grid": [m_nx, m_nx, m_nz], "h_rel": round(h2 / h1, 6), "face_min_mmhg_unrounded": f2},
            {"level": f_lvl, "grid": [f_nx, f_nx, f_nz], "h_rel": 1.0, "face_min_mmhg_unrounded": f1},
        ],
        "refinement_ratios": {"r32": round(r32, 6), "r21": round(r21, 6)},
        "convergence_regime": regime,
        "mesh_uncertainty_status": mesh_uncertainty_status,
        "observed_order_p_raw": round(p_obs_raw, 6) if p_obs_raw is not None else None,
        "apparent_order_p": round(p_app, 6) if p_app is not None else None,
        "order_qualified": order_qualified,
        "interval_valid": interval_valid,
        "richardson_extrapolated_mmhg": round(f_ext, 8) if f_ext is not None else None,
        "grid_min_mmhg": f_min,
        "grid_max_mmhg": f_max,
        "grid_spread_mmhg": spread,
        "fine_grid_face_min_mmhg_unrounded": f1,
        "discretization_uncertainty_mmhg": u_num,
        "conservative_po2_lower_bound_mmhg": l_bound,
        "conservative_po2_upper_bound_mmhg": u_bound,
        "straddles_canonical_15mmhg_floor": straddles,
        "mesh_convergence_resolved": bool(verdict in ("PASS", "FAIL")),
        "hold_reason_token": hold_tok,
        "verdict_from_mesh_policy": verdict,
    }


def _evaluate_nw08_mesh_convergence(