        SUBROUTINE oilsep(z,M7Plus,SG7Plus,NumSep,Pres,Tres,
     1     PSep,TSep,Psc,Tsc,x,ysepgas,FVF,API,GOR,GG,GHV)

c***********************************************************************
c       This subroutine calculates the formation volume factor, API
c          gravity, gas-oil ratio, separator gas gravity, separator gas
c          gross heating value, oil composition, and gas composition
c          resulting from an oil flashed thru a specified number of
c          separators.  These calculations are described in Amyx, James
c          W.,Bass, Daniel M., Jr., and Whiting, Robert L., Petroleum
c          Reservoir Engineering, McGraw-Hill, 1960, pp.352-356.

c       Author: Scott K. Laudeman
c       Date: June 20, 1987

c       Input Variables.

c          z: Array containing mole fraction of each component in feed
c             mixture. Components are assigned subscripts as shown below.
c                1: carbon dioxide
c                2: hydrogen sulfide
c                3: nitrogen
c                4: methane
c                5: ethane
c                6: propane
c                7: isobutane
c                8: n-butane
c                9: isopentane
c               10: n-pentane
c               11: n-hexane
c               12: heptanes plus
c          M7Plus: Molecular weight of heptanes plus, lbs/lb*mole.
c          SG7Plus: Specific gravity of heptanes plus, lbs/lb*mole.
c          NumSep: Number of stages including stock tank.
c          Pres: Reservoir pressure, psia.
c          Tres: Reservoir temperature, Deg. F.
c          PSep: Array containing separator pressures, psia.
c          TSep: Array conataining separator temperatures, Deg. F.
c          Psc: Pressure at standard conditions, psia.
c          Tsc: Temperature at standard conditions, Deg. F.

c       Output Variables.

c          x: Array containing mole fraction of each component in
c             separator liquid.
c          ysepgas: Array containing mole fraction of each component in
c             separator gas.
c          FVF: Oil formation volume factor, bbl/STB.
c          API: API gravity of stock tank oil, Deg. API.
c          GOR: Total gas-oil ratio, scf/STB.
c          GG: Gas Gravity.

c       Other Variables.

c          cvCf: Conversion factor for cf, cf/bbl.
c          cvRankine: Conversion factor for degrees Rankine, Deg. R.
c          DenRes: Density of reservoir oil, scf/STB.
c          DenST: Density of stock tank oil, scf/STB.
c          DenWater: Density of water, scf/STB.
c          F: Function to be solved for mole fraction of vapor phase.
c          GHV: Gross heating value of separator gas, BTU/scf.
c          H: Array containing gross heating values for each component,
c             BTU/scf.
c          i: Loop counter.
c          j: Loop counter.
c          K: Array containing equilibrium ratios.
c          M: Array containing molecular weights for each component,
c             lbs/(lb*mole).
c          MAir: Molecular weight of air, lbs/(lb*mole).
c          MaxError: Error tolerance for vapor phase mole fraction.
c          molgas: Moles of separator gas for 1 mole of feed.
c          molgstage:  Moles of separator gas at specified stage.
c          molsepgas:  Array containing accumulated moles for each
c             separator gas component.
c          molvol: Molar volume of gas at standard conditions, scf.
c          MWres: Molecular weight of reservoir oil, scf/STB.
c          MWST Molecular weight of stock tank oil, scf/STB.
c          NumIter: Iterations required for flash calculation.
c          moloil: Moles of stock tank for 1 mole of feed, STB.
c          P: Pressure, psia
c          R: Universal gas constant, cf*psi/(Deg. R.).
c          T: Temperature, Deg. F.
c          V: Total mole fraction of vapor phase.
c          volg: Volume of separator gas for 1 mole of feed, scf.
c          volmol: Molar volume of gas, cf.
c          volres: Volume occupied by 1 mole of reservoir oil, bbl.
c          volST: Volume of stock tank oil for 1 mole of feed, STB.
c          x: Array containing mole fraction of each component in liquid
c             phase.
c          y: Array containing mole fraction of each component in vapor
c             phase.
c          zsep: Array containing mole fraction of each feed component.

c       Subprograms Used

c          flash: Calculates the compositions of the liquid and vapor
c             phases resulting when a mixture is flashed.
c          kvalue: Calculates equilibrium ratios.
c          oildensity: Calculates oil density.

c***********************************************************************

c***********************************************************************
c       Declarations
c***********************************************************************

c=======================================================================
c       Constants
c=======================================================================

        REAL MAir,MaxError
           PARAMETER (cvCf=5.615,cvRankine=459.67,DenWater=62.365,
     1                MAir=28.9625,MaxError=1E-06,NumComp=12,R=10.73)

c=======================================================================
c       Variables
c=======================================================================


        REAL H(NumComp),K(NumComp),M(NumComp),molgas,molgstage,moloil,
     1         molvol,molsepgas(NumComp),MWRes,MWST,M7Plus,
     1         PSep(NumSep),TSep(NumSep),x(NumComp),y(NumComp),
     1         ysepgas(NumComp),z(NumComp),zsep(NumComp)

c=======================================================================
c       Initialize Arrays
c=======================================================================

        DATA M/44.010,34.076,28.013,16.043,30.070,44.097,
     1     58.124,58.124,72.151,72.151,86.178,0.0/
        DATA H/0.0,0.0,0.0,1010.0,1769.6,2516.1,3251.9,
     1     3262.3,4000.9,4008.9,4655.9,6248.9/

c----------------------------------------------------------------------
c       Molecular weights and gross heating values are from Gas
c          Processors Suppliers Association, Engineering Data Book,
c          Volume II (1987) 23-2:4. Gross heating value for octane is
c          used for heptanes plus.
c----------------------------------------------------------------------


c***********************************************************************
c       Main Program
c***********************************************************************

        M(12)  = M7Plus
        molgas = 0.0
        moloil = 1.0
        volg   = 0.0
        volmol = R*(Tsc + cvRankine)/Psc
        DO 10 i = 1,NumComp
              zsep(i)    = z(i)
              molsepgas(i) = 0.0
10      CONTINUE

c=======================================================================
c       Calculate moles and composition of stock tank oil and separator
c          gas resulting from multiple separator flashes.
c=======================================================================

       DO 20 i = 1,NumSep
           CALL kvalue(PSep(i),TSep(i),K)
           CALL flash(NumComp,zsep,K,MaxError,V,x,y,NumIter,F)
           molgstage = V*moloil
           molgas    = molgas + molgstage
           volg      = volg   + volmol*molgstage
           moloil    = (1.0 - V)*moloil

          DO 30 j = 1,NumComp
              zsep(j)    = x(j)
              molsepgas(j) = molsepgas(j) + y(j)*molgstage
30        CONTINUE

20      CONTINUE

c=======================================================================
c       Calculate molecular weight of reservoir and stock tank oil.
c          Calculate separator gas composition and properties.
c=======================================================================

        MWRes = 0.0
        MWST  = 0.0
        GG    = 0.0
        GHV   = 0.0
        DO 40 i = 1,NumComp
           MWRes      = MWRes + z(i)*M(i)
           MWST       =  MWST + x(i)*M(i)
           ysepgas(i) = molsepgas(i)/molgas
           GG         =    GG + ysepgas(i)*M(i)/MAir
           GHV        =   GHV + ysepgas(i)*H(i)
40      CONTINUE

c=======================================================================
c       Calculate formation volume factor, stock tank gravity, and
c          gas-oil ratio.
c=======================================================================

        CALL oildensity(z,Pres,Tres,M7Plus,SG7Plus,DenRes)
        CALL oildensity(x,Psc,Tsc,M7Plus,SG7Plus,DenST)
        volres = MWRes/DenRes
        volST  = moloil*MWST/DenST
        FVF    = volres/volST
        API    = 141.5/(DenST/DenWater)-131.5
        GOR    = volg*cvCf/volST

        RETURN

        END
