import numpy as np import pandas as pd from IPM_DLL.simple_refrig_api import Refifc class RefProperties(object): """ Class to define and handle the DLL calls :param RERFRIG: Refrigerant type :type RERFRIG: str """ def __init__(self, RERFRIG): """Constructor method""" self.refrig = Refifc(RERFRIG) def T_px(self, p, x): """ Returns temperature given pressure and quality, along with additional properties """ temp = self.refrig.T_px(p, x) return self.calculate_all_properties(p=p, T=temp, x=x) def H_pT(self, p, t): """ Returns enthalpy given pressure and temperature, along with additional properties """ enthalpy = self.refrig.h_pT(p, t) return self.calculate_all_properties(p=p, T=t) def calculate_all_properties(self, p=None, T=None, x=None, h=None): """ Calculates and returns all thermodynamic properties based on given inputs. :param p: Pressure (optional) :param T: Temperature (optional) :param x: Quality (optional) :param h: Enthalpy (optional) :return: Dictionary of all thermodynamic properties """ properties = {} if T is not None and x is not None: properties['Temperature (°C)'] = T - 273.15 properties['Pressure (kPa)'] = p * 1e-3 properties['Liquid Density (kg/m³)'] = self.refrig.rhosl_px(p, 0) properties['Vapor Density (kg/m³)'] = self.refrig.rhosv_px(p, 1) properties['Liquid Enthalpy (kJ/kg)'] = self.refrig.hsl_px(p, 0) * 1e-3 properties['Vapor Enthalpy (kJ/kg)'] = self.refrig.hsv_px(p, 1) * 1e-3 properties['Liquid Entropy (kJ/kg·K)'] = self.refrig.ssl_px(p, 0) properties['Vapor Entropy (kJ/kg·K)'] = self.refrig.ssv_px(p, 1) properties['Liquid Cp (kJ/kg·K)'] = self.refrig.Cpsl_px(p, 0) properties['Vapor Cp (kJ/kg·K)'] = self.refrig.Cpsv_px(p, 1) if p is not None and T is not None: properties['Temperature (°C)'] = T - 273.15 properties['Pressure (kPa)'] = p * 1e-3 properties['Enthalpy (kJ/kg)'] = self.refrig.h_pT(p, T) * 1e-3 properties['Density (kg/m³)'] = self.refrig.rho_px(p, T) properties['Entropy (kJ/kg·K)'] = self.refrig.s_px(p, T) properties['Cp (kJ/kg·K)'] = self.refrig.Cp_px(p, T) properties['Cv (kJ/kg·K)'] = self.refrig.CpCv_px(p, T) if p is not None and h is not None: properties['Quality'] = self.refrig.x_ph(p, h) properties['Saturation Temperature (°C)'] = self.refrig.Ts_ph(p, h) - 273.15 return properties def properties_to_dataframe(self, properties_dict): """ Converts a dictionary of properties to a DataFrame. :param properties_dict: Dictionary of properties :type properties_dict: dict :return: DataFrame of properties :rtype: pd.DataFrame """ df = pd.DataFrame([properties_dict]) return df def saturation_table(self, temp_start, temp_end, step): """ Generates a DataFrame of saturation properties based on temperature range and step. Includes temperature, pressure, densities, enthalpies, entropy, and specific heat capacities. :param temp_start: Starting temperature :param temp_end: Ending temperature :param step: Step size for temperature :return: DataFrame with saturation properties """ temperatures = np.arange(temp_start, temp_end, step) pressures, liquid_densities, vapor_densities = [], [], [] liquid_enthalpies, vapor_enthalpies = [], [] liquid_entropies, vapor_entropies = [], [] liquid_cp, vapor_cp = [], [] for temp in temperatures: pressure = self.refrig.p_Tx(temp, 0) pressures.append(pressure * 1e-3) liquid_densities.append(self.refrig.rhosl_px(pressure, 0)) vapor_densities.append(self.refrig.rhosv_px(pressure, 1)) liquid_enthalpies.append(self.refrig.hsl_px(pressure, 0) * 1e-3) vapor_enthalpies.append(self.refrig.hsv_px(pressure, 1) * 1e-3) liquid_entropies.append(self.refrig.ssl_px(pressure, 0)) vapor_entropies.append(self.refrig.ssv_px(pressure, 1)) liquid_cp.append(self.refrig.Cpsl_px(pressure, 0)) vapor_cp.append(self.refrig.Cpsv_px(pressure, 1)) # Create and return DataFrame df = pd.DataFrame({ 'Temperature (°C)': temperatures - 273.15, 'Pressure (kPa)': pressures, 'Liquid Density (kg/m³)': liquid_densities, 'Vapor Density (kg/m³)': vapor_densities, 'Liquid Enthalpy (kJ/kg)': liquid_enthalpies, 'Vapor Enthalpy (kJ/kg)': vapor_enthalpies, 'Liquid Entropy (kJ/kg·K)': liquid_entropies, 'Vapor Entropy (kJ/kg·K)': vapor_entropies, 'Liquid Cp (kJ/kg·K)': liquid_cp, 'Vapor Cp (kJ/kg·K)': vapor_cp }) return df