Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Simulation and digital twin

The engine that runs a project on a controller also runs it as a simulation: the same packages, the same fixed step, the same image, as fast as the models compute. A digital twin is the same project again, run in real time beside the plant and fed with its measurements.

Offline simulation

A plant model and the application under test in one project, wired in the image:

[engine]
tick_ms = 100.0

[models.plant]
kind = "fmi"
path = "packages/plant.fmu"

[models.ctrl]
kind = "raw"
path = "packages/ctrl"

[[instance]]                  # the controller reads the plant's output of the previous tick
id = "ctrl"
model = "ctrl"
inputs = { pv = "plant.y" }

[[instance]]
id = "plant"
model = "plant"
inputs = { u = "ctrl.u" }

[[recorder]]
id = "main"
signals = ["@io"]
taktwerk run sim.toml --speed max --no-connectors --until-time 86400 --seed 1
  • --speed max runs unpaced on engine time; input ages and every event count model time, so the rows are the same at any speed (Pace).
  • --set, --model and a scenario file change a run without editing the project: other parameters, another package version, a disturbance, a sensor fault. Each is recorded in run.json.
  • The record is the same format a plant run writes; the Python package and the workbench read both.

examples/closed-loop is an FMU and a C controller wired this way; examples/campaign adds a seeded noisy sensor and a disturbance scenario.

Digital twin

A twin runs a plant model in real time with the plant’s own inputs, so its outputs can be put next to the measurements, or stand in for what the plant does not measure:

flowchart LR
  PLC[PLC] -- "manipulated values, measurements<br/>opcua-client" --> E
  subgraph E[taktwerk]
    TW[plant model] --> PI[(image)]
  end
  E -- "OPC UA server" --> HMI[HMI, historian]
  E -- "recorder" --> R[records]
  • An opcua-client reads the manipulated values (and the measurements to compare with) into external inputs, with a max_age_ms, so the twin stops stepping when the data stops.
  • The model’s outputs are on the engine’s OPC UA server for any client; mapped in the opcua-client they go back to the PLC. A twin that only reads needs no writes.
  • A recorder with @io keeps every input as received and every output each tick, for a comparison afterwards: the same record can be replayed offline with the measured inputs set by a scenario, or compared run against run in the workbench.

An estimator that corrects the model from the measurements is an application package of its own; see A controller or estimator beside a PLC.