ode-pump-dashboard / engine_bridge.py
pandeydigant31's picture
Upload folder using huggingface_hub
ed65aea verified
Raw
History Blame Contribute Delete
3.9 kB
"""
Engine bridge — flat imports for HuggingFace Spaces deployment.
Dispatches to fast, ode_v2, or njit_10var engines.
"""
import os
import numpy as np
from typing import Optional, Tuple, List, Dict, Any
from cycle2mdot_fast import ICV_open as ICV_open_fast
_HAS_ODE_V2 = False
try:
from cycle2mdot_ode_v2 import ODE_driver_v2
_HAS_ODE_V2 = True
except ImportError:
pass
_HAS_NJIT = False
try:
from cycle2mdot_10var_njit import ICV_open_10var
_HAS_NJIT = True
except ImportError:
pass
ENGINES = ['fast']
if _HAS_ODE_V2:
ENGINES.append('ode_v2')
if _HAS_NJIT:
ENGINES.append('njit_10var')
_DEFAULT_ENGINE = 'fast'
def _select_engine(engine_name, fluid='h2'):
if engine_name == 'njit_10var' and fluid.lower() not in ('h2', 'hydrogen'):
return 'ode_v2' if _HAS_ODE_V2 else 'fast'
return engine_name
def cycle_sim(Pexit_barg, speed_f, Ptank_barg, Psat_barg,
ICVparam, DCVparam, pump_geom, proc_param,
fluid="h2", prtMode=0, flash_eff=0.0,
exit_param=None, _init_ds=None, engine=None):
if engine is None:
engine = os.environ.get('DASHBOARD_ENGINE', _DEFAULT_ENGINE)
engine = _select_engine(engine, fluid)
if engine == 'njit_10var':
out, hist = ICV_open_10var(
Pexit_barg, speed_f, Ptank_barg, Psat_barg,
ICVparam, DCVparam, pump_geom, proc_param,
fluid=fluid, prtMode=prtMode, flash_eff=flash_eff,
exit_param=exit_param, n_cycles=1)
hist['engine'] = 'njit_10var'
elif engine == 'ode_v2':
out, hist = ODE_driver_v2(
Pexit_barg, speed_f, Ptank_barg, Psat_barg,
ICVparam, DCVparam, pump_geom, proc_param,
fluid=fluid, prtMode=prtMode, flash_eff=flash_eff,
exit_param=exit_param, n_cycles=1)
hist['engine'] = 'ode_v2'
else:
out, hist = ICV_open_fast(
Pexit_barg, speed_f, Ptank_barg, Psat_barg,
ICVparam, DCVparam, pump_geom, proc_param,
fluid=fluid, prtMode=prtMode, flash_eff=flash_eff)
hist['engine'] = 'fast'
if out.shape[0] < 15:
out_new = np.zeros((15, 2), dtype=object)
out_new[:out.shape[0], :] = out
out = out_new
return out, hist
def multi_cycle_sim(num_cycles, Pexit_barg, speed_f, Ptank_barg, Psat_barg,
ICVparam, DCVparam, pump_geom, proc_param,
fluid="h2", prtMode=0, flash_eff=0.0,
exit_param=None, engine=None):
"""Multi-cycle simulation. Dispatches to engine with n_cycles support."""
if engine is None:
engine = os.environ.get('DASHBOARD_ENGINE', _DEFAULT_ENGINE)
engine = _select_engine(engine, fluid)
if engine == 'njit_10var':
out, hist = ICV_open_10var(
Pexit_barg, speed_f, Ptank_barg, Psat_barg,
ICVparam, DCVparam, pump_geom, proc_param,
fluid=fluid, prtMode=prtMode, flash_eff=flash_eff,
exit_param=exit_param, n_cycles=num_cycles)
hist['engine'] = 'njit_10var'
elif engine == 'ode_v2':
out, hist = ODE_driver_v2(
Pexit_barg, speed_f, Ptank_barg, Psat_barg,
ICVparam, DCVparam, pump_geom, proc_param,
fluid=fluid, prtMode=prtMode, flash_eff=flash_eff,
exit_param=exit_param, n_cycles=num_cycles)
hist['engine'] = 'ode_v2'
else:
for c in range(num_cycles):
out, hist = ICV_open_fast(
Pexit_barg, speed_f, Ptank_barg, Psat_barg,
ICVparam, DCVparam, pump_geom, proc_param,
fluid=fluid, prtMode=prtMode, flash_eff=flash_eff)
hist['engine'] = 'fast'
hist['n_cycles'] = num_cycles
if out.shape[0] < 15:
out_new = np.zeros((15, 2), dtype=object)
out_new[:out.shape[0], :] = out
out = out_new
return out, hist