# raesga02/fluid-simulation architecture

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

- Interactive diagram: https://gitdiagram.com/raesga02/fluid-simulation
- Repository: https://github.com/raesga02/fluid-simulation

## Overview

##### Project Type and Purpose
- This is a Unity project focused on simulating fluid dynamics both in 2D and 3D. The project uses compute shaders (HLSL and .compute files) and C# scripts to create simulations involving fluid rendering, physics computations, collision handling, and spawner setups.
- The README is minimal, but the rich file structure—dividing scripts into separate 2D and 3D modules, tests, helpers, and various simulation-related scenes—indicates a multifaceted simulation tool.

##### File Structure Analysis
- Top-Level Directories:
- Assets: Contains all Unity-specific assets.
- Assets/Scenes: Contains Unity scene files including “Simulation2D”, “Simulation3D”, and test scenes.
- Assets/Scripts: Divided into subdirectories for 2D, 3D, Helpers, and test modules. This points to a modular design where simulation logic is separated by dimensionality and functionality.
- Naming conventions like “FluidColliders”, “FluidComputations”, “FluidRendering”, “FluidSpawning” and “SimulationManager” clearly list out the major components.
- Presence of HLSL and compute shader files (.compute) indicates GPU offloading or heavy calculation areas, a key architectural detail.

##### Insights from the README
- Although the README content is terse, the project title confirms that the system’s primary role is simulating fluid dynamics. This reinforces the importance of the simulation engines and related compute components in the overall architecture.

- Main Components to Highlight:
- Frontend / Presentation: Unity Scenes (Simulation2D, Simulation3D, Test Scenes) which serve as the visual and interactive front end.
- Simulation Engines:
- 2D Simulation: Contains FluidPhysicsHelper2D, FluidUpdater (CPU and GPU variants), FluidRenderer, FluidSpawner, and Collider components.
- 3D Simulation: Mirrors the 2D structure with FluidPhysicsHelper3D, FluidUpdaterGPU3D, FluidRenderer, 3D spawner components, and additional features like marching cubes for rendering fluid surfaces.
- Shared/Helpers: Utility libraries and common code (e.g., ConvexMeshGenerator, SpatialHashingUtils) that support computations for both 2D and 3D.
- Test Modules: Separate directories under test for validating specific components such as collision detection, lighting, reduction tasks, and spawning tests.
- Interactions & Relationships:
- Dependency relationships where the simulation manager orchestrates fluid spawning, updates (both on CPU and GPU via compute shaders), rendering, and collision handling.
- Integration of external mechanisms (GPU shaders, Unity’s physics engine) and indicate that testing components interact with these modules for verification.
- Architectural Patterns & Design Principles:
- Modular organization: Clear separation between 2D and 3D modules.
- Data-parallel processing: Emphasize the use of compute shaders for fluid dynamics and spatial hashing techniques.
- MVC-like pattern in Unity: Scenes (view) invoke simulation managers (controller) which utilize underlying computational modules (model).
- Relevant Technologies & Frameworks:
- Unity Engine as the core framework.
- HLSL/C# for scripting and shader programming.
- Compute shader technology for performant GPU-based calculations.
- Prefabs, materials, and scenes represent typical Unity resource management patterns.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend / Presentation Layer
    FE("Frontend/Presentation (Unity Scenes)"):::frontend

    %% Simulation Managers
    subgraph "Simulation Managers"
        SM2D("2D Simulation Manager"):::manager
        SM3D("3D Simulation Manager"):::manager
    end

    %% 2D Simulation Components
    subgraph "2D Simulation"
        FColl2D("2D Fluid Colliders"):::subcomp
        FComp2D("2D Fluid Computations (CPU & GPU)"):::compute
        FRender2D("2D Fluid Rendering"):::subcomp
        FSpawn2D("2D Fluid Spawning"):::subcomp
        Helpers2D("2D Helpers/Utilities"):::helper
    end

    %% 3D Simulation Components
    subgraph "3D Simulation"
        FColl3D("3D Fluid Colliders"):::subcomp
        FComp3D("3D Fluid Computations (CPU & GPU)"):::compute
        FRender3D("3D Fluid Rendering"):::subcomp
        FSpawn3D("3D Fluid Spawning"):::subcomp
        UInput3D("3D User Input Management"):::subcomp
    end

    %% Test Modules
    Test("Test Modules"):::test

    %% Connections: Frontend to Simulation Managers
    FE -->|"initiate"| SM2D
    FE -->|"initiate"| SM3D

    %% Connections: 2D Simulation Manager to its components
    SM2D -->|"orchestrates"| FColl2D
    SM2D -->|"orchestrates"| FComp2D
    SM2D -->|"orchestrates"| FRender2D
    SM2D -->|"orchestrates"| FSpawn2D
    SM2D -->|"orchestrates"| Helpers2D

    %% Connections: 3D Simulation Manager to its components
    SM3D -->|"orchestrates"| FColl3D
    SM3D -->|"orchestrates"| FComp3D
    SM3D -->|"orchestrates"| FRender3D
    SM3D -->|"orchestrates"| FSpawn3D
    SM3D -->|"orchestrates"| UInput3D

    %% Connections: Test Modules
    SM2D -->|"testedBy"| Test
    SM3D -->|"testedBy"| Test
    FE -->|"triggers"| Test

    %% Click Events
    click SM2D "https://github.com/raesga02/fluid-simulation/blob/main/Assets/Scripts/2D/SimulationManager.cs"
    click FColl2D "https://github.com/raesga02/fluid-simulation/tree/main/Assets/Scripts/2D/FluidColliders"
    click FComp2D "https://github.com/raesga02/fluid-simulation/tree/main/Assets/Scripts/2D/FluidComputations"
    click FRender2D "https://github.com/raesga02/fluid-simulation/tree/main/Assets/Scripts/2D/FluidRendering"
    click FSpawn2D "https://github.com/raesga02/fluid-simulation/tree/main/Assets/Scripts/2D/FluidSpawning"
    click Helpers2D "https://github.com/raesga02/fluid-simulation/tree/main/Assets/Scripts/2D/Helpers"
    click SM3D "https://github.com/raesga02/fluid-simulation/blob/main/Assets/Scripts/3D/SimulationManager.cs"
    click FColl3D "https://github.com/raesga02/fluid-simulation/tree/main/Assets/Scripts/3D/FluidColliders"
    click FComp3D "https://github.com/raesga02/fluid-simulation/tree/main/Assets/Scripts/3D/FluidComputations"
    click FRender3D "https://github.com/raesga02/fluid-simulation/tree/main/Assets/Scripts/3D/FluidRendering"
    click FSpawn3D "https://github.com/raesga02/fluid-simulation/tree/main/Assets/Scripts/3D/FluidSpawning"
    click UInput3D "https://github.com/raesga02/fluid-simulation/blob/main/Assets/Scripts/3D/UserInputManager.cs"
    click FE "https://github.com/raesga02/fluid-simulation/tree/main/Assets/Scenes"
    click Test "https://github.com/raesga02/fluid-simulation/tree/main/Assets/Scripts/test"

    %% Styles
    classDef frontend fill:#B5EAD7,stroke:#333,stroke-width:2px;
    classDef manager fill:#FFCC00,stroke:#333,stroke-width:2px;
    classDef subcomp fill:#A0C4FF,stroke:#333,stroke-width:2px;
    classDef compute fill:#CDB4DB,stroke:#333,stroke-width:2px;
    classDef helper fill:#FFFFCC,stroke:#333,stroke-width:2px;
    classDef test fill:#FFADAD,stroke:#333,stroke-width:2px;
```
