def isentropic_at_fixed_quality(s, quality, T_initial=None): """ Returns None if solution cannot be found Returns the T, P, rhoV, rhoL and enthalpy """ # Use IAPWS-84 for initial estimate # Should be very good for IAPWS-95 if T_initial is None: start_T_value = 100.0 + 273.15 else: start_T_value = T_initial def fcn(n, x, fvec, iflag): T_sat = x[0] sL_sat = 4.4514E-08*(T_sat**3) - 6.6115E-05*(T_sat**2) + 4.1971E-02*(T_sat) - 7.4527E+00 sV_sat = -1.1821E-07*(T_sat**3) + 1.7632E-04*(T_sat**2) - 9.4510E-02*(T_sat) + 2.4203E+01 #sL_sat = IAPWS84.specific_entropy_of_saturated_liquid(T)*1e-3 #sV_sat = IAPWS84.specific_entropy_of_saturated_vapor(T)*1e-3 fvec[0] = (s - (quality*sV_sat + (1.0 - quality)*sL_sat)) #print 'T_sat:', T_sat, 'fvec[0]:', fvec[0] solution = dnsqe.dnsqe_nice(fcn, None, [start_T_value], bounds=((273.15, CRITICAL_TEMPERATURE),)) #print 'FROM IAPWS-84:', solution solution_type = solution[3] if int(solution_type) != 1: return None start_T_value = solution[0][0] if start_T_value < 273.16 or start_T_value > CRITICAL_TEMPERATURE: start_T_value = 373.15 def fcn2(n, x, fvec, iflag): T_sat = x[0] #print 'T_sat:', T_sat fcn2.P_value = sat = saturation_pressure_at_fixed_temperature(T_sat) rhoV, rhoL = sat[2], sat[3] sV_sat = entropy(rhoV, T_sat) sL_sat = entropy(rhoL, T_sat) fvec[0] = (s - (quality*sV_sat + (1.0 - quality)*sL_sat)) fcn2.rhoV = rhoV fcn2.rhoL = rhoL fcn2.sV = sV_sat fcn2.sL = sL_sat solution = dnsqe.dnsqe_nice(fcn2, None, [start_T_value], bounds=((273.15, CRITICAL_TEMPERATURE),)) solution_type = solution[3] if int(solution_type) != 1: return None T_value = solution[0][0] return T_value, fcn2.P_value, fcn2.rhoV, fcn2.rhoL, fcn2.sV, fcn2.sL
def solve(self, x, f, bounds=None): f_nle_string = self.generate_nle() exec(f_nle_string) x, f, fnorm, info_code, info_mesg = dnsqe_nice(f_nle, None, x, bounds=bounds) i = 0 keys = self.Variables.keys() for i in xrange(0, len(x)): self.Variables[keys[i]] = x[i] self.fnorm = fnorm; #print 'Norm of function residual vector:', fnorm self.info = info_code, info_mesg #print 'Info:', info_code #print 'Message:', info_mesg ret = '' if self.info[0] == 1: ret = ret + "Most recent solution:\n" ret = ret + "---------------------\n" ret = ret + "Solution norm: " + str(self.fnorm) + "\n" ret = ret + pretty_variables(self.Variables) return '\n' + ret + '\n' else: ret = ret + self.info[1] ret = ret + "---------------------\n" ret = ret + "Solution norm: " + str(self.fnorm) + "\n" ret = ret + pretty_variables(self.Variables) return '\n' + ret + '\n'
def density_at_fixed_pressure_and_temperature(T, P, rho_initial=None): """ Returns None if solution cannot be found Returns the T, P, rho """ T_value = T P_value = P if rho_initial is None: if T_value >= CRITICAL_TEMPERATURE or P_value >= CRITICAL_PRESSURE: rho_initial = IAPWS84.density_of_saturated_vapor(CRITICAL_TEMPERATURE - 10) #rho_initial = CRITICAL_DENSITY + 1 else: P_sat_value, rhoV_value, rhoL_value = saturation_pressure_at_fixed_temperature(T_value)[1:] if P_value > P_sat_value: rho_initial = rhoL_value else: rho_initial = rhoV_value start_rho_value = rho_initial def fcn(n, x, fvec, iflag): rho_guess = x[0] fvec[0] = ((pressure(rho_guess, T_value) - P_value))#/P_value) #print 'fvec[0]:', fvec[0], 'fvec[1]:', fvec[1] solution = dnsqe.dnsqe_nice(fcn, None, [start_rho_value], bounds=((0, 1e10),)) #print solution solution_type = solution[3] if int(solution_type) != 1: return None rho_value = solution[0][0] return T_value, pressure(rho_value, T_value), rho_value
def isentropic_at_fixed_pressure(P, s, T_initial=None): """ Returns None if solution cannot be found Returns the T, P, rho and entropy """ if T_initial is None: if P * 1e-3 > IAPWS84.IAPWS84_CRITICAL_PRESSURE - 10: start_T_value = CRITICAL_TEMPERATURE - 1.0 else: start_T_value = IAPWS84.saturation_temperature(P*1e-3) else: start_T_value = T_initial def fcn(n, x, fvec, iflag): T_guess = x[0] if hasattr(fcn, 'last_solution'): last_rho = fcn.last_solution[2] else: last_rho = None solution = isentropic_at_fixed_temperature(T_guess, s, rho_initial=last_rho) #print solution fvec[0] = (P - solution[1]) fcn.T_value = solution[0] fcn.P_value = solution[1] fcn.rho_value = solution[2] fcn.s_value = solution[3] solution = dnsqe.dnsqe_nice(fcn, None, [start_T_value], bounds=((1e-10, 1e100),)) solution_type = solution[3] if int(solution_type) != 1: return None return fcn.T_value, fcn.P_value, fcn.rho_value, fcn.s_value
def temperature_at_fixed_pressure_and_density(P, rho, T_initial=None): """ Returns None if solution cannot be found Returns the T, P and rho """ if T_initial is None: if P * 1e-3 > IAPWS84.IAPWS84_CRITICAL_PRESSURE - 1.0: start_T_value = IAPWS84.IAPWS84.CRITICAL_TEMPERATURE - 1.0 start_rho_value = IAPWS84.density_of_saturated_liquid(start_T_value) else: start_T_value = IAPWS84.saturation_temperature(P*1e-3) else: start_T_value = T_initial def fcn(n, x, fvec, iflag): T_guess = x[0] fvec[0] = ((pressure(rho, T_guess) - P))#/P_value) #print 'fvec[0]:', fvec[0], 'fvec[1]:', fvec[1] solution = dnsqe.dnsqe_nice(fcn, None, [start_T_value]) #print solution solution_type = solution[3] if int(solution_type) != 1: return None T_value = solution[0][0] rho_value = rho return T_value, pressure(rho_value, T_value), rho_value
def saturation_temperature_at_fixed_pressure(P): """ Returns None if solution cannot be found Returns the T, P, rho_Vapor, rho_Liquid """ #This is actually a very good guess of the saturation temperature #If we get a None, that means there's no solution T_value = IAPWS84.saturation_temperature(P*1e-3) if T_value is None: return None rhoV_value = IAPWS84.density_of_saturated_vapor(T_value) rhoL_value = IAPWS84.density_of_saturated_liquid(T_value) P_value = P def fcn (n, x, fvec, iflag): x0 = x[0] x1 = x[1] x2 = x[2] res = phase_equilibrium(x0, x1, x2, P_value) fvec[0] = res[0] fvec[1] = res[1] fvec[2] = res[2] solution = dnsqe.dnsqe_nice(fcn, None, [rhoV_value, rhoL_value, T_value], bounds=((1e-10, 1e10),(1e-10, 1e10), (1e-10, 1e10))) solution_type = solution[3] if int(solution_type) != 1: return None rhoV_value, rhoL_value, T_value = solution[0] return T_value, P_value, rhoV_value, rhoL_value
def saturation_pressure_at_fixed_temperature(T): """ Returns None if solution cannot be found Returns the T, P, rho_Vapor and rho_Liquid. """ if T > CRITICAL_TEMPERATURE: return None rhoV_value = IAPWS84.density_of_saturated_vapor(T) rhoL_value = IAPWS84.density_of_saturated_liquid(T) P_value_temp = IAPWS84.vapor_pressure(T) T_value = T def fcn (n, x, fvec, iflag): x0 = x[0] x1 = x[1] x2 = x[2] res = phase_equilibrium(x0, x1, T_value, x2) fvec[0] = res[0] fvec[1] = res[1] fvec[2] = res[2] solution = dnsqe.dnsqe_nice(fcn, None, [rhoV_value, rhoL_value, P_value_temp], bounds=((1e-10, 1e10),(1e-10, 1e10), (1e-10, 1e100))) solution_type = solution[3] if int(solution_type) != 1: return None rhoV_value, rhoL_value, P_value_sat = solution[0] return T, P_value_sat, rhoV_value, rhoL_value
def isentropic_at_fixed_temperature(T, s, rho_initial=None): """ Returns None if solution cannot be found Returns the T, P, rho and enthalpy """ s_value = s if rho_initial is None: if T < CRITICAL_TEMPERATURE: T_sat = T sL_sat = IAPWS84.specific_entropy_of_saturated_liquid(T_sat)*1e-3 sV_sat = IAPWS84.specific_entropy_of_saturated_vapor(T_sat)*1e-3 rhoV_sat = IAPWS84.density_of_saturated_vapor(T_sat) rhoL_sat = IAPWS84.density_of_saturated_liquid(T_sat) #print 'T_sat:', T_sat #print 'hL_sat:', sL_sat, 'hV_sat:', sV_sat #print 'rhoV_sat:', rhoV_sat, 'rhoL_sat:', rhoL_sat #print '(sL_sat - s):', (sL_sat - s) #print '(sV_sat - s):', (sV_sat - s) if abs(sL_sat - s) <= abs(sV_sat - s): start_rho_value = rhoL_sat else: start_rho_value = rhoV_sat #start_rho_value = rhoV_sat else: T_sat = CRITICAL_TEMPERATURE start_rho_value = sL_sat = IAPWS84.specific_entropy_of_saturated_liquid(T_sat) else: start_rho_value = rho_initial def jac(n, x, fvec, fjac, ldfjac, iflag): delta = x[0] tau = CRITICAL_TEMPERATURE/T fjac[0][0] = tau*(RES_phi_delta_tau(delta, tau)) - RES_phi_delta(delta, tau) # Derivative of first eqn wrt to first var def fcn(n, x, fvec, iflag): #print 'x:', x delta = x[0] tau = CRITICAL_TEMPERATURE/T fvec[0] = ((entropy_raw(delta, tau) - s_value/SPECIFIC_GAS_CONSTANT))#/h_value) T_value = T solution = dnsqe.dnsqe_nice(fcn, jac, [start_rho_value/CRITICAL_DENSITY], bounds=((1e-10, 1e10),)) solution_type = solution[3] if int(solution_type) != 1: return None #print solution delta_value = solution[0][0] rho_value = delta_value*CRITICAL_DENSITY return T_value, pressure(rho_value, T_value), rho_value, entropy(rho_value, T_value)
def isenthalpic_at_fixed_pressure(P, h): """ Returns None if solution cannot be found Returns the T, P, rho and enthalpy """ h_value = h P_value = P if P_value * 1e-3 > IAPWS84.IAPWS84_CRITICAL_PRESSURE - 10.0: start_T_value = 373.15 start_rho_value = IAPWS84.density_of_saturated_liquid(start_T_value) elif P_value < 1: start_T_value = 273.16 start_rho_value = IAPWS84.density_of_saturated_vapor(start_T_value) else: T_sat = IAPWS84.saturation_temperature(P*1e-3) hL_sat = IAPWS84.specific_enthalpy_of_saturated_liquid(T_sat)*1e-3 hV_sat = IAPWS84.specific_enthalpy_of_saturated_vapor(T_sat)*1e-3 rhoV_sat = IAPWS84.density_of_saturated_vapor(T_sat) rhoL_sat = IAPWS84.density_of_saturated_liquid(T_sat) #print 'T_sat:', T_sat #print 'hL_sat:', hL_sat, 'hV_sat:', hV_sat #print 'rhoV_sat:', rhoV_sat, 'rhoL_sat:', rhoL_sat #print '(hL_sat - h):', (hL_sat - h) #print '(hV_sat - h):', (hV_sat - h) if abs(hL_sat - h) <= abs(hV_sat - h): start_rho_value = rhoL_sat else: start_rho_value = rhoV_sat start_T_value = T_sat def fcn(n, x, fvec, iflag): rho_guess = x[0] T_guess = x[1] fvec[0] = ((enthalpy(rho_guess, T_guess) - h_value))#/h_value) fvec[1] = ((pressure(rho_guess, T_guess) - P_value))#/P_value) #print 'fvec[0]:', fvec[0], 'fvec[1]:', fvec[1] solution = dnsqe.dnsqe_nice(fcn, None, [start_rho_value, start_T_value], bounds=((1e-10, 1e10), (1e-10, 1e10))) solution_type = solution[3] if int(solution_type) != 1: return None #print solution rho_value, T_value = solution[0] return T_value, pressure(rho_value, T_value), rho_value, enthalpy(rho_value, T_value)
def isenthalpic_at_fixed_temperature(T, h): """ Returns None if solution cannot be found Returns the T, P, rho and enthalpy """ h_value = h last_solution = None start_P_value = 101.325 def fcn(n, x, fvec, iflag): P_guess = x[0] last_solution = isenthalpic_at_fixed_pressure(P_guess, h) fvec[0] = (T - last_solution[0]) fcn.last_solution = last_solution #fvec[1] = ((pressure(rho_guess, T_guess) - P_value))#/P_value) #print 'P_guess:', P_guess, 'fvec[0]:', fvec[0] solution = dnsqe.dnsqe_nice(fcn, None, [start_P_value], bounds=((1e-10, 1e100),)) solution_type = solution[3] if int(solution_type) != 1: return None return fcn.last_solution
def isenthalpic_at_fixed_quality(h, quality, T_initial=None): """ Returns None if solution cannot be found Returns the T, P, rhoV, rhoL and enthalpy """ # Use IAPWS-84 for initial estimate # Should be very good for IAPWS-95 if T_initial is None: start_T_value = 100.0 + 273.15 else: start_T_value = T_initial def fcn(n, x, fvec, iflag): T_sat = x[0] #hL_sat = IAPWS84.specific_enthalpy_of_saturated_liquid(T_sat)*1e-3 #hV_sat = IAPWS84.specific_enthalpy_of_saturated_vapor(T_sat)*1e-3 hL_sat = 1.0554E-07*(T_sat**4) - 1.7366E-04*(T_sat**3) + 1.0645E-01*(T_sat**2) - 2.4465E+01*(T_sat) + 1.7031E+03 hV_sat = -1.7621E-07*(T_sat**4) + 2.8586E-04*(T_sat**3) - 1.7493E-01*(T_sat**2) + 4.9073E+01*(T_sat) - 2.7153E+03 fvec[0] = (h - (quality*hV_sat + (1.0 - quality)*hL_sat)) #print 'T_sat:', T_sat, 'fvec[0]:', fvec[0] solution = dnsqe.dnsqe_nice(fcn, None, [start_T_value], bounds=((1e-10, CRITICAL_TEMPERATURE),)) #print 'FROM IAPWS-84:', solution solution_type = solution[3] if int(solution_type) != 1: return None start_T_value = solution[0][0] if start_T_value < 273.16 or start_T_value > CRITICAL_TEMPERATURE: start_T_value = 373.15 def fcn2(n, x, fvec, iflag): T_sat = x[0] fcn2.P_value = sat = saturation_pressure_at_fixed_temperature(T_sat) rhoV, rhoL = sat[2], sat[3] hV_sat = enthalpy(rhoV, T_sat) hL_sat = enthalpy(rhoL, T_sat) fcn2.rhoV = rhoV fcn2.rhoL = rhoL fcn2.hV_sat = hV_sat fcn2.hL_sat = hL_sat fvec[0] = (h - (quality*hV_sat + (1.0 - quality)*hL_sat)) solution = dnsqe.dnsqe_nice(fcn2, None, [start_T_value], bounds=((273.15, CRITICAL_TEMPERATURE),)) #print 'FROM IAPWS-95:', solution solution_type = solution[3] if int(solution_type) != 1: return None T_value = solution[0][0] rhoV = fcn2.rhoV rhoL = fcn2.rhoL hV = fcn2.hV_sat hL = fcn2.hL_sat return T_value, fcn2.P_value, rhoV, rhoL, hV, hL