261 : Axisymmetric Stokes with Weighted Reconstruction

(source code)

This example solves the axisymmetric Stokes problem with a manufactured solution whose pressure behaves like $r^s$ and thus has weak radial regularity. For the derivation of the axisymmetric formulation, which seeks a velocity $\mathbf{u} = (u_r, u_z)$ and a pressure $p$ on a 2D domain that is rotated around the axis $r = 0$, we refer to Example260. The weak formulation reads

\[\begin{aligned} a(\mathbf{u},\mathbf{v}) - b(p,\mathbf{v}) & = (\mathbf{f},\mathbf{v}) \\ b(q,\mathbf{u}) & = 0 \end{aligned}\]

with the bilinear forms

\[\begin{aligned} a(\mathbf{u},\mathbf{v}) := \int_{\Omega} \left( \nabla \mathbf{u} : \nabla \mathbf{v} + r^{-2} u_r v_r \right) r dr dz\\ b(q,\mathbf{v}) := \int_{\Omega} q \left( \mathrm{div}(\mathbf{v}) + r^{-1} v_r \right) r dr dz \end{aligned}\]

where $r$ is the distance to the rotation axis, the usual Cartesian differential operators can be used, and the factor $2\pi$ from the integral over the rotation angle drops out. Note that the incompressibility constraint means that the $r$-weighted velocity is divergence-free in the classical sense, $\mathrm{div}(r \mathbf{u}) = 0$. Consequently, traditional divergence-free finite element methods from the Cartesian setting – even if inf-sup stable – are in general not divergence-free in the axisymmetric formulation.

The weighted $r$ motivates a weighted reconstruction operator $\Pi : H1BR\{2\} \to \mathrm{BDM1}(H(\mathrm{div}))$, which matches $r$-weighted normal and tangential moment integrals at faces. Applied to the load term, it renders the right-hand side discretely $r$-divergence-free, and hence makes the discretization and the error estimation pressure-robust. However, standard interpolation operators from the Cartesian setting lack properties that are needed to derive optimal consistency error estimates. Hence, in the modified variants the RT0/BDM1 basis functions are modified such that they vanish at the rotation axis $r = 0$ (see reference below). The compared variants are $:identity$ (no reconstruction, standard Galerkin), $:rt$ and $:bdm$ (reconstruction via unmodified RT0/BDM1 interpolation) and $:rt_plus$ and $:bdm_plus$ (modified variants whose reconstruction basis functions vanish at the rotation axis). All reconstruction variants are pressure-robust, but only the modified variants guarantee optimal convergence rates.

Reference

"Pressure-robustness for the axisymmetric Stokes problem by velocity reconstruction",
P.L. Lederer, C. Lehrenfeld, C. Merdon, T. van Beeck,
arXiv:2603.01030 [math.NA] (2026),
>Journal-Link<

The computed solution and error histories for the default parameters look like this:

module Example261_AxisymmetricStokesReconstruction

using ExtendableFEM
using ExtendableGrids
using ExtendableSparse
using GridVisualize
using LinearAlgebra
import Symbolics
using Symbolics: @variables, build_function
import Term

# Build representation matrix F of the weighted reconstruction operator
# Pi : H1BR{2} -> HDIVBDM1{2}, which matches r-weighted normal moments at faces
# (RT0 part, columns 1..nfaces) and r-weighted tangential moments at faces
# (BDM1 part, columns nfaces+1..2nfaces). axisymmetric_modifications switches the
# special treatment of faces at the rotation axis r = 0, onlyRT0 drops the BDM1 part.
function H1BR_to_HDIVBDM1_axisymmetric_interpolator(FES_BR::FESpace{Tv, Ti, H1BR{2}}; axisymmetric_modifications = true, onlyRT0 = false) where {Tv, Ti}
    facedofs_V1::VariableTargetAdjacency{Int32} = FES_BR[FaceDofs]

    # extract grid information
    xgrid = FES_BR.xgrid
    xFaceVolumes = xgrid[FaceVolumes]
    xCoordinates = xgrid[Coordinates]
    xFaceNodes = xgrid[FaceNodes]
    xFaceNormals = xgrid[FaceNormals]
    nfaces = num_sources(xFaceNormals)

    # create reconstruction space
    FES_reconst = FESpace{HDIVBDM1{2}}(xgrid)

    # create representation matrix of interpolation
    F = FEMatrix(FES_BR, FES_reconst)
    FE = F.entries
    w = zeros(Float64, 2)
    for face in 1:nfaces
        # get radial coordinates of face endpoints
        w[1] = xCoordinates[1, xFaceNodes[1, face]]
        w[2] = xCoordinates[1, xFaceNodes[2, face]]
        wmid = (w[1] + w[2]) / 2

        # RT0 dofs: r-weighted normal moment integrals
        FE[facedofs_V1[1, face], face] = xFaceVolumes[face] * (1 // 6 * w[1] + 1 // 3 * wmid) * xFaceNormals[1, face]
        FE[facedofs_V1[2, face], face] = xFaceVolumes[face] * (1 // 6 * w[2] + 1 // 3 * wmid) * xFaceNormals[1, face]
        FE[facedofs_V1[3, face], face] = xFaceVolumes[face] * (1 // 6 * w[1] + 1 // 3 * wmid) * xFaceNormals[2, face]
        FE[facedofs_V1[4, face], face] = xFaceVolumes[face] * (1 // 6 * w[2] + 1 // 3 * wmid) * xFaceNormals[2, face]
        FE[facedofs_V1[5, face], face] = xFaceVolumes[face] * wmid

        # BDM1 dofs: r-weighted tangential moment integrals
        if w[1] == 0 && axisymmetric_modifications
            # face at rotation axis: replace tangential moment by a modified one, such that
            # the reconstruction basis functions vanish at the rotation axis
            for d in 1:5
                FE[facedofs_V1[d, face], nfaces + face] = 1 // 6 * FE[facedofs_V1[d, face], face]
            end
        elseif w[2] == 0 && axisymmetric_modifications
            # same but for the opposite orientation of the face normal
            for d in 1:5
                FE[facedofs_V1[d, face], nfaces + face] = -1 // 6 * FE[facedofs_V1[d, face], face]
            end
        elseif !onlyRT0
            FE[facedofs_V1[1, face], nfaces + face] = xFaceVolumes[face] * (-1 // 12 * w[1]) * xFaceNormals[1, face]
            FE[facedofs_V1[2, face], nfaces + face] = xFaceVolumes[face] * (1 // 12 * w[2]) * xFaceNormals[1, face]
            FE[facedofs_V1[3, face], nfaces + face] = xFaceVolumes[face] * (-1 // 12 * w[1]) * xFaceNormals[2, face]
            FE[facedofs_V1[4, face], nfaces + face] = xFaceVolumes[face] * (1 // 12 * w[2]) * xFaceNormals[2, face]
        end
    end
    flush!(FE)
    return F, FES_reconst
end

# exact data of a manufactured solution via Symbolics: velocity of type vtype, pressure of
# type ptype, and load f = -mu * (Delta u - r^{-2} u_r e_r) + grad(p)
function prepare_data(; μ = 1, vtype = 3, ptype = 3)

    @variables r z

    # velocity u = (u_r, u_z), all types are r-divergence-free
    if vtype == 0
        u = [0 * r, 1]
    elseif vtype == 1
        u = [r, -2 * z]
    elseif vtype == 2
        u = [r^2, -3 * r * z]
    elseif vtype == 3
        u = [r * cos(pi * r / 2) * sin(pi * z + pi / 2), cos(pi * z + pi / 2) * ((2 / pi) * cos(pi * r / 2) - (r / 2) * sin(pi * r / 2))]
    end

    # pressure, with varying radial regularity r^s
    if ptype == 0
        p = 0 * r
    elseif ptype == 1
        p = r^(7 / 4) - 2 / (15 / 4)
        p += z^2 - 1 / 3
    elseif ptype == 2
        p = log2(r)
    elseif ptype == 3
        s = 0.25
        p = r^s - 2 / (2 + s)
    elseif ptype == 4
        s = -0.5
        p = 10 * (r^s - 2 / (2 + s))
    elseif ptype == 5
        s = 3
        p = r^s - 2 / (2 + s)
    end

    # gradient of velocity
    ∇u = Symbolics.jacobian(u, [r, z])
    ∇u_reshaped = [∇u[1, 1], ∇u[1, 2], ∇u[2, 1], ∇u[2, 2]]

    # cylindrical Laplacian
    Δu = [
        (Symbolics.gradient(∇u[1, 1], [r]) + Symbolics.gradient(∇u[1, 2], [z]))[1] + ∇u[1, 1] / r,
        (Symbolics.gradient(∇u[2, 1], [r]) + Symbolics.gradient(∇u[2, 2], [z]))[1] + ∇u[2, 1] / r,
    ]

    # right-hand side
    f = -μ * (Δu - [u[1] / r^2, 0]) + Symbolics.gradient(p, [r, z])

    # build functions
    p_eval = build_function(p, r, z, expression = Val{false})
    u_eval = build_function(u, r, z, expression = Val{false})
    ∇u_eval = build_function(∇u_reshaped, r, z, expression = Val{false})
    f_eval = build_function(f, r, z, expression = Val{false})

    return f_eval[2], u_eval[2], ∇u_eval[2], p_eval
end

# convection term r * (u * grad)u (used when nonlinear = true)
function kernel_convection!(result, input, qpinfo)
    u, ∇u = view(input, 1:2), view(input, 3:6)
    r = qpinfo.x[1]
    result[1] = r * (∇u[1] * u[1] + ∇u[2] * u[2])
    result[2] = r * (∇u[3] * u[1] + ∇u[4] * u[2])
    return nothing
end

# axisymmetric Stokes bilinear form, with viscosity μ passed via qpinfo.params.
# Result entries refer to the test functions of [id(u), grad(u), id(p)], weighted by r.
function kernel_stokes_axisymmetric!(result, u_ops, qpinfo)
    u, ∇u, p = view(u_ops, 1:2), view(u_ops, 3:6), view(u_ops, 7)
    r = qpinfo.x[1]
    μ = qpinfo.params[1]
    result[1] = μ / r * u[1] - p[1]
    result[2] = 0
    result[3] = μ * r * ∇u[1] - r * p[1]
    result[4] = μ * r * ∇u[2]
    result[5] = μ * r * ∇u[3]
    result[6] = μ * r * ∇u[4] - r * p[1]
    result[7] = -(r * (∇u[1] + ∇u[4]) + u[1])
    return nothing
end

function main(;
        μ = 0.1,
        levels = 1:5,
        vtype = 3,
        ptype = 3,
        nonlinear = false,
        operators = [:identity, :rt, :rt_plus, :bdm, :bdm_plus],
        quadorder = 10,
        Plotter = nothing,
        kwargs...,
    )
    nlevels = length(levels)
    noperators = length(operators)

    # unknowns: velocity, pressure, and reconstructed weighted velocity ru = Pi(r u_h)
    u = Unknown("u", name = "velocity")
    p = Unknown("p", name = "pressure")
    ru = Unknown("ru", name = "reconstructed weighted velocity")

    # exact data (independent of μ for the Stokes problem without convection)
    f_eval, u_eval, ∇u_eval, p_eval = prepare_data(; μ, vtype, ptype)
    f!(result, qpinfo) = f_eval(result, qpinfo.x[1], qpinfo.x[2])
    u!(result, qpinfo) = u_eval(result, qpinfo.x[1], qpinfo.x[2])
    ∇u!(result, qpinfo) = ∇u_eval(result, qpinfo.x[1], qpinfo.x[2])
    p!(result, qpinfo) = (result[1] = p_eval(qpinfo.x[1], qpinfo.x[2]))

    # kernels for error measurement, with volume weight r = x[1] (inverse weight in negative norms)
    function kernel_l2error(result, uh, qpinfo)
        u!(result, qpinfo)
        return result .= (result .- uh) .^ 2 * qpinfo.x[1]
    end

    function kernel_energyerror(result, u_ops, qpinfo)
        u!(result, qpinfo)
        ∇u!(view(result, 3:6), qpinfo)
        result .-= u_ops
        result .= result .^ 2
        result[1] /= qpinfo.x[1]
        result[2] = 0
        result[3:6] .*= qpinfo.x[1]
        return nothing
    end

    function kernel_l2m1error(result, ru_h, qpinfo)
        u!(view(result, 1:2), qpinfo)
        return result .= (qpinfo.x[1] .* view(result, 1:2) .- ru_h) .^ 2 / qpinfo.x[1]
    end

    function kernel_pl2error(result, ph, qpinfo)
        p!(result, qpinfo)
        return result .= (result .- ph) .^ 2 * qpinfo.x[1]
    end

    function kernel_l2div(result, u_ops, qpinfo)
        idu, divu = view(u_ops, 1:2), view(u_ops, 3)
        result[1] = (qpinfo.x[1] * divu[1] + idu[1])^2 / qpinfo.x[1]
        return nothing
    end

    function kernel_l2div_rv(result, div_ru, qpinfo)
        result[1] = div_ru[1]^2 / qpinfo.x[1]
        return nothing
    end

    function kernel_bnd_normalerror(result, ru_h, qpinfo)
        result .= ru_h .^ 2
        return nothing
    end

    function kernel_pmean(result, ph, qpinfo)
        result[1] = qpinfo.x[1] * ph[1]
        return nothing
    end

    L21ErrorIntegrator = ItemIntegrator(kernel_l2error, [id(u)]; quadorder)
    EnergyErrorIntegrator = ItemIntegrator(kernel_energyerror, [id(u), grad(u)]; quadorder)
    L2m1ErrorIntegrator = ItemIntegrator(kernel_l2m1error, [id(ru)]; quadorder, resultdim = 2)
    PErrorIntegrator = ItemIntegrator(kernel_pl2error, [id(p)]; quadorder)
    AxiDivIntegrator = ItemIntegrator(kernel_l2div, [id(u), div(u)]; quadorder, resultdim = 1)
    DivIntegrator = ItemIntegrator(kernel_l2div_rv, [div(ru)]; quadorder, resultdim = 1)
    L2bndIntegrator = ItemIntegratorDG(kernel_bnd_normalerror, [this(id(ru))]; entities = ON_BFACES, regions = [4], resultdim = 2, quadorder)
    PMeanIntegrator = ItemIntegrator(kernel_pmean, [id(p)]; quadorder, resultdim = 1)

    # error columns: L2_1(u), L2_{-1}(ru), energy(u), L2_1(p), div(u_h), div(Pi(r u_h)), |ru| on axis
    Results = zeros(Float64, nlevels, 7 * noperators)
    NDofs = zeros(Int, nlevels)
    sol = nothing

    # refinement loop
    for (l, level) in enumerate(levels)
        xgrid = uniform_refine(grid_unitsquare(Triangle2D), level)

        # loop over reconstruction operators
        for (recid, reconstruction) in enumerate(operators)
            offset = (recid - 1) * 7

            # FESpace: Bernardi-Raugel velocity, piecewise constant pressure
            FES = [FESpace{H1BR{2}}(xgrid), FESpace{L2P0{1}}(xgrid)]

            # representation matrix F and space of the weighted reconstruction operator
            F, FES_reconst = H1BR_to_HDIVBDM1_axisymmetric_interpolator(FES[1]; axisymmetric_modifications = reconstruction in [:rt_plus, :bdm_plus], onlyRT0 = reconstruction in [:rt, :rt_plus])

            # problem description
            PD = ProblemDescription("axisymmetric Stokes")
            assign_unknown!(PD, u)
            assign_unknown!(PD, p)
            assign_operator!(PD, BilinearOperator(kernel_stokes_axisymmetric!, [id(u), grad(u), id(p)]; params = [μ], kwargs...))
            if nonlinear
                assign_operator!(PD, NonlinearOperator(kernel_convection!, [id(u)], [id(u), grad(u)]; kwargs...))
            end

            # load term: either directly, or via reconstruction Pi as a discrete H(div)-flux
            if reconstruction == :identity
                function fr!(result, qpinfo)
                    f!(result, qpinfo)
                    result .*= qpinfo.x[1]
                    return nothing
                end
                assign_operator!(PD, LinearOperator(fr!, [id(u)]; quadorder))
            else
                # bR = load applied to BDM1 test functions, and c = F' bR collects the load
                # against the reconstructed BR basis functions, c_i = (f, Pi(phi_i))
                bR = FEVector(FES_reconst)
                assemble!(bR, LinearOperator(f!, [id(1)]; quadorder))
                c = F.entries.cscmatrix * bR.entries
                assign_operator!(PD, LinearOperator(c, [u]))
            end

            # boundary conditions: u_r = 0 (no penetration) at rotation axis, else interpolate exact data
            assign_operator!(PD, HomogeneousBoundaryData(u; regions = [4], mask = (1, 0, 1)))
            assign_operator!(PD, InterpolateBoundaryData(u, u!; regions = [1, 2, 3]))
            assign_restriction!(PD, BoundaryDataRestriction(u; regions = [4], value = 0, mask = (1, 0, 1)))
            assign_operator!(PD, FixDofs(p; dofs = [1]))

            # solve
            sol = solve(PD, FES; kwargs...)
            NDofs[l] = length(sol.entries)

            # postprocess: append reconstruction ru = Pi(r u_h)
            if reconstruction == :identity
                append!(sol, FESpace{H1Pk{2, 2, 3}}(xgrid); tag = ru)
                lazy_interpolate!(sol[ru], sol, [id(u)]; postprocess = (result, input, qpinfo) -> (result .= input .* qpinfo.x[1]))
            else
                append!(sol, FES_reconst; tag = ru)
                view(sol[ru]) .= F.entries.cscmatrix' * view(sol[u])
            end

            # correct pressure mean with weight r (note that sum(CellVolumes) = 2 * integral(r))
            pmean = sum(evaluate(PMeanIntegrator, sol)) / sum(xgrid[CellVolumes])
            view(sol[p]) .-= 2 * pmean

            # measure errors
            Results[l, 1 + offset] = sqrt(sum(evaluate(L21ErrorIntegrator, sol)))
            Results[l, 2 + offset] = sqrt(sum(evaluate(L2m1ErrorIntegrator, sol)))
            Results[l, 3 + offset] = sqrt(sum(evaluate(EnergyErrorIntegrator, sol)))
            Results[l, 4 + offset] = sqrt(sum(evaluate(PErrorIntegrator, sol)))
            Results[l, 5 + offset] = sqrt(sum(evaluate(AxiDivIntegrator, sol)))
            Results[l, 6 + offset] = sqrt(sum(evaluate(DivIntegrator, sol)))
            Results[l, 7 + offset] = sqrt(sum(evaluate(L2bndIntegrator, sol)))
        end
    end

    # print convergence histories
    ylabels = ["|| u - u_h ||_{0,1}", "|| ru - Pi(ru_h) ||_{0,-1}", "|| u - u_h ||_V", "|| p - p_h ||_{0,1}", "|| div u_h ||_{0,1}", "||div(Pi(ru_h))||_{0,-1}"]
    for (recid, reconstruction) in enumerate(operators)
        println("\nResults for reconstruction = $reconstruction")
        print_convergencehistory(NDofs, Results[:, (recid - 1) * 7 .+ (1:6)]; X_to_h = X -> X .^ (-1 / 2), ylabels, xlabel = "ndof")
    end

    # plot solution and error histories
    plt = GridVisualizer(; Plotter, layout = (2, 2), clear = true, size = (1200, 900))
    vectorplot!(plt[1, 1], id(u), sol; title = "u_h (last run)")
    scalarplot!(plt[1, 2], id(p), sol; title = "p_h (last run)")
    plot_convergencehistory!(plt[2, 1], NDofs, Results[:, 3 .+ 7 .* (0:(noperators - 1))]; add_h_powers = [1], X_to_h = X -> 2 * X .^ (-1 / 2), legend = :best, title = "|| u - u_h ||_V", ylabels = string.(operators))
    plot_convergencehistory!(plt[2, 2], NDofs, Results[:, 1 .+ 7 .* (0:(noperators - 1))]; add_h_powers = [2], X_to_h = X -> X .^ (-1 / 2), legend = :best, title = "|| u - u_h ||_{L^2_1}", ylabels = string.(operators))
    reveal(plt)

    return Results, plt
end

    # reconstruction variants deliver exactly r-divergence-free reconstructed fluxes
    # the BDM1-based reconstructions are more accurate in L2 than the plain RT0 one
end # module

This page was generated using Literate.jl.