Example of Using the Excel Unit Op, страница 2

The variable nComp has the number of components that are in the component list in CHEMCAD.  The variable nInlets has the number of inlet streams to the Excel Unit Op as the variable nOutlets has the number of outlets from the Excel Unit Op. 

The next group of variables which start with the word “inlet” have the properties of the inlet streams stored in arrays. The last group of variables which start with the word “outlet” hold the properties of the outlet streams.  The values element i of the array pertains to the stream connected to inlet number i. For example, inletTempR(2) holds the temperature of the stream connected to the second inlet. 

If an inlet stream has defined values in CHEMCAD, the values will be stored in the “inlet” variables when the OnEntry macro runs. The “inlet” variables can be called by the user’s code. When the OnExit macro runs, it returns the values of the outlet arrays (outletTemp(I), etc) to CHEMCAD. The user’s VBA code, in ExcelUop macro, determines the values of the outlet arrays.

Note that the units of the inlet and outlet arrays are Rankine, Psia,  Btu/hr, and lbmol/hr. Different units may be used for calculation, but the numbers returned to CHEMCAD via the outlet arrays are evaluated in the listed units. The unit conversion methods available for VBA are discussed in the VBA services manual.

In this section, type in your code. Below is the code for the mixer example.  A detailed analysis follows the code. 

Dim X As Integer

Dim compLbmol_Hr(1 To SIZE_COMP_ARRAY) As Single, flashcompLbmol_Hr(1 To SIZE_COMP_ARRAY) As Single

               For X = 1 To nComp

outletCompRatesLbmol_Hr(1, X) = inletCompRatesLbmol_Hr(1, X) + inletCompRatesLbmol_Hr(2, X)

                              compLbmol_Hr(X) = outletCompRatesLbmol_Hr(1, X)

               Next X

               outletEnthBtu_Hr(1) = inletEnthBtu_Hr(1) + inletEnthBtu_Hr(2)

If inletPresPsia(1) <= inletPresPsia(2) Then outletPresPsia(1) = inletPresPsia(1) Else outletPresPsia(1) = inletPresPsia(2)

 Dim tempR As Single, flashTempR As Single

 Dim presPsia As Single, flashpresPsia As Single

 Dim enthbtu_hr As Single, flashenthbtu_hr As Single

 Dim flowrate As Single

               presPsia = outletPresPsia(1)

               enthbtu_hr = outletEnthBtu_Hr(1)

               tempR = (inletTempR(1) + inletTempR(2)) / 2

               'must define a stream before flashing it!

               check = flash.DefineFeedStream(tempR, presPsia, enthbtu_hr, compLbmol_Hr)

               check = flash.CalculateHPFlash(enthbtu_hr, presPsia)

               outletMoleVapFrac(1) = flash.GetMoleVaporFraction

               check = flash.GetVaporStream(flashTempR, flashpresPsia, flashenthbtu_hr, flowrate, f            lashcompLbmol_Hr)

               If flashTempR = 0 Then

check = flash.GetLiquidStream(flashTempR, flashpresPsia, flashenthbtu_hr, flowrate, flashcompLbmol_Hr)

               End If

               outletTempR(1) = flashTempR

A portion of this code is just used to define more variables that are not previously defined.  The lines of code that do this are seen below.

Dim X As Integer

Dim compLbmol_Hr(1 To SIZE_COMP_ARRAY) As Single, flashcompLbmol_Hr(1 To                  SIZE_COMP_ARRAY) As Single

Dim tempR As Single, flashTempR As Single

Dim presPsia As Single, flashpresPsia As Single

Dim enthbtu_hr As Single, flashenthbtu_hr As Single

Dim flowrate As Single

The first section of code that performs a  task would be to use predefined variables of the inlet and outlet streams of the unit and combine the inlets to create the outlets.  The “For….Next” section adds up each of the component amounts from the two inlet streams and stores them into the outlet stream. 

For X = 1 To nComp

outletCompRatesLbmol_Hr(1, X) = inletCompRatesLbmol_Hr(1, X) + inletCompRatesLbmol_Hr(2, X)