# deepmodeling/jax-fem architecture

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

- Interactive diagram: https://gitdiagram.com/deepmodeling/jax-fem
- Repository: https://github.com/deepmodeling/jax-fem

## Overview

This project is a GPU‐accelerated, differentiable finite element analysis (FEM) package built on JAX that is designed for a wide range of simulation problems – from linear and nonlinear elasticity to inverse design and multi-physics applications. The package also provides various demonstrations, benchmarks, and example applications to showcase its capabilities in solving problems such as hyperelasticity, plasticity, Poisson’s equation, topology optimization, and more.

##### Overall Project Type and Purpose
- This is a scientific simulation library specialized in finite element analysis. The focus is on differentiable simulation, which leverages GPU capabilities and automatic differentiation via JAX.
- It has components for defining and solving FEM problems, meshing, handling various boundary conditions, and even sensitivity analysis for inverse problems.
- Modular design that supports validation through demos and tests as well as example applications (in “applications” and “demos” folders).

##### Main Components
a. Core FEM Library (jax_fem):
- Contains the core functionality such as mesh generation (get_meshio_cell_type, rectangle_mesh, generate_mesh.py), problem definition (problem.py), solver routines (solver.py), finite element formulation (fe.py, basis.py), sensitivity analysis (hessian.py), and experimental extensions (under experimental folder).
- Represents the analytical engine where FEM kernels, assembly routines, and GPU acceleration via JAX are implemented.

b. Applications and Demos:
- “applications” directory: Examples and specialized scripts (e.g., arc_length, battery, dendrite, topology optimization, plasticity gradient, etc.) showcase different use cases and extended features (for example, handling different boundary conditions, dynamic relaxations, or multi-scale analyses).
- “demos” directory: Curated demonstrations for particular FEM cases (hyperelasticity, inverse problems, thermal-mechanical coupling, phase field fracture, etc.). They serve as both tutorials and validation cases.

c. Test and Benchmark Suite:
- The “tests” folder contains automated benchmarks and test cases which ensure numerical accuracy and performance. Subdirectories under tests/benchmarks group together different simulation cases.

d. External Dependencies (Integration Layer):
- JAX: Provides GPU acceleration and automatic differentiation.
- PETSc: Integration for solver choices.
- Other tools for visualization (e.g., Paraview, as mentioned in examples) and numerical libraries.

e. Supporting Files:
- Environment and configuration files such as environment.yml, pyproject.toml, which indicate dependency management and build configuration.
- Documentation (README.md, demo READMEs) and example scripts for user orientation.

##### Interactions and Data Flow
- Application / Demo Layer: Application scripts (in “applications” and “demos”) instantiate FEM problems – they use helper functions to generate meshes, specify material properties, and set boundary/initial conditions.
- Problem Definition: User-defined or pre-coded problems are defined using classes from jax_fem.problem. These are augmented by boundary condition definitions and source/mass mappings.
- Mesh Generation: The process of converting geometric domains into finite element meshes (via rectangle_mesh and generate_mesh functions).
- Solver Workflow: After setting up the problem, the solver (jax_fem.solver) is invoked and, if needed, integrated with PETSc or other libraries for numerical solution.
- Post-Processing: Results are saved using utilities (e.g., save_sol from jax_fem.utils) and later visualized with external tools like Paraview.
- Testing and Benchmarking: Test cases in the “tests” directory validate various aspects of the FEM simulation, ensuring correctness and performance.

##### Key Architectural Patterns and Design Principles
- Modular Separation: Core FEM functionalities are separated from application-specific scripts. This promotes reuse and better maintainability.
- Layered Approach: Distinguish between low-level numerical kernels (mesh generation, assembly) and high-level problem specification (applications/demos).
- Extensibility: The folder structure indicates that new applications or problem types can be added simply by introducing additional directories under “applications” or “demos”.
- Integration with External Libraries: Highlight use of JAX for GPU acceleration and automatic differentiation along with potential integration with PETSc.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Library Subgraph
    subgraph "Core Library (jax_fem)"
        A1["Problem Definition (problem.py)"]:::core
        A2["Mesh Generation (generate_mesh.py)"]:::core
        A3["FEM Formulation (fe.py & basis.py)"]:::core
        A4["Solver Routines (solver.py)"]:::core
        A5["Sensitivity Analysis (hessian.py)"]:::core
        A6["Experimental Extensions (experimental)"]:::core
    end

    %% Other Major Components
    B1["Applications"]:::app
    B2["Demos"]:::app
    B3["Test/Benchmark Suite"]:::test
    B4["JAX (GPU & Autodiff)"]:::external
    B5["PETSc (Solver Integration)"]:::external
    B6["Visualization Tools"]:::external
    B7["Supporting Files (env.yml, pyproject.toml, README.md)"]:::config

    %% Interactions and Data Flow
    B1 -->|"calls"| A1
    B2 -->|"calls"| A1
    A1 -->|"defines-mesh"| A2
    A2 -->|"provides-mesh"| A4
    A3 -->|"supplies-formulation"| A4
    A4 -->|"iterative-feedback"| A5
    A4 -->|"extends"| A6
    B3 -->|"validates"| A4
    B4 ---|"accelerates"| A4
    B5 ---|"integrates"| A4
    A4 -->|"post-process"| B6
    B7 ---|"configures"| A1

    %% Styles
    classDef core fill:#ADD8E6,stroke:#0000FF,stroke-width:2px;
    classDef app fill:#90EE90,stroke:#008000,stroke-width:2px;
    classDef external fill:#FFA07A,stroke:#FF4500,stroke-width:2px;
    classDef test fill:#FFE4B5,stroke:#FF8C00,stroke-width:2px;
    classDef config fill:#DDA0DD,stroke:#9932CC,stroke-width:2px;

    %% Click Events for Core Library
    click A1 "https://github.com/deepmodeling/jax-fem/blob/main/jax_fem/problem.py"
    click A2 "https://github.com/deepmodeling/jax-fem/blob/main/jax_fem/generate_mesh.py"
    click A3 "https://github.com/deepmodeling/jax-fem/blob/main/jax_fem/fe.py"
    click A3 "https://github.com/deepmodeling/jax-fem/blob/main/jax_fem/basis.py"
    click A4 "https://github.com/deepmodeling/jax-fem/blob/main/jax_fem/solver.py"
    click A5 "https://github.com/deepmodeling/jax-fem/blob/main/jax_fem/hessian.py"
    click A6 "https://github.com/deepmodeling/jax-fem/tree/main/jax_fem/experimental"

    %% Click Events for Other Components
    click B1 "https://github.com/deepmodeling/jax-fem/tree/main/applications"
    click B2 "https://github.com/deepmodeling/jax-fem/tree/main/demos"
    click B3 "https://github.com/deepmodeling/jax-fem/tree/main/tests/benchmarks"
    click B7 "https://github.com/deepmodeling/jax-fem/blob/main/environment.yml"
    click B7 "https://github.com/deepmodeling/jax-fem/blob/main/pyproject.toml"
    click B7 "https://github.com/deepmodeling/jax-fem/blob/main/README.md"
```
