Hysys Excel Automation

Hysys Excel Automation Training

Tuesday, October 1, 2013

How to Use Hysys Splitter in VBA

Hi there,

This post is requested by Mayembe. He asked me to post a VBA code in Excel to access Hysys splitter.

Here is the VBA example

=================================================



' VBA code starts here
Option Explicit
Public hyApp As HYSYS.Application
Public simCase As SimulationCase
Public tee1 As TeeOp


Public Sub StartHYSYS()
Dim filename As String
Dim ratio As Variant

 ' LOADING HYSYS SIMULATION FILE
    Set hyApp = CreateObject("HYSYS.Application")
    hyApp.Visible = True
    Set simCase = hyApp.ActiveDocument
    If simCase Is Nothing Then
        filename = Worksheets("Sheet1").Range("c4")
        
        If filename <> "False" And simCase Is Nothing Then
            Set simCase = GetObject(filename, "HYSYS.SimulationCase")
            simCase.Visible = True
        End If
    End If

Set tee1 = simCase.Flowsheet.Operations("teeop").Item("tee-101")
ratio = tee1.SplitsValue
ratio(0) = Worksheets("Sheet1").Range("c5").Value
ratio(1) = Worksheets("Sheet1").Range("c6").Value

tee1.Splits.SetValues ratio, ""

        
End Sub
'this is the end of VBA code

=====================================

Just for your note, "TEEOP" is a operation code name for "TEE" or splitter in Hysys.

And here is the excel interface. I use previous example of hysys file to develop our model. If you forget about our previous hysys model, check previous example





Grab hysys file  and excel file from google drive


That's it. Hysys splitter is in your hand.

Let me know that you have comment or request. We'll see what I can do. I hope.



Alex 

Wednesday, May 15, 2013

Pipe Segment of Aspen Hysys Through Excel VBA



Now, we're trying to access pipesegment on Aspen Hysys 7.3 through VBA. This post is an answer of previous post comment.

Here is the example

Create a table as shown figure



Then add following VBA code

Option Explicit

Public hyApp As HYSYS.Application
Public simCase As SimulationCase
Public pStream As ProcessStream
Public psegment As PipeSegment


Public Sub StartHYSYS()
Dim filename As String
Dim ITEM As Integer
Dim lengthVar As Variant
Dim p_length, p_elev, p_rough, p_idiam, p_odiam, p_cond As Variant
    
    ' HYSYS CUSTOMATION THROUGH EXCEL TO ACCESS PIPE SEGMENT
    
    ' LOADING HYSYS SIMULATION FILE
    Set hyApp = CreateObject("HYSYS.Application")
    
    hyApp.Visible = True
    Set simCase = hyApp.ActiveDocument
    If simCase Is Nothing Then
        filename = Worksheets("HYSYS").Range("C4").Text
        
        If filename <> "False" And simCase Is Nothing Then
            Set simCase = GetObject(filename, "HYSYS.SimulationCase")
            simCase.Visible = True
        End If
    End If
  
    ' define operation
    Set psegment = simCase.Flowsheet.Operations("pipeseg").ITEM("pipe-100")
        
    ' define pipesegment's parameters
    p_length = psegment.SegmentLengthValue
    p_elev = psegment.SegmentElevationChangeValue
    p_rough = psegment.RoughnessValue
    p_idiam = psegment.SegmentInnerDiamValue
    p_odiam = psegment.SegmentOuterDiamValue
    p_cond = psegment.PipeConductValue
    
    

    ' collecting data as variant

    For ITEM = 1 To 4
        p_length(ITEM - 1) = Cells(6, ITEM + 2)
        p_elev(ITEM - 1) = Cells(7, ITEM + 2)
        p_odiam(ITEM - 1) = Cells(8, ITEM + 2)
        p_idiam(ITEM - 1) = Cells(9, ITEM + 2)
        p_rough(ITEM - 1) = Cells(10, ITEM + 2)
        p_cond(ITEM - 1) = Cells(11, ITEM + 2)
            
    Next ITEM
    
    ' inputting data to hysys
    psegment.SegmentLength.Values = p_length
    psegment.SegmentElevationChange.Values = p_elev
    psegment.Roughness.Values = p_rough
    psegment.SegmentInnerDiam.Values = p_idiam
    psegment.SegmentOuterDiam.Values = p_odiam
    psegment.PipeConduct.Values = p_cond
      
    ' FINISH
End Sub

As you can see, Hysys model is just as simple as following figure.








And now, pipe segment is on your hand :D

Friday, May 3, 2013

How to create and acces molar component (molar flowrate, composition, ect)

It has been so long time not to update this blog. And now I'm posting how to access stream component fraction, component molar flowrate and mass flowrate.

Here is the VBA example




Option Explicit
Public hyApp As HYSYS.Application
Public simCase As SimulationCase
Public pStream As ProcessStream
Public Sub StartHYSYS()
Dim filename As String
Dim ITEM As Integer
Dim Compositions As Variant        

' HYSYS CUSTOMATION THROUGH EXCEL TO ACCESS STREAM COMPONENTS        


' LOADING HYSYS SIMULATION FILE    


Set hyApp = CreateObject("HYSYS.Application")        

hyApp.Visible = True    
Set simCase = hyApp.ActiveDocument    
If simCase Is Nothing Then        
          filename = Worksheets("HYSYS").Range("C4").Text               
If filename <> "False" And simCase Is Nothing Then            
          Set simCase = GetObject(filename, "HYSYS.SimulationCase")            
          simCase.Visible = True        
End If    
End If      


' READ DATA FROM EXCEL TO "STREAM 1"    

Set pStream = simCase.Flowsheet.MaterialStreams.ITEM("STREAM 1")    
Compositions = pStream.ComponentMolarFractionValue    
For ITEM = 0 To 11        'collect composition data as variant        
          Compositions(ITEM) = Worksheets("HYSYS").Range("D" & ITEM + 7).Value    
Next ITEM   

' apply composition data    

pStream.ComponentMolarFraction.Values = Compositions            
' WRITE COMPOSITION DATA FROM HYSYS TO EXCEL FROM "STREAM 3"       

Set pStream = simCase.Flowsheet.MaterialStreams.ITEM("STREAM 3")    

For ITEM = 0 To 11        ' number of components are 12
  ' component molar fraction        
Worksheets("HYSYS").Range("E" & ITEM + 7).Value = pStream.ComponentMolarFraction(ITEM)
  ' component mass flowrate        
Worksheets("HYSYS").Range("F" & ITEM + 7).Value = pStream.ComponentMassFlow(ITEM)
   ' component molar fraction    
Worksheets("HYSYS").Range("G" & ITEM + 7).Value = pStream.ComponentMolarFlow(ITEM)
Next ITEM            

' FINISH


End Sub



Spreadsheet is look like this figure


Please note, hysys file source is located in Cell C4, refer to following code 
               filename = Worksheets("HYSYS").Range("C4").Text

Hysys stream is configured as following figure





Hope that helps. Any thought? Please feel free to comment.

Friday, June 10, 2011

Hysys Type Library

I forgot to mention about this one. Hysys simulation won't run if there is no Hysys Library on Excel. Hysys library can be found and activated through Microsoft Visual Basic - Tools - References. Find Hysys Type Library, activate by check mark and OK.



This blog doesn't contain any tutorial of using Visual Basic Application. You may learn by others literatures, like Visual Basic Application

Hysys Training to Link Hysys Valve to Excel

Linking Hysys Simulation to access HYSYS UNIT OPERATION VALVE using VBA macro.


Here is the source code:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



Option Explicit
Public hyApp As HYSYS.Application
Public simCase As SimulationCase
Public FEED1 As ProcessStream, FEED2 As ProcessStream





Public Sub StartHYSYS()
Dim fileName As String
Dim PRESSFEED2 As Double, FLOWFEED2 As Double, TEMPFEED2 As Double
Dim VALVE1 As Valve, VALVE2 As Valve
Dim PRESSDROP1 As Double


    ' LOADING HYSYS SIMULATION FILE
    Set hyApp = CreateObject("HYSYS.Application")
    hyApp.Visible = True
    Set simCase = hyApp.ActiveDocument
    If simCase Is Nothing Then
        fileName = Worksheets("Sheet1").Range("c2")
        
        If fileName <> "False" And simCase Is Nothing Then
            Set simCase = GetObject(fileName, "HYSYS.SimulationCase")
            simCase.Visible = True
        End If

    End If
     
    Set FEED1 = simCase.Flowsheet.MaterialStreams.Item("FEED1")
    Worksheets("Sheet1").Range("D6").Value = FEED1.MassFlow.GetValue("LB/HR")       ' import data MASS FLOWRATE from hysys simulation streams
    Worksheets("Sheet1").Range("D7").Value = FEED1.Pressure.GetValue("PSIG")       ' import data PRESSURE from hysys tutorial streams
    Worksheets("Sheet1").Range("D8").Value = FEED1.Temperature.GetValue("C")       ' import data TEMPERATURE from hysys tutorial streams
    
    
    Set FEED2 = simCase.Flowsheet.MaterialStreams.Item("FEED2")
    
    PRESSFEED2 = Worksheets("Sheet1").Range("H7").Value
    FEED2.Pressure.SetValue PRESSFEED2, "PSIA"                                      ' export pressure
    
    FLOWFEED2 = Worksheets("Sheet1").Range("H6").Value
    FEED2.MolarFlow.SetValue FLOWFEED2, "MMSCFD"                                    ' export molar flowrate
    
    TEMPFEED2 = Worksheets("Sheet1").Range("H8").Value
    FEED2.Temperature.SetValue TEMPFEED2, "F"                                       ' export temperature
    
    
    '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    Set VALVE1 = simCase.Flowsheet.Operations.Item("VALVE-1")
    PRESSDROP1 = Worksheets("Sheet1").Range("D16").Value
    VALVE1.PressureDrop.SetValue PRESSDROP1, "inHg(60F)"    ' send data pressure drop to hysys
       
        
    Worksheets("Sheet1").Range("D12").Value = FEED1.Pressure.GetValue("psia")   ' get data from hysys simulation
    Worksheets("Sheet1").Range("D13").Value = FEED1.Pressure.GetValue("inHg(60F)") ' get data from hysys tutorial
    Worksheets("Sheet1").Range("D14").Value = FEED1.Temperature.GetValue("c") ' get data from hysys tutorial
    Worksheets("Sheet1").Range("D15").Value = FEED1.Temperature.GetValue("r") ' get data from hysys tutorial
    
     
    Worksheets("Sheet1").Range("D18").Value = VALVE1.PressureDrop.GetValue("mmHg(0C)") ' get data from hysys simulation
    Worksheets("Sheet1").Range("D19").Value = VALVE1.PressureDrop.GetValue("bar") ' get data from hysys simulation
    Worksheets("Sheet1").Range("D20").Value = VALVE1.PressureDrop.GetValue("kPa") ' get data from hysys tutorial
    Worksheets("Sheet1").Range("D21").Value = VALVE1.PressureDrop.GetValue("atm") ' get data from hysys tutorial
    Worksheets("Sheet1").Range("D22").Value = VALVE1.PressureDrop.GetValue("psi") ' get data from hysys tutorial
    


End Sub

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
HYSYS simulation and excel file can be downloaded here. REMEMBER TO CHANGE HYSYS SIMULATION DIRECTORY ADDRESS ON CELL "C2"


Note: Hysys simulation is generated by Hysys V7.1, may not work well in previous version of Hysys, s.a Hysys 2006 or Hysys 3.2. 


If Hysys V7.1 is not available, make your own simulation. Stream FEED1 and FEED2 specification can be found in previous post . Read and simulate previous post, add two Valves named "VALVE-1" and "VALVE-2", add two more streams and named "OUT-1" and "OUT-2". 








Save in a specific folder/directory you like. 


MAKE SURE TO WRITE DIRECTORY ADDRESS TO CELL "C2" OR HYSYS FILE WILL NOT BE FOUND.


Make some test. Input pressure drop in Cell "D16", and click "RUN HYSYS". 






Thursday, June 9, 2011

Hysys Tutorial to Link Hysys Streams on Excel



How to link Hysys Simulation to Excel?
Open Example1.HSC. This file is generated in Aspen Hysys V7.1, may not work in previous version of Hysys, such as Hysys 2006 or Hysys 3.2. If you have older version of Hysys, make your own simulation with two streams named FEED1 and FEED2. Here is some operating condition of the streams.


Linking between Hysys Simulations and Excel using Macro VBA (Visual Basic Application). Create new module in Macro VBA and put this code.

Option Explicit

Public hyApp As HYSYS.Application
Public simCase As SimulationCase
' LOADING HYSYS TUTORIAL FILE
Set hyApp = CreateObject("HYSYS.Application")    hyApp.Visible = True    Set simCase = hyApp.ActiveDocument    If simCase Is Nothing Then        fileName = Worksheets("inputhysys").Range("c2")                 If fileName <> "False" And simCase Is Nothing Then            Set simCase = GetObject(fileName, "HYSYS.SimulationCase")            simCase.Visible = True        End If    End If 

Note: I put directory address of Hysys file (HSC file) in cell C2. And the address is H:\BLOG\hysys\example1.hsc.
PUT HSC FILE IN YOUR DIRECTORY, MAKE SURE YOU EDIT  EDIT THE  DIRECTORY ADDRESS, OR HYSYS FILE WILL NOT BE LOADED

How to Access Hysys Streams from Excel?
Put this code to make link to Hysys Streams
Public FEED1 As ProcessStream, FEED2 As ProcessStream
Set FEED1 = simCase.Flowsheet.MaterialStreams.Item("FEED1")
How to Export Data from Excel To Hysys Simulation?
How to Import Data from Hysys Streams to Excel File?
Write down this code to export and import data from and to excel



Set FEED1 = simCase.Flowsheet.MaterialStreams.Item("FEED1")Worksheets("Sheet1").Range("D6").Value = FEED1.MassFlow.GetValue("LB/HR")       ' import data MASS FLOWRATE from hysys streamsWorksheets("Sheet1").Range("D7").Value = FEED1.Pressure.GetValue("PSIG")       ' import data PRESSURE from hysys streamsWorksheets("Sheet1").Range("D8").Value = FEED1.Temperature.GetValue("C")       ' import data TEMPERATURE from hysys streams


Set FEED2 = simCase.Flowsheet.MaterialStreams.Item("FEED2")    PRESSFEED2 = Worksheets("Sheet1").Range("H7").Value    FEED2.Pressure.SetValue PRESSFEED2, "PSIA"         ' export pressure    FLOWFEED2 = Worksheets("Sheet1").Range("H6").Value    FEED2.MolarFlow.SetValue FLOWFEED2, "MMSCFD"      ' export molar flowrate    TEMPFEED2 = Worksheets("Sheet1").Range("H8").Value    FEED2.Temperature.SetValue TEMPFEED2, "F"           ' export temperature

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Hysys file can be found here
Excel file can be found here

MAKE SURE TO PUT CORRECT DIRECTORY ADDRESS OF HYSYS FILE IN CELL

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Here is complete code from example.xls

Option Explicit
Public hyApp As HYSYS.Application
Public simCase As SimulationCase
Public FEED1 As ProcessStream, FEED2 As ProcessStream




Public Sub StartHYSYS()
Dim fileName As String
Dim PRESSFEED2 As Double, FLOWFEED2 As Double, TEMPFEED2 As Double
    
    ' LOADING HYSYS TUTORIAL FILE
    Set hyApp = CreateObject("HYSYS.Application")
    hyApp.Visible = True
    Set simCase = hyApp.ActiveDocument
    If simCase Is Nothing Then
        fileName = Worksheets("Sheet1").Range("c2")
        
        If fileName <> "False" And simCase Is Nothing Then
            Set simCase = GetObject(fileName, "HYSYS.SimulationCase")
            simCase.Visible = True
        End If





    End If
     
    Set FEED1 = simCase.Flowsheet.MaterialStreams.Item("FEED1")
    Worksheets("Sheet1").Range("D6").Value = FEED1.MassFlow.GetValue("LB/HR")       ' import data MASS FLOWRATE from hysys streams
    Worksheets("Sheet1").Range("D7").Value = FEED1.Pressure.GetValue("PSIG")       ' import data PRESSURE from hysys streams
    Worksheets("Sheet1").Range("D8").Value = FEED1.Temperature.GetValue("C")       ' import data TEMPERATURE from hysys streams
    
    
    Set FEED2 = simCase.Flowsheet.MaterialStreams.Item("FEED2")
    
    PRESSFEED2 = Worksheets("Sheet1").Range("H7").Value
    FEED2.Pressure.SetValue PRESSFEED2, "PSIA"         ' export pressure
    
    FLOWFEED2 = Worksheets("Sheet1").Range("H6").Value
    FEED2.MolarFlow.SetValue FLOWFEED2, "MMSCFD"         ' export molar flowrate
    
    TEMPFEED2 = Worksheets("Sheet1").Range("H8").Value
    FEED2.Temperature.SetValue TEMPFEED2, "F"          ' export temperature
End Sub



+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Hysys Stream Parameters and units can be found here. REMEMBER TO TYPE UNIT AND PARAMETER AS DESCRIBED HERE.