scripts/verify_iip0056_route4_omentum_and_exvivo_rest.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0056_route4_omentum_and_exvivo_rest.py · SHA-256: 25e12d578ba80b55…
d_in = perm[bnd]
d_out = np.roll(perm, shift, axis=axis)[bnd]
p_in = p3[bnd]
p_out = np.roll(p3, shift, axis=axis)[bnd]
face_vals.append((d_in * p_in + d_out * p_out) / np.maximum(d_in + d_out, 1e-30))
if mask.shape[2] > 1:
bnd_up = mask[:, :, :-1] & (~mask[:, :, 1:])
if np.any(bnd_up):
d_in, d_out = perm[:, :, :-1][bnd_up], perm[:, :, 1:][bnd_up]
face_vals.append((d_in * p3[:, :, :-1][bnd_up] + d_out * p3[:, :, 1:][bnd_up]) / np.maximum(d_in + d_out, 1e-30))
bnd_dn = mask[:, :, 1:] & (~mask[:, :, :-1])
if np.any(bnd_dn):
d_in, d_out = perm[:, :, 1:][bnd_dn], perm[:, :, :-1][bnd_dn]
face_vals.append((d_in * p3[:, :, 1:][bnd_dn] + d_out * p3[:, :, :-1][bnd_dn]) / np.maximum(d_in + d_out, 1e-30))
all_faces = np.concatenate(face_vals)
return {
"fv_harmonic_face_min_mmhg": float(np.min(all_faces)),
"fv_harmonic_face_mean_mmhg": float(np.mean(all_faces)),
}
# =============================================================================
# 3. Geometry Solvers: Route 3P, R4-Omentum (Monolayer & Clinical Bilayer), R4-SARS-Free
# =============================================================================
def run_planar_route3p_case(
c: Dict[str, Any],
*,
area_cm2: float = 333.3,
h_slot_um: float = 180.0,
cluster_diam_um: float = 65.0,
bed_po2_mmhg: float = 35.0,
fibrin_um: float = 0.0,
fibrin_d_factor: float = 0.5,
ocr_mult: float = 1.097,
alpha_o2: float | None = None,
nx: int = 28,
nz: int = 32,
) -> Dict[str, Any]:
alpha_use = c["alpha_o2_nominal_mol_cm3_mmhg"] if alpha_o2 is None else alpha_o2
d_gel = c["d_o2_gel_chronic_cm2_s"]
d_islet = c["d_o2_islet_cm2_s"]
vmax_pure = c["vmax_islet_pure_mol_cm3_s"]
vol_ieq = c["vol_1_ieq_cm3"]
loading_ieq_cm2 = 500_000.0 / area_cm2
r_cm = 0.5 * cluster_diam_um * 1e-4
v_cl = (4.0 / 3.0) * math.pi * (r_cm ** 3)
bilayer = cluster_diam_um <= 80.0
n_layer = (0.5 if bilayer else 1.0) * (loading_ieq_cm2 * vol_ieq / v_cl)
pitch_cm = math.sqrt(1.0 / n_layer)
h_cm = h_slot_um * 1e-4
dx = pitch_cm / nx
dz = h_cm / nz
xs = (np.arange(nx) + 0.5) * dx
zs = (np.arange(nz) + 0.5) * dz - 0.5 * h_cm
X, Y, Z = np.meshgrid(xs, xs, zs, indexing="ij")
dv = dx * dx * dz
def pdist(a: np.ndarray, ctr: float) -> np.ndarray:
d = np.abs(a - ctr)
return np.minimum(d, pitch_cm - d)
in_any = np.zeros_like(X, dtype=bool)
vmax = np.zeros_like(X, dtype=np.float64)
masks: List[np.ndarray] = []
if bilayer:
z_c = 0.5 * h_cm - r_cm - 15.0e-4
centers = [(0.5 * pitch_cm, 0.5 * pitch_cm, +z_c), (0.0, 0.0, -z_c)]
else:
centers = [(0.5 * pitch_cm, 0.5 * pitch_cm, 0.0)]
for cx, cy, cz in centers:
msk = (pdist(X, cx) ** 2 + pdist(Y, cy) ** 2 + (Z - cz) ** 2) <= (r_cm ** 2)
v_vox = float(np.sum(msk)) * dv
in_any |= msk
vmax[msk] = (vmax_pure * ocr_mult) * (v_cl / v_vox)
masks.append(msk)
in_gel = ~in_any
phi_tot = (loading_ieq_cm2 * vol_ieq) / h_cm
vmax[in_gel] = (phi_tot * vmax_pure * c["stromal_ocr_fraction"] * ocr_mult) / float(np.mean(in_gel))
perm = np.where(in_any, d_islet * alpha_use, d_gel * alpha_use)
p_ext = d_gel * (fibrin_d_factor if fibrin_um > 0.0 else 1.0) * alpha_use
res = solve_3d_mm_oxygen_fvm(
perm, vmax, np.zeros_like(X), dx, dx, dz,
p_bed=bed_po2_mmhg, km_o2=c["km_o2_mmhg"], alpha_o2=alpha_use,
standoff_bot_cm=fibrin_um * 1e-4, standoff_top_cm=fibrin_um * 1e-4,
p_ext_bot=p_ext, p_ext_top=p_ext, top_adiabatic=False,
)
p3 = res["p_3d"]
worst_face = min(compute_exact_fv_interface_stats(p3, perm, m)["fv_harmonic_face_min_mmhg"] for m in masks)
worst_core = min(float(np.min(p3[m])) for m in masks)
return {
"grid": [nx, nx, nz],
"area_cm2": area_cm2,
"loading_ieq_cm2": round(loading_ieq_cm2, 2),
"pitch_um": round(pitch_cm * 1e4, 3),
"h_slot_um": h_slot_um,
"cluster_diam_um": cluster_diam_um,
"bed_po2_mmhg": bed_po2_mmhg,
"fibrin_um": fibrin_um,
"alpha_o2_mol_cm3_mmhg": alpha_use,
"worst_face_mmhg": round(worst_face, 4),
"worst_core_mmhg": round(worst_core, 4),
"passes_15_and_5": bool(worst_face >= 15.0 and worst_core >= 5.0),
"o2_rel_mass_balance_error": _fmt_sci_float(res["rel_mass_balance_error"]),
"o2_post_rel_residual": _fmt_sci_float(res["post_rel_residual"]),
}
def run_r4_monolayer_case(
c: Dict[str, Any],
*,
site_name: str,
cluster_diam_um: float,
pitch_um: float,
bed_po2_mmhg: float,
fibrin_um: float = 0.0,
fibrin_d_factor: float = 0.5,
d_matrix_cm2_s: float | None = None,
interstitial_margin_each_um: float = 15.0,
top_adiabatic: bool = False,
ocr_mult: float = 1.097,
alpha_o2: float | None = None,
nx: int = 24,
nz: int = 20,
) -> Dict[str, Any]:
alpha_use = c["alpha_o2_nominal_mol_cm3_mmhg"] if alpha_o2 is None else alpha_o2
d_mat = c["d_o2_gel_chronic_cm2_s"] if d_matrix_cm2_s is None else d_matrix_cm2_s
d_islet = c["d_o2_islet_cm2_s"]
vmax_pure = c["vmax_islet_pure_mol_cm3_s"]
vol_ieq = c["vol_1_ieq_cm3"]
r_cm = 0.5 * cluster_diam_um * 1e-4
v_cl = (4.0 / 3.0) * math.pi * (r_cm ** 3)
pitch_cm = pitch_um * 1e-4
h_um = cluster_diam_um + 2.0 * interstitial_margin_each_um
h_cm = h_um * 1e-4
dx = pitch_cm / nx
dz = h_cm / nz
xs = (np.arange(nx) + 0.5) * dx - 0.5 * pitch_cm
zs = (np.arange(nz) + 0.5) * dz - 0.5 * h_cm
X, Y, Z = np.meshgrid(xs, xs, zs, indexing="ij")
dv = dx * dx * dz
in_cl = (X ** 2 + Y ** 2 + Z ** 2) <= (r_cm ** 2)
v_vox = float(np.sum(in_cl)) * dv
require_cond(v_vox > 0.0, f"Empty cluster mask for {site_name}")
vmax = np.zeros_like(X, dtype=np.float64)
vmax[in_cl] = (vmax_pure * ocr_mult) * (v_cl / v_vox)
in_mat = ~in_cl
loading_ieq_cm2 = (v_cl / vol_ieq) / (pitch_cm * pitch_cm)
phi_tot = v_cl / (pitch_cm * pitch_cm * h_cm)
vmax[in_mat] = (phi_tot * vmax_pure * c["stromal_ocr_fraction"] * ocr_mult) / float(np.mean(in_mat))
perm = np.where(in_cl, d_islet * alpha_use, d_mat * alpha_use)
p_ext = c["d_o2_gel_chronic_cm2_s"] * (fibrin_d_factor if fibrin_um > 0.0 else 1.0) * alpha_use
res = solve_3d_mm_oxygen_fvm(
perm, vmax, np.zeros_like(X), dx, dx, dz,
p_bed=bed_po2_mmhg, km_o2=c["km_o2_mmhg"], alpha_o2=alpha_use,
standoff_bot_cm=fibrin_um * 1e-4, standoff_top_cm=fibrin_um * 1e-4,
p_ext_bot=p_ext, p_ext_top=p_ext, top_adiabatic=top_adiabatic,
)
p3 = res["p_3d"]
fv = compute_exact_fv_interface_stats(p3, perm, in_cl)
wf = fv["fv_harmonic_face_min_mmhg"]
wc = float(np.min(p3[in_cl]))
return {
"site": site_name,
"grid": [nx, nx, nz],
"cluster_diam_um": cluster_diam_um,
"pitch_um": round(pitch_um, 2),
"layer_thickness_um": h_um,
"areal_loading_ieq_cm2": round(loading_ieq_cm2, 2),
"bed_po2_mmhg": bed_po2_mmhg,
"fibrin_um": fibrin_um,
"d_matrix_cm2_s": d_mat,
"alpha_o2_mol_cm3_mmhg": alpha_use,
"top_adiabatic": top_adiabatic,
"worst_face_mmhg": round(wf, 4),
"worst_core_mmhg": round(wc, 4),
"passes_15_and_5": bool(wf >= 15.0 and wc >= 5.0),
"o2_rel_mass_balance_error": _fmt_sci_float(res["rel_mass_balance_error"]),
"o2_post_rel_residual": _fmt_sci_float(res["post_rel_residual"]),
}
def run_r4_clinical_bilayer_case(
c: Dict[str, Any],
*,
site_name: str,
loading_ieq_cm2: float,
h_um: float = 120.0,
cluster_diam_um: float = 65.0,
bed_po2_mmhg: float = 40.0,
bed_po2_bot_mmhg: float | None = None,
bed_po2_top_mmhg: float | None = None,
fibrin_um: float = 0.0,
d_matrix_cm2_s: float | None = None,
top_adiabatic: bool = False,
ocr_mult: float = 1.097,
alpha_o2: float | None = None,
z_c_um: float | None = None,
nx: int = 20,
nz: int = 20,
) -> Dict[str, Any]:
"""Exact 3D FVM of envelope-free clinical staggered bilayer (combined areal loading over both planes)."""
alpha_use = c["alpha_o2_nominal_mol_cm3_mmhg"] if alpha_o2 is None else alpha_o2
d_gel = c["d_o2_gel_chronic_cm2_s"] if d_matrix_cm2_s is None else d_matrix_cm2_s
d_islet = c["d_o2_islet_cm2_s"]
vol_ieq = c["vol_1_ieq_cm3"]
r_cm = 0.5 * cluster_diam_um * 1e-4
v_cl = (4.0 / 3.0) * math.pi * (r_cm ** 3)
n_layer = 0.5 * (loading_ieq_cm2 * vol_ieq / v_cl)
pitch_cm = math.sqrt(1.0 / n_layer)
h_cm = h_um * 1e-4
z_c = (0.5 * h_cm - r_cm - 5.0e-4) if z_c_um is None else (float(z_c_um) * 1e-4)
z_c_used_um = z_c * 1e4
wall_clearance_um = 0.5 * h_um - (z_c_used_um + 0.5 * cluster_diam_um)
min_dist = math.sqrt(0.5 * (pitch_cm ** 2) + (2.0 * z_c) ** 2)
require_cond(
pitch_cm > 2.0 * r_cm and min_dist > 2.0 * r_cm and wall_clearance_um >= -1e-9,
f"Steric or slab-protrusion overlap at {loading_ieq_cm2} IEQ/cm^2 (pitch={pitch_cm*1e4:.2f}, min_dist={min_dist*1e4:.2f}, wall_clear={wall_clearance_um:.2f})",
)
pb_bot = bed_po2_mmhg if bed_po2_bot_mmhg is None else float(bed_po2_bot_mmhg)
pb_top = bed_po2_mmhg if bed_po2_top_mmhg is None else float(bed_po2_top_mmhg)
dx = pitch_cm / nx
dz = h_cm / nz
xs = (np.arange(nx) + 0.5) * dx
zs = (np.arange(nz) + 0.5) * dz - 0.5 * h_cm
X, Y, Z = np.meshgrid(xs, xs, zs, indexing="ij")
dv = dx * dx * dz
def pdist(a: np.ndarray, ctr: float) -> np.ndarray:
d = np.abs(a - ctr)
return np.minimum(d, pitch_cm - d)
in_any = np.zeros_like(X, dtype=bool)
vmax = np.zeros_like(X, dtype=np.float64)
masks: List[np.ndarray] = []
for cx, cy, cz in [(0.5 * pitch_cm, 0.5 * pitch_cm, +z_c), (0.0, 0.0, -z_c)]:
msk = (pdist(X, cx) ** 2 + pdist(Y, cy) ** 2 + (Z - cz) ** 2) <= (r_cm ** 2)
in_any |= msk
vmax[msk] = (c["vmax_islet_pure_mol_cm3_s"] * ocr_mult) * (v_cl / (float(np.sum(msk)) * dv))
masks.append(msk)
in_gel = ~in_any