# andreweiner/machine-learning-applied-to-cfd architecture

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

- Interactive diagram: https://gitdiagram.com/andreweiner/machine-learning-applied-to-cfd
- Repository: https://github.com/andreweiner/machine-learning-applied-to-cfd

## Overview

##### Project Type and Purpose
- This is a specialized application that combines CFD with various ML techniques (supervised, unsupervised, and reinforcement learning).
- Its purpose is to showcase how ML algorithms can be integrated into different steps of CFD studies—whether pre-, run-time, or post-processing (such as geometry/mesh generation, dynamic boundary conditions, and result analysis).

##### File Structure
- The directory tree shows clear divisions:
- An “OpenFOAM” directory that contains CFD cases (e.g., “moving_boundary” and “moving_boundary_wedge”) and an “apps” folder hosting a custom PyTorch displacement application. These contain all the simulation setup files (case files, control dictionaries, mesh definitions, etc.) and source code for OpenFOAM’s integration of PyTorch.
- A “notebooks” folder that hosts multiple Jupyter notebooks and helper modules for ML experiments and analysis.
- A “data” folder with input datasets (CSV and pickle files) that are likely used for training and evaluation.
- Support files like the “runContainer.sh” script to launch a Docker container configured for OpenFOAM+PyTorch, and ancillary documentation and literature files.

##### Key Components
a. ML/Analysis Environment:
- Components: Jupyter Notebooks, Python libraries (NumPy, Pandas, PyTorch, Matplotlib, Scikit-Learn).

b. Simulation Environment (CFD):
- Components: OpenFOAM cases (“moving_boundary” & “moving_boundary_wedge”), custom application code (pyTorchDisplacement… files), dynamic mesh/control dictionaries.
- Role: Execute the CFD simulations integrating machine learning (for instance, runtime ML components for dynamic boundary conditions).

c. Data Management:
- Components: “data” directory (input CSV files, pickle models, regimes data).
- Role: Provide datasets for training ML models and for validating CFD simulations.

d. Execution/Deployment Environment:
- Components: Docker container (configured via runContainer.sh and a Docker image on Dockerhub) for compiling and executing OpenFOAM+PyTorch applications.
- Role: Ensure reproducible CFD simulation runs and proper integration with ML apps.

##### Relationships and Interactions
- Data flows from the “data” directory to both “notebooks” (for training/analysis) and the CFD cases (to set up simulation parameters or validate simulation results).
- The Jupyter environment processes and visualizes ML models coming from experiments on CFD data.
- CFD simulations run within the Docker-based OpenFOAM container may ingest ML predictions (from pyTorchDisplacement modules) that affect boundary conditions.
- Feedback loops could be indicated where CFD simulation outputs are further used to improve ML models in an iterative fashion.

##### Architectural Patterns
- Separation of concerns: Clear boundary between CFD simulation (OpenFOAM) and ML experimentation (Python notebooks and PyTorch code).
- Micro-environment encapsulation: The use of Docker for containerizing the OpenFOAM+PyTorch simulation environment.
- Modular design: Distinct folders for simulation cases, ML notebooks, and data, facilitating isolated development and testing of each piece.

## Diagram (Mermaid)

```mermaid
graph TD
    Data["Data Storage"]:::data

    subgraph "ML Environment (Jupyter & PyTorch)"
        ML_NB["Jupyter Notebook: 2D_shape_approximation.ipynb"]:::ml
        ML_Helper["Helper Module: helper_module.py"]:::ml
    end

    subgraph "CFD Simulation (OpenFOAM Cases & Apps)"
        subgraph "CFD Cases"
            OF_Case1["moving_boundary"]:::sim
            OF_Case2["moving_boundary_wedge"]:::sim
        end
        OF_App["pyTorch Displacement App"]:::sim
    end

    Deployment["Deployment Container (Docker)"]:::deploy

    Data -->|"for_training"| ML_NB
    Data -->|"simulation_input"| OF_Case1
    Data -->|"simulation_input"| OF_Case2
    ML_NB -->|"ML_predictions"| OF_App
    OF_App -->|"simulation_output"| ML_NB
    OF_Case1 -->|"case_processes"| OF_App
    OF_Case2 -->|"case_processes"| OF_App
    Deployment -->|"executes_simulation"| OF_App

    click ML_NB "https://github.com/andreweiner/machine-learning-applied-to-cfd/blob/master/notebooks/2D_shape_approximation.ipynb"
    click ML_Helper "https://github.com/andreweiner/machine-learning-applied-to-cfd/blob/master/notebooks/helper_module.py"
    click OF_Case1 "https://github.com/andreweiner/machine-learning-applied-to-cfd/tree/master/OpenFOAM/cases/moving_boundary"
    click OF_Case2 "https://github.com/andreweiner/machine-learning-applied-to-cfd/tree/master/OpenFOAM/cases/moving_boundary_wedge"
    click OF_App "https://github.com/andreweiner/machine-learning-applied-to-cfd/blob/master/OpenFOAM/apps/pyTorchDisplacement/pyTorchDisplacementPointPatchVectorField.C"
    click Data "https://github.com/andreweiner/machine-learning-applied-to-cfd/tree/master/data"
    click Deployment "https://github.com/andreweiner/machine-learning-applied-to-cfd/blob/master/OpenFOAM/runContainer.sh"

    classDef ml fill:#FFFACD,stroke:#333,stroke-width:2px;
    classDef sim fill:#AED6F1,stroke:#333,stroke-width:2px;
    classDef data fill:#D5F5E3,stroke:#333,stroke-width:2px;
    classDef deploy fill:#F5B7B1,stroke:#333,stroke-width:2px;
```
