# max-models/patchwork architecture

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

- Interactive diagram: https://gitdiagram.com/max-models/patchwork
- Repository: https://github.com/max-models/patchwork
- Video tour (about a minute): https://gitdiagram.com/max-models/patchwork/video

## Overview

Patchwork is a block-structured adaptive-mesh library for cell- and node-centered fields, with optional MPI distribution. Users construct a hierarchy, allocate patch fields, and can exchange halos, regrid, and plot it. Marker workflows locate and migrate points, deposit charge, accumulate it onto composite nodes, and interpolate fields back. Composite spaces support Poisson, variable-coefficient cell, and curl-curl solves using configurable backends. README documents these capabilities; source excerpts and index references support only some implementation links, so unsampled relationships are omitted.

Main entry points: [`src/patchwork/mesh/hierarchy.py`](https://github.com/max-models/patchwork/blob/devel/src/patchwork/mesh/hierarchy.py) (AMR hierarchy), [`src/patchwork/solvers/nodal_poisson.py`](https://github.com/max-models/patchwork/blob/devel/src/patchwork/solvers/nodal_poisson.py) (Nodal Poisson), [`src/patchwork/spaces/nodes.py`](https://github.com/max-models/patchwork/blob/devel/src/patchwork/spaces/nodes.py) (Composite nodes), [`src/patchwork/solvers/cell_operator.py`](https://github.com/max-models/patchwork/blob/devel/src/patchwork/solvers/cell_operator.py) (Cell operator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_mesh["Mesh and fields"]
  node_hierarchy["AMR hierarchy<br/>[hierarchy.py]"]
  node_halos["Halo exchange<br/>[halos.py]"]
  node_regrid["Tag and regrid<br/>[regrid.py]"]
  node_grading["Grading and tags<br/>[grading.py]"]
  node_accumulate["Halo accumulation<br/>[accumulate.py]"]
  node_reflux["Flux register<br/>[reflux.py]"]
  node_plotting["Patch plotting<br/>[plotting.py]"]
end

subgraph group_spaces["Composite spaces"]
  node_boundaries["Boundary conditions<br/>[boundaries.py]"]
  node_cells["Composite cells<br/>[cells.py]"]
  node_nodes["Composite nodes<br/>[nodes.py]"]
  node_edges["Composite edges<br/>[edges.py]"]
  node_faces["Composite faces<br/>[faces.py]"]
  node_mass["Mass matrices<br/>[mass.py]"]
end

subgraph group_solvers["Composite solvers"]
  node_nodal["Nodal Poisson<br/>[nodal_poisson.py]"]
  node_cell_operator["Cell operator<br/>[cell_operator.py]"]
  node_poisson["Cell Poisson<br/>[poisson.py]"]
  node_curl_curl["Curl-curl solver<br/>[curl_curl.py]"]
  node_backends["Linear backends<br/>[backends.py]"]
  node_multigrid["Geometric multigrid<br/>[multigrid.py]"]
  node_distributed["PETSc system<br/>[distributed.py]"]
end

subgraph group_markers["Marker operations"]
  node_locate["Point location"]
  node_deposit["Node deposition"]
  node_scatter["Block scatter"]
  node_gather["Block gather"]
end

node_user(("Application user"))
node_marker(("Particle markers"))
node_profiling["Profiling tags<br/>[_profiling.py]"]

node_user -->|"constructs"| node_hierarchy
node_hierarchy -->|"exchanges halos"| node_halos
node_user -->|"requests refinement"| node_regrid
node_regrid -->|"builds hierarchy"| node_hierarchy
node_user -->|"plots patches"| node_plotting
node_hierarchy -->|"accumulates halos"| node_accumulate
node_hierarchy -->|"locates points"| node_locate
node_hierarchy -->|"scatters blocks"| node_scatter
node_hierarchy -->|"gathers blocks"| node_gather
node_marker -->|"locates and migrates"| node_hierarchy
node_marker -->|"deposits charge"| node_deposit
node_deposit -->|"weights onto nodes"| node_nodes
node_accumulate -->|"combines copies"| node_nodes
node_nodes -->|"interpolates field"| node_marker
node_user -->|"solves potential"| node_nodal
node_nodal -->|"assembles unknowns"| node_nodes
node_nodal -->|"sets up solver"| node_backends
node_backends -->|"creates PETSc system"| node_distributed
node_backends -->|"builds hierarchy"| node_multigrid
node_user -->|"solves cell system"| node_cell_operator
node_cell_operator -->|"uses leaf cells"| node_cells
node_user -->|"solves Poisson"| node_poisson
node_user -->|"solves curl-curl"| node_curl_curl
node_cell_operator -->|"uses conditions"| node_boundaries
node_nodal -->|"uses conditions"| node_boundaries
node_nodal -->|"uses edge space"| node_edges
node_edges -->|"uses mass blocks"| node_mass
node_nodes -->|"uses mass operators"| node_mass
node_user -->|"tags cells"| node_grading
node_user -.->|"registers flux"| node_reflux
node_profiling -->|"marks operations"| node_hierarchy

click node_hierarchy "https://github.com/max-models/patchwork/blob/devel/src/patchwork/mesh/hierarchy.py"
click node_halos "https://github.com/max-models/patchwork/blob/devel/src/patchwork/mesh/halos.py"
click node_regrid "https://github.com/max-models/patchwork/blob/devel/src/patchwork/mesh/regrid.py"
click node_grading "https://github.com/max-models/patchwork/blob/devel/src/patchwork/mesh/grading.py"
click node_accumulate "https://github.com/max-models/patchwork/blob/devel/src/patchwork/mesh/accumulate.py"
click node_reflux "https://github.com/max-models/patchwork/blob/devel/src/patchwork/mesh/reflux.py"
click node_plotting "https://github.com/max-models/patchwork/blob/devel/src/patchwork/mesh/plotting.py"
click node_boundaries "https://github.com/max-models/patchwork/blob/devel/src/patchwork/spaces/boundaries.py"
click node_cells "https://github.com/max-models/patchwork/blob/devel/src/patchwork/spaces/cells.py"
click node_nodes "https://github.com/max-models/patchwork/blob/devel/src/patchwork/spaces/nodes.py"
click node_edges "https://github.com/max-models/patchwork/blob/devel/src/patchwork/spaces/edges.py"
click node_faces "https://github.com/max-models/patchwork/blob/devel/src/patchwork/spaces/faces.py"
click node_mass "https://github.com/max-models/patchwork/blob/devel/src/patchwork/spaces/mass.py"
click node_nodal "https://github.com/max-models/patchwork/blob/devel/src/patchwork/solvers/nodal_poisson.py"
click node_cell_operator "https://github.com/max-models/patchwork/blob/devel/src/patchwork/solvers/cell_operator.py"
click node_poisson "https://github.com/max-models/patchwork/blob/devel/src/patchwork/solvers/poisson.py"
click node_curl_curl "https://github.com/max-models/patchwork/blob/devel/src/patchwork/solvers/curl_curl.py"
click node_backends "https://github.com/max-models/patchwork/blob/devel/src/patchwork/solvers/backends.py"
click node_multigrid "https://github.com/max-models/patchwork/blob/devel/src/patchwork/solvers/multigrid.py"
click node_distributed "https://github.com/max-models/patchwork/blob/devel/src/patchwork/solvers/distributed.py"
click node_locate "https://github.com/max-models/patchwork/tree/devel/src/patchwork/kernels/locate_points"
click node_deposit "https://github.com/max-models/patchwork/tree/devel/src/patchwork/kernels/deposit_nodes"
click node_scatter "https://github.com/max-models/patchwork/tree/devel/src/patchwork/kernels/scatter_blocks"
click node_gather "https://github.com/max-models/patchwork/tree/devel/src/patchwork/kernels/gather_blocks"
click node_profiling "https://github.com/max-models/patchwork/blob/devel/src/patchwork/_profiling.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_hierarchy,node_halos,node_regrid,node_grading,node_accumulate,node_reflux,node_plotting,node_user toneBlue
class node_boundaries,node_cells,node_nodes,node_edges,node_faces,node_mass toneAmber
class node_nodal,node_cell_operator,node_poisson,node_curl_curl,node_backends,node_multigrid,node_distributed toneMint
class node_locate,node_deposit,node_scatter,node_gather toneRose
class node_marker toneIndigo
class node_profiling toneTeal
```

## Components

### Mesh and fields

- [AMR hierarchy](https://github.com/max-models/patchwork/blob/devel/src/patchwork/mesh/hierarchy.py): `src/patchwork/mesh/hierarchy.py`
- [Halo exchange](https://github.com/max-models/patchwork/blob/devel/src/patchwork/mesh/halos.py): `src/patchwork/mesh/halos.py`
- [Tag and regrid](https://github.com/max-models/patchwork/blob/devel/src/patchwork/mesh/regrid.py): `src/patchwork/mesh/regrid.py`
- [Grading and tags](https://github.com/max-models/patchwork/blob/devel/src/patchwork/mesh/grading.py): `src/patchwork/mesh/grading.py`
- [Halo accumulation](https://github.com/max-models/patchwork/blob/devel/src/patchwork/mesh/accumulate.py): `src/patchwork/mesh/accumulate.py`
- [Flux register](https://github.com/max-models/patchwork/blob/devel/src/patchwork/mesh/reflux.py): `src/patchwork/mesh/reflux.py`
- [Patch plotting](https://github.com/max-models/patchwork/blob/devel/src/patchwork/mesh/plotting.py): `src/patchwork/mesh/plotting.py`

### Composite spaces

- [Boundary conditions](https://github.com/max-models/patchwork/blob/devel/src/patchwork/spaces/boundaries.py): `src/patchwork/spaces/boundaries.py`
- [Composite cells](https://github.com/max-models/patchwork/blob/devel/src/patchwork/spaces/cells.py): `src/patchwork/spaces/cells.py`
- [Composite nodes](https://github.com/max-models/patchwork/blob/devel/src/patchwork/spaces/nodes.py): `src/patchwork/spaces/nodes.py`
- [Composite edges](https://github.com/max-models/patchwork/blob/devel/src/patchwork/spaces/edges.py): `src/patchwork/spaces/edges.py`
- [Composite faces](https://github.com/max-models/patchwork/blob/devel/src/patchwork/spaces/faces.py): `src/patchwork/spaces/faces.py`
- [Mass matrices](https://github.com/max-models/patchwork/blob/devel/src/patchwork/spaces/mass.py): `src/patchwork/spaces/mass.py`

### Composite solvers

- [Nodal Poisson](https://github.com/max-models/patchwork/blob/devel/src/patchwork/solvers/nodal_poisson.py): `src/patchwork/solvers/nodal_poisson.py`
- [Cell operator](https://github.com/max-models/patchwork/blob/devel/src/patchwork/solvers/cell_operator.py): `src/patchwork/solvers/cell_operator.py`
- [Cell Poisson](https://github.com/max-models/patchwork/blob/devel/src/patchwork/solvers/poisson.py): `src/patchwork/solvers/poisson.py`
- [Curl-curl solver](https://github.com/max-models/patchwork/blob/devel/src/patchwork/solvers/curl_curl.py): `src/patchwork/solvers/curl_curl.py`
- [Linear backends](https://github.com/max-models/patchwork/blob/devel/src/patchwork/solvers/backends.py): `src/patchwork/solvers/backends.py`
- [Geometric multigrid](https://github.com/max-models/patchwork/blob/devel/src/patchwork/solvers/multigrid.py): `src/patchwork/solvers/multigrid.py`
- [PETSc system](https://github.com/max-models/patchwork/blob/devel/src/patchwork/solvers/distributed.py): `src/patchwork/solvers/distributed.py`

### Marker operations

- [Point location](https://github.com/max-models/patchwork/tree/devel/src/patchwork/kernels/locate_points): `src/patchwork/kernels/locate_points`
- [Node deposition](https://github.com/max-models/patchwork/tree/devel/src/patchwork/kernels/deposit_nodes): `src/patchwork/kernels/deposit_nodes`
- [Block scatter](https://github.com/max-models/patchwork/tree/devel/src/patchwork/kernels/scatter_blocks): `src/patchwork/kernels/scatter_blocks`
- [Block gather](https://github.com/max-models/patchwork/tree/devel/src/patchwork/kernels/gather_blocks): `src/patchwork/kernels/gather_blocks`

### Other components

- **Application user**
- **Particle markers**
- [Profiling tags](https://github.com/max-models/patchwork/blob/devel/src/patchwork/_profiling.py): `src/patchwork/_profiling.py`

## Connections

- Application user → AMR hierarchy: constructs (evidence: `README.md`)
- AMR hierarchy → Halo exchange: exchanges halos (evidence: `src/patchwork/mesh/hierarchy.py`)
- Application user → Tag and regrid: requests refinement (evidence: `README.md`)
- Tag and regrid → AMR hierarchy: builds hierarchy (evidence: `README.md`)
- Application user → Patch plotting: plots patches (evidence: `README.md`)
- AMR hierarchy → Halo accumulation: accumulates halos (evidence: `src/patchwork/mesh/hierarchy.py`)
- AMR hierarchy → Point location: locates points (evidence: `src/patchwork/mesh/hierarchy.py`)
- AMR hierarchy → Block scatter: scatters blocks (evidence: `src/patchwork/mesh/hierarchy.py`)
- AMR hierarchy → Block gather: gathers blocks (evidence: `src/patchwork/mesh/hierarchy.py`)
- Particle markers → AMR hierarchy: locates and migrates (evidence: `README.md`)
- Particle markers → Node deposition: deposits charge (evidence: `README.md`)
- Node deposition → Composite nodes: weights onto nodes (evidence: `src/patchwork/spaces/nodes.py`)
- Halo accumulation → Composite nodes: combines copies (evidence: `README.md`)
- Composite nodes → Particle markers: interpolates field (evidence: `README.md`)
- Application user → Nodal Poisson: solves potential (evidence: `README.md`)
- Nodal Poisson → Composite nodes: assembles unknowns (evidence: `src/patchwork/solvers/nodal_poisson.py`)
- Nodal Poisson → Linear backends: sets up solver (evidence: `src/patchwork/solvers/nodal_poisson.py`)
- Linear backends → PETSc system: creates PETSc system (evidence: `src/patchwork/solvers/backends.py`)
- Linear backends → Geometric multigrid: builds hierarchy (evidence: `src/patchwork/solvers/backends.py`)
- Application user → Cell operator: solves cell system (evidence: `README.md`)
- Cell operator → Composite cells: uses leaf cells (evidence: `src/patchwork/solvers/cell_operator.py`)
- Application user → Cell Poisson: solves Poisson (evidence: `README.md`)
- Application user → Curl-curl solver: solves curl-curl (evidence: `README.md`)
- Cell operator → Boundary conditions: uses conditions (evidence: `src/patchwork/solvers/cell_operator.py`)
- Nodal Poisson → Boundary conditions: uses conditions (evidence: `src/patchwork/solvers/nodal_poisson.py`)
- Nodal Poisson → Composite edges: uses edge space (evidence: `src/patchwork/solvers/nodal_poisson.py`)
- Composite edges → Mass matrices: uses mass blocks (evidence: `src/patchwork/spaces/edges.py`)
- Composite nodes → Mass matrices: uses mass operators (evidence: `src/patchwork/spaces/nodes.py`)
- Application user → Grading and tags: tags cells (evidence: `README.md`)
- Application user → Flux register: registers flux
- Profiling tags → AMR hierarchy: marks operations (evidence: `src/patchwork/mesh/hierarchy.py`)
