Esempio n. 1
0
def a_calc(IDs, EoS, T):
    Tc = np.array(data.Tc(IDs))
    ac = np.array(ac_calc(IDs, EoS, T))
    alfa = np.array(alfa_calc(IDs, EoS, T))

    #Calculate a
    a = ac * alfa

    return a
Esempio n. 2
0
def b_calc(IDs, EoS):
    Tc = np.array(data.Tc(IDs))
    Pc = np.array(data.Pc(IDs))
    param = parameters(EoS)
    OMEGAb = param[3]

    #Calculate b
    if EoS <= 4:  #SRK, SRK+RG, PR, PR+RG
        b = OMEGAb * R * Tc / Pc
    else:  #CPA, CPA+RG
        b = np.array(data.bCPA(IDs))

    return b
Esempio n. 3
0
def ac_calc(IDs, EoS, T):

    Tc = np.array(data.Tc(IDs))
    Pc = np.array(data.Pc(IDs))
    alfa = np.array(alfa_calc(IDs, EoS, T))
    param = parameters(EoS)
    OMEGAa = param[2]

    if EoS <= 4:  #SRK, SRK+RG, PR, PR+RG
        ac = OMEGAa * R * R * Tc * Tc / Pc
    else:  #CPA, CPA+RG
        ac = np.array(data.a0(IDs))

    return ac
Esempio n. 4
0
def alfa_pr_calc(IDs, EoS, T):
    omega = np.array(data.omega(IDs))
    Tc = np.array(data.Tc(IDs))
    Tr = T / Tc

    #Calculate kapa
    kapa = {
        1: 0.48508 + 1.55171 * omega - 0.15613 * omega * omega,  #SRK
        2: 0.48508 + 1.55171 * omega - 0.15613 * omega * omega,  #SRK+RG
        3: 0.37464 + 1.54226 * omega - 0.26992 * omega * omega,  #PR
        4: 0.37464 + 1.54226 * omega - 0.26992 * omega * omega  #PR+RG
    }.get(EoS, 'NULL')

    alfa_pr = kapa * (kapa - (1 + kapa) * (Tr**(-0.5)))
    return alfa_pr
Esempio n. 5
0
def a_pr_calc(IDs, EoS, T):
    Tc = np.array(data.Tc(IDs))
    ac = np.array(ac_calc(IDs, EoS, T))
    omega = np.array(data.omega(IDs))

    #Calculate kapa
    kapa = {
        1: 0.48508 + 1.55171 * omega - 0.15613 * omega * omega,  #SRK
        2: 0.48508 + 1.55171 * omega - 0.15613 * omega * omega,  #SRK+RG
        3: 0.37464 + 1.54226 * omega - 0.26992 * omega * omega,  #PR
        4: 0.37464 + 1.54226 * omega - 0.26992 * omega * omega  #PR+RG
    }.get(EoS, 'NULL')

    #Calculate a
    a = {
        1: -ac * kapa * ((1 + kapa) / np.sqrt(T / Tc) - kapa) / Tc,  #SRK
        2: -ac * kapa * ((1 + kapa) / np.sqrt(T / Tc) - kapa) / Tc,  #SRK+RG
        3: -ac * kapa * ((1 + kapa) / np.sqrt(T / Tc) - kapa) / Tc,  #PR
        4: -ac * kapa * ((1 + kapa) / np.sqrt(T / Tc) - kapa) / Tc,  #PR+RG
    }.get(EoS, 'NULL')

    return a
Esempio n. 6
0
def alfa_calc(IDs, EoS, T):
    omega = np.array(data.omega(IDs))
    Tc = np.array(data.Tc(IDs))
    Tr = T / Tc

    #Calculate kapa
    if EoS == 1 or EoS == 2:  #SRK, SRK+RG
        if omega[0] < 0.2:
            kapa = 0.48508 + 1.55171 * omega - 0.15613 * omega * omega
        else:
            kapa = 0.48508 + 1.55171 * omega - 0.15613 * omega * omega
    elif EoS == 3 or EoS == 4:  #PR, PR+RG
        if omega[0] < 0.2:
            #kapa = 0.37640+1.54230*omega-0.26990*omega*omega
            kapa = 0.37464 + 1.54226 * omega - 0.26992 * omega * omega
        else:
            kapa = 0.37464 + 1.54226 * omega - 0.26992 * omega * omega
            #kapa = 0.3796+(1.4850+(-0.1644+0.01667*omega)*omega)*omega
    else:  #CPA, CPA+RG
        kapa = np.array(data.c1(IDs))

    alfa = (1.0 + kapa * (1.0 - (Tr**0.5)))**2

    return alfa
def renorm(EoS,IDs,MR,T,nd,nx,kij,nc,CR,en_auto,beta_auto,SM,n,estimate,L_est,phi_est):
    #nd    Size of density grid
    #nx    Size of mole fraction grid
    #n     Main loop iteration controller
    
    #If only 1 component is present mimic a binary mixture made of the same component
    if nc==1:
        IDs[1] = IDs[0]
    
    #Recover parameters
    L_rg = data.L(IDs)         #Vector with L parameters (cutoff length)
    phi_rg = data.phi(IDs)     #Vector with phi parameters
    Tc = data.Tc(IDs)
    
    #Components parameters
    a = eos.a_calc(IDs,EoS,T)
    b = eos.b_calc(IDs,EoS)
    Tr = T/np.array(Tc)
    
    #Main loop parameters
    x = np.array([0.0001,0.9999])
    stepx = (1/float(nx)) #Step to calculate change
    k = 0               #Vector fill counter
    i = 1               #Main loop counter
    r = 0               #Report counter
    count = 0
    rho = np.empty((nd))            #Density vector
    rhov = []                       #Density vector to export
    x0v = []                        #Mole fraction vector to export
    bmixv = []
    f = np.empty((nd))              #Helmholtz energy density vector
    fv = []                         #Helmholtz energy density vector to export
    fresv = []                      #Residual Helmholtz energy density vector to export
    Tv = []                         #Temperature values to export
    df = np.empty((nd))             #Changes in helmholtz energy density vector
    f_orig = np.empty((nd))         #Unmodified Helmholtz energy density vector
    rhob = []                       #Adimensional density vector
    u = np.empty((nd))
    X = np.ones((4*nc))
    Pv = []
    fmat = []
    Pmatv = np.empty((nx,nd))
    fmatres = []
    umat = []
    ures = np.empty((nd))
    uv = []
    df_vdw = np.empty((nd))
    
    df_vec2 = []
    f_vec2 = []
    P_vec2 = []
    u_vec2 = []
    aa2 = []
    
    if nc==1:
        X = np.ones((8))
    
    #Main loop*************************************
    while x[0]<1.0:
        if nc>1:
            print x[0]
        if x[0]==0.006: #after first step
            x[0]=0.005
        x[1]=1-x[0]
        
        if nc==1:
            x[0] = 0.999999
            x[1] = 0.000001
        
        #Mixture parameters
        bmix = eos.bmix_calc(MR,b,x)
        amix = eos.amix_calc(MR,a,x,kij)
        Nav = 6.023e23
        rhomax = 0.999999
        
        #Mixture Renormalization parameters
        L = np.dot(x,np.power(L_rg,3.0))
        L = np.power(L,1.0/3.0)
        phi = np.dot(x,phi_rg)
        
        #print L
        #print phi
        
        
        pi = math.pi
        omega = data.omega(IDs)[0]
        sig = np.power(6/pi*b/Nav*np.exp(omega),1.0/3.0)[0]
        #sig = np.power(b/Nav,1.0/3.0)[0]
        #c1 = data.c1(IDs)[0]
        #en = data.en(IDs)[0]
        #sig = np.power(1.15798*b/Nav,1.0/3.0)[0]
        L = sig
        #L = 1.5*sig
        #L = 1/c1*sig
        #print L,phi
        #L = 0.5/c1*sig
        #PHI = 4*(pi**2.0)
        
        #PHI = 1.0/pi/4.0
        #lamda = 1.5
        #w_LJ = (9.0*sig/7.0) #lennard-jones
        #print 'LJ=',w_LJ
        #w_SW = np.sqrt((1./5.)*(sig**2.0)*(lamda**5.0-1)/(lamda**3.0-1)) #square-well potential
        #print 'SW=',w_SW
        #phi = PHI*(w_LJ**2)/2/(L**2)
        #phi = PHI*(w_SW**2)/2/(L**2)
        
        #om = data.omega(IDs)
        #phi = 2/np.power(np.exp(om),4)[0]
        #w = 0.575*sig*en/T/kB/b[0]*1e6
        #print 'w=',w
        #phi = 2/np.power(np.exp(c1),4)[0]
        
        #w = 100.0*1e-9/100 #van der waals wavelength 100nm
        #phi = PHI*(w**2)/2/(L**2)
        
        #print L
        #print phi
        #print '---------'
        
        #New parameters
        #L = 1.5*np.power(b/Nav,1.0/3.0)
        #h = 6.626e-34
        #kkB = 1.38e-23
        #MM = 0.034
        #deBroglie = h/np.sqrt(3*kkB*T*MM/Nav)
        #phi = (deBroglie**2.0)/(L**2.0)*150*3.14
        #L = L[0]
        #phi = phi[0]
        #print 'L=',L
        #print 'phi=',phi
        

        if estimate==True:
            L = L_est
            phi = phi_est

        for k in range(0,nd):
            rho[k] = np.array(float(k)/nd/bmix)
            if k==0:
                rho[0] = 1e-6
            if EoS==6:
                if k==0:
                    X = association.frac_nbs(nc,1/rho[k],CR,en_auto,beta_auto,b,bmix,X,0,x,0,T,SM)
                else:
                    X = association.frac_nbs(nc,1/rho[k],CR,en_auto,beta_auto,b,bmix,X,1,x,0,T,SM)
            #print X,k
            #raw_input('...')
            f[k] = np.array(helm_rep(EoS,R,T,rho[k],amix,bmix,X,x,nc))   #Helmholtz energy density
            k = k+1
            
        f_orig = f                                #Initial helmholtz energy density
        
        """
        #-------------------------------------------
        #Fluctuation Analysis-----------------------
        #-------------------------------------------
        drho = rho[int(nd/2)]-rho[int(nd/2)-1]
        for i in range(1,nd-2):
            u[i] = (f[i+1]-f[i-1])/(2*drho)
        u[nd-1] = (f[nd-1]-f[nd-2])/drho
        u[0] = (f[1]-f[0])/drho
        
        fspl = splrep(rho,f,k=3)         #Cubic Spline Representation
        f3 = splev(rho,fspl,der=0)
        u = splev(rho,fspl,der=1)        #Evaluate Cubic Spline First derivative

        P = -f3+rho*u
        
        P_vec2.append(P)
        u_vec2.append(u)
        #===========================================
        #===========================================
        """
    
        #Subtract attractive forces (due long range correlations)
        f = f + 0.5*amix*(rho**2)
        
        df_vec2.append(rho)
        f_vec2.append(rho)

        #Adimensionalization
        rho = rho*bmix
        f = f*bmix*bmix/amix
        T = T*bmix*R/amix
        
        f_vec2.append(f)

        rho1 = rho.flatten()

        #Main loop****************************************************************
        i = 1
        while i<=n:
            #print i
            #K = kB*T/((2**(3*i))*(L**3))
            #K = R*T/((L**3)*(2**(3*i)))
            K = T/(2**(3*i))/((L**3)/bmix*6.023e23)
            
            
            #Long and Short Range forces
            fl = helm_long(EoS,rho,f)
            fs = helm_short(EoS,rho,f,phi,i)

            #Calculate df
            width = rhomax/nd
            w = 0
            for w in range(0,nd):
                df[w] = renorm_df(w,nd,fl,fs,K,rho,width)
            
            #Update Helmholtz Energy Density
            df = np.array(df) #used to evaluate each step
            f = f + df
            df_vec2.append(list(df/bmix/bmix*amix*1e6/rho))
            f_vec2.append(list(f))
            #print 'i=',i,K,f[2],df[2],T,df_vec2[1][2]
            i = i+1
            #print i

        #Dimensionalization
        rho = rho/bmix
        f = f/bmix/bmix*amix
        T = T/bmix/R*amix
        
        #df_total = 
        #df = np.array(df)
        #df_vec.append(df)
        
        #Add original attractive forces
        f = f - 0.5*amix*(rho**2)
        
        #Store residual value of f
        #fres = f - rho*R*T*(np.log(rho)-1) #WRONG
        fres = f - rho*R*T*np.log(rho)
        #f = f + rho*R*T*(np.log(rho)-1) #Already accounting ideal gas energy
        
        #strT = str(T)
        #dfT = ('df_%s.csv' %strT)
        TT = np.zeros((nd))
        df_vdw = 0.5*((rho*bmix)**2)
        df_vec2.append(list(df_vdw))
        f_vec2.append(list(df_vdw))
        for i in range(0,nd):
            TT[i] = T
        df_vec2.append(TT)
        f_vec2.append(TT)
        envelope.report_df(df_vec2,'df.csv')
        envelope.report_df(f_vec2,'f.csv')
        #raw_input('----')

        #if(EoS==6):
        #    f = fres
        
        fv.append(f)
        fresv.append(fres)
        x0v.append(x[0])
        bmixv.append(bmix)
        
        if r==0:
            rhob.append(rho*bmix) #rhob vector is always the same
            rhov.append(rho) #in case the calculation is done for one-component
        r=1

        drho = rho[int(nd/2)]-rho[int(nd/2)-1]
        for i in range(1,nd-2):
            u[i] = (f[i+1]-f[i-1])/(2*drho)
        u[nd-1] = (f[nd-1]-f[nd-2])/drho
        u[0] = (f[1]-f[0])/drho
        
        fspl = splrep(rho,f,k=3)         #Cubic Spline Representation
        f = splev(rho,fspl,der=0)
        u = splev(rho,fspl,der=1)        #Evaluate Cubic Spline First derivative

        P = -f+rho*u
        Pv.append(P)
        for j in range(0,nd):
            Pmatv[count][j] = P[j]
            #print Pmatv[count][j],count,j,x[0]
        count = count+1
        
        """
        #Fluctuation Analysis-----------------------
        P_vec2.append(P)
        u_vec2.append(u)
        P_vec2.append(TT)
        u_vec2.append(TT)
        envelope.report_df(P_vec2,'P.csv')
        envelope.report_df(u_vec2,'u.csv')
        #===========================================
        """

        fmat.append(f)
        fmatres.append(fres)

        x[0] = x[0]+stepx
        #if nc>1:
        #    if abs(x[0]-1.0)<1e-5:
        #        x[0] = 0.9999
        
    if nc>1:
        Pmat = RectBivariateSpline(x0v,rhob,Pmatv)
    else:
        Pmat = 'NULL'
        
        #differente imposed by renormalization
        dfv = []
        dff = f-f_orig
        dfv.append(dff)
        avdw = []
        aa2 = 0.5*amix*(rho**2)
        avdw.append(aa2)

    renorm_out = []
    renorm_out.append(fv)
    renorm_out.append(x0v)
    renorm_out.append(rhov)
    renorm_out.append(rhob)
    renorm_out.append(fmat)
    renorm_out.append(Pmat)
    if nc>1: #If binary mixture, report calculated values
        print 'before report'
        ren_u = report_renorm_bin(rhob,x0v,fmatres,nx,nd,MR,IDs,EoS)
        renorm_out.append(ren_u)
    else:
        renorm_out.append(0)
    renorm_out.append(fresv)
    renorm_out.append(Pv)
    renorm_out.append(bmixv)
    renorm_out.append(dfv)
    renorm_out.append(avdw)
    return renorm_out
def crit_mix(estimate_crit,Tci,rhoci,EoS,IDs,MR,T,nd,nx,kij,nc,CR,en_auto,beta_auto,SM,n,x):

    #Initial estimate of critical point
    if estimate_crit==True:
        Tci = Tci
        Vci = rhoci
    else:
        Tcvec = data.Tc(IDs)
        Tci = 1.5*np.dot(x,Tcvec)
        b = eos.b_calc(IDs,EoS)
        bmix = eos.bmix_calc(MR,b,x)
        Vci = 4*bmix
    Tc = Tci
    P = 0.1
    
    print 'Initial Estimates:','Tc=',Tci,'Vci=',Vci
    raw_input('...')
        
    #Solve det(Q)=0 for initial condition
    nt = 1.0 #Total number of moles   
    delta = np.identity(nc) #Kronecker delta
    
    #Calculate thermodynamic properties at initial T
    nx = 4 #Renorm will calculate only given x
    nd = 400
    
    h = 1e-4
    x0 = np.array([0.1,0.9])
    nt = np.sum(x0)
    n0 = nt*x0
    
    tolC = 1e-20
    tolQ = 1e-20
    C = tolC+1
    detQ = tolQ+1
    Q = np.zeros((nc,nc))
    n1 = np.zeros((nc))
    n1 = n0
    
    r_dat0 = 1
    r_dat1 = 1
    
    
    #External V loop - solve triple sum = 0
    while C>tolC:
        #Internal T loop - solve det(Q)=0------------------------------------------------------------------------------------
        while detQ>tolQ:
            
            dT = 1e-5*Tc
            
            #Q at T
            Q = np.zeros((nc,nc))
            if EoS==2 or EoS==4 or EoS==6:
                r_dat0 = renorm(EoS,IDs,MR,Tc,nd,nx,kij,nc,CR,en_auto,beta_auto,SM,n,False,0,0)    
            lnfugcoef0 = eos.lnfugcoef_func(IDs,EoS,MR,P,Tc,x0,kij, 1,Vci,en_auto,beta_auto,CR,SM,0,0,r_dat0)[0] #vapor
            for j in range(0,nc):
                n1[j] = n0[j]+h
                x1 = n1/(nt+h)
                if EoS==2 or EoS==4 or EoS==6:
                    r_dat1 = renorm(EoS,IDs,MR,Tc,nd,nx,kij,nc,CR,en_auto,beta_auto,SM,n,False,0,0)
                lnfugcoef1 = eos.lnfugcoef_func(IDs,EoS,MR,P,Tc,x1,kij, 1,Vci,en_auto,beta_auto,CR,SM,0,0,r_dat1)[0] #vapor
                print lnfugcoef1,x1
                for i in range(0,nc):
                    Q[i][j] = (lnfugcoef1[i]-lnfugcoef0[i])/h
                n1 = n0
                x1 = x0
            detQ = np.linalg.det(Q)
            print 'lnfug0',lnfugcoef0,x0
            print 'lnfug1',lnfugcoef1,x1
            print 'detQ=',detQ
            print 'Q=',Q
            raw_input('...')
            
            #Q at T+dT
            Q = np.zeros((nc,nc))
            if EoS==2 or EoS==4 or EoS==6:
                r_dat0 = renorm(EoS,IDs,MR,Tc+dT,nd,nx,kij,nc,CR,en_auto,beta_auto,SM,n,False,0,0)
            lnfugcoef0 = eos.lnfugcoef_func(IDs,EoS,MR,P,Tc+dT,x0,kij, 1,Vci,en_auto,beta_auto,CR,SM,0,0,r_dat0)[0] #vapor
            for j in range(0,nc):
                n1[j] = n0[j]+h
                x1 = n1/(nt+h)
                if EoS==2 or EoS==4 or EoS==6:
                    r_dat1 = renorm(EoS,IDs,MR,Tc+dT,nd,nx,kij,nc,CR,en_auto,beta_auto,SM,n,False,0,0)
                lnfugcoef1 = eos.lnfugcoef_func(IDs,EoS,MR,P,Tc+dT,x1,kij, 1,Vci,en_auto,beta_auto,CR,SM,0,0,r_dat1)[0] #vapor
                for i in range(0,nc):
                    Q[i][j] = (lnfugcoef1[i]-lnfugcoef0[i])/h
                n1 = n0
            detQ_dT = np.linalg.det(Q)
            
            dF = (detQ_dT-detQ)/(dT*Tc)
            Tc = Tc - detQ/dF
            print 'Tc=',Tc
            #End Internal T loop - solve det(Q)=0===============================================================================
            
        nt = 1.0
        nt0 = 1.0
        n0 = x0*nt0
        dn = 1.0
        """
        n1k = np.zeros((4))
        nt1k = np.zeros((4))
        x1k = np.zeros((4))
        k = -2
        for i in range(0,4)
            n1k[i] = (x0+k*1e-3*dn/nt0)*nt0
            nt1k[i] = np.sum(n1)
            x1k[i] = n1/nt1
            k = k+1
        """
        
        dV = 1e-6*Vci
            
        #C at V
        C = 0
        if EoS==2 or EoS==4 or EoS==6:
            r_dat0 = renorm(EoS,IDs,MR,Tc,nd,nx,kij,nc,CR,en_auto,beta_auto,SM,n,False,0,0)
        lnfugcoef0 = eos.lnfugcoef_func(IDs,EoS,MR,P,Tc,x0,kij, 1,Vci,en_auto,beta_auto,CR,SM,0,0,r_dat0)[0] #vapor
        for j in range(0,nc):
            eps = 1e-3
            n1[j] = (x0[j]+eps*dn/nt)*nt
            nt1 = np.sum(n1)
            x1 = n1/nt1
            if EoS==2 or EoS==4 or EoS==6:
                r_dat1 = renorm(EoS,IDs,MR,Tc,nd,nx,kij,nc,CR,en_auto,beta_auto,SM,n,False,0,0)
            lnfugcoef1 = eos.lnfugcoef_func(IDs,EoS,MR,P,Tc,x1,kij, 1,Vci,en_auto,beta_auto,CR,SM,0,0,r_dat1)[0] #vapor
            for i in range(0,nc):
                Q[i][j] = (lnfugcoef1[i]-lnfugcoef0[i])/eps
                C = C + Q[i][j]*n1[j]*n1[j]
            n1 = n0
        
        #C at T+dV
        C_dV = 0
        if EoS==2 or EoS==4 or EoS==6:
            r_dat0 = renorm(EoS,IDs,MR,Tc,nd,nx,kij,nc,CR,en_auto,beta_auto,SM,n,False,0,0)
        lnfugcoef0 = eos.lnfugcoef_func(IDs,EoS,MR,P,Tc,x0,kij, 1,Vci+dV,en_auto,beta_auto,CR,SM,0,0,r_dat0)[0] #vapor
        for j in range(0,nc):
            eps = 1e-3
            n1[j] = (x0[j]+eps*dn/nt)*nt
            nt1 = np.sum(n1)
            x1 = n1/nt1
            if EoS==2 or EoS==4 or EoS==6:
                r_dat1 = renorm(EoS,IDs,MR,Tc,nd,nx,kij,nc,CR,en_auto,beta_auto,SM,n,False,0,0)
            lnfugcoef1 = eos.lnfugcoef_func(IDs,EoS,MR,P,Tc,x1,kij, 1,Vci+dV,en_auto,beta_auto,CR,SM,0,0,r_dat1)[0] #vapor
            for i in range(0,nc):
                Q[i][j] = (lnfugcoef1[i]-lnfugcoef0[i])/eps
                C_dV = C_dV + Q[i][j]*n1[j]*n1[j]
            n1 = n0
        
        dF = (C_dV-C)/(dV*Vci)
        Vci = Vci - Vci/dF
    
    #Solve det(Q)=0 to find Tc and rhoc
        
    print Tc
    print Vci
    rhoc = 1/Vci
    crit = []
    crit.append(Tc)
    #crit.append(Pc)
    crit.append(rhoc)
    return crit