scripts/run_layer4_tissue_and_metabolic_twin.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/run_layer4_tissue_and_metabolic_twin.py · SHA-256: 624a491d5e418a7d…
y_floor = (p_floor_mmhg ** hill_n) / ((p50_mmhg ** hill_n) + (p_floor_mmhg ** hill_n))
released_umol = total_o2_umol * max(0.0, y_start - y_floor)
phi_ref = (2500.0 * VOL_1_IEQ_CM3) / 0.0350
graft_vmax_vasc_umol_min = (phi_ref * VMAX_ISLET_PURE_MOL_CM3_S * 1.15 * 7.00) * 60.0 * 1e6
duration_s = (released_umol / graft_vmax_vasc_umol_min) * 60.0
return {
"carrier_mass_mg": round(mass_g * 1e3, 3),
"total_heme_capacity_umol_o2": round(total_o2_umol, 5),
"p_start_mmhg": float(p_start_mmhg),
"p_floor_mmhg": float(p_floor_mmhg),
"p50_mmhg": float(p50_mmhg),
"hill_n": float(hill_n),
"bound_fraction_at_start": round(y_start, 6),
"bound_fraction_at_floor": round(y_floor, 6),
"releasable_umol_above_floor": round(released_umol, 5),
"graft_500k_ieq_plus_15pct_vasc_demand_umol_min": round(graft_vmax_vasc_umol_min, 6),
"standalone_bridge_duration_above_floor_s": round(duration_s, 4),
}
def compute_design_z_140um_parameterized_case(
*,
bed_po2_mmhg: float,
ocr_multiplier: float,
d_gel_cm2_s: float,
k_perf_s_inv: float,
pfc_vol_fraction: float = 0.0,
areal_loading_ieq_cm2: float = 1500.0,
cluster_diam_um: float = 100.0,
standoff_um: float = 15.0,
standoff_ext_diffusivity_factor: float = 1.0,
stromal_ocr_fraction: float = 0.15,
nx_override: int = 48,
nz_override: int = 28,
) -> Dict[str, Any]:
"""Executable parameterized 3D FVM solver for Design Z 140 um Zero-CaO2 cases gated on unrounded min-face pO2 (IIP-0021; DEC-100; IIP-0025; DEC-104)."""
r_cm = 0.5 * float(cluster_diam_um) * 1e-4
v_cl = (4.0 / 3.0) * math.pi * (r_cm ** 3)
n_mono = (float(areal_loading_ieq_cm2) * VOL_1_IEQ_CM3) / v_cl
pitch_cm = math.sqrt(1.0 / n_mono)
slab_h_cm = 0.0140
nx, ny, nz = int(nx_override), int(nx_override), int(nz_override)
dx = pitch_cm / nx
dy = pitch_cm / ny
dz = slab_h_cm / nz
xs = (np.arange(nx) + 0.5) * dx - 0.5 * pitch_cm
ys = (np.arange(ny) + 0.5) * dy - 0.5 * pitch_cm
zs = (np.arange(nz) + 0.5) * dz - 0.5 * slab_h_cm
X, Y, Z = np.meshgrid(xs, ys, zs, indexing="ij")
R = np.sqrt(X ** 2 + Y ** 2 + Z ** 2)
in_cl = (X ** 2 + Y ** 2 + Z ** 2) <= (r_cm ** 2)
mg_factor = 1.0
if pfc_vol_fraction > 0.0:
kp_ratio = 20.0
phi_p = float(pfc_vol_fraction)
mg_factor = (kp_ratio + 2.0 + 2.0 * phi_p * (kp_ratio - 1.0)) / (kp_ratio + 2.0 - phi_p * (kp_ratio - 1.0))
p_krogh_gel = float(d_gel_cm2_s) * mg_factor * ALPHA_BUNSEN_MOL_CM3_MMHG
p_krogh_tissue = D_O2_ISLET_TISSUE_CM2_S * ALPHA_BUNSEN_MOL_CM3_MMHG
p_krogh_chronic_ext = 1.50e-5 * float(standoff_ext_diffusivity_factor) * ALPHA_BUNSEN_MOL_CM3_MMHG
perm = np.where(in_cl, p_krogh_tissue, p_krogh_gel)
t_phi = (float(areal_loading_ieq_cm2) * VOL_1_IEQ_CM3) / slab_h_cm
in_gel = ~in_cl
vmax = np.zeros_like(X)
vmax[in_cl] = (VMAX_ISLET_PURE_MOL_CM3_S * float(ocr_multiplier)) * (t_phi / float(np.mean(in_cl)))
vmax[in_gel] = (t_phi * VMAX_ISLET_PURE_MOL_CM3_S * float(stromal_ocr_fraction) * float(ocr_multiplier)) / float(np.mean(in_gel))
src = np.zeros_like(X)
kp = np.where(in_gel, float(k_perf_s_inv), 0.0)
fvm_res = _solve_3d_oxygen_strict_iip_0019(
perm, vmax, src, kp, dx, dy, dz,
p_bed=float(bed_po2_mmhg),
standoff_z_cm=float(standoff_um) * 1e-4,
p_ext=p_krogh_chronic_ext,
)
p3 = fvm_res["p_3d"]
fv_z = _compute_exact_fv_interface_stats_iip_0019(p3, perm, in_cl)
fv_min_raw = float(fv_z["fv_harmonic_face_min_mmhg"])
shell_mask = (R >= 0.95 * r_cm) & (R <= 1.05 * r_cm)
if_po2 = float(np.mean(p3[shell_mask]))
core_po2 = float(np.min(p3[in_cl]))
passes_unrounded_min_face = bool(fv_min_raw >= 15.0 and core_po2 >= 5.0)
return {
"mesh": f"{nx}x{ny}x{nz}",
"bed_po2_mmhg": float(bed_po2_mmhg),
"cluster_diam_um": float(cluster_diam_um),
"ocr_multiplier": float(ocr_multiplier),
"stromal_ocr_fraction": float(stromal_ocr_fraction),
"d_gel_cm2_s": float(d_gel_cm2_s),
"k_perf_s_inv": float(k_perf_s_inv),
"standoff_ext_diffusivity_factor": round(float(standoff_ext_diffusivity_factor), 6),
"pfc_vol_fraction": float(pfc_vol_fraction),
"maxwell_garnett_factor": round(mg_factor, 7),
"pericellular_interface_po2_mmhg": round(if_po2, 4),
"pericellular_shell_mean_po2_unrounded_mmhg": if_po2,
"fv_harmonic_face_min_mmhg": round(fv_min_raw, 4),
"fv_harmonic_face_min_unrounded_mmhg": fv_min_raw,
"min_core_po2_mmhg": round(core_po2, 4),
"min_core_po2_unrounded_mmhg": core_po2,
"passes_shell_mean_diagnostic_15_and_5": bool(if_po2 >= 15.0 and core_po2 >= 5.0),
"passes_unrounded_min_face_iip_0025": passes_unrounded_min_face,
"passes_15mmhg_interface_and_5mmhg_core_gate": passes_unrounded_min_face,
}
def compute_early_washout_and_catalase_derating_checks() -> Dict[str, Any]:
"""Executable parameterized replay of 48-h early washout in derated 462.6 mg Design X pouch and 2.5 um discrete-bead catalase sweeps (IIP-0021; DEC-100)."""
# 1. Early 48-h washout in a 41.55% CaO2-derated (462.6 mg) Design X (a_frac = 0.34) pouch under t_50 = 60 h
inv_scale = 462.6 / (10.9811487 * 72.078)
a_frac = 0.34
nz = 51
half_L = 0.0175
geometry = _slab_control_volume_profiles(nz, half_L, lamina_half_width_fraction=a_frac)
z_grid = geometry["z"]
dz = float(z_grid[1] - z_grid[0])
s_prof = geometry["central_source"]
central_mask = geometry["central_mask"]
d_prof_int = geometry["outer_demand"][1:-1]
phi_2500 = (2500.0 * VOL_1_IEQ_CM3) / (2.0 * half_L)
local_phi_islet = phi_2500 / (1.0 - a_frac)
d_o2_faces = _slab_oxygen_face_diffusivities(z_grid, 0.42 ** 1.5, lamina_half_width_fraction=a_frac, d_o2_hydrogel_override_cm2_s=None)
k_ref_s = math.log(2.0) / (68.0 * 3600.0)
r_o2_0_mol_s = (0.45 * inv_scale) * (phi_2500 * VMAX_ISLET_PURE_MOL_CM3_S * 1.15) * 7.00
n_cao2_initial_mmol = (2.0 * r_o2_0_mol_s / k_ref_s) * 1e3
t_grid = [float(h) for h in range(337)]
def perf_ode(t_h: float, y: np.ndarray) -> np.ndarray:
exp_arg = -(t_h - 60.0) / 8.0
targ = 0.0 if exp_arg > 700.0 else 0.97 / (1.0 + math.exp(exp_arg))
return np.array([(targ - y[0]) / 3.0])
perf_y = rk4_integrate(perf_ode, (0.0, 336.0), np.array([0.01]), t_grid, dt_max=0.25)[0]
washout_48h_cases: Dict[str, Any] = {}
for law in ("first_order", "ginstling_brounshtein"):
rem_vec = _cao2_inventory_fractions(np.asarray(t_grid), 68.0, law)
k_hyd_s = math.log(2.0) / (68.0 * 3600.0)
k_gb_h = (1.0 - (2.0 / 3.0) * 0.5 - (0.5 ** (2.0 / 3.0))) / 68.0
min_post_wash_slab = math.inf
for idx, t_h in enumerate(t_grid):
endo_mult = 0.65 if t_h <= 48.0 else 1.0
v_dem = (phi_2500 * VMAX_ISLET_PURE_MOL_CM3_S * (1.15 * endo_mult)) / ALPHA_BUNSEN_MOL_CM3_MMHG
m_rem = max(0.0, float(rem_vec[idx]))
if law == "ginstling_brounshtein":
denom_gb = max(0.05, (max(1e-9, m_rem) ** (-1.0 / 3.0)) - 1.0) if m_rem > 0.0 else math.inf
rate_s = (1.5 * (k_gb_h / 3600.0) / denom_gb) if math.isfinite(denom_gb) else 0.0
o2_gen = 0.5 * rate_s * (n_cao2_initial_mmol * 1e-3) / 7.00
else:
o2_gen = 0.5 * k_hyd_s * (n_cao2_initial_mmol * m_rem * 1e-3) / 7.00
s_h2o2 = 2.0 * o2_gen * 1e9 * s_prof
cat_frac = math.exp(-math.log(2.0) * t_h / 168.0)
k1_prof = 0.008 + (18.0 * cat_frac - 0.008) * central_mask[1:-1]
kp_s = 0.14 * float(perf_y[idx])
perox = _solve_peroxide_slab_bvp(s_h2o2[1:-1], dz, k1_prof, kp_s, km_uM=25000.0)
q_src = (0.5 * perox["decomposition_uM_s"] * 1e-9) / ALPHA_BUNSEN_MOL_CM3_MMHG
p = np.full(nz, 40.0, dtype=np.float64)
dl = (d_o2_faces[:-1] / (dz * dz)).copy()
dr = (d_o2_faces[1:] / (dz * dz)).copy()
for _ in range(80):
pi = p[1:-1]
diff_t = dl * (p[:-2] - pi) + dr * (p[2:] - pi)
cons = v_dem * d_prof_int * (pi / (KM_O2_MMHG + pi))
res = diff_t - cons + q_src + kp_s * (40.0 - pi)
jdiag = -(dl + dr) - v_dem * d_prof_int * (KM_O2_MMHG / ((KM_O2_MMHG + pi) ** 2)) - kp_s
mat = np.diag(jdiag) + np.diag(dr[:-1], 1) + np.diag(dl[1:], -1)
step = np.linalg.solve(mat, -res)
p[1:-1] = np.maximum(0.0, pi + 0.8 * step)
if float(np.max(np.abs(step))) < 1e-6:
break
if t_h > 48.0:
min_post_wash_slab = min(min_post_wash_slab, float(np.min(p)))
bvp_100 = solve_spherical_cluster_po2_bvp(
min_post_wash_slab, 0.0050, phi_islet_in_slab=local_phi_islet, ocr_multiplier=1.0
)
washout_48h_cases[law] = {
"derated_cao2_mass_mg": 462.6,
"inosculation_t50_h": 60.0,
"washout_hour": 48.0,
"post_washout_min_slab_po2_mmhg": round(min_post_wash_slab, 4),
"post_washout_100um_gel_only_surface_po2_mmhg": round(float(bvp_100["wigner_seitz_two_zone_fv_gel_only_supply_surface_po2_unrounded_mmhg"]), 4),
"post_washout_100um_gel_only_core_po2_mmhg": round(float(bvp_100["wigner_seitz_two_zone_fv_gel_only_supply_core_po2_unrounded_mmhg"]), 4),
}
# 2. Parameterized 2.5 um discrete edge-bead H2O2 replay for 73,800 IU retained (~105,429 IU post-cure) vs 37,800 IU split vs 56.3% bead-number-derated catalase
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
r_o2_0_base = 0.45 * (phi_2500 * VMAX_ISLET_PURE_MOL_CM3_S * 1.15)
r_h2o2_0_uM_s = 2.0 * r_o2_0_base * 1e9
catalase_cases: Dict[str, Any] = {}
for cid, inv_sc, cat_iu_333, bead_density_derated in (
("Retained_73800IU_105429IU_PostCure_t24_FullCaO2", 1.0, 73800.0, False),
("Split_37800IU_ZeroAddedEnzyme_t24_FullCaO2", 1.0, 37800.0, False),
("BeadDensityDerated_56p3pct_41550IU_t24", 0.563, 73800.0 * 0.563, True),
):
solid_frac = 0.58 * (inv_sc if bead_density_derated else 1.0)
side_cm = max(math.sqrt(v_b / (solid_frac * 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))
src_3d = np.zeros((nx3, nx3, nz3), dtype=np.float64)
src_3d[in_shell] = ((r_h2o2_0_uM_s * inv_sc) * (68.0 / 24.0)) / float(np.mean(in_shell))
diff_3d = np.where(in_core, 0.0, D_H2O2_HYDROGEL_CM2_S)
cat_iu_200 = cat_iu_333 * (200.0 / 333.3333333333333)
lam_iu_200 = cat_iu_200 * (22680.0 / 44280.0) if cat_iu_333 > 40000.0 else cat_iu_200 * 0.5
buf_iu_200 = cat_iu_200 - lam_iu_200
k_cat = np.full((nx3, nx3, nz3), 0.008, dtype=np.float64)
k_cat[in_lam & (~in_core)] = (lam_iu_200 / (7.0 * 1500.0)) / float(np.mean(in_lam & (~in_core)))
k_cat[in_buf] = (buf_iu_200 / (7.0 * 1500.0)) / float(np.mean(in_buf))
kp_3d = np.where(in_lam | in_buf, 0.0, 0.14 * 0.01)
sol_h = _solve_3d_h2o2_strict_iip_0019(diff_3d, src_3d, k_cat, kp_3d, dx3, dx3, h_cm)
h3 = sol_h["h_3d"]
kp_idx = int(np.argmin(np.abs(zs3 - 0.0070)))
pk = float(np.max(h3[:, :, kp_idx]))
catalase_cases[cid] = {
"retained_active_catalase_iu_333cm2": round(cat_iu_333, 1),
"post_cure_t0_active_catalase_iu_at_70pct_day7_retention": round(cat_iu_333 / 0.70, 1),
"islet_plane_24h_burst_max_h2o2_uM": round(pk, 4),
"passes_2p5uM_tier1_target": bool(pk <= 2.5),
}
return {
"early_48h_washout_derated_462p6mg_t50_60h_cases": washout_48h_cases,
"parameterized_2p5um_discrete_bead_catalase_cases": catalase_cases,
}
def build_lineage_partitioned_metabolic_rest_and_m101_exclusion_audit_iip_0021() -> Dict[str, Any]:
"""Build reproducible Layer-4 lineage-partitioned OCR, Design Z post-washout, staged exercise lag, and M101 reservoir audit (IIP-0021; DEC-100)."""
# 1. Alias vs two-scale lineage-partitioned verification
legacy_alias_070 = _run_single_pouch_pde_regime(
"AUDIT_LEGACY_ALIAS_0P70", 0.45, "central", 68.0, True, 0.97,
demand_multiplier_extra=0.70, _assess_numerical_gate=False,
)
uniform_ocr_070 = _run_single_pouch_pde_regime(
"AUDIT_UNIFORM_OCR_0P70", 0.45, "central", 68.0, True, 0.97,
areal_loading_ieq_cm2=2500.0, cell_intrinsic_ocr_multiplier=0.70, _assess_numerical_gate=False,
)
two_scale_endo055_vasc100 = run_lineage_partitioned_intrinsic_ocr_regime(
regime_id="AUDIT_TWO_SCALE_ENDO0P55_VASC1P00",
areal_loading_ieq_cm2=2500.0,
endocrine_intrinsic_ocr_multiplier=0.55,
vascular_intrinsic_ocr_multiplier=1.00,
)
two_scale_endo150_vasc100 = run_lineage_partitioned_intrinsic_ocr_regime(