0% found this document useful (0 votes)
9 views38 pages

HYSYS Automation Basics with Excel

The document provides an introduction to Automation in HYSYS, explaining how users can programmatically interact with the application using tools like Microsoft Excel and Visual Basic. It outlines the benefits of Automation, such as improving efficiency and reducing human errors in repetitive tasks, and includes a workshop section for practical application. Additionally, it covers prerequisites, basic VBA techniques, and the object hierarchy within HYSYS for effective coding and debugging.

Uploaded by

Son Pham
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)
9 views38 pages

HYSYS Automation Basics with Excel

The document provides an introduction to Automation in HYSYS, explaining how users can programmatically interact with the application using tools like Microsoft Excel and Visual Basic. It outlines the benefits of Automation, such as improving efficiency and reducing human errors in repetitive tasks, and includes a workshop section for practical application. Additionally, it covers prerequisites, basic VBA techniques, and the object hierarchy within HYSYS for effective coding and debugging.

Uploaded by

Son Pham
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

Automation Introduction 1

Automation Introduction

1
© 2004 AspenTech - All Rights Reserved.
EA1000.32.02
12 Automation Introduction
2 Automation Introduction

Introduction
Automation is the ability to programmatically interact with an
application through objects exposed by that application.

By using an Automation client like Microsoft Excel, or Visual Basic, the


end user can write code to access these objects and interact with HYSYS.
Code can also be written in HYSYS itself in the form of User Variables or
Macro Language Editor (MLE) macros. The available objects are the
same.

The exposed objects make it possible to programmatically perform


nearly any action that can be accomplished through the HYSYS
graphical user interface.

Workshop
In this module you will review and begin to understand an Automation
front-end to the Turbo Expander case using Microsoft Excel.
Additionally a simple HYSYS User Variable will be created.

Learning Objectives
In this module, you will gain an understanding of the possibilities that
Automation access to HYSYS can bring. The examples given should give
a starting point for any further learning.

If you want to learn more about Automation programming with HYSYS,


AspenTech offers another course that will meet your needs. Ask the
instructor for more information.

2
Automation Introduction 3

Prerequisites
No prior programming experience is assumed. However before
beginning this module, you should have a reasonable understanding of
the HYSYS program.

Why Use HYSYS Automation?


The main reason for using the Automation capabilities of HYSYS is to
improve the efficiency of your work processes. By reducing the amount
of time spent on repetitive, tedious tasks, and hence by reducing the
amount of human errors, more time is left for engineering tasks.

The efficiency of your work processes can be increased by using HYSYS


Automation for:

• Automating manual data entry tasks, for example extract HYSYS


data to an Excel spreadsheet for delivery to a vendor.
• Creating hybrid solutions across applications, for example allow
for seamless data transfer between HYSYS and any other
Automation enabled application.
• Hide the complexity of HYSYS while taking advantage of its full
capabilities, for example build custom front ends for plant
personnel.
• Extend the functionality of HYSYS to meet particular needs, for
example use a User Variable to report custom stream properties.

The benefits that you will see from HYSYS Automation will depend
entirely on what you use HYSYS for. If you find yourself performing the
same task or calculation several times during a project, writing a MLE
macro or a User Variable will save a lot of valuable project time. This will
be especially true if the calculation is complex or requires several
variables simultaneously.

3
4 Automation Introduction

Excel Front-End to the Turbo Expander Plant


A simple front end to the Turbo Expander plant has been constructed
using Microsoft Excel.

Rather than typing a large amount of code into the Excel Visual Basic
Don’t worry if you haven’t Editor, in this Workshop you will review some prewritten code using
built the Turbo Expander
plant case. VBA's debugging tools.
The file
“ADV5_Spreads&CaseStud The instructor may choose that the class does this individually, or as a
_Soln.hsc” contains this group.
case.

Preparation
1. Open the Turbo Expander HYSYS case.
2. Open the Microsoft Excel file: “Adv Automation - [Link]”.
3. In order to use VBA macros in Excel you need to tell Excel to Enable
Macros.

Figure 1

In Excel 2000 you may need to change the Security setting to Medium
on the Tools... Macro... Security menu option, before you see this
window on opening the file.

The Excel spreadsheet has already been set up with some labels and
values.

4
Automation Introduction 5

Figure 2

4. Open the Visual Basic Macro Editor by going to Tools... Macro...


Visual Basic Editor, or by pressing Alt + F11.

5
6 Automation Introduction

The code to link to HYSYS will now appear.

Figure 3

Making a Type Library reference


The first step in accessing HYSYS via VBA is to make a Type Library
reference.

1. In the VBA Editor go to the Tools … References menu option.


2. Ensure the HYSYS 3.0 Type Library entry is checked.

6
Automation Introduction 7

Figure 4

Don’t worry if the type


library version number
doesn’t correspond to the
HYSYS version being used.
Just check that the Location
is for the correct HYSYS
version.

VBA Basics
The intention of this section is to introduce the very basics of
manipulating VBA code rather than to teach the details of HYSYS VBA
programming.

The techniques described here will be used later when examining the
prewritten code.

Running Macros
To run a macro, place the cursor within the macro in the VBA Editor, and
click the Run Sub / UserForm toolbar button.

7
8 Automation Introduction

Figure 5

Macros can also be run within the VBA Editor, from the Run menu or by
pressing F5.

Additionally it is possible to trigger a macro by clicking a button on the


worksheet. The example spreadsheet has a button to do this. (To set
which macro is triggered, right-click on the button and choose Assign
Macro.)

Simple Debugging - Breakpoints


In this module the example code will be run in Break mode. This allows
Breakpoints cannot be
placed on comment lines or the code to be stepped through one line at a time. This helps to gain
variable declaration lines. understanding of what the code is doing, and is also useful when fixing
Comment lines are those bugs.
starting with ‘ marks, that
appear green in the VBA
editor. These are ignored To make VBA enter Break mode it is first necessary to add a breakpoint.
when the code runs. First select the position in the code at which to add a break point then
either:

• Select Toggle Breakpoint from the Debug menu in the main


menu bar.
• Press the F9 hot key.
• Click in the grey column on the left side of the code window
beside the desired breakpoint location.
• Use the Toggle Breakpoint toolbar icon.

Figure 6

8
Automation Introduction 9

Next trigger the code as above. When VBA encounters the breakpoint
execution will stop and the code window will appear. The line of code
that is about to be processed is highlighted in yellow.

There are a number of ways to step through the code:

• Select Step Into from the Debug menu.


• Press the F8 hot key.
• Use the Step Into toolbar icon.

Figure 7

HYSYS Automation Basics


Each object within HYSYS, for example: a stream, the flowsheet, a case,
or even the HYSYS application itself has a corresponding Automation
object. It is via these objects that HYSYS can be accessed and controlled
through code.

This example illustrates access to some of the most commonly used


Automation objects within HYSYS.

9
10 Automation Introduction

HYSYS Objects are organised into a tree: The object hierarchy. The
objects that will be accessed in this example are illustrated below:

Figure 8

The first stage of linking to HYSYS is to link in to an object at the top of


the tree. In this example the line of code:

Set hyApp = GetObject(, “[Link]”)

is used. This sets up the object variable hyApp to refer to the HYSYS
application. The resulting object is of type Application.

10
Automation Introduction 11

To link to further objects within the tree, dot notation is used. For
example the Turbo Expander Excel interface uses the line:

Set hyCase = [Link]

to refer to the currently active simulation. The resulting object is of type


Simulation Case.

i.e. to refer an object on the next layer down in the tree a full stop is
used.

Using the Object Browser


The objects available within HYSYS, and the HYSYS object hierarchy can
be viewed using a VBA tool called the Object Browser. To view the Object
Browser either:

• Select Object Browser from the View menu.


• Press the F2 hot key.
• Use the Object Browser toolbar icon.

Figure 9

The object browser window will appear. By default the object browser
will list the objects available in all the type libraries that are selected in
the References lists (Tools … References). In order to limit it to just
HYSYS objects change the <All Libraries> drop-down at the top left to
HYSYS.

11
12 Automation Introduction

Figure 10

Clicking on an object (or 'Class') in the left hand list then shows all the
members (objects or properties) that are associated with that object.
The Object Browser also allows searching.

Using Watches to monitor Variables


Variables are key to any programming language. They hold values that
can be manipulated by the program as it runs.

It is good practice to declare When accessing HYSYS via Automation, an Object Variable can be used
all variables at the top of the in the code to link to a HYSYS object.
procedure.

12
Automation Introduction 13

By adding a Watch it is possible to see how the value of a variable


changes as the code executes in Break Mode.

First the Watches window must be shown. Do this by choosing the


Watch Window option in the View menu, or by using the Watch Window
toolbar icon.

Figure 11

The Watches window will then appear.

Figure 12

Secondly select the variable of interest in the code by clicking and


dragging, then either:

• Drag the selected variable onto the Watches window.


• Choose Add Watch… or Quick Watch… from the Debug menu
• Press the SHIFT F9 hot key.

If the variable is an object variable then it will appear with a '+' sign next
to it, clicking this will show any sub-objects and properties within the
object.

The Watches window illustration above shows what happens if the


hyApp object is added to the watch window and the code run in break
mode.

13
14 Automation Introduction

Running the Code in Break Mode


1. First look at the top of the procedure. It is good practice to include a
description of what the procedure does at the beginning. All the
variables are then declared. Note that the variables that will be
linked to HYSYS objects are declared as specific types depending on
the kind of object they are linked to.

Figure 13

14
Automation Introduction 15

After the variable declarations, there is a VB instruction that sets what


happens when an error occurs. In this case, jump to the label
ErrorHandler at the bottom of the code, and display an error message.

Figure 14

2. Add a breakpoint to the On Error line and start the code running. (If
you need to, you can refer back to page 8 for details of breakpoints,
and making the code run.) Make the Set hyApp … line execute.

Figure 15

3. Add a Watch for the hyApp variable, click the '+' sign next to the
variable to show the properties of this object, note that
ActiveDocument is one of them, and that it's type is SimulationCase.

15
16 Automation Introduction

4. Execute the next line of code, add a Watch for hyCase and note that
it shows the same information as the ActiveDocument property of
the hyApp object.
5. Use the object browser to view the HYSYS Application object. Note
again that the type of the ActiveDocument property is
SimulationCase.

Hence when hyCase was declared, it's type was SimulationCase.

Figure 16

The next section of code checks if there was a case open in HYSYS using
the VB 'Is Nothing' construction.

Figure 17

16
Automation Introduction 17

Try running the code with no case open. What happens now?

What is the value of the hyCase variable in the Watches window?

Next some values are read in from the Excel spreadsheet.

Figure 18

There are a number of ways to refer to a particular cell in the


spreadsheet:

• Range() with a cell name. e.g. Range(“C15”).Value


• Range() with a named range. e.g. Range(“MyRange”).Value
• Cells() with a row column reference. e.g. Cells(15,4).Value

Try adding a Watch for [Link](“C15”), what kind of properties


does this have?

What is the type of this variable? Where in the Object Browser could this
object type be found?

17
18 Automation Introduction

6. Run the code down to the Set hyStream = …. line

Figure 19

Here a particular stream within the flowsheet is placed into the


hyStream object variable.

7. Look up SimulationCase in the Object browser and navigate


through to the MaterialStreams property of the Flowsheet object.
Note that it is important to
enclose the stream name in
“quotation marks”. This is a Collection. In programming terms Collections are objects that
hold a group of sub-objects - in this case all the material streams in the
flowsheet. The Item property is used to refer to a specific stream.

8. Add a Watch for the hyStream object. Look at all the properties this
has. Note that the Type of this variable is ProcessStream.
9. Execute the [Link] = False line. In HYSYS the solver
is now turned off (Red Traffic light).

Now the temperature in the stream can be set using the two values that
were retrieved from the Excel spreadsheet earlier.

Figure 20

10. Execute this line - check that the value in the HYSYS case matches
that entered in Excel.
11. Look up the ProcessStream object in the Object Browser and find
the Temperature property. This is of type RealVariable.

18
Automation Introduction 19

12. Look up RealVariable in the object browser.

Figure 21

RealVariables are special HYSYS objects that hold numerical values, but
also contain other information that is useful for the programmer.

• IsKnown. Whether the value is known or is <empty>


• CanModify. Whether the value can be changed (i.e. True = a
Blue HYSYS number, False = Black, calculated by something
Method icon in the Object
else)
Browser • CalcBy. Which HYSYS object calculated the value
• UnitConversionType. What kind of unit the value has

RealVariables also have two useful methods: SetValue and GetValue.


These allow numbers to be put into, and retrieved from HYSYS. In
general Methods are used to tell objects to do something.

19
20 Automation Introduction

The SetValue method takes two parameters:

Sub SetValue(val As Double, [unit])

The first parameter is the new value to be set, and the second is the unit
to set the value in. Note that the square brackets mean that the second
parameter is optional. If it is not supplied then HYSYS assumes the value
is being supplied in it's internal units. (°C in the case of temperature.)

Look at the Temperature in the Watch window. What are the values of some
of the properties listed above?

13. Execute the code down to the line just above where the HYSYS solver
is turned back on.

Figure 22

This section of code is similar to the section above where a temperature


is set. However instead of referring directly to a named stream, the
product stream from a given operation is used. The flowsheet object has
another collection called Operations that includes all the operations on
the flowsheet.

14. Add a Watch for the hyExpander object and look at some of the
properties it has.

20
Automation Introduction 21

15. Reposition the VBA editor window and the HYSYS window so that
both are visible.
16. Execute the line [Link] = True, and observe that
HYSYS resolves the case.

The next parts of the code retrieve various information from the newly
modified HYSYS case.

Figure 23

Since the “Comp-HP” stream is on a sub flowsheet, it must be accessed


from an object for the sub flowsheet. Like the MaterialStreams
collection, each flowsheet has a collection (called Flowsheets) which
contains objects for all it's sub flowsheets. Hence the Item property is
used on the Flowsheets collection of the main flowsheet, to refer to a
particular sub flowsheet within the collection.

Flowsheets are referred to by their Tag (not their Name). This can be
found on the Connections tab.

21
22 Automation Introduction

Next a new stream is assigned to the hyStream variable, using the Item
property of the EnergyStreams collection of this new sub flowsheet
object. The EnergyStreams collection contains only the Energy streams
in the flowsheet.
Variables can be reused
when they are finished with. The ProcessStream object type can hold both Material and Energy
There is no need to declare
a different variable for each stream objects, however Energy streams have far fewer accessible
stream or operation that is properties. In this case the HeatFlow property is used.
accessed.
The GetValue method is then used. This is analogous to the SetValue
method except that it takes only one parameter: the required unit.

17. Execute the Set hyStream = … line.


18. Look at the Watch for the hyStream variable now - Note that it now
carries details of the energy stream.

Is there a way to tell whether a ProcessStream object refers to a material or


energy stream?

Hint: Use the object browser to look for properties of the


ProcessStream object starting with 'i'.

Similarly the compressor duty and LPG product mass flow are obtained.
Note that there is no need to obtain an object variable for a stream or
operation before running the GetValue method against one of it's
properties. However if the same object is to be used more than once it
makes sense to create an object variable for it.

Figure 24

22
Automation Introduction 23

19. Execute the code above.

The next section of the code retrieves some additional values from some
operations on the main flowsheet.

Figure 25

20. Execute this section of the code.

The final sections of the code deal with obtaining component specific
properties.

Figure 26

21. Execute this section of the code.

23
24 Automation Introduction

Note that each time execution reaches the Next IntCount line, it jumps
back up to Set hyCpt = … line. This is the first time a VB loop has been
used in this code. Each time the loop executes the value of the IntCount
variable is increased until it reaches the number of components minus
one.

Why shouldn't the loop count up to [Link]?

All the HYSYS collections are “zero based” - The first term is at position
zero.

The result of this loop is that if Methane is present in the case then it's
position in the list of components will have been placed into the
IntCH4Idx variable.

Figure 27

24
Automation Introduction 25

All HYSYS values that contain arrays of data (e.g. component mass
fractions, or mole flows, or tray by tray data in a column) have a special
object type called RealFlexVariable.

22. Look up the ComponentMolarFraction parameter of the


ProcessStream object in the Object Browser and confirm that it is of
type RealFlexVariable.

RealFlexVariables have methods called SetValues and GetValues, which


are analogous to those for RealVariables except that the value passed or
returned is a Variant. This is a special kind of VB variable that can
contain any kind of data. In this case it contains an array of data.

23. Note that at the top of procedure VarHyArray is defined as type


Variant.
24. Run the code above, when the VarHyArray = … line is reached, add a
Note that the CanModify Watch for VarHyArray.
property of the
RealFlexVariables also
returns a Variant array of Figure 28
Boolean (True / False)
values.

What is the value in this array at the position held in the IntCH4Idx
variable? Does this agree with the Methane mole fraction displayed in
HYSYS?

Finally all the component mass fractions in the Product LPG stream are
to be reported.

25
26 Automation Introduction

Figure 29

25. Execute this section of the code.

Here the VarHyArray variant array is first filled with the names of all the
components and then the component mass fractions, and each are
written into the spreadsheet.

Note that instead of using [Link]() as


above, this time the ComponentMassFractionValue property of the
ProcessStream object is used.

26. Examine the ProcessStream object in the Object Browser. Note that
each of the properties also has a corresponding …Value property.
(e.g. Temperature and TemperatureValue or Pressure and
PressureValue)

What are the types of the …Value properties?

26
Automation Introduction 27

In each case the …Value property simply returns a value of type Double
(in the case of single valued properties like temperature), or Variant (in
the case of component properties), in the HYSYS internal calculation
units.
RealVariables and
RealFlexVariables also have
properties called Value and Rather than use the...Value properties of the ProcessStream object, it is
Values, which return generally better practice to use [Link](s) property of the
numbers in HYSYS internal Real(Flex)Variable.
units.

The final lines of the main code, clear all the object variables and then
instruct VBA to skip the error handler if no error has occurred.

Figure 30

27
28 Automation Introduction

HYSYS User Variables


Introduction
User Variables can be used to add to the internal functionality of HYSYS
objects, such as streams and unit operations, by attaching variables and
code to those objects from within HYSYS itself. User Variables can be
used like the variables built in to HYSYS objects; so can be added to
spreadsheets, targeted by logic controllers, have their values specified by
user input, etc.

Typical uses for User Variables are:

• Calculation of Custom Properties:


• Calculation of a dew point temperature.
• Automation of actions:
• Automatically adding an energy stream each time a pump is
added.
• Adding extra intelligence to the HYSYS model:
• Relating the pressure drop through a heater to the flow rate.

The first two examples can be found in Section 5.7 of the Customization
Guide. In this exercise a User Variable will be implemented that relates
the heater pressure drop to the flow rate in one of the exchangers in the
Turbo Expander Case.

28
Automation Introduction 29

Adding a User Variable


The location of the User Variable information within the HYSYS
property windows, depends on the object they are being added to.

Object Location
Operations (except Logicals) Design tab... User Variables page
Streams Worksheet tab... User Variables page
Logical Operations User Variables tab
Flowsheet Flowsheet menu... Flowsheet User
Variables option
Simulation Case Simulation menu... Simulation Case
User Variables option

When any of these locations is opened the view will be similar. (Below is
the view for a stream).

Figure 31

29
30 Automation Introduction

To add a new User Variable, click the Create New User Variable icon. The
Create New User Variable dialog box then appears.

Create New User Variable Figure 32


icon

The only User Variables that


can run in Dynamics are the
DynComp... and DynPres...
User Variables for
operations.

These are called before


each composition / pressure
flow spec respectively.

This window is the same for all types of user variables. The only
difference is the available macro types.

Stream Operation Flowsheet Simulation Case

The only way to close the This choice sets when the code gets called. For Streams, Operations and
window and save any Flowsheets the choices are before and after the object does it's
changes made is to click the
OK button. calculations. Simulation Case user variables are only ever executed
when the user clicks the button on the User Variable window.

Before the User Variable code window can be closed (by clicking OK), a
name must be set for the User Variable.

30
Automation Introduction 31

The Show / Hide Variable Details icon (green triangle) in the top right
corner of this window is used to toggle the display of the User Variable
details tabs.
Show / Hide Variable
Details icon In order to edit an existing User Variable, click the Edit the Selected User
Variable icon on the User Variable page of the HYSYS object, or double-
click the user variable value cell.

Edit the Selected User


Variable icon
Important User Variable Parameters
Two of the most important parameters are:

1. User Variable Type

Changing the combobox values at the top-right allows the user to


choose the type of User Variable.

Figure 33

- Numeric (Real), Text, Code Only...


- Single Value (Scalar), or vectors,
matrices or cubes
- Units of any numeric values
(Temperature, Pressure...)

2. User Variable Activation

Setting the Activation parameters on the Attributes tab, tells HYSYS on


which objects to enable the User Variable. (Automatic = Enabled on all
objects of the same type, User Enabled = Allow the user to pick on which
objects the User Variable should be enabled.)

Figure 34

31
32 Automation Introduction

If the User Variable doesn't appear on the User Variables page of an


object on which it is to be enabled it may be necessary to ensure the
Show / Hide Variable Enabling Checkboxes button (the green tick) is
selected.

Figure 35

A more comprehensive description of the User Variable code window


can be found in Section 5 of the HYSYS Customization Guide.

Writing User Variable Code


The HYSYS VB code editing window offers most of the same
functionality of the VBA editor found in Microsoft Excel.

Breakpoints can be added by clicking in the left margin or clicking the


Toggle Break toolbar icon. When running in break mode, Watches can
be set on particular variables.

There is also a built in Object Browser. Since the code is executed from
HYSYS there is no need to make a reference to the HYSYS type library -
this is already set internally.

The code that is written in the User Variable uses almost exactly the
same VBA syntax as when accessing HYSYS from Excel. The only
difference is how to connect into the HYSYS object hierarchy, and how
to interact with the displayed user variable value.

32
Automation Introduction 33

Accessing Top Level Objects


Two built-in objects allow entry into the object hierarchy

1. ActiveObject
• This returns on object for the owner of the code. The type of this
object depends on what kind of User Variable is using it:
• Stream User Variable = Returns a ProcessStream type object for
the stream containing the user variable.
• Operation User Variable = Returns an object for the operation
containing the user variable. (Type of object depends on the type
of operation.)
• Flowsheet User Variables = Flowsheet object for owner
flowsheet.
• Simulation Case User Variables = Not supported - use
ActiveCase object instead.
2. ActiveCase - Always returns a SimulationCase object for the case
containing the User Variable.

Interacting with the User Variable Value


Using the code [Link] will return an
object for the User Variable. The type of object depends on the user
variable type.

User Variable... Type ... Dimensions Object Type


Real Scalar RealVariable
Vector, Matrix, Cube RealFlexVariable
Enumeration Scalar RealVariable
Vector RealFlexVariable
Text Scalar TextVariable
Vector TextFlexVariable
Code Only No variable available

Hence the methods GetValue() or SetValue() can then be used as in the


Excel - HYSYS example.

33
34 Automation Introduction

Importing/Exporting User Variables


You may import and export User Variables between cases via the Import
User Variables are saved and Export User Variables window. (Access this by going to the
into files with .huv Simulation … Import and Export User Variables option.)
extensions.

Figure 36

Exporting a User Variable


1. Open the Import and Export User Variables view.

A list of User Variables currently attached to the case is displayed in the


User Variables in Case group. The list box on the right displays a list of
variables attached to the object (stream, operation, flowsheet, or
simulation case) selected in the list box on the left.

2. Select the User Variable to export then click the Export button.
3. On the file dialogue that appears set the file name of the required
User Variable export file (This will have a .huv extensions).

Importing a User Variable


1. Open the Import and Export User Variables view.
2. Click the Select File button, then navigate to the location of the .huv
file.
3. Select the variable you wish to import and click the Import button.

34
Automation Introduction 35

Exercise - User Variables


A simple User Variable that relates the pressure drop on a Cooler to the
Don’t worry if you haven’t
built the Turbo Expander mass flow rate will be demonstrated.
plant case.
Again, rather than typing a large amount of code into the User Variable
The file code window, in this Workshop you will review use some prewritten
“ADV5_Spreads&CaseStud
code.
_Soln.hsc” contains this
case.
1. Open the Turbo Expander HYSYS case.
2. Add a User Variable to Cooler E-101 (the Recompressor after cooler)
- See page 28 for guidance on how to do this.
3. Set the following User Variable parameters:

Parameter Value
Name Pressure Drop Calc
(Tag will automatically be set to the
same)
Type Real
Dimensions Scalar
Units Pressure Drop
Execution (Macro tab) PreExecute()
Activation (Attributes tab) User Enabled
Variable... Calculate Only (Security tab) Checked

35
36 Automation Introduction

4. Either type the code below into the code window, or paste it from
the supplied text file. (“Adv Automation - UV [Link]”)

Figure 37

36
Automation Introduction 37

Figure 38

5. Place a breakpoint on the Sub PreExecute() line, then make the code
run by changing the flow rate of the Feed Gas stream.
6. Step through the code and ensure that it is behaving as expected.

Note that the first time the code is called after the change is made, the
mass flow through the cooler is not known hence the code in the If
It’s best to trigger the User [Link]=True Then … End If section is not
Variable for debugging by executed.
changing a value in the
flowsheet, and hence
making HYSYS call the This is because when the solver performs steady state calculations there
code. are two solve passes which it performs: the forget pass and the calculate
pass. When the value of a variable changes, the solver first does one
If the code is triggered by solve pass with the value marked as unknown. This is the forget pass.
clicking the Start / Resume
toolbar button then any
This allows HYSYS to correctly propagate the effects of any change.
ActiveObject references will
not point to the correct 7. Export the User Variable and import it into another case. (You could
object. try one of the HYSYS sample cases, or one of the solution cases for
this course.)

Challenge
Try using this code in a similar User Variable for a Heater operation or a
Valve. Does it work? What modifications need to be made?

Try adapting the code to base the pressure drop on some other
parameter - for example the molar flow, or the density, or the
composition of a particular component.

37
38 Automation Introduction

38

You might also like