# flexcompute/tidy3d architecture

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

- Interactive diagram: https://gitdiagram.com/flexcompute/tidy3d
- Repository: https://github.com/flexcompute/tidy3d

## Overview

##### Identify the Project Type and Purpose
Tidy3D is a **full-stack scientific computing application** designed for solving large-scale electrodynamics problems using the **Finite-Difference Time-Domain (FDTD)** method. It offers both a **Python API** for programmatic control and a **web-based graphical user interface (GUI)** for user-friendly interaction. The primary purpose is to allow users to define, submit, manage, and postprocess simulations on Flexcompute's servers.

##### Analyze the File Structure
- **Top-Level Directories:**
- `.github`: Contains GitHub workflows, issue templates, and configuration files for CI/CD.
- `docs`: Documentation for the project, including API references and installation guides.
- `scripts`: Utility scripts for benchmarking, testing, and pre-release tasks.
- `tests`: Comprehensive testing suite covering various components and plugins.
- `tidy3d`: Core Python package implementing the simulation functionalities.
- `tidy3d/plugins`: Extensible plugins for additional features like adjoint optimization, dispersion fitting, and microwave modeling.
- `tidy3d/web`: Web-based frontend for interacting with Tidy3D via a graphical interface.

- **Key Patterns and Architectural Choices:**
- **Modular Architecture:** Separation of core functionalities (`tidy3d`), plugins (`tidy3d/plugins`), and web interface (`tidy3d/web`).
- **Plugin System:** Enables extensibility and customization through plugins, indicating a **microservices-inspired** approach within the application.
- **Comprehensive Testing:** Presence of extensive tests suggests a focus on reliability and maintainability.
- **CI/CD Pipeline:** Automated workflows for testing, releasing, and syncing documentation, ensuring continuous integration and deployment.

- **Configuration and Deployment:**
- Configuration files like `.pre-commit-config.yaml`, `pyproject.toml`, and GitHub workflow YAML files indicate standardized development practices and automated deployment processes.

##### Examine the README for Additional Insights
The README highlights:
- **Dual Interface:** Users can interact via a Python API or a web-based GUI.
- **Simulation Management:** Ability to define, submit, manage, and postprocess simulations.
- **Authentication and API Integration:** Requires API keys for accessing Flexcompute servers.
- **Documentation and Examples:** Extensive documentation and notebook examples available for users.
- **Open Source with Licensed Backend:** The frontend is open source, but backend simulations require an account with credits.

##### Frontend Components
- **Python API (`tidy3d`):** Allows programmatic control of simulations.
- **Web Interface (`tidy3d/web`):** Provides a graphical user interface for users to interact with simulations.

##### Backend Components
- **Simulation Engine (`tidy3d/components`):** Core functionalities for setting up and running FDTD simulations.
- **Plugins (`tidy3d/plugins`):** Extensible modules adding specialized features (e.g., adjoint optimization, dispersion fitting).
- **Simulation Management Service:** Handles submission, monitoring, and management of simulations on Flexcompute servers.

##### Infrastructure Components
- **Flexcompute Servers:** Hosts the computational resources required for running simulations.
- **API Gateway:** Manages API requests from the Python API and web interface to backend services.
- **Database:** Stores user data, simulation configurations, results, and monitoring data.

##### Auxiliary Components
- **Documentation (`docs`):** Maintains project documentation, API references, and user guides.
- **CI/CD Pipeline (`.github/workflows`):** Automates testing, deployment, and documentation synchronization.
- **Testing Suite (`tests`):** Ensures code reliability through extensive automated tests.

##### b. Determine Relationships and Interactions
- **User Interaction Flow:**
1. **User Interface:**
- Users interact via the **Python API** or **Web Interface** to define simulations.
2. **API Gateway:**
- Requests from both interfaces are routed through the **API Gateway**.
3. **Simulation Management Service:**
- Handles simulation submissions, communicates with **Flexcompute Servers**, and manages simulation lifecycle.
4. **Simulation Engine:**
- Executes the FDTD simulations using core components and any enabled plugins.
5. **Database:**
- Stores simulation configurations, results, and user information.
6. **Postprocessing:**
- Users can download and analyze simulation results via the API or web interface.

- **Development and Deployment Flow:**
- **Codebase (`tidy3d`, `tidy3d/plugins`, `tidy3d/web`)** is continuously tested (`tests`) and deployed through automated **CI/CD pipelines**.

##### c. Highlight Architectural Patterns and Design Principles
- **Modular and Layered Architecture:** Separation of concerns between frontend, backend, plugins, and infrastructure.
- **Extensibility via Plugins:** Facilitates adding new features without altering the core system.
- **Microservices-Inspired Services:** Components like simulation management operate independently, enhancing scalability and maintainability.
- **Automated Testing and Deployment:** Ensures code quality and streamlined releases.

##### d. Incorporate Relevant Technologies and Frameworks
- **Programming Language:** Python is the primary language for both the API and core functionalities.
- **Web Framework:** Likely uses frameworks such as **FastAPI** or **Flask** for the web interface and API Gateway.
- **CI/CD Tools:** GitHub Actions (`.github/workflows`) for automating workflows.
- **Documentation Tools:** Sphinx for generating documentation (`docs/conf.py`).
- **Testing Frameworks:** PyTest for automated testing (`tests` directory).
- **Package Management:** Poetry (`poetry.toml`) for dependency management.

##### Elements

- **Components:**
- **Frontend:**
- Python API
- Web Interface
- **Backend:**
- API Gateway
- Simulation Management Service
- Simulation Engine
- Plugins
- Database
- **Infrastructure:**
- Flexcompute Servers

- **Interactions:**
- **Dependencies** between Simulation Engine and Plugins.

- **Visual Distinctions:**

- **Labels:**

- **Data Flow:**
- Flow of simulation definitions, submission requests, execution status, and results retrieval.

##### Final Recommendations
- **Detail-Oriented:** Ensure all essential components, especially the plugin architecture and simulation workflow, are captured.
- **Iterative Refinement:** Start with high-level components and progressively add detailed interactions and dependencies.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Subgraphs and modules
    subgraph Frontend
        API["Python API"]:::frontend
        Web["Web Interface"]:::frontend
    end
    subgraph Backend
        APIGateway["API Gateway"]:::backend
        SimManager["Simulation Management Service"]:::backend
        SimEngine["Simulation Engine"]:::backend
        Plugins["Plugins"]:::backend
        DB["Database"]:::database
    end
    subgraph Infrastructure
        Flexcompute["Flexcompute Servers"]:::infrastructure
    end
    subgraph Auxiliary
        Docs["Documentation"]:::auxiliary
        CICD["CI/CD Pipeline"]:::auxiliary
        Tests["Testing Suite"]:::auxiliary
    end
    %% Connections
    API --> APIGateway
    Web --> APIGateway
    APIGateway --> SimManager
    SimManager --> SimEngine
    SimEngine --> Plugins
    SimEngine --> DB
    SimManager --> Flexcompute
    SimEngine -->|Executes on| Flexcompute
    %% Click Events
    click API "https://github.com/flexcompute/tidy3d/tree/develop/tidy3d/"
    click Web "https://github.com/flexcompute/tidy3d/tree/develop/tidy3d/web/"
    click APIGateway "https://github.com/flexcompute/tidy3d/tree/develop/tidy3d/web/api/"
    click SimEngine "https://github.com/flexcompute/tidy3d/tree/develop/tidy3d/components/"
    click Plugins "https://github.com/flexcompute/tidy3d/tree/develop/tidy3d/plugins/"
    click Docs "https://github.com/flexcompute/tidy3d/tree/develop/docs/"
    click CICD "https://github.com/flexcompute/tidy3d/tree/develop/.github/workflows/"
    click Tests "https://github.com/flexcompute/tidy3d/tree/develop/tests/"
    %% Styles
    classDef frontend fill:#a6cee3,stroke:#000,stroke-width:1px
    classDef backend fill:#b2df8a,stroke:#000,stroke-width:1px
    classDef infrastructure fill:#fdbf6f,stroke:#000,stroke-width:1px
    classDef auxiliary fill:#dddddd,stroke:#000,stroke-width:1px
    classDef database fill:#ffffcc,stroke:#000,stroke-width:1px
    class API,Web frontend
    class APIGateway,SimManager,SimEngine,Plugins backend
    class Flexcompute infrastructure
    class Docs,CICD,Tests auxiliary
    class DB database
```
