# flaport/fdtd architecture

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

- Interactive diagram: https://gitdiagram.com/flaport/fdtd
- Repository: https://github.com/flaport/fdtd

## Overview

##### Project type and purpose
- This is a Python library (fdtd) implementing a 3-dimensional Finite-Difference Time-Domain electromagnetic simulator.
- It provides a programmable API for defining simulation grids, placing objects, sources, detectors, boundaries (PML, periodic), and running time-stepped Maxwell’s equations updates.
- It optionally targets GPU acceleration via a PyTorch backend, but defaults to NumPy on CPU.

##### File structure
Top-level:
- setup files (.gitignore, pyproject.toml, Makefile, CI/workflows) – packaging & CI.
- README.md + docs/ – user documentation and Sphinx config.
- examples/ – Jupyter notebooks demonstrating use cases.
- fdtd/ – core library:
– backend.py – strategy pattern for selecting NumPy vs Torch.
– grid.py – simulation engine (Grid class holds fields, runs time steps).
– boundaries.py – boundary‐condition classes (PML, periodic).
– objects.py, sources.py, detectors.py – placement of simulation entities.
– fourier.py, waveforms.py, conversions.py, constants.py – utilities/math.
– visualization.py – 2D/3D rendering via matplotlib.
– typing_.py – type definitions for API.
- tests/ – pytest fixtures and unit tests for each module.

##### README for additional insights
- Detailed explanation of Grid API, field update equations (curl_E, curl_H), boundary updates, PML algorithm.
- Backend selection mechanism (NumPy vs Torch/dtypes/GPU).

- Clear labels: “Grid Engine,” “Backend,” “Objects,” etc.

##### Detailed capturing of essential elements
- Field arrays (E, H) as internal state.
- Slice‐based “plugin” registration for objects/sources/detectors/boundaries on the Grid.
- Backend selection at initialization and how compute kernels dispatch to either NumPy or Torch.
- Visualization submodule reading the Grid state and calling matplotlib.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% API Layer
    subgraph "API Layer" 
        direction TB
        User["User Scripts / Notebooks"]:::api
        Examples["examples/"]:::api
    end

    %% Core Library
    subgraph "Core Library"
        direction TB
        GridEngine["Grid Engine"]:::core
        BackendStrategy["Backend Strategy"]:::core
        subgraph "Plugin Modules"
            direction TB
            Objects["Objects"]:::core
            Sources["Sources"]:::core
            Detectors["Detectors"]:::core
            Boundaries["Boundary Conditions"]:::core
        end
        subgraph "Utilities"
            direction TB
            Conversions["Conversions"]:::core
            Constants["Constants"]:::core
            Fourier["Fourier Transforms"]:::core
            Waveforms["Waveforms"]:::core
            Typing["Type Definitions"]:::core
        end
        Visualization["Visualization"]:::core
    end

    %% Computation Layer
    subgraph "Computation Layer"
        direction TB
        NumPy["NumPy"]:::ext
        PyTorch["PyTorch (CPU/GPU)"]:::ext
    end

    %% External Dependencies
    subgraph "External Dependencies"
        direction TB
        Matplotlib["matplotlib"]:::ext
        SciPy["scipy"]:::ext
        TQDM["tqdm"]:::ext
    end

    %% Development Pipeline
    subgraph "Development Pipeline"
        direction TB
        Docs["Documentation (Sphinx)"]:::dev
        CI["CI Workflows"]:::dev
        Packaging["Packaging & Config"]:::dev
    end

    %% Runtime Flow
    User -->|uses| Examples
    User -->|calls API| GridEngine
    Examples --> GridEngine
    GridEngine -->|selects| BackendStrategy
    BackendStrategy -->|dispatches to| NumPy
    BackendStrategy -->|dispatches to| PyTorch
    GridEngine -->|registers| Objects
    GridEngine -->|registers| Sources
    GridEngine -->|registers| Detectors
    GridEngine -->|registers| Boundaries
    GridEngine -->|uses| Conversions
    GridEngine -->|uses| Constants
    GridEngine -->|uses| Fourier
    GridEngine -->|uses| Waveforms
    GridEngine -->|uses| Typing
    GridEngine -->|provides state to| Visualization
    Visualization -->|renders via| Matplotlib

    %% Development Flow
    CI -.->|runs tests on| GridEngine
    CI -.->|validates docs in| Docs
    CI -.->|checks packaging| Packaging
    Packaging -.->|includes pyproject.toml etc| GridEngine

    %% Click Events
    click Examples "https://github.com/flaport/fdtd/tree/master/examples/"
    click GridEngine "https://github.com/flaport/fdtd/blob/master/fdtd/grid.py"
    click BackendStrategy "https://github.com/flaport/fdtd/blob/master/fdtd/backend.py"
    click Boundaries "https://github.com/flaport/fdtd/blob/master/fdtd/boundaries.py"
    click Objects "https://github.com/flaport/fdtd/blob/master/fdtd/objects.py"
    click Sources "https://github.com/flaport/fdtd/blob/master/fdtd/sources.py"
    click Detectors "https://github.com/flaport/fdtd/blob/master/fdtd/detectors.py"
    click Conversions "https://github.com/flaport/fdtd/blob/master/fdtd/conversions.py"
    click Constants "https://github.com/flaport/fdtd/blob/master/fdtd/constants.py"
    click Fourier "https://github.com/flaport/fdtd/blob/master/fdtd/fourier.py"
    click Waveforms "https://github.com/flaport/fdtd/blob/master/fdtd/waveforms.py"
    click Typing "https://github.com/flaport/fdtd/blob/master/fdtd/typing_.py"
    click Visualization "https://github.com/flaport/fdtd/blob/master/fdtd/visualization.py"
    click Docs "https://github.com/flaport/fdtd/tree/master/docs/"
    click CI "https://github.com/flaport/fdtd/tree/master/.github/workflows/"
    click Packaging "https://github.com/flaport/fdtd/blob/master/pyproject.toml"

    %% Styles
    classDef core fill:#D6EAF8,stroke:#2980B9,stroke-width:1px
    classDef ext fill:#D5F5E3,stroke:#27AE60,stroke-width:1px
    classDef dev fill:#FCF3CF,stroke:#F1C40F,stroke-width:1px
    classDef api fill:#FADBD8,stroke:#C0392B,stroke-width:1px
```
