import os for _v in ('OMP_NUM_THREADS','OPENBLAS_NUM_THREADS','MKL_NUM_THREADS','NUMEXPR_NUM_THREADS'): os.environ[_v]='1' import sys; sys.path.insert(0,'.packages'); sys.path.insert(0,'vendor'); sys.path.insert(0,'.') import copy from physics_core import (osmotic_pressure_diff, osmotic_pressure_diff_conc, uf_flux, diffusion_flux, cori_react, REFLECTION, R_T, SOLUTES, PROTEINS) from physics_sim import PhysicsSim # Assert the physics that step() INLINES is byte-identical to the physics_core self-tested primitives # (audit item: step() re-implements uf/diffusion/osmosis/Cori instead of calling the tested functions; # this test fails loudly if they ever diverge). sim = PhysicsSim(weight=70) sim.layers[0].solutes['Urea'] += 50.0 * sim.layers[0].volume # make a gradient sim.layers[0].solutes['Glucose'] += 120.0 * sim.layers[0].volume serum, layer, vd, N, dt = sim.serum, sim.layers[0], sim.serum_vd, sim.N, 1.0 # 1) osmotic Δπ: the pool form vs the concentration form that step() now calls. # UPDATED 2026-08-09. This check used to compare physics_core against a hand-rolled van't Hoff # sum written out here, because step() carried its own inline copy of the law. It no longer # does — step() calls osmotic_pressure_diff_conc — so the reference implementation to compare # against is that function, not a third copy living in a test file. The old expression is kept # below and asserted to DIFFER, which is the honest way to record that plasma protein stopped # obeying van't Hoff: it is not a rounding difference, it is a different constitutive law # (Landis-Pappenheimer colloid osmotic pressure — see physics_core). lc = {s: layer.conc(s) for s in SOLUTES} sc = {s: serum.conc(s, vd[s]) for s in SOLUTES} dpi_p = osmotic_pressure_diff(layer, serum, vd) dpi_i = osmotic_pressure_diff_conc(lc, sc) assert abs(dpi_p - dpi_i) < 1e-12, f"osmotic Δπ mismatch: {dpi_p} vs {dpi_i}" # 1b) the SUPERSEDED all-van't-Hoff form must now differ, and only in the protein term. dpi_vh = sum(REFLECTION[s] * R_T * (lc[s] - sc[s]) for s in SOLUTES) assert abs(dpi_p - dpi_vh) > 1.0, ( f"protein is still on van't Hoff: Landis-Pappenheimer {dpi_p} vs van't Hoff {dpi_vh}") crys_p = sum(REFLECTION[s] * R_T * (lc[s] - sc[s]) for s in SOLUTES if s not in PROTEINS) crys_vh = dpi_vh - sum(REFLECTION[s] * R_T * (lc[s] - sc[s]) for s in PROTEINS) assert abs(crys_p - crys_vh) < 1e-12, "the CRYSTALLOID terms must be untouched by the protein change" # 2) UF value: uf_flux == step's (Kf/N)*(dP+dpi)*dt Kf_layer, dP = sim.Kf / N, 8.0 uf_i = Kf_layer * (dP + dpi_p) * dt s2, l2 = copy.deepcopy(serum), copy.deepcopy(layer) uf_p = uf_flux(s2, l2, Kf_layer, dP, dt, vd) assert abs(uf_p - uf_i) < 1e-12, f"UF mismatch: {uf_p} vs {uf_i}" # 3) diffusion: diffusion_flux moves exactly step's (mtac/N)*(sc-lc)*dt per solute s3, l3 = copy.deepcopy(serum), copy.deepcopy(layer) mtac_layer = {s: sim.mtac[s] / N for s in SOLUTES} before = {s: s3.solutes[s] for s in SOLUTES} diffusion_flux(s3, l3, mtac_layer, dt, vd) for s in SOLUTES: j_i = (sim.mtac[s] / N) * (serum.conc(s, vd[s]) - layer.conc(s)) * dt moved = before[s] - s3.solutes[s] assert abs(moved - j_i) < 1e-9, f"diffusion {s} mismatch: {moved} vs {j_i}" # 4) Cori: cori_react moves 1 Lactate -> 1 Bicarbonate (== step's inline) s4 = copy.deepcopy(serum); lac0, hco0 = s4.solutes['Lactate'], s4.solutes['Bicarbonate'] cori_react(s4, 0.3) assert abs((lac0 - s4.solutes['Lactate']) - 0.3) < 1e-12 and abs((s4.solutes['Bicarbonate'] - hco0) - 0.3) < 1e-12, "Cori mismatch" print("ENGINE-EQUIVALENCE PASS: step() inline membrane/UF/diffusion/Cori == physics_core self-tested primitives") print(os.popen("TZ='America/Phoenix' date '+🕐 %H%M MST (%Y-%m-%d)'").read().strip())