# jihoonkim888/pytopo3d architecture

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

- Interactive diagram: https://gitdiagram.com/jihoonkim888/pytopo3d
- Repository: https://github.com/jihoonkim888/pytopo3d

## Overview

#### Purpose and Entry Points
PyTopo3D is a Python framework for 3D SIMP structural topology optimization: it iteratively removes material while satisfying a volume fraction constraint and minimizing compliance. Users enter through the CLI launcher `main.py`, its argument layer `pytopo3d/cli/parser.py`, or the package API function `top3d` in `pytopo3d/core/optimizer.py`.

#### Optimization Orchestrator
`top3d` owns the optimization lifecycle: initialize element densities, apply domain/obstacle constraints, repeatedly solve equilibrium, evaluate compliance sensitivities, filter them, and update densities until tolerance or iteration limit. Its output is the optimized density field, stored internally in `(nely, nelx, nelz)` order.
- `pytopo3d/core/optimizer.py`
- `pytopo3d/core/evaluator.py`
- `pytopo3d/core/compliance.py`

#### Finite-Element Analysis Boundary
The physics layer builds the global stiffness system from SIMP-penalized element densities, applies supports and loads, solves nodal displacements, then supplies displacement-derived compliance and gradients to the optimizer. Precomputed assembly indices reduce repeated sparse-matrix setup cost.
- `pytopo3d/utils/assembly.py`
- `pytopo3d/utils/stiffness.py`
- `pytopo3d/utils/solver.py`

The linear-solver boundary prefers PyPardiso when available and otherwise uses SciPy sparse solving. Optional GPU mode uses CuPy/CUDA primarily for solve and filtering workloads.

#### Density Constraints and Updates
Sensitivity filtering regularizes optimization and prevents mesh-scale artifacts. The optimality-criteria update enforces the volume constraint while respecting fixed voids from obstacles or imported design-space masks. Boundary and axis utilities preserve the public right-handed `(x, y, z)` convention despite y-first array storage.
- `pytopo3d/utils/filter.py`
- `pytopo3d/utils/oc_update.py`
- `pytopo3d/utils/boundary.py`
- `pytopo3d/utils/axis_convention.py`

#### Geometry Ingestion and Domain Masks
Preprocessing converts externally defined geometry into optimizer-compatible voxel masks. JSON describes normalized cube, sphere, and cylinder obstacles; STL input defines or inverts a design space at a chosen voxel pitch. These masks form material-exclusion constraints passed into the optimizer.
- `pytopo3d/preprocessing/geometry.py`
- `pytopo3d/utils/obstacles.py`
- `pytopo3d/utils/import_design_space.py`

#### Runs, Results, and Observability
The experiment runner binds parsed inputs to a reproducible run: creates result directories, records parameters and metrics, manages logging, and coordinates optional post-processing. This is the main control boundary between CLI/API invocation and compute stages.
- `pytopo3d/runners/experiment.py`
- `pytopo3d/utils/results_manager.py`
- `pytopo3d/utils/logger.py`
- `pytopo3d/utils/metrics.py`

#### Visualization and Export
Density fields are rendered for interactive display or captured through iterations for animation. Final designs can be converted to STL via contouring/marching-cubes-style export, with optional mesh smoothing; import/export handles the required x/y layout conversion.
- `pytopo3d/visualization/runner.py`
- `pytopo3d/visualization/animation.py`
- `pytopo3d/visualization/visualizer.py`
- `pytopo3d/utils/export.py>

#### Packaging and Delivery
The project is distributed as a Python package with optional GPU dependencies and supports Conda-based environments. CI validates CPU behavior and packaging; publishing is automated separately.
- `pyproject.toml`
- `environment.yml`
- `.github/workflows/ci.yml`
- `.github/workflows/publish.yml`

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry &amp; Runs"]
  node_main["CLI launcher<br/>Python entry point<br/>[main.py]"]
  node_parser["Argument parser<br/>CLI arguments<br/>[parser.py]"]
  node_experiment["Experiment runner<br/>run coordinator<br/>[experiment.py]"]
  node_results["Results &amp; metrics<br/>run observability<br/>[results_manager.py]"]
end

subgraph group_optimization["Optimization Core"]
  node_optimizer{{"top3d optimizer<br/>optimization API<br/>[optimizer.py]"}}
  node_evaluator["Compliance evaluator<br/>iteration evaluator<br/>[evaluator.py]"]
  node_compliance["Compliance sensitivities<br/>objective gradients<br/>[compliance.py]"]
  node_filter["Sensitivity filter<br/>density regularization<br/>[filter.py]"]
  node_ocupdate["Optimality-criteria update<br/>constrained density update<br/>[oc_update.py]"]
end

subgraph group_physics["Finite-Element Analysis"]
  node_assembly["Sparse assembly<br/>stiffness assembly<br/>[assembly.py]"]
  node_stiffness["SIMP stiffness<br/>element stiffness<br/>[stiffness.py]"]
  node_solver["Linear solver<br/>sparse equilibrium solve<br/>[solver.py]"]
end

subgraph group_geometry["Geometry &amp; Output"]
  node_geometry["Obstacle geometry<br/>JSON voxel masks<br/>[geometry.py]"]
  node_designspace["STL design-space import<br/>voxel mask import"]
  node_visualization["Visualization runner<br/>rendering and animation<br/>[runner.py]"]
  node_export["STL export<br/>mesh export"]
end

node_main -->|"parses CLI"| node_parser
node_parser -->|"run inputs"| node_experiment
node_experiment -->|"starts optimization"| node_optimizer
node_experiment -->|"records run"| node_results
node_geometry -->|"obstacle mask"| node_optimizer
node_designspace -->|"design-space mask"| node_optimizer
node_optimizer -->|"densities each iteration"| node_evaluator
node_evaluator -->|"equilibrium system"| node_assembly
node_assembly -->|"SIMP element terms"| node_stiffness
node_assembly -->|"global stiffness and loads"| node_solver
node_solver -->|"nodal displacements"| node_compliance
node_compliance -->|"raw sensitivities"| node_filter
node_filter -->|"filtered sensitivities"| node_ocupdate
node_ocupdate -->|"updated densities"| node_optimizer
node_optimizer -->|"iteration metrics"| node_results
node_optimizer -->|"density fields"| node_visualization
node_optimizer -->|"final density field"| node_export

click node_main "https://github.com/jihoonkim888/pytopo3d/blob/main/main.py"
click node_parser "https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/cli/parser.py"
click node_experiment "https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/runners/experiment.py"
click node_results "https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/utils/results_manager.py"
click node_optimizer "https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/core/optimizer.py"
click node_evaluator "https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/core/evaluator.py"
click node_compliance "https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/core/compliance.py"
click node_filter "https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/utils/filter.py"
click node_ocupdate "https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/utils/oc_update.py"
click node_assembly "https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/utils/assembly.py"
click node_stiffness "https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/utils/stiffness.py"
click node_solver "https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/utils/solver.py"
click node_geometry "https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/preprocessing/geometry.py"
click node_designspace "https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/utils/import_design_space.py"
click node_visualization "https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/visualization/runner.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_main,node_parser,node_experiment,node_results toneBlue
class node_optimizer,node_evaluator,node_compliance,node_filter,node_ocupdate toneAmber
class node_assembly,node_stiffness,node_solver toneMint
class node_geometry,node_designspace,node_visualization,node_export toneRose
```

## Components

### Entry & Runs

- [CLI launcher](https://github.com/jihoonkim888/pytopo3d/blob/main/main.py): Python entry point · `main.py`
- [Argument parser](https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/cli/parser.py): CLI arguments · `pytopo3d/cli/parser.py`
- [Experiment runner](https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/runners/experiment.py): run coordinator · Creates reproducible runs and coordinates compute and post-processing. · `pytopo3d/runners/experiment.py`
- [Results & metrics](https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/utils/results_manager.py): run observability · Manages result directories, logging, parameters, and metrics. · `pytopo3d/utils/results_manager.py`

### Optimization Core

- [top3d optimizer](https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/core/optimizer.py): optimization API · Owns the density-optimization lifecycle. · `pytopo3d/core/optimizer.py`
- [Compliance evaluator](https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/core/evaluator.py): iteration evaluator · Coordinates equilibrium evaluation and sensitivity calculation. · `pytopo3d/core/evaluator.py`
- [Compliance sensitivities](https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/core/compliance.py): objective gradients · `pytopo3d/core/compliance.py`
- [Sensitivity filter](https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/utils/filter.py): density regularization · `pytopo3d/utils/filter.py`
- [Optimality-criteria update](https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/utils/oc_update.py): constrained density update · Enforces volume fraction and fixed voids. · `pytopo3d/utils/oc_update.py`

### Finite-Element Analysis

- [Sparse assembly](https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/utils/assembly.py): stiffness assembly · Uses precomputed indices for repeated global matrix construction. · `pytopo3d/utils/assembly.py`
- [SIMP stiffness](https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/utils/stiffness.py): element stiffness · `pytopo3d/utils/stiffness.py`
- [Linear solver](https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/utils/solver.py): sparse equilibrium solve · Prefers PyPardiso, falls back to SciPy, with optional CuPy/CUDA. · `pytopo3d/utils/solver.py`

### Geometry & Output

- [Obstacle geometry](https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/preprocessing/geometry.py): JSON voxel masks · Converts normalized primitives into material-exclusion masks. · `pytopo3d/preprocessing/geometry.py`
- [STL design-space import](https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/utils/import_design_space.py): voxel mask import · Voxelizes or inverts imported STL design spaces. · `pytopo3d/utils/import_design_space.py`
- [Visualization runner](https://github.com/jihoonkim888/pytopo3d/blob/main/pytopo3d/visualization/runner.py): rendering and animation · Displays density fields and captures iteration frames. · `pytopo3d/visualization/runner.py`
- **STL export**: mesh export · Contours final densities with required axis conversion.

## Connections

- CLI launcher → Argument parser: parses CLI
- Argument parser → Experiment runner: run inputs
- Experiment runner → top3d optimizer: starts optimization
- Experiment runner → Results & metrics: records run
- Obstacle geometry → top3d optimizer: obstacle mask
- STL design-space import → top3d optimizer: design-space mask
- top3d optimizer → Compliance evaluator: densities each iteration
- Compliance evaluator → Sparse assembly: equilibrium system
- Sparse assembly → SIMP stiffness: SIMP element terms
- Sparse assembly → Linear solver: global stiffness and loads
- Linear solver → Compliance sensitivities: nodal displacements
- Compliance sensitivities → Sensitivity filter: raw sensitivities
- Sensitivity filter → Optimality-criteria update: filtered sensitivities
- Optimality-criteria update → top3d optimizer: updated densities
- top3d optimizer → Results & metrics: iteration metrics
- top3d optimizer → Visualization runner: density fields
- top3d optimizer → STL export: final density field
