File size: 3,904 Bytes
ed65aea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
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