Spaces:
Sleeping
Sleeping
| """ | |
| Cryogenic Pump Cycle Analysis - AbstractState-Accelerated Version (Phase 1) | |
| Uses CoolProp's AbstractState API instead of PropsSI to eliminate redundant | |
| state updates and string parsing overhead. All thermodynamic VALUES are | |
| identical to cycle2mdot_cached.py — only the API calling convention changed. | |
| PERFORMANCE GAINS: | |
| - PropsSI parses fluid name, input type strings, and unit conversions on EVERY | |
| call. AbstractState.update() does the flash once, then property reads (p(), | |
| hmass(), smass(), etc.) are trivial attribute accesses. | |
| - State update: 5 PropsSI calls → 3 AbstractState.update() calls (~50x faster) | |
| - ICV flow: 4 PropsSI calls → 2 AbstractState.update() calls (~17x faster) | |
| - Blowby flow: 4 PropsSI calls → 2 AbstractState.update() calls (~17x faster) | |
| - DCV flow: already uses 1D table (unchanged) | |
| - Convection: CoolProp via refprop (unchanged, called rarely) | |
| WHAT DID NOT CHANGE: | |
| - Energy balance equation (sacred) | |
| - Adaptive timestep logic | |
| - Valve dynamics, geometry, all physics | |
| - Output format and history arrays | |
| - Numerical values (same CoolProp solver, same results) | |
| Author: Auto-generated Phase 1 acceleration | |
| """ | |
| import numpy as np | |
| import math | |
| from typing import Optional, Tuple, List, Dict, Any | |
| import time | |
| import CoolProp | |
| from CoolProp.CoolProp import PropsSI | |
| # Import non-hot-loop functions from the original | |
| from cycle2mdot_cached import ( | |
| refprop, coolprop_fluid_name, subcool_K, kv_from_Cd_and_RO_dia, | |
| fluid_phase_qual, mixture_pump_prop, flow_RF_kgpm, | |
| composite_thermal_conductivity, mean_free_path_meter, | |
| molecular_cond_WpmK, free_conv_2cyl_Wpm, minmax, max_dt_s, | |
| _get_cached_fluid_props, _build_ps_table_1d, print_results, | |
| PI, STEFAN_BOLTZMANN, RU, | |
| ) | |
| # ── Module-level PS table cache (avoids 10k CoolProp calls on repeat runs) ── | |
| _PS_TABLE_CACHE = {} | |
| def _build_ps_table_1d_cached(s_fixed, P_range_MPa, fluid, n=2000): | |
| """Cached wrapper around _build_ps_table_1d. | |
| Cache key uses rounded values to handle floating-point comparison safely. | |
| The table only changes when exit conditions (entropy, pressure range, fluid) change. | |
| """ | |
| key = (round(s_fixed, 6), round(P_range_MPa[0], 6), | |
| round(P_range_MPa[1], 6), fluid, n) | |
| if key in _PS_TABLE_CACHE: | |
| return _PS_TABLE_CACHE[key] | |
| result = _build_ps_table_1d(s_fixed, P_range_MPa, fluid, n) | |
| _PS_TABLE_CACHE[key] = result | |
| return result | |
| # ── SS304 thermal conductivity (NIST Cryogenic Materials Database) ─────────── | |
| _SS304_T = [4, 6, 8, 10, 15, 20, 30, 40, 50, 60, 77, 100, 150, 200, 250, 300] | |
| _SS304_K = [0.3, 0.5, 0.75, 1.0, 1.8, 2.6, 3.9, 5.0, 6.0, 6.9, 8.0, 9.5, 11.5, 13.0, 14.5, 15.5] | |
| def tc_ss304(T_K: float) -> float: | |
| """Thermal conductivity of SS304 stainless steel [W/m/K]. | |
| Piecewise linear interpolation of NIST cryogenic data. | |
| Valid range: 4–300 K. Clamps to endpoints outside range. | |
| """ | |
| return float(np.interp(T_K, _SS304_T, _SS304_K)) | |
| def ICV_open(Pexit_barg: float, speed_f: float, Ptank_barg: float, | |
| Psat_barg: float, ICVparam: List[float], DCVparam: List[float], | |
| pump_geom: List[float], proc_param: List[float], | |
| fluid: str = "h2", prtMode: int = 0, | |
| flash_eff: float = 0.0 | |
| ) -> Tuple[np.ndarray, Dict[str, Any]]: | |
| """AbstractState-accelerated pump cycle simulation. | |
| Identical interface, output, and numerical values to cycle2mdot_cached.ICV_open() | |
| when flash_eff=0.0. Uses CoolProp's low-level AbstractState API to batch | |
| property queries. | |
| Parameters added (VBA Pack 3): | |
| flash_eff : float | |
| Film boiling heat transfer efficiency for thermal mass effect (0–1). | |
| Default 0.0 disables thermal mass for backward compatibility. | |
| Typical value: 0.02 (2%). | |
| """ | |
| t_start = time.time() | |
| # Create AbstractState object — reused for all CoolProp calls in the loop | |
| fluid_cp = coolprop_fluid_name(fluid) | |
| AS = CoolProp.AbstractState('HEOS', fluid_cp) | |
| # Dynamic critical point — works for H2, N2, or any CoolProp fluid | |
| _fluid_Tc = AS.T_critical() # K (H2: 33.145, N2: 126.21) | |
| _fluid_Pc = AS.p_critical() # Pa (H2: 1.2964e6, N2: 3.396e6) | |
| # ------------------------------------------------------------------ unpack | |
| ICVport_mm = ICVparam[0] | |
| ICVmass_g = ICVparam[1] | |
| ICVtravel_mm = ICVparam[2] | |
| ICVdpArea_mm2 = ICVparam[3] | |
| ICVFs_N = ICVparam[4] | |
| ICVSC_Npmm = ICVparam[5] | |
| ICVleakKv = ICVparam[6] | |
| ICVcomp_eff = ICVparam[7] | |
| ICV_Npts = ICVparam[8] | |
| DCVport_mm = DCVparam[0] | |
| DCVmass_g = DCVparam[1] | |
| DCVtravel_mm = DCVparam[2] | |
| DCVdpArea_mm2 = DCVparam[3] | |
| DCVFs_N = DCVparam[4] | |
| DCVSC_Npmm = DCVparam[5] | |
| DCVleakKv = DCVparam[6] | |
| DCVcomp_eff = DCVparam[7] | |
| DCV_Npts = DCVparam[8] | |
| bore_mm = pump_geom[0] | |
| stroke_mm = pump_geom[1] | |
| HousingOD_mm = pump_geom[2] | |
| ChamberLen_mm = pump_geom[3] | |
| em_housing = pump_geom[4] | |
| em_shield = pump_geom[5] | |
| kvoid = pump_geom[6] | |
| khousing = pump_geom[7] | |
| Vfvoid = pump_geom[8] | |
| Vacuum_micron = pump_geom[9] | |
| design_cpm = pump_geom[10] | |
| dvf = pump_geom[11] | |
| Tamb_K = proc_param[0] | |
| htc_amb = proc_param[1] | |
| NetDriveCouplerForce_kgf = proc_param[2] | |
| F_multiplier = proc_param[3] | |
| Kv_BB = proc_param[4] | |
| Pbbexit_barg = proc_param[5] | |
| fric2chamber = proc_param[6] | |
| Exp_eff = proc_param[7] | |
| # ---------------------------------------------------------------- geometry | |
| stroke = stroke_mm / 1000.0 | |
| bore = bore_mm / 1000.0 | |
| Vdisp = PI / 4.0 * bore**2 * stroke | |
| V_dead = dvf * Vdisp | |
| pump_cpm = design_cpm * speed_f | |
| tcycle = 60.0 / pump_cpm | |
| tstroke = tcycle / 2.0 | |
| vm_piston = PI * stroke * pump_cpm / 60.0 | |
| # --------------------------------------------------------- thermodynamics | |
| # Pre-loop: use refprop (one-time cost, matches original exactly) | |
| PsMPa = Psat_barg / 10.0 + 0.101325 | |
| PtMPa = Ptank_barg / 10.0 + 0.101325 | |
| PeMPa = Pexit_barg / 10.0 + 0.101325 | |
| Tin_K = refprop("t", fluid, "pq", "si", PsMPa, 0.0) | |
| den_in = refprop("d", fluid, "pt", "si", PtMPa, Tin_K) | |
| h_in = refprop("h", fluid, "pt", "si", PtMPa, Tin_K) | |
| den_out = mixture_pump_prop("d", Ptank_barg, Pexit_barg, 0.0, DCVcomp_eff, fluid, Tin_K) | |
| h_out = mixture_pump_prop("h", Ptank_barg, Pexit_barg, 0.0, DCVcomp_eff, fluid, Tin_K) | |
| Tout_K = mixture_pump_prop("t", Ptank_barg, Pexit_barg, 0.0, DCVcomp_eff, fluid, Tin_K) | |
| Tout_C = Tout_K - 273.15 | |
| # ------------------------------------------------ property-level P,S flash cache | |
| _props = _get_cached_fluid_props(fluid) | |
| _cf = _props["cf"] | |
| _s_exit = refprop("s", fluid, "pt", "si", PeMPa, Tout_K) | |
| _h1_exit_J = refprop("h", fluid, "pt", "si", PeMPa, Tout_K) * 1000.0 | |
| _s_tank = refprop("s", fluid, "pt", "si", PtMPa, Tin_K) | |
| _h1_tank_J = refprop("h", fluid, "pt", "si", PtMPa, Tin_K) * 1000.0 | |
| # 1D tables for DCV (unchanged from original) | |
| _p2hat_lo = 0.101325 | |
| _p2hat_hi = PeMPa + 1.0 | |
| _dcv1d_P, _dcv1d_h, _dcv1d_d = _build_ps_table_1d_cached( | |
| s_fixed=_s_exit, | |
| P_range_MPa=(_p2hat_lo, _p2hat_hi), | |
| fluid=fluid, | |
| n=2000, | |
| ) | |
| # ---- Flow computation helpers (unchanged for DCV table path) | |
| def _flow_core(p1_barg, p2_barg, h1_J, Kv, d2hat, h2hat_kJ): | |
| if abs(p1_barg - p2_barg) < 0.0001: | |
| return 0.0 | |
| sg = 1.0 | |
| if p2_barg > p1_barg: | |
| sg = -1.0 | |
| h2hat_J = h2hat_kJ * 1000.0 | |
| dh = h1_J - h2hat_J | |
| if dh < 0.0: | |
| dh = 0.0 | |
| mdot_kghr = Kv * d2hat * np.sqrt(1000.0 / 1e5) * np.sqrt(dh) | |
| return sg * mdot_kghr / 60.0 | |
| def _cached_flow_1d(p1_barg, p2_barg, h1_J, Kv, P_grid, h_tbl, d_tbl): | |
| if abs(p1_barg - p2_barg) < 0.0001: | |
| return 0.0 | |
| Phigha = max(p1_barg, p2_barg) + 1.01325 | |
| Plowa = min(p1_barg, p2_barg) + 1.01325 | |
| p2hat_MPa = max(Plowa / 10.0, Phigha * _cf / 10.0) | |
| d2hat = float(np.interp(p2hat_MPa, P_grid, d_tbl)) | |
| h2hat_kJ = float(np.interp(p2hat_MPa, P_grid, h_tbl)) | |
| return _flow_core(p1_barg, p2_barg, h1_J, Kv, d2hat, h2hat_kJ) | |
| # ---- AbstractState-accelerated flow (replaces flow_RF_kgpm for hot loop) | |
| # Pre-compute constants used by flow formula | |
| _rho0_p0_factor = np.sqrt(1000.0 / 1e5 / 1.0) # sqrt(rho0 / 1e5 / p0) | |
| # Critical point constants now set dynamically from AS (see _fluid_Tc, _fluid_Pc above) | |
| def _flow_AS(p1_barg, p2_barg, Tupstream_C, GasKv): | |
| """flow_RF_kgpm using AbstractState — identical values, faster API.""" | |
| if abs(p1_barg - p2_barg) < 0.0001: | |
| return 0.0 | |
| sg = 1.0 | |
| Phigh = p1_barg | |
| Plow = p2_barg | |
| if p2_barg > p1_barg: | |
| Phigh = p2_barg | |
| Plow = p1_barg | |
| sg = -1.0 | |
| Phigha = Phigh + 1.01325 # bara | |
| Plowa = Plow + 1.01325 # bara | |
| T_K = Tupstream_C + 273.15 | |
| P_up_Pa = Phigha * 1e5 | |
| pc_bara = Phigha * _cf | |
| p2hat_bara = max(Plowa, pc_bara) | |
| p2hat_Pa = p2hat_bara * 1e5 | |
| # Specify phase before PT flash to avoid saturation boundary crash | |
| # At T < Tc, the PT flash can land exactly on Psat(T) — specify liquid | |
| if T_K > _fluid_Tc: | |
| AS.specify_phase(CoolProp.iphase_supercritical_gas) | |
| elif P_up_Pa > _fluid_Pc: | |
| AS.specify_phase(CoolProp.iphase_supercritical_liquid) | |
| else: | |
| AS.specify_phase(CoolProp.iphase_liquid) | |
| # Upstream state: single update → h, s | |
| try: | |
| AS.update(CoolProp.PT_INPUTS, P_up_Pa, T_K) | |
| except Exception: | |
| # Near saturation: nudge temperature 0.05K into liquid region and retry | |
| try: | |
| T_nudged = T_K - 0.0015 * _fluid_Tc | |
| AS.update(CoolProp.PT_INPUTS, P_up_Pa, T_nudged) | |
| except Exception: | |
| AS.unspecify_phase() | |
| return 0.0 | |
| h1_J = AS.hmass() # J/kg (CoolProp native units) | |
| s1 = AS.smass() # J/kg/K | |
| d1 = AS.rhomass() # kg/m³ (saved for fallback) | |
| # Specify phase before PS flash to avoid saturation boundary issues | |
| if T_K > _fluid_Tc and p2hat_Pa > _fluid_Pc: | |
| AS.specify_phase(CoolProp.iphase_supercritical_gas) | |
| elif T_K < _fluid_Tc and p2hat_Pa > _fluid_Pc: | |
| AS.specify_phase(CoolProp.iphase_supercritical_liquid) | |
| else: | |
| AS.specify_phase(CoolProp.iphase_liquid) | |
| # Downstream isentropic state: single update → d, h | |
| try: | |
| AS.update(CoolProp.PSmass_INPUTS, p2hat_Pa, s1) | |
| d2hat = AS.rhomass() # kg/m³ | |
| h2hat_J = AS.hmass() # J/kg | |
| except Exception: | |
| # Fallback: incompressible liquid approximation | |
| # Δh ≈ ΔP/ρ — accurate for near-saturation liquid states | |
| d2hat = d1 | |
| h2hat_J = h1_J + (p2hat_bara - Phigha) * 1e5 / d1 | |
| finally: | |
| AS.unspecify_phase() | |
| dh = h1_J - h2hat_J | |
| if dh < 0.0: | |
| dh = 0.0 | |
| mdot_kghr = GasKv * d2hat * _rho0_p0_factor * np.sqrt(dh) | |
| return sg * mdot_kghr / 60.0 | |
| # ---------------------------------------------------- DCV physical params | |
| DCVmass = DCVmass_g / 1000.0 | |
| DCVSC_Npm = DCVSC_Npmm * 1000.0 | |
| DCVtravel = DCVtravel_mm / 1000.0 | |
| DCVdpArea = DCVdpArea_mm2 / 1e6 | |
| tc_Fs_DCV = np.sqrt(2.0 * DCVmass * DCVtravel / max(DCVFs_N, 1e-9)) | |
| # ---------------------------------------------------- ICV physical params | |
| ICVmass = ICVmass_g / 1000.0 | |
| ICVSC_Npm = ICVSC_Npmm * 1000.0 | |
| ICVtravel = ICVtravel_mm / 1000.0 | |
| ICVdpArea = ICVdpArea_mm2 / 1e6 | |
| K_bulk = refprop("bs", fluid, "pt", "si", PtMPa, Tin_K) | |
| tc_Fs_ICV = np.sqrt(2.0 * ICVmass * ICVtravel / max(ICVFs_N, 1e-9)) | |
| # -------------------------------------------------- chamber initial state | |
| mc = V_dead * den_out | |
| hc = h_out | |
| mc0 = mc | |
| vc = Vdisp * dvf | |
| den = den_out | |
| pc = Pexit_barg | |
| yp = 0.0 | |
| kv_DCV = DCVleakKv + kv_from_Cd_and_RO_dia(DCVport_mm) | |
| leak_rate_DCV = _cached_flow_1d(Pexit_barg, pc, _h1_exit_J, kv_DCV, _dcv1d_P, _dcv1d_h, _dcv1d_d) / 60.0 | |
| leak_rate_prev_DCV = leak_rate_DCV | |
| Pexit_MPa = (Pexit_barg + 1.01325) / 10.0 | |
| Tc_K = refprop("t", fluid, "ph", "si", Pexit_MPa, hc) | |
| p_init_Pa = (pc + 1.01325) * 1e5 | |
| uc = hc - p_init_Pa / den / 1000 | |
| # -------------------------------------------------- ICV initial state | |
| t = 0.0 | |
| xip = 0.0; vip = 0.0 | |
| Fdp_ICV = (Ptank_barg - pc) * 1e5 * ICVdpArea | |
| vwave = np.sqrt(K_bulk * 1e6 / den) | |
| v_piston = vm_piston * np.sin(2.0 * PI * t / tcycle) | |
| WHdp = den * v_piston * vwave * ICVdpArea | |
| Fs_ICV = ICVFs_N - ICVSC_Npm * (ICVtravel - xip) | |
| Ftot_ICV = (-Fs_ICV + Fdp_ICV + WHdp) | |
| aip = Ftot_ICV / ICVmass | |
| kv_ICV = ICVleakKv | |
| TupstreamC = (Tout_K if pc > Ptank_barg else Tin_K) - 273.15 | |
| leak_rate_ICV = flow_RF_kgpm(Ptank_barg, pc, TupstreamC, kv_ICV, fluid) / 60.0 | |
| leak_rate_prev_ICV = leak_rate_ICV | |
| # -------------------------------------------------- DCV initial state | |
| xp = 0.0; vp = 0.0 | |
| Fs_DCV = DCVFs_N - DCVSC_Npm * xp | |
| ap = Fs_DCV / DCVmass | |
| # -------------------------------------------------- output accumulators | |
| Fmax_ICV = 0.0 | |
| Fmax_DCV = 0.0 | |
| ICVmax_frac = 0.0 | |
| Fmax_ICV_close = 0.0 | |
| Vmax_ICVopen = 0.0 | |
| m_in = 0.0 | |
| m_out = 0.0 | |
| kWh_retract = 0.0 # accumulated pV work during retract (kJ) | |
| kWh_extend = 0.0 # accumulated pV work during extend (kJ) | |
| retract = True | |
| Extend = False | |
| DCVmoving = True | |
| ICVmoving = False | |
| DCV_ct = 0.0 | |
| DCV_ot = tcycle | |
| ICV_openst = 0.0 | |
| ICV_ct = tcycle | |
| # -------------------------------------------------- adaptive time-step | |
| dt0 = max(tc_Fs_ICV / max(ICV_Npts, 1), tc_Fs_DCV / max(DCV_Npts, 1)) / 10.0 | |
| kv1 = kv_from_Cd_and_RO_dia(ICVport_mm) | |
| dp_icv = ICVFs_N / ICVdpArea / 1e5 | |
| dtmax_ICV = max_dt_s(Ptank_barg, kv1, dp_icv, Ptank_barg, Psat_barg, 1, Vdisp, fluid) / 2.0 | |
| kv1 = kv_from_Cd_and_RO_dia(DCVport_mm) | |
| dp_dcv = DCVFs_N / DCVdpArea / 1e5 | |
| dtmax_DCV = max_dt_s(Pexit_barg, kv1, dp_dcv, Ptank_barg, Psat_barg, 0, Vdisp, fluid) / 2.0 | |
| dtmax = 5e-6 | |
| DCVFs_min = DCVFs_N - DCVSC_Npm * DCVtravel | |
| if (DCVFs_N - DCVFs_min) != 0: | |
| slp = (dt0 - dtmax) / (DCVFs_N - DCVFs_min) | |
| icp = dtmax - slp * DCVFs_min | |
| else: | |
| slp = 0.0 | |
| icp = dt0 | |
| # -------------------------------------------------- radiation/convection geometry | |
| bot = 1.0 / em_housing + (1.0 - em_housing) / em_housing * (bore_mm / HousingOD_mm) | |
| ds = (bore_mm + HousingOD_mm) / 2.0 | |
| bot = bot + 2.0 * (1.0 - em_shield) / em_shield * (bore_mm / ds) | |
| keff = composite_thermal_conductivity(1, kvoid, Vfvoid, khousing) | |
| htc_all = 1.0 / (1.0 / htc_amb + (HousingOD_mm - bore_mm) / 2.0 / 1000.0 / keff) | |
| pa = Vacuum_micron / 1000.0 * (101325.0 / 760.0) | |
| Ffric = NetDriveCouplerForce_kgf * F_multiplier * 9.80665 | |
| Qfric = Ffric * vm_piston * fric2chamber | |
| Qrad = PI * bore_mm * ChamberLen_mm * STEFAN_BOLTZMANN * (Tamb_K**4 - Tc_K**4) / (2.0 * bot) / 1e6 | |
| _Qconv_base = free_conv_2cyl_Wpm(bore_mm / 1000.0, Tc_K, HousingOD_mm / 1000.0, Tamb_K, pa, "air") | |
| _Tc_last_conv = Tc_K | |
| Qconv = -_Qconv_base * (ChamberLen_mm / 1000.0) | |
| Qconv = Qconv + 2.0 * PI / 4.0 * HousingOD_mm**2 * htc_amb * (Tamb_K - Tc_K) / 1e6 | |
| dt = dt0 | |
| Qig = (Qrad + Qconv) * dt / 1000.0 | |
| Qf = Qfric * dt / 1000.0 | |
| # -------------------------------------------------- thermal mass effect (VBA Pack 3) | |
| # Models cyclic heat storage/release in the SS304 chamber wall. | |
| # During retract: wall releases stored heat into cold chamber (+Qtmass) | |
| # During extend: hot gas heats the wall (−Qtmass) | |
| if flash_eff > 0.0: | |
| _theta_max = Tout_K - Tin_K # K, max temp amplitude | |
| _theta_avg = 0.215 * _theta_max # K, avg (21.5% from transient diffusion) | |
| _tc_wall = tc_ss304(Tin_K + _theta_avg) # W/m/K, SS304 at avg T | |
| _rho_wall = 7800.0 # kg/m3 | |
| _cp_wall = 500.0 # J/kg/K | |
| _kappa = _tc_wall / _rho_wall / _cp_wall # m2/s, thermal diffusivity | |
| _delta = 2.6 * math.sqrt(_kappa * tcycle) # m, thermal penetration depth | |
| _a_chamber = 2.0 * (PI / 4.0 * bore**2) + PI * bore * stroke # m2, chamber surface | |
| tmass = (flash_eff * _rho_wall * _cp_wall * _a_chamber | |
| * _delta * _theta_avg / tstroke / 1000.0) # kJ/s | |
| else: | |
| tmass = 0.0 | |
| Qtmass = tmass * dt # kJ (initial, retract→positive) | |
| dm_bb = flow_RF_kgpm(Pbbexit_barg, pc, Tc_K - 273.15, Kv_BB, fluid, 1) / 60.0 * dt | |
| h_bb = hc | |
| pc_last = pc | |
| vc_prev = vc | |
| del_pc = 0.0 | |
| # ------------------------------------------- history storage | |
| hist_t : List[float] = [] | |
| hist_pc : List[float] = [] | |
| hist_den : List[float] = [] | |
| hist_yp : List[float] = [] | |
| hist_mc : List[float] = [] | |
| hist_Tc : List[float] = [] | |
| hist_hc : List[float] = [] | |
| hist_dcvof : List[float] = [] | |
| hist_dcvlk : List[float] = [] | |
| hist_icvof : List[float] = [] | |
| hist_icvlk : List[float] = [] | |
| hist_dmtot : List[float] = [] | |
| j = 1 | |
| # ============================= main integration loop ======================= | |
| while t < tcycle and j < 50000: | |
| if t > tstroke and not Extend: | |
| retract = False | |
| Extend = True | |
| # ---- adaptive dt (UNCHANGED) | |
| ICVopenfr = ICV_openst / tstroke if tstroke > 0 else 0.0 | |
| ICVmovfr = 1.0 - ICVopenfr | |
| n_seg = 5 | |
| df = ICVmovfr / n_seg if ICVmovfr > 0 else 0.2 | |
| xn = (yp - ICVopenfr) / df if df > 0 else 0.0 | |
| if DCVmoving: | |
| dt = minmax(slp * Fs_DCV + icp, dt0, dtmax) | |
| elif ICVmoving: | |
| dt = dt0 * (50.0 ** xn) | |
| dt = minmax(dt, dt0, dtmax * 2.0) | |
| elif (pc - Ptank_barg < 2.0) and not ICVmoving: | |
| dt = dt0 | |
| elif (Pexit_barg - pc < abs(del_pc)) and not DCVmoving: | |
| dt = dt0 | |
| else: | |
| dt = dtmax | |
| t += dt | |
| # ---- chamber heat/mass inputs (UNCHANGED) | |
| dm_DCV = leak_rate_prev_DCV * dt | |
| dm_ICV = leak_rate_prev_ICV * dt | |
| dm_tot = dm_ICV + dm_DCV + dm_bb | |
| m_out += dm_DCV | |
| m_in += dm_ICV | |
| mc_prev = mc | |
| mc = max(mc + dm_tot, mc0 / 1000.0) | |
| dh_DCV = (h_out if leak_rate_prev_DCV > 0 else hc) * dm_DCV | |
| dh_ICV = (h_in if leak_rate_prev_ICV > 0 else hc) * dm_ICV | |
| dh_bb = (h_bb if dm_bb > 0 else hc) * dm_bb | |
| work = (-pc * vc + pc_last * vc_prev) * 100.0 | |
| kWh_retract += work if retract else 0.0 | |
| kWh_extend += work if not retract else 0.0 | |
| uc_prev_step = uc | |
| # ---- cv via AbstractState (T, D) update | |
| # Replaces: refprop("cv", fluid, "td", "si", Tc_K, den) | |
| try: | |
| if Tc_K > _fluid_Tc and (pc + 1.01325) * 1e5 > _fluid_Pc: | |
| AS.specify_phase(CoolProp.iphase_supercritical_gas) | |
| else: | |
| AS.unspecify_phase() | |
| AS.update(CoolProp.DmassT_INPUTS, den, Tc_K) | |
| cv_kJkgK = AS.cvmass() / 1000.0 # J/kg/K → kJ/kg/K | |
| if cv_kJkgK <= 0.0: | |
| cv_kJkgK = 10.0 | |
| except Exception: | |
| cv_kJkgK = 10.0 | |
| finally: | |
| AS.unspecify_phase() | |
| # ---- SACRED energy balance (+ Qtmass from VBA Pack 3) | |
| uc = (uc * mc_prev + (dh_DCV + dh_ICV + dh_bb + Qig + Qf + Qtmass) + work) / mc | |
| yp = 0.5 * (1.0 - np.cos(2.0 * PI * t / tcycle)) | |
| vc_prev = vc | |
| vc = Vdisp * (dvf + yp) | |
| den = mc / vc | |
| # ---- Robust state update via (T, D) flash using AbstractState | |
| # Two-pass refinement, identical logic to original. | |
| # Replaces: 4 × refprop("p"/"h", "td") → 2 × AS.update(DmassT) | |
| try: | |
| dT_est = (uc - uc_prev_step) / cv_kJkgK | |
| T_est = max(Tc_K + dT_est, 14.0) | |
| # Specify phase for supercritical states | |
| if T_est > _fluid_Tc and (pc + 1.01325) * 1e5 > _fluid_Pc: | |
| AS.specify_phase(CoolProp.iphase_supercritical_gas) | |
| else: | |
| AS.unspecify_phase() | |
| AS.update(CoolProp.DmassT_INPUTS, den, T_est) | |
| pc_new = AS.p() / 1e5 - 1.01325 # Pa → barg | |
| hc_new = AS.hmass() / 1000.0 # J/kg → kJ/kg | |
| Tc_new = T_est | |
| # One refinement pass | |
| p_new_Pa = (pc_new + 1.01325) * 1e5 | |
| uc_new_ref = hc_new - p_new_Pa / den / 1000 | |
| dT_ref = (uc - uc_new_ref + (uc - uc_prev_step)) / cv_kJkgK | |
| T_ref = max(Tc_K + dT_ref, 14.0) | |
| if T_ref > _fluid_Tc and (pc_new + 1.01325) * 1e5 > _fluid_Pc: | |
| AS.specify_phase(CoolProp.iphase_supercritical_gas) | |
| else: | |
| AS.unspecify_phase() | |
| AS.update(CoolProp.DmassT_INPUTS, den, T_ref) | |
| pc_new = AS.p() / 1e5 - 1.01325 | |
| hc_new = AS.hmass() / 1000.0 | |
| Tc_new = T_ref | |
| except Exception: | |
| pc_new = pc_last | |
| hc_new = hc | |
| Tc_new = Tc_K | |
| finally: | |
| AS.unspecify_phase() | |
| del_pc = pc_new - pc_last | |
| pc_last = pc_new | |
| pc = pc_new | |
| hc = hc_new | |
| Tc_K = Tc_new | |
| uc = hc - (pc + 1.01325) * 1e5 / den / 1000 | |
| # ---- DCV motion (UNCHANGED) | |
| Fs_DCV = DCVFs_N - DCVSC_Npm * xp | |
| Fdp_DCV = (Pexit_barg - pc) * 1e5 * DCVdpArea | |
| Fmax_DCV = max(Fmax_DCV, Fs_DCV + Fdp_DCV) | |
| ap = (Fs_DCV + Fdp_DCV) / DCVmass | |
| vp += ap * dt | |
| if (xp == 0.0 and vp < 0.0) or (xp == DCVtravel and vp > 0.0): | |
| vp = 0.0 | |
| xp = xp + vp * dt | |
| xp = minmax(xp, 0.0, DCVtravel) | |
| x_frac = minmax(xp / DCVtravel, 0.0, 1.0) | |
| if retract and x_frac >= 1.0 and DCVmoving: | |
| DCVmoving = False | |
| DCV_ct = t | |
| elif Extend and x_frac < 1.0 and not DCVmoving: | |
| DCVmoving = True | |
| DCV_ot = t | |
| kv_DCV = DCVleakKv + kv_from_Cd_and_RO_dia(DCVport_mm * (1.0 - x_frac)) | |
| if Pexit_barg > pc: | |
| leak_rate_DCV = _cached_flow_1d(Pexit_barg, pc, _h1_exit_J, kv_DCV, _dcv1d_P, _dcv1d_h, _dcv1d_d) / 60.0 | |
| else: | |
| # Reverse flow: use AbstractState | |
| TupstreamC = Tc_K - 273.15 | |
| leak_rate_DCV = _flow_AS(Pexit_barg, pc, TupstreamC, kv_DCV) / 60.0 | |
| leak_rate_prev_DCV = leak_rate_DCV | |
| # ---- ICV motion (UNCHANGED) | |
| Fs_ICV = ICVFs_N - ICVSC_Npm * (ICVtravel - xip) | |
| Fdp_ICV = (Ptank_barg - pc) * 1e5 * ICVdpArea | |
| vwave = np.sqrt(K_bulk * 1e6 / max(den, 1e-6)) | |
| v_piston = vm_piston * np.sin(2.0 * PI * t / tcycle) | |
| WHdp = (1.0 if retract else -1.0) * den * v_piston * vwave * ICVdpArea | |
| Ftot_ICV = Fdp_ICV - Fs_ICV + WHdp | |
| Fmax_ICV = max(Fmax_ICV, Ftot_ICV) | |
| Fmax_ICV_close = min(Fmax_ICV_close, Ftot_ICV) | |
| aip = Ftot_ICV / ICVmass | |
| vip += aip * dt | |
| if (xip == 0.0 and vip < 0.0) or (xip == ICVtravel and vip > 0.0): | |
| vip = 0.0 | |
| Vmax_ICVopen = max(Vmax_ICVopen, vip) | |
| xip = xip + vip * dt | |
| xip = minmax(xip, 0.0, ICVtravel) | |
| xi_frac = xip / ICVtravel | |
| if retract and xip > 0.0 and not ICVmoving: | |
| ICVmoving = True | |
| ICV_openst = t | |
| elif Extend and xi_frac == 0.0 and ICVmoving: | |
| ICVmoving = False | |
| ICV_ct = t | |
| ICVmax_frac = max(ICVmax_frac, xi_frac) | |
| kv_ICV = ICVleakKv + kv_from_Cd_and_RO_dia(ICVport_mm * xi_frac) | |
| # ICV flow: AbstractState-accelerated | |
| # Replaces: flow_RF_kgpm (4 PropsSI calls → 2 AS.update calls) | |
| TupstreamC = (Tc_K if pc > Ptank_barg else Tin_K) - 273.15 | |
| leak_rate_ICV = _flow_AS(Ptank_barg, pc, TupstreamC, kv_ICV) / 60.0 | |
| leak_rate_prev_ICV = leak_rate_ICV | |
| # ---- heat ingress, friction, blowby | |
| Qrad = PI * bore_mm * ChamberLen_mm * STEFAN_BOLTZMANN * (Tamb_K**4 - Tc_K**4) / (2.0 * bot) / 1e6 | |
| if abs(Tc_K - _Tc_last_conv) > 2.0: | |
| _Qconv_base = free_conv_2cyl_Wpm(bore_mm / 1000.0, Tc_K, HousingOD_mm / 1000.0, Tamb_K, pa, "air") | |
| _Tc_last_conv = Tc_K | |
| Qconv = -_Qconv_base * (ChamberLen_mm / 1000.0) | |
| Qconv = Qconv + 2.0 * PI / 4.0 * HousingOD_mm**2 * htc_amb * (Tamb_K - Tc_K) / 1e6 | |
| Qig = (Qrad + Qconv) * dt / 1000.0 | |
| Qfric = Ffric * abs(v_piston) * fric2chamber | |
| Qf = Qfric * dt / 1000.0 | |
| Qtmass = (1.0 if retract else -1.0) * tmass * dt # kJ, thermal mass | |
| # Blowby: AbstractState-accelerated | |
| dm_bb = _flow_AS(Pbbexit_barg, pc, Tc_K - 273.15, Kv_BB) / 60.0 * dt | |
| h_bb = hc | |
| if prtMode: | |
| print(f"j={j:5d} angle={t*360/tcycle:6.1f}° pc={pc:8.3f} bar" | |
| f" den={den:8.3f} kg/m³ yp={yp:.4f} mc={mc*1000:.4f} g" | |
| f" DCV x/L={x_frac:.3f} ICV xi/L={xi_frac:.3f}" | |
| f" Tc={Tc_K:.2f} K hc={hc:.3f} kJ/kg") | |
| # ---- record history | |
| hist_t.append(t * 360.0 / tcycle) | |
| hist_pc.append(pc) | |
| hist_den.append(den) | |
| hist_yp.append(yp) | |
| hist_mc.append(mc * 1000.0) | |
| hist_Tc.append(Tc_K) | |
| hist_hc.append(hc) | |
| hist_dcvof.append(1.0 - x_frac) | |
| hist_dcvlk.append(leak_rate_DCV * 60.0) | |
| hist_icvof.append(xi_frac) | |
| hist_icvlk.append(leak_rate_ICV * 60.0) | |
| hist_dmtot.append(dm_tot / dt if dt > 0 else 0.0) | |
| j += 1 | |
| # ============================ post-processing ============================ | |
| mass_eff = m_in / (Vdisp * den_in) | |
| m_out = -m_out | |
| m_out_eff = m_out / (Vdisp * den_in) | |
| mdot_out = m_out / tcycle * 60.0 | |
| # Per-stroke kWh normalization (VBA Pack 3) | |
| kWh_retract_out = kWh_retract / 3600.0 / m_out if m_out > 0 else 0.0 | |
| kWh_extend_out = kWh_extend / 3600.0 / m_out if m_out > 0 else 0.0 | |
| t_end = time.time() | |
| out = np.zeros((15, 2), dtype=object) | |
| out[0, 0] = t_end - t_start; out[0, 1] = "s, cpu time" | |
| out[1, 0] = mass_eff; out[1, 1] = ", mass inflow efficiency" | |
| out[2, 0] = DCV_ct; out[2, 1] = "s, DCV closure time" | |
| out[3, 0] = ICV_openst / tstroke; out[3, 1] = ", stroke fraction ICV starts to open" | |
| out[4, 0] = ICVmax_frac; out[4, 1] = ", max ICV open fraction" | |
| out[5, 0] = Fmax_DCV; out[5, 1] = "N, max closure force on DCV" | |
| out[6, 0] = Fmax_ICV; out[6, 1] = "N, max open force on ICV" | |
| out[7, 0] = Fmax_ICV_close; out[7, 1] = "N, max closure force on ICV" | |
| out[8, 0] = Vmax_ICVopen; out[8, 1] = "m/s, max ICV opening velocity" | |
| out[9, 0] = m_out; out[9, 1] = "kg, total discharged mass per cycle" | |
| out[10, 0] = m_out_eff; out[10, 1] = ", mass efficiency of cycle" | |
| out[11, 0] = DCV_ot / tstroke - 1.0; out[11, 1] = ", stroke fraction DCV starts to open" | |
| out[12, 0] = ICV_ct - tstroke; out[12, 1] = "s, ICV closure time after extend start" | |
| out[13, 0] = kWh_retract_out; out[13, 1] = "kWh/kg, retract pV work" | |
| out[14, 0] = kWh_extend_out; out[14, 1] = "kWh/kg, extend pV work" | |
| history = { | |
| 'angle_deg': np.array(hist_t), | |
| 'pc': np.array(hist_pc), | |
| 'den': np.array(hist_den), | |
| 'yp': np.array(hist_yp), | |
| 'mc_g': np.array(hist_mc), | |
| 'Tc_K': np.array(hist_Tc), | |
| 'hc': np.array(hist_hc), | |
| 'DCV_open_frac': np.array(hist_dcvof), | |
| 'DCV_leak_kgpm': np.array(hist_dcvlk), | |
| 'ICV_open_frac': np.array(hist_icvof), | |
| 'ICV_leak_kgpm': np.array(hist_icvlk), | |
| 'dm_tot_kgps': np.array(hist_dmtot), | |
| 'mdot_kgpm': mdot_out, | |
| 'tcycle_s': tcycle, | |
| 'tstroke_s': tstroke, | |
| 'steps': j, | |
| 'kWh_retract': kWh_retract_out, | |
| 'kWh_extend': kWh_extend_out, | |
| } | |
| return out, history | |
| if __name__ == "__main__": | |
| print("Cryogenic Pump Cycle Analysis - AbstractState-Accelerated (Phase 1)") | |
| print("-" * 60) | |
| ICVparam = [6.0, 28.0, 8.0, 897.7, 11.4, 1.134, 0.01, 1.0, 200] | |
| DCVparam = [8.3, 25.0, 4.0, 78.54, 3.1, 0.419, 0.01, 0.8, 400] | |
| pump_geom = [40.3, 60.0, 120.0, 300.0, 1.0, 1.0, 0.026, 16.0, 0.5, 760000, 500.0, 0.02] | |
| proc_param = [293.0, 10.0, 4.0, 30.0, 0.01, 0.0, 0.5, 0.2] | |
| print("Running AbstractState-accelerated simulation...") | |
| t0 = time.time() | |
| out, hist = ICV_open(700.0, 1.0, 9.0, 2.0, ICVparam, DCVparam, pump_geom, proc_param) | |
| wall = time.time() - t0 | |
| print(f"Wall time: {wall:.2f}s Steps: {hist['steps']} mdot: {hist['mdot_kgpm']:.4f} kg/min") | |
| print_results(out) | |