"expected 'mode_a_default' or 'two_pool_conserved_v2' (IIP-0029; DEC-108)" ) def verify_mode_b_v2_against_scipy_dop853_iip_0029( params: ModeBV2GranuleStoreParams | None = None, *, local_paracrine_preserved_systemic_scale: bool = True, ) -> Dict[str, Any]: """ Independent piecewise SciPy DOP853 verification of Mode B v2 across glucose G(t), RRP store Q_P(t), reserve store Q_R(t), and cumulative pre-hepatic secretion CumPre(t) over 0-1,440 min (breakpoints 600 and 660 min; IIP-0029; DEC-108). """ use_params = params if params is not None else ModeBV2GranuleStoreParams() sim = run_mode_b_v2_24h_metabolic_sensitivity_iip_0029( use_params, local_paracrine_preserved_systemic_scale=local_paracrine_preserved_systemic_scale, return_traces=True, ) rk4_mat = sim["state_matrix"] sys_p = sim["sys_b"] try: import scipy from scipy.integrate import solve_ivp as _dop_solve_ivp except ImportError: return { "scipy_available": False, "verified": False, "note": "SciPy is not installed in this Python interpreter; run with SciPy to execute live DOP853 parity.", } t_s1 = np.linspace(0.0, 600.0, 2401) t_s2 = np.linspace(600.0, 660.0, 241) t_s3 = np.linspace(660.0, 1440.0, 3121) d1 = _dop_solve_ivp(sys_p["rhs"], (0.0, 600.0), sys_p["y0"], method="DOP853", t_eval=t_s1, rtol=1e-11, atol=1e-13, max_step=1.0) d2 = _dop_solve_ivp(sys_p["rhs"], (600.0, 660.0), d1.y[:, -1], method="DOP853", t_eval=t_s2, rtol=1e-11, atol=1e-13, max_step=1.0) d3 = _dop_solve_ivp(sys_p["rhs"], (660.0, 1440.0), d2.y[:, -1], method="DOP853", t_eval=t_s3, rtol=1e-11, atol=1e-13, max_step=1.0) if not (d1.success and d2.success and d3.success): raise RuntimeError("Piecewise SciPy DOP853 verification failed to converge (IIP-0029; DEC-108)") dop_full = np.concatenate([d1.y, d2.y[:, 1:], d3.y[:, 1:]], axis=1) max_g_diff = float(np.max(np.abs(rk4_mat[0, :] - dop_full[0, :]))) max_qp_diff = float(np.max(np.abs(rk4_mat[7, :] - dop_full[7, :]))) max_qr_diff = float(np.max(np.abs(rk4_mat[8, :] - dop_full[8, :]))) max_cum_diff = float(np.max(np.abs(rk4_mat[10, :] - dop_full[10, :]))) max_g_0_600 = float(np.max(np.abs(rk4_mat[0, :2401] - d1.y[0, :]))) return { "scipy_available": True, "scipy_version": str(scipy.__version__), "verified": bool( max_g_diff < 1e-5 and max_qp_diff < 1e-6 and max_qr_diff < 1e-5 and max_cum_diff < 1e-5 ), "dop853_0_to_600min_max_abs_g_diff_mM": max_g_0_600, "dop853_0_to_1440min_max_abs_g_diff_mM": max_g_diff, "dop853_0_to_1440min_max_abs_qp_diff_iu": max_qp_diff, "dop853_0_to_1440min_max_abs_qr_diff_iu": max_qr_diff, "dop853_0_to_1440min_max_abs_cum_prehep_diff_iu": max_cum_diff, } def _replay_live_mode_a_imposed_perifusion_iip_0029( low_mM: float, high_mM: float, pre_low_min: float, high_durations: List[float], low_gaps: List[float], *, force_zero_sst: bool = False, dt_out: float = 0.1, ) -> Dict[str, Any]: """Direct imposed-glucose replay of live Mode A secretion/SST equations showing zero granule conservation.""" def _step(st: np.ndarray, g_val: float, dur: float) -> tuple[np.ndarray, np.ndarray]: n_st = max(1, int(round(dur / dt_out))) t_v = np.linspace(0.0, float(dur), n_st + 1) def _rhs_a(_t: float, y: np.ndarray) -> np.ndarray: u_v, s_v, m_v = float(y[0]), float(y[1]), float(y[2]) gp = max(0.5, float(g_val)) sp = max(0.0, s_v) gs = (sp / (1e-4 + sp)) if sp > 0.0 else 0.0 g_eff = max(0.0, gp - 3.5) k_eff = 7.2 - 3.5 phi_g = (g_eff ** 4.2) / ((k_eff ** 4.2) + (g_eff ** 4.2)) m_t = phi_g / (1.0 + 1.50 * sp * gs) if m_t < m_v: tau_s = 1.85 / (1.0 + 0.8 * sp) if gp < 4.20: tau_s = tau_s * ((gp / 4.20) ** 4) tau_m = 6.80 * (1.0 - gs) + tau_s * gs else: tau_m = 2.00 dm = (m_t - m_v) / tau_m du = (1.2 * m_v - u_v) / 2.5 s_drv = 0.0 if force_zero_sst else (0.65 * u_v + 0.45 * ((gp ** 4) / (5.8 ** 4 + gp ** 4))) ds = (s_drv - s_v) / 1.5 return np.array([du, ds, dm], dtype=np.float64) ym = courant_safe_rk4_integrate_iip_0029(_rhs_a, st, t_v, max_stiff_rate=3.0) gp_c = max(0.5, float(g_val)) sec = np.array( [ ( (6.8 - 0.8 * (0.0 if force_zero_sst else ((max(0.0, ym[1, k]) / (1e-4 + max(0.0, ym[1, k]))) if ym[1, k] > 0 else 0.0))) + 88.0 * max(0.0, ym[2, k]) ) * ( 1.0 - (0.0 if force_zero_sst else ((max(0.0, ym[1, k]) / (1e-4 + max(0.0, ym[1, k]))) if ym[1, k] > 0 else 0.0)) * (1.0 - (gp_c / 4.20) ** 8) if gp_c < 4.20 else 1.0 ) for k in range(len(t_v)) ], dtype=np.float64, ) return ym[:, -1].copy(), sec st = np.array([0.0, 0.0 if force_zero_sst else 0.12, 0.0], dtype=np.float64) st, _ = _step(st, low_mM, pre_low_min) peaks: List[float] = [] for i, hd in enumerate(high_durations): st, sec_hi = _step(st, high_mM, hd) peaks.append(float(np.max(sec_hi))) if i < len(low_gaps): st, _ = _step(st, low_mM, low_gaps[i]) return { "peaks": peaks, "peak2_over_peak1_pct": round(100.0 * peaks[1] / max(1e-15, peaks[0]), 4) if len(peaks) >= 2 else None, } def build_exploratory_mode_b_v2_granule_sensitivity_audit_iip_0029( part_b_mode_a: Dict[str, Any] | None = None, ) -> Dict[str, Any]: """ Build the bounded, strictly opt-in exploratory Mode B v2 two-clock granule-store sensitivity audit attached to Layer 4 Part B (IIP-0029; DEC-108). Preserves exact Mode A default parity and is NEVER a Stage-0 clinical release gate. """ p_portal = ModeBV2GranuleStoreParams(c0_ref_iu_at_300k_beta_eeq=89.9, beta_cell_fraction=0.60, implanted_total_ieq=500_000.0, f_hepatic=0.50) sim_p = run_mode_b_v2_24h_metabolic_sensitivity_iip_0029(p_portal, local_paracrine_preserved_systemic_scale=True) sim_f40 = run_mode_b_v2_24h_metabolic_sensitivity_iip_0029(ModeBV2GranuleStoreParams(f_hepatic=0.40), local_paracrine_preserved_systemic_scale=True) sim_f60 = run_mode_b_v2_24h_metabolic_sensitivity_iip_0029(ModeBV2GranuleStoreParams(f_hepatic=0.60), local_paracrine_preserved_systemic_scale=True) sim_f00 = run_mode_b_v2_24h_metabolic_sensitivity_iip_0029(ModeBV2GranuleStoreParams(f_hepatic=0.00), local_paracrine_preserved_systemic_scale=True) # Real piecewise SciPy DOP853 multi-state verification (G, Q_P, Q_R, CumPre): dop_audit = verify_mode_b_v2_against_scipy_dop853_iip_0029(p_portal, local_paracrine_preserved_systemic_scale=True) # Infinite-store convergence to live Mode A (reference 1,440-min 7-state ODE): p_inf = ModeBV2GranuleStoreParams(c0_ref_iu_at_300k_beta_eeq=1e9, f_rrp=0.10, tau_refill_min=0.5, gamma_amp=5.0, f_hepatic=0.0) sim_inf = run_mode_b_v2_24h_metabolic_sensitivity_iip_0029(p_inf, viable_endocrine_fraction_override=1.0, return_traces=True) lead_a_tir = None lead_a_min_g = None if isinstance(part_b_mode_a, dict) and part_b_mode_a.get("graft_architectures_compared"): lead_a = part_b_mode_a["graft_architectures_compared"][0] lead_a_tir = float(lead_a["time_in_tight_range_3p9_to_10p0_mM_pct"]) lead_a_min_g = float(lead_a["min_glucose_mM"]) # Positivity, mass conservation, small-lag stability, and C^1 biosynthesis saturation: p_stiff = ModeBV2GranuleStoreParams(c0_ref_iu_at_300k_beta_eeq=0.25, f_rrp=0.0045, tau_refill_min=16.5, enable_biosyn=True) sim_stiff = run_mode_b_v2_24h_metabolic_sensitivity_iip_0029(p_stiff, meals_override=[(60.0, 120.0), (240.0, 120.0)]) rep_small_ch = replay_mode_b_v2_perifusion_iip_0029(ModeBV2GranuleStoreParams(tau_chamber_min=0.01), 2.8, 16.7, 30.0, [10.0], [5.0], dt_out=0.2) sim_small_vasc = run_mode_b_v2_24h_metabolic_sensitivity_iip_0029(p_portal, extravascular_insulin_lag_total_min=0.05, local_paracrine_preserved_systemic_scale=True) fast_12h = replay_mode_b_v2_perifusion_iip_0029(p_portal, 2.8, 2.8, 720.0, [10.0], [], dt_out=1.0) sys_p_diag = build_mode_b_v2_granule_store_system_iip_0029(p_portal, assay_context="in_vivo_metabolic_twin") hypo_2mM_biosyn = sys_p_diag["fluxes"](2.0, 0.12, 0.0, sys_p_diag["qp0_iu"], 0.50 * sys_p_diag["qr0_iu"], 0.0)["j_biosyn_iu"] # Effluent shutoff & short low-interval censoring: r2_std = replay_mode_b_v2_perifusion_iip_0029(p_portal, 2.8, 16.7, 90.0, [20.0, 20.0], [15.0], dt_out=0.1) p_ch06 = ModeBV2GranuleStoreParams(tau_chamber_min=0.60) r2_short_2m = replay_mode_b_v2_perifusion_iip_0029(p_ch06, 2.8, 16.7, 90.0, [20.0], [2.0], dt_out=0.1) r2_long_60m = replay_mode_b_v2_perifusion_iip_0029(p_ch06, 2.8, 16.7, 90.0, [20.0], [60.0], dt_out=0.1) # Ceteris-paribus isolated tau_refill = 16.5 -> 45.0 min at fixed 56.88 IU (f_rrp=0.0045, gamma_amp=0.08): c0_w6 = 89.9 * 0.632693 p_w6_fast = ModeBV2GranuleStoreParams(c0_ref_iu_at_300k_beta_eeq=c0_w6, f_rrp=0.0045, tau_refill_min=16.5, gamma_amp=0.08, f_hepatic=0.50) p_w6_slow = ModeBV2GranuleStoreParams(c0_ref_iu_at_300k_beta_eeq=c0_w6, f_rrp=0.0045, tau_refill_min=45.0, gamma_amp=0.08, f_hepatic=0.50) m_5h7h = [(60.0, 75.0), (360.0, 85.0), (780.0, 90.0)] m_4h4h = [(60.0, 75.0), (300.0, 85.0), (540.0, 90.0)] s_fast_57 = run_mode_b_v2_24h_metabolic_sensitivity_iip_0029(p_w6_fast, local_paracrine_preserved_systemic_scale=True, meals_override=m_5h7h) s_slow_57 = run_mode_b_v2_24h_metabolic_sensitivity_iip_0029(p_w6_slow, local_paracrine_preserved_systemic_scale=True, meals_override=m_5h7h) s_fast_44 = run_mode_b_v2_24h_metabolic_sensitivity_iip_0029(p_w6_fast, local_paracrine_preserved_systemic_scale=True, meals_override=m_4h4h) s_slow_44 = run_mode_b_v2_24h_metabolic_sensitivity_iip_0029(p_w6_slow, local_paracrine_preserved_systemic_scale=True, meals_override=m_4h4h) # Local 4-schedule Jacobian & FIM conditioning (illustrating unidentifiability from cross-protocol summaries): fim_scheds = ( ("peak", 2.0, 20.0, 120.0, [18.0, 18.0], [5.0]), ("peak", 2.8, 20.0, 120.0, [19.0, 14.0], [17.0]), ("area", 2.8, 20.0, 90.0, [30.0, 30.0], [30.0]), ("area", 2.8, 20.0, 120.0, [60.0, 60.0], [60.0]), ) def _eval_fim_vec(c0_v: float, fr_v: float, tr_v: float, ga_v: float) -> np.ndarray: pf = ModeBV2GranuleStoreParams(c0_ref_iu_at_300k_beta_eeq=c0_v, f_rrp=fr_v, tau_refill_min=tr_v, gamma_amp=ga_v, enable_biosyn=False, tau_chamber_min=0.0) vals = [] for rk, lo, hi, pre, hd, gp in fim_scheds: rep = replay_mode_b_v2_perifusion_iip_0029(pf, lo, hi, pre, hd, gp, dt_out=0.5) vals.append(rep["peak_ratios"][1] if rk == "peak" else rep["total_area_ratios"][1]) return np.array(vals, dtype=np.float64) th0 = np.array([89.9, 0.0045, 16.5, 0.08], dtype=np.float64) eps = 1e-3 jac = np.zeros((4, 4), dtype=np.float64) for j in range(4): tp, tm = th0.copy(), th0.copy() tp[j] *= math.exp(eps) tm[j] *= math.exp(-eps) jac[:, j] = (_eval_fim_vec(*tp) - _eval_fim_vec(*tm)) / (2.0 * eps) sv = np.linalg.svd(jac, compute_uv=False) jac_cond = float(sv[0] / max(1e-15, sv[-1])) # Live Mode A zero-conservation diagnostic on 5-min gap: ma_5m = _replay_live_mode_a_imposed_perifusion_iip_0029(2.0, 20.0, 120.0, [18.0, 18.0], [5.0], force_zero_sst=False) ma_5m_nosst = _replay_live_mode_a_imposed_perifusion_iip_0029(2.0, 20.0, 120.0, [18.0, 18.0], [5.0], force_zero_sst=True) return { "decision_id": "DEC-108", "contribution_id": "IIP-0029", "evidence_status": "EXPLORATORY_OPT_IN_SENSITIVITY_COMPARATOR_NOT_A_RELEASE_GATE", "default_mode_a_unchanged": True, "mode_a_default_reference_parity": { "lead_60_30_10_mode_a_tir_pct": lead_a_tir, "lead_60_30_10_mode_a_min_glucose_mM": lead_a_min_g, "infinite_store_mode_b_v2_tir_pct": round(sim_inf["tir_3p9_to_10_elapsed_pct"], 2), "infinite_store_mode_b_v2_min_glucose_mM": round(sim_inf["min_glucose_0p25min_mM"], 4), "live_mode_a_5min_gap_peak2_over_peak1_pct": ma_5m["peak2_over_peak1_pct"], "live_mode_a_zero_sst_5min_gap_peak2_over_peak1_pct": ma_5m_nosst["peak2_over_peak1_pct"], }, "real_scipy_dop853_multi_state_verification": dop_audit, "positivity_mass_conservation_and_biosyn_audit": { "c0_0p25_iu_min_qp_iu": sim_stiff["min_qp_iu"], "c0_0p25_iu_relative_mass_error": sim_stiff["relative_mass_conservation_error"], "c0_0p25_iu_m_sec_range": [round(sim_stiff["min_m_sec"], 6), round(sim_stiff["max_m_sec"], 6)], "small_tau_chamber_0p01_min_finite_and_positive": bool(math.isfinite(rep_small_ch["pulses"][0]["peak"]) and rep_small_ch["min_qp_iu"] > 0.0), "small_tau_vasc_0p05_min_max_ip": round(sim_small_vasc["max_abs_ip"], 4), "fasting_12h_store_drift_iu": abs(fast_12h["final_store_iu"] - fast_12h["initial_store_c0_iu"]), "hypoglycemia_2p0mM_biosynthesis_iu_per_min": float(hypo_2mM_biosyn), }, "effluent_shutoff_and_short_interval_censoring_audit": { "tau_chamber_0p35_min_t50_min": round(float(r2_std["pulses"][0]["effluent_stepdown_t50_min"]), 4), "tau_chamber_0p35_min_t90_min": round(float(r2_std["pulses"][0]["effluent_stepdown_t90_min"]), 4), "tau_chamber_0p60_min_2min_low_t90_censored": r2_short_2m["pulses"][0]["effluent_stepdown_t90_min"], "tau_chamber_0p60_min_60min_low_t90_min": round(float(r2_long_60m["pulses"][0]["effluent_stepdown_t90_min"]), 4), "instantaneous_stepdown_drop_pct": round(float(r2_std["instantaneous_stepdown_drop_pct"]), 6),