Trigger Subroutines

From SysCAD Documentation
Jump to navigation Jump to search

Navigation: PGMs ➔ Subroutines ➔ Trigger Subroutines

Functions Subroutines Classes
Defining a Function Predefined Functions Tag Functions Mathematical Functions String Functions Defining a Subroutine Trigger Subroutines Defining a Class

Related Links: Predefined Special Constants, Model Procedures


Introduction

Trigger subroutines contain commands which are executed by SysCAD at different stages of the evaluation of the model. All subroutines are optional.

These are similar to the trigger functions used in Model Procedures.

The trigger subroutines are replacements to the predefined constants OnInitialise and OnTerminate which are used in older versions. These old constants have limited functionality and will be removed in future updates. Please consider changing these old constants to trigger subroutines as soon as possible.

When starting a new PGM file the trigger subroutines are contained in the standard PGM template.

Trigger Subroutine Description Syntax

InitialisePreStart

The InitialisePreStart() subroutine is for logic which is to be executed very early before the first iteration.

  • Deactivation of flowsheets can only be performed within this subroutine.
  • Other items which might be performed within this subroutine are turning controllers or units on or off.
Sub InitialisePreStart() 
  ;--- Logic executed very early before first iteration
  
EndSub

See examples below.

PreStart

The PreStart() subroutine is for logic which is to be executed early before the first iteration.

  • This is an replacement option to the OnInitialise special constant in SysCAD 9.2 or earlier versions.
  • This is similar to the setting of tags in the Plant Model SetTagList.
  • Items which may be performed within this subroutine include turning controllers or units on or off and the setting of options or methods.
Sub PreStart() 
  ;--- Logic executed early before first iteration
  
EndSub

InitialiseSolution

The InitialiseSolution() subroutine is for logic which is to be executed during initialisation of the model, before the first iteration.

  • This is an replacement option to the OnInitialise special constant in SysCAD 9.2 or earlier versions.
  • It is also similar to the MP_InitialiseSolution function used in Model Procedures. Items which may be performed within this subroutine include setting of initial values or constant values in units.
Sub InitialiseSolution() 
  ;--- Logic executed during initialisation before first iteration
  
EndSub

TerminateSolution

The TerminateSolution() subroutine is for logic which is to be executed after the last iteration, when the solver is stopped.

Sub TerminateSolution() 
  ;--- Logic executed after last iteration (when solver is stopped)
  
EndSub

InitialisePreStart Examples

Reset Actions

Sub InitialisePreStart()
 ;--- Logic executed very early before first iteration

 ; Perform complete reset of project
 ["$Solver.Cmd.CompleteReset"] = 1  

EndSub

Activate/deactivate flowsheets

Note: Separate tags for Activate / Deactivate. These cannot be set to 0.
Sub InitialisePreStart()
 ;--- Logic executed very early before first iteration

 ; Activate flowsheet 05_Flowsheet
 ["PlantModel.Page[05_Flowsheet].Activate"] = 1  

 ; Deactivate flowsheet 10_Flowsheet
 ["PlantModel.Page[10_Flowsheet].DeActivate"] = 1

EndSub

Cross-page connections

Sub InitialisePreStart()
 ;--- Logic executed very early before first iteration

 ; Turn on cross-page connection in Feeder XPG_001
 ["XPG_001.ConnectionOn"] = 1

 ; Turn off cross-page connection in Feeder XPG_002
 ["XPG_002.ConnectionOn"] = 0

 ; Set cross-page connection in Feeder XPG_001 to XPG_002
 [str "XPG_001.Conn_To"] = "XPG_002"

EndSub

Making multi-step configuration changes using trigger subroutines

Where multiple steps are required to re-configure a unit operation, the steps may need to be broken up between the different trigger subroutines so that they are executed in series.

Two examples are shown here:

  1. In this example, a Filter Press model is required to be turned on, the method changed and a parameter set. Since the parameter may not be available until the method selection is made, these will be performed in different subroutines.
  2. In this example, the PID controller block can be switched on / off based on a trigger variable (true/false).
Example1 Example2
Sub InitialisePreStart()
 ;--- Logic executed very early before first iteration
 ; Turn on filter press FP_001
 ["FP_001.On"] = 1

EndSub
 
Sub PreStart()
 ;--- Logic executed early before first iteration
 ; Set method in filter press FP_001 to FiltrateConcAt25C
 ["FP_001.Method"] = 1
EndSub

Sub InitialiseSolution()
 ;--- Logic executed during initialisation before first iteration
 ; Set required filtrate solid concentration in filter press FP_001
 ["FP_001.FiltrateSolidConc25Reqd (g/L)"] = 2

EndSub
Sub PreStart()
 ;--- Logic executed early before first iteration
 ; example for switching PID control block on/off based on trigger:
  if UsePID_Control
	["Acid_Control.Cfg.[2].On"] = 0
  Else
	["Acid_Control.Cfg.[2].On"] = 1
  Endif
EndSub

Converting Predefined Constants to Trigger Subroutines

When using the Predefined Constants,
user could have written the PGM code like this,
where OnInitialise (or OnTerminate) is used multiple times:
When using Trigger Subroutines,
you can only call the subroutine once per PGM.
So user needs to change the code to look like this:
If OnInitialise() 
 a = 0
 b = 0
Endif 

;Some other code 
 
If OnInitialise() 
 c = 0
 d = 0
Endif

If OnTerminate()
  code here
Endif
Sub Init1()
 a = 0
 b = 0
EndSub

Sub Init2()
 c = 0
 d = 0
EndSub

Sub InitialiseSolution() 
 Init1()
 Init2()
EndSub

Or

Sub InitialiseSolution()
 a = 0
 b = 0
 c = 0
 d = 0
EndSub

Sub TerminateSolution()
 code here
EndSub