Previous post: Fits and Tolerances for a Triplex Pump Next post: What Shape Cuts Drag? Breeding the Teardrop

Topology Optimization

A live topology optimizer uses Rust and WebAssembly to reshape a 60 × 20 grid, cutting material use while preserving a stiff load path.

Overview

This widget lets you watch an optimization algorithm design a frame. It starts with a 60 × 20 grid of 1,200 square cells, using half the available material. The setup is the half-MBB beam benchmark: the left edge can move vertically but not horizontally, the bottom-right corner rests on a roller, and a downward load acts near the top.

The optimizer gradually removes material from less useful regions and retains it along the paths that carry the load. The goal is to make the structure as stiff as possible while using only half the material.

The result typically forms a strong diagonal load path between the applied load and the supports. The same principle is used in engineering to reduce the weight of brackets and other structural parts without sacrificing too much stiffness.

The solver is written in Rust and compiled to WebAssembly from the separate diolei/topology repository, so it can run directly in the browser without a GPU.

Try It

Find the shape widget on the home page.

  • Load place moves the downward force along the top edge. The slider ranges from 0 to 100 and starts at 0. The load is not fixed at the top-left corner; the slider controls its position. Changes take effect at the next solver step.
  • Pause stops the animation. Reset restarts the optimization from a uniform gray grid.
  • Logs show the iteration, compliance, change in material distribution, load position, and solver state.

Press Reset and watch the uniform grid develop into a frame. Pause at any point to see which cells the optimizer has retained.

The Optimization Problem

The objective is to make the frame bend as little as possible while keeping the amount of material fixed. Each cell has a fill value xe between 0 and 1, where 0 represents a void and 1 represents solid material.

minxe⁡c=𝐮⊤𝐊(x)𝐮subject to1N∑e=1Nxe=v‾,0≤xe≤1

Here, 𝐊(x) is the stiffness matrix of the current design, and 𝐮 is the displacement field obtained by solving 𝐊𝐮=𝐟 for the applied load 𝐟. The value v‾=0.5 fixes the material fraction at 50%.

The objective c is called compliance. Lower compliance means the structure deforms less under the same load, so it is stiffer. Because the total material is fixed, the optimizer must decide where each part of that material will be most useful.

How Material Is Represented

Real structures are made of solid material and empty space, but directly searching all possible solid-or-void arrangements is impractical. Instead, the optimizer lets each cell take any fill value between 0 and 1, then penalizes intermediate values so the final design tends toward solid or empty cells.

The stiffness assigned to each cell is

E(xe)=Emin⁡+xep(E0−Emin⁡),p=3

The full-material stiffness is E0=1. An empty cell retains a tiny stiffness floor, Emin⁡=10−9, to keep the numerical solver stable.

The exponent p=3 makes partly filled cells disproportionately weak. For example, a cell with fill xe=0.5 has only 0.53=0.125, or one-eighth, of the full stiffness, apart from the small floor. This encourages the optimizer to concentrate material in the cells that contribute most to stiffness rather than spreading it evenly.

Solving and Smoothing

The 60 × 20 grid has 1,200 cells and 61 × 21 grid points. Each grid point has two displacement components, giving 2,562 displacement values. The solver assembles the cells into a large, sparse stiffness matrix and solves

𝐊𝐮=𝐟

using an iterative method called preconditioned conjugate gradients. Rather than factorizing the entire matrix, it improves the solution over repeated iterations with the help of a preconditioner.

Each optimization step starts from the previous displacement solution. Because the material distribution changes only a little at each step, the previous solution is already a useful starting point. This warm start reduces the work needed to solve the next system.

The solver also estimates how much each cell contributes to the frame’s deformation. The sensitivity of compliance to a cell’s fill is

∂c∂xe=−p(E0−Emin⁡)xep−1𝐮e⊤𝐊e𝐮e

Here, 𝐊e is the cell’s element stiffness matrix, and 𝐮e contains the displacements at its corners. This sensitivity tells the optimizer how compliance is expected to change if the cell’s fill changes.

Raw sensitivities can produce a checkerboard pattern, where neighboring cells alternate between solid and empty. To prevent this artifact, the solver smooths the sensitivity information over nearby cells using a cone-shaped filter with a radius of 1.5 cells. Closer cells contribute more to the filtered value than farther ones.

Updating the Shape

The optimizer must update all 1,200 cell fills while keeping the total material fraction at 50%. It uses an optimality-criteria update, which increases the fill of cells that are useful for reducing compliance and decreases the fill of less useful cells.

The update is

xenew=max⁡(xe−m,min⁡(xe+m,xe−∂c/∂xeλ))

The move limit is m=0.2, so no cell can change its fill by more than 0.2 in a single step. The parameter λ is chosen by bisection so the updated grid still meets the material constraint.

This keeps each update controlled: the optimizer can redistribute material quickly enough to make progress, but cannot change the entire frame in one step.

The Optimization Loop

Each step rebuilds the stiffness matrix, solves for displacement, evaluates compliance, calculates sensitivities, smooths them, and updates the cell fills.

x = 0.5
for iter in 1..100:
E = Emin + x^p * (E0 - Emin)
K = assemble(E)
u = pcg(K, f, warm_start=u)
ce = element_strain_energy(u)
c = sum(E * ce)
dc = -p * (E0 - Emin) * x^(p-1) * ce
dc = filter_sensitivities(x, dc, radius=1.5)
x_new = oc_update(x, dc, volfrac=0.5, move=0.2)
if max(abs(x_new - x)) < 0.01:
break
x = x_new

The run stops when the largest cell-fill change falls below 0.01 or after 100 iterations. It typically takes around 90–95 iterations to settle. The compliance readout should generally fall as the frame becomes stiffer.

The solver advances one optimization step at a time, allowing the widget to display the shape as it develops. Resizing redraws the current grid, and a small beam sketch indicates the support conditions.

Why Rust and WebAssembly?

The expensive work is in repeatedly assembling the stiffness matrix and solving the sparse system. These operations involve many numerical loops, so Rust compiled to WebAssembly is a better fit than implementing the solver entirely in TypeScript.

The solver and its small JavaScript interface add about 50 KB. The site loads the WebAssembly module when the widget approaches the visible area rather than fetching it immediately on page load.

The trade-off is an initial wait for that download. On the 60 × 20 grid, a full optimization step takes roughly 150 ms when the displacement solve is allowed to converge. The animation therefore runs at a few frames per second while the shape settles over about 15 seconds. These are approximate timings and can vary with the browser and build.

Component Details
Grid 60 × 20 cells; 1,200 cells and 2,562 displacement values
Material model SIMP interpolation with penalty exponent p=3
Solver Iterative sparse solve with a warm start between steps
Smoothing Cone filter with a 1.5-cell radius
Update Optimality-criteria method; material fraction 0.5; move limit 0.2
Stop condition Maximum fill change below 0.01 or 100 iterations; commonly 90–95
Rendering Canvas 2D, enlarged grid; black for solid material and white for void

The grid dimensions and visible controls can be checked against the TypeScript and tests. Timing, binary size, and iteration counts depend on the WebAssembly solver and current build, so treat them as approximate rather than guaranteed values.

Limitations

This is a 2D topology-optimization demonstration using a 60 × 20 grid, a simplified plane-stress model, SIMP material penalization, and sensitivity filtering. The solver runs on a single thread in WebAssembly and requires an initial download.

The resulting frame is optimized for stiffness under the specified load and support conditions, with half the grid’s material retained. It is not a finished, manufacturing-ready component: real parts also require checks for strength, buckling, fatigue, manufacturing constraints, and other load cases.

References

  • Andreassen, Erik, Anders Clausen, Mattias Schevenels, Boyan S. Lazarov, and Ole Sigmund. 2011. “Efficient Topology Optimization in MATLAB Using 88 Lines of Code.” Structural and Multidisciplinary Optimization 43: 1–16. https://doi.org/10.1007/s00158-010-0594-7
  • Bendsøe, Martin P., and Ole Sigmund. 2003. Topology Optimization: Theory, Methods, and Applications. Springer.
  • Sigmund, Ole. 2001. “A 99 Line Topology Optimization Code Written in MATLAB.” Structural and Multidisciplinary Optimization 21: 120–127. https://doi.org/10.1007/s001580050176
  • Wikimedia Foundation. 2026. “Topology Optimization.” Accessed October 5, 2026. https://en.wikipedia.org/wiki/Topology_optimization