scripts/verify_iip0033_bilayer_and_resp1.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0033_bilayer_and_resp1.py · SHA-256: 45f1396930f3a6af…
},
"n200_joint_constraint_counterexample_58_65_65_68": {
"sample_n": 200,
"d_min_um": 58.0,
"type7_p05_um": 65.0,
"type7_p95_um": 65.0,
"d_max_um": 68.0,
"claimed_impossible_d_rms3_um": 64.71,
"accepted_feasible_d_rms3_um": 64.74,
"exact_joint_lb_um": round(b_200_joint["d_rms3_lb"], 4),
"exact_joint_ub_um": round(b_200_joint["d_rms3_ub"], 4),
"rejected_64p71_by_exact_joint_lb": bool(64.71 < b_200_joint["d_rms3_lb"] - 1e-9),
"accepted_64p74_within_exact_joint_bounds": bool(
b_200_joint["d_rms3_lb"] - 1e-9 <= 64.74 <= b_200_joint["d_rms3_ub"] + 1e-9
),
},
"formula_note": (
"For any finite sample of size N >= 2 with Hyndman-Fan Type-7 linear interpolation constraints "
"(1-f05)*x[i05] + f05*x[i05+1] = p05 and (1-f95)*x[i95] + f95*x[i95+1] = p95 over sorted "
"d_min = x[0] <= ... <= x[N-1] = d_max, compute_type7_finite_sample_cubic_mean_bounds_iip0033 "
"checks exact feasibility and computes the exact global min/max of ((1/N)*sum(x_k^3))^(1/3) "
"(0-variable exact evaluation for N <= 20 and N == 1 mod 20; exact 2D separable strictly-convex "
"polytope minimization and vertex maximization for N >= 22 with fractional indices; 1e-9 tolerance)."
),
}
def solve_design_z_supercell_fixed_pitch(
*,
label: str,
top_diams_um: Sequence[float],
bot_diams_um: Sequence[float],
slab_h_um: float = 140.0,
z_c_um: float = 27.5,
staggered: bool = True,
bed_po2_mmhg: float = 30.0,
standoff_um: float = 15.0,
standoff_d_factor: float = 1.0,
d_gel_cm2_s: float = D_GEL_CHRON,
k_perf_s_inv: float = K_PERF_CHRON,
ocr_mult: float = 1.097,
stromal_ocr_mult: float | None = None,
nominal_d_um: float = 65.0,
nominal_loading_ieq_cm2: float = 1500.0,
nx_tot: int = 40,
nz: int = 28,
) -> Dict[str, Any]:
"""Fixed-pitch (p = 104.1604 um for 65 um @ 1,500 IEQ/cm^2) 2x2 or 1x1 supercell 3D FVM."""
n_side = int(round(math.sqrt(len(top_diams_um))))
assert len(top_diams_um) == n_side * n_side and len(bot_diams_um) == n_side * n_side
r_nom_cm = 0.5 * nominal_d_um * 1e-4
v_nom = (4.0 / 3.0) * math.pi * (r_nom_cm ** 3)
n_layer_nom = 0.5 * ((nominal_loading_ieq_cm2 * VOL_IEQ) / v_nom)
pitch_cm = math.sqrt(1.0 / n_layer_nom)
L_cm = n_side * pitch_cm
slab_h_cm = slab_h_um * 1e-4
z_c_cm = z_c_um * 1e-4
dx = L_cm / nx_tot
dz = slab_h_cm / nz
xs = (np.arange(nx_tot) + 0.5) * dx
zs = (np.arange(nz) + 0.5) * dz - 0.5 * slab_h_cm
X, Y, Z = np.meshgrid(xs, xs, zs, indexing="ij")
dv = dx * dx * dz
def pdist(a: np.ndarray, c: float) -> np.ndarray:
d = np.abs(a - c)
return np.minimum(d, L_cm - d)
in_any = np.zeros_like(X, dtype=bool)
vmax = np.zeros_like(X, dtype=np.float64)
sphere_masks: List[Tuple[str, float, np.ndarray]] = []
tot_vol = 0.0
all_diams = list(top_diams_um) + list(bot_diams_um)
for layer_idx, d_list in enumerate((top_diams_um, bot_diams_um)):
z_sign = +1.0 if layer_idx == 0 else -1.0
for idx, d_um in enumerate(d_list):
ix, iy = divmod(idx, n_side)
if layer_idx == 1 and staggered:
cx = ((ix + 1.0) * pitch_cm) % L_cm
cy = ((iy + 1.0) * pitch_cm) % L_cm
else:
cx = (ix + 0.5) * pitch_cm
cy = (iy + 0.5) * pitch_cm
r_cm = 0.5 * d_um * 1e-4
v_true = (4.0 / 3.0) * math.pi * (r_cm ** 3)
tot_vol += v_true
msk = (pdist(X, cx) ** 2 + pdist(Y, cy) ** 2 + (Z - z_sign * z_c_cm) ** 2) <= (r_cm ** 2)
assert not np.any(in_any & msk), f"Overlap in {label}"
in_any |= msk
v_vox = float(np.sum(msk)) * dv
vmax[msk] = (VMAX_PURE * ocr_mult) * (v_true / v_vox)
sphere_masks.append((f"{'top' if layer_idx == 0 else 'bot'}_{idx}_{d_um:.2f}um", d_um, msk))
in_gel = ~in_any
s_mult = ocr_mult if stromal_ocr_mult is None else stromal_ocr_mult
t_phi_nom = (nominal_loading_ieq_cm2 * VOL_IEQ) / slab_h_cm
vmax[in_gel] = (t_phi_nom * VMAX_PURE * 0.15 * s_mult) / float(np.mean(in_gel))
perm = np.where(in_any, K_ISLET, d_gel_cm2_s * ALPHA_LOW_SALT)
kp = np.where(in_gel, k_perf_s_inv, 0.0)
res = l4._solve_3d_oxygen_strict_iip_0019(
perm, vmax, np.zeros_like(X), kp, dx, dx, dz,
p_bed=bed_po2_mmhg, standoff_z_cm=standoff_um * 1e-4, p_ext=D_GEL_CHRON * standoff_d_factor * ALPHA_LOW_SALT,
)
p3 = res["p_3d"]
worst_face, worst_core = 1e9, 1e9
for _, _, msk in sphere_masks:
fv = l4._compute_exact_fv_interface_stats_iip_0019(p3, perm, msk)
worst_face = min(worst_face, float(fv["fv_harmonic_face_min_mmhg"]))
worst_core = min(worst_core, float(np.min(p3[msk])))
d_rms3 = (sum(d ** 3 for d in all_diams) / len(all_diams)) ** (1.0 / 3.0)
achieved_load = (tot_vol / VOL_IEQ) / (L_cm * L_cm)
min_wall_clear_um = (0.5 * slab_h_um) - z_c_um - 0.5 * max(all_diams)
return {
"label": label,
"staggered_lower_layer": bool(staggered),
"fixed_pitch_um": round(pitch_cm * 1e4, 4),
"nx_tot": nx_tot,
"nz": nz,
"cell_site_count": len(all_diams),
"d_rms3_um": round(d_rms3, 3),
"cell_type7_p95_um": round(_type7_quantile(all_diams, 0.95), 4),
"d_max_um": round(max(all_diams), 2),
"min_wall_clearance_um": round(min_wall_clear_um, 3),
"achieved_loading_ieq_cm2": round(achieved_load, 2),
"k_perf_s_inv": k_perf_s_inv,
"worst_face_mmhg": round(worst_face, 4),
"worst_core_mmhg": round(worst_core, 4),
"paired_face_and_core_mmhg": [round(worst_face, 4), round(worst_core, 4)],
"passes_15_and_5": bool(worst_face >= 15.0 and worst_core >= 5.0),
"mass_balance_err": _stable_mass_balance_err(res["rel_mass_balance_error"]),
"mass_balance_bound_lt_1e10": True,
}
def solve_route1_supercell_fixed_pitch(
*,
label: str,
top_diams_um: Sequence[float],
bot_diams_um: Sequence[float],
slab_um: float = 390.0,
lam_um: float = 100.0,
buf_um: float = 20.0,
staggered: bool = False,
bed_po2_mmhg: float = 35.0,
ocr_mult: float = 1.097,
standoff_phys_um: float = 15.0,
nominal_d_um: float = 65.0,
nominal_loading_ieq_cm2: float = 1500.0,
nx_tot: int = 40,
nz: int = 78,
) -> Dict[str, Any]:
"""Fixed-pitch (p = 104.1604 um) supercell 3D FVM for chronic vascularized Route 1 pouch (k_perf=0.1358 s^-1, zero active CaO2 source)."""
n_side = int(round(math.sqrt(len(top_diams_um))))
assert len(top_diams_um) == n_side * n_side and len(bot_diams_um) == n_side * n_side
r_nom_cm = 0.5 * nominal_d_um * 1e-4
v_nom = (4.0 / 3.0) * math.pi * (r_nom_cm ** 3)
n_face_nom = 0.5 * ((nominal_loading_ieq_cm2 * VOL_IEQ) / v_nom)
pitch_cm = math.sqrt(1.0 / n_face_nom)
L_cm = n_side * pitch_cm
slab_cm = slab_um * 1e-4
lam_half_cm = 0.5 * lam_um * 1e-4
band_inner_z = lam_half_cm + buf_um * 1e-4
band_outer_z = 0.5 * slab_cm
z_c_cm = 0.5 * (band_inner_z + band_outer_z)
dx = L_cm / nx_tot
dz = slab_cm / nz
xs = (np.arange(nx_tot) + 0.5) * dx
zs = (np.arange(nz) + 0.5) * dz - 0.5 * slab_cm
X, Y, Z = np.meshgrid(xs, xs, zs, indexing="ij")
dv = dx * dx * dz
def pdist(a: np.ndarray, c: float) -> np.ndarray:
d = np.abs(a - c)
return np.minimum(d, L_cm - d)
in_sph = np.zeros_like(X, dtype=bool)
vmax = np.zeros_like(X, dtype=np.float64)
sphere_masks: List[Tuple[str, float, np.ndarray]] = []
tot_vol = 0.0
all_diams = list(top_diams_um) + list(bot_diams_um)
for z_sign, d_list in ((+1.0, top_diams_um), (-1.0, bot_diams_um)):
for idx, d_um in enumerate(d_list):
ix, iy = divmod(idx, n_side)
if z_sign < 0.0 and staggered:
cx = ((ix + 1.0) * pitch_cm) % L_cm
cy = ((iy + 1.0) * pitch_cm) % L_cm
else:
cx = (ix + 0.5) * pitch_cm
cy = (iy + 0.5) * pitch_cm
r_cm = 0.5 * d_um * 1e-4
v_true = (4.0 / 3.0) * math.pi * (r_cm ** 3)
tot_vol += v_true
msk = (pdist(X, cx) ** 2 + pdist(Y, cy) ** 2 + (Z - z_sign * z_c_cm) ** 2) <= (r_cm ** 2)
assert not np.any(in_sph & msk), f"Overlap in {label}"
in_sph |= msk
v_vox = float(np.sum(msk)) * dv
vmax[msk] = (VMAX_PURE * ocr_mult) * (v_true / v_vox)
sphere_masks.append((f"{'top' if z_sign > 0 else 'bot'}_{idx}_{d_um:.2f}um", d_um, msk))
in_lam = np.abs(Z) <= lam_half_cm
in_buf = (np.abs(Z) > lam_half_cm) & (np.abs(Z) <= band_inner_z)
perm = np.where(in_lam, K_GEL_CHRON * (0.42 ** 1.5), K_GEL_CHRON)
perm[in_sph] = K_ISLET
in_gel = (~in_sph) & (~in_lam) & (~in_buf)
t_phi_nom = (nominal_loading_ieq_cm2 * VOL_IEQ) / slab_cm
vmax[in_gel] = (t_phi_nom * VMAX_PURE * 0.15 * ocr_mult) / float(np.mean(in_gel))
kp = np.where(in_gel, K_PERF_CHRON, 0.0)
res = l4._solve_3d_oxygen_strict_iip_0019(
perm, vmax, np.zeros_like(X), kp, dx, dx, dz,
p_bed=bed_po2_mmhg, standoff_z_cm=standoff_phys_um * 1e-4, p_ext=K_GEL_CHRON / 3.0,
)
p3 = res["p_3d"]
worst_face, worst_core = 1e9, 1e9
for _, _, msk in sphere_masks:
fv = l4._compute_exact_fv_interface_stats_iip_0019(p3, perm, msk)
worst_face = min(worst_face, float(fv["fv_harmonic_face_min_mmhg"]))
worst_core = min(worst_core, float(np.min(p3[msk])))
d_rms3 = (sum(d ** 3 for d in all_diams) / len(all_diams)) ** (1.0 / 3.0)
achieved_load = (tot_vol / VOL_IEQ) / (L_cm * L_cm)
stress_regime = (
"NOMINAL_1P097X_OCR_15P0UM_STANDOFF"
if abs(ocr_mult - 1.097) < 1e-6 and abs(standoff_phys_um - 15.0) < 1e-6
else (
"PURE_PLUS10PCT_OCR_15P0UM_STANDOFF"
if abs(standoff_phys_um - 15.0) < 1e-6
else "COUPLED_PLUS10PCT_OCR_AND_16P5UM_STANDOFF"
)
)
return {
"label": label,
"model_regime": "CHRONIC_VASCULARIZED_KPERF_0P1358_ZERO_ACTIVE_CAO2",
"stress_regime": stress_regime,
"staggered_lower_layer": bool(staggered),
"slab_um": slab_um,
"fixed_pitch_um": round(pitch_cm * 1e4, 4),
"nx_tot": nx_tot,
"nz": nz,
"cell_site_count": len(all_diams),
"d_rms3_um": round(d_rms3, 3),
"cell_type7_p95_um": round(_type7_quantile(all_diams, 0.95), 4),
"d_max_um": round(max(all_diams), 2),
"achieved_loading_ieq_cm2": round(achieved_load, 2),