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…

    f_finite_034_2500_iec_exact = _exact_finite_lamina_f_chem_min(sigma_passive_with_iec / 2500.0, a_lamina)
    f_finite_034_3000_iec_exact = _exact_finite_lamina_f_chem_min(sigma_passive_with_iec / 3000.0, a_lamina)
    diag_fd = np.full(n_fd - 2, -2.0 / (dz_fd * dz_fd), dtype=np.float64)
    off_fd = np.full(n_fd - 3, 1.0 / (dz_fd * dz_fd), dtype=np.float64)

    def _solve_zero_order_fd_min_po2(f_val: float, q_val: float = q_2500_pure) -> float:
        rhs_fd = np.full(n_fd - 2, q_val / (D_O2_HYDROGEL_CM2_S * ALPHA_BUNSEN_MOL_CM3_MMHG), dtype=np.float64) - (
            f_val * q_val / (D_O2_HYDROGEL_CM2_S * ALPHA_BUNSEN_MOL_CM3_MMHG)
        ) * mask_fd[1:-1]
        rhs_fd[0] -= P_BOUNDARY_HOST_BED_MMHG / (dz_fd * dz_fd)
        rhs_fd[-1] -= P_BOUNDARY_HOST_BED_MMHG / (dz_fd * dz_fd)
        p_fd = _tridiag_thomas_solve(off_fd, diag_fd, off_fd, rhs_fd)
        return float(np.min(p_fd))

    min_po2_at_delta_sheet_f = _solve_zero_order_fd_min_po2(f_delta_exact)
    min_po2_at_finite_lamina_f = _solve_zero_order_fd_min_po2(f_finite_034_exact)
    min_po2_at_2500_iec_piecewise_f = _solve_zero_order_fd_min_po2(f_finite_034_2500_iec_exact, q_val=q_2500_iec)
    min_po2_at_3000_iec_piecewise_f = _solve_zero_order_fd_min_po2(f_finite_034_3000_iec_exact, q_val=q_3000_iec)
    min_po2_at_shipped_f045 = _solve_zero_order_fd_min_po2(0.45)

    # Reconcile initial vs 72h-integrated vs total-inventory chemical O2 fractions (AUDIT-REF):
    f_chem_init = 0.45
    t_half_h = 68.0
    k_hyd_h = math.log(2.0) / t_half_h
    f_chem_72h_integrated = f_chem_init * (1.0 - math.exp(-k_hyd_h * 72.0)) / (k_hyd_h * 72.0)
    f_chem_total_inventory_72h_window = f_chem_init / (k_hyd_h * 72.0)

    # Reconcile fixed-areal-loading (fixed-sigma, linear in H) vs fixed-volume-fraction (fixed-phi, quadratic in H)
    # critical passive slab thickness at sigma = 3,000 IEQ/cm^2 (AUDIT-REF):
    sigma_target_high = 3000.0
    phi_at_3000_and_350um = (sigma_target_high * VOL_1_IEQ_CM3) / (2.0 * half_L_cm)  # 0.151470
    h_crit_fixed_sigma_islet_only_um = 350.0 * (sigma_passive_pure_islet / sigma_target_high)
    h_crit_fixed_sigma_with_iec_um = 350.0 * (sigma_passive_with_iec / sigma_target_high)
    h_crit_fixed_phi_islet_only_um = 350.0 * math.sqrt(sigma_passive_pure_islet / sigma_target_high)
    h_crit_fixed_phi_with_iec_um = 350.0 * math.sqrt(sigma_passive_with_iec / sigma_target_high)
    sigma_at_fixed_phi_h_crit_islet_only = sigma_target_high * (h_crit_fixed_phi_islet_only_um / 350.0)

    # Programmatically derived non-overlapping 7.00 cm^3 phase volume ledger (AUDIT-REF):
    active_slab_total_cm3 = 200.0 * (2.0 * half_L_cm)  # 7.0000 cm^3
    k_ref_s = math.log(2.0) / (68.0 * 3600.0)
    phi_2500 = (2500.0 * VOL_1_IEQ_CM3) / (2.0 * half_L_cm)
    r_o2_0_lead = f_chem_init * (phi_2500 * VMAX_ISLET_PURE_MOL_CM3_S * 1.15) * active_slab_total_cm3
    n_cao2_mmol_lead = (2.0 * r_o2_0_lead / k_ref_s) * 1e3
    cao2_mass_mg_lead = n_cao2_mmol_lead * 72.078
    rho_cao2 = 2.91
    rho_pdms = 1.03
    v_cao2_cm3 = (cao2_mass_mg_lead * 1e-3) / rho_cao2
    pdms_mass_b_mg = cao2_mass_mg_lead * (68.75 / 31.25)
    v_pdms_b_cm3 = (pdms_mass_b_mg * 1e-3) / rho_pdms
    v_beads_b_cm3 = v_cao2_cm3 + v_pdms_b_cm3
    v_lamina_51_cm3 = active_slab_total_cm3 * CENTRAL_LAMINA_HALF_WIDTH_FRACTION
    v_lamina_cont_cm3 = v_lamina_51_cm3
    v_pore_51_cm3 = v_lamina_51_cm3 - v_beads_b_cm3
    v_pore_cont_cm3 = v_lamina_cont_cm3 - v_beads_b_cm3
    v_islet_cm3 = 500000.0 * VOL_1_IEQ_CM3
    v_iec_cm3 = v_islet_cm3 * 0.15
    v_outer_gel_51_cm3 = active_slab_total_cm3 - (v_beads_b_cm3 + v_pore_51_cm3 + v_islet_cm3 + v_iec_cm3)
    v_outer_gel_cont_cm3 = active_slab_total_cm3 - (v_beads_b_cm3 + v_pore_cont_cm3 + v_islet_cm3 + v_iec_cm3)
    phases_51 = [v_beads_b_cm3, v_pore_51_cm3, v_islet_cm3, v_iec_cm3, v_outer_gel_51_cm3]
    if min(phases_51) < 0.0 or abs(sum(phases_51) - active_slab_total_cm3) > 1e-9:
        raise RuntimeError("Phase volume conservation violated in 7.00 cm^3 pouch ledger.")

    return {
        "slab_full_thickness_um": 350.0,
        "slab_half_thickness_um": 175.0,
        "host_boundary_po2_mmhg": P_BOUNDARY_HOST_BED_MMHG,
        "target_minimum_interior_po2_mmhg": P_CRIT_GSIS_SHUTOFF_MMHG,
        "passive_capacity_islet_only_ieq_per_cm2": round(sigma_passive_pure_islet, 3),
        "passive_capacity_with_15pct_iec_stroma_ieq_per_cm2": round(sigma_passive_with_iec, 3),
        "finite_width_lamina_vs_thin_sheet_zero_order_fd_verification_v80_015": {
            "grid_points_n": n_fd,
            "lamina_half_width_fraction_a": a_lamina,
            "thin_sheet_delta_source_threshold_f_chem": round(f_delta_exact, 7),
            "finite_width_lamina_zero_order_threshold_f_chem": round(f_finite_034_exact, 7),
            "finite_width_lamina_piecewise_threshold_2500_with_iec_f_chem": round(f_finite_034_2500_iec_exact, 7),
            "finite_width_lamina_piecewise_threshold_3000_with_iec_f_chem": round(f_finite_034_3000_iec_exact, 7),
            "min_po2_at_thin_sheet_threshold_f_0p3077_mmhg": round(min_po2_at_delta_sheet_f, 5),
            "min_po2_at_finite_width_threshold_f_0p3137_mmhg": round(min_po2_at_finite_lamina_f, 5),
            "min_po2_at_2500_with_iec_piecewise_threshold_f_0p3544_mmhg": round(min_po2_at_2500_iec_piecewise_f, 5),
            "min_po2_at_3000_with_iec_piecewise_threshold_f_0p4107_mmhg": round(min_po2_at_3000_iec_piecewise_f, 5),
            "min_po2_at_shipped_lead_f_0p4500_mmhg": round(min_po2_at_shipped_f045, 5),
            "derivation_note": (
                "For zero-order consumption across [-L, L] with a central source lamina of half-width fraction a = 0.34, "
                "the exact two-branch piecewise zero-order threshold for min_{z} p(z) >= 15.0 mmHg is "
                "f_b1 = (1 - r)/(2 - a) when f_b1 <= a (r >= (1 - a)^2, minimum at z = 0; e.g. 2,500 pure -> f = 0.3136659) "
                "and f_b2 = 1 - sqrt(r) when f_b1 > a (r < (1 - a)^2, where z = 0 is a local maximum and the global minimum "
                "occurs in the outer passive annulus at |z| = f*L > a*L; e.g. 2,500+iEC -> f = 0.3544032, 3,000+iEC -> f = 0.4106534). "
                "Verified to 5 decimal places (15.00000 mmHg) against the 4,001-node FD BVP solver across all loading regimes."
            ),
        },
        "critical_passive_slab_thickness_reconciliation_at_3000_ieq_cm2_sol_v80_r2_10": {
            "fixed_areal_loading_sigma_3000_ieq_cm2_linear_scaling": {
                "scaling_law": "Delta_p = (V_max * sigma * V_IEQ * H) / (8 * D_O2 * alpha) -> H_crit_fixed_sigma = H_0 * (sigma_passive / sigma)",
                "h_crit_islet_only_um": round(h_crit_fixed_sigma_islet_only_um, 2),
                "h_crit_with_15pct_iec_um": round(h_crit_fixed_sigma_with_iec_um, 2),
                "resulting_islet_volume_fraction_at_h_crit_islet_only": round(
                    (sigma_target_high * VOL_1_IEQ_CM3) / (h_crit_fixed_sigma_islet_only_um * 1e-4), 4
                ),
            },
            "fixed_volume_fraction_phi_0p1515_quadratic_scaling": {
                "scaling_law": "Delta_p = (V_max * phi * H^2) / (8 * D_O2 * alpha) -> H_crit_fixed_phi = H_0 * sqrt(sigma_passive / sigma_0)",
                "reference_volume_fraction_phi": round(phi_at_3000_and_350um, 4),
                "h_crit_islet_only_um": round(h_crit_fixed_phi_islet_only_um, 2),
                "h_crit_with_15pct_iec_um": round(h_crit_fixed_phi_with_iec_um, 2),
                "corresponding_areal_loading_sigma_at_h_crit_ieq_cm2": round(sigma_at_fixed_phi_h_crit_islet_only, 1),
            },
        },
        "chemical_o2_fraction_definitions_reconciled_sol_v80_12": {
            "f_chem_initial_hour0_rate_fraction": round(f_chem_init, 4),
            "f_chem_72h_time_averaged_integrated_fraction": round(f_chem_72h_integrated, 6),
            "f_chem_total_inventory_to_72h_demand_ratio": round(f_chem_total_inventory_72h_window, 6),
            "cumulative_72h_pure_endocrine_o2_demand_mmol": round(
                (phi_2500 * VMAX_ISLET_PURE_MOL_CM3_S) * active_slab_total_cm3 * 72.0 * 3600.0 * 1e3, 4
            ),
            "cumulative_72h_iec_stroma_inclusive_1p15x_o2_demand_mmol": round(
                (phi_2500 * VMAX_ISLET_PURE_MOL_CM3_S * 1.15) * active_slab_total_cm3 * 72.0 * 3600.0 * 1e3, 4
            ),
            "loaded_cao2_inventory_mmol_at_2mol_per_mol_o2_and_1p15x_demand": round(n_cao2_mmol_lead, 4),
            "loaded_cao2_inventory_mass_mg_at_1p15x_demand": round(cao2_mass_mg_lead, 2),
            "pure_endocrine_1p00x_comparator_cao2_inventory_mmol": round(n_cao2_mmol_lead / 1.15, 4),
            "pure_endocrine_1p00x_comparator_cao2_mass_mg": round((n_cao2_mmol_lead / 1.15) * 72.078, 2),
            "reconciliation_note": (
                "At f_chem_initial = 0.45 and the unvalidated formulation-specific reference/target t_1/2 = 68 h (k_hyd = 0.010193 h^-1; used in 20 of 21 main PDE regimes while the uncoated CaO2 control uses 4 h; each distinct CaO2:PDMS wt% recipe, CaO2 crystal PSD, composite microbead PSD, catalase surface treatment, and pre-implant wetting state requires independent empirical t_1/2 + kinetic-law qualification under IIP-0010 / OQ-005 / DEC-088), the Hour-0 chemical O2 release rate "
                "supplies 45.0% of total graft+iEC metabolic demand (1.15x vascular/stromal demand multiplier on pure endocrine V_max = 3.40e-8 mol/(cm^3*s), "
                "giving 8.9547 mmol cumulative 72-h O2 demand vs 7.7867 mmol pure endocrine demand, and 2 * 0.613145 * 8.9547 = 10.9811 mmol = 791.49 mg CaO2 "
                "vs 9.5488 mmol = 688.25 mg on a pure-endocrine basis; exceeding both the 35.44% delta-sheet lower bound and "
                f"the {100*loading_table[2]['f_chem_min_finite_width_lamina_a0p34_zero_order_including_15pct_iec_demand']:.2f}% fixed-width zero-order threshold). Over the "
                "72-hour avascular bridge, exponential decay yields a 0-72h time-averaged chemical supply fraction of "
                f"{f_chem_72h_integrated:.4f} (31.88%), while the total loaded CaO2 inventory equals {f_chem_total_inventory_72h_window:.4f} "
                "(61.31%) of 72-hour cumulative graft+iEC O2 demand because progressive iEC vascular anastomosis (t_50 = 36.0 h "
                "in a 14-day pre-vascularized bed) takes over O2 delivery from Hour 24 onward."
            ),
        },
        "active_vs_structural_volume_reconciliation_v70_12": (
            "At 2,500 IEQ/cm^2 across 200 cm^2 exchange area (or 2,000 IEQ/cm^2 across 250 cm^2), the active 350 um "
            "oxygen-consuming hydrogel slab volume is exactly 200 * 0.035 = 7.00 cm^3 (or 250 * 0.035 = 8.75 cm^3), NOT 20 cm^3. "
            "Any external macroporous PTFE/silicone frame or inter-leaf vascularized host tissue channel (~11-13 cm^3) is outside "
            "the 350 um diffusion barrier and must not be used as the denominator for hydrogel CaO2 wt/vol loading."
        ),
        "non_overlapping_phase_volume_and_cad_ledger_7cm3_ultracode_v80_18": {
            "active_exchange_slab_total_volume_cm3": round(active_slab_total_cm3, 4),
            "cao2_initial_molar_inventory_mmol": round(n_cao2_mmol_lead, 3),
            "cao2_initial_mass_mg": round(cao2_mass_mg_lead, 1),
            "cao2_crystal_density_g_cm3": rho_cao2,
            "cao2_pure_solid_volume_cm3": round(v_cao2_cm3, 4),
            "pdms_cured_density_g_cm3": rho_pdms,
            "layout_option_a_25wt_pct_cao2_in_extended_polymer_scaffold": {
                "formulation": "25 wt% CaO2 : 75 wt% PDMS (1:3 wt/wt; 791.5 mg CaO2 + 2,374.5 mg PDMS = 3,166.0 mg composite)",
                "pdms_volume_cm3": round(2.3745 / rho_pdms, 4),
                "composite_solid_volume_cm3": round(v_cao2_cm3 + 2.3745 / rho_pdms, 4),
                "composite_fraction_of_7cm3_slab": round((v_cao2_cm3 + 2.3745 / rho_pdms) / active_slab_total_cm3, 4),
                "spatial_reconciliation": (
                    f"The 25 wt% alternative occupies {v_cao2_cm3 + 2.3745 / rho_pdms:.4f} cm^3, exceeding "
                    f"the model's fixed {100*CENTRAL_LAMINA_HALF_WIDTH_FRACTION:.2f}% central lamina "
                    f"({v_lamina_cont_cm3:.4f} cm^3). It is an unmodeled alternative requiring a new geometric "
                    "layout; it must not be interpreted as fitting the current confined Layout B source zone."
                ),
            },
            "layout_option_b_31wt_pct_cao2_confined_central_lamina_microbeads": {
                "formulation": f"31.25 wt% CaO2 : 68.75 wt% PDMS (1:2.2 wt/wt; {cao2_mass_mg_lead:.1f} mg CaO2 + {pdms_mass_b_mg:.1f} mg PDMS = {cao2_mass_mg_lead + pdms_mass_b_mg:.1f} mg composite microbeads)",
                "cao2_pure_solid_volume_cm3": round(v_cao2_cm3, 4),
                "pdms_volume_cm3": round(v_pdms_b_cm3, 4),
                "composite_microbead_solid_volume_cm3": round(v_beads_b_cm3, 4),
                "central_lamina_interstitial_hydrogel_and_catalase_pore_volume_cm3": round(v_pore_51_cm3, 4),
                "central_lamina_total_envelope_volume_cm3": round(v_lamina_51_cm3, 4),
                "central_lamina_continuous_34pct_envelope_volume_cm3": round(v_lamina_cont_cm3, 4),
                "central_lamina_continuous_34pct_interstitial_hydrogel_pore_volume_cm3": round(v_pore_cont_cm3, 4),
                "central_lamina_void_fraction_from_mass_and_density": v_pore_cont_cm3 / v_lamina_cont_cm3,
                "eps_0p42_inventory_retention_at_same_formulation_and_lamina": (v_lamina_cont_cm3 * (1.0 - 0.42)) / v_beads_b_cm3,
                "outer_band_thickness_um_each": half_L_cm * 1e4 * (1.0 - CENTRAL_LAMINA_HALF_WIDTH_FRACTION),
                "diameter_120um_full_outer_band_exclusion_feasible": bool(120. <= half_L_cm * 1e4 * (1.0 - CENTRAL_LAMINA_HALF_WIDTH_FRACTION)),
                "control_volume_geometry_reconciliation_preastra_a094": (
                    f"One physical source lamina |z| <= {175*CENTRAL_LAMINA_HALF_WIDTH_FRACTION:.2f} um "
                    f"spans {100*CENTRAL_LAMINA_HALF_WIDTH_FRACTION:.2f}% of the slab = {v_lamina_cont_cm3:.4f} cm^3. "
                    "Node control-volume overlap fractions integrate this same geometry on every mesh. "
                    f"Interstitial lamina pore volume is {v_pore_cont_cm3:.4f} cm^3 and outer cell-laden matrix "
                    f"volume is {v_outer_gel_cont_cm3:.4f} cm^3. Source normalization preserves the declared molar inventory."
                ),
                "central_lamina_half_width_fraction": CENTRAL_LAMINA_HALF_WIDTH_FRACTION,
                "pure_sc_islet_endocrine_tissue_volume_cm3": round(v_islet_cm3, 4),
                "co_seeded_iec_pericyte_vascular_stroma_volume_cm3": round(v_iec_cm3, 4),
                "outer_annular_cell_laden_peg_fibrin_hydrogel_matrix_volume_cm3": round(v_outer_gel_51_cm3, 4),
                "outer_annular_continuous_66pct_peg_fibrin_hydrogel_matrix_volume_cm3": round(v_outer_gel_cont_cm3, 4),
                "sum_of_all_non_overlapping_phases_cm3": round(sum(phases_51), 4),
                "sum_of_all_non_overlapping_phase_fractions": round(sum(phases_51) / active_slab_total_cm3, 4),
            },
            "effective_diffusivity_provenance_v80_110": {
                "d_o2_water_37c_cm2_s": 2.92e-5,
                "maxwell_mackie_meares_hydrogel_obstruction_factor": 0.322005,
                "d_o2_hydrogel_effective_cm2_s": D_O2_HYDROGEL_CM2_S,
                "derivation_note": (
                    "D_O2_HYDROGEL_CM2_S = 0.940254e-5 cm^2/s is the Maxwell-Garnett / Mackie-Meares effective diffusivity "
                    "of a 5 wt% PEG-4MAL/fibrin hydrogel at 37 C containing phi_islet = 0.126225 and 15% vascular stroma, "
                    "yielding sigma_passive_pure_islet = 1,198.286 IEQ/cm^2."
                ),
            },
            "catalase_localization_and_homogenization_qualification": (
                "The 1D reaction-diffusion BVP models k_decomp = 18.0 s^-1 and D_O2 = 0.940e-5 cm^2/s as homogenized effective "
                "continuum parameters across the hydrated hydrogel phase. Co-immobilizing Catalase on hydrophobic PDMS-CaO2 "
                "microbead surfaces requires wet-lab verification of retained specific activity, washout/inactivation half-life "
                "over 14 days, phase-dependent effective diffusivity, and local Ca2+/OH-/osmolality buffering."
            ),
            "cao2_crystal_particle_size_distribution_um": "unknown_pending_stage0_measurement",
            "pdms_cao2_composite_microbead_diameter_distribution_um": "unknown_pending_stage0_measurement",
            "cao2_solid_volume_fraction_in_cured_composite_by_formulation": {
                "layout_option_a_25wt_pct": round((0.25 / rho_cao2) / ((0.25 / rho_cao2) + (0.75 / rho_pdms)), 4),
                "layout_option_b_31p25wt_pct": round((0.3125 / rho_cao2) / ((0.3125 / rho_cao2) + (0.6875 / rho_pdms)), 4),
                "design_x_46p39wt_pct": round((0.463906 / rho_cao2) / ((0.463906 / rho_cao2) + (0.536094 / rho_pdms)), 4),
            },
            "formulation_transfer_and_percolation_qualification_note_iip_0010": (
                "At rho_CaO2 = 2.91 g/cm^3 and rho_PDMS = 1.03 g/cm^3, raising dry CaO2 loading from 25.00 wt% (10.55 vol% solid filler; Pedraza et al. 2012 PMID:22371586 macroscopic monolithic PDMS disk context) "
                "to 31.25 wt% (13.86 vol% in Layout B's 119-um lamina) and 46.39 wt% (23.45 vol% in Stage-0 Design X's 100-um lamina) substantially increases inorganic solid volume fraction inside cured PDMS microbeads, "
                "where hydrophilic filler clustering, particle-size distribution, microbead surface-to-volume ratio (Accolla et al. 2023 PMID:36971050; Ke et al. 2022 PMID:35743287), and co-immobilized hydrophilic catalase "
                "can alter water wicking, connected porosity, kinetic law (first-order vs reaction-controlled shrinking-core vs diffusion-controlled Ginstling-Brounshtein), and effective t_1/2. "
                "Accordingly, t_1/2 = 68.0 h is treated strictly as an unvalidated formulation-specific reference/target rather than a conserved material constant, without claiming a universal 16 vol% percolation cliff across polydisperse PDMS composites (IIP-0010 / OQ-005 / DEC-088)."
            ),
        },