Automating process dynamic simulations from Python using the DWSIM Automation API and pythonnet.
This repository contains a series of case studies showing how to drive DWSIM dynamic flowsheets programmatically. Each case is self-contained and builds on the same core idea: use Python to make dynamic process simulation reproducible, scriptable, and easier to connect with larger computational workflows.
Running a dynamic simulation from the DWSIM GUI is useful for interactive exploration. It becomes limiting when the goal is to repeat the same scenario many times, run structured what-if studies, generate datasets, or compare control strategies.
Once a dynamic flowsheet can be controlled from Python, it becomes a reproducible process scenario. The same model can be used to run parameter sweeps, test controller behaviour, generate time-series data, or evaluate how the system responds to structured disturbances — all without manually operating the GUI.
dwsim-python-dynamic-simulation/
├── README.md
├── 01_separator/
│ ├── dwsim_separator_dynamic_control.ipynb
│ └── separator_python_run.csv
└── 02_separator_from_scratch/
├── DWSIM_SEPARATOR_REFERENCE_MODEL.ipynb
├── DWSIM_SEPARATOR_PYTHON_BUILT_MODEL.ipynb
└── results/
The first case study automates an existing DWSIM dynamic sample flowsheet from Python.
Instead of running the dynamic schedule from the DWSIM GUI, the notebook loads the model, advances the dynamic solver step by step, applies pressure setpoint changes manually, triggers the internal PID controllers, and records the response.
Base model: Dynamic Simulation - Separator Pressure and Level Control.dwxmz
Property package: Raoult's Law
Compounds: Air, CO₂, Water, Methanol
Feed ──► FV-01 ──► SG-01 ──► PV-01 ──► gas outlet
│
└──► LV-01 ──► liquid outlet
| Controller | Controlled variable | Manipulated variable | Setpoint |
|---|---|---|---|
| PID-012 | Separator pressure | PV-01 opening | 2 bar |
| PID-013 | Separator liquid level | LV-01 opening | 0.3 m |
A 30-minute dynamic run with two scheduled pressure setpoint changes:
- t = 3 min → pressure setpoint steps to 5 bar
- t = 10 min → pressure setpoint returns to 2 bar
01_separator/
├── dwsim_separator_dynamic_control.ipynb ← main notebook
└── separator_python_run.csv ← recorded time series
The 30-minute dynamic scenario runs in approximately 50 seconds, around 36× faster than real time.
| Topic | Correct pattern | Common mistake |
|---|---|---|
| Dynamic solver | DWSIMSolver.SolveFlowsheet(fs, 1) |
RequestCalculationAndWait runs the steady-state solver |
| Schedule execution | Reproduce events manually in Python | dm.RunSchedule() is GUI-dependent |
| Event trigger time | ev.TimeStamp |
ev.TriggerTime does not exist |
| Event object reference | ev.SimulationObjectID is a GUID and needs lookup |
Using it as a tag directly |
| PID setpoint | pid.SetPoint |
pid.SetPointAbs does not exist |
| Monitored variable history | Capture values inside the Python loop | Expecting history to persist after reload |
The second case study goes one step further: instead of only automating an existing flowsheet, it builds an equivalent DWSIM dynamic separator model programmatically from Python.
A reference model is first prepared from the DWSIM built-in sample case. Then, a second notebook constructs the equivalent flowsheet from scratch and validates it against the reference behavior.
Property package: Raoult's Law Compounds: Air, CO₂, Water, Methanol Design basis: 7,500 kg/h Vessel: 1.0 m³ volume, 2.0 m height Main setpoints: pressure = 2.0 bar, liquid level = 1.0 m
| Valve | Fluid/service | Kv | Basis |
|---|---|---|---|
| PV-01 | Gas outlet | 85 | IEC 60534-2-1 gas sizing |
| LV-01 | Liquid outlet | 12 | IEC 60534 liquid sizing |
| FV-01 | Feed inlet | 450 | Flow boundary / inlet pressure drop control |
Starts from the DWSIM built-in separator dynamic sample and prepares a reference case for validation.
The workflow:
- removes the internal DWSIM PID controllers and display instruments;
- resizes the valves using IEC-based Kv values;
- fixes outlet boundary pressures;
- pre-equilibrates the model using Python PID controllers;
- saves the prepared reference state;
- runs open-loop and closed-loop dynamic tests.
Dynamic tests include:
- open-loop feed step;
- closed-loop pressure setpoint changes;
- closed-loop level setpoint changes.
Builds an equivalent dynamic separator flowsheet directly from Python, without manual GUI construction.
The workflow:
- creates the material streams and unit operations;
- defines the thermodynamic package and compounds;
- connects the flowsheet topology;
- configures valve sizing and separator geometry;
- initializes the dynamic state;
- runs the same scenarios as the reference model;
- compares the Python-built model against the reference model.
02_separator_from_scratch/
├── DWSIM_SEPARATOR_REFERENCE_MODEL.ipynb ← reference model preparation and tests
├── DWSIM_SEPARATOR_PYTHON_BUILT_MODEL.ipynb ← Python-built model and validation
└── results/ ← generated locally, not versioned
The results/ folder is kept in the repository with a .gitkeep file, but generated plots and temporary outputs are not committed. They can be regenerated by running the notebooks.
| Finding | Detail |
|---|---|
| Python-built valves require explicit Kv/opening behaviour | The opening-Kv relationship may need to be enabled explicitly through reflection. |
| Dynamic properties must be refreshed after each solver step | sg.RunDynamicModel() is required after each step to read updated pressure and level values reliably. |
| Saved flowsheets may contain orphaned graphic objects | Removed GUI objects can leave XML nodes behind unless the saved file is cleaned. |
| Initial valve opening matters | Starting too close to the hydraulic limit can destabilize the pre-equilibration. |
| Vessel pressure initialization matters | The inlet stream should be initialized near the operating pressure before the first dynamic step. |
| PI control is preferable for setpoint tracking | Derivative action can create spikes during setpoint changes. |
- DWSIM 9.x installed on Windows
- Python 3.x
pythonnet 3.xnumpypandasmatplotlib
Install Python dependencies:
pip install pythonnet numpy pandas matplotlib- DWSIM — open-source process simulator
- LinkedIn article series — process engineering context and discussion for each case study
DWSIM 9.x · Python 3.x · pythonnet 3.x