Truss topology optimization finds the optimal layout of bar elements (trusses) that minimizes compliance (maximizes stiffness) subject to a volume constraint. Unlike continuum topology optimization which uses density-based methods, truss optimization works with discrete bar elements whose cross-sectional areas are the design variables.
This tutorial demonstrates truss optimization using a JSON input file. The truss optimization framework supports:
2D and 3D truss structures
Multiple load cases
Stress constraints (optional)
Direct integration with Nonconvex.jl optimizers
Setup
Load the required packages:
usingTopOpt, LinearAlgebrausingCairoMakie
Precompiling packages...
2633.8 ms ✓ QuartoNotebookWorkerTablesExt (serial)
1 dependency successfully precompiled in 3 seconds
Precompiling packages...
1314.5 ms ✓ QuartoNotebookWorkerJSONExt (serial)
1 dependency successfully precompiled in 1 seconds
Precompiling packages...
1272.0 ms ✓ QuartoNotebookWorkerJSON3Ext (serial)
1 dependency successfully precompiled in 1 seconds
Precompiling packages...
1350.3 ms ✓ QuartoNotebookWorkerLaTeXStringsExt (serial)
1 dependency successfully precompiled in 2 seconds
Precompiling packages...
6814.2 ms ✓ QuartoNotebookWorkerMakieExt (serial)
1 dependency successfully precompiled in 7 seconds
Precompiling packages...
5649.3 ms ✓ QuartoNotebookWorkerCairoMakieExt (serial)
1 dependency successfully precompiled in 6 seconds
Load the truss problem from JSON
Truss problems are defined by node coordinates, element connectivity, material properties, cross-sectional areas, boundary conditions, and load cases. These can be loaded from a JSON file:
ndim =2node_points, elements, mats, crosssecs, fixities, load_cases =load_truss_json("path/to/truss_problem.json")nnodes =length(node_points)ncells =length(elements)loads = load_cases["1"] # first load case
The JSON file contains:
node_points: coordinates of all nodes
elements: connectivity (node indices for each bar)
mats: Young’s modulus for each element
crosssecs: initial cross-sectional areas
fixities: fixed degrees of freedom at nodes
load_cases: named load vectors
Create the TrussProblem
problem =TrussProblem(Val{:Linear}, node_points, elements, loads, fixities, mats, crosssecs)
The Val{:Linear} specifies linear truss elements (axial deformation only). The problem object assembles the global stiffness matrix and applies boundary conditions.
Define design variables and constraints
xmin =0.0001# minimum cross-sectional area (prevents singularity)x0 =fill(1.0, ncells) # initial design (uniform bars)p =4.0# power-law penalty exponentV =0.5# maximum volume fraction (50% of initial material)
The penalty exponent p=4.0 drives the design toward discrete 0/1 solutions (void bars or full-size bars).
The visualization shows the optimized truss layout — bars with larger cross- sectional areas carry more load, while inefficient bars are removed (area → 0).