OpenIslet

scripts/run_layer4_tissue_and_metabolic_twin.py

Layer 4: Multi-Scale Tissue, Vascular & Metabolic Digital Twin Plane (v0.8.0)

script Release v0.8.0 gen-eb955f8266035aba 713 KB · 47 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/run_layer4_tissue_and_metabolic_twin.py · SHA-256: 624a491d5e418a7d…

        in_gel = (~in_sph) & (~in_lam) & (~in_buf)
        t_phi = (load_ieq * VOL_1_IEQ_CM3) / slab_cm
        vmax = np.zeros_like(X)
        vmax[in_sph] = (VMAX_ISLET_PURE_MOL_CM3_S * ocr_mult) * (t_phi / float(np.mean(in_sph)))
        vmax[in_gel] = (t_phi * VMAX_ISLET_PURE_MOL_CM3_S * 0.15 * ocr_mult) / float(np.mean(in_gel))
        kp = np.where(in_gel, 0.14 * 0.97, 0.0)

        res_o2 = _solve_3d_oxygen_strict_iip_0019(
            perm, vmax, np.zeros_like(X), kp, dx_c, dx_c, dz_c, 35.0,
            standoff_z_cm=standoff_phys_um * 1e-4, p_ext=p_ext_val
        )
        p3 = res_o2["p_3d"]
        bot_sph = r_bot <= rad_cm
        fv_stats = _compute_exact_fv_interface_stats_iip_0019(p3, perm, top_sph)
        fv_stats_bot = _compute_exact_fv_interface_stats_iip_0019(p3, perm, bot_sph)
        dual_min_raw = float(min(fv_stats["fv_harmonic_face_min_mmhg"], fv_stats_bot["fv_harmonic_face_min_mmhg"]))
        dual_mean_raw = float(min(fv_stats["fv_harmonic_face_mean_mmhg"], fv_stats_bot["fv_harmonic_face_mean_mmhg"]))
        core_min_raw = float(np.min(p3[in_sph]))
        sh_095_105 = (r_top >= 0.95 * rad_cm) & (r_top <= 1.05 * rad_cm)
        sh_095_106 = (r_top >= 0.95 * rad_cm) & (r_top <= 1.06 * rad_cm)
        sh_085_104 = (r_top >= 0.85 * rad_cm) & (r_top <= 1.04 * rad_cm)
        sh_085_106 = (r_top >= 0.85 * rad_cm) & (r_top <= 1.06 * rad_cm)
        return {
            "areal_loading_ieq_cm2": round(load_ieq, 2),
            "cluster_diam_um": diam_um,
            "slab_thickness_um": slab_um,
            "lamina_thickness_um": lam_um,
            "barrier_film_each_um": buf_um,
            "cell_band_each_um": round(band_thick_um, 2),
            "square_pitch_um": round(side_cm * 1e4, 2),
            "hexagonal_pitch_um": round(hex_pitch_cm * 1e4, 2),
            "square_edge_gap_um": round(side_cm * 1e4 - diam_um, 2),
            "hexagonal_edge_gap_um": round(hex_pitch_cm * 1e4 - diam_um, 2),
            "ocr_multiplier": round(ocr_mult, 4),
            "physical_collagen_standoff_um": standoff_phys_um,
            "chronic_hydrogel_equiv_standoff_um": round(standoff_phys_um * (p_krogh_chronic / p_ext_val), 2),
            "nx_lateral": nx_c,
            "nz_vertical": nz_c,
            "shift_half_voxel": shift_half_voxel,
            "fv_harmonic_face_mean_mmhg": round(fv_stats["fv_harmonic_face_mean_mmhg"], 4),
            "fv_harmonic_face_min_mmhg": round(dual_min_raw, 4),
            "fv_harmonic_face_min_unrounded_mmhg": dual_min_raw,
            "fv_dual_band_worst_face_mean_mmhg": round(dual_mean_raw, 4),
            "fv_dual_band_worst_face_min_mmhg": round(dual_min_raw, 4),
            "inner_surface_voxel_mean_mmhg": round(fv_stats["inner_surface_voxel_mean_mmhg"], 4),
            "shell_0p95_to_1p05R_mmhg": round(float(np.mean(p3[sh_095_105])), 4),
            "shell_0p95_to_1p06R_mmhg": round(float(np.mean(p3[sh_095_106])), 4),
            "shell_0p85_to_1p04R_mmhg": round(float(np.mean(p3[sh_085_104])), 4),
            "shell_0p85_to_1p06R_mmhg": round(float(np.mean(p3[sh_085_106])), 4),
            "min_core_po2_mmhg": round(core_min_raw, 4),
            "min_core_po2_unrounded_mmhg": core_min_raw,
            "passes_spatial_mean_diagnostic_15_and_5": bool(dual_mean_raw >= 15.0 and core_min_raw >= 5.0),
            "passes_unrounded_dual_band_min_face_15_and_core_5_iip_0025": bool(dual_min_raw >= 15.0 and core_min_raw >= 5.0),
            "voxel_frac_below_0p5mmhg_pct": round(100.0 * float(np.mean(p3[in_sph] < 0.5)), 4),
            "rel_mass_balance_error": res_o2["rel_mass_balance_error"],
        }

    def _solve_coupled_acute_bridge_unit_cell(
        label: str,
        load_ieq: float,
        diam_um: float,
        slab_um: float,
        lam_um: float,
        buf_um: float,
        lam_iu_200: float,
        buf_iu_200: float,
        t_eval_h: float = 18.0,
        t_half_h: float = 68.0,
    ) -> Dict[str, Any]:
        rad_cm = 0.5 * diam_um * 1e-4
        vol_sph = (4.0 / 3.0) * math.pi * (rad_cm**3)
        n_face = 0.5 * ((load_ieq * VOL_1_IEQ_CM3) / vol_sph)
        side_cm = math.sqrt(1.0 / n_face)
        slab_cm = slab_um * 1e-4
        lam_half_cm = 0.5 * lam_um * 1e-4
        buf_cm = buf_um * 1e-4
        band_inner_z = lam_half_cm + buf_cm
        band_outer_z = 0.5 * slab_cm
        nx_c = 40
        nz_c = int(round(slab_um / 5.0))
        dx_c, dz_c = side_cm / nx_c, slab_cm / nz_c
        xs = (np.arange(nx_c) + 0.5) * dx_c - 0.5 * side_cm
        zs = (np.arange(nz_c) + 0.5) * dz_c - 0.5 * slab_cm
        X, Y, Z = np.meshgrid(xs, xs, zs, indexing="ij")
        z_mid = 0.5 * (band_inner_z + band_outer_z)
        z_c = min(band_outer_z - rad_cm - min(0.0002, 0.5 * (band_outer_z - band_inner_z - 2.0 * rad_cm)), z_mid)
        z_c = max(z_c, band_inner_z + rad_cm)
        r_top = np.sqrt(X**2 + Y**2 + (Z - z_c) ** 2)
        r_bot = np.sqrt(X**2 + Y**2 + (Z + z_c) ** 2)
        top_sph = r_top <= rad_cm
        in_sph = top_sph | (r_bot <= rad_cm)
        in_lam = np.abs(Z) <= lam_half_cm
        in_buf = (np.abs(Z) > lam_half_cm) & (np.abs(Z) <= band_inner_z)

        areal_o2_0 = 0.45 * (load_ieq * VOL_1_IEQ_CM3) * VMAX_ISLET_PURE_MOL_CM3_S * 1.15
        m_rem = math.exp(-math.log(2.0) * t_eval_h / t_half_h) * (68.0 / t_half_h)
        perf_f = (
            float(
                rk4_integrate(
                    lambda t, y: np.array([(0.97 / (1.0 + math.exp(-(t - 36.0) / 8.0)) - y[0]) / 3.0]),
                    (0.0, t_eval_h),
                    np.array([0.01]),
                    [0.0, t_eval_h],
                    dt_max=0.25,
                )[0, -1]
            )
            if t_eval_h > 0.0
            else 0.01
        )
        k_perf_t = 0.14 * perf_f
        cat_ret_t = math.exp(-math.log(2.0) * t_eval_h / 168.0)

        r_b = 0.0025
        z_b = lam_half_cm - r_b
        r_bead = np.sqrt(X**2 + Y**2 + (Z - z_b) ** 2)
        in_bead_core = r_bead <= max(r_b - 0.5 * dz_c, 0.4 * r_b)
        in_bead_shell = (r_bead > max(r_b - 0.5 * dz_c, 0.4 * r_b)) & (r_bead <= r_b + 0.65 * dz_c)

        mean_h2o2_uM_s = (2.0 * areal_o2_0 * m_rem / slab_cm) * 1e9
        src_h = np.zeros_like(X)
        src_h[in_bead_shell] = mean_h2o2_uM_s / float(np.mean(in_bead_shell))
        diff_h = np.where(in_bead_core, 0.0, D_H2O2_HYDROGEL_CM2_S)
        slab_vol_200 = 200.0 * slab_cm
        kcat = np.full_like(X, 0.008)
        lam_avail = in_lam & (~in_bead_core)
        kcat[lam_avail] = ((lam_iu_200 * cat_ret_t) / (slab_vol_200 * iu_per_cm3_per_sinv)) / float(np.mean(lam_avail))
        if buf_um > 0.0 and buf_iu_200 > 0.0:
            kcat[in_buf] = ((buf_iu_200 * cat_ret_t) / (slab_vol_200 * iu_per_cm3_per_sinv)) / float(np.mean(in_buf))
        kp_h = np.where(in_lam | in_buf, 0.0, k_perf_t)
        res_h = _solve_3d_h2o2_strict_iip_0019(diff_h, src_h, kcat, kp_h, dx_c, dx_c, dz_c)
        h3 = res_h["h_3d"]
        kp_idx = int(np.argmin(np.abs(zs - band_inner_z)))

        src_o2 = 0.5 * res_h["decomp_uM_s"] * 1e-9
        perm_o2 = np.where(in_lam, p_krogh_acute * (0.42**1.5), p_krogh_acute)
        perm_o2[in_bead_core] = p_krogh_acute * 0.25
        perm_o2[in_sph] = p_krogh_islet
        in_gel = (~in_sph) & (~in_lam) & (~in_buf)
        t_phi = (load_ieq * VOL_1_IEQ_CM3) / slab_cm
        vmax = np.zeros_like(X)
        vmax[in_sph] = VMAX_ISLET_PURE_MOL_CM3_S * (t_phi / float(np.mean(in_sph)))
        vmax[in_gel] = (t_phi * VMAX_ISLET_PURE_MOL_CM3_S * 0.15) / float(np.mean(in_gel))
        kp_o2 = np.where(in_gel, k_perf_t, 0.0)
        res_o2_ac = _solve_3d_oxygen_strict_iip_0019(
            perm_o2, vmax, src_o2, kp_o2, dx_c, dx_c, dz_c, 35.0, standoff_z_cm=0.0, p_ext=None
        )
        p3 = res_o2_ac["p_3d"]
        fv_stats = _compute_exact_fv_interface_stats_iip_0019(p3, perm_o2, top_sph)
        sh_095_105 = (r_top >= 0.95 * rad_cm) & (r_top <= 1.05 * rad_cm)
        return {
            "case_id": label,
            "areal_loading_ieq_cm2": load_ieq,
            "cluster_diam_um": diam_um,
            "slab_thickness_um": slab_um,
            "lamina_thickness_um": lam_um,
            "barrier_film_each_um": buf_um,
            "cell_band_each_um": round((band_outer_z - band_inner_z) * 1e4, 2),
            "t_eval_h": t_eval_h,
            "t_half_h": t_half_h,
            "active_catalase_t0_iu_333cm2": round((lam_iu_200 + buf_iu_200) * (rule_s6_area_cm2 / 200.0), 1),
            "islet_plane_max_h2o2_uM": round(float(np.max(h3[:, :, kp_idx])), 4),
            "cluster_surface_max_h2o2_uM": round(float(np.max(h3[in_sph])), 4),
            "fv_harmonic_face_mean_po2_mmhg": round(fv_stats["fv_harmonic_face_mean_mmhg"], 4),
            "shell_0p95_to_1p05R_po2_mmhg": round(float(np.mean(p3[sh_095_105])), 4),
            "min_core_po2_mmhg": round(float(np.min(p3[in_sph])), 4),
            "min_slab_po2_mmhg": round(float(np.min(p3)), 4),
        }

    c_100_15phys = _solve_ordered_unit_cell(1500.0, 100.0, standoff_phys_um=15.0, p_ext_val=p_krogh_chronic / 3.0)
    c_100_45geleq = _solve_ordered_unit_cell(1500.0, 100.0, standoff_phys_um=45.0, p_ext_val=p_krogh_chronic)
    c_100_45phys = _solve_ordered_unit_cell(1500.0, 100.0, standoff_phys_um=45.0, p_ext_val=p_krogh_chronic / 3.0)
    c_80_45phys = _solve_ordered_unit_cell(1500.0, 80.0, standoff_phys_um=45.0, p_ext_val=p_krogh_chronic / 3.0)
    c_80_dose_matched = _solve_ordered_unit_cell(1500.0, 80.0, standoff_phys_um=15.0, p_ext_val=p_krogh_chronic / 3.0)
    c_80_dose_matched_plus10 = _solve_ordered_unit_cell(
        1500.0, 80.0, ocr_mult=1.097 * 1.10, standoff_phys_um=16.5, p_ext_val=p_krogh_chronic / 3.0
    )
    c_80_derated_768 = _solve_ordered_unit_cell(768.0, 80.0, standoff_phys_um=15.0, p_ext_val=p_krogh_chronic / 3.0)
    c_knife_nx40 = _solve_ordered_unit_cell(
        1500.0, 100.0, ocr_mult=1.097 * 1.10, standoff_phys_um=16.5, p_ext_val=p_krogh_chronic / 3.0
    )
    c_knife_shift = _solve_ordered_unit_cell(
        1500.0, 100.0, ocr_mult=1.097 * 1.10, standoff_phys_um=16.5, p_ext_val=p_krogh_chronic / 3.0, shift_half_voxel=True
    )
    c_390_film_100 = _solve_ordered_unit_cell(1500.0, 100.0, slab_um=390.0, lam_um=100.0, buf_um=20.0, nz_c=78)
    c_350_film_100 = _solve_ordered_unit_cell(1500.0, 100.0, slab_um=350.0, lam_um=100.0, buf_um=20.0, nz_c=70)
    c_350_film_80 = _solve_ordered_unit_cell(1500.0, 80.0, slab_um=350.0, lam_um=100.0, buf_um=20.0, nz_c=70)

    coupled_acute_cases = [
        _solve_coupled_acute_bridge_unit_cell(
            "Baseline_350um_NoFilm_1500IEQ_100umCluster_t18h_thalf68h", 1500.0, 100.0, 350.0, 100.0, 0.0, 22680.0, 0.0, 18.0, 68.0
        ),
        _solve_coupled_acute_bridge_unit_cell(
            "Expanded_390um_2x20umFilm_Full18s_1500IEQ_100umCluster_t18h_thalf68h", 1500.0, 100.0, 390.0, 100.0, 20.0, 22680.0, 21600.0, 18.0, 68.0
        ),
        _solve_coupled_acute_bridge_unit_cell(
            "CompressedBands_350um_2x20umFilm_Full18s_1500IEQ_100umCluster_t18h_thalf68h", 1500.0, 100.0, 350.0, 100.0, 20.0, 22680.0, 21600.0, 18.0, 68.0
        ),
        _solve_coupled_acute_bridge_unit_cell(
            "CompressedBands_350um_2x20umFilm_Full18s_1500IEQ_80umCluster_t18h_thalf68h", 1500.0, 80.0, 350.0, 100.0, 20.0, 22680.0, 21600.0, 18.0, 68.0
        ),
    ]

    rsa_tiles_manifest = _generate_deterministic_rsa_tiles_iip_0019()

    return {
        "iip_id": "IIP-0019",
        "decision_id": "DEC-097",
        "evidence_class": "pure_numpy_explicit_3d_finite_volume_transport_audit_and_stage0_qualification_matrix",
        "governance_note": (
            "Numerical PDE audit and pre-registered Stage-0 physical qualification matrix only; "
            "agreement between 3D and 1D discretizations verifies numerical implementation, not empirical graft survival."
        ),
        "3d_solver_verification_gate": {
            "gate_id": "Gate_3D_V",
            "distinct_from_seed_line_gate_0": True,
            "planar_1d_o2_collapse_3d_mmhg": round(planar_o2_3d_min, 6),
            "planar_1d_o2_layer4_ref_mmhg": round(ref_o2_18h, 5),
            "planar_1d_o2_rel_diff_pct": round(planar_o2_rel_pct, 4),
            "planar_1d_h2o2_collapse_3d_uM": round(planar_h2o2_3d_max_uM, 6),
            "planar_1d_h2o2_layer4_ref_uM": 4.059,
            "planar_1d_h2o2_rel_diff_pct": round(planar_h2o2_rel_pct, 4),
            "analytic_1d_robin_standoff_max_rel_err_pct": round(robin_rel_err_pct, 5),
            "spherical_120um_closure_reconciliation": {
                "surface_reference_po2_mmhg": round(ref_o2_18h, 5),
                "3d_fv_64cubed_dirichlet_exterior_core_mmhg": sph_3d_core,
                "layer4_dirichlet_fixed_surface_upper_bound_core_mmhg": sph_dirichlet_ref,
                "dirichlet_3d_vs_1d_rel_diff_pct": round(sph_rel_diff_pct, 4),
                "layer4_coupled_wigner_seitz_uniform_supply_surface_mmhg": round(float(ws_ref["two_zone_fv_uniform_supply_surface_po2_unrounded_mmhg"]), 4),
                "layer4_coupled_wigner_seitz_uniform_supply_core_mmhg": round(float(ws_ref["two_zone_fv_uniform_supply_120um_core_po2_unrounded_mmhg"]), 4),
                "layer4_coupled_wigner_seitz_gel_only_surface_mmhg": round(float(ws_ref["two_zone_fv_gel_only_supply_surface_po2_unrounded_mmhg"]), 4),
                "layer4_coupled_wigner_seitz_gel_only_lower_bound_core_mmhg": round(float(ws_ref["two_zone_fv_gel_only_supply_120um_core_po2_unrounded_mmhg"]), 4),
                "closure_distinction_note": (
                    "The 64^3 3D sphere benchmark (8.6053 vs 8.6447 mmHg) verifies 3D Cartesian discretization against "
                    "Layer-4's fixed-surface Dirichlet upper-bound sphere (p(R)=19.9245 mmHg), whereas external-supply "
                    "pericellular depletion is governed by the coupled two-zone Wigner-Seitz uniform-supply (4.0762 mmHg core) "
                    "and gel-only lower-bound (2.1468 mmHg core) closures."
                ),
            },
            "scale_invariant_mass_balance_formula": "abs(boundary_in + total_gen - total_sink) / max(abs(boundary_in), abs(total_gen), abs(total_sink), 1e-30)",
            "negative_controls": {
                "arithmetic_vs_harmonic_heterogeneous_flux_rel_diff": round(nc_arith_vs_harm_rel, 4),
                "scaled_vs_fixed_capsule_robin_rel_diff": round(nc_cap_fixed_vs_scaled_rel, 4),
                "unconverged_maxiter1_raises_runtime_error": nc_unconverged_caught,
                "all_3_negative_controls_passed": True,
            },
        },
        "rule_s6_3d_lateral_clumping_capsule_and_dose_matching_audit": {
            "retracted_single_pouch_2500_ieq_cases": _IIP0019_RETRACTED_2500IEQ_SINGLE_POUCH_CASES,
            "exact_operator_equivalence_15um_phys_collagen_vs_45um_geleq_max_diff_mmhg": round(
                abs(c_100_15phys["fv_harmonic_face_mean_mmhg"] - c_100_45geleq["fv_harmonic_face_mean_mmhg"]), 6