# sk-subroto/pignn-dhn architecture

> Architecture diagram of the GitHub repository sk-subroto/pignn-dhn, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sk-subroto/pignn-dhn
- Repository: https://github.com/sk-subroto/pignn-dhn

## Overview

#### Purpose and execution entry points
`pignn-dhn` is a physics-informed GNN solver for steady-state district-heating hydraulic flows. It predicts pipe mass flows from boundary setpoints without using Newton–Raphson solutions as training labels. Main runnable modules are `dhn_gnn/train.py`, `dhn_gnn/evaluate.py`, and `dhn_gnn/generate.py`; verification entry points are `tests/run_gates.py` and `tests/run_model_smoke.py`.

#### Network and data boundary
The bundled Verbier DHN topology and pipe parameters are CSV inputs under `data/network/nodes.csv`, `data/network/pipes.csv`, and `data/network/heating_stations.csv`. Boundary measurements and weather inputs live in `data/measurements/mass_flow.csv` and `data/weather/outdoor_temp_verbier_2022-01.csv`. `dhn_gnn/config.py` centralizes dataset paths and fixed 50 °C fluid properties. Generated solved datasets are intentionally external/git-ignored.

#### Graph-operator construction
`dhn_gnn/data/network_operators.py` converts the PyDHN-backed network topology into graph algebra: node-edge incidence `A`, cycle matrix `B`, and masks for independent loops. This is a critical representation boundary: the solver works in cycle-space variables, with pipe flows reconstructed as a feasible base flow plus cycle corrections. Since cycle-space directions satisfy `A·Z = 0`, mass conservation is preserved structurally rather than imposed as a penalty.

#### Differentiable hydraulic physics
`dhn_gnn/data/physics.py` implements differentiable Darcy–Weisbach pressure-loss/resistance behavior and its flow derivative in PyTorch. The governing loop residual is `B·φ(Bᵀ·m̃)`, evaluated from reconstructed edge flows. PyDHN remains a material external dependency for network construction and validation/oracle comparisons, while the optimization-time physics path is Torch-native.

#### Unrolled hybrid solver
`dhn_gnn/model/unrolled_solver.py` is the central K-step solver. At each step it recomputes a diagonal Newton-style correction from the known hydraulic derivative, applies damping, and adds a learned bounded correction. `dhn_gnn/model/stability.py` supplies evaluation-time protections such as velocity caps and Armijo-style acceptance. This separates guaranteed physical structure from the learned residual refinement.

#### Learned graph refinement
`dhn_gnn/model/attention.py` implements edge-biased multi-head attention over the pipe graph. Its attention bias is based on the current linearized hydraulic resistance, recomputed each unrolled step. The learned block operates only as a correction to the physics-derived update, not as a standalone flow predictor.

#### Training and evaluation flow
`dhn_gnn/losses.py` defines discounted, deep-supervised physics-residual losses across unrolled steps. `dhn_gnn/train.py` trains self-supervised with truncated backpropagation: state is detached between solver steps to control instability from the quadratic operator. `dhn_gnn/evaluate.py` runs the solver against generated PyDHN reference outputs, calculates flow metrics, and emits result artifacts. Runtime is Python 3.12 with PyTorch, NumPy/Pandas, Matplotlib, PyDHN, and Meteostat as declared in `requirements.txt`.

Main entry points: [`dhn_gnn/model/unrolled_solver.py`](https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/model/unrolled_solver.py) (K-step unrolled solver), [`dhn_gnn/train.py`](https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/train.py) (Training entry point), [`dhn_gnn/evaluate.py`](https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/evaluate.py) (Evaluation entry point), [`dhn_gnn/data/network_operators.py`](https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/data/network_operators.py) (Cycle-space operators)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_inputs["Network &amp; inputs"]
  node_nodes_csv["Network nodes<br/>CSV topology<br/>[nodes.csv]"]
  node_pipes_csv["Pipe parameters<br/>CSV parameters<br/>[pipes.csv]"]
  node_stations_csv["Heating stations<br/>CSV boundary data"]
  node_measurements["Mass-flow measurements<br/>CSV measurements<br/>[mass_flow.csv]"]
  node_weather["Outdoor temperature<br/>CSV weather"]
  node_config["Dataset &amp; fluid config<br/>Python configuration<br/>[config.py]"]
end

subgraph group_operators["Physics representation"]
  node_pydhn{{"PyDHN<br/>External network engine"}}
  node_network_operators["Cycle-space operators<br/>Graph algebra builder"]
  node_physics["Differentiable hydraulics<br/>Torch physics<br/>[physics.py]"]
end

subgraph group_solver["Hybrid solver"]
  node_attention["Resistance-biased attention<br/>GNN refinement<br/>[attention.py]"]
  node_stability["Stability controls<br/>Solver safeguards<br/>[stability.py]"]
  node_unrolled_solver["K-step unrolled solver<br/>Hybrid GNN solver<br/>[unrolled_solver.py]"]
  node_losses["Deep-supervised residual loss<br/>Physics loss<br/>[losses.py]"]
end

subgraph group_workflows["Execution &amp; verification"]
  node_generated_data[("Generated reference datasets<br/>External solved data")]
  node_train["Training entry point<br/>Python runnable<br/>[train.py]"]
  node_evaluate["Evaluation entry point<br/>Python runnable<br/>[evaluate.py]"]
  node_generate["Dataset generation<br/>Python runnable<br/>[generate.py]"]
  node_verification["Verification gates<br/>Python runnable<br/>[run_gates.py]"]
end

node_nodes_csv -->|"topology"| node_pydhn
node_pipes_csv -->|"pipe data"| node_pydhn
node_stations_csv -->|"setpoints"| node_pydhn
node_pydhn -->|"network graph"| node_network_operators
node_network_operators -->|"A, B, loop basis"| node_unrolled_solver
node_physics -->|"losses &amp; derivatives"| node_unrolled_solver
node_attention -->|"learned correction"| node_unrolled_solver
node_stability -->|"evaluation safeguards"| node_unrolled_solver
node_unrolled_solver -->|"per-step residuals"| node_losses
node_config -->|"paths &amp; fluid properties"| node_train
node_measurements -->|"boundary inputs"| node_train
node_weather -->|"weather inputs"| node_train
node_unrolled_solver -->|"unrolled predictions"| node_train
node_losses -->|"self-supervised objective"| node_train
node_pydhn -->|"solves reference cases"| node_generate
node_generate -->|"writes"| node_generated_data
node_unrolled_solver -->|"predictions"| node_evaluate
node_generated_data -->|"reference flows"| node_evaluate
node_pydhn -.->|"oracle comparison"| node_evaluate
node_train -.->|"trained model"| node_verification

click node_nodes_csv "https://github.com/sk-subroto/pignn-dhn/blob/main/data/network/nodes.csv"
click node_pipes_csv "https://github.com/sk-subroto/pignn-dhn/blob/main/data/network/pipes.csv"
click node_stations_csv "https://github.com/sk-subroto/pignn-dhn/blob/main/data/network/heating_stations.csv"
click node_measurements "https://github.com/sk-subroto/pignn-dhn/blob/main/data/measurements/mass_flow.csv"
click node_weather "https://github.com/sk-subroto/pignn-dhn/blob/main/data/weather/outdoor_temp_verbier_2022-01.csv"
click node_config "https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/config.py"
click node_network_operators "https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/data/network_operators.py"
click node_physics "https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/data/physics.py"
click node_attention "https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/model/attention.py"
click node_stability "https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/model/stability.py"
click node_unrolled_solver "https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/model/unrolled_solver.py"
click node_losses "https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/losses.py"
click node_train "https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/train.py"
click node_evaluate "https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/evaluate.py"
click node_generate "https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/generate.py"
click node_verification "https://github.com/sk-subroto/pignn-dhn/blob/main/tests/run_gates.py"

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_nodes_csv,node_pipes_csv,node_stations_csv,node_measurements,node_weather,node_config toneBlue
class node_pydhn,node_network_operators,node_physics toneAmber
class node_attention,node_stability,node_unrolled_solver,node_losses toneMint
class node_generated_data,node_train,node_evaluate,node_generate,node_verification toneRose
```

## Components

### Network & inputs

- [Network nodes](https://github.com/sk-subroto/pignn-dhn/blob/main/data/network/nodes.csv): CSV topology · `data/network/nodes.csv`
- [Pipe parameters](https://github.com/sk-subroto/pignn-dhn/blob/main/data/network/pipes.csv): CSV parameters · `data/network/pipes.csv`
- [Heating stations](https://github.com/sk-subroto/pignn-dhn/blob/main/data/network/heating_stations.csv): CSV boundary data · `data/network/heating_stations.csv`
- [Mass-flow measurements](https://github.com/sk-subroto/pignn-dhn/blob/main/data/measurements/mass_flow.csv): CSV measurements · `data/measurements/mass_flow.csv`
- [Outdoor temperature](https://github.com/sk-subroto/pignn-dhn/blob/main/data/weather/outdoor_temp_verbier_2022-01.csv): CSV weather · `data/weather/outdoor_temp_verbier_2022-01.csv`
- [Dataset & fluid config](https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/config.py): Python configuration · Centralizes paths and fixed 50 °C fluid properties. · `dhn_gnn/config.py`

### Physics representation

- **PyDHN**: External network engine · Builds the network and provides validation/oracle comparisons.
- [Cycle-space operators](https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/data/network_operators.py): Graph algebra builder · Builds incidence A, cycle matrix B, and independent-loop masks. · `dhn_gnn/data/network_operators.py`
- [Differentiable hydraulics](https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/data/physics.py): Torch physics · Computes Darcy–Weisbach losses, derivatives, and loop residuals. · `dhn_gnn/data/physics.py`

### Hybrid solver

- [Resistance-biased attention](https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/model/attention.py): GNN refinement · Produces bounded learned corrections from current linearized resistance. · `dhn_gnn/model/attention.py`
- [Stability controls](https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/model/stability.py): Solver safeguards · Applies velocity caps and Armijo-style acceptance at evaluation. · `dhn_gnn/model/stability.py`
- [K-step unrolled solver](https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/model/unrolled_solver.py): Hybrid GNN solver · Reconstructs feasible flows, applies Newton-style updates, then learned corrections. · `dhn_gnn/model/unrolled_solver.py`
- [Deep-supervised residual loss](https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/losses.py): Physics loss · Discounts loop-residual loss across solver steps. · `dhn_gnn/losses.py`

### Execution & verification

- **Generated reference datasets**: External solved data · Git-ignored PyDHN reference outputs.
- [Training entry point](https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/train.py): Python runnable · Self-supervised training with truncated backpropagation between steps. · `dhn_gnn/train.py`
- [Evaluation entry point](https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/evaluate.py): Python runnable · Scores flows against PyDHN references and emits artifacts. · `dhn_gnn/evaluate.py`
- [Dataset generation](https://github.com/sk-subroto/pignn-dhn/blob/main/dhn_gnn/generate.py): Python runnable · Generates solved reference datasets. · `dhn_gnn/generate.py`
- [Verification gates](https://github.com/sk-subroto/pignn-dhn/blob/main/tests/run_gates.py): Python runnable · Regression gates and model smoke checks. · `tests/run_gates.py`

## Connections

- Network nodes → PyDHN: topology
- Pipe parameters → PyDHN: pipe data
- Heating stations → PyDHN: setpoints
- PyDHN → Cycle-space operators: network graph
- Cycle-space operators → K-step unrolled solver: A, B, loop basis · Cycle corrections preserve mass conservation structurally.
- Differentiable hydraulics → K-step unrolled solver: losses & derivatives
- Resistance-biased attention → K-step unrolled solver: learned correction
- Stability controls → K-step unrolled solver: evaluation safeguards
- K-step unrolled solver → Deep-supervised residual loss: per-step residuals
- Dataset & fluid config → Training entry point: paths & fluid properties
- Mass-flow measurements → Training entry point: boundary inputs
- Outdoor temperature → Training entry point: weather inputs
- K-step unrolled solver → Training entry point: unrolled predictions
- Deep-supervised residual loss → Training entry point: self-supervised objective
- PyDHN → Dataset generation: solves reference cases
- Dataset generation → Generated reference datasets: writes
- K-step unrolled solver → Evaluation entry point: predictions
- Generated reference datasets → Evaluation entry point: reference flows
- PyDHN → Evaluation entry point: oracle comparison
- Training entry point → Verification gates: trained model
