Coverage for backend/pinch_service/heat_exchanger_profiler/profile_generator.py: 49%

35 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-12-18 04:00 +0000

1import numpy as np 

2import CoolProp.CoolProp as CP 

3from .classes.state_evaluation import StateEvaluation 

4 

5def get_heat_exchanger_length_states( 

6 ppKey: str, 

7 composition: list[tuple[str, float]], 

8 mole_flow: float, 

9 t_supply: float = None, 

10 t_target: float = None, 

11 p_supply: float = None, 

12 p_target: float = None, 

13 h_supply: float = None, 

14 h_target: float = None, 

15 num_points: int = 100, 

16 prev_state: dict = None): 

17 """ 

18 Generates a set of x, y points for a TH curve. 

19 :param: t_supply - Initial temperature of stream (K) 

20 :param: t_target - Final temperature of stream (K) 

21 :param: p_supply - Initial pressure of stream (Pa) 

22 :param: p_target - Final pressure of stream (Pa) 

23 :param: h_supply - Initial molar enthalpy of stream (J/kg) 

24 :param: h_target - Final molar enthalpy of stream (J/kg)  

25 :param: composition - list of Tuple(compound name, molar fraction) 

26 :param: num_points - Number of intervals to evaluate stream at 

27 :param: prev_state - The previous serialised state evaluation for this stream 

28 :returns: Tuple(Heat Flow (W), Temperatures (K)) 

29 """ 

30 # Generate temperature, pressure and enthalpy points 

31 pressures = np.linspace(p_supply, p_target, num_points) 

32 temperatures = None 

33 if ppKey == "peng-robinson": 33 ↛ 34line 33 didn't jump to line 34 because the condition on line 33 was never true

34 h_supply = None 

35 if h_supply is not None: 35 ↛ 38line 35 didn't jump to line 38 because the condition on line 35 was always true

36 molar_enthalpies = np.linspace(h_supply, h_target, num_points) 

37 else: 

38 temperatures = np.linspace(t_supply, t_target, num_points) 

39 

40 heat_flows = np.zeros(num_points) 

41 serialised_states = None 

42 

43 # Evaluate total energy (heat) flow or temperature depending on the selected independent variable 

44 try: 

45 if ppKey in ["humid_air", "milk", "helmholtz", "peng-robinson"]: 45 ↛ 55line 45 didn't jump to line 55 because the condition on line 45 was always true

46 if h_supply is not None: 46 ↛ 50line 46 didn't jump to line 50 because the condition on line 46 was always true

47 state = StateEvaluation(ppKey, composition, mole_flow=mole_flow, pressure_ls=pressures, molar_enthalpy_ls=molar_enthalpies, prev_states_sol=prev_state) 

48 temperatures = state.get_temperature() 

49 else: 

50 state = StateEvaluation(ppKey, composition, mole_flow=mole_flow, pressure_ls=pressures, temperature_ls=temperatures, prev_states_sol=prev_state) 

51 

52 heat_flows = state.get_total_energy_flow() 

53 serialised_states = state.serialize_states() 

54 else: 

55 mixture_string = "HEOS::" + "&".join([f"{comp}[{frac}]" for comp, frac in composition]) 

56 if temperatures is not None: 

57 molar_enthalpies = np.zeros(num_points) 

58 for i in range(len(temperatures)): 

59 molar_enthalpies[i] = CP.PropsSI('HMOLAR', 'P', pressures[i], 'T', temperatures[i], mixture_string) 

60 heat_flows[i] = molar_enthalpies[i] * mole_flow 

61 else: 

62 temperatures = np.zeros(len(molar_enthalpies)) 

63 for i in range(len(molar_enthalpies)): 

64 temperatures[i] = CP.PropsSI('T', 'P', pressures[i], 'HMOLAR', molar_enthalpies[i], mixture_string) 

65 heat_flows[i] = molar_enthalpies[i] * mole_flow 

66 except: 

67 raise ValueError("Warning: Failed to compute enthalpy for T-h curve.") 

68 

69 t_h_curve_points = np.column_stack((heat_flows, temperatures)).tolist() 

70 return t_h_curve_points, serialised_states