Lesson 3.13 · III Modelling
Growing: fields and a bone
What it's about: A shape that is not drawn but described: the outline of a thigh bone, the forces while walking, where it is held and how much bone there should be. The inner structure grows by a rule like the one living bone follows. The tools for it are fields, one number per cell of a grid in the plane, and a computation that says where material carries load.


1. Fields
For shapes in the plane there is the value type
field: one number per cell of a grid, such as a
density, a distance or an energy. You compute with fields as with
numbers: d * 2, a + b,
max(d, 0.01), clamp(…), sqrt(…)
work cell by cell.
Isn't that the broadcasting the language deliberately lacks (Lesson
2.1)? No – and the difference is exactly the reason it lacks it: a field
is one value, mathematically a function over the plane.
For two functions, f + g is, in mathematics, the function x
↦ f(x) + g(x); there is no question of "one or many?" and no combination
rules. A list of fields, on the other hand, is not processed
automatically – that is why the model says
fold(r.energy, 0 * d, (+)), the sum over all load cases
(section 3).
Shapes as fields. sdf(curve, grid)
gives, for every cell, the distance to the curve, negative inside (a
signed distance field). Combining shapes then becomes
arithmetic: the minimum of two distance fields is their union. With a
smooth minimum they merge with a fillet – written in
the language itself:
fn smin(a, b, k) = min(a, b) - h * h * k / 4
where h = max(k - abs(a - b), 0) / k
let outline = smin(smin(smin(sdf(shaft, g), sdf(troch, g), 10),
smin(sdf(neck, g), sdf(head, g), 8), 10), sdf(small, g), 6)
For the bone below, shaft, greater trochanter, neck, head and lesser trochanter are described separately like this, the neck at the anatomical angle of 130° to the shaft, and together they make an organic outline.
A field becomes a solid with
plate(field, level:, thickness:) (a plate where the field
is above level) or
relief(field, height:, base:) (a height per cell, Lesson
2.5). More tools: grid (an empty field over a rectangle),
xs/ys (the coordinates of every cell),
step, blur (soften) and mean.
2. What bone does
If you cut a thigh bone open lengthwise, you see a solid shell on the outside (the cortex), an empty marrow cavity in the shaft, and at the top, in the head and neck, a sponge of fine little struts (the cancellous bone, or spongiosa). The struts are not placed at random: they follow the lines along which the bone is pushed and pulled. In the 19th century the engineer Karl Culmann noticed that the stress lines of a crane look just the same; from this the surgeon Julius Wolff formulated Wolff's law in 1892: bone remodels itself so that it follows the loads.
How this works inside the bone is described by the model of Rik Huiskes and colleagues (1987–2000): bone cells sense the deformation around them. If the stimulus is large, bone is added; if it is small, bone is removed. Independently, the engineering world invented the same mathematics – topology optimisation (Bendsøe & Kikuchi 1988; the form common today, "SIMP", from 1989 on). An optimisation step there and a remodelling step in Huiskes' model are almost the same formula.
3. Declarative – and why that fits a functional language
The model examples/bone.kf has two parts. The first
describes the problem and computes nothing:
let shape = step(-outline) // the outline (see below)
let cortex = shape * step(outline + wall) // the cortex is fixed
let supports = shape * step(4 - ys(g)) // clamped at the bottom
let cases = [case(2317, 24, 703, 28), // standing on one leg
case(1158, -15, 351, -8), // abduction
case(1548, 56, 468, 35)] // adduction
The second part is the growth rule, and it is written in the language, not in the Rust core:
fn stimulus(d) = blur(fold(r.energy, 0 * d, (+)) / max(d, 0.01), sense, within: shape)
where r = analyze_plane(d, shape, supports, cases)
fn remodel(d) = max(adapt(d, s, threshold(d, s, 1e-9, 1e9, 60)), cortex)
where s = stimulus(d)
let states = history(start, steps, remodel)
This fits the functional style for three reasons:
- Growth is a sequence of states. Each step is a pure
function from the old density field to the new one;
iterateapplies it n times,historyreturns all the intermediate states (the picture with the five stages is made from them). Some node editors have their own simulation zones for this – the same pattern. - Same description, same bone. Nothing depends on chance or on a clock.
- The rule can be swapped out. If you want to try a
different biology, you change three lines (exercise 2). The core only
supplies the expensive computation:
analyze_plane, a method with finite elements (in depth A). It divides the plate into small cells, works out how they deform under the loads and gives the strain energy per cell: high where the material works, low where it is only carried along.
4. What comes out – and what doesn't
Without any of this being drawn:
- In the head, the struts fan out towards the joint force and bundle together in the neck – like the "compressive trabeculae" in a real femur.
- In the neck and below the greater trochanter, a sponge with arches forms.
- Lower down, the shaft becomes hollow, with a Gothic arch above it.
The shaft is not as empty as in a real bone – further up, a network of struts remains. Less bone overall makes it hollower, but then the rest turns truss-like again. An attempt with Huiskes' "set point" rule (exercise 2) also shifts the balance, but doesn't fully solve it.
5. Limits
- 2D: it is a cross-section, computed as a thin plate. A real bone is three-dimensional; in 3D the system of equations becomes a hundred times larger and needs different solvers (multigrid methods).
- Material: the same in all directions (isotropic). Real struts are themselves directional.
- The cortex is given, not grown.
- Grid: the struts cannot get thinner than about two cells, and a finer grid costs much more time. The outline is smoothed slightly before the plate is built, so that it doesn't follow the grid cells.
In depth A: finite elements in the plane
analyze_plane computes how the plate deforms under the
loads:
- Every cell inside the bone is an element: a square with four corner nodes, and every node can move in x and y.
- Every element has a stiffness matrix (8 × 8), computed by numerical integration. How stiff it is depends on its density: density³ (the "P" in SIMP – half-dense material is disproportionately soft, so the computation settles on either full or empty).
- All elements together give a system of equations K · u = f with about 15,000 unknowns. Because the nodes are numbered row by row, all entries lie close to the diagonal (a band matrix); the Cholesky decomposition for it costs only a fraction of the full one. One decomposition is enough for all three load cases.
- Result: the strain energy in every cell.
The loads come from bone research (Carter et al. 1989, Beaupré et al. 1990): three typical postures, each with the force in the hip joint and the pull of the abductor muscles on the greater trochanter.
In depth B: the remodelling rule in detail
- Stimulus: strain energy per bone mass, summed over
all three postures and averaged over the sensing radius of the bone
cells (
blur– in Huiskes' model the "influence function"). Without this averaging, checkerboard patterns appear. - Remodelling: every cell changes its density by the factor (stimulus / threshold)^speed – by at most 0.2 per step.
- Threshold: chosen so that the amount of bone stays the same – found by bisection, written as a function with two equations (the first one ends the search):
fn threshold(d, s, lo, hi, 0) = sqrt(lo * hi)
fn threshold(d, s, lo, hi, k) =
if mean(adapt(d, s, mid), within: shape) > volume then threshold(d, s, mid, hi, k - 1)
else threshold(d, s, lo, mid, k - 1)
where mid = sqrt(lo * hi)
- Porosity: the decisive building block. Without it you get a truss: a few thick struts, a big X in the shaft – the classic result of topology optimisation, because for a fixed amount of material a few thick bars are stiffest. But bone is porous everywhere. That is why each neighbourhood may contain at most a certain share of bone (after Wu, Aage, Westermann, Sigmund 2017, "approaching bone-like porous structures"). Then the material has to spread over many thin struts:
fn porous(d) = d * min(1, local / max(blur(d, pore, within: shape), 0.01))
One line, and the truss turns into cancellous bone.
How it is checked
The finite element computation is checked against cases whose result is known without it: an element moved or rotated as a whole produces no force; a uniform stretch stores exactly ½ E ε²; the stored energy is exactly half the work done by the loads; a cantilever bends a little less than beam theory says (bilinear square elements are known for being too stiff in bending). The whole model must grow with coarse settings and give a closed plate.
Try it yourself
cargo run --release -p kf -- render examples/bone.kf -o out/bone.png --view -90,89
cargo run --release -p kf -- render examples/bone.kf -D stages=5 -D cell=1.2 -o out/growth.png --view -90,89 --width 1100
cargo run --release -p kf -- render examples/bone.kf -o out/bone.stl
Exercises:
Compute with the one-legged stance only (
cases = [case(2317, 24, 703, 28)]). How does the pattern in the head change? (The crossed arches in a real femur only arise from several postures.)Replace the fixed amount of bone with a set point (Huiskes): the threshold is then not the result of a search but fixed, and the amount of bone adjusts itself:
let setpoint_value = setpoint * mean(stimulus(start), within: shape) fn remodel(d) = max(porous(clamp(d * (stimulus(d) / setpoint_value) ^ speed, max(0.01, d - 0.2), min(1, d + 0.2)) * shape), cortex)(
setpoint_valuehas to come afterlet start = …: values are computed in order, Lesson 2.2.)Change
poreandlocal. When does the cancellous bone fall apart into fragments, and when does it turn back into a truss?Something to think about: why would the computation produce a checkerboard without
blurin the stimulus?