def fitAll(self, fluidObject=SolutionData()): if fluidObject.Tbase==None: fluidObject.Tbase = (fluidObject.Tmin + fluidObject.Tmax) / 2.0 if fluidObject.xbase==None: fluidObject.xbase = (fluidObject.xmin + fluidObject.xmax) / 2.0 tData = fluidObject.temperature.data xData = fluidObject.concentration.data tBase = fluidObject.Tbase xBase = fluidObject.xbase # Set the standard order for polynomials std_xorder = 3+1 std_yorder = 5+1 std_coeffs = np.zeros((std_xorder,std_yorder)) errList = (ValueError, AttributeError, TypeError, RuntimeError) if fluidObject.density.coeffs == None: try: fluidObject.density.setxyData(tData,xData) fluidObject.density.coeffs = np.copy(std_coeffs) fluidObject.density.type = IncompressibleData.INCOMPRESSIBLE_POLYNOMIAL fluidObject.density.fitCoeffs(tBase,xBase) except errList as ve: if fluidObject.density.DEBUG: print("{0}: Could not fit polynomial {1} coefficients: {2}".format(fluidObject.name,'density',ve)) pass if fluidObject.specific_heat.coeffs == None: try: fluidObject.specific_heat.setxyData(tData,xData) fluidObject.specific_heat.coeffs = np.copy(std_coeffs) fluidObject.specific_heat.type = IncompressibleData.INCOMPRESSIBLE_POLYNOMIAL fluidObject.specific_heat.fitCoeffs(tBase,xBase) except errList as ve: if fluidObject.specific_heat.DEBUG: print("{0}: Could not fit polynomial {1} coefficients: {2}".format(fluidObject.name,'specific heat',ve)) pass if fluidObject.conductivity.coeffs == None: try: fluidObject.conductivity.setxyData(tData,xData) fluidObject.conductivity.coeffs = np.copy(std_coeffs) fluidObject.conductivity.type = IncompressibleData.INCOMPRESSIBLE_POLYNOMIAL fluidObject.conductivity.fitCoeffs(tBase,xBase) except errList as ve: if fluidObject.conductivity.DEBUG: print("{0}: Could not fit polynomial {1} coefficients: {2}".format(fluidObject.name,'conductivity',ve)) pass if fluidObject.viscosity.coeffs == None: try: fluidObject.viscosity.setxyData(tData,xData) tried = False if len(fluidObject.viscosity.yData)==1:# and np.isfinite(fluidObject.viscosity.data).sum()<10: fluidObject.viscosity.coeffs = np.array([+5e+2, -6e+1, +1e+1]) fluidObject.viscosity.type = IncompressibleData.INCOMPRESSIBLE_EXPONENTIAL fluidObject.viscosity.fitCoeffs(tBase,xBase) if fluidObject.viscosity.coeffs==None or IncompressibleFitter.allClose(fluidObject.viscosity.coeffs, np.array([+5e+2, -6e+1, +1e+1])): # Fit failed tried = True if len(fluidObject.viscosity.yData)>1 or tried: #fluidObject.viscosity.coeffs = np.zeros(np.round(np.array(std_coeffs.shape) * 1.5)) fluidObject.viscosity.coeffs = np.copy(std_coeffs) fluidObject.viscosity.type = IncompressibleData.INCOMPRESSIBLE_EXPPOLYNOMIAL fluidObject.viscosity.fitCoeffs(tBase,xBase) except errList as ve: if fluidObject.viscosity.DEBUG: print("{0}: Could not fit polynomial {1} coefficients: {2}".format(fluidObject.name,'viscosity',ve)) pass if fluidObject.saturation_pressure.coeffs == None: try: fluidObject.saturation_pressure.setxyData(tData,xData) tried = False if len(fluidObject.saturation_pressure.yData)==1:# and np.isfinite(fluidObject.saturation_pressure.data).sum()<10: fluidObject.saturation_pressure.coeffs = np.array([-5e+3, +6e+1, -1e+1]) fluidObject.saturation_pressure.type = IncompressibleData.INCOMPRESSIBLE_EXPONENTIAL fluidObject.saturation_pressure.fitCoeffs(tBase,xBase) if fluidObject.saturation_pressure.coeffs==None or IncompressibleFitter.allClose(fluidObject.saturation_pressure.coeffs, np.array([-5e+3, +6e+1, -1e+1])): # Fit failed tried = True if len(fluidObject.saturation_pressure.yData)>1 or tried: #fluidObject.saturation_pressure.coeffs = np.zeros(np.round(np.array(std_coeffs.shape) * 1.5)) fluidObject.saturation_pressure.coeffs = np.copy(std_coeffs) fluidObject.saturation_pressure.type = IncompressibleData.INCOMPRESSIBLE_EXPPOLYNOMIAL fluidObject.saturation_pressure.fitCoeffs(tBase,xBase) except errList as ve: if fluidObject.saturation_pressure.DEBUG: print("{0}: Could not fit polynomial {1} coefficients: {2}".format(fluidObject.name,'saturation pressure',ve)) pass # reset data for getArray and read special files if fluidObject.xid!=fluidObject.ifrac_pure and fluidObject.xid!=fluidObject.ifrac_undefined: if fluidObject.T_freeze.coeffs == None: fluidObject.T_freeze.setxyData([0.0],xData) try: if len(fluidObject.T_freeze.xData)==1:# and np.isfinite(fluidObject.T_freeze.data).sum()<10: fluidObject.T_freeze.coeffs = np.array([+7e+2, -6e+1, +1e+1]) fluidObject.T_freeze.type = IncompressibleData.INCOMPRESSIBLE_EXPONENTIAL else: fluidObject.specific_heat.coeffs = np.copy(std_coeffs) fluidObject.T_freeze.type = IncompressibleData.INCOMPRESSIBLE_EXPPOLYNOMIAL fluidObject.T_freeze.fitCoeffs(tBase,xBase) except errList as ve: if fluidObject.T_freeze.DEBUG: print("{0}: Could not fit {1} coefficients: {2}".format(fluidObject.name,"T_freeze",ve)) pass
def fitFluid(self): if self.Tbase==None: self.Tbase = (self.Tmin + self.Tmax) / 2.0 if self.xbase==None: self.xbase = (self.xmin + self.xmax) / 2.0 std_coeffs = np.zeros((4,6)) errList = (ValueError, AttributeError, TypeError, RuntimeError) try: self.density.coeffs = np.copy(std_coeffs) self.density.type = self.density.INCOMPRESSIBLE_POLYNOMIAL self.density.fitCoeffs(self.Tbase,self.xbase) except errList as ve: if self.density.DEBUG: print("{0}: Could not fit polynomial {1} coefficients: {2}".format(self.name,'density',ve)) pass try: self.specific_heat.coeffs = np.copy(std_coeffs) self.specific_heat.type = self.specific_heat.INCOMPRESSIBLE_POLYNOMIAL self.specific_heat.fitCoeffs(self.Tbase,self.xbase) except errList as ve: if self.specific_heat.DEBUG: print("{0}: Could not fit polynomial {1} coefficients: {2}".format(self.name,'specific heat',ve)) pass try: self.conductivity.coeffs = np.copy(std_coeffs) self.conductivity.type = self.conductivity.INCOMPRESSIBLE_POLYNOMIAL self.conductivity.fitCoeffs(self.Tbase,self.xbase) except errList as ve: if self.conductivity.DEBUG: print("{0}: Could not fit polynomial {1} coefficients: {2}".format(self.name,'conductivity',ve)) pass # try: # self.viscosity.coeffs = np.copy(std_coeffs) # self.viscosity.type = self.viscosity.INCOMPRESSIBLE_EXPPOLYNOMIAL # self.viscosity.fitCoeffs(self.Tbase,self.xbase) # except errList as ve: # if self.viscosity.DEBUG: print("{0}: Could not fit polynomial {1} coefficients: {2}".format(self.name,'viscosity',ve)) # pass try: tried = False if len(self.viscosity.yData)==1:# and np.isfinite(fluidObject.viscosity.data).sum()<10: self.viscosity.coeffs = np.array([+7e+2, -6e+1, +1e+1]) self.viscosity.type = IncompressibleData.INCOMPRESSIBLE_EXPONENTIAL self.viscosity.fitCoeffs(self.Tbase,self.xbase) if self.viscosity.coeffs==None or IncompressibleFitter.allClose(self.viscosity.coeffs, np.array([+7e+2, -6e+1, +1e+1])): # Fit failed tried = True if len(self.viscosity.yData)>1 or tried: self.viscosity.coeffs = np.copy(std_coeffs)#np.zeros(np.round(np.array(std_coeffs.shape) * 1.5)) self.viscosity.type = IncompressibleData.INCOMPRESSIBLE_EXPPOLYNOMIAL self.viscosity.fitCoeffs(self.Tbase,self.xbase) except errList as ve: if self.viscosity.DEBUG: print("{0}: Could not fit polynomial {1} coefficients: {2}".format(self.name,'viscosity',ve)) pass # reset data for getArray and read special files if self.xid!=self.ifrac_pure and self.xid!=self.ifrac_undefined: allowNegativeData_org = self.allowNegativeData self.allowNegativeData = True try: x,_,z = self.getArray(dataID='TFreeze') self.T_freeze.yData = (x - 273.15) / 100.0 self.T_freeze.xData = [0.0] if np.min(z)<150: z += 273.15 self.T_freeze.data = z.T try: self.T_freeze.source = self.T_freeze.SOURCE_DATA self.T_freeze.type = self.T_freeze.INCOMPRESSIBLE_EXPONENTIAL self.T_freeze.coeffs = np.array([+7e+6, +6e+4, +1e+1]) self.T_freeze.fitCoeffs(self.Tbase,self.xbase) #if np.isfinite(self.T_freeze.data).sum()<10: # self.T_freeze.coeffs = np.array([+7e+6, +6e+4, +1e+1]) # self.T_freeze.type = self.T_freeze.INCOMPRESSIBLE_EXPONENTIAL #else: # self.T_freeze.coeffs = np.zeros(np.round(np.array(std_coeffs.shape) * 2)) # self.T_freeze.type = self.T_freeze.INCOMPRESSIBLE_EXPPOLYNOMIAL #self.T_freeze.fitCoeffs(self.Tbase,self.xbase) except errList as ve: if self.T_freeze.DEBUG: print("{0}: Could not fit {1} coefficients: {2}".format(self.name,"T_freeze",ve)) pass except errList as ve: if self.T_freeze.DEBUG: print("{0}: Could not load {1} data: {2}".format(self.name,"T_freeze",ve)) pass # reset data for getArray again self.allowNegativeData = allowNegativeData_org try: x,_,z = self.getArray(dataID='Vol2Mass') massData = z[:,0] /100.0 volData = (x - 273.15) /100.0 if self.xid==self.ifrac_volume: _,_,self.mass2input.data = IncompressibleFitter.shapeArray(volData,axs=1) self.mass2input.xData = [0.0] self.mass2input.yData = massData try: self.mass2input.coeffs = np.copy(std_coeffs) self.mass2input.source = self.mass2input.SOURCE_DATA self.mass2input.type = self.mass2input.INCOMPRESSIBLE_POLYNOMIAL self.mass2input.fitCoeffs(self.Tbase,self.xbase) except errList as ve: if self.mass2input.DEBUG: print("{0}: Could not fit {1} coefficients: {2}".format(self.name,"mass2input",ve)) pass elif self.xid==self.ifrac_mass: _,_,self.volume2input.data = IncompressibleFitter.shapeArray(massData,axs=1) self.volume2input.xData = [0.0] self.volume2input.yData = volData try: self.volume2input.coeffs = np.copy(std_coeffs) self.volume2input.source = self.volume2input.SOURCE_DATA self.volume2input.type = self.volume2input.INCOMPRESSIBLE_POLYNOMIAL self.volume2input.fitCoeffs(self.Tbase,self.xbase) except errList as ve: if self.volume2input.DEBUG: print("{0}: Could not fit {1} coefficients: {2}".format(self.name,"volume2input",ve)) pass else: raise ValueError("Unknown xid specified.") except errList as ve: if self.mass2input.DEBUG or self.volume2input.DEBUG: print("{0}: Could not load {1} data: {2}".format(self.name,"Vol2Mass",ve)) pass
def getArray(self, dataID=None, func=None, x_in=None, y_in=None, DEBUG=False): """ Tries to read a data file, overwrites it if x or y do not match :param dataID : ID to contruct the path to the data file :param func : Callable object that can take x_in and y_in :param x_in : a 1D array in x direction or 2D with one column, most likely temperature :param y_in : a 1D array in y direction or 2D with one row, most likely cocentration :param DEBUG : a boolean that controls verbosity :returns : Returns a tuple with three entries: x(1D),y(1D),data(2D) """ x = None y = None z = None # First we try to read the file if (not dataID is None and os.path.isfile(self.getFile(dataID))): # File found fileArray = self.getFromFile(dataID) x = np.copy(fileArray[1:,0 ]) y = np.copy(fileArray[0 ,1:]) z = np.copy(fileArray[1:,1:]) else: if DEBUG: print("No readable file found for {0}: {1}".format(dataID,self.getFile(dataID))) updateFile = DEBUG if not x_in is None: # Might need update if not x is None: # Both given, check if different mask = np.isfinite(x) if IncompressibleFitter.allClose(x[mask], x_in[mask]): if DEBUG: print("Both x-arrays are the same, no action required.") updateFile = (updateFile or False) # Do not change a True value to False else: updateFile = True if DEBUG: print("x-arrays do not match. {0} contains \n {1} \n and will be updated with \n {2}".format(self.getFile(dataID),x,x_in)) else: updateFile = True elif x is None: if DEBUG: print("Could not load x from file {0} and no x_in provided, aborting.".format(self.getFile(dataID))) return None,None,None else: updateFile = (updateFile or False) # Do not change a True value to False if not y_in is None: # Might need update if not y is None: # Both given, check if different mask = np.isfinite(y) if IncompressibleFitter.allClose(y[mask], y_in[mask]): if DEBUG: print("Both y-arrays are the same, no action required.") updateFile = (updateFile or False) # Do not change a True value to False else: updateFile = True if DEBUG: print("y-arrays do not match. {0} contains \n {1} \n and will be updated with \n {2}".format(self.getFile(dataID),y,y_in)) else: updateFile = True elif y is None: if DEBUG: print("Could not load y from file {0} and no y_in provided, aborting.".format(self.getFile(dataID))) return None,None,None else: updateFile = (updateFile or False) # Do not change a True value to False if DEBUG: print("Updating data file {0}".format(updateFile)) if not updateFile: return x,y,z # Done, data read from file # Overwrite inputs x = x_in y = y_in z = np.zeros( (len(x)+1,len(y)+1) ) r,c = z.shape if func is None: raise ValueError("Need a function to update the data file.") for i in range(r-1): for j in range(c-1): z[i+1,j+1] = func(x[i],y[j]) z[0,0 ] = np.NaN z[1:,0] = x z[0,1:] = y if not dataID is None: self.writeToFile(dataID, z) else: if DEBUG: print("Not updating data file, dataID is missing.") return x,y,z[1:,1:]