def fit_shomate_species(symbol, T, Cp, H0, S0): """Derives the shomate species from fitting the heat capacity (J/mol/K) and temperature (K) data and including the formation of enthalpy (kJ/mol) and entropy (J/mol/K).""" T_low = min(T) T_high = max(T) t = np.array(T)/1000. [a, pcov] = curve_fit(_shomate_Cp, t, np.array(Cp)) a = list(a) a.extend([0., 0., 0.]) shomate_instance = shomate(symbol = symbol, T_low = T_low, T_high = T_high, a = np.array(a)) a6 = H0 - shomate_instance.get_HoRT(c.T0)*c.R('kJ/mol/K')*c.T0 a7 = S0 - shomate_instance.get_SoR(c.T0)*c.R('J/mol/K') shomate_instance.a[5] = a6 shomate_instance.a[6] = a7 return shomate_instance
def plot_thermo(self, T_low=None, T_high=None, units=None): """ Plots the heat capacity, enthalpy and entropy in the temperature range specified. The units for the plots can be specified by using R """ import matplotlib.pyplot as plt if T_low == None: print "T_low not specified. Using self.T_low attribuate." T_low = self.T_low if T_high == None: print "T_low not specified. Using self.T_high attribuate." T_high = self.T_high T = np.linspace(T_low, T_high) Cp = self.get_CpoR(T) H = self.get_HoRT(T) S = self.get_SoR(T) if units is not None: Cp = Cp * c.R(units) H = H * c.R(units) * T S = S * c.R(units) plt.figure() plt.subplot(311) plt.plot(T, Cp, 'r-') if units is None: plt.ylabel('Cp/R') else: plt.ylabel('Cp (%s)' % units) plt.xlabel('T (K)') plt.title('Plots for %s using NASA polynomials.' % self.symbol) plt.subplot(312) plt.plot(T, H, 'b-') if units is None: plt.ylabel('H/RT') else: plt.ylabel('H (%s)' % units.replace('/K', '')) plt.xlabel('T (K)') #Entropy graph plt.subplot(313) plt.plot(T, S, 'k-') if units is None: plt.ylabel('S/R') else: plt.ylabel('S (%s)' % units) plt.xlabel('T (K)')
def read_fund_csv(symbol, csv_path, print_graph = False): """Reads a csv file for data that will be fed into function fit_shomate_species.""" import matplotlib.pyplot as plt H0_S0_read = False T = [] Cp = [] print "Reading from file: %s" % csv_path with open(csv_path, 'r') as csv_file: for line in csv_file: if line[0] != '!': #Split data and remove unnecessary characters data = line.split(',') T.append(float(data[0])) Cp.append(float(data[1])) if len(data) > 2 and not H0_S0_read: H0 = float(data[2]) S0 = float(data[3]) H0_S0_read = True shomate = fit_shomate_species(symbol, T, Cp, H0, S0) if print_graph: T_range = np.linspace(shomate.T_low, shomate.T_high) Cp_fit = shomate.get_CpoR(T_range)*c.R('J/mol/K') H_fit = shomate.get_HoRT(T_range)*T_range*c.R('kJ/mol/K') S_fit = shomate.get_SoR(T_range)*c.R('J/mol/K') plt.figure() plt.subplot(311) plt.plot(T, Cp, 'ro', T_range, Cp_fit, 'b-') plt.legend(['NIST Data', 'Fit']) plt.xlabel('Temperature (K)') plt.ylabel('Cp (J/mol/K)') plt.subplot(312) plt.plot(T_range, H_fit) plt.xlabel('Temperature (K)') plt.ylabel('H (kJ/mol/K)') plt.subplot(313) plt.plot(T_range, S_fit) plt.xlabel('Temperature (K)') plt.ylabel('S (J/mol/K)') return shomate
def _get_single_SoR(self, T, verbose = True): """Calculates the dimensionless entropy (i.e. S/R) given a temperature.""" t = T/1000. T_arr = np.array([np.log(t), t, t ** 2 / 2., t ** 3 / 3., -0.5 * ( 1 / t ) ** 2, 0., 1., 0.]) if verbose: if T < self.T_low: print "Warning. Input temperature (%f) lower than T_low (%f) for species %s" % (T, self.T_low, self.symbol) elif T > self.T_high: print "Warning. Input temperature (%f) higher than T_high (%f) for species %s" % (T, self.T_high, self.symbol) return np.dot(T_arr, self.a)/c.R('J/mol/K')
def _get_single_CpoR(self, T, verbose = True): """Calculates the heat capacity at constant pressure (i.e. Cp/R) given a temperature.""" t = T/1000. T_arr = np.array([1, t, t ** 2, t ** 3, (1/t) ** 2, 0, 0, 0]) if verbose: if T < self.T_low: print "Warning. Input temperature (%f) lower than T_low (%f) for species %s" % (T, self.T_low, self.symbol) elif T > self.T_high: print "Warning. Input temperature (%f) higher than T_high (%f) for species %s" % (T, self.T_high, self.symbol) return np.dot(T_arr, self.a)/c.R('J/mol/K')
def _get_single_HoRT(self, T, H_correction = False, verbose = True): """Calculates the dimensionless enthalpy (i.e. H/RT) given a temperature.""" t = T/1000. if H_correction: T_arr = np.array([t, t ** 2 / 2, t ** 3 / 3, t ** 4 / 4, -1/t, 1., 0., 1.]) else: T_arr = np.array([t, t ** 2 / 2, t ** 3 / 3, t ** 4 / 4, -1/t, 1., 0., 0.]) if verbose: if T < self.T_low: print "Warning. Input temperature (%f) lower than T_low (%f) for species %s" % (T, self.T_low, self.symbol) elif T > self.T_high: print "Warning. Input temperature (%f) higher than T_high (%f) for species %s" % (T, self.T_high, self.symbol) return np.dot(T_arr, self.a)/(c.R('kJ/mol/K')*T)