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…
def solve_spherical_cluster_po2_bvp(
surface_po2_mmhg: float,
radius_cm: float,
nr: int = 31,
phi_islet_in_slab: float = 0.1262247,
d_hydrogel_override: float | None = None,
ocr_multiplier: float = 1.0,
) -> Dict[str, Any]:
"""
Solve the nonlinear spherical Michaelis-Menten reaction-diffusion BVP inside an islet cluster:
D_islet * (d2p/dr2 + (2/r) * dp/dr) - (V_max / alpha) * p / (Km + p) = 0
under:
1. Homogenized-slab Dirichlet upper-bound surface boundary p(R_c) = surface_po2_mmhg (AUDIT-REF),
2. Coupled two-zone finite-volume (FV) Wigner-Seitz spherical unit cell r in [0, R_ws] with outer radius
R_ws = R_c * phi_islet^(-1/3) (119.6106 um for R_c = 60 um at phi_islet = 0.1262247), interfacial
flux continuity D_islet * dp/dr(R_c-) = D_hydrogel * dp/dr(R_c+), no-flux at R_ws, and cell-volume
mean <p>_ws = surface_po2_mmhg under BOTH uniform supply ('uniform', surface 13.62 / core 4.08 mmHg)
and conservative extracellular hydrogel-only supply ('gel_only', surface 12.78 / core 2.15 mmHg), and
3. Analytical Ewald shell diagnostic (f_ewald = 1 - 1.8*phi^(1/3) + phi - 0.2*phi^2 = 0.2201084).
Directly closes AUDIT-REF, AUDIT-REF, and AUDIT-REF.
"""
if not isinstance(nr, int) or isinstance(nr, bool) or nr < 3:
raise ValueError("nr must be an integer >= 3")
# Validate even the zero-oxygen branch before computing fractional powers.
if not all(isinstance(v, (int, float)) and not isinstance(v, bool) and math.isfinite(v)
for v in (surface_po2_mmhg, radius_cm, phi_islet_in_slab, ocr_multiplier)):
raise ValueError("spherical BVP arguments must be finite")
if surface_po2_mmhg < 0 or radius_cm <= 0 or ocr_multiplier <= 0 or not (0 < phi_islet_in_slab < 1):
raise ValueError("require pO2 >= 0, radius > 0, ocr_multiplier > 0, and 0 < phi < 1 for both supply closures")
if d_hydrogel_override is not None and (
isinstance(d_hydrogel_override, bool)
or not isinstance(d_hydrogel_override, (int, float))
or not math.isfinite(d_hydrogel_override)
or d_hydrogel_override <= 0
):
raise ValueError("d_hydrogel_override must be finite and positive")
vmax_eff = VMAX_ISLET_PURE_MOL_CM3_S * float(ocr_multiplier)
delta_p_zero_order = (vmax_eff * (radius_cm ** 2)) / (
6.0 * D_O2_ISLET_TISSUE_CM2_S * ALPHA_BUNSEN_MOL_CM3_MMHG
)
r_ws_cm = radius_cm * (phi_islet_in_slab ** (-1.0 / 3.0))
if surface_po2_mmhg <= 1e-4:
return {
"core_po2_nonlinear_mm_mmhg": 0.0,
"core_po2_homogenized_dirichlet_upper_bound_mmhg": 0.0,
"core_po2_zero_order_krogh_mmhg": 0.0,
"zero_order_krogh_drop_mmhg": round(delta_p_zero_order, 3),
"wigner_seitz_outer_radius_um": round(r_ws_cm * 1e4, 2),
"wigner_seitz_outer_radius_exact_um": round(r_ws_cm * 1e4, 4),
"wigner_seitz_two_zone_fv_uniform_supply_surface_po2_mmhg": 0.0,
"wigner_seitz_two_zone_fv_uniform_supply_surface_po2_unrounded_mmhg": 0.0,
"wigner_seitz_two_zone_fv_uniform_supply_core_po2_mmhg": 0.0,
"wigner_seitz_two_zone_fv_uniform_supply_core_po2_unrounded_mmhg": 0.0,
"wigner_seitz_two_zone_fv_gel_only_supply_surface_po2_mmhg": 0.0,
"wigner_seitz_two_zone_fv_gel_only_supply_surface_po2_unrounded_mmhg": 0.0,
"wigner_seitz_two_zone_fv_gel_only_supply_core_po2_mmhg": 0.0,
"wigner_seitz_two_zone_fv_gel_only_supply_core_po2_unrounded_mmhg": 0.0,
"ewald_analytic_shell_diagnostic_surface_po2_mmhg": 0.0,
"ewald_analytic_shell_diagnostic_core_po2_mmhg": 0.0,
"wigner_seitz_fluctuation_corrected_surface_po2_mmhg": 0.0,
"wigner_seitz_fluctuation_corrected_core_po2_mmhg": 0.0,
"wigner_seitz_unsubtracted_shell_surface_po2_mmhg": 0.0,
"wigner_seitz_unsubtracted_shell_core_po2_mmhg": 0.0,
"wigner_seitz_two_zone_fv_uniform_supply_exact": {
**_zero_oxygen_shortcut_diagnostics(surface_po2_mmhg, 1e-4, "volume_mean"),
"mean_constraint_error_mmhg": -surface_po2_mmhg},
"wigner_seitz_two_zone_fv_gel_only_supply_exact": {
**_zero_oxygen_shortcut_diagnostics(surface_po2_mmhg, 1e-4, "volume_mean"),
"mean_constraint_error_mmhg": -surface_po2_mmhg},
"spherical_bvp_solver_diagnostics": {
**_zero_oxygen_shortcut_diagnostics(surface_po2_mmhg, 1e-4, "surface_boundary"),
"clipped_nodes": 0, "raw_core_po2_mmhg": 0.0,
},
}
core_homog, _, sph_diag = _solve_fixed_surface_spherical_bvp_with_diagnostics(
surface_po2_mmhg, radius_cm, nr=nr, ocr_multiplier=ocr_multiplier
)
# Coupled conservative two-zone Finite-Volume (FV) Wigner-Seitz unit-cell solutions (AUDIT-REF):
fv_uniform = _solve_two_zone_fv_wigner_seitz_bvp(
surface_po2_mmhg,
radius_cm,
phi_islet_in_slab=phi_islet_in_slab,
n_per_zone=100,
supply_closure="uniform",
d_hydrogel_override=d_hydrogel_override,
ocr_multiplier=ocr_multiplier,
)
fv_gel_only = _solve_two_zone_fv_wigner_seitz_bvp(
surface_po2_mmhg,
radius_cm,
phi_islet_in_slab=phi_islet_in_slab,
n_per_zone=100,
supply_closure="gel_only",
d_hydrogel_override=d_hydrogel_override,
ocr_multiplier=ocr_multiplier,
)
# Analytical Ewald form factor shell diagnostic (f_ewald(phi) = 1 - 1.8*phi^(1/3) + phi - 0.2*phi^2 = 0.2201084):
phi_13 = phi_islet_in_slab ** (1.0 / 3.0)
# Factorized exact polynomial avoids cancellation near phi=1 without
# imposing a positive shell resistance when the shell has vanished.
ewald_factor = (1.0 - phi_13) ** 3 * (1.0 + 1.2 * phi_13 + 0.6 * phi_13 ** 2 + 0.2 * phi_13 ** 3)
vol_cluster = (4.0 / 3.0) * math.pi * (radius_cm ** 3)
d_gel_ewald = float(d_hydrogel_override) if d_hydrogel_override is not None else D_O2_HYDROGEL_CM2_S
p_lower, p_upper = 0.0, surface_po2_mmhg
ewald_residual = math.inf
for _ in range(64):
p_surf_ewald = 0.5 * (p_lower + p_upper)
core_ewald, i_clust, ewald_spherical_diagnostics = _solve_fixed_surface_spherical_bvp_with_diagnostics(
p_surf_ewald, radius_cm, nr=nr, ocr_multiplier=ocr_multiplier)
v_eff_mol_cm3_s = i_clust / vol_cluster
dp_ws = ((v_eff_mol_cm3_s * (radius_cm ** 2)) / (3.0 * d_gel_ewald * ALPHA_BUNSEN_MOL_CM3_MMHG)) * ewald_factor
ewald_residual = p_surf_ewald + dp_ws - surface_po2_mmhg
if abs(ewald_residual) <= 1e-10 * max(1.0, surface_po2_mmhg):
break
if ewald_residual > 0.0:
p_upper = p_surf_ewald
else:
p_lower = p_surf_ewald
return {
"core_po2_nonlinear_mm_mmhg": round(core_homog, 2),
"core_po2_homogenized_dirichlet_upper_bound_mmhg": round(core_homog, 4),
"core_po2_zero_order_krogh_mmhg": round(max(0.0, surface_po2_mmhg - delta_p_zero_order), 2),
"zero_order_krogh_drop_mmhg": round(delta_p_zero_order, 2),
"wigner_seitz_outer_radius_um": round(r_ws_cm * 1e4, 2),
"wigner_seitz_outer_radius_exact_um": round(r_ws_cm * 1e4, 4),
"ewald_shell_form_factor_exact": round(ewald_factor, 7),
"ewald_analytic_shell_diagnostic_surface_po2_mmhg": round(p_surf_ewald, 2),
"ewald_analytic_shell_diagnostic_core_po2_mmhg": round(core_ewald, 2),
"ewald_analytic_shell_solver_diagnostics": {
"converged": bool(ewald_spherical_diagnostics["converged"]
and abs(ewald_residual) <= 1e-10 * max(1.0, surface_po2_mmhg)),
"surface_po2_unrounded_mmhg": p_surf_ewald,
"core_po2_unrounded_mmhg": core_ewald,
"shell_closure_residual_mmhg": ewald_residual,
"method": "BRACKETED_SELF_CONSISTENT_SHELL_DIAGNOSTIC",
},
"wigner_seitz_two_zone_fv_uniform_supply_surface_po2_mmhg": round(fv_uniform["surface_po2_unrounded_mmhg"], 2),
"wigner_seitz_two_zone_fv_uniform_supply_surface_po2_unrounded_mmhg": fv_uniform["surface_po2_unrounded_mmhg"],
"wigner_seitz_two_zone_fv_uniform_supply_core_po2_mmhg": round(fv_uniform["core_po2_unrounded_mmhg"], 2),
"wigner_seitz_two_zone_fv_uniform_supply_exact": fv_uniform,
"wigner_seitz_two_zone_fv_uniform_supply_core_po2_unrounded_mmhg": fv_uniform["core_po2_unrounded_mmhg"],
"wigner_seitz_two_zone_fv_gel_only_supply_surface_po2_mmhg": round(fv_gel_only["surface_po2_unrounded_mmhg"], 2),
"wigner_seitz_two_zone_fv_gel_only_supply_surface_po2_unrounded_mmhg": fv_gel_only["surface_po2_unrounded_mmhg"],
"wigner_seitz_two_zone_fv_gel_only_supply_core_po2_mmhg": round(fv_gel_only["core_po2_unrounded_mmhg"], 2),
"wigner_seitz_two_zone_fv_gel_only_supply_exact": fv_gel_only,
"wigner_seitz_two_zone_fv_gel_only_supply_core_po2_unrounded_mmhg": fv_gel_only["core_po2_unrounded_mmhg"],
# Map legacy keys to the conservative two-zone FV closures (gel-only lower bound and uniform supply)
# so all gates use solved two-zone FV closures rather than an ad-hoc 0.45 capillary shell multiplier:
"wigner_seitz_fluctuation_corrected_surface_po2_mmhg": round(fv_gel_only["surface_po2_unrounded_mmhg"], 2),
"wigner_seitz_fluctuation_corrected_core_po2_mmhg": round(fv_gel_only["core_po2_unrounded_mmhg"], 2),
"wigner_seitz_unsubtracted_shell_surface_po2_mmhg": round(fv_uniform["surface_po2_unrounded_mmhg"], 2),
"wigner_seitz_unsubtracted_shell_core_po2_mmhg": round(fv_uniform["core_po2_unrounded_mmhg"], 2),
"spherical_bvp_solver_diagnostics": sph_diag,
}
def verify_exact_slab_source_distribution_algebra() -> Dict[str, Any]:
"""
Analytical and 4,001-node finite-difference verification of the 1D steady-state reaction-diffusion
boundary-value problem across the 350 um double-sided exchange slab (2L = 0.035 cm, L = 0.0175 cm).
Directly closes Audit V70-12, R05, AUDIT-REF, AUDIT-REF, 015, 097, and 099.
"""
half_L_cm = 0.0175 # 175 um half-thickness (total slab thickness 2L = 350 um = 0.035 cm)
delta_p_max = P_BOUNDARY_HOST_BED_MMHG - P_CRIT_GSIS_SHUTOFF_MMHG # 40 - 15 = 25 mmHg
q_passive_mol_cm3_s = 2.0 * D_O2_HYDROGEL_CM2_S * ALPHA_BUNSEN_MOL_CM3_MMHG * delta_p_max / (half_L_cm ** 2)
phi_passive_pure_islet = q_passive_mol_cm3_s / VMAX_ISLET_PURE_MOL_CM3_S
sigma_passive_pure_islet = (phi_passive_pure_islet * (2.0 * half_L_cm)) / VOL_1_IEQ_CM3 # 1,198.286 IEQ/cm^2
vascular_demand_multiplier = 1.15
sigma_passive_with_iec = sigma_passive_pure_islet / vascular_demand_multiplier # 1,041.988 IEQ/cm^2
def _exact_finite_lamina_f_chem_min(r_ratio: float, a_frac: float) -> float:
"""
Two-branch piecewise exact zero-order threshold for a central source lamina [-aL, aL]:
- Branch 1 (f <= a, i.e. r >= (1 - a)^2): minimum is at z = 0, giving f = (1 - r) / (2 - a).
- Branch 2 (f > a, i.e. r < (1 - a)^2): local supply exceeds consumption inside [-aL, aL],
so z = 0 is a local maximum and the global minimum occurs in the outer passive annulus at |z| = f*L > a*L,
giving f = 1 - sqrt(r) (matching the thin-sheet delta-source formula).
Directly closes AUDIT-REF and AUDIT-REF.
"""
f_b1 = max(0.0, (1.0 - r_ratio) / (2.0 - a_frac))
return f_b1 if f_b1 <= a_frac else max(0.0, 1.0 - math.sqrt(r_ratio))
loading_table = []
for sigma in [1500.0, 2000.0, 2500.0, 3000.0]:
area_for_500k_cm2 = 500000.0 / sigma
active_slab_vol_cm3 = area_for_500k_cm2 * (2.0 * half_L_cm)
pure_islet_vol_cm3 = 500000.0 * VOL_1_IEQ_CM3 # 0.8836 cm^3
phi_islet = pure_islet_vol_cm3 / active_slab_vol_cm3
r_pure = sigma_passive_pure_islet / sigma
r_iec = sigma_passive_with_iec / sigma
f_uniform_pure = max(0.0, 1.0 - r_pure)
f_central_sheet_pure = max(0.0, 1.0 - math.sqrt(r_pure))
# Exact two-branch piecewise finite-width zero-order threshold for a central lamina of half-width fraction a from the shared physical geometry; a=.35 is an explicitly separate width sensitivity:
f_finite_lamina_034_pure = _exact_finite_lamina_f_chem_min(r_pure, CENTRAL_LAMINA_HALF_WIDTH_FRACTION)
f_finite_lamina_035_pure = _exact_finite_lamina_f_chem_min(r_pure, 0.35)
f_center_z0_branch_034_pure = max(0.0, (1.0 - r_pure) / (2.0 - CENTRAL_LAMINA_HALF_WIDTH_FRACTION))
f_center_z0_branch_035_pure = max(0.0, (1.0 - r_pure) / (2.0 - 0.35))
f_uniform_with_iec = max(0.0, 1.0 - r_iec)
f_central_sheet_with_iec = max(0.0, 1.0 - math.sqrt(r_iec))
f_finite_lamina_034_with_iec = _exact_finite_lamina_f_chem_min(r_iec, CENTRAL_LAMINA_HALF_WIDTH_FRACTION)
f_finite_lamina_035_with_iec = _exact_finite_lamina_f_chem_min(r_iec, 0.35)
f_center_z0_branch_034_with_iec = max(0.0, (1.0 - r_iec) / (2.0 - CENTRAL_LAMINA_HALF_WIDTH_FRACTION))
f_center_z0_branch_035_with_iec = max(0.0, (1.0 - r_iec) / (2.0 - 0.35))
loading_table.append(
{
"areal_loading_ieq_per_cm2": sigma,
"required_double_sided_exchange_area_for_500k_ieq_cm2": round(area_for_500k_cm2, 2),
"active_350um_hydrogel_slab_volume_cm3": round(active_slab_vol_cm3, 3),
"pure_islet_tissue_volume_cm3": round(pure_islet_vol_cm3, 4),
"islet_volume_fraction_in_active_slab": round(phi_islet, 4),
"f_chem_min_uniform_dispersion_islet_only": round(f_uniform_pure, 4),
"f_chem_min_central_core_sheet_islet_only": round(f_central_sheet_pure, 4),
"f_chem_min_delta_sheet_lower_bound_islet_only": round(f_central_sheet_pure, 4),
"f_chem_min_finite_width_lamina_a0p34_zero_order_islet_only": round(f_finite_lamina_034_pure, 4),
"f_chem_min_finite_width_lamina_a0p35_zero_order_islet_only": round(f_finite_lamina_035_pure, 4),
"f_chem_min_center_z0_branch_only_a0p34_islet_only": round(f_center_z0_branch_034_pure, 4),
"f_chem_min_center_z0_branch_only_a0p35_islet_only": round(f_center_z0_branch_035_pure, 4),
"f_chem_min_uniform_dispersion_including_15pct_iec_demand": round(f_uniform_with_iec, 4),
"f_chem_min_central_core_sheet_including_15pct_iec_demand": round(f_central_sheet_with_iec, 4),
"f_chem_min_delta_sheet_lower_bound_including_15pct_iec_demand": round(f_central_sheet_with_iec, 4),
"f_chem_min_finite_width_lamina_a0p34_zero_order_including_15pct_iec_demand": round(f_finite_lamina_034_with_iec, 4),
"f_chem_min_finite_width_lamina_a0p35_zero_order_including_15pct_iec_demand": round(f_finite_lamina_035_with_iec, 4),
"f_chem_min_center_z0_branch_only_a0p34_including_15pct_iec_demand": round(f_center_z0_branch_034_with_iec, 4),
"f_chem_min_center_z0_branch_only_a0p35_including_15pct_iec_demand": round(f_center_z0_branch_035_with_iec, 4),
"damkohler_passive_gsis_islet_only": round(sigma / sigma_passive_pure_islet, 4),
"thiele_modulus_passive_gsis_islet_only": round(math.sqrt(sigma / sigma_passive_pure_islet), 4),
"damkohler_passive_gsis_with_15pct_iec": round(sigma / sigma_passive_with_iec, 4),
"thiele_modulus_passive_gsis_with_15pct_iec": round(math.sqrt(sigma / sigma_passive_with_iec), 4),
}
)
# Independent 4,001-node Thomas finite-difference verification of two-branch piecewise finite-width lamina vs delta-sheet (AUDIT-REF):
n_fd = 4001
z_fd = np.linspace(-half_L_cm, half_L_cm, n_fd)
dz_fd = z_fd[1] - z_fd[0]
q_2500_pure = (2500.0 * VOL_1_IEQ_CM3 / (2.0 * half_L_cm)) * VMAX_ISLET_PURE_MOL_CM3_S
q_2500_iec = q_2500_pure * 1.15
q_3000_iec = (3000.0 * VOL_1_IEQ_CM3 / (2.0 * half_L_cm)) * VMAX_ISLET_PURE_MOL_CM3_S * 1.15
a_lamina = CENTRAL_LAMINA_HALF_WIDTH_FRACTION
mask_fd = _slab_control_volume_profiles(n_fd, half_L_cm)["central_source"]
f_delta_exact = 1.0 - math.sqrt(sigma_passive_pure_islet / 2500.0)
f_finite_034_exact = _exact_finite_lamina_f_chem_min(sigma_passive_pure_islet / 2500.0, a_lamina)