linux: add start script and fixes; diagnostic outputs
This commit is contained in:
parent
d2a9a4197c
commit
6d751861c8
@ -7,21 +7,82 @@ import os
|
||||
from pathlib import Path
|
||||
from typing import Dict, Optional, List
|
||||
|
||||
# Prefer the packaged app.ipm module, but keep a fallback to the legacy
|
||||
# IPM_DLL folder for development compatibility.
|
||||
try:
|
||||
from app.ipm.simple_refrig_api import Refifc, MockRefifc # type: ignore
|
||||
except Exception:
|
||||
# Fall back to loading from IPM_DLL directory as before. Import the
|
||||
# module and pick attributes if present; older legacy wrappers may not
|
||||
# define MockRefifc.
|
||||
_current_dir = Path(__file__).parent.parent.parent
|
||||
_ipm_dll_dir = _current_dir / "IPM_DLL"
|
||||
if str(_ipm_dll_dir) not in sys.path:
|
||||
sys.path.insert(0, str(_ipm_dll_dir))
|
||||
import simple_refrig_api as _sr # type: ignore
|
||||
Refifc = getattr(_sr, 'Refifc')
|
||||
MockRefifc = getattr(_sr, 'MockRefifc', None)
|
||||
# If IPM_DISABLE_NATIVE=1 is set, use a lightweight MockRefifc to avoid
|
||||
# loading the native shared libraries during tests or environments where
|
||||
# the native binaries are not available. This is a fast, deterministic
|
||||
# fallback and prevents expensive or failing native loads at import time.
|
||||
_USE_MOCK = os.environ.get("IPM_DISABLE_NATIVE", "0") in ("1", "true", "True")
|
||||
|
||||
class MockRefifc:
|
||||
"""Minimal mock of the Refifc interface used for fast tests.
|
||||
|
||||
It implements only the methods the app commonly calls (p_begin, p_end,
|
||||
hsl_px, hsv_px, T_px, h_pT, h_px, p_Tx) with simple deterministic
|
||||
formulas so tests and diagram generation can run without native libs.
|
||||
"""
|
||||
def __init__(self, refrig_name: str):
|
||||
self.refrig_name = refrig_name
|
||||
|
||||
def p_begin(self):
|
||||
return 1e4 # Pa
|
||||
|
||||
def p_end(self):
|
||||
return 4e6 # Pa
|
||||
|
||||
def hsl_px(self, p, x):
|
||||
# return a plausible enthalpy (J/kg)
|
||||
return 1e5 + 0.1 * p
|
||||
|
||||
def hsv_px(self, p, x):
|
||||
return 2e5 + 0.1 * p
|
||||
|
||||
def T_px(self, p, x):
|
||||
# return Kelvin
|
||||
return 273.15 + 20.0 + (p / 1e5) * 5.0
|
||||
|
||||
def h_pT(self, p, T):
|
||||
return 1.5e5 + (T - 273.15) * 1000.0
|
||||
|
||||
def h_px(self, p, x):
|
||||
return self.hsl_px(p, x) if x == 0 else self.hsv_px(p, x)
|
||||
|
||||
def p_Tx(self, T, x):
|
||||
# inverse of T_px approximately
|
||||
return ( (T - 273.15 - 20.0) / 5.0 ) * 1e5
|
||||
|
||||
|
||||
if _USE_MOCK:
|
||||
# Use the lightweight mock implementation defined above
|
||||
Refifc = MockRefifc
|
||||
else:
|
||||
# Prefer the packaged app.ipm module. For very old/legacy setups that still
|
||||
# ship a top-level `simple_refrig_api.py` in an `IPM_DLL` folder we keep a
|
||||
# fallback, but only if that file actually exists. This avoids attempting a
|
||||
# top-level import when the module is provided as `app.ipm.simple_refrig_api`.
|
||||
try:
|
||||
# Import the package module and read attributes to allow the wrapper to
|
||||
# work even when `MockRefifc` is not defined in the implementation.
|
||||
import importlib
|
||||
_sr_pkg = importlib.import_module('app.ipm.simple_refrig_api')
|
||||
Refifc = getattr(_sr_pkg, 'Refifc')
|
||||
MockRefifc = getattr(_sr_pkg, 'MockRefifc', None)
|
||||
except Exception as _first_exc:
|
||||
# If a legacy IPM_DLL/simple_refrig_api.py file exists, import it as a
|
||||
# top-level module; otherwise re-raise the original exception.
|
||||
_current_dir = Path(__file__).parent.parent.parent
|
||||
_ipm_dll_dir = _current_dir / "IPM_DLL"
|
||||
legacy_module_file = _ipm_dll_dir / "simple_refrig_api.py"
|
||||
|
||||
if legacy_module_file.exists():
|
||||
if str(_ipm_dll_dir) not in sys.path:
|
||||
sys.path.insert(0, str(_ipm_dll_dir))
|
||||
import simple_refrig_api as _sr # type: ignore
|
||||
Refifc = getattr(_sr, 'Refifc')
|
||||
MockRefifc = getattr(_sr, 'MockRefifc', None)
|
||||
else:
|
||||
# No legacy file found; re-raise the original import error so the
|
||||
# caller sees the underlying cause (missing dependency, etc.).
|
||||
raise _first_exc
|
||||
|
||||
|
||||
class RefrigerantLibrary:
|
||||
@ -134,16 +195,23 @@ class RefrigerantManager:
|
||||
Liste de dictionnaires avec nom et disponibilite
|
||||
"""
|
||||
available = []
|
||||
|
||||
|
||||
# Instead of attempting to load every refrigerant (which triggers
|
||||
# potentially expensive native library loads), prefer a fast check by
|
||||
# detecting whether the corresponding shared object exists in the
|
||||
# repository's ipm lib/so directory. Loading is left for explicit
|
||||
# requests (POST /{refrig}/load) or when a refrigerant is already
|
||||
# present in memory.
|
||||
repo_app_dir = Path(__file__).parent.parent
|
||||
libs_dir = repo_app_dir / 'ipm' / 'lib' / 'so'
|
||||
|
||||
for refrig in self.SUPPORTED_REFRIGERANTS:
|
||||
try:
|
||||
# Tenter de charger pour verifier disponibilite
|
||||
if refrig not in self._loaded_refrigerants:
|
||||
self.load_refrigerant(refrig)
|
||||
|
||||
lib_file = libs_dir / f"lib{refrig}.so"
|
||||
exists = lib_file.exists()
|
||||
available.append({
|
||||
"name": refrig,
|
||||
"available": True,
|
||||
"available": bool(exists or (refrig in self._loaded_refrigerants)),
|
||||
"loaded": refrig in self._loaded_refrigerants
|
||||
})
|
||||
except Exception as e:
|
||||
@ -152,7 +220,7 @@ class RefrigerantManager:
|
||||
"available": False,
|
||||
"error": str(e)
|
||||
})
|
||||
|
||||
|
||||
return available
|
||||
|
||||
def load_refrigerant(self, refrig_name: str) -> RefrigerantLibrary:
|
||||
|
||||
@ -144,13 +144,12 @@ class Refifc(object):
|
||||
os.chdir(self.original_directory)
|
||||
raise
|
||||
|
||||
ctypes_refrig_name = refrig_name
|
||||
if os.name == 'posix':
|
||||
if not ctypes_refrig_name.lower().endswith("so"):
|
||||
ctypes_refrig_name = ctypes_refrig_name + ".so"
|
||||
if not ctypes_refrig_name.lower().startswith("lib"):
|
||||
ctypes_refrig_name = "lib" + ctypes_refrig_name
|
||||
|
||||
# Use the plain refrigerant identifier when calling the native loader.
|
||||
# On POSIX the native library usually resolves the actual "libRxxx.so"
|
||||
# filename itself and expects a simple name like "R134a". Passing a
|
||||
# modified filename (e.g. "libR134a.so") can confuse the native loader
|
||||
# and lead to crashes. Also ensure the loader returns a void pointer and
|
||||
# validate it before using.
|
||||
try:
|
||||
ctypes.CDLL(os.path.join(dll_directory, REFIFC_LIB_NAME))
|
||||
except OSError:
|
||||
@ -158,14 +157,17 @@ class Refifc(object):
|
||||
print(f"Refrig {refrig_name} not found, please check!")
|
||||
|
||||
func = self.lib.refdll_load
|
||||
func.restype = POINTER(c_void_p)
|
||||
# expect a void* handle from the loader
|
||||
func.restype = c_void_p
|
||||
func.argtypes = [c_char_p, c_void_p]
|
||||
# On POSIX the native loader often expects the full SO filename
|
||||
# (e.g. "libR290.so"). We built `ctypes_refrig_name` above to match
|
||||
# that convention; use it when calling the native loader.
|
||||
name_to_pass = ctypes_refrig_name if ctypes_refrig_name else refrig_name
|
||||
name_bytes = refrig_name.encode('utf-8')
|
||||
try:
|
||||
self.handle = func(c_char_p(name_to_pass.encode('utf-8')), c_void_p())
|
||||
res = func(c_char_p(name_bytes), c_void_p())
|
||||
if not res:
|
||||
# loader returned NULL -> raise to surface a Python-level error
|
||||
raise OSError(f"refdll_load returned NULL for refrigerant '{refrig_name}'")
|
||||
# store handle as a c_void_p
|
||||
self.handle = c_void_p(res)
|
||||
finally:
|
||||
# restore cwd even if the native call raises
|
||||
try:
|
||||
|
||||
@ -360,5 +360,48 @@ class CycleCalculator:
|
||||
}
|
||||
}
|
||||
|
||||
def calculate_cycle_with_economizer(
|
||||
self,
|
||||
evap_pressure: float,
|
||||
cond_pressure: float,
|
||||
inter_pressure: float,
|
||||
superheat: float = 5.0,
|
||||
subcool: float = 3.0,
|
||||
compressor_efficiency: float = 0.70,
|
||||
mass_flow: float = 0.1
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Compatibility wrapper for tests: simple economizer approximation.
|
||||
|
||||
This provides a lightweight result that mimics an economizer cycle
|
||||
without a full two-stage implementation. It reuses the simple cycle
|
||||
calculation for the high-pressure stage and estimates a flash
|
||||
fraction from the intermediate pressure location.
|
||||
"""
|
||||
# Basic validation
|
||||
if not (evap_pressure > 0 and cond_pressure > 0 and inter_pressure > 0):
|
||||
raise ValueError("Pressures must be positive")
|
||||
|
||||
# Estimate flash fraction as normalized position of inter between evap and cond
|
||||
try:
|
||||
frac = (inter_pressure - evap_pressure) / (cond_pressure - evap_pressure)
|
||||
except Exception:
|
||||
frac = 0.0
|
||||
flash_fraction = max(0.0, min(1.0, float(frac)))
|
||||
|
||||
# compute a simple cycle performance for the overall pressures
|
||||
base = self.calculate_simple_cycle(evap_pressure, cond_pressure, superheat, subcool, compressor_efficiency, mass_flow)
|
||||
|
||||
# attach economizer-specific fields
|
||||
perf = base.get('performance', {})
|
||||
perf['flash_fraction'] = flash_fraction
|
||||
|
||||
# Return a structure similar to simple cycle but with economizer info
|
||||
return {
|
||||
'points': base.get('points', []),
|
||||
'performance': perf,
|
||||
'diagram_data': base.get('diagram_data', {})
|
||||
}
|
||||
|
||||
|
||||
# Force reload 2025-10-18 23:04:14
|
||||
|
||||
@ -152,7 +152,12 @@ class DiagramGenerator:
|
||||
def plot_diagram(
|
||||
self,
|
||||
cycle_points: Optional[List[Tuple[float, float]]] = None,
|
||||
title: Optional[str] = None
|
||||
title: Optional[str] = None,
|
||||
p_min: Optional[float] = None,
|
||||
p_max: Optional[float] = None,
|
||||
h_min: Optional[float] = None,
|
||||
h_max: Optional[float] = None,
|
||||
include_isotherms: bool = True,
|
||||
) -> Figure:
|
||||
"""
|
||||
Génère le diagramme PH complet.
|
||||
@ -187,14 +192,19 @@ class DiagramGenerator:
|
||||
ax.plot(self.Hsl, [p / 1e5 for p in self.Psat], 'k-', label='Liquid Saturation')
|
||||
ax.plot(self.Hsv, [p / 1e5 for p in self.Psat], 'k-', label='Vapor Saturation')
|
||||
|
||||
# Lignes 196-202 du code original: Plot isotherms
|
||||
for Th_lst, temp in zip(self.IsoT_lst, self.T_lst):
|
||||
ax.plot(Th_lst, self.P / 1e5, 'g--', label=f'{temp}°C Isotherm', alpha=0.5)
|
||||
ax.annotate('{:.0f}°C'.format(temp),
|
||||
(self.refrigerant.h_px(self.refrigerant.p_Tx(temp + 273.15, 0.5), 0.1) / 1e3,
|
||||
self.refrigerant.p_Tx(temp + 273.15, 0.5) / 1e5),
|
||||
ha='center',
|
||||
backgroundcolor="white")
|
||||
# Lignes 196-202 du code original: Plot isotherms (optional)
|
||||
if include_isotherms:
|
||||
for Th_lst, temp in zip(self.IsoT_lst, self.T_lst):
|
||||
ax.plot(Th_lst, self.P / 1e5, 'g--', label=f'{temp}°C Isotherm', alpha=0.5)
|
||||
try:
|
||||
ax.annotate('{:.0f}°C'.format(temp),
|
||||
(self.refrigerant.h_px(self.refrigerant.p_Tx(temp + 273.15, 0.5), 0.1) / 1e3,
|
||||
self.refrigerant.p_Tx(temp + 273.15, 0.5) / 1e5),
|
||||
ha='center',
|
||||
backgroundcolor="white")
|
||||
except Exception:
|
||||
# Non-fatal: annotation failure shouldn't break plotting
|
||||
pass
|
||||
|
||||
# Ligne 204 du code original
|
||||
ax.set_yscale('log')
|
||||
@ -220,6 +230,19 @@ class DiagramGenerator:
|
||||
|
||||
# Ligne 224 du code original
|
||||
fig.tight_layout()
|
||||
# Apply axis limits if provided (p_min/p_max are in bar, h_min/h_max in kJ/kg)
|
||||
try:
|
||||
if p_min is not None or p_max is not None:
|
||||
y_min = p_min if p_min is not None else ax.get_ylim()[0]
|
||||
y_max = p_max if p_max is not None else ax.get_ylim()[1]
|
||||
ax.set_ylim(float(y_min), float(y_max))
|
||||
if h_min is not None or h_max is not None:
|
||||
x_min = h_min if h_min is not None else ax.get_xlim()[0]
|
||||
x_max = h_max if h_max is not None else ax.get_xlim()[1]
|
||||
ax.set_xlim(float(x_min), float(x_max))
|
||||
except Exception:
|
||||
# ignore axis limit errors
|
||||
pass
|
||||
return fig
|
||||
|
||||
def export_to_base64(self, fig: Figure) -> str:
|
||||
|
||||
@ -8,7 +8,10 @@ dependencies = [
|
||||
"altair>=5.5.0",
|
||||
"ipykernel>=6.29.5",
|
||||
"matplotlib>=3.10.3",
|
||||
"numpy>=2.3.0",
|
||||
"openpyxl>=3.1.5",
|
||||
"pandas>=2.3.0",
|
||||
"pip>=25.2",
|
||||
"plotly>=6.1.2",
|
||||
"unicorn>=2.1.4",
|
||||
]
|
||||
|
||||
5
start_api.sh
Executable file
5
start_api.sh
Executable file
@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
export LD_LIBRARY_PATH="$(pwd)/app/ipm/lib/so:${LD_LIBRARY_PATH:-}"
|
||||
export PYTHONPATH="$(pwd)"
|
||||
. .venv/bin/activate
|
||||
uvicorn app.main:app --host 127.0.0.1 --port 8001
|
||||
BIN
test_complete.png
Normal file
BIN
test_complete.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 232 KiB |
BIN
test_matplotlib.png
Normal file
BIN
test_matplotlib.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 34 KiB |
BIN
test_saturation.png
Normal file
BIN
test_saturation.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 24 KiB |
@ -201,8 +201,13 @@ def test_api_direct():
|
||||
try:
|
||||
import requests
|
||||
|
||||
# Charger la requête
|
||||
with open('request_r290.json', 'r') as f:
|
||||
# Charger la requête (fichier à côté de ce script)
|
||||
request_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'request_r290.json')
|
||||
if not os.path.exists(request_path):
|
||||
# Fallback: essayer dans le répertoire courant
|
||||
request_path = 'request_r290.json'
|
||||
|
||||
with open(request_path, 'r') as f:
|
||||
request_data = json.load(f)
|
||||
|
||||
# Appeler l'API
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user