Predefined Functions

From SysCAD Documentation
Jump to navigation Jump to search

Navigation: PGMs ➔ Functions ➔ Predefined Functions

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

Predefined Functions

A summary of the predefined functions available in SysCAD are listed in the Tables below. These do not include functions written for predefined classes. For information on those functions, please refer to the Predefined Classes section for the appropriate links.

Notes:

  1. All Function names are NOT case sensitive.
  2. "String" or "Message" used in the following functions refer to "A string" or "a variable of the STRING type".

Tag Related Functions

The Tag functions are used to Fetch/Set Values from/To SysCAD Project. See Tag Functions

Function Syntax Description
= [ ]
(Recommended format)
variable = ["Full SysCAD Tag"]
variable = [StringVariable]
This function gets a tag from SysCAD and returns the value of the parameter.
This format can be used in place of GetTag(), GetDynTag() and GetDynStrTag()
GetTag() variable = GetTag("Full SysCAD Tag") The GetTag function returns the value of the parameter in numerical type.
GetDynTag() variable = GetDynTag("String") The GetDynTag function returns the value of the parameter in numerical type, referred to by the tag contained in string
GetDynStrTag() String Variable = GetDynStrTag("String") The GetDynStrTag function returns the Tag name of the parameter, referred to by the tag contained in string.
[ ] =
(Recommended format)
["FullTag"] = variable
[StringVariable] = variable
[Str "FullTag"] = StringVariable
This function assigns the value to the parameter.
This format can be used in place of SetTag(), SetDynTag() and SetDynStrTag()
SetTag() SetTag("Full SysCAD Tag", value) The SetTag function assigns the value to the parameter, referred to by the tag contained in Full SysCAD Tag.
SetDynTag() SetDynTag(string, value) or
SetDynTag("Full SysCAD Tag", value)
The SetDynTag function assigns the value to the parameter, referred to by the tag contained in string or Full SysCAD Tag.
SetDynStrTag() SetDynStrTag(String, String2) The SetDynStrTag function assigns a String2 to String.
DynTagExists() variable = DynTagExists("String") The DynTagExists function returns True if the SysCAD tag represented by the string exists, otherwise it returns False.

Conditional Functions

The Conditional Functions are used to evaluate the condition of the project, based on the result, set a value or command to the project. See Conditional Functions

Function Syntax and Example Description
If()
Elseif()
Else
Endif
if (expression1)
	statements1
elseif (expression2)
	statements2
else
	statementsN
endif

statements1 : are executed if expression1 is True, otherwise they are ignored.
statements2 : are executed if expression1 is False and expression2 is True, otherwise they are ignored.
statementsN : is executed if all preceding expressions are false.
Elseif and Else expressions are optional.

iif() variable = iif(expression, truePart, falsePart)

For Example:
Recovery = iif(Feed==0, 0, Product/Feed)
Recovery = iif(Feed>0.000001, Product/Feed, 0)

The iif function is two-part if() statement, based on the condition of the expression, the True or False value is returned.
  • See IIf for Full Description and Examples.
iifStr() String Variable = iifStr(expression, truePart, falsePart)

For Example:
Control_Status = iifstr(Controller_On, "Auto", "Manual")

Available in Build139.29680 or later.
The iifStr function is a two-part if() statement, based on the condition of the expression, the True or False value is returned.
  • See IIfStr for Full Description and Examples.
While()
EndWhile
While (expression1)
	statements
endWhile
While the expression is true, the statements will be executed. When writing the While loop, ensure the loop can end, else the solver will be "stuck" inside the loop.

Time Related Functions

Function Syntax Example Description
DeltaTime() variable = DeltaTime()
Simulation_Time = Simulation_Time + DeltaTime() ; This will increment the simulation time every iteration.
The DeltaTime function returns the current SysCAD value of Delta Time (simulation step size) in seconds.

For ProBal this is always 1. Note: during the Initialisation step, DeltaTime will equal zero as no time has passed.

Time() variable = Time()
Sub InitialiseSolution()
     StartTime = Time()
EndSub
The Time function, returns the SysCAD time in seconds.

Miscellaneous Functions

Function Syntax and Example Description
Random() variable = Random(Number)

For Example:
d = Random(100)

The Random function, returns a random number between 0 and Number.
If Number is NAN then the function returns an integer between 0 and 32767. Precision is 1 in 32768.

d = a random number between 0 and 100

ForEachClass()
ForEachClass(ClassName, SubroutineName()) 
ForEachClass({ClassNameList}, SubroutineName())
ForEachClass({ClassInstanceList}, SubroutineName())
ForEachClass(ClassArray, SubroutineName())
Available in Build 139.31388 or later.
The ForEachClass macro generates code that loops through all instances of the user specified Class (or all members of the specified Class Array) and calls the specified Sub.
The Sub may have parameters.
Class instances must be in the global scope and declared prior to the ForEachClass() macro.

Species Functions

See Species Database Class for additional functions related to species properties.

Function Syntax Examples Description
MW()
variable = MW("Species")
variable = MW("Compound")
variable = MW("Element")
MW_Water = MW("H2O(l)")        ;Species
MW_H2O   = MW("H2O")           ;Compound
MW_H_Gas = MW("H")*2           ;Elements 
MW_H_Gas = MW("H2")            ;Compound
MW_SO4   = MW("S") + MW("O")*4 ;Elements
Returns the Molecular Weight of Species, Compound or Element.

NOTES:

  • The compound must be used in the project for this to work.
  • The compound name is CASE SENSITIVE
  • Only works if the species name is specified directly. If using species index, please use SpMolecularWt(SpIndex) function.
SpI() variable = SpI("Species") Water_Species_Index = SpI("H2O(l)")

Efficient function to return the Species Index of the specified species in the list of the project species. A file load error occurs if species is not found.
NOTES:

  • The species must be used in the project for this to work.
  • The species name must include phase and is CASE SENSITIVE.
  • Only works if the species name is specified directly (literal string).
  • If the species name is a string variable, use FindSpecies function. See comparison here.
  • This function is designed for use with the Species Database Class Functions.
  • See Species Information for usage of species index.
ElemI() variable = ElemI("Element") Na_Elemental_Index = ElemI("Na")

Only available in Build 139.29999 or later. Efficient function to return the Element Index of the specified element in the list of the elements used in the project. A file load error occurs if element is not used in the project.
NOTES:

  • The element must be used in the project for this to work.
  • The element name is CASE SENSITIVE.
  • Only works if the element name is specified directly (literal string).
  • If the element name is a string variable, use FindElem function. See comparison here.
  • This function is designed for use with the Species Database Class Functions.

User Defined Messages and Errors Functions

Please note that the SysCAD log file may be enabled or disabled by the user by ticking the Log to file : $Prj\MessageLog.csv tick box on the Message Window Options Tab. By default this option is disabled. If this option is enabled, then SysCAD will create the MessageLog.csv file in the project.

Function Syntax and Examples Description
LogError() LogError(string)

For Example:
LogError("Temperature out of Range") or
LogError("Temperature out of Range", "Please check species data")

The LogError function displays the supplied message string in the SysCAD message window on the messages tab with an error indicator. Supports multiple string parameters.
LogWarning() LogWarning(string)

For Example:
LogWarning("Flowrate requirement not met.")

The LogWarning function displays the supplied message string in the SysCAD message window on the messages tab with a warning indicator. Supports multiple string parameters.
Available in Build 139.32341 or later.
LogNote() LogNote(string)

For Example:
LogNote("Solids Density Approaches Limit")

The LogNote function displays the supplied message string in the SysCAD message window on the messages tab with a note indicator. Supports multiple string parameters.
ConditionError() ConditionError(Index, Boolean expression, string)

For Example:
ConditionError(0, ReactorT>250, "Temperature Out of Range")

Index - valid range from 0 to 7, that is, only 8 error messages can be reported per PGM/General Controller.
If the Boolean expression evaluates to true, then the message string specified is displayed in the Condition tab of the Message Window. As a Condition state message this is only shown for the current solver iteration. Supports multiple string parameters.
ConditionNote() ConditionNote(Index, Boolean expression, string)

For Example:
ConditionNote(0, ReactorT<150, "Temperature Low, check conditions")

Index - valid range from 0 to 7, that is, only 8 error messages can be reported per PGM/General Controller.
If the Boolean expression evaluates to true, then the message string specified is displayed in the Condition tab of the Message Window. As a Condition state message this is only shown for the current solver iteration. Supports multiple string parameters.
MsgBox() MsgBox(string)

For Example:
MsgBox("Temperature Low, check operating conditions")

The MsgBox function displays the supplied message string in the SysCAD message window in exactly the same way as LogNote.
  • NOTE:This function will be removed in future updates of SysCAD. Please replace with one of the Logxxx() functions listed above.
StopSolver() StopSolver(string)

For Example:
StopSolver(FeederTag, "Tag not found, please check flowsheet.")

The StopSolver function stops the SysCAD solver and displays the supplied message string in the message window. Supports multiple string parameters.
Available in Build 139.32346 or later.
PauseSolver() PauseSolver(string)

For Example:
PauseSolver("Precip001 not converged, please check feed.")

The PauseSolver function pauses the SysCAD solver and displays the supplied message string in the message window. Supports multiple string parameters.
Available in Build 139.32346 or later.
Beep() Beep() The Beep function calls the MS Windows MessageBeep function playing a simple beep.
Note that this is asynchronous and queues the beep to be played when the application is idle. Behaviour of the beep() function will depend on number of calls to Beep(), CPU utilisation, as well as OS & sound card settings.

NOTE: If a value is defined within the function, it is ignored.

Sound()
Sound(Freq, Duration)

For Examples:
Sound(2000, 10) ;generates a short dull beep sound.
Sound(3000, 500) ;generates a fairly long and loud beep sound.

The Sound function allows the user to set:

  • The sound frequency (Freq) in hertz (valid range 37 - 32,767) and
  • Duration of the sound in milliseconds (valid range 1 - 500).

Warning: Use with caution as Duration uses CPU time, using sound with long duration every iteration will slow down the solve time!

Conversion Functions

See Also Conversion Functions and Declaring Variables

Function Syntax Description
Cnv() variable = Cnv(variable1, unit) The Cnv function converts variable1 to the conversion units specified by unit and returns the result.
  • See Cnv for Full description and Examples.
CnvVal() variable = CnvVal(variable1, family, unit, ReqdUnit) The CnvVal function converts variable1 from the conversion units specified by unit to the conversion units specified by ReqdUnit for the specified conversion family and returns the result.
  • See CnvVal for Full description and Examples.

Tag Visibility Functions

Function Syntax Description
SetConcealedState() SetConcealedState(variable, bit ConcealVariable) The SetConcealedState function hides or shows the variable in the Access window.

SetConcealedState

Use SetConcealedState to set a variable to be hidden from the access window, the hidden variable can be viewed when the ShowConcealed button is pressed on the PGM access window. The variable (tag) is still accessible by other controllers, Excel reporting, etc. It is simply hidden from user in Access window when it is concealed.

Syntax:

SetConcealedState(Variable, bit ConcealVariable)
  • Variable: a valid variable or class for which the Concealed state must be set.
  • ConcealVariable: this may be either be a single bit value (true or false) or an expression which resolves into a single true or false result.
  • The SetConcealedState function is used to change the Concealed state of data variables based on the specified bit value. By changing the Concealed state the variables become visible or concealed on the Access window. Concealed variables are still accessible as tags in the flowsheet (e.g. from Excel).
  • The Variable can be a data variable or a class. If it is a class, all the variables for that class will have the state changed.
  • Only watched variables that are defined as inputs or outputs can have the state set. It is meaningless to use SetConcealedState on variables that are not declared as input or output to make them visible as tags.

Example:

Bit    UseSetSplit{i,s}
Real   RequiredSplit{i,Cnv("Frac","%"),Range(0,100)}

Sub InitialiseSolution()
     SetConcealedState(RequiredSplit, (UseSetSplit==false))  ;Only want variable visible if option is chosen where this is used
EndSub

if (UseSetSplit)
    SetTag("X_001.GM.IOs.[P_002].Split (%)", RequiredSplit)
endif

Using SetConcealedState with Classes:

  1. It is possible to set the concealed state of a variable in a class, for example SetConcealedState(T.d1, true).
  2. It is also possible to set the concealed state of a variable in an array of classes as long as the index is specified directly, for example SetConcealedState(T[3].d1, true).
  3. It is NOT possible to specify the index pragmatically, for example SetConcealedState(T[i].d1, true).
  4. When writing a function within a class definition, it is possible to use SetConcealedState to change the concealed status of variables within the class. This is useful for a class to adjust visible variables as a function of its parameters. You can set the concealed state of all variables within the class by specifying this as the Variable parameter.

For example, include these functions in the class to show/hide all variables in the class.

;Show all variables in the class
Sub ShowAll()
   SetConcealedState(this, false)
EndSub

;Show hide variables in the class
Sub HideAll()
   SetConcealedState(this, True)
EndSub

For more information on how to apply this to a PGM class, please see Aqueous Feed Concentration Calculator example PGM file.