# Gallery of examples¶

This page contains an overview of the examples contained in the source code repository.

## Poisson equation¶

### Example 1: Poisson equation with unit load¶

This example solves the Poisson problem \(-\Delta u = 1\) with the Dirichlet boundary condition \(u = 0\) in the unit square using piecewise-linear triangular elements.

(Source code, png, hires.png, pdf)

See the source code of Example 1 for more information.

### Example 7: Discontinuous Galerkin method¶

This example solves the Poisson problem \(-\Delta u = 1\) with \(u=0\) on the boundary using discontinuous Galerkin method. The finite element basis is piecewise-quartic but discontinuous over the element edges.

(Source code, png, hires.png, pdf)

See the source code of Example 7 for more information.

### Example 12: Postprocessing¶

This example demonstrates postprocessing the value of a functional, Boussinesq’s k-factor.

(Source code, png, hires.png, pdf)

See the source code of Example 12 for more information.

### Example 13: Laplace with mixed boundary conditions¶

This example solves \(\Delta u = 0\) in \(\Omega=\{(x,y):1<x^2+y^2<4,~0<\theta<\pi/2\}\), where \(\tan \theta = y/x\), with \(u = 0\) on \(y = 0\), \(u = 1\) on \(x = 0\), and \(\frac{\partial u}{\partial n} = 0\) on the rest of the boundary.

(Source code, png, hires.png, pdf)

See the source code of Example 13 for more information.

### Example 14: Laplace with inhomogeneous boundary conditions¶

This example demonstrates how to impose coordinate-dependent Dirichlet conditions for the Laplace equation \(\Delta u = 0\). The solution will satisfy \(u=x^2 - y^2\) on the boundary of the square domain.

(Source code, png, hires.png, pdf)

See the source code of Example 14 for more information.

### Example 15: One-dimensional Poisson equation¶

This example solves \(-u'' = 1\) in \((0,1)\) with the boundary condition \(u(0)=u(1)=0\).

See the source code of Example 15 for more information.

### Example 9: Three-dimensional Poisson equation¶

This example solves \(-\Delta u = 1\) with \(u=0\) on the boundary using linear tetrahedral elements and a preconditioned conjugate gradient method.

See the source code of Example 9 for more information.

### Example 22: Adaptive Poisson equation¶

This example solves Example 1 adaptively in an L-shaped domain. Using linear elements, the error indicators read \(\eta_K^2 = h_K^2 \|f\|_{0,K}^2\) and \(\eta_E^2 = h_E \| [[\nabla u_h \cdot n ]] \|_{0,E}^2\) for each element \(K\) and edge \(E\).

(Source code, png, hires.png, pdf)

See the source code of Example 22 for more information.

### Example 37: Mixed Poisson equation¶

This example solves the mixed formulation of the Poisson equation using the lowest order Raviart-Thomas elements.

See the source code of Example 37 for more information.

### Example 38: Point source¶

Point sources require different assembly to other linear forms.

This example computes the Green’s function for a disk; i.e. the solution of the Dirichlet problem for the Poisson equation with the source term concentrated at a single interior point \(\boldsymbol{s}\), \(-\Delta u = \delta (\boldsymbol{x} - \boldsymbol{s})\).

(Source code, png, hires.png, pdf)

See the source code of Example 38 for more information.

### Example 40: Hybridizable discontinuous Galerkin method¶

This examples solves the Poisson equation with unit load using a technique where the finite element basis is first discontinous across element edges and then the continuity is recovered with the help of Lagrange multipliers defined on the mesh skeleton (i.e. a “skeleton mesh” consisting only of the edges of the original mesh).

(Source code, png, hires.png, pdf)

See the source code of Example 40 for more information.

### Example 41: Mixed meshes¶

This example solves the Poisson equation with unit load on a mesh consisting of both triangles and quadrilaterals. The support for mixed meshes is preliminary and works only for elements with nodal or internal degrees-of-freedom (sharing face and edge DOFs between mesh types is work-in-progress).

(Source code, png, hires.png, pdf)

See the source code of Example 41 for more information.

## Solid mechanics¶

### Example 2: Kirchhoff plate bending problem¶

This example solves the biharmonic Kirchhoff plate bending problem \(D \Delta^2 u = f\) in the unit square with a constant loading \(f\), bending stiffness \(D\) and a combination of clamped, simply supported and free boundary conditions.

(Source code, png, hires.png, pdf)

See the source code of Example 2 for more information.

### Example 3: Linear elastic eigenvalue problem¶

This example solves the linear elastic eigenvalue problem \(\mathrm{div}\,\sigma(u)= \lambda u\) with the displacement fixed on the left boundary.

(Source code, png, hires.png, pdf)

See the source code of Example 3 for more information.

### Example 4: Linearized contact problem¶

This example solves a single interation of the contact problem between two elastic bodies using the Nitsche’s method. Triangular and quadrilateral second-order elements are used in the discretization of the two elastic bodies.

(Source code, png, hires.png, pdf)

See the source code of Example 4 for more information.

### Example 8: Argyris basis functions¶

This example visualizes the \(C^1\)-continuous fifth degree Argyris basis functions on a simple triangular mesh. This element can be used in the conforming discretization of biharmonic problems.

(Source code, png, hires.png, pdf)

See the source code of Example 8 for more information.

### Example 11: Three-dimensional linear elasticity¶

This example solves the three-dimensional linear elasticity equations \(\mathrm{div}\,\sigma(u)=0\) using trilinear hexahedral elements. Dirichlet conditions are set on the opposing faces of a cube: one face remains fixed and the other is displaced slightly outwards.

See the source code of Example 11 for more information.

### Example 21: Structural vibration¶

This example demonstrates the solution of a three-dimensional vector-valued eigenvalue problem by considering the vibration of an elastic structure.

See the source code of Example 21 for more information.

### Example 34: Euler-Bernoulli beam¶

This example solves the Euler-Bernoulli beam equation \((EI u'')'' = 1\) with the boundary conditions \(u(0)=u'(0) = 0\) and using cubic Hermite elements. The exact solution at \(x=1\) is \(u(1)=1/8\).

See the source code of Example 34 for more information.

### Example 36: Nearly incompressible hyperelasticity¶

This example demonstrates the implementation of a two field mixed formulation for nearly incompressible Neo-Hookean solids.

See the source code of Example 36 for more information.

### Example 43: Hyperelasticity¶

This example demonstrates Newton’s method applied to the classical formulation of a hyperelastic Neo-Hookean solid.

See the source code of Example 43 for more information.

## Fluid mechanics¶

### Example 18: Stokes equations¶

This example solves for the creeping flow problem in the primitive variables, i.e. velocity and pressure instead of the stream-function. These are governed by the Stokes momentum \(- \nu\Delta\boldsymbol{u} + \rho^{-1}\nabla p = \boldsymbol{f}\) and the continuity equation \(\nabla\cdot\boldsymbol{u} = 0\).

See the source code of Example 18 for more information.

### Example 20: Creeping flow via stream-function¶

This example solves the creeping flow problem via the stream-function formulation. The stream-function \(\psi\) for two-dimensional creeping flow is governed by the biharmonic equation \(\nu \Delta^2\psi = \mathrm{rot}\,\boldsymbol{f}\) where \(\nu\) is the kinematic viscosity (assumed constant), \(\boldsymbol{f}\) the volumetric body-force, and \(\mathrm{rot}\,\boldsymbol{f} = \partial f_y/\partial x - \partial f_x/\partial y\). The boundary conditions at a wall are that \(\psi\) is constant (the wall is impermeable) and that the normal component of its gradient vanishes (no slip)

See the source code of Example 20 for more information.

### Example 24: Stokes flow with inhomogeneous boundary conditions¶

This example solves the Stokes flow over a backward-facing step with a parabolic velocity profile at the inlet.

See the source code of Example 24 for more information.

### Example 27: Backward-facing step¶

This example uses pacopy 0.1.2 to extend the Stokes equations over a backward-facing step (Example 24) to finite Reynolds number; this means defining a residual for the nonlinear problem and its derivatives with respect to the solution and to the Reynolds number.

Note

This example requires the external package pacopy 0.1.2.

See the source code of Example 27 for more information.

### Example 29: Linear hydrodynamic stability¶

The linear stability of one-dimensional solutions of the Navier-Stokes equations is governed by the Orr-Sommerfeld equation. This is expressed in terms of the stream-function \(\phi\) of the perturbation, giving a two-point boundary value problem \(\alpha\phi(\pm 1) = \phi'(\pm 1) = 0\) for a complex fourth-order ordinary differential equation,

where \(U(z)\) is the base velocity profile, \(c\) and \(\alpha\) are the wavespeed and wavenumber of the disturbance, and \(R\) is the Reynolds number.

See the source code of Example 29 for more information.

### Example 30: Krylov-Uzawa method for the Stokes equation¶

This example solves the Stokes equation iteratively in a square domain.

See the source code of Example 30 for more information.

### Example 32: Block diagonally preconditioned Stokes solver¶

This example solves the Stokes problem in three dimensions, with an algorithm that scales to reasonably fine meshes (a million tetrahedra in a few minutes).

See the source code of Example 32 for more information.

### Example 42: Periodic meshes¶

This example solves the advection equation on a periodic square mesh.

See the source code of Example 42 for more information.

## Heat transfer¶

### Example 17: Insulated wire¶

This example solves the steady heat conduction with generation in an insulated wire. In radial coordinates, the governing equations read: find \(T\) satisfying \(\nabla \cdot (k_0 \nabla T) + A = 0,~0<r<a\), and \(\nabla \cdot (k_1 \nabla T) = 0,~a<r<b\), with the boundary condition \(k_1 \frac{\partial T}{\partial r} + h T = 0\) on \(r=b\).

See the source code of Example 17 for more information.

### Example 19: Heat equation¶

This example solves the heat equation \(\frac{\partial T}{\partial t} = \kappa\Delta T\) in the domain \(|x|<w_0\) and \(|y|<w_1\) with the initial value \(T_0(x,y) = \cos\frac{\pi x}{2w_0}\cos\frac{\pi y}{2w_1}\) using the generalized trapezoidal rule (“theta method”) and fast time-stepping by factorizing the evolution matrix once and for all.

See the source code of Example 19 for more information.

### Example 25: Forced convection¶

This example solves the plane Graetz problem with the governing advection-diffusion equation \(\mathrm{Pe} \;u\frac{\partial T}{\partial x} = \nabla^2 T\) where the velocity profile is \(u (y) = 6 y (1 - y)\) and the Péclet number \(\mathrm{Pe}\) is the mean velocity times the width divided by the thermal diffusivity.

See the source code of Example 25 for more information.

### Example 26: Restricting problem to a subdomain¶

This example extends Example 17 by restricting the solution to a subdomain.

See the source code of Example 26 for more information.

### Example 28: Conjugate heat transfer¶

This example extends Example 25 to conjugate heat transfer by giving a finite thickness and thermal conductivity to one of the walls. The example is modified to a configuration for which there exists a fully developed solution which can be found in closed form: given a uniform heat flux over each of the walls, the temperature field asymptotically is the superposition of a uniform longitudinal gradient and a transverse profile.

Note

This example requires the external package pygmsh.

See the source code of Example 28 for more information.

### Example 39: One-dimensional heat equation¶

This examples reduces the two-dimensional heat equation of Example 19 to demonstrate the special post-processing required.

See the source code of Example 39 for more information.

Electromagnetism

## Miscellaneous¶

### Example 10: Nonlinear minimal surface problem¶

This example solves the nonlinear minimal surface problem \(\nabla \cdot \left(\frac{1}{\sqrt{1 + \|u\|^2}} \nabla u \right)= 0\) with \(u=g\) prescribed on the boundary of the square domain. The nonlinear problem is linearized using the Newton’s method with an analytical Jacobian calculated by hand.

See the source code of Example 10 for more information.

### Example 16: Legendre’s equation¶

This example solves the eigenvalue problem \(((1 - x^2) u')' + k u = 0\) in \((-1,1)\).

See the source code of Example 16 for more information.

### Example 23: Bratu-Gelfand¶

This example solves the Bratu-Gelfand two-point boundary value problem \(u'' + \lambda \mathrm e^u = 0\), \(0 < x < 1\), with \(u(0)=u(1)=0\) and where \(\lambda > 0\) is a parameter.

Note

This example requires the external package pacopy 0.1.2.

See the source code of Example 23 for more information.

### Example 31: Curved elements¶

This example solves the eigenvalue problem \(-\Delta u = \lambda u\) with the boundary condition \(u|_{\partial \Omega} = 0\) using isoparametric mapping via biquadratic basis and finite element approximation using fifth-order quadrilaterals.

See the source code of Example 31 for more information.

### Example 33: H(curl) conforming model problem¶

This example solves the vector-valued problem \(\nabla \times \nabla \times E + E = f\) in domain \(\Omega = [-1, 1]^3\) with the boundary condition \(E \times n|_{\partial \Omega} = 0\) using the lowest order Nédélec edge element.

See the source code of Example 33 for more information.

### Example 35: Characteristic impedance and velocity factor¶

This example solves the series inductance (per meter) and parallel capacitance (per meter) of RG316 coaxial cable. These values are then used to compute the characteristic impedance and velocity factor of the cable.

See the source code of Example 35 for more information.