0% found this document useful (0 votes)
22 views

Automation Example - Creating A Case From Scratch

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Automation Example - Creating A Case From Scratch

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Automation example - Creating a case from scratch

Products: Aspen HYSYS


Last Updated: 03-Nov-2020
Versions: V14.3, V14.2, V14.0, V12.1, V12.0, V11.0, V10.0
Article ID: 000065873
Primary Subject: Programmability/Extensibility
Converted from "113794_Default.txt"

Creation Date: 05-Jul-2004 10:15AM

Applicable Version(s)
All

Problem Statement
Automation example - Creating a case from scratch

Solution
The example code below illustrates this. To use the code: open up HYSYS, paste the code into the VBA
editor in Excel, set the code to reference the required case file name, make a reference to the HYSYS type
library (Tools ... References menu option in the VBA editor) and run the procedure. For troubleshooting
advice on common HYSYS / OLE Automation errors see Knowledgebase Solution #112361.

Public Sub CreateCase()

'Description: Illustrates how to make a new case, add a fluid package


' add components, and make a simple flowsheet
'
' AspenTech support Knowledgebase #113794
'
'Declare Variables---------------------------------------------------------
Dim hyApp As HYSYS.Application
Dim hyCase As HYSYS.SimulationCase
Dim hyBasis As HYSYS.BasisManager
Dim hyFluidPkg As HYSYS.FluidPackage
Dim hyPRFluidPkg As HYSYS.PengRobinsonPropPkg
Dim hyFlwSht As HYSYS.Flowsheet
Dim hyStrm As HYSYS.ProcessStream
Dim hyCool As HYSYS.CoolerOp
Dim hySep As HYSYS.Separator

Dim strCase As String


Dim varComps As Variant
Dim i As Integer

'Procedure-----------------------------------------------------------------
'Link to the currently open app
Set hyApp = CreateObject("HYSYS.Application") 'Links to open instance or starts a new one
hyApp.Visible = True

'Create a new case

'First delete any existing case of the same name - if this isn't done then the .Add line below
'opens the existing case
strCase = "d:\Temp.hsc"
If Dir(strCase) <> "" Then Kill (strCase)

Set hyCase = hyApp.SimulationCases.Add(strCase)


hyCase.Visible = True

'Link to Basis
Set hyBasis = hyCase.BasisManager

'Since we started a new case we'll be in the basis environment anyway - could check for this a
If hyBasis.IsChangingBasis = False Then
'Turn off the solver - if we do this now it prevents HYSYS asking us if we want to
'be in Hold mode when we come out of the basis.
hyCase.Solver.CanSolve = False
hyBasis.StartBasisChange
End If

'Add a new fluidpackage - the parameter passed is the "Name" of the FP as it will
'appear in the Basis Manager
Set hyFluidPkg = hyBasis.FluidPackages.Add("New FP")
'Set the type of the fluid package
hyFluidPkg.PropertyPackageName = "PengRob"

'confusingly in some cases (e.g. PR) this is different to the type you see if you examine
'the PropertyPackageName property using the watch window
'All Property Package names can be found in Techtip #109180 in the Knowledgebase

'Can now go into the PR Property Package if we want


Set hyPRFluidPkg = hyFluidPkg.PropertyPackage

'See Techtip #112864 for details of setting property package parameters

'Now add some components


With hyFluidPkg.Components
.Add "Methane"
.Add "Ethane"
.Add "Propane"
.Add "i-Butane"
.Add "n-Butane"
.Add "Water"
End With

'Go back to the Simulation environment


If hyBasis.CanEndBasisChange = True Then
hyBasis.EndBasisChange
Else
MsgBox "Couldn't finish Basis Change"
Exit Sub
End If

'Switch off Solver - stops HYSYS trying to solve after every change is made
hyCase.Solver.CanSolve = False

Set hyFlwSht = hyCase.Flowsheet

'Add a Stream, make it equimolar at given conditions


Set hyStrm = hyFlwSht.MaterialStreams.Add("Feed Stream")
With hyStrm
.Pressure.SetValue 10, "bar"
.Temperature.SetValue 25, "C"
.MolarFlow.SetValue 100, "kgmole/h"

'Initialise composition array


varComps = .ComponentMolarFraction.Values
'Make it equimolar
For i = 0 To UBound(varComps)
varComps(i) = 1 / (UBound(varComps) + 1)
Next 'i
.ComponentMolarFraction.Values = varComps

End With

'Add a downstream cooler and separator


'[Find the operation Type by linking to an existing operation and looking at its .TypeName pro
Set hyCool = hyFlwSht.Operations.Add("Feed Cooler", "coolerop")
Set hySep = hyFlwSht.Operations.Add("Feed Separator", "sep3op")

'Connect them up
hyCool.FeedStream = hyStrm
Set hyStrm = hyFlwSht.EnergyStreams.Add("Cooler Q")
hyCool.EnergyStream = hyStrm
Set hyStrm = hyFlwSht.MaterialStreams.Add("Cooler Out")
hyCool.ProductStream = hyStrm
hySep.Feeds.Add hyStrm
Set hyStrm = hyFlwSht.MaterialStreams.Add("Vapour")
hySep.VapourProduct = hyStrm
Set hyStrm = hyFlwSht.MaterialStreams.Add("Liquid")
hySep.LiquidProduct = hyStrm
Set hyStrm = hyFlwSht.MaterialStreams.Add("Water")
hySep.HeavyLiquidProduct = hyStrm

'Set parameters
hyCool.PressureDrop.SetValue 0.5, "bar"
hyCool.ProductTemperature.SetValue 10, "C"

'Switch on Solver
hyCase.Solver.CanSolve = True

'Save the Case


hyCase.Save

'Close (False parameter = without saving changes)


hyCase.Close False

End Sub

Note
The Knowledge Base examples are provided for academic purposes only and as such are not subject to the
quality and support procedures of officially released heritage-Hyprotech products. Users are strongly
encouraged to check performance and results carefully and, by downloading, agree to assume all risk related
to the use of these examples. We invite any feedback through the normal support channel at
[email protected].

KeyWords
Case, New

You might also like