Python Script Controller
Process input signals and generate output according to user scripts, which are used to implement combination control, conditional judgment and custom control logic that are difficult to express in standard operation components.
Component Appearance and Location
Expand Controller in component library on the left and find "Python Script Controller".
Ports and Connections
| Port Key | Type | Direction | Interface Name | Display Conditions |
|---|---|---|---|---|
in1 | CONTROL | input | Enter 1 | When the "number of input ports" is not less than 1 |
in2 | CONTROL | input | Enter 2 | When the "number of input ports" is not less than 2 |
in3 | CONTROL | input | Enter 3 | When the "number of input ports" is not less than 3 |
in4 | CONTROL | input | Enter 4 | When the "number of input ports" is not less than 4 |
out1 | CONTROL | output | Output 1 | When "Number of output ports" is not less than 1 |
out2 | CONTROL | output | Output 2 | When the "number of output ports" is not less than 2 |
out3 | CONTROL | output | Output 3 | When the "number of output ports" is not less than 3 |
out4 | CONTROL | output | Output 4 | When the "number of output ports" is not less than 4 |
The number of input and output ports can be set between 0 and 4. After adjusting the number, the canvas only displays the corresponding ports; existing connections need to be rechecked.
Parameter List
Port Configuration
| Name | key | Type | Default / Options | Description |
|---|---|---|---|---|
| Input Port Count | InputCount | integer | 1 | 0–4 interleaved input ports; recheck wiring after changes. |
| Output Port Count | OutputCount | integer | 1 | 0–4 interleaved output ports; recheck wiring and return every enabled output from the script. |
Script Implementation
| Name | key | Type | Default / Options | Description |
|---|---|---|---|---|
| Python Script | PythonScript | python-script | See the Workbench default configuration | Python control logic. |
Parameter Guidance
The gas workbench allows 0–4 input and 0–4 output ports. Recheck connections and keys after changing counts. Define control in Python Script; connected signals determine input and output meaning.
Scripting Interface
The signature is control(inputs, is_initial, time, dt, params).
| Argument | Meaning |
|---|---|
inputs | Numeric input dictionary keyed by connected ports such as in1. Connect valid drivers to all enabled inputs. |
is_initial | True for the first initialization call. Continuation preserves saved call state. |
time | Simulation date and time as a Python datetime. |
dt | Passed interval in seconds; zero at initialization. |
params | Component parameters, including port counts. |
| Return value | Dictionary containing exactly all enabled output keys such as out1, with values convertible to finite numbers. An empty dictionary is allowed for zero outputs. |
Store accumulation or latches in module-level runtime_state. The following one-input, one-output example returns zero at initialization, then accumulates using input and the passed interval.
runtime_state = {"total": 0.0}
def control(inputs, is_initial, time, dt, params):
if is_initial:
runtime_state["total"] = 0.0
else:
runtime_state["total"] += inputs["in1"] * dt
return {"out1": runtime_state["total"]}
Modeling Steps
- Configure ports and connect measurements, setpoints, or arithmetic results.
- Enter the function and check input and all output keys.
- Connect outputs to arithmetic components, oscilloscopes, or adapters. Enable the device signal control source for actuation.
- Run transient simulation and compare inputs, outputs, and units at startup and later instants. Oscilloscope
View Results
Inspect committed port values at the same control instant. Expose internal accumulations or flags through enabled outputs to observe them. Logs distinguish initialization, execution, output-key, and numeric errors.
Professional Theory
A script is a stateful discrete mapping. Call maps inputs , time , interval , parameters , and state to outputs and updated state:
Physical transients call scripts during control initialization and relevant physical-step boundaries. Saving results or propagating held values reuses outputs. Accumulate using the passed interval, rather than assuming each call has fixed duration.
Applicability and Limits
- Arithmetic and state. Standard components suit direct operations; scripts suit combined conditions, latches, and memory.
- Continuation. Keep mutable saved state in module-level
runtime_state, using serializable JSON data and finite numbers. Other module variables should be static. - Rollback. Make evaluation repeatable; avoid external file writes or network calls that rollback cannot undo.
- Commands. Adapters, control sources, and device models determine adopted targets and response. Actuation Targets
Common Inspections
- Missing or wrong keys. Check ports, wiring, and enabled
in1–in4andout1–out4. - Rejected output. Check denominators, exponentials, finiteness, units, and ranges.
- Unsupported continuation state. Check all mutable state is in
runtime_state, with no array objects, custom types, or nonfinite values. - Unexpected accumulation. Check initialization,
dtseconds, dimensions, and actual result intervals.