Ejemplo n.º 1
0
class SolutionData(object):
    """
    A base class that defines all the variables needed
    in order to make a proper fit. You can copy this code
    put in your data and add some documentation for where the
    information came from.
    """
    ifrac_mass      = "mass"
    ifrac_mole      = "mole"
    ifrac_volume    = "volume"
    ifrac_undefined = "not defined"
    ifrac_pure      = "pure"

    def __init__(self):


        self.significantDigits = 7

        self.name        = None # Name of the current fluid
        self.description = None # Description of the current fluid
        self.reference   = None # Reference data for the current fluid

        self.Tmax        = None # Maximum temperature in K
        self.Tmin        = None # Minimum temperature in K
        self.xmax        = 1.0 # Maximum concentration
        self.xmin        = 0.0 # Minimum concentration
        self.xid         = self.ifrac_undefined # Concentration is mole, mass or volume-based
        self.TminPsat    = None # Minimum saturation temperature in K
        self.Tbase       = None  # Base value for temperature fits
        self.xbase       = None  # Base value for concentration fits

        self.temperature   = IncompressibleData() # Temperature for data points in K
        self.concentration = IncompressibleData() # Concentration data points in weight fraction
        self.density       = IncompressibleData() # Density in kg/m3
        self.specific_heat = IncompressibleData() # Heat capacity in J/(kg.K)
        self.viscosity     = IncompressibleData() # Dynamic viscosity in Pa.s
        self.conductivity  = IncompressibleData() # Thermal conductivity in W/(m.K)
        self.saturation_pressure = IncompressibleData() # Saturation pressure in Pa
        self.T_freeze      = IncompressibleData() # Freezing temperature in K
        self.mass2input    = IncompressibleData() # dd
        self.volume2input  = IncompressibleData() # dd
        self.mole2input    = IncompressibleData() # dd

        ## Some of the functions might need a guess array
        #self.viscosity.type = self.viscosity.INCOMPRESSIBLE_EXPONENTIAL
        #self.viscosity.coeffs = np.array([+7e+2, -6e+1, +1e+1])
        #self.saturation_pressure.type = self.saturation_pressure.INCOMPRESSIBLE_EXPONENTIAL
        #self.saturation_pressure.coeffs = np.array([-5e+3, +3e+1, -1e+1])


        self.xref = 0.0
        self.Tref = 0.0
        self.pref = 0.0
        self.href = 0.0
        self.sref = 0.0
        self.uref = 0.0
        self.rhoref = 0.0

#    def getDataObjects(self):
#        objList  = {}
#        objList["temperature"] = self.temperature
#        objList["concentration"] = self.concentration
#        objList["density"] = self.density
#        objList["specific_heat"] = self.specific_heat
#        objList["viscosity"] = self.viscosity
#        objList["conductivity"] = self.conductivity
#        objList["saturation_pressure"] = self.saturation_pressure
#        objList["T_freeze"] = self.T_freeze
#        objList["volume2mass"] = self.volume2mass
#        objList["mass2mole"] = self.mass2mole
#        return objList

    def roundSingle(self,x):
        if x==0.0: return 0.0
        return round(x, self.significantDigits-int(math.floor(math.log10(abs(x))))-1)

    def round(self, x):
        r,c,res = IncompressibleFitter.shapeArray(x)
        #digits = -1*np.floor(np.log10(res))+self.significantDigits-1
        for i in range(r):
            for j in range(c):
                if np.isfinite(res[i,j]):
                    res[i,j] = self.roundSingle(res[i,j])
        return res

#    def getPolyObjects(self):
#        objList  = {}
#        objList["density"] = self.density
#        objList["specific heat"] = self.specific_heat
##        objList["viscosity"] = self.viscosity
#        objList["conductivity"] = self.conductivity
##        objList["saturation_pressure"] = self.saturation_pressure
##        objList["T_freeze"] = self.T_freeze
##        objList["volume2mass"] = self.volume2mass
##        objList["mass2mole"] = self.mass2mole
#        return objList
#
#    def getExpPolyObjects(self):
#        objList  = {}
#        objList["viscosity"] = self.viscosity
#        objList["saturation pressure"] = self.saturation_pressure
#        return objList


    def checkT(self, T, p, x):
        if self.Tmin <= 0.: raise ValueError("Please specify the minimum temperature.")
        if self.Tmax <= 0.: raise ValueError("Please specify the maximum temperature.");
        if ((self.Tmin > T) or (T > self.Tmax)): raise ValueError("Your temperature {0} is not between {1} and {2}.".format(T, self.Tmin, self.Tmax))
        TF = 0.0
        if (self.T_freeze.type!=IncompressibleData.INCOMPRESSIBLE_NOT_SET): TF = self.Tfreeze(T, p, x)
        if ( T<TF ): raise ValueError("Your temperature {0} is below the freezing point of {1}.".format(T, TF))
        else: return True
        return False

    def checkP(self, T, p, x):
        ps = 0.0
        if (self.saturation_pressure.type!=IncompressibleData.INCOMPRESSIBLE_NOT_SET): ps = self.psat(T, p, x)
        if (p < 0.0): raise  ValueError("You cannot use negative pressures: {0} < {1}. ".format(p, 0.0))
        if (p < ps) : raise ValueError("Equations are valid for liquid phase only: {0} < {1}. ".format(p, ps))
        else        : return True
        return False


    def checkX(self, x):
        if (self.xmin < 0.0 or self.xmin > 1.0): raise ValueError("Please specify the minimum concentration between 0 and 1.");
        if (self.xmax < 0.0 or self.xmax > 1.0): raise ValueError("Please specify the maximum concentration between 0 and 1.");
        if ((self.xmin > x) or (x > self.xmax)): raise ValueError("Your composition {0} is not between {1} and {2}.".format(x, self.xmin, self.xmax))
        else: return True
        return False

    def checkTPX(self, T, p, x):
        try:
            return (self.checkT(T,p,x) and self.checkP(T,p,x) and self.checkX(x))
        except ValueError as ve:
            #print("Check failed: {0}".format(ve))
            pass
        return False


    def rho (self, T, p=0.0, x=0.0, c=None):
        if not self.checkTPX(T, p, x): return np.NAN
        if c is None:
            c=self.density.coeffs
        if self.density.type==self.density.INCOMPRESSIBLE_POLYNOMIAL:
            return np.polynomial.polynomial.polyval2d(T-self.Tbase, x-self.xbase, c)
        else:  raise ValueError("Unknown function.")

    def c   (self, T, p=0.0, x=0.0, c=None):
        if not self.checkTPX(T, p, x): return np.NAN
        if c is None:
            c = self.specific_heat.coeffs
        if self.specific_heat.type==self.specific_heat.INCOMPRESSIBLE_POLYNOMIAL:
            return np.polynomial.polynomial.polyval2d(T-self.Tbase, x-self.xbase, c)
        else:  raise ValueError("Unknown function.")

    def cp  (self, T, p=0.0, x=0.0, c=None):
        return self.c(T,p,x,c)

    def cv  (self, T, p=0.0, x=0.0, c=None):
        return self.c(T,p,x,c)

    def u   (self, T, p=0.0, x=0.0, c=None):
        if not self.checkTPX(T, p, x): return np.NAN
        if c is None:
            c = self.specific_heat.coeffs
        if self.specific_heat.type==self.specific_heat.INCOMPRESSIBLE_POLYNOMIAL:
            c_tmp = np.polynomial.polynomial.polyint(c)
            return np.polynomial.polynomial.polyval2d(T-self.Tbase, x-self.xbase, c_tmp)
        else:  raise ValueError("Unknown function.")
    #def u   (T, p, x);

    def h   (self, T, p=0.0, x=0.0):
        return self.h_u(T,p,x)

    def visc(self, T, p=0.0, x=0.0, c=None):
        if not self.checkTPX(T, p, x): return np.NAN
        return self.viscosity.baseFunction(T, x, self.Tbase, self.xbase, c=c)

    def cond(self, T, p=0.0, x=0.0, c=None):
        if not self.checkTPX(T, p, x): return np.NAN
        return self.conductivity.baseFunction(T, x, self.Tbase, self.xbase, c=c)

    def    psat(self, T, p=0.0, x=0.0, c=None):
        if (T<=self.TminPsat): return 0.0
        return self.saturation_pressure.baseFunction(T, x, self.Tbase, self.xbase, c=c)

    def Tfreeze(self, T, p=0.0, x=0.0, c=None):
        if c is None:
            c = self.T_freeze.coeffs

        if self.T_freeze.type==self.T_freeze.INCOMPRESSIBLE_POLYNOMIAL:
            return np.polynomial.polynomial.polyval2d(p-0.0, x-self.xbase, c)

        elif self.T_freeze.type==self.T_freeze.INCOMPRESSIBLE_POLYOFFSET:
            #if y!=0.0: raise ValueError("This is 1D only, use x not y.")
            return self.T_freeze.basePolyOffset(c, x) # offset included in coeffs

        elif self.T_freeze.type==self.T_freeze.INCOMPRESSIBLE_EXPONENTIAL:
            #if y!=0.0: raise ValueError("This is 1D only, use x not y.")
            return self.T_freeze.baseExponential(c, x)

        elif self.T_freeze.type==IncompressibleData.INCOMPRESSIBLE_LOGEXPONENTIAL:
            #if y!=0.0: raise ValueError("This is 1D only, use x not y.")
            return self.T_freeze.baseLogexponential(c, x)

        elif self.T_freeze.type==self.T_freeze.INCOMPRESSIBLE_EXPPOLYNOMIAL:
            return np.exp(np.polynomial.polynomial.polyval2d(p-0.0, x-self.xbase, c))

        else:
            raise ValueError("Unknown function: {0}.".format(self.T_freeze.type))




    #def V2M (T,           y);
    #def M2M (T,           x);


    def h_u(self, T, p, x):
        return self.u(T,p,x)+p/self.rho(T,p,x)-self.href

    def u_h(self, T, p, x):
        return self.h(T,p,x)-p/self.rho(T,p,x)+self.href

    def set_reference_state(self, T0, p0, x0=0.0, h0=0.0, s0=0.0):
        self.rhoref = self.rho(T0,p0,x0)
        self.pref = p0
        self.uref = h0 - p0/self.rhoref
        self.uref = self.u(T0,p0,x0)
        self.href = h0
        self.sref = s0
        self.href = self.h(T0,p0,x0)
        self.sref = self.s(T0,p0,x0)
Ejemplo n.º 2
0
    def __init__(self):


        self.significantDigits = 7

        self.name        = None # Name of the current fluid
        self.description = None # Description of the current fluid
        self.reference   = None # Reference data for the current fluid

        self.Tmax        = None # Maximum temperature in K
        self.Tmin        = None # Minimum temperature in K
        self.xmax        = 1.0 # Maximum concentration
        self.xmin        = 0.0 # Minimum concentration
        self.xid         = self.ifrac_undefined # Concentration is mole, mass or volume-based
        self.TminPsat    = None # Minimum saturation temperature in K
        self.Tbase       = None  # Base value for temperature fits
        self.xbase       = None  # Base value for concentration fits

        self.temperature   = IncompressibleData() # Temperature for data points in K
        self.concentration = IncompressibleData() # Concentration data points in weight fraction
        self.density       = IncompressibleData() # Density in kg/m3
        self.specific_heat = IncompressibleData() # Heat capacity in J/(kg.K)
        self.viscosity     = IncompressibleData() # Dynamic viscosity in Pa.s
        self.conductivity  = IncompressibleData() # Thermal conductivity in W/(m.K)
        self.saturation_pressure = IncompressibleData() # Saturation pressure in Pa
        self.T_freeze      = IncompressibleData() # Freezing temperature in K
        self.mass2input    = IncompressibleData() # dd
        self.volume2input  = IncompressibleData() # dd
        self.mole2input    = IncompressibleData() # dd

        ## Some of the functions might need a guess array
        #self.viscosity.type = self.viscosity.INCOMPRESSIBLE_EXPONENTIAL
        #self.viscosity.coeffs = np.array([+7e+2, -6e+1, +1e+1])
        #self.saturation_pressure.type = self.saturation_pressure.INCOMPRESSIBLE_EXPONENTIAL
        #self.saturation_pressure.coeffs = np.array([-5e+3, +3e+1, -1e+1])


        self.xref = 0.0
        self.Tref = 0.0
        self.pref = 0.0
        self.href = 0.0
        self.sref = 0.0
        self.uref = 0.0
        self.rhoref = 0.0