Projection and interpolation#

In the introduction, we considered the projection problem \(u=\frac{f}{g}\) where \(f\) and \(g\) were some known functions. However, there are many cases where derived quantities are of interest, including stress calculations such as von Mises stresses.

There are many ways of computing such quantities, and we have already covered how to set up a projection scheme Find \(u\in V_h\) such that

(13)#\[\begin{align} \int_\Omega u\cdot v~\mathrm{d}x = \int_\Omega h(g,f,x)\cdot v~\mathrm{d}x\qquad \forall v \in V_h. \end{align}\]

where \(g\) and \(f\) are functions from some finite element space.

Reusable projector#

Imagine we want to solve a sequence of such post-processing steps for functions \(f\) and \(g\). If the mesh isn’t changing between each projection, the left hand side is constant. Thus, it would make sense to only assemble the matrix once. Following this, we create a general projector class

import pyvista
import numpy as np
import dolfinx
from mpi4py import MPI
import ufl
from petsc4py import PETSc
from typing import Optional
class Projector():
    Projector for a given function.
    Solves Ax=b, where

    .. highlight:: python
    .. code-block:: python

        u, v = ufl.TrialFunction(Space), ufl.TestFunction(space)
        dx = ufl.Measure("dx", metadata=metadata)
        A = inner(u, v) * dx
        b = inner(function, v) * dx(metadata=metadata)

        function: UFL expression of function to project
        space: Space to project function into
        petsc_options: Options to pass to PETSc
        jit_options: Options to pass to just in time compiler
        form_compiler_options: Options to pass to the form compiler
        metadata: Data to pass to the integration measure

    _A: PETSc.Mat  # The mass matrix
    _b: PETSc.Vec  # The rhs vector
    _lhs: dolfinx.fem.FormMetaClass  # The compiled form for the mass matrix
    _ksp: PETSc.KSP  # The PETSc solver
    _x: dolfinx.fem.Function  # The solution vector
    _dx: ufl.Measure  # Integration measure

    def __init__(self,
                 space: dolfinx.fem.FunctionSpace,
                 petsc_options: Optional[dict] = None,
                 jit_options: Optional[dict] = None,
                 form_compiler_options: Optional[dict] = None,
                 metadata: Optional[dict] = None):
        petsc_options = {} if petsc_options is None else petsc_options
        jit_options = {} if jit_options is None else jit_options
        form_compiler_options = {} if form_compiler_options is None else form_compiler_options

        # Assemble projection matrix once
        u = ufl.TrialFunction(space)
        v = ufl.TestFunction(space)
        self._dx = ufl.Measure("dx", domain=space.mesh, metadata=metadata)
        a = ufl.inner(u, v) * self._dx(metadata=metadata)
        self._lhs = dolfinx.fem.form(a, jit_options=jit_options,
        self._A = dolfinx.fem.petsc.assemble_matrix(self._lhs)

        # Create vectors to store right hand side and the solution
        self._x = dolfinx.fem.Function(space)
        self._b = dolfinx.fem.Function(space)

        # Create Krylov Subspace solver
        self._ksp = PETSc.KSP().create(space.mesh.comm)

        # Set PETSc options
        prefix = f"projector_{id(self)}"
        opts = PETSc.Options()
        for k, v in petsc_options.items():
            opts[k] = v

        # Set matrix and vector PETSc options

    def reassemble_lhs(self):
        dolfinx.fem.petsc.assemble_matrix(self._A, self._lhs)

    def assemble_rhs(self, h: ufl.core.expr.Expr):
        Assemble the right hand side of the problem
        v = ufl.TestFunction(self._b.function_space)
        rhs = ufl.inner(h, v) * self._dx
        rhs_compiled = dolfinx.fem.form(rhs)
        self._b.x.array[:] = 0.
        dolfinx.fem.petsc.assemble_vector(self._b.vector, rhs_compiled)

    def project(self, h: ufl.core.expr.Expr):
        Compute projection using a PETSc KSP solver

            assemble_rhs: Re-assemble RHS and re-apply boundary conditions if true
        self._ksp.solve(self._b.vector, self._x.vector)
        return self._x

With this class, we can send in any expression written in the unified form language to the projector, and then generate code for the right hand side assembly, and then solve the linear system. If we use LU factorization, most of the cost will be in the first projection, when the matrix is factorized. This is then cached, so that the solution algorithm is reduced to solving to linear problems; one upper diagonal matrix and one lower diagonal matrix.

Approximation with continuous and discontinuous finite elements#

We will try to approximate the following function

$h(x) = \begin{cases} \cos(\pi x) \quad\text{if } x<\frac{\pi}{5}\ -\sin(x) \quad\text{otherwise} \end{cases} We will use ufl.conditional as explained in the the previous section.

def h(mesh: dolfinx.mesh.Mesh):
    x = ufl.SpatialCoordinate(mesh)
    return ufl.conditional([0], 0.1*ufl.pi), ufl.cos(ufl.pi*x[0]), -ufl.sin(x[0]))

Interpolation of functions and UFL-expressions#

Above we have defined a function that is dependent on the spatial coordinates of ufl, and it is a purely symbolic expression. If we want to evaluate this expression, either at a given point or interpolate it into a function space, we need to compile code similar to the code generated with dolfinx.fem.form or calling FFCx. The main difference is that for an expression, there is no summation over quadrature. To perform this compilation for a given point in the reference cell, we call

mesh = dolfinx.mesh.create_unit_interval(MPI.COMM_WORLD, 20)
compiled_h = dolfinx.fem.Expression(h(mesh), np.array([0.5]))

We can now evaluate the expression at the point 0.5 in the reference element for any cell (this coordinate is then pushed forward to the given input cell). For instance, we can evaluate this expression in the cell with index 0 with


We can also interpolate functions from an expression into any suitable function space by calling

V = dolfinx.fem.FunctionSpace(mesh, ("Discontinuous Lagrange", 2))
compiled_h_for_V = dolfinx.fem.Expression(
    h(mesh), V.element.interpolation_points())

Comparison of continuous and discontinuous Lagrange#

Let us now test the code for a first order Lagrange space

mesh = dolfinx.mesh.create_unit_interval(MPI.COMM_WORLD, 20)
V = dolfinx.fem.FunctionSpace(mesh, ("Lagrange", 1))
V_projector = Projector(
    V, petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
uh = V_projector.project(h(V.mesh))

Next, we plot the solution

def plot_1D_scalar_function(u: dolfinx.fem.Function, title: str):
    u_grid = pyvista.UnstructuredGrid(
    u_grid.point_data["u"] = u.x.array
    warped = u_grid.warp_by_scalar()
    plotter = pyvista.Plotter()
    plotter.add_title(title, font_size=12)
    plotter.add_mesh(warped, style="wireframe")
plot_1D_scalar_function(uh, "First order Lagrange")

We can now repeat the study for a DG-1 function

W = dolfinx.fem.FunctionSpace(mesh, ("DG", 1))
W_projector = Projector(
    W, petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
wh = W_projector.project(h(W.mesh))
plot_1D_scalar_function(wh, "First order Discontinuous Lagrange")

We observe that both solutions overshoot and undershoot around the discontinuity Let us try this for a variety of mesh sizes

for N in [50, 100, 200]:
    mesh = dolfinx.mesh.create_unit_interval(MPI.COMM_WORLD, N)
    V = dolfinx.fem.FunctionSpace(mesh, ("Lagrange", 1))
    W = dolfinx.fem.FunctionSpace(mesh, ("DG", 1))
    V_projector = Projector(
        V, petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
    uh = V_projector.project(h(V.mesh))
    W_projector = Projector(
        W, petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
    wh = W_projector.project(h(W.mesh))
    # Lagrange
    plot_1D_scalar_function(uh, f"First order Lagrange ({N})")

    # Discontinuous Lagrange
    plot_1D_scalar_function(wh, f"First order discontinuous Lagrange ({N})")
../_images/b638d5566108f8345085f5ef99eb88bb7a7394eab1ae188ed99e3d4f3d880d6c.png ../_images/df68dd23b7080c6ad6f24a066fe226d2cab294cda58d54b17a86be6eed427734.png ../_images/feb5adec0e2c77f70b989a637880f0ad872b618c926d8f815ba047c93c195a0e.png ../_images/69209ff7709d584044a394c030e9cd0d0db669d424d9d140ed3b0741b28dfc83.png ../_images/a8042b58d44dee99bb0b1be48f14d51d96ee8ff366856206d9c1de25fcf2edcb.png ../_images/241a39cc2027e5b9f87f6bbca9b51f7a677f0a7bf3b4573341025b415ba31905.png

We still see overshoots with either space. This is known as Gibbs phenomenom and is discussed in detail in [Zha22]. However, if we align the discontinuity with the mesh, we will observe something interesting

def h_aligned(mesh: dolfinx.mesh.Mesh):
    x = ufl.SpatialCoordinate(mesh)
    return ufl.conditional([0], 0.2), ufl.cos(ufl.pi*x[0]), -ufl.sin(x[0]))
for N in [20, 40, 80]:
    mesh = dolfinx.mesh.create_unit_interval(MPI.COMM_WORLD, N)
    V = dolfinx.fem.FunctionSpace(mesh, ("Lagrange", 1))
    W = dolfinx.fem.FunctionSpace(mesh, ("DG", 1))
    V_projector = Projector(
        V, petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
    uh = V_projector.project(h_aligned(V.mesh))
    W_projector = Projector(
        W, petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
    wh = W_projector.project(h_aligned(W.mesh))

    # Lagrange
    plot_1D_scalar_function(uh, f"Aligned first order Lagrange ({N})")

    # Discontinuous Lagrange
        wh, f"Aligned first order discontinuous Lagrange ({N})")
../_images/a39d1d3127dd91041947b9237c4d0536ea8a818fc455c7f423604927dc2af858.png ../_images/0d2eddaac633c13929577e228da96ee2ec546ff364501d7c42bdba2ea7d1b218.png ../_images/159baf5f6d9be7c806d611d201df88350882470c7421e878497ef46ffa05af3e.png ../_images/6a1fccfa038f7d702567eb3fd98324280b26bfb6b2000f6fad25e46c6764daeb.png ../_images/a7c11ecb89665eaec1542a2181b03912e9673361bd4b76aea753b72b04f13f22.png ../_images/238952acec51b41787858fedcd6e5a02856b1ca8af68249a8e137b7786ab184b.png


Von Mises stresses#

When working with linear elasticity, it is often common to consider Von Mises stresses, defined as:

(14)#\[\begin{align} \sigma_m &= \sqrt{\frac{3}{2}s:s}\\ s(u)&=\sigma(u) -\frac{1}{3}\mathrm{tr}(\sigma(u))I \end{align}\]
  1. Implement a simple linear elastic beam (2D), where the right hand side is fixed to the wall and the beam is deformed in y-direction by gravity.

  2. Compute the von Mises stresses with a projector

Discussion exercises#

  1. If we choose displacement in a first order continuous Lagrange space, what space should we place \(\sigma_s\) in?

  2. Can we use interpolation to compute Von Mises stresses in a continuous Lagrange space?

  3. Can we use interpolation to compute Von Mises stresses in a discontinuous Lagrange space?

Further implementation exercises#

  1. Implement the suitable options derived from 3.-5.



Shun Zhang. Battling Gibbs phenomenon: On finite element approximations of discontinuous solutions of PDEs. Computers & Mathematics with Applications, 122:35–47, 2022. doi:10.1016/j.camwa.2022.07.014.