def termwise_Rsquared(self): keepers = [] values = [] print('%s terms at start' % len(self.N0)) for i in range(len(self.N0)): n = helmholtz.vectord([float(1)]) d = helmholtz.vectord([self.D0[i]]) t = helmholtz.vectord([self.T0[i]]) l = helmholtz.vectord([self.L0[i]]) self.phir = helmholtz.phir_power(n, d, t, l, 0, 0) PPF = self.evaluate_EOS(np.array(list(n))) R2 = rsquared(PPF.p, self.phir.dDeltaV(self.tauV, self.deltaV)) values.append((R2, i)) if R2 > 0.9: keepers.append(i) values, indices = zip(*reversed(sorted(values))) keepers = list(indices[0:30]) self.N0 = self.N0[keepers] self.T0 = self.T0[keepers] self.D0 = self.D0[keepers] self.L0 = self.L0[keepers] print('%s terms at end' % len(self.N0))
def fit(self): # Kill off some not as good terms # self.termwise_Rsquared() # Load up the residual Helmholtz term with parameters n = helmholtz.vectord(self.N0) d = helmholtz.vectord(self.D0) t = helmholtz.vectord(self.T0) l = helmholtz.vectord(self.L0) self.phir = helmholtz.phir_power(n, d, t, l, 1, len(self.N0) - 1) # Solve for the coefficients Nbounds = [(-10, 10) for _ in range(len(self.N0))] tbounds = [(-1, 30) for _ in range(len(self.T0))] print(self.OBJECTIVE(np.array(list(self.N0)))) #self.N = self.N0 #self.N = scipy.optimize.minimize(self.OBJECTIVE, np.array(list(self.N0)), bounds = Nbounds, options = dict(maxiter = 5)).x self.N = scipy.optimize.minimize(self.OBJECTIVE, np.array(list(self.N0)), method='L-BFGS-B', bounds=Nbounds, options=dict(maxiter=100)).x # Write the coefficients to HDF5 file h = h5py.File('fit_coeffs.h5', 'w') grp = h.create_group(self.Ref) grp.create_dataset("n", data=np.array(self.N), compression="gzip") print(self.N) #grp.create_dataset("t", data = np.array(self.N[len(self.N)//2::]), compression = "gzip") h.close()
def evaluate_EOS(self, N): self.phir.n = helmholtz.vectord(N) dDelta = self.phir.dDeltaV(self.tauV, self.deltaV) dTau2 = self.phir.dTau2V(self.tauV, self.deltaV) dDelta2 = self.phir.dDelta2V(self.tauV, self.deltaV) dDelta_dTau = self.phir.dDelta_dTauV(self.tauV, self.deltaV) # Evaluate the pressure p = (self.rho * self.R * self.T) * (1 + self.delta * dDelta) # Evaluate the specific heat at constant volume cv_over_R = -self.tau**2 * (self.d2phi0_dTau2 + dTau2) cv = cv_over_R * self.R # Evaluate the specific heat at constant pressure cp_over_R = cv_over_R + (1.0 + self.delta * dDelta - self.delta * self.tau * dDelta_dTau)**2 / (1 + 2 * self.delta * dDelta + self.delta**2 * dDelta2) cp = cp_over_R * self.R # Evaluate the speed of sound w = np.sqrt(1000 * self.R * self.T * cp_over_R / cv_over_R * (1 + 2 * self.delta * dDelta + self.delta**2 * dDelta2)) class stub: pass PPF = stub() PPF.p = np.array(p, ndmin=1).T PPF.cp = np.array(cp, ndmin=1).T PPF.cv = np.array(cv, ndmin=1).T PPF.w = np.array(w, ndmin=1).T return PPF
def load_data(self): h = h5py.File('T_rho_p.h5', 'r') self.T = h.get(self.Ref + '/T').value self.rho = h.get(self.Ref + '/rho').value self.p = h.get(self.Ref + '/p').value self.cp = h.get(self.Ref + '/cp').value self.cv = h.get(self.Ref + '/cv').value self.speed_sound = h.get(self.Ref + '/speed_sound').value self.tau = self.Tc / self.T self.delta = self.rho / self.rhoc self.tauV = helmholtz.vectord(self.tau) self.deltaV = helmholtz.vectord(self.delta) # Get the derivative d2phi0_dTau2 from the ideal part fitter self.d2phi0_dTau2 = self.IPF.d2phi0_dTau2(self.tau)
def evaluate_EOS(self, N): self.phir.n = helmholtz.vectord(N) dDelta = self.phir.dDeltaV(self.tauV, self.deltaV) dTau2 = self.phir.dTau2V(self.tauV, self.deltaV) dDelta2 = self.phir.dDelta2V(self.tauV, self.deltaV) dDelta_dTau = self.phir.dDelta_dTauV(self.tauV, self.deltaV) # Evaluate the pressure p = (self.rho * self.R * self.T) * (1 + self.delta * dDelta) # Evaluate the specific heat at constant volume cv_over_R = -self.tau**2 * (self.d2phi0_dTau2 + dTau2) cv = cv_over_R * self.R # Evaluate the specific heat at constant pressure cp_over_R = cv_over_R + ( 1.0 + self.delta * dDelta - self.delta * self.tau * dDelta_dTau )**2 / (1 + 2 * self.delta * dDelta + self.delta**2 * dDelta2) cp = cp_over_R * self.R # Evaluate the speed of sound w = np.sqrt(1000 * self.R * self.T * cp_over_R / cv_over_R * (1 + 2 * self.delta * dDelta + self.delta**2 * dDelta2)) class stub: pass PPF = stub() PPF.p = np.array(p, ndmin=1).T PPF.cp = np.array(cp, ndmin=1).T PPF.cv = np.array(cv, ndmin=1).T PPF.w = np.array(w, ndmin=1).T return PPF
def load_data(self): h = h5py.File("T_rho_p.h5", "r") self.T = h.get(self.Ref + "/T").value self.rho = h.get(self.Ref + "/rho").value self.p = h.get(self.Ref + "/p").value self.cp = h.get(self.Ref + "/cp").value self.cv = h.get(self.Ref + "/cv").value self.speed_sound = h.get(self.Ref + "/speed_sound").value self.tau = self.Tc / self.T self.delta = self.rho / self.rhoc self.tauV = helmholtz.vectord(self.tau) self.deltaV = helmholtz.vectord(self.delta) # Get the derivative d2phi0_dTau2 from the ideal part fitter self.d2phi0_dTau2 = self.IPF.d2phi0_dTau2(self.tau)