# openwateranalytics/epanet architecture

> Architecture diagram of the GitHub repository openwateranalytics/epanet, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/openwateranalytics/epanet
- Repository: https://github.com/openwateranalytics/epanet

## Overview

##### Project type and purpose
- OWA-EPANET is not a web-app or microservice suite but a C-based toolkit and command-line program for simulating hydraulic and water-quality behavior in pipe networks. It exposes a Programmer’s Toolkit (API library) plus a CLI “run” executable that uses that library.

##### File structure
- Top-level:
- CMakeLists.txt, conanfile.py, BUILDING.md → build & package
- / → public API headers (epanet2.h, enums, language bindings)
- src/ → core engine (hydraulics, water quality, I/O, memory, error handling, output module)
- run/ → CLI wrapper main.c
- tests/ → automated unit/integration tests, data, test-output generators
- tools/ → helper scripts for CI, config, reporting
- doc/ → Doxygen docs & examples
- Patterns: modular C code separated by functionality (input parser, solver, output), single static/shared library built with CMake, conan support.

a. Main components
- CLI Frontend (run/main.c)
- Input Parser module (src/input*.c)
- Memory & Error Manager (src/util/filemanager.c, src/util/errormanager.c, mempool)
- Hydraulic Solver (src/hydraul.c, hydsolver, flowbalance, hydstatus)
- Water-Quality Solver (src/qualreact.c, qualroute.c, quality.c)
- Reporting & Output (src/report.c, src/output.c, src/outfile/)
- Public API Layer (include/epanet2.h + language bindings)
- Build & CI (CMake, Conan, workflows)
- Test Harness (tests/)

b. Relationships & data flow
1. CLI reads network input file → calls API’s EN<function>
2. API delegates to Input Parser → builds in-memory network model
3. Model fed to Hydraulic Solver → computes flows/pressures
4. Results passed to Quality Solver → computes concentrations
5. Output Writer / Reporter formats results → writes text/binary files
6. CLI prints/returns exit code
7. Memory & Error Manager supports all modules for allocation & logging

c. Architectural patterns & principles
- Modular layering: CLI → API → core engine → util → OS I/O
- Single codebase delivering both library (static/shared) and executable
- Separation of concerns: parsing, solving, reporting
- Build-system abstraction: CMake + Conan for cross-platform

d. Key technologies
- C (ANSI)
- CMake, Conan, AppVeyor, GitHub Actions for builds/tests
- Doxygen for docs
- Python for some CI tools (tools/nrtest-epanet)

##### Elements
- Notes on tools: CMake, Doxygen, Conan

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Layers and components

    %% CLI Frontend
    subgraph "Frontend"
        CLI["CLI Frontend"]:::core
    end

    %% Public API
    subgraph "Public API"
        API["Public API"]:::public
    end

    %% Core Engine
    subgraph "Core Engine"
        subgraph "Parser"
            InpFile["inpfile.c"]:::core
            Input1["input1.c"]:::core
            Input2["input2.c"]:::core
            Input3["input3.c"]:::core
        end
        subgraph "Hydraulic Solver"
            Hydraul["hydraul.c"]:::core
            HydSolver["hydsolver.c"]:::core
            FlowBalance["flowbalance.c"]:::core
            HydStatus["hydstatus.c"]:::core
            HydCoeffs["hydcoeffs.c"]:::core
            SMatrix["smatrix.c"]:::core
        end
        subgraph "Water-Quality Solver"
            Quality["quality.c"]:::core
            QualReact["qualreact.c"]:::core
            QualRoute["qualroute.c"]:::core
        end
        subgraph "Output Writer"
            Report["report.c"]:::core
            Output["output.c"]:::core
            subgraph "Outfile Module"
                OutfileSrc["epanet_output.c"]:::core
                OutfileHdrs["include/"]:::core
            end
        end
    end

    %% Utilities
    subgraph "Utilities"
        MemPoolC["mempool.c"]:::util
        MemPoolH["mempool.h"]:::util
        ErrMgrC["errormanager.c"]:::util
        ErrMgrH["errormanager.h"]:::util
    end

    %% Build & CI
    subgraph "Build & CI"
        CMake["CMakeLists.txt"]:::infra
        Conan["conanfile.py"]:::infra
        Workflows[".github/workflows/"]:::infra
        AppVeyor["appveyor.yml"]:::infra
        Tools["tools/"]:::infra
    end

    %% Tests
    Tests["Test Harness (tests/)"]:::infra

    %% File System
    FS["Filesystem I/O"]:::infra

    %% Data Flow
    CLI -->|"calls EN<func>()"| API
    API -->|"parses network"| InpFile
    API --> Input1
    API --> Input2
    API --> Input3
    API -->|"builds model"| Hydraul
    Hydraul --> HydSolver
    HydSolver --> FlowBalance
    FlowBalance --> HydStatus
    HydStatus --> HydCoeffs
    HydCoeffs --> SMatrix
    SMatrix --> Quality
    Quality --> QualReact
    QualReact --> QualRoute
    QualRoute --> Report
    Report --> Output
    Output --> OutfileSrc
    OutfileSrc --> OutfileHdrs
    OutfileHdrs --> FS
    FS --> CLI

    %% Utility dependencies
    MemPoolC -.->|supports| InpFile
    MemPoolC -.-> Hydraul
    MemPoolC -.-> Quality
    MemPoolC -.-> Report
    ErrMgrC -.-> InpFile
    ErrMgrC -.-> Hydraul
    ErrMgrC -.-> Quality
    ErrMgrC -.-> Report

    %% API to Tests
    API -->|unit tests| Tests

    %% CI dependencies
    CMake -.-> CLI
    CMake -.-> API
    CMake -.-> Hydraul
    Conan -.-> CMake
    Workflows -.-> Tests
    AppVeyor -.-> Tests
    Tools -.-> Workflows

    %% Click Events
    click CLI "https://github.com/openwateranalytics/epanet/blob/dev/run/main.c"
    click InpFile "https://github.com/openwateranalytics/epanet/blob/dev/src/inpfile.c"
    click Input1 "https://github.com/openwateranalytics/epanet/blob/dev/src/input1.c"
    click Input2 "https://github.com/openwateranalytics/epanet/blob/dev/src/input2.c"
    click Input3 "https://github.com/openwateranalytics/epanet/blob/dev/src/input3.c"
    click MemPoolC "https://github.com/openwateranalytics/epanet/blob/dev/src/mempool.c"
    click MemPoolH "https://github.com/openwateranalytics/epanet/blob/dev/src/mempool.h"
    click ErrMgrC "https://github.com/openwateranalytics/epanet/blob/dev/src/util/errormanager.c"
    click ErrMgrH "https://github.com/openwateranalytics/epanet/blob/dev/src/util/errormanager.h"
    click Hydraul "https://github.com/openwateranalytics/epanet/blob/dev/src/hydraul.c"
    click HydSolver "https://github.com/openwateranalytics/epanet/blob/dev/src/hydsolver.c"
    click FlowBalance "https://github.com/openwateranalytics/epanet/blob/dev/src/flowbalance.c"
    click HydStatus "https://github.com/openwateranalytics/epanet/blob/dev/src/hydstatus.c"
    click HydCoeffs "https://github.com/openwateranalytics/epanet/blob/dev/src/hydcoeffs.c"
    click SMatrix "https://github.com/openwateranalytics/epanet/blob/dev/src/smatrix.c"
    click Quality "https://github.com/openwateranalytics/epanet/blob/dev/src/quality.c"
    click QualReact "https://github.com/openwateranalytics/epanet/blob/dev/src/qualreact.c"
    click QualRoute "https://github.com/openwateranalytics/epanet/blob/dev/src/qualroute.c"
    click Report "https://github.com/openwateranalytics/epanet/blob/dev/src/report.c"
    click Output "https://github.com/openwateranalytics/epanet/blob/dev/src/output.c"
    click OutfileSrc "https://github.com/openwateranalytics/epanet/blob/dev/src/outfile/src/epanet_output.c"
    click OutfileHdrs "https://github.com/openwateranalytics/epanet/tree/dev/src/outfile/include/"
    click API "https://github.com/openwateranalytics/epanet/blob/dev/include/epanet2.h"
    click CMake "https://github.com/openwateranalytics/epanet/blob/dev/CMakeLists.txt"
    click Conan "https://github.com/openwateranalytics/epanet/blob/dev/conanfile.py"
    click Workflows "https://github.com/openwateranalytics/epanet/tree/dev/.github/workflows/"
    click AppVeyor "https://github.com/openwateranalytics/epanet/blob/dev/appveyor.yml"
    click Tools "https://github.com/openwateranalytics/epanet/tree/dev/tools/"
    click Tests "https://github.com/openwateranalytics/epanet/tree/dev/tests/"

    %% Styles
    classDef public fill:#c7f9c0,stroke:#2f855a,color:#2f855a
    classDef core fill:#bee3f8,stroke:#2b6cb0,color:#2b6cb0
    classDef util fill:#fed7aa,stroke:#c05621,color:#c05621
    classDef infra fill:#e2e8f0,stroke:#4a5568,color:#4a5568
```
