Finite element methods
| Problem | Solve elliptic boundary value problems such as in one dimension and on regions of the plane, including variable and discontinuous coefficients and irregular domains |
| Devised by | Variational methods: Walther Ritz (1909), Boris Galerkin (1915); piecewise-linear elements: Richard Courant (1943); engineering development: Turner, Clough, Martin and Topp (1956), Argyris (1954–55), Clough named the method (1960); mathematical theory: Strang and Fix (1973) |
| Accuracy | Linear elements: second order in the solution values, first order in the gradient (the energy norm) |
| Cost | Assembly ; a sparse symmetric positive-definite system, solved here by a tridiagonal solver (1D) or preconditioned conjugate gradients (2D) |
| Read first | Finite difference methods |
| API | fem_1d, EndCondition, fem_poisson_2d, TriangleMesh |
Overview
Finite differences approximate the equation: derivatives become difference quotients on a grid. Finite elements approximate the solution: it is sought among simple functions, typically piecewise polynomials on a mesh of intervals or triangles, and the equation is required to hold in an averaged (“weak”) sense. The weak form needs only first derivatives, handles coefficients that jump from one material to another without special treatment, imposes flux boundary conditions naturally, and works on any mesh, so curved and irregular domains pose no difficulty.
These properties made the finite element method the standard tool of structural engineering, heat transfer, electromagnetics and many other fields. The library implements the simplest and most widely used version, linear (P1) elements, in one dimension and on triangle meshes in two.
Where it is used
The finite element method is the standard tool for engineering analysis of real shapes.
- Structural mechanics. Stress and deflection of bridges, aircraft parts, buildings and machine components.
- Heat, fluids and electromagnetics. Cooling of electronics, flow through pipes and around bodies, and fields in motors and transformers.
- Biomedical engineering. Stresses in bones and implants, and blood flow in vessels.
- Why it matters here. The one-dimensional version in this entry shows the whole idea (weak form, basis functions, assembly of a sparse matrix) that commercial packages apply in 3-D.
History
The mathematical roots are variational. Walther Ritz (1909) approximated the solution of a minimisation problem by a combination of a few chosen functions, and Boris Galerkin (1915), an engineer who had been imprisoned for revolutionary activity, generalised the idea to equations that are not minimisation problems. In a 1943 lecture on torsion Richard Courant used piecewise-linear functions on triangles, the essential ingredient of the modern method, but the idea lay dormant.
It was reinvented by engineers analysing aircraft structures. John Argyris in London (1954–55) and the Boeing team of Jon Turner, Ray Clough, Harold Martin and LeRoy Topp (1956) treated a structure as an assembly of small elements whose stiffness could be computed individually. Clough named it the “finite element method” in 1960. Mathematicians then recognised the Ritz-Galerkin method in the engineers’ work, and Gilbert Strang and George Fix’s An Analysis of the Finite Element Method (1973) established its convergence theory.
The idea
The weak form
Take the one-dimensional problem on . Multiply by a test function that vanishes where is prescribed, and integrate over the interval: . The first term contains a second derivative of . Integrating it by parts moves one derivative onto , , which gives
This weak form asks for less than the original equation: needs only one derivative, and that derivative may jump, which is exactly what happens at the interface between two materials. The boundary term carries the flux , so a prescribed flux enters the right-hand side directly; such natural conditions need no special treatment. Values of , by contrast, are essential conditions imposed on the space of functions.
Galerkin’s method with hat functions
Choose a mesh and the hat functions , piecewise linear with if and otherwise. Seek , and require the weak form for every . The result is a linear system
The stiffness matrix is symmetric and, since each hat function overlaps only its neighbours, tridiagonal. It is assembled element by element: each interval contributes a block computed by Gauss quadrature, added into the global matrix.
Galerkin orthogonality and Céa’s lemma. The error is orthogonal to every function in the finite element space in the energy inner product . Consequently is the best approximation to in the energy norm : .
Approximation theory then gives the rates: the best piecewise-linear approximation has gradient error and value error . For :
Table 2.28. Convergence of linear finite elements in one dimension, at nodes and midpoints.
| elements | error at the nodes | error at element midpoints | midpoint ratio | |
|---|---|---|---|---|
| 4 | 1/4 | 4.88e-3 | 6.62e-2 | |
| 8 | 1/8 | 1.19e-3 | 1.77e-2 | 3.74 |
| 16 | 1/16 | 2.96e-4 | 4.50e-3 | 3.93 |
| 32 | 1/32 | 7.39e-5 | 1.13e-3 | 3.98 |
| 64 | 1/64 | 1.85e-5 | 2.83e-4 | 4.00 |
| 128 | 1/128 | 4.62e-6 | 7.07e-5 | 4.00 |
The error at the element midpoints falls by four per refinement. The nodal values converge at the same rate but are about fifteen times more accurate; with they would be exact, as the next section shows.
Discontinuous coefficients
In one dimension with , linear finite elements reproduce the exact solution at the nodes, whatever is, as long as the load is integrated accurately. (The Green’s function of the problem is piecewise linear in the right variable, so it lies in the finite element space.) This makes the method ideal for layered materials:
Figure 2.29. A layered rod.
The conductivity jumps from 1 to 10 at , the slope of jumps by the same factor, and the finite element values sit exactly on the true solution. A finite difference scheme with a naively averaged coefficient loses accuracy at the interface.
Triangles in two dimensions
On a triangle mesh the hat functions are the piecewise-linear functions equal to 1 at one node and 0 at all others; on each triangle they are the barycentric coordinates. Their gradients are constant on each triangle, so the element stiffness matrix of is simply
with evaluated at the centroid and the triangle’s area. The load is integrated with the three-edge-midpoint rule, exact for quadratics. The global matrix is sparse (about seven nonzeros per row on a regular mesh) and, after the prescribed values are eliminated, symmetric positive definite; the library solves it by conjugate gradients with a diagonal (Jacobi) preconditioner. For on the unit square:
Table 2.29. Convergence of linear finite elements on a square.
| mesh | triangles | maximum nodal error | ratio |
|---|---|---|---|
| 8 × 8 | 128 | 1.29e-2 | |
| 16 × 16 | 512 | 3.21e-3 | 4.01 |
| 32 × 32 | 2048 | 8.03e-4 | 4.00 |
| 64 × 64 | 8192 | 2.01e-4 | 4.00 |
Algorithm
One dimension: for each element, compute the stiffness and mass contributions and the load by two-point Gauss quadrature; add them into the tridiagonal matrix and load vector; add prescribed fluxes to the end loads; replace the rows of Dirichlet ends by value (moving the known column to the right-hand side); solve with the Thomas algorithm.
Two dimensions: for each triangle, compute the barycentric gradients and area, add the stiffness block and the load into sparse rows; subtract the contributions of the Dirichlet nodes from the right-hand side; solve the reduced system by Jacobi-preconditioned conjugate gradients.
Implementation
for t in &mesh.triangles {
let [p0, p1, p2] = [mesh.nodes[t[0]], mesh.nodes[t[1]], mesh.nodes[t[2]]];
let area2 = (p1[0] - p0[0]) * (p2[1] - p0[1]) - (p2[0] - p0[0]) * (p1[1] - p0[1]);
if area2.abs() < 1e-300 {
return Err(ErrorsJSL::InvalidInputRange("Degenerate triangle in the mesh."));
}
let area = 0.5 * area2.abs();
// Gradients of the barycentric basis functions.
let grad = [
[(p1[1] - p2[1]) / area2, (p2[0] - p1[0]) / area2],
[(p2[1] - p0[1]) / area2, (p0[0] - p2[0]) / area2],
[(p0[1] - p1[1]) / area2, (p1[0] - p0[0]) / area2],
];
let centroid = [(p0[0] + p1[0] + p2[0]) / 3.0, (p0[1] + p1[1] + p2[1]) / 3.0];
let kc = k(centroid[0], centroid[1]);
// Load by the three-edge-midpoint rule (exact for quadratics).
let mids = [[(p0[0] + p1[0]) / 2.0, (p0[1] + p1[1]) / 2.0], [(p1[0] + p2[0]) / 2.0, (p1[1] + p2[1]) / 2.0], [(p2[0] + p0[0]) / 2.0, (p2[1] + p0[1]) / 2.0]];
let fm: Vec<f64> = mids.iter().map(|m| f(m[0], m[1])).collect();
// φ_i at the midpoints: ½ on the two edges touching node i, 0 on the opposite edge.
let phi_at_mid = [[0.5, 0.0, 0.5], [0.5, 0.5, 0.0], [0.0, 0.5, 0.5]];
for a in 0..3 {
load[t[a]] += area / 3.0 * (0..3).map(|m| fm[m] * phi_at_mid[a][m]).sum::<f64>();
for b in 0..3 {
*rows[t[a]].entry(t[b]).or_insert(0.0) += kc * area * (grad[a][0] * grad[b][0] + grad[a][1] * grad[b][1]);
}
}
}
- The sparse rows are stored as ordered maps during assembly and converted to compressed rows for the solver.
- Nodes that belong to no triangle must be listed among the Dirichlet nodes, or the system is singular.
- Natural (zero-flux) conditions hold automatically on every boundary node that is not prescribed.
In practice
//! Finite elements in one and two dimensions.
//!
//! Run with `cargo run --release -p lib_jsl_integration --example pde_finite_elements`.
use lib_jsl_integration::pde::finite_elements::{EndCondition, TriangleMesh, fem_1d, fem_poisson_2d};
fn main() {
// A cooling fin: −u'' + 4u = 0 with the base held at 100 and an insulated tip (zero flux).
// Exact: u = 100 cosh(2(1 − x)) / cosh 2.
let nodes: Vec<f64> = (0..=20).map(|i| i as f64 / 20.0).collect();
let u = fem_1d(|_| 1.0, |_| 4.0, |_| 0.0, &nodes, EndCondition::Dirichlet(100.0), EndCondition::Flux(0.0)).unwrap();
let tip_exact = 100.0 / 2f64.cosh();
println!("fin tip temperature: {:.4} (exact {tip_exact:.4})", u[20]);
// A loaded square membrane: −∇²u = 1, u = 0 on the edges, on 2 × 32² triangles.
let mesh = TriangleMesh::rectangle(1.0, 1.0, 32, 32);
let fixed = mesh.box_boundary_nodes();
let u = fem_poisson_2d(&mesh, |_, _| 1.0, |_, _| 1.0, |_, _| 0.0, &fixed).unwrap();
let centre = mesh.nodes.iter().position(|p| (p[0] - 0.5).abs() < 1e-12 && (p[1] - 0.5).abs() < 1e-12).unwrap();
println!("membrane deflection at the centre: {:.6} (series value 0.0736713...)", u[centre]);
// The same membrane with a stiffer right half: k = 1 for x < ½ and 5 beyond.
let u = fem_poisson_2d(&mesh, |x, _| if x < 0.5 { 1.0 } else { 5.0 }, |_, _| 1.0, |_, _| 0.0, &fixed).unwrap();
println!("with a stiff right half: centre deflection {:.6}", u[centre]);
}
Strengths
- Any mesh, so irregular domains are no harder than squares.
- Variable and discontinuous coefficients and flux conditions are handled naturally.
- A best-approximation property and a complete error theory.
Weaknesses
- Mesh generation for complicated regions is a substantial task in its own right (the library only builds rectangles).
- Linear elements are second order; smooth problems on simple domains are solved far more accurately by spectral methods.
- Gradients (fluxes, stresses) are only first-order accurate and discontinuous between elements.
Exercises
- Compute the element stiffness matrix of on an interval of length , and show that assembling it on a uniform mesh gives times the familiar finite difference matrix.
- Show that for on a uniform mesh with integrated exactly, the nodal values are exact. (Use the Green’s function , which is piecewise linear in .)
- Use
fem_1dwith a mesh that is fine near to solve , , for . How should the nodes be placed? - Build an L-shaped domain by removing the triangles of one quadrant from a rectangle mesh, and solve with on the boundary. Estimate the convergence rate by comparing successive refinements. Why is it slower than on the square?
- Prove Galerkin orthogonality, for all , from the weak forms satisfied by and .
Hints and answers
- For linear elements the stiffness matrix of an interval of length is . Adding the overlapping contributions at an interior node gives .
- The inverse of the stiffness matrix has entries (the Green’s function sampled at the nodes), and the load vector is . The exact solution is ; since is piecewise linear, it equals , so .
- The solution has boundary layers of width about at both ends, because at and . Put several nodes within a few multiples of at each end (a geometric grading) and few in the middle.
- The re-entrant corner of the L produces a singularity , so the solution is not smooth enough for the usual rate: the energy-norm error falls like instead of .
- For , and . Subtracting gives .
Further reading
- R. Courant, “Variational methods for the solution of problems of equilibrium and vibrations”, Bulletin of the American Mathematical Society 49 (1943) 1–23.
- M. J. Turner, R. W. Clough, H. C. Martin and L. J. Topp, “Stiffness and deflection analysis of complex structures”, Journal of the Aeronautical Sciences 23 (1956) 805–823.
- G. Strang and G. J. Fix, An Analysis of the Finite Element Method, Prentice-Hall, 1973.
- C. Johnson, Numerical Solution of Partial Differential Equations by the Finite Element Method, Cambridge University Press, 1987. A clear introduction for students.
- S. C. Brenner and L. R. Scott, The Mathematical Theory of Finite Element Methods, 3rd ed., Springer, 2008.