# energinet-simtools/mtb architecture

> Architecture diagram of the GitHub repository energinet-simtools/mtb, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/energinet-simtools/mtb
- Repository: https://github.com/energinet-simtools/mtb

## Overview

1. **What this repository is**
   MTB is a Python-driven automation harness for running and comparing electrical grid studies across two external simulation platforms: **DIgSILENT PowerFactory** for RMS/PDT studies and **PSCAD** for EMT studies. The repo is not the plant model itself; it is the orchestration, data preparation, execution, and post-processing layer around those tools.

2. **Top-level architecture**
   The code splits into three main concerns: **case setup**, **simulation execution**, and **result visualization/QA**. The workflow is driven by a study definition sheet (`testcases.xlsx`), configuration files (`config.ini`, `plotter/config.ini`), and model files for each simulator (`.pfd`, `.pswx`, `.pscx`, plus PSCAD/PowerFactory components).

3. **Study definition and run configuration**
   `case_setup.py` appears to translate the workbook-based study matrix into executable simulation cases. This is the boundary where a user-defined set of faults, operating points, signals, and comparison scenarios becomes a runnable batch. The repository’s “source of truth” for what to execute is therefore the spreadsheet, not hard-coded Python.

4. **PowerFactory execution path**
   `execute_pf.py` is the PowerFactory runner, likely using the local PowerFactory Python environment described in the README. It bridges MTB case definitions into PowerFactory study execution, then writes results out for comparison. The `sim_interface.py` module suggests there is a shared abstraction layer for simulator-specific operations.

5. **PSCAD execution path**
   `execute_pscad.py` and `batch_execute_pscad.py` handle PSCAD runs, including batch execution of multiple cases. The repo also includes `pscad_synchronize_pgbs.py` and `pscad_update_ums.py`, which indicate model-maintenance automation around PSCAD project contents. The file `interface.f` is a key boundary artifact: it is the Fortran source for the PSCAD-side MTB interface component embedded in the EMT model.

6. **Model integration boundary**
   The repo assumes MTB components are inserted into the plant models themselves:
   - PowerFactory model files: `.pfd`
   - PSCAD project files: `.pswx` / `.pscx`
   - Example setups are provided in `setup_examples/`
   
   This means the automation layer does not replace the simulator models; it configures and controls them externally while the model-specific MTB component exposes signals and hooks for studies.

7. **Simulation outputs and intermediate formats**
   PSCAD output handling is a notable subsystem. The `plotter/process_psout.py`, `plotter/psout_to_csv.py`, `plotter/read_and_write_functions.py`, and related helpers imply a pipeline that converts PSCAD output files into tabular data for analysis. The repository also contains reference/recorded outputs under `recordings/`, which likely serve as comparison baselines or sample signals.

8. **Result processing and comparison**
   The `plotter/` package is responsible for turning simulation outputs into comparative plots and analysis artifacts. Its structure shows concerns for:
   - result objects (`Result.py`)
   - figure/cursor configuration (`Figure.py`, `Cursor.py`, CSV setup files)
   - sampling and downsampling (`sampling_functions.py`, `down_sampling_method.py`)
   - plotting and guide logic (`plotter.py`, `guide_functions.py`)
   
   This is the main presentation layer for comparing PowerFactory and PSCAD responses.

9. **Quality assurance and model inspection tools**
   `utility_scripts/` contains one-off and diagnostic tools that support model governance rather than simulations:
   - extracting component data from PowerFactory
   - comparing PSCAD and PowerFactory component data
   - getting DSL checksums and relay data
   - listing PSCAD output signals
   - recovering `.psout` files
   - cleaning project definitions
   - checking PowerFactory models
   
   These scripts form a QA/validation subsystem around the two model ecosystems.

10. **Repository artifacts and examples**
    The repo includes example projects and inputs to help users wire MTB into their own models:
    - `setup_examples/` with sample PowerFactory and PSCAD setups
    - `recordings/` with example waveforms and measured data
    - `MTB.pfd` / `MTB.pslx` as likely core project artifacts
    - `powerfactory.pyi` as an interface stub for PowerFactory scripting support

11. **End-to-end data flow**
    The practical flow is: define studies in Excel → generate/configure cases → execute the relevant PowerFactory and PSCAD simulations → collect outputs → convert/normalize outputs where needed → plot and compare results. The important architectural boundary is that execution happens inside external simulators, while MTB manages orchestration and post-processing in Python.

12. **Runtime and tooling dependencies that matter architecturally**
    The repository depends on the simulator runtimes themselves, especially the tested PowerFactory and PSCAD Python environments mentioned in the README. Those external engines are not optional plugins; they are the primary compute backends. The Python code here is an integration layer that depends on the simulators’ APIs and project formats.

Main entry points: [`case_setup.py`](https://github.com/energinet-simtools/mtb/blob/main/case_setup.py) (Case setup), [`execute_pscad.py`](https://github.com/energinet-simtools/mtb/blob/main/execute_pscad.py) (PSCAD runner), [`execute_pf.py`](https://github.com/energinet-simtools/mtb/blob/main/execute_pf.py) (PF runner), [`plotter/process_psout.py`](https://github.com/energinet-simtools/mtb/blob/main/plotter/process_psout.py) (PSOUT flow)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_orchestration["Orchestration"]
  node_testcases["Study sheet<br/>study definition<br/>[testcases.xlsx]"]
  node_config["Main config<br/>runtime config<br/>[config.ini]"]
  node_case_setup["Case setup<br/>case builder<br/>[case_setup.py]"]
  node_examples["Setup examples"]
end

subgraph group_backends["Simulation Backends"]
  node_execute_pf["PF runner<br/>executor<br/>[execute_pf.py]"]
  node_execute_pscad["PSCAD runner<br/>executor<br/>[execute_pscad.py]"]
  node_batch_execute_pscad["Batch PSCAD<br/>batch executor"]
  node_sim_interface["Sim interface<br/>adapter<br/>[sim_interface.py]"]
  node_powerfactory_runtime[("PowerFactory<br/>external simulator")]
  node_pscad_runtime[("PSCAD<br/>external simulator")]
  node_interface_f["MTB interface<br/>model interface<br/>[interface.f]"]
  node_powerfactory_stub["PF stub<br/>type stub<br/>[powerfactory.pyi]"]
end

subgraph group_results["Results & Plotting"]
  node_plotter_pkg["Plotter<br/>analysis package<br/>[plotter.py]"]
  node_process_psout["PSOUT flow<br/>conversion pipeline<br/>[process_psout.py]"]
  node_process_results["Results merge<br/>analysis pipeline<br/>[process_results.py]"]
  node_read_write["I/O helpers<br/>data utils"]
  node_recordings["Recordings<br/>reference outputs<br/>[DK1_Voltage.csv]"]
end

subgraph group_qa["QA Tools"]
  node_component_tools["PF data tools<br/>qa tool"]
  node_model_checks["Model checks<br/>qa tool"]
  node_psout_tools["PSOUT tools<br/>qa tool"]
end

node_testcases -->|"defines"| node_case_setup
node_config -->|"configures"| node_case_setup
node_case_setup -->|"builds cases for"| node_execute_pf
node_case_setup -->|"builds cases for"| node_execute_pscad
node_case_setup -->|"batches for"| node_batch_execute_pscad
node_sim_interface -->|"adapts"| node_execute_pf
node_sim_interface -->|"adapts"| node_execute_pscad
node_execute_pf -->|"runs in"| node_powerfactory_runtime
node_execute_pscad -->|"runs in"| node_pscad_runtime
node_batch_execute_pscad -->|"orchestrates"| node_pscad_runtime
node_interface_f -->|"embedded in"| node_pscad_runtime
node_execute_pscad -->|"exposes signals"| node_interface_f
node_pscad_runtime -->|"produces"| node_process_psout
node_process_psout -->|"uses"| node_read_write
node_process_psout -->|"feeds"| node_process_results
node_process_results -->|"used by"| node_plotter_pkg
node_plotter_pkg -->|"compares with"| node_recordings
node_component_tools -->|"inspects"| node_powerfactory_runtime
node_model_checks -->|"validates"| node_powerfactory_runtime
node_psout_tools -->|"inspects"| node_pscad_runtime
node_examples -->|"demonstrates"| node_execute_pf
node_examples -->|"demonstrates"| node_execute_pscad
node_powerfactory_stub -->|"supports"| node_execute_pf

click node_testcases "https://github.com/energinet-simtools/mtb/blob/main/testcases.xlsx"
click node_config "https://github.com/energinet-simtools/mtb/blob/main/config.ini"
click node_case_setup "https://github.com/energinet-simtools/mtb/blob/main/case_setup.py"
click node_execute_pf "https://github.com/energinet-simtools/mtb/blob/main/execute_pf.py"
click node_execute_pscad "https://github.com/energinet-simtools/mtb/blob/main/execute_pscad.py"
click node_batch_execute_pscad "https://github.com/energinet-simtools/mtb/blob/main/batch_execute_pscad.py"
click node_sim_interface "https://github.com/energinet-simtools/mtb/blob/main/sim_interface.py"
click node_interface_f "https://github.com/energinet-simtools/mtb/blob/main/interface.f"
click node_plotter_pkg "https://github.com/energinet-simtools/mtb/blob/main/plotter/plotter.py"
click node_process_psout "https://github.com/energinet-simtools/mtb/blob/main/plotter/process_psout.py"
click node_process_results "https://github.com/energinet-simtools/mtb/blob/main/plotter/process_results.py"
click node_read_write "https://github.com/energinet-simtools/mtb/blob/main/plotter/read_and_write_functions.py"
click node_powerfactory_stub "https://github.com/energinet-simtools/mtb/blob/main/powerfactory.pyi"
click node_component_tools "https://github.com/energinet-simtools/mtb/blob/main/utility_scripts/get_component_data_from_powerfactory.py"
click node_model_checks "https://github.com/energinet-simtools/mtb/blob/main/utility_scripts/check_powerfactory_model.py"
click node_psout_tools "https://github.com/energinet-simtools/mtb/blob/main/utility_scripts/list_psout_signals.py"
click node_examples "https://github.com/energinet-simtools/mtb/blob/main/setup_examples/MTB_Setup_Example.pfd"
click node_recordings "https://github.com/energinet-simtools/mtb/blob/main/recordings/DK1_Voltage.csv"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_testcases,node_config,node_case_setup,node_examples toneBlue
class node_execute_pf,node_execute_pscad,node_batch_execute_pscad,node_sim_interface,node_powerfactory_runtime,node_pscad_runtime,node_interface_f,node_powerfactory_stub toneAmber
class node_plotter_pkg,node_process_psout,node_process_results,node_read_write,node_recordings toneMint
class node_component_tools,node_model_checks,node_psout_tools toneRose
```

## Components

### Orchestration

Study definition, case generation, and simulator execution entrypoints.

- [Study sheet](https://github.com/energinet-simtools/mtb/blob/main/testcases.xlsx): study definition · Spreadsheet that defines the study matrix and run scenarios. · `testcases.xlsx`
- [Main config](https://github.com/energinet-simtools/mtb/blob/main/config.ini): runtime config · Primary repository configuration for orchestration and execution. · `config.ini`
- [Case setup](https://github.com/energinet-simtools/mtb/blob/main/case_setup.py): case builder · Translates workbook studies into executable cases and parameters. · `case_setup.py`
- [Setup examples](https://github.com/energinet-simtools/mtb/blob/main/setup_examples/MTB_Setup_Example.pfd): examples · Sample PowerFactory and PSCAD project setups. · `setup_examples/MTB_Setup_Example.pfd`

### Simulation Backends

Shared interface and simulator-specific runners.

- [PF runner](https://github.com/energinet-simtools/mtb/blob/main/execute_pf.py): executor · Runs PowerFactory studies and captures outputs for comparison. · `execute_pf.py`
- [PSCAD runner](https://github.com/energinet-simtools/mtb/blob/main/execute_pscad.py): executor · Runs PSCAD cases for EMT studies. · `execute_pscad.py`
- [Batch PSCAD](https://github.com/energinet-simtools/mtb/blob/main/batch_execute_pscad.py): batch executor · Coordinates multi-case PSCAD execution. · `batch_execute_pscad.py`
- [Sim interface](https://github.com/energinet-simtools/mtb/blob/main/sim_interface.py): adapter · Shared abstraction for simulator-specific operations. · `sim_interface.py`
- **PowerFactory**: external simulator · External RMS/PDT simulation backend.
- **PSCAD**: external simulator · External EMT simulation backend.
- [MTB interface](https://github.com/energinet-simtools/mtb/blob/main/interface.f): model interface · Fortran-side interface embedded in the PSCAD model. · `interface.f`
- [PF stub](https://github.com/energinet-simtools/mtb/blob/main/powerfactory.pyi): type stub · Python interface stub for PowerFactory scripting support. · `powerfactory.pyi`

### Results & Plotting

PSCAD output conversion, result processing, and comparative plotting.

- [Plotter](https://github.com/energinet-simtools/mtb/blob/main/plotter/plotter.py): analysis package · Package that converts outputs into comparative plots and analysis artifacts. · `plotter/plotter.py`
- [PSOUT flow](https://github.com/energinet-simtools/mtb/blob/main/plotter/process_psout.py): conversion pipeline · Parses PSCAD output files and prepares tabular data. · `plotter/process_psout.py`
- [Results merge](https://github.com/energinet-simtools/mtb/blob/main/plotter/process_results.py): analysis pipeline · Processes normalized results for comparison and reporting. · `plotter/process_results.py`
- [I/O helpers](https://github.com/energinet-simtools/mtb/blob/main/plotter/read_and_write_functions.py): data utils · Shared read/write helpers for plotter workflows. · `plotter/read_and_write_functions.py`
- [Recordings](https://github.com/energinet-simtools/mtb/blob/main/recordings/DK1_Voltage.csv): reference outputs · Sample waveforms and measurement files used as references. · `recordings/DK1_Voltage.csv`

### QA Tools

Maintenance and validation scripts for model and project data.

- [PF data tools](https://github.com/energinet-simtools/mtb/blob/main/utility_scripts/get_component_data_from_powerfactory.py): qa tool · Extracts and compares PowerFactory and PSCAD component data. · `utility_scripts/get_component_data_from_powerfactory.py`
- [Model checks](https://github.com/energinet-simtools/mtb/blob/main/utility_scripts/check_powerfactory_model.py): qa tool · Validates model contents, checksums, relay data, and project definitions. · `utility_scripts/check_powerfactory_model.py`
- [PSOUT tools](https://github.com/energinet-simtools/mtb/blob/main/utility_scripts/list_psout_signals.py): qa tool · Lists and recovers PSCAD output files and signals. · `utility_scripts/list_psout_signals.py`

## Connections

- Study sheet → Case setup: defines · Study matrix drives case generation.
- Main config → Case setup: configures · Run settings feed case preparation.
- Case setup → PF runner: builds cases for · Prepared cases are handed to the PowerFactory runner.
- Case setup → PSCAD runner: builds cases for · Prepared cases are handed to the PSCAD runner.
- Case setup → Batch PSCAD: batches for · Case sets can be grouped for batch EMT execution.
- Sim interface → PF runner: adapts · Shared simulator abstraction used by the PF path.
- Sim interface → PSCAD runner: adapts · Shared simulator abstraction used by the PSCAD path.
- PF runner → PowerFactory: runs in · PF studies execute inside the external simulator.
- PSCAD runner → PSCAD: runs in · PSCAD studies execute inside the external simulator.
- Batch PSCAD → PSCAD: orchestrates · Batch execution manages multiple PSCAD runs.
- MTB interface → PSCAD: embedded in · Fortran interface is part of the PSCAD-side model integration.
- PSCAD runner → MTB interface: exposes signals · Runner depends on the model interface boundary.
- PSCAD → PSOUT flow: produces · PSCAD output files feed the conversion pipeline.
- PSOUT flow → I/O helpers: uses · Parsing and serialization share common I/O helpers.
- PSOUT flow → Results merge: feeds · Converted outputs continue into result processing.
- Results merge → Plotter: used by · Result processing supports the plotting workflow.
- Plotter → Recordings: compares with · Reference recordings act as comparison baselines.
- PF data tools → PowerFactory: inspects · Scripts inspect PF component data and project state.
- Model checks → PowerFactory: validates · Checks target PowerFactory models and project definitions.
- PSOUT tools → PSCAD: inspects · Tools inspect PSCAD outputs and recover missing files.
- Setup examples → PF runner: demonstrates · Sample setups illustrate the PF execution path.
- Setup examples → PSCAD runner: demonstrates · Sample setups illustrate the PSCAD execution path.
- PF stub → PF runner: supports · Type stub supports PF scripting integration.
