Академический Документы
Профессиональный Документы
Культура Документы
Extn 6.pdf
2 Enhancing Your Extensions
Introduction
This module will introduce you to several advanced topics that you will
use to enhance the functionality of the unit operation extension
created in the previous module.
While the extension that was created in the previous module worked
adequately, there are several things that we can do to improve its
operation and enhance its appearance.
In this module, we will change both the EDF and the DLL to improve
the extension. Therefore, this module will allow you to combine your
knowledge on these two areas of extension design.
Learning Objectives
This module will teach you how to enhance your extensions by adding
plots and other options to an extension.
Prerequisites
As this module will build upon material that was covered in two
previous modules (Creating an EDF and Unit Operation Extensions), it
is vital that you understand the material that was presented there
before you begin this module. If you have any questions about the
material presented in those two modules, please ask the instructor to
explain it again.
Also, it is important that the extension that you created in the previous
module be functioning properly before you start to enhance it. If your
extension is not operating properly, ask the instructor to help you
complete the previous module.
2
Enhancing Your Extensions 3
Background
Recall that the extension that was created in the last module modelled a
natural gas wellhead. The extension was fully functional in that it
accepted the P-Q data entered by the user and calculated the unknown
properties in the simulation. This is the minimum that we would expect
from any operation in HYSYS.
We are now left with one question. Can our extension have all of these
features? The answer is: Yes. This module will teach you how to add
these features to the extension created in this course. This will allow
you to add similar features to any extension that you create.
For the extension that was created in this course (the wellhead
modeller), one of the most desirable enhancements would be a plot to
display the P-Q data in a graphical format. This will involve changes to
both the EDF and the DLL. As well, the code for the extension will be
modified to improve its efficiency. Both of these improvements will be
done in the Workshop portion of this module.
3
4 Enhancing Your Extensions
Workshop
Writing Efficient Code
One of the principles of creating efficient code is that each command
and calculation be performed only when necessary. This reduces or
eliminates duplicate and repetitive calculation that increase the
running time and the memory required by the extension.
Can you see any part of the code for this extension that
would allow the extension to run more efficiently if it was
placed in another location, yes or no? __________
What improvements do you recommend?
___________________________________________________
Look at the Execute subroutine in this code; each time that this
subroutine runs, the object references for the streams are reset. This is
unnecessary and inefficient. The extension would perform better if this
code was placed in a method that only ran if the streams were changed.
Case "FeedStream"
Set Feed = myContainer.FindVariable("FeedStream").Variable.object
Case "ProductStream"
Set Product = myContainer.FindVariable("ProductStream").Variable.object
4
Enhancing Your Extensions 5
Once these lines are in place, the lines in the Execute method that
performed the same task can be removed, if they have not been already.
If desired, and if time permits, you may want to test the revised code of
the extension, either run the extension in VB or recreate the DLL. You
should be aware that you will probably not be able to notice an
improvement in the performance of the extension. Any improvement
may only be seen in large simulations where multiple extensions of the
same type are used; this allows a small improvement to be multiplied
several times so that it can be seen.
The plot that will be added will be on the same tab as the table; and a
Visibility controller will be used to determine whether the plot or the
table will be visible.
1. Open the Extension View Editor application, and retrieve the EDF
file that was created for the unit operation extension in a previous
module.
5
6 Enhancing Your Extensions
Visibility controllers are added from this view by pressing the Create
Controller button. Once the controller is created, it can be edited to
match our requirements by pressing the Edit button within the new
controller’s group box. The Visibility controller’s properties page looks
like this:
6
Enhancing Your Extensions 7
Complete the Property View for the Radio buttons so that it looks like
this:
Note that in order to select State Value as the Target Moniker, you must
first select the PlotVisController as the Target Widget. The State Value
moniker is used as the Target Moniker when the value that controls the
visibility is not needed in the code. If you use the State Value as the
moniker, the correct Target Widget must be selected first.
Make sure that the values for Table and Plot match the values that were
entered in the PlotVisController properties view.
For each of the widgets in the list above, completing the following steps
will set up the Visibility managers correctly.
7
8 Enhancing Your Extensions
After all three widgets have been properly set up, they can be tested.
There are two ways to test the function of the Visibility controllers.
Either use the Visibility manager, or select Test from the menu that
appears after right-clicking on the form.
8
Enhancing Your Extensions 9
Return to the view editing environment, and use the Visibility Manager
to select the Plot view of the P-Q Data page. There should only be the
Group Box widget and the Radio Buttons widget visible now, all other
widgets should be hidden.
Name PQPlotWidget
X-Stretch Yes
Y-Stretch Yes
Size the widget to occupy most of the available area inside of the Group
Box and below the radio buttons.
Access the Visibility manager for this widget by pressing the Visibility
button. Add the PlotVisController to this widget and set the visibility to
true for the Plot state.
Note that if the plot is going to stretch in both the X and Y directions,
the Group Box that surrounds it will also have to stretch in both
directions. Make the necessary changes now.
For now, the plot widget will just look like a blank box. The plot will be
defined in the VB code for the extension.
9
10 Enhancing Your Extensions
2. Add the following line of code to the Initialize function with the
other Set ... lines.
3. At the end of all the code, i.e. after the Interpolate and Sort
methods, create a new subroutine called CreatePlot. There are no
arguments for this subroutine. Type the following line of code
and the End Sub line should be automatically entered.
Sub CreatePlot
4. Return to the Initialize method and add the following line of code
at the end of this function, but make sure that it is before the error
handler.
Call CreatePlot
The first four lines in this code will check to see if a plot already exists in
the EDF, and if there is a preexisting plot, the plot and its reference will
be deleted. The fifth line uses the BuildPlot2 method of the container
to create the plot in the EDF. The first argument in this method
10
Enhancing Your Extensions 11
("PQPlot") is the label of the plot. The second argument (myPlot) is the
object in HYSYS that will hold the reference to the plot, and the third
argument (hptTwoDimensionalPlot) is the type of plot that we are
creating. More information on the BuildPlot2 method can be found
using the VB Object Browser.
Finally, the last line in this code block will connect the plot widget in the
EDF to the newly created plot. This is done by setting the value of the
myPlotName variable (which is tied to the PlotName variable in the
EDF) to the name of the plot that was entered as the first argument in
the BuildPlot2 method.
6. With the plot now created, we can define its attributes. Add the
following lines of code to the CreatePlot subroutine immediately
after the lines that were added in step #5.
With myPlot
.LegendVisible = True
.CreateXYDataSet 1, "PQData"
.SetDataSetXData 1, flowRFV
.SetDataSetYData 1, pressureRFV
.SetDataSetColour 1, "Red"
End With
11
12 Enhancing Your Extensions
myContainer.DeletePlot "PQPlot"
Set myPlot = Nothing
Set myPlotName = Nothing
The VB code can now be tested using one of the two methods presented
previously in this course. Either put the code into the Run mode, and
register the EDF, or compile the code into a DLL and register the DLL
and the EDF together. If you choose to make the DLL at this time
remember to use the same filename as was used when the EDF was
saved.
Remember to save the VBP and CLS files before you run the program.
Save the VBP file as Wellhead_Plot.VBP.
12
Enhancing Your Extensions 13
Exercise
In this exercise, you will continue to enhance the functionality of the
extension and the appearance of the EDF by adding new controls to the
EDF and a new subroutine to the extension’s code.
Recall that in the EDF module, the Status Bar was located in the middle
of the lower portion of the view. There was about 2 cm (1 inch) of room
on both the left and the right sides of the status bar. We will be adding
the Delete button to the left side of the bar. If there is not enough room
for this button on your EDF, either move the status bar over, or reduce
its size. You will need about 2 cm (1 inch) of room in order to add the
widget.
In order to add a Delete button to the EDF we must first add a Button
widget to the form. On the properties page for this widget, change the
name of the widget to DeleteButton, change the Label to Delete, and
select the message as Delete Object. The message can be selected by
pressing the [...] button in the message cell and then clicking the Insert
button on the view that appears. Select Delete Object from the list and
press the OK button.
This widget should not stretch in either direction, and it should be tied
to the bottom left corner of the view.
This will complete the installation procedure for the Delete button.
13
14 Enhancing Your Extensions
Add a Check Box widget to the view to the right of the Status Bar. On the
properties page for this widget, specify the following information.
Name IgnoredCB
Label Ignored
When both the Delete button and the Ignore check box are added to the
form, the bottom area of the form should look something like this:
14
Enhancing Your Extensions 15
What will happen if the user enters -10 for the number of
data points? ______________
What about 1 000 000 ? _____________
However, if a very large number (say, 1 000 000) is entered, the program
will accept this and continue, it will probably take several minutes to
set up the flowRFV and pressureRFV variables to take in a million
points. Obviously, it is unlikely that any user will have more than 100
data points to define the P-Q relationship. Therefore, it is a good idea
for the program to check the value that the user will input, and display a
descriptive error message if the number is too large, or if it just does not
make sense.
15
16 Enhancing Your Extensions
The Procedure
1. Add a new function to the code for the extension. The name of
the function will be VariableChanging; there is one argument for
this function. The argument will be Variable and it will be of the
type: InternalVariableWrapper. The function type is Boolean.
Check the Object Browser to determine the format of this
method.
2. Use a Select Case command to determine if the variable that is
changing is the "NumberOfPoints" variable. Hint: see the
VariableChanged subroutine for help in setting up the Select Case
command.
3. If the variable that is changing is the "NumberOfPoints" variable,
use an If ... Then statement to check if the inputted value is
between 2 and 100, inclusive. Any number less than 2, or greater
than 100 is not valid.
4. If the inputted number is not valid, a message box should be
displayed explaining the error, and the function’s value should be
set to False.
5. Otherwise, the function’s value should be set to True.
Test the extension by adding it to the HYSYS case that was used in the
previous module.
Optional Exercise
Add the necessary code to the VariableChanging function to check if a
specified feed or product stream exists, and if it does not, to create a
stream with the entered name.
16