# epic-model/epic architecture

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

- Interactive diagram: https://gitdiagram.com/epic-model/epic
- Repository: https://github.com/epic-model/epic

## Overview

Based on our review of the complete file tree and the README, it’s clear that EPIC – “Elliptical Parcel-in-Cell” – is a scientific simulation tool written mostly in Fortran (with MPI support for parallelism) and complemented by Python scripts for analysis and visualization. Its purpose is to simulate parcel-based (likely fluid or atmospheric) processes in both 2D and 3D, while offering comprehensive configuration, diagnostic, and testing capabilities.

##### Project Type & Purpose
- This is a high-performance scientific simulation tool with a focus on parcel-in-cell methods.
- Its modules compute physical fields, manage parcel data, perform FFTs, and use MPI-based parallelization to simulate and analyze 2D/3D models.
- Additionally, there is extensive support for configuration, unit-testing, postprocessing (analysis), and visualization via Python.

##### Main Architectural Components
A. Build & Deployment Infrastructure:
- Autotools files (configure.ac, Makefile.am, m4 scripts) are used to manage the build process.
- The “dependencies” directory lists external libraries (HDF5, NetCDF, Scalasca, SCORE-P) that are integrated into the build.

B. Simulation Core:
- The “src” directory contains the heart of the computing kernels.
- Within src/2d and src/3d you find simulation drivers (epic2d.f90 and epic3d.f90) that integrate different sub-modules.
- Modules for fields and parcels are clearly separated, each with diagnostics, netCDF I/O, and other support.
- There’s an FFT subcomponent (src/fft) that handles spectral methods.
- The “stepper” subdirectories (in both 2d and 3d) implement time-stepping methods (e.g., rk4 variants).
- Utility modules (src/utils) encapsulate various support algorithms, numerical utilities, and physics subroutines.

C. Parallel Communication and MPI Integration:
- The “src/mpi” directory contains MPI-specific implementations (MPI environment, collectives, data types, communication utilities).
- These components ensure that the simulation scales across processing cores.

D. Physics Models:
- The “models” directory (with distinct 2d and 3d subdirectories) provides various physical model implementations (e.g., robert, taylor-green, beltrami).
- This layer abstracts the actual physical processes and numerical models that are plugged into the simulation core.

E. Additional Modules:
- “analyse”: Contains modules for performing post hoc analysis on 2D and 3D simulation results (likely generating specialized diagnostics).
- “merging_standalone”: Provides an independent example or test case for parcel merging using standalone approaches.
- “run-script”: Includes scripts to launch simulations using specific configurations (template.config, epic-run.py).

F. Data I/O Layer:
- The “src/netcdf” directory handles reading/writing simulation data in the NetCDF format.
- Configuration and example files (in examples and docs/manual) indicate how simulation runs are controlled.

G. Post-Processing and Visualization:
- “python-scripts” contains Python-based tools for plotting, animation, and diagnostics (using libraries like Matplotlib, Bokeh, etc.).
- These tools likely act as consumers of the netCDF output produced by the simulation, offering visualization and further analysis.

H. Testing and Quality Assurance:
- “unit-tests”, “mpi-tests”, and “time_step_tests” collectively cover a comprehensive suite for verifying simulation correctness.
- They target a variety of components – from MPI data handling to numerical accuracy in FFT computations and parcel physics.

I. Documentation & Examples:
- “docs” and “examples” provide the user manual, installation instructions, and sample configuration files which are critical for understanding runtime behavior.

##### Relationships and Data Flow
- Input Components: The simulation is configured via input files and templates (found in examples and run-script). These are fed into a configuration manager that sets up the simulation parameters.
- Simulation Core: The central engine (EPIC) uses the scientific models (from the “models” folder) and computation modules (in “src”, “fft”, and “mpi”) to perform simulations.
- I/O: Simulation results are written in NetCDF format which then serves as input for analysis.
- Post-Processing: Separate analysis components (Fortran-based in “analyse” and Python-based visualization in “python-scripts”) take simulation output and generate diagnostics, plots, or animations.
- Testing: At each stage, dedicated tests (unit-tests, mpi-tests) check computational integrity and parallel behavior.
- Build/Deployment: The autotools setup manages compilation and linking of all components with their dependencies.

##### Important Architectural Patterns & Design Principles
- Modularity: Careful separation of concerns by splitting simulation kernels, physical models, MPI utilities, and post-processing tools into dedicated directories.
- Layering: Clear layers for configuration input, computational core, I/O, and analysis.
- Reusability: Many modules (e.g., in src/utils and src/mpi) are designed to be reusable across both 2D and 3D simulations.
- Parallelism: MPI is deeply integrated as a first-class citizen allowing distributed computation.
- Testing Driven: A rich set of unit and integration tests ensures the simulation’s reliability on different system layers.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Build & Deployment Layer
    subgraph "Build & Deployment"
        BD_A["Autotools"]:::build
        BD_M["m4 Scripts"]:::build
        BD_D["Dependencies"]:::build
    end

    %% Simulation Core Layer
    subgraph "Simulation Core"
        subgraph "2D Simulation"
            s2_driver["epic2d.f90 (Driver)"]:::core
            s2_fields["2D Fields"]:::core
            s2_parcels["2D Parcels"]:::core
            s2_steppers["2D Timesteppers"]:::core
            s2_utils["2D Utilities"]:::core
        end
        subgraph "3D Simulation"
            s3_driver["epic3d.f90 (Driver)"]:::core
            s3_fields["3D Fields"]:::core
            s3_parcels["3D Parcels"]:::core
            s3_steppers["3D Timesteppers"]:::core
            s3_utils["3D Utilities"]:::core
        end
        fft["FFT Routines"]:::core
    end

    %% Parallel Communication and MPI Integration
    subgraph "Parallel Communication"
        mpi["MPI Integration"]:::mpi
    end

    %% Physics Models
    subgraph "Physics Models"
        pm2["2D Physics Models"]:::model
        pm3["3D Physics Models"]:::model
    end

    %% Additional Modules
    subgraph "Additional Modules"
        ana["Fortran Analysis"]:::additional
        merg["Standalone Merging"]:::additional
        rs["Run Scripts"]:::additional
    end

    %% Data I/O Layer
    subgraph "Data I/O"
        io["NetCDF I/O"]:::io
    end

    %% Post-Processing and Visualization
    subgraph "Post-Processing & Visualization"
        py["Python Visualization"]:::post
    end

    %% Testing and Quality Assurance
    subgraph "Testing & QA"
        T1["Unit Tests"]:::test
        T2["MPI Tests"]:::test
        T3["Time-Step Tests"]:::test
    end

    %% Documentation & Examples
    subgraph "Documentation & Examples"
        docs["Docs"]:::docs
        ex["Examples"]:::docs
    end

    %% Connections (Data flow)

    %% Build & Deployment flows into Simulation Core
    BD_A -->|"builds"| s2_driver
    BD_A -->|"builds"| s3_driver

    %% Run Scripts launch simulations
    rs -->|"launches"| s2_driver
    rs -->|"launches"| s3_driver

    %% Simulation Core drivers interact with submodules internally (implicit grouping)
    s2_driver --> s2_fields
    s2_driver --> s2_parcels
    s2_driver --> s2_steppers
    s2_driver --> s2_utils

    s3_driver --> s3_fields
    s3_driver --> s3_parcels
    s3_driver --> s3_steppers
    s3_driver --> s3_utils

    %% Simulation Core uses FFT and MPI components
    s2_driver -->|"calls"| fft
    s3_driver -->|"calls"| fft

    s2_driver -->|"communicates"| mpi
    s3_driver -->|"communicates"| mpi

    %% Simulation Core interacts with Physics Models
    s2_driver -->|"uses"| pm2
    s3_driver -->|"uses"| pm3

    %% Simulation Core outputs to Data I/O
    s2_driver -->|"outputs"| io
    s3_driver -->|"outputs"| io

    %% Data I/O feeds Post-Processing & Visualization
    io -->|"provides data for"| py

    %% Simulation Core supports Additional Analysis and Merging Modules
    s2_driver -->|"analyzes"| ana
    s3_driver -->|"analyzes"| ana
    merg -->|"demonstrates"| s2_driver
    merg -->|"demonstrates"| s3_driver

    %% Simulation Core tested by Testing suite
    s2_driver -->|"verified by"| T1
    s3_driver -->|"verified by"| T1
    mpi -->|"tested by"| T2
    fft -->|"evaluated by"| T3

    %% Documentation relates to additional modules and examples
    ex -->|"provides inputs"| rs
    docs -->|"guides"| rs

    %% Click Events
    click BD_A "https://github.com/epic-model/epic/blob/main/configure.ac"
    click BD_M "https://github.com/epic-model/epic/tree/main/m4"
    click BD_D "https://github.com/epic-model/epic/tree/main/dependencies"
    click s2_driver "https://github.com/epic-model/epic/blob/main/src/2d/epic2d.f90"
    click s3_driver "https://github.com/epic-model/epic/blob/main/src/3d/epic3d.f90"
    click s2_steppers "https://github.com/epic-model/epic/tree/main/src/2d/stepper"
    click s3_steppers "https://github.com/epic-model/epic/tree/main/src/3d/stepper"
    click s2_fields "https://github.com/epic-model/epic/tree/main/src/2d/fields"
    click s2_parcels "https://github.com/epic-model/epic/tree/main/src/2d/parcels"
    click s2_utils "https://github.com/epic-model/epic/tree/main/src/2d/utils"
    click s3_fields "https://github.com/epic-model/epic/tree/main/src/3d/fields"
    click s3_parcels "https://github.com/epic-model/epic/tree/main/src/3d/parcels"
    click s3_utils "https://github.com/epic-model/epic/tree/main/src/3d/utils"
    click fft "https://github.com/epic-model/epic/tree/main/src/fft"
    click mpi "https://github.com/epic-model/epic/tree/main/src/mpi"
    click pm2 "https://github.com/epic-model/epic/tree/main/models/2d"
    click pm3 "https://github.com/epic-model/epic/tree/main/models/3d"
    click ana "https://github.com/epic-model/epic/tree/main/analyse"
    click merg "https://github.com/epic-model/epic/tree/main/merging_standalone"
    click rs "https://github.com/epic-model/epic/tree/main/run-script"
    click io "https://github.com/epic-model/epic/tree/main/src/netcdf"
    click py "https://github.com/epic-model/epic/tree/main/python-scripts"
    click T1 "https://github.com/epic-model/epic/tree/main/unit-tests"
    click T2 "https://github.com/epic-model/epic/tree/main/mpi-tests"
    click T3 "https://github.com/epic-model/epic/tree/main/time_step_tests"
    click docs "https://github.com/epic-model/epic/tree/main/docs"
    click ex "https://github.com/epic-model/epic/tree/main/examples"

    %% Styles
    classDef build fill:#F4A460,stroke:#8B4513,stroke-width:2px;
    classDef core fill:#98FB98,stroke:#006400,stroke-width:2px;
    classDef mpi fill:#ADD8E6,stroke:#00008B,stroke-width:2px;
    classDef model fill:#FFD700,stroke:#B8860B,stroke-width:2px;
    classDef additional fill:#87CEFA,stroke:#4682B4,stroke-width:2px;
    classDef io fill:#FFB6C1,stroke:#C71585,stroke-width:2px;
    classDef post fill:#DDA0DD,stroke:#800080,stroke-width:2px;
    classDef test fill:#CD5C5C,stroke:#800000,stroke-width:2px;
    classDef docs fill:#90EE90,stroke:#006400,stroke-width:2px;
```
