{ "cells": [ { "cell_type": "markdown", "source": [ "# Tutorial 6: Poisson equation (with DG)" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "In this tutorial, we will learn\n", " - How to solve a simple PDE with a DG method\n", " - How to compute jumps and averages of quantities on the mesh skeleton\n", " - How to implement the method of manufactured solutions\n", " - How to integrate error norms\n", " - How to generate Cartesian meshes in arbitrary dimensions\n", "\n", "\n", "## Problem statement\n", "\n", "The goal of this tutorial is to solve a PDE using a DG formulation. For simplicity, we take the Poisson equation on the unit cube $\\Omega \\doteq (0,1)^3$ as the model problem, namely" ], "metadata": {} }, { "cell_type": "markdown", "source": [ "$$\n", "\\left\\lbrace\n", "\\begin{aligned}\n", "-\\Delta u = f \\ &\\text{in} \\ \\Omega,\\\\\n", "u = g \\ &\\text{on}\\ \\partial\\Omega,\\\\\n", "\\end{aligned}\n", "\\right.\n", "$$\n", "where $f$ is the source term and $g$ is the prescribed Dirichlet boundary function. In this tutorial, we follow the method of manufactured solutions since we want to illustrate how to compute discretization errors. We take $u(x) = 3 x_1 + x_2 + 2 x_3$ as the exact solution of the problem, for which $f=0$ and $g(x) = u(x)$. The selected manufactured solution $u$ is a first order multi-variate polynomial, which can be represented exactly by the FE interpolation that we are going to define below. In this scenario, the discretization error has to be close to the machine precision. We will use this result to validate the proposed implementation.\n", "\n", "## Numerical Scheme\n", "\n", "We consider a DG formulation to approximate\n", "the problem. In particular, we consider the symmetric\n", "interior penalty method (see, e.g. [1], for specific details). For this formulation, the approximation space is made of discontinuous piece-wise polynomials, namely\n", "\n", "$$\n", "V \\doteq \\{ v\\in L^2(\\Omega):\\ v|_{T}\\in Q_p(T) \\text{ for all } T\\in\\mathcal{T} \\},\n", "$$\n", "where $\\mathcal{T}$ is the set of all cells $T$ of the FE mesh, and $Q_p(T)$ is a polynomial space of degree $p$ defined on a generic cell $T$. For simplicity, we consider Cartesian meshes in this tutorial. In this case, the space $Q_p(T)$ is made of multi-variate polynomials up to degree $p$ in each spatial coordinate.\n", "\n", "In order to write the weak form of the problem, we need to introduce some notation. The sets of interior and boundary facets associated with the FE mesh $\\mathcal{T}$ are denoted here as $\\mathcal{F}_\\Gamma$ and $\\mathcal{F}_{\\partial\\Omega}$ respectively. In addition, for a given function $v\\in V$ restricted to the interior facets $\\mathcal{F}_\\Gamma$, we introduce the well known jump and mean value operators,\n", "$$\n", "\\begin{aligned}\n", "\\lbrack\\!\\lbrack v\\ n \\rbrack\\!\\rbrack &\\doteq v^+\\ n^+ + v^- n^-,\\\\\n", "\\{\\! \\!\\{\\nabla v\\}\\! \\!\\} &\\doteq \\dfrac{ \\nabla v^+ + \\nabla v^-}{2},\n", "\\end{aligned}\n", "$$\n", "with $v^+$, and $v^-$ being the restrictions of $v\\in V$ to the cells $T^+$, $T^-$ that share a generic interior facet in $\\mathcal{F}_\\Gamma$, and $n^+$, and $n^-$ are the facet outward unit normals from either the perspective of $T^+$ and $T^-$ respectively.\n", "\n", "With this notation, the weak form associated with the interior penalty formulation of our problem reads: find $u\\in V$ such that $a(u,v) = b(v)$ for all $v\\in V$. The bilinear and linear forms $a(\\cdot,\\cdot)$ and $b(\\cdot)$ have contributions associated with the bulk of $\\Omega$, the boundary facets $\\mathcal{F}_{\\partial\\Omega}$, and the interior facets $\\mathcal{F}_\\Gamma$, namely\n", " $$\n", "\\begin{aligned}\n", "a(u,v) &= a_{\\Omega}(u,v) + a_{\\partial\\Omega}(u,v) + a_{\\Gamma}(u,v),\\\\\n", "b(v) &= b_{\\Omega}(v) + b_{\\partial\\Omega}(v).\n", "\\end{aligned}\n", "$$\n", "These contributions are defined as\n", "$$\n", "\\begin{aligned}\n", "a_{\\Omega}(u,v) &\\doteq \\sum_{T\\in\\mathcal{T}} \\int_{T} \\nabla v \\cdot \\nabla u \\ {\\rm d}T,\\\\\n", "b_{\\Omega}(v) &\\doteq \\int_{\\Omega} v\\ f \\ {\\rm d}\\Omega,\n", "\\end{aligned}\n", "$$\n", "for the volume,\n", "$$\n", "\\begin{aligned}\n", "a_{\\partial\\Omega}(u,v) &\\doteq \\sum_{F\\in\\mathcal{F}_{\\partial\\Omega}} \\dfrac{\\gamma}{|F|} \\int_{F} v\\ u \\ {\\rm d}F \\\\ & - \\sum_{F\\in\\mathcal{F}_{\\partial\\Omega}} \\int_{F} v\\ (\\nabla u \\cdot n) \\ {\\rm d}F \\\\ & - \\sum_{F\\in\\mathcal{F}_{\\partial\\Omega}} \\int_{F} (\\nabla v \\cdot n)\\ u \\ {\\rm d}F, \\\\\n", "b_{\\partial\\Omega} &\\doteq \\sum_{F\\in\\mathcal{F}_{\\partial\\Omega}} \\dfrac{\\gamma}{|F|} \\int_{F} v\\ g \\ {\\rm d}F \\\\ & - \\sum_{F\\in\\mathcal{F}_{\\partial\\Omega}} \\int_{F} (\\nabla v \\cdot n)\\ g \\ {\\rm d}F,\n", "\\end{aligned}\n", "$$\n", "for the boundary facets and,\n", "$$\n", "\\begin{aligned}\n", "a_{\\Gamma}(u,v) &\\doteq \\sum_{F\\in\\mathcal{F}_{\\Gamma}} \\dfrac{\\gamma}{|F|} \\int_{F} \\lbrack\\!\\lbrack v\\ n \\rbrack\\!\\rbrack\\cdot \\lbrack\\!\\lbrack u\\ n \\rbrack\\!\\rbrack \\ {\\rm d}F \\\\ &- \\sum_{F\\in\\mathcal{F}_{\\Gamma}} \\int_{F} \\lbrack\\!\\lbrack v\\ n \\rbrack\\!\\rbrack\\cdot \\{\\! \\!\\{\\nabla u\\}\\! \\!\\} \\ {\\rm d}F \\\\ & - \\sum_{F\\in\\mathcal{F}_{\\Gamma}} \\int_{F} \\{\\! \\!\\{\\nabla v\\}\\! \\!\\}\\cdot \\lbrack\\!\\lbrack u\\ n \\rbrack\\!\\rbrack \\ {\\rm d}F,\n", "\\end{aligned}\n", "$$\n", " for the interior facets. In previous expressions, $|F|$ denotes the diameter of the face $F$ (in our Cartesian grid, this is equivalent to the characteristic mesh size $h$), and $\\gamma$ is a stabilization parameter that should be chosen large enough such that the bilinear form $a(\\cdot,\\cdot)$ is stable and continuous. Here, we take $\\gamma = p\\ (p+1)$ as done in the numerical experiments in reference [2].\n", "\n", "## Manufactured solution\n", "\n", "We start by loading the Gridap library and defining the manufactured solution $u$ and the associated source term $f$ and Dirichlet function $g$." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "using Gridap\n", "u(x) = 3*x[1] + x[2] + 2*x[3]\n", "f(x) = 0\n", "g(x) = u(x)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "We also need to define the gradient of $u$ since we will compute the $H^1$ error norm later. In that case, the gradient is simply defined as" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "∇u(x) = VectorValue(3,1,2)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "In addition, we need to tell the Gridap library that the gradient of the function `u` is available in the function `∇u` (at this moment `u` and `∇u` are two standard Julia functions without any connection between them). This is done by adding an extra method to the function `gradient` (aka `∇`) defined in Gridap:" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "import Gridap: ∇\n", "∇(::typeof(u)) = ∇u" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ " Now, it is possible to recover function `∇u` from function `u` as `∇(u)`. You can check that the following expression evaluates to `true`." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "∇(u) === ∇u" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "## Cartesian mesh generation\n", " In order to discretize the geometry of the unit cube, we use the Cartesian mesh generator available in Gridap." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "L = 1.0\n", "domain = (0.0, L, 0.0, L, 0.0, L)\n", "n = 4\n", "partition = (n,n,n)\n", "model = CartesianDiscreteModel(domain,partition)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "The type `CartesianDiscreteModel` is a concrete type that inherits from `DiscreteModel`, which is specifically designed for building Cartesian meshes. The `CartesianDiscreteModel` constructor takes a tuple containing limits of the box we want to discretize plus a tuple with the number of cells to be generated in each direction (here $4\\times4\\times4$ cells). You can write the model in vtk format to visualize it (see next figure)." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ " writevtk(model,\"model\")" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "![](../assets/dg_discretization/model.png)\n", "\n", " Note that the `CaresianDiscreteModel` is implemented for arbitrary dimensions. For instance, the following lines build a `CartesianDiscreteModel` for the unit square $(0,1)^2$ with 4 cells per direction" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "domain2D = (0.0, L, 0.0, L)\n", "partition2D = (n,n)\n", "model2D = CartesianDiscreteModel(domain2D,partition2D)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "You could also generate a mesh for the unit tesseract $(0,1)^4$ (i.e., the unit cube in 4D). Look how the 2D and 3D models are built and just follow the sequence.\n", "\n", "## FE spaces\n", "\n", "On top of the discrete model, we create the discontinuous space $V$ as follows" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "order = 3\n", "V = TestFESpace(\n", " reffe=:Lagrangian, valuetype=Float64, order=order,\n", " conformity=:L2, model=model)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "We have select a Lagrangian, scalar-valued interpolation of order $3$ within the cells of the discrete model. Since the cells are hexahedra, the resulting Lagrangian shape functions are tri-cubic polynomials. In contrast to previous tutorials, where we have constructed $H^1$-conforming (i.e., continuous) FE spaces, here we construct a $L^2$-conforming (i.e., discontinuous) FE space. That is, we do not impose any type of continuity of the shape function on the cell boundaries, which leads to the discontinuous FE space $V$ of the DG formulation. Note also that we do not pass any information about the Dirichlet boundary to the `TestFESpace` constructor since the Dirichlet boundary conditions are not imposed strongly in this example.\n", "\n", "From the `V` object we have constructed in previous code snippet, we build the trial FE space as usual." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "U = TrialFESpace(V)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "Note that we do not pass any Dirichlet function to the `TrialFESpace` constructor since we do not impose Dirichlet boundary conditions strongly here.\n", "\n", "## Numerical integration\n", "\n", "Once the FE spaces are ready, the next step is to set up the numerical integration. In this example, we need to integrate in three different domains: the volume covered by the cells $\\mathcal{T}$ (i.e., the computational domain $\\Omega$), the surface covered by the boundary facets $\\mathcal{F}_{\\partial\\Omega}$ (i.e., the boundary $\\partial\\Omega$), and the surface covered by the interior facets $\\mathcal{F}_{\\Gamma}$ (i.e. the so-called mesh skeleton). In order to integrate in $\\Omega$ and on its boundary $\\partial\\Omega$, we use `Triangulation` and `BoundaryTriangulation` objects as already discussed in previous tutorials." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "trian = Triangulation(model)\n", "btrian = BoundaryTriangulation(model)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "Here, we do not pass any boundary identifier to the `BoundaryTriangulation` constructor. In this case, an integration mesh for the entire boundary $\\partial\\Omega$ is constructed by default (which is just what we need in this example).\n", "\n", "In order to generate an integration mesh for the interior facets $\\mathcal{F}_{\\Gamma}$, we use a new type of `Triangulation` referred to as `SkeletonTriangulation`. It can be constructed from a `DiscreteModel` object as follows:" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "strian = SkeletonTriangulation(model)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "As any other type of `Triangulation`, an `SkeletonTriangulation` can be written into a vtk file for its visualization (see next figure, where the interior facets $\\mathcal{F}_\\Gamma$ are clearly observed)." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "writevtk(strian,\"strian\")" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "![](../assets/dg_discretization/skeleton_trian.png)\n", "\n", "Once we have constructed the triangulations needed in this example, we define the corresponding quadrature rules." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "degree = 2*order\n", "quad = CellQuadrature(trian,degree)\n", "bquad = CellQuadrature(btrian,degree)\n", "squad = CellQuadrature(strian,degree)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "We still need a way to represent the unit outward normal vector to the boundary $\\partial\\Omega$, and the unit normal vector on the interior faces $\\mathcal{F}_\\Gamma$. This is done with the `get_normal_vector` getter." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "nb = get_normal_vector(btrian)\n", "ns = get_normal_vector(strian)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "The `get_normal_vector` getter takes either a boundary or a skeleton triangulation and returns an object representing the normal vector to the corresponding surface. For boundary triangulations, the returned normal vector is the unit outwards one, whereas for skeleton triangulations the orientation of the returned normal is arbitrary. In the current implementation (Gridap v0.5.0), the unit normal is outwards to the cell with smaller id among the two cells that share an interior facet in $\\mathcal{F}_\\Gamma$.\n", "\n", "## Weak form\n", "\n", "With these ingredients we can define the different terms in the weak form. First, we start with the terms $a_\\Omega(\\cdot,\\cdot)$ , and $b_\\Omega(\\cdot)$ associated with integrals in the volume $\\Omega$. This is done as in the tutorial for the Poisson equation." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "a_Ω(u,v) = ∇(v)*∇(u)\n", "b_Ω(v) = v*f\n", "t_Ω = AffineFETerm(a_Ω,b_Ω,trian,quad)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "The terms $a_{\\partial\\Omega}(\\cdot,\\cdot)$ and $b_{\\partial\\Omega}(\\cdot)$ associated with integrals on the boundary $\\partial\\Omega$ are defined using an analogous approach. First, we define two functions representing the integrands of the forms $a_{\\partial\\Omega}(\\cdot,\\cdot)$ and $b_{\\partial\\Omega}(\\cdot)$. Then, we build an `AffineFETerm` from these functions and the boundary triangulation and its corresponding quadrature rule:" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "h = L / n\n", "γ = order*(order+1)\n", "a_∂Ω(u,v) = (γ/h)*v*u - v*(∇(u)*nb) - (∇(v)*nb)*u\n", "b_∂Ω(v) = (γ/h)*v*g - (∇(v)*nb)*g\n", "t_∂Ω = AffineFETerm(a_∂Ω,b_∂Ω,btrian,bquad)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "Note that in the definition of the functions `a_∂Ω` and `b_∂Ω`, we have used the object `nb` representing the outward unit normal to the boundary $\\partial\\Omega$. The code definition of `a_∂Ω` and `b_∂Ω` is indeed very close to the mathematical definition of the forms $a_{\\partial\\Omega}(\\cdot,\\cdot)$ and $b_{\\partial\\Omega}(\\cdot)$.\n", "\n", "Finally, we need to define the term $a_\\Gamma(\\cdot,\\cdot)$ integrated on the interior facets $\\mathcal{F}_\\Gamma$. In this case, we use a `LinearFETerm` since the terms integrated on the interior facets only contribute to the system matrix and not to the right-hand-side vector." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "a_Γ(u,v) = (γ/h)*jump(v*ns)*jump(u*ns) - jump(v*ns)*mean(∇(u)) - mean(∇(v))*jump(u*ns)\n", "t_Γ = LinearFETerm(a_Γ,strian,squad)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "Note that the arguments `v`, `u` of function `a_Γ` represent a test and trial function *restricted* to the interior facets $\\mathcal{F}_\\Gamma$. As mentioned before in the presentation of the DG formulation, the restriction of a function $v\\in V$ to the interior faces leads to two different values $v^+$ and $v^-$ . In order to compute jumps and averages of the quantities $v^+$ and $v^-$, we use the functions `jump` and `mean`, which represent the jump and mean value operators $\\lbrack\\!\\lbrack \\cdot \\rbrack\\!\\rbrack$ and $\\{\\! \\!\\{\\cdot\\}\\! \\!\\}$ respectively. Note also that we have used the object `ns` representing the unit normal vector on the interior facets. As a result, the notation used to define function `a_Γ` is very close to the mathematical definition of the terms in the bilinear form $a_\\Gamma(\\cdot,\\cdot)$.\n", "\n", "Once the different terms of the weak form have been defined, we build and solve the FE problem." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "op = AffineFEOperator(U,V,t_Ω,t_∂Ω,t_Γ)\n", "uh = solve(op)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "## Discretization error\n", "\n", "We end this tutorial by quantifying the discretization error associated with the computed numerical solution `uh`. In DG methods a simple error indicator is the jump of the computed (discontinuous) approximation on the interior faces. This quantity can be easily computed in Gridap as follows. First, we need to restrict the computed solution `uh` to the skeleton triangulation." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "uh_Γ = restrict(uh,strian)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "The resulting object `uh_Γ` is an object which represents the two values $u^+_h$, $u^-_h$ of the solution $u_h$ restricted to the interior facets $\\mathcal{F}_\\Gamma$. We compute and visualize the jump of these values as follows (see next figure):" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "writevtk(strian,\"jumps\",cellfields=[\"jump_u\"=>jump(uh_Γ)])" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "Note that the jump of the numerical solution is very small, close to the machine precision (as expected in this example with manufactured solution).\n", "![](../assets/dg_discretization/jump_u.png)\n", "\n", " A more rigorous way of quantifying the error is to measure it with a norm. Here, we use the $L^2$ and $H^1$ norms, namely\n", " $$\n", "\\begin{aligned}\n", " \\| w \\|_{L^2}^2 & \\doteq \\int_{\\Omega} w^2 \\ \\text{d}\\Omega, \\\\\n", " \\| w \\|_{H^1}^2 & \\doteq \\int_{\\Omega} w^2 + \\nabla w \\cdot \\nabla w \\ \\text{d}\\Omega.\n", "\\end{aligned}\n", "$$\n", "\n", "The discretization error can be computed in this example as the difference of the manufactured and numerical solutions." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "e = u - uh" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "We compute the error norms as follows. First, we implement the integrands of the norms we want to compute." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "l2(u) = u*u\n", "h1(u) = a_Ω(u,u) + l2(u)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "Then, we compute the corresponding integrals with the `integrate` function." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "el2 = sqrt(sum( integrate(l2(e),trian,quad) ))\n", "eh1 = sqrt(sum( integrate(h1(e),trian,quad) ))" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "The `integrate` function returns a lazy object representing the contribution to the integral of each cell in the underlying triangulation. To end up with the desired error norms, one has to sum these contributions and take the square root. You can check that the computed error norms are close to machine precision (as one would expect)." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "tol = 1.e-10\n", "@assert el2 < tol\n", "@assert eh1 < tol" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "## References\n", "\n", "[1] D. N. Arnold, F. Brezzi, B. Cockburn, and L. Donatella Marini. Unified analysis of discontinuous Galerkin methods for elliptic problems. *SIAM Journal on Numerical Analysis*, 39 (5):1749–1779, 2001. doi:[10.1137/S0036142901384162](http://dx.doi.org/10.1137/S0036142901384162).\n", "\n", "[2] B. Cockburn, G. Kanschat, and D. Schötzau. An equal-order DG method for the incompressible Navier-Stokes equations. *Journal of Scientific Computing*, 40(1-3):188–210, 2009. doi:[10.1007/s10915-008-9261-1](http://dx.doi.org/10.1007/s10915-008-9261-1)." ], "metadata": {} }, { "cell_type": "markdown", "source": [ "---\n", "\n", "*This notebook was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*" ], "metadata": {} } ], "nbformat_minor": 3, "metadata": { "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.3.1" }, "kernelspec": { "name": "julia-1.3", "display_name": "Julia 1.3.1", "language": "julia" } }, "nbformat": 4 }