Skip to main content

Module finite_differences

Module finite_differences 

Source
Expand description

Finite difference solvers for model partial differential equations.

  • The heat equation u_t = α u_xx on [0, L] with Dirichlet ends: explicit FTCS (stable for r = α Δt / Δx² ≤ ½) and Crank-Nicolson (unconditionally stable, second order in time and space), the latter solving a tridiagonal system per step by the Thomas algorithm.
  • The wave equation u_tt = c² u_xx with fixed ends by the explicit leapfrog scheme (stable for Courant number c Δt / Δx ≤ 1, exact on the grid when it equals one).
  • The Poisson equation −∇²u = f on a rectangle with Dirichlet data, by the five-point stencil solved matrix-free with conjugate gradients.

See the book entry Finite difference methods.

Enums§

HeatScheme
The time-stepping scheme for heat_equation_1d.

Functions§

conjugate_gradient
Conjugate gradients for a symmetric positive-definite operator apply, from zero, to the relative residual tolerance. If max_iterations is reached first, a solution whose residual is within a factor 1000 of the target is still returned (rounding often stalls the last digits); otherwise the result is an error.
heat_equation_1d
Advances u_t = α u_xx on a uniform grid of u0.len() points spanning [0, length] (endpoints included and held at left and right) through steps steps of size dt.
poisson_2d
Solves −∇²u = f on [0, width] × [0, height] with u = g on the boundary, on an nx × ny grid of points including the boundary. Returns u[j][i] at (i Δx, j Δy). The five-point system is symmetric positive definite and is solved by conjugate gradients to a relative residual of 1e-12.
solve_tridiagonal
Solves the tridiagonal system with sub-diagonal a, diagonal b, super-diagonal c (a[0] and c[n−1] unused) by the Thomas algorithm.
wave_equation_1d
Advances u_tt = c² u_xx with fixed ends u = 0 from displacement u0 and velocity v0 on a uniform grid spanning [0, length], by leapfrog with a Taylor first step; returns the displacement after steps steps of size dt.