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…

        r40 = _solve_uc_iip25(load_ieq, diam_um, slab_um=slab_um, buf_um=buf_um, standoff_phys_um=standoff_phys_um, ocr_mult=ocr_mult, nx_c=40)
        r60 = _solve_uc_iip25(load_ieq, diam_um, slab_um=slab_um, buf_um=buf_um, standoff_phys_um=standoff_phys_um, ocr_mult=ocr_mult, nx_c=60)
        r80 = _solve_uc_iip25(load_ieq, diam_um, slab_um=slab_um, buf_um=buf_um, standoff_phys_um=standoff_phys_um, ocr_mult=ocr_mult, nx_c=80)
        mins = [r40["fv_harmonic_face_min_unrounded_mmhg"], r60["fv_harmonic_face_min_unrounded_mmhg"], r80["fv_harmonic_face_min_unrounded_mmhg"]]
        cores = [r40["min_core_po2_unrounded_mmhg"], r60["min_core_po2_unrounded_mmhg"], r80["min_core_po2_unrounded_mmhg"]]
        min_all = min(mins)
        core_all = min(cores)
        return {
            "nx40": r40,
            "nx60": r60,
            "nx80": r80,
            "fv_face_min_by_nx_40_60_80_mmhg": [round(v, 4) for v in mins],
            "min_over_nx_40_60_80_unrounded_mmhg": min_all,
            "min_over_nx_40_60_80_mmhg": round(min_all, 4),
            "mesh_spread_mmhg": round(max(mins) - min_all, 4),
            "min_core_over_nx_40_60_80_mmhg": round(core_all, 4),
            "passes_unrounded_multimesh_dual_band_iip_0025": bool(min_all >= 15.0 and core_all >= 5.0),
        }

    mm_100_nofilm = _multimesh_pack(1500.0, 100.0, slab_um=350.0, buf_um=0.0)
    mm_100_350bar = _multimesh_pack(1500.0, 100.0, slab_um=350.0, buf_um=20.0)
    mm_100_390bar = _multimesh_pack(1500.0, 100.0, slab_um=390.0, buf_um=20.0)
    mm_80_nofilm = _multimesh_pack(1500.0, 80.0, slab_um=350.0, buf_um=0.0)
    mm_80_350bar = _multimesh_pack(1500.0, 80.0, slab_um=350.0, buf_um=20.0)
    mm_80_390bar = _multimesh_pack(1500.0, 80.0, slab_um=390.0, buf_um=20.0)
    mm_80_plus10_350bar = _multimesh_pack(1500.0, 80.0, slab_um=350.0, buf_um=20.0, standoff_phys_um=16.5, ocr_mult=1.097 * 1.10)
    uc_80_plus10_nofilm_nx40 = _solve_uc_iip25(1500.0, 80.0, slab_um=350.0, buf_um=0.0, standoff_phys_um=16.5, ocr_mult=1.097 * 1.10, nx_c=40)
    uc_80_350bar_shift_nx40 = _solve_uc_iip25(1500.0, 80.0, slab_um=350.0, buf_um=20.0, nx_c=40, shift_half_voxel=True)

    # 60x60x105 7,200-voxel boundary-plane reflection-symmetry diagnostics
    load_095 = (6.0 / 7.0) * 1500.0 / 0.95
    sym_095_inc_fv = _solve_uc_iip25(load_095, 80.0, 350.0, 100.0, 20.0, standoff_phys_um=16.5, ocr_mult=1.097 * 1.10, nx_c=60, nz_c=105, eps_lam_cm=+1e-12, gel_ref_load_ieq=1500.0)
    sym_095_exc_fv = _solve_uc_iip25(load_095, 80.0, 350.0, 100.0, 20.0, standoff_phys_um=16.5, ocr_mult=1.097 * 1.10, nx_c=60, nz_c=105, eps_lam_cm=-1e-12, gel_ref_load_ieq=1500.0)
    sym_095_inc_co = _solve_uc_iip25(load_095, 80.0, 350.0, 100.0, 20.0, standoff_phys_um=16.5, ocr_mult=1.097 * 1.10, nx_c=60, nz_c=105, eps_lam_cm=+1e-12)
    sym_095_exc_co = _solve_uc_iip25(load_095, 80.0, 350.0, 100.0, 20.0, standoff_phys_um=16.5, ocr_mult=1.097 * 1.10, nx_c=60, nz_c=105, eps_lam_cm=-1e-12)
    sym_nom80_inc = _solve_uc_iip25(1500.0, 80.0, 350.0, 100.0, 20.0, nx_c=60, nz_c=105, eps_lam_cm=+1e-12)
    sym_nom80_exc = _solve_uc_iip25(1500.0, 80.0, 350.0, 100.0, 20.0, nx_c=60, nz_c=105, eps_lam_cm=-1e-12)

    layout_b_119um_caught = False
    try:
        _solve_uc_iip25(1500.0, 100.0, slab_um=350.0, lam_um=119.0, buf_um=20.0, nx_c=40)
    except RuntimeError:
        layout_b_119um_caught = True

    # Unrounded Wigner-Seitz BVP 0.497 mmHg boundary check (preventing 0.497 -> 0.50 false pass)
    lo_ws, hi_ws = 5.0, 20.0
    for _ in range(70):
        mid_ws = 0.5 * (lo_ws + hi_ws)
        if solve_spherical_cluster_po2_bvp(mid_ws, 0.0060)["wigner_seitz_two_zone_fv_gel_only_supply_core_po2_unrounded_mmhg"] < 0.497:
            lo_ws = mid_ws
        else:
            hi_ws = mid_ws
    bvp_0497 = solve_spherical_cluster_po2_bvp(0.5 * (lo_ws + hi_ws), 0.0060)

    # Design Z & Contrast B3 unrounded fv_harmonic_face_min_mmhg cases & root solves
    def _find_z_cutoff(diam_um: float, strom: float, kp_v: float, nx_z: int, nz_z: int, lo_init: float, hi_init: float, ext_f: float = 1.0) -> float:
        a, b = lo_init, hi_init

        def _margin(pb_v: float) -> float:
            rz = compute_design_z_140um_parameterized_case(
                bed_po2_mmhg=pb_v,
                ocr_multiplier=1.097,
                d_gel_cm2_s=1.5e-5,
                k_perf_s_inv=kp_v,
                cluster_diam_um=diam_um,
                standoff_ext_diffusivity_factor=ext_f,
                stromal_ocr_fraction=strom,
                nx_override=nx_z,
                nz_override=nz_z,
            )
            return min(rz["fv_harmonic_face_min_unrounded_mmhg"] - 15.0, rz["min_core_po2_unrounded_mmhg"] - 5.0)

        fa, fb = _margin(a), _margin(b)
        while fa >= 0.0:
            a -= 1.0
            fa = _margin(a)
        while fb < 0.0:
            b += 1.0
            fb = _margin(b)
        side_flag = 0
        for _ in range(14):
            c = (fa * b - fb * a) / (fa - fb)
            fc = _margin(c)
            if abs(fc) < 1e-9 or abs(b - a) < 1e-7:
                return c
            if fc >= 0.0:
                b, fb = c, fc
                if side_flag == 1:
                    fa *= 0.5
                side_flag = 1
            else:
                a, fa = c, fc
                if side_flag == -1:
                    fb *= 0.5
                side_flag = -1
        return (fa * b - fb * a) / (fa - fb)

    z_pb30_100 = compute_design_z_140um_parameterized_case(bed_po2_mmhg=30.0, ocr_multiplier=1.097, d_gel_cm2_s=1.5e-5, k_perf_s_inv=0.1358, cluster_diam_um=100.0)
    z_pb30_80 = compute_design_z_140um_parameterized_case(bed_po2_mmhg=30.0, ocr_multiplier=1.097, d_gel_cm2_s=1.5e-5, k_perf_s_inv=0.1358, cluster_diam_um=80.0)
    z_pb30_100_div3 = compute_design_z_140um_parameterized_case(
        bed_po2_mmhg=30.0, ocr_multiplier=1.097, d_gel_cm2_s=1.5e-5, k_perf_s_inv=0.1358, cluster_diam_um=100.0, standoff_ext_diffusivity_factor=1.0 / 3.0
    )
    z_pfc5_25_100 = compute_design_z_140um_parameterized_case(
        bed_po2_mmhg=25.0, ocr_multiplier=1.097, d_gel_cm2_s=1.5e-5, k_perf_s_inv=0.1358, pfc_vol_fraction=0.05, cluster_diam_um=100.0
    )
    z_pfc5_25_80 = compute_design_z_140um_parameterized_case(
        bed_po2_mmhg=25.0, ocr_multiplier=1.097, d_gel_cm2_s=1.5e-5, k_perf_s_inv=0.1358, pfc_vol_fraction=0.05, cluster_diam_um=80.0
    )
    b3_pb30_intact_100 = compute_design_z_140um_parameterized_case(
        bed_po2_mmhg=30.0, ocr_multiplier=1.097, d_gel_cm2_s=1.5e-5, k_perf_s_inv=0.1358, cluster_diam_um=100.0, stromal_ocr_fraction=0.0
    )
    b3_pb30_intact_80 = compute_design_z_140um_parameterized_case(
        bed_po2_mmhg=30.0, ocr_multiplier=1.097, d_gel_cm2_s=1.5e-5, k_perf_s_inv=0.1358, cluster_diam_um=80.0, stromal_ocr_fraction=0.0
    )
    b3_pb30_halved_100 = compute_design_z_140um_parameterized_case(
        bed_po2_mmhg=30.0, ocr_multiplier=1.097, d_gel_cm2_s=1.5e-5, k_perf_s_inv=0.0679, cluster_diam_um=100.0, stromal_ocr_fraction=0.0
    )
    b3_pb30_halved_80 = compute_design_z_140um_parameterized_case(
        bed_po2_mmhg=30.0, ocr_multiplier=1.097, d_gel_cm2_s=1.5e-5, k_perf_s_inv=0.0679, cluster_diam_um=80.0, stromal_ocr_fraction=0.0
    )

    cut_100_48 = _find_z_cutoff(100.0, 0.15, 0.1358, 48, 28, 30.0, 31.5, 1.0)
    cut_100_72 = _find_z_cutoff(100.0, 0.15, 0.1358, 72, 42, cut_100_48 - 0.15, cut_100_48 + 0.15, 1.0)
    cut_80_48 = _find_z_cutoff(80.0, 0.15, 0.1358, 48, 28, 30.0, 31.5, 1.0)
    cut_80_72 = _find_z_cutoff(80.0, 0.15, 0.1358, 72, 42, cut_80_48 - 0.15, cut_80_48 + 0.15, 1.0)
    cut_100_div3 = _find_z_cutoff(100.0, 0.15, 0.1358, 48, 28, 34.5, 36.5, 1.0 / 3.0)
    cut_b3_intact_100 = _find_z_cutoff(100.0, 0.0, 0.1358, 48, 28, 28.5, 30.5, 1.0)
    cut_b3_intact_80 = _find_z_cutoff(80.0, 0.0, 0.1358, 48, 28, 28.5, 30.5, 1.0)
    cut_b3_halved_100 = _find_z_cutoff(100.0, 0.0, 0.0679, 48, 28, 29.5, 31.5, 1.0)
    cut_b3_halved_80 = _find_z_cutoff(80.0, 0.0, 0.0679, 48, 28, 29.5, 31.5, 1.0)

    z_ribbon_vol_cm3 = 0.0140 * (1000.0 / 3.0)
    pfc_5pct_vol_cm3 = 0.05 * z_ribbon_vol_cm3
    pfc_5pct_o2_umol = pfc_5pct_vol_cm3 * (20.0 * ALPHA_BUNSEN_MOL_CM3_MMHG) * (40.0 - 15.0) * 1e6
    graft_500k_demand_umol_s = (curative_ieq * VOL_1_IEQ_CM3 * VMAX_ISLET_PURE_MOL_CM3_S * 1.15) * 1e6
    pfc_5pct_bridge_s = pfc_5pct_o2_umol / graft_500k_demand_umol_s

    # Gate P05 two-timepoint (Day-7 >= 70.0% + 336-h >= 73,800 IU P04040) sizing & dual-basis H2O2 profiles
    target_336h_iu = 73_800.0
    precure_cap_iu = 158_300.0
    cure_yield_assumed = 0.65
    postcure_at_cap_iu = precure_cap_iu * cure_yield_assumed  # 102,895.0 IU
    spec_act_iu_mg = 50_000.0
    k2_total_mass_mg = ((target_336h_iu / 0.25) / cure_yield_assumed) / spec_act_iu_mg  # 9.083076923076924 mg

    p05_sizing_scenarios = []
    for sid, d7_pct, r14_frac, t0_iu in (
        ("Live_Undersized_Day7_Basis_105429IU", 70.0, 0.49, target_336h_iu / 0.70),
        ("Ref_50pct_14d_Retention_147600IU", round(100.0 * math.sqrt(0.50), 4), 0.50, target_336h_iu / 0.50),
        ("K1_0p70sq_49pct_14d_Retention_150612IU", 70.0, 0.49, target_336h_iu / (0.70 ** 2)),
        ("Reconciled_65pctYield_80pctDay7_75pctDay14_102895IU", 80.0, 0.75, postcure_at_cap_iu),
        ("Adverse_K2_tHalf168h_295200IU_Failed_Day7", 50.0, 0.25, target_336h_iu / 0.25),
    ):
        precure_iu = t0_iu / cure_yield_assumed
        tot_mg = precure_iu / spec_act_iu_mg
        rem_336_iu = t0_iu * r14_frac
        exact_eu_cap = min(0.10, 0.50 / tot_mg)
        d7_ok = d7_pct >= 70.0
        d14_ok = rem_336_iu >= target_336h_iu - 1e-6
        cap_ok = precure_iu <= precure_cap_iu + 1e-6
        p05_sizing_scenarios.append({
            "scenario_id": sid,
            "assumed_day7_retention_pct": d7_pct,
            "passes_day7_70pct_gate": bool(d7_ok),
            "assumed_336h_retention_fraction": r14_frac,
            "retained_336h_active_iu_333cm2": round(rem_336_iu, 2),
            "passes_336h_73800iu_gate": bool(d14_ok),
            "post_cure_t0_active_iu_333cm2": round(t0_iu, 2),
            "precure_input_iu_at_assumed_65pct_yield": round(precure_iu, 1),
            "within_158300u_precure_ceiling": bool(cap_ok),
            "reconciles_day7_336h_and_158300u_cap": bool(d7_ok and d14_ok and cap_ok),
            "total_p04040_protein_mass_unrounded_mg": tot_mg,
            "total_p04040_protein_mass_mg": round(tot_mg, 4),
            "exact_unrounded_endotoxin_cap_eu_per_mg": exact_eu_cap,
            "display_5dec_endotoxin_cap_eu_per_mg": round(exact_eu_cap, 5),
        })

    h_cm = 2.5e-4
    nz3 = int(round(0.0350 / h_cm))
    r_b = 25.0e-4
    v_b = (4.0 / 3.0) * math.pi * (r_b ** 3)
    lam_cm = 0.0100
    side_cm = max(math.sqrt(v_b / (0.58 * lam_cm)), 50.0 * 1.15e-4)
    nx3 = max(16, int(round(side_cm / h_cm)))
    dx3 = side_cm / nx3
    xs3 = (np.arange(nx3) + 0.5) * dx3 - 0.5 * side_cm
    zs3 = (np.arange(nz3) + 0.5) * h_cm - 0.0175
    X3, Y3, Z3 = np.meshgrid(xs3, xs3, zs3, indexing="ij")
    r_dist = np.sqrt(X3 ** 2 + Y3 ** 2 + (Z3 - (0.5 * lam_cm - r_b)) ** 2)
    in_core = r_dist <= (r_b - 0.5 * h_cm)
    in_shell = (r_dist > (r_b - 0.5 * h_cm)) & (r_dist <= (r_b + 0.6 * h_cm))
    in_lam = np.abs(Z3) <= 0.5 * lam_cm
    in_buf = (np.abs(Z3) > 0.5 * lam_cm) & (np.abs(Z3) <= (0.5 * lam_cm + 0.0020))
    diff_3d = np.where(in_core, 0.0, D_H2O2_HYDROGEL_CM2_S)
    kp_3d = np.where(in_lam | in_buf, 0.0, 0.14 * 0.01)
    kp_idx = int(np.argmin(np.abs(zs3 - 0.0070)))

    iu_sweep = [
        ("t0_105429_iu", target_336h_iu / 0.70),
        ("retained_73800_iu", 73_800.0),
        ("undersized_51660_iu", (target_336h_iu / 0.70) * 0.49),
        ("half_36900_iu", 36_900.0),
        ("undersized_26357_iu", (target_336h_iu / 0.70) * 0.25),
    ]
    dual_basis_h2o2: Dict[str, Any] = {}
    for basis_ieq in (1500.0, 2500.0):
        phi_b = (basis_ieq * VOL_1_IEQ_CM3) / 0.0350
        r_h2o2 = 2.0 * (0.45 * (phi_b * VMAX_ISLET_PURE_MOL_CM3_S * 1.15)) * 1e9
        basis_rows: Dict[str, Any] = {}
        for lbl_iu, iu_val in iu_sweep:
            src_3d = np.zeros((nx3, nx3, nz3), dtype=np.float64)
            src_3d[in_shell] = (r_h2o2 * (68.0 / 24.0)) / float(np.mean(in_shell))
            cat_200 = iu_val * 0.60
            lam_200 = cat_200 * (22_680.0 / 44_280.0)
            buf_200 = cat_200 - lam_200
            k_cat = np.full((nx3, nx3, nz3), 0.008, dtype=np.float64)
            k_cat[in_lam & (~in_core)] = (lam_200 / (7.0 * 1500.0)) / float(np.mean(in_lam & (~in_core)))
            k_cat[in_buf] = (buf_200 / (7.0 * 1500.0)) / float(np.mean(in_buf))
            h_res = _solve_3d_h2o2_strict_iip_0019(diff_3d, src_3d, k_cat, kp_3d, dx3, dx3, h_cm)
            h3 = h_res["h_3d"]
            cc_uM = float(np.max(h3[:, :, kp_idx]))
            iface_uM = float(np.max(0.5 * (h3[:, :, kp_idx - 1] + h3[:, :, kp_idx])))
            basis_rows[lbl_iu] = {
                "active_catalase_iu_333cm2": round(iu_val, 2),
                "gel_interface_z70p0um_max_h2o2_uM": round(iface_uM, 4),
                "gel_interface_z70p0um_unrounded_uM": iface_uM,
                "cell_center_z71p25um_max_h2o2_uM": round(cc_uM, 4),
                "cell_center_z71p25um_unrounded_uM": cc_uM,
                "passes_2p5uM_tier1_at_z70_interface": bool(iface_uM <= 2.5),
                "passes_5p0uM_tier2_at_z70_interface": bool(iface_uM <= 5.0),
            }
        dual_basis_h2o2[f"basis_{int(basis_ieq)}_ieq_cm2"] = basis_rows

    r_cl_100 = 0.5 * 100.0e-4
    r_cl_80 = 0.5 * 80.0e-4
    n_cl_100 = curative_ieq / (((4.0 / 3.0) * math.pi * (r_cl_100 ** 3)) / VOL_1_IEQ_CM3)
    n_cl_80 = curative_ieq / (((4.0 / 3.0) * math.pi * (r_cl_80 ** 3)) / VOL_1_IEQ_CM3)

    return {
        "contribution_id": "IIP-0025",
        "decision_id": "DEC-104",
        "offgrid_guardrail_id": "OFFGRID_GUARD_010_SPATIAL_MEAN_OXYGEN_MASKING_SINGLE_TIMEPOINT_CATALASE_SIZING_AND_PITCH_HELD_DOSE_HALVING",
        "graveyard_card_id": "GY-020",
        "zero_pre_wetlab_prior_inflation": True,
        "headline_single_design_and_portfolio_odds_unchanged": True,
        "primary_citations": [
            "PMID:21788575",
            "PMID:21693022",
            "PMID:35241836",
            "PMID:38993891",