Global Script¶
Introduction¶
A Global Script is a self-contained C-style routine that executes at runtime and can freely read from and write to all global variables. It has access to global constants and the full library of built-in functions, and may define intermediate variables that are used only inside the script. Global scripts are implemented as System UDFs, meaning they are compiled and evaluated by the same expression engine as other UDF-based components in M-Star. This makes them the most flexible runtime element for implementing logic that cannot be expressed easily through standard controllers or filters.
In the example below, a global script is used to reduce instantaneous drug substance concentration data predicted at four probe locations to a single coefficient of variation. The probe locations are shown in red. We present the time evolution of the species concentration at each probe, as well as the time evolution of the reduced COV value. This approach is used commonly in multi-probe experimental systems to concentrate measurements into one representative number for the whole system. The script reads the probe concentrations from their global variables, computes the mean and standard deviation, and writes the coefficient of variation to another global variable for logging or downstream use.
Download Sample File: Global Script
Global scripts are often used to encode multi-step operating procedures, rule-based controls, or complex mathematical updates. They behave like small, embedded programs that execute in the solver at each step (or at a specified interval), allowing the user to orchestrate variable evolution, perform conditional actions, or compute derived quantities.
Because they can directly manipulate any global variable, global scripts are well suited for tasks such as sequencing charge/ramp/hold steps, implementing safety interlocks, running phased feed strategies, or coordinating changes in operating conditions in response to sensor-like signals.
Script¶
Pressing the script button launches the Global Script Editor. This is a system UDF that can be used to edit the values of global variables. At a minimum, the script must define a function named timestep. For convenience, this function is pre-defined in the script editor when a new custom script is added to the model. The timestep function is a void function with the current simulation time t and the simulation time step dt as input variables. It is evaluated once per simulation step and can be used to update System UDF output variables. Note that, as a void function, the timestep function does not return a value. Assignments are made directly to the System UDF Input variables within the function body. Within this function, you can access any of the System UDF input variables, as well as functions from the built-in math library.
In addition to the required timestep function, the custom script can also invoke locally defined subroutines. These subroutines can be called from multiple locations within the time step function body. In the example below, the script contains a help function named addAndSquare, which is then evoked in the timestep function.
Note
Locally defined subroutines are scoped to the current custom script and can only be accessed by the time step function defined within it.
Global Script Toolbar¶
Context-Specific Toolbar Options |
Description |
|---|---|
|
The Help command launches the M-Star reference documentation in your web browser. |
See also Child Geometry Context Specific Toolbar.
For a full description of each selection on the Context-Specific Toolbar, see Toolbar Selections.
Help