def iterate_to_solve_Z(self, T, P, phase) -> float: """ :param T: temperature in K :param P: pressure in Pa :param phase: phase [vapor or liquid] :type phase: str :return: compressibility factor """ beta = self.beta_expr(T, P) q = self.q_expr(T) if phase == 'liquid': Z_nm1 = beta func = self.Z_liquid_RHS elif phase == 'vapor': Z_nm1 = 1. func = self.Z_vapor_RHS else: raise Exception('Phase {} not found!'.format(phase)) Z_n = func(Z_nm1, beta, q) while percent_difference(Z_n, Z_nm1) > self.tol: Z_nm1 = Z_n Z_n = func(Z_nm1, beta, q) return Z_n
def __init__(self, dippr_no: str = None, compound_name: str = None, cas_number: str = None, T_min_fit: float = None, T_max_fit: float = None, n_points_fit: int = 1000, poly_order: int = 2): from scithermo import os, ROOT_DIR file = os.path.join(ROOT_DIR, 'vapor_viscosity.csv') my_header = [ 'Cmpd. no.', 'Name', 'Formula', 'CAS no.', 'Mol. wt. [g/mol]', 'C1', 'C2', 'C3', 'C4', 'Tmin [K]', 'Val at Tmin', 'Tmax [K]', 'Val at Tmax' ] self.dippr_no = dippr_no self.compound_name = compound_name self.cas_number = cas_number found_compound = False self.units = 'Pa*s' with open(file, 'r') as f: header = next(f).rstrip('\n').split(',') assert header == my_header, 'Wrong header!' for line in f: vals = line.rstrip('\n').split(',') if vals[0] == self.dippr_no or vals[ 1] == self.compound_name or vals[3] == self.cas_number: assert not found_compound, 'Input compound found twice in table!' found_compound = True (self.dippr_no, self.compound_name, self.formula, self.cas_number) = vals[:4] (self.MW, self.C1, self.C2, self.C3, self.C4, self.T_min, self.mu_T_min, self.T_max, self.mu_T_max) = map(float, vals[4:]) assert found_compound, 'No compound was found in table! for {}, {}, {}'.format( self.dippr_no, self.compound_name, self.cas_number) # test that values are consistent if percent_difference(self.mu_T_min, self.eval(self.T_min)) > 0.1: raise Exception('Inconsistent data for {}'.format( self.compound_name)) if percent_difference(self.mu_T_max, self.eval(self.T_max)) > 0.1: raise Exception('Inconsistent data for {}'.format( self.compound_name)) self.T_min_fit = T_min_fit self.T_max_fit = T_max_fit if self.T_min_fit is None: self.T_min_fit = self.T_min if self.T_max_fit is None: self.T_max_fit = self.T_max self.T_fit = np.linspace(self.T_min_fit, self.T_max_fit, n_points_fit) assert self.T_min <= self.T_min_fit < self.T_max, 'Minimum temp not in correct range: {} !<= {} !<= {}'.format( self.T_min, self.T_min_fit, self.T_max) assert self.T_min < self.T_max_fit <= self.T_max, 'Max temp not in correct range: {} !< {} !<= {}'.format( self.T_min, self.T_max_fit, self.T_max) self.poly_order = poly_order y = self.eval(self.T_fit) self.coeffs = np.polyfit(self.T_fit, y, self.poly_order) ssxm, ssxym, ssyxm, ssym = np.cov(self.T_fit, y, bias=1).flat self.R2 = ssxym * ssxym / (ssxm * ssym) assert self.R2 > 0.98, 'Poor fit to polynomial, R2 is only %3.2f' % self.R2 self.mu = np.poly1d(self.coeffs)
def test_cp_star_Tmax(self): for i in compounds_to_test: I = CpStar(compound_name=i, T_ref=300., **self.kwargs) self.assertTrue( percent_difference(I.Cp_Tmax, I.eval(I.T_max)) < self.tol)
def test_cp_ig_Tmax(self): for i in compounds_to_test: I = CpIdealGas(compound_name=i, **self.kwargs) self.assertTrue( percent_difference(I.Cp_Tmax, I.eval(I.T_max)) < self.tol)
def get_max_percent_difference(self): """Get largest percent difference""" y_fit = self.Cp_poly(self.T_fit) pd = [percent_difference(i, j) for i, j in zip(y_fit, self.Cp_fit)] return max(pd)
def get_numerical_percent_difference(self): """Calculate the percent difference with numerical integration obtained by :ref:`scipy`""" integral_poly_fit = self.cp_integral(self.T_min_fit, self.T_max_fit) integral_numerical, err_numerical = self.numerical_integration( self.T_min_fit, self.T_max_fit) return percent_difference(integral_poly_fit, integral_numerical)