A structural engine in Rust: exact math, one codebase, three targets
Why a bridge analysis engine is written in Rust for native, WASM and Python at once, and why exact methods beat 'fine enough' when engineers have to trust the answer.
· 6 min read · Erik Kubica
In the first post I described the problem: engineers hand-tuning about forty inputs on an unmaintained bridge-checking tool until they find a design that passes and is cheap. The fix is an engine fast enough to search the design space for them, and trustworthy enough that they believe its answers.
This post is about that engine: why it's Rust, what "exact" means in practice, and how open-source solvers help without ending up in the codebase.
Three targets, one engine
The engine has to live in three places:
- An optimiser loop running a very large number of candidate designs, ideally across all cores.
- A browser or desktop UI where an engineer edits a bridge and sees results update live.
- Python, because that's where the optimisation ecosystem (Optuna, pymoo) and the engineers' scripts live.
Rust covers all three from one codebase: native with rayon for the optimiser, WASM for the UI,
PyO3 for Python. No garbage-collector pauses in a tight numeric loop, and no second implementation
to keep in sync.
I seriously considered Go. It's pleasant to write and easy to deploy, but its numerical ecosystem is thin and its WASM output is heavier and clumsier to integrate. Python alone was never an option for the inner loop. It's the right host and the wrong engine.
The workspace is split into small crates by concern: a FEM core, loads, design-code profiles, prestress, transverse analysis, bridge typologies, the optimiser, a WASM wrapper, a CLI, and an adapter for the analysis software the firm already uses. Modules talk through versioned data contracts, never by calling each other's internals.
Keep the core boring: no heavy dependencies
The FEM core has essentially no dependencies. In particular, it uses its own banded Cholesky solver instead of a linear-algebra crate.
That sounds like reinventing the wheel, but a continuous beam's stiffness matrix is symmetric, positive definite and narrow-banded. A banded Cholesky is a few dozen lines, easy to test exhaustively, and compiles to WASM without any C or Fortran toolchain drama. The same reasoning applies to the eigenvalue solver for dynamics: it's small, ours, and portable.
Sign conventions are written once, at the top of the beam module, and never repeated differently
elsewhere: loads down positive, sagging moment positive, deflection down positive, V = dM/dx. Every
interface converts at the edge. Internally everything is SI in f64. Display units (kN, kN·m, MPa, mm)
exist only at input and output.
Exact beats "fine enough"
Most beam codes are "fine enough": mesh the beam finely, sample results at points, move a load in small steps to find the worst position. The results converge, and the tests pass at loose tolerances.
I took the other route wherever the math allows it.
Exact results inside elements. For a prismatic beam element under point loads and uniform or linear loads, the moment, shear and deflection between nodes have closed forms. So the engine doesn't need a fine mesh to get the right value at mid-span. One element per span segment is exact.
Influence lines by the adjoint method. An influence line answers "how does the moment at point C change as a unit load moves across the bridge?". The naive way is one solve per load position: hundreds of solves per effect. The adjoint way (Müller-Breslau, in structural language) is one solve per effect: apply a unit kink or unit slip at C, and the resulting deflected shape is the influence line, in closed form along every element.
Exact envelopes for traffic. Design codes ask for the worst placement of distributed traffic loads. With an exact influence line, "worst" means loading exactly the parts where the influence line has the adverse sign, and integrating the uniform load over those intervals exactly. There's no step size to choose and no peak to miss between samples.
The payoff is speed (fewer solves) and something more important: tolerances in tests can be tight. When results are exact, a mismatch at the 1e-8 level is a bug, not noise.
Open-source solvers: oracles, not dependencies
There are good open-source structural tools. Some are permissively licensed, many are GPL or AGPL. Since this may become a commercial product, the rule is:
- GPL/AGPL tools are run, never copied. A Python script runs them to generate fixtures (inputs and expected outputs as CSV), and the Rust tests compare against those fixtures. The code can be read for ideas. It's never pasted.
- MIT/BSD/Apache code can be adapted, with attribution in the file header.
The interesting part is what happens when we disagree.
A small story about who's right
One cross-check uses a three-span continuous beam (12, 18 and 14 m) with a mix of uniform and point loads, compared against an independent open-source continuous-beam solver.
Moments and shears matched to about 1e-8 relative. Deflections didn't: they were off by a few parts in ten thousand of the peak.
The easy move is to loosen the tolerance and move on. Instead, the question is "who's right, and can we prove it?" The reference solver gets deflection by integrating curvature numerically on a fixed grid. Ours is closed form. So we refined its grid step by step, up to 20,000 points, and watched its deflection converge towards ours.
The test now carries that history in a comment: the tolerance for deflection is set to the reference's known discretisation error, with a note on how it was established. Anyone who later wonders why that one tolerance is looser than the others gets the answer next to the assert.
That's the pattern for every disagreement: a closed-form case or a convergence study decides, and the proof lives in the test.
Design codes are data, not if statements
The structural math is universal. The design codes (Eurocodes, national codes, others) aren't. They differ in load factors, material models, partial safety factors and national choices.
So no check function contains a normative constant. Every factor comes from a code profile through a single lookup, keyed by clause and symbol, and every value carries its provenance. The rules:
- A missing national value → use the recommended value and show a visible warning. Never a silent default.
- An unknown clause → error. Never a guess.
- Every check returns its working: demand, resistance, utilisation, clause, and the intermediate values (formula → substituted numbers → result), so an engineer can follow it like a hand calculation.
- Stresses are summed phase by phase, each on the section that existed in that construction phase. For composite and prestressed decks, getting this wrong is a classic error.
Adding a code family means adding a provider and its data, not touching the core.
What I'd do differently
- Write the sign conventions down before the first line of code. We did, and it's the best decision in the project. I mention it only because I've seen what happens otherwise.
- Start the cross-check fixtures earlier. Each independent solver found something, even if only something about itself.
- Design the data contracts between modules up front. Retrofitting versioned contracts after modules had started reaching into each other cost more than designing them would have.
The engine is the least glamorous part of the project and the part everything else stands on. Exact methods, tight tests, and a written reason for every tolerance: that's what lets an engineer trust the shortlist an optimiser hands them.