# d3/d3-force architecture

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

- Interactive diagram: https://gitdiagram.com/d3/d3-force
- Repository: https://github.com/d3/d3-force

## Overview

##### Main Components

a) Core Simulation Engine
- Center the "ForceSimulation" component as the core orchestrator

b) Force Components (arrange these as modules around the core):
- Center force (center.js)
- Collision detection (collide.js)
- Many-body force (manyBody.js)
- Link force (link.js)
- Radial force (radial.js)
- Positional forces (x.js, y.js)

**→ Data flow**
**⟲ Force application**
**● Core components**
**□ Force modules**
**◇ Utility functions**

##### Component Relationships

a) Primary Layer:
- Force components in a circular pattern around it
- How each force component can be plugged into the simulation

b) Secondary Layer:
- Utility functions (jiggle.js, lcg.js) as supporting components
- Constant.js as a global configuration provider

##### Data Flow Representation

- Input: Initial particle/node positions and properties
- Processing: Force calculations and velocity updates
- Output: Updated particle positions after each tick

##### Specific Implementation Details

- The modular nature of the force components
- The iterative simulation process
- How forces can be combined and configured
- The physics-based calculation pipeline

##### Implementation Notes

Callouts explaining:
- How forces are registered with the simulation
- The tick-based update cycle
- Force parameter configuration
- Collision detection optimization

- Clear hierarchical relationships
- Nested components
- Text annotations

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Core
        Sim["Simulation Core"]:::core
        click Sim "https://github.com/d3/d3-force/blob/main/src/simulation.js"
        Entry["Module Entry Point"]:::core
        click Entry "https://github.com/d3/d3-force/blob/main/src/index.js"
        IntSys["Integration System"]:::core
        Verlet["Verlet Integration"]:::math
    end

    subgraph "Force Calculators"
        Center["Center Force"]:::force
        click Center "https://github.com/d3/d3-force/blob/main/src/center.js"
        Collide["Collision Force"]:::force
        click Collide "https://github.com/d3/d3-force/blob/main/src/collide.js"
        ManyBody["Many-Body Force"]:::force
        click ManyBody "https://github.com/d3/d3-force/blob/main/src/manyBody.js"
        Link["Link Force"]:::force
        click Link "https://github.com/d3/d3-force/blob/main/src/link.js"
        Radial["Radial Force"]:::force
        click Radial "https://github.com/d3/d3-force/blob/main/src/radial.js"
        PosX["X Position Force"]:::force
        click PosX "https://github.com/d3/d3-force/blob/main/src/x.js"
        PosY["Y Position Force"]:::force
        click PosY "https://github.com/d3/d3-force/blob/main/src/y.js"
    end

    subgraph "Particle System"
        PosMgmt["Position Management"]:::particle
        VelMgmt["Velocity Management"]:::particle
    end

    subgraph "Utilities"
        Jiggle["Jiggle Calculator"]:::util
        click Jiggle "https://github.com/d3/d3-force/blob/main/src/jiggle.js"
        LCG["Linear Congruential Generator"]:::util
        click LCG "https://github.com/d3/d3-force/blob/main/src/lcg.js"
        Const["Constants"]:::util
        click Const "https://github.com/d3/d3-force/blob/main/src/constant.js"
    end

    subgraph "Testing"
        SimTest["Simulation Tests"]:::test
        click SimTest "https://github.com/d3/d3-force/blob/main/test/simulation-test.js"
        ForceTests["Force Tests"]:::test
        click ForceTests "https://github.com/d3/d3-force/blob/main/test/center-test.js"
    end

    Entry --> Sim
    Sim --> IntSys
    IntSys --> Verlet
    
    Sim --> Center
    Sim --> Collide
    Sim --> ManyBody
    Sim --> Link
    Sim --> Radial
    Sim --> PosX
    Sim --> PosY

    Center --> PosMgmt
    Collide --> PosMgmt
    ManyBody --> PosMgmt
    Link --> PosMgmt
    Radial --> PosMgmt
    PosX --> PosMgmt
    PosY --> PosMgmt

    PosMgmt --> VelMgmt
    VelMgmt --> Verlet
    Verlet --> PosMgmt

    ManyBody -.-> Jiggle
    Collide -.-> LCG
    Sim -.-> Const

    classDef core fill:#2196F3,stroke:#1565C0,color:white
    classDef force fill:#4CAF50,stroke:#388E3C,color:white
    classDef particle fill:#FF9800,stroke:#F57C00,color:white
    classDef math fill:#9C27B0,stroke:#7B1FA2,color:white
    classDef util fill:#607D8B,stroke:#455A64,color:white
    classDef test fill:#795548,stroke:#5D4037,color:white

    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["Force Calculators"]:::force
        L3["Particle System"]:::particle
        L4["Math Components"]:::math
        L5["Utilities"]:::util
        L6["Testing"]:::test
    end
```
