Skip to main content

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".

Created with Fabric.js 5.3.0

Ports and Connections​

Port KeyTypeDirectionInterface NameDisplay Conditions
in1CONTROLinputEnter 1When the "number of input ports" is not less than 1
in2CONTROLinputEnter 2When the "number of input ports" is not less than 2
in3CONTROLinputEnter 3When the "number of input ports" is not less than 3
in4CONTROLinputEnter 4When the "number of input ports" is not less than 4
out1CONTROLoutputOutput 1When "Number of output ports" is not less than 1
out2CONTROLoutputOutput 2When the "number of output ports" is not less than 2
out3CONTROLoutputOutput 3When the "number of output ports" is not less than 3
out4CONTROLoutputOutput 4When 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

NamekeyTypeDefault / OptionsDescription
Input Port CountInputCountinteger10–4 interleaved input ports; recheck wiring after changes.
Output Port CountOutputCountinteger10–4 interleaved output ports; recheck wiring and return every enabled output from the script.

Script Implementation

NamekeyTypeDefault / OptionsDescription
Python ScriptPythonScriptpython-scriptSee the Workbench default configurationPython 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).

ArgumentMeaning
inputsNumeric input dictionary keyed by connected ports such as in1. Connect valid drivers to all enabled inputs.
is_initialTrue for the first initialization call. Continuation preserves saved call state.
timeSimulation date and time as a Python datetime.
dtPassed interval in seconds; zero at initialization.
paramsComponent parameters, including port counts.
Return valueDictionary 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​

  1. Configure ports and connect measurements, setpoints, or arithmetic results.
  2. Enter the function and check input and all output keys.
  3. Connect outputs to arithmetic components, oscilloscopes, or adapters. Enable the device signal control source for actuation.
  4. 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 kk maps inputs uk\boldsymbol u_k, time tkt_k, interval Δtk\Delta t_k, parameters p\boldsymbol p, and state sk\boldsymbol s_k to outputs and updated state:

(yk,sk+1)=F(uk,tk,Δtk,p,sk).(\boldsymbol y_k,\boldsymbol s_{k+1})= F(\boldsymbol u_k,t_k,\Delta t_k,\boldsymbol p,\boldsymbol s_k).

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–in4 and out1–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, dt seconds, dimensions, and actual result intervals.