Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
Laplacian Assembly (DG)

Author: Pedro Peixoto ppeix.nosp@m.oto@.nosp@m.usp.b.nosp@m.r

This note records:

  1. A standard DG (interior penalty) formulation for the Laplacian.
  2. The specific formulation assembled in Vulpes in examples/assembly/laplacian/lap_dg_2d_main.cpp.
  3. Show an example of usage and post-processing.

Reference for notation and DG formulation style: L. Cangiani, Z. Dong, E. H. Georgoulis, and P. Houston, hp-Version Discontinuous Galerkin Methods on Polygonal and Polyhedral Meshes, Springer, 2017. DOI: 10.1007/978-3-319-19800-2

1) Theoretical DG Formulation of the Laplacian

Consider the Poisson problem

\[-\Delta u = f \quad \text{in } \Omega, \]

with Dirichlet boundary condition

\[u = g \quad \text{on } \partial\Omega. \]

Let \(\mathcal{T}_h\) be a mesh, \(\mathcal{F}_h\) the set of interior faces, and \(\mathcal{F}_h^{\mathcal{B}}\) the set of boundary faces. For a scalar \(w\) and vector \(\tau\), on an interior face shared by elements \(K^+\) and \(K^-\) (with outward normals \(n^+,n^-\)), define

\[\{\!\{w\}\!\} = \tfrac12(w^+ + w^-), \qquad [\![w]\!] = w^+ n^+ + w^- n^-. \]

A standard symmetric interior penalty DG (SIPG) bilinear form is

\[a_h(u_h,v_h) = \sum_{K\in\mathcal{T}_h} \int_K \nabla u_h\cdot\nabla v_h\,dx - \sum_{e\in\mathcal{F}_h\cup\mathcal{F}_h^{\mathcal{B}}} \int_e \{\!\{\nabla u_h\}\!\}\cdot [\![v_h]\!] \,ds - \sum_{e\in\mathcal{F}_h\cup\mathcal{F}_h^{\mathcal{B}}} \int_e \{\!\{\nabla v_h\}\!\}\cdot [\![u_h]\!] \,ds + \sum_{e\in\mathcal{F}_h\cup\mathcal{F}_h^{\mathcal{B}}} \int_e \gamma [\![u_h]\!]\cdot[\![v_h]\!] \,ds. \]

The linear functional for Dirichlet data is

\[\ell_h(v_h) = \sum_{K\in\mathcal{T}_h}\int_K f v_h\,dx - \sum_{e\in\mathcal{F}_h^{\mathcal{B}}} \int_e g\,\nabla v_h\cdot n\,ds + \sum_{e\in\mathcal{F}_h^{\mathcal{B}}} \int_e \gamma g v_h\,ds. \]

where \(\gamma\) is the discontinuous penalty function.

The DG problem is: find \(u_h\in V_h\) such that

\[a_h(u_h,v_h) = \ell_h(v_h) \qquad \forall v_h\in V_h. \]

2) Cellwise derivation

Vulpes, and in fact most DG codes, loop over cells and then the edges of the respective cells, not through the whole set of faces directly. Here we will re-write the formulation above to accommodate for that.

We will look into the internal edge term, dropping the \(_h\) for brevity, and noting that \(n^+=-n^-\).

\[ - \{\!\{\nabla u\}\!\}\cdot [\![v]\!] - \{\!\{\nabla v\}\!\}\cdot [\![u]\!] =\\ - \frac{1}{2}(\nabla u^{+} +\nabla u^{-}) \cdot (v^+ n^+ + v^- n^-) - \frac{1}{2}(\nabla v^+ + \nabla v^-)\cdot (u^+n^++u^-n^-) =\\ - \frac{1}{2}(\nabla u^{+}\cdot n^+)v^+ - \frac{1}{2}(\nabla u^{-}\cdot n^-)v^- + \frac{1}{2}(\nabla u^{+}\cdot n^+)v^- + \frac{1}{2}(\nabla u^{-}\cdot n^-)v^+ + \\ - \frac{1}{2}(\nabla v^{+}\cdot n^+)u^+ - \frac{1}{2}(\nabla v^{-}\cdot n^-)u^- + \frac{1}{2}(\nabla v^{+}\cdot n^+)u^- + \frac{1}{2}(\nabla v^{-}\cdot n^-)u^+ . \]

We will split these terms into the cell interior forms (C), that do not have mixed terms with cell and neighbor, and the mixed ones, with connections to neighbors (N), so that

\[- \{\!\{\nabla u\}\!\}\cdot [\![v]\!] - \{\!\{\nabla v\}\!\}\cdot [\![u]\!] = C+N \]

with

\[C = C^++C^- = \\- \frac{1}{2}(\nabla u^{+}\cdot n^+)v^+ - \frac{1}{2}(\nabla u^{-}\cdot n^-)v^- \\ - \frac{1}{2}(\nabla v^{+}\cdot n^+)u^+ - \frac{1}{2}(\nabla v^{-}\cdot n^-)u^- \]

\[N = N^+ + N^- = \\ \frac{1}{2}(\nabla u^{+}\cdot n^+)v^- + \frac{1}{2}(\nabla u^{-}\cdot n^-)v^+ \\ + \frac{1}{2}(\nabla v^{+}\cdot n^+)u^- + \frac{1}{2}(\nabla v^{-}\cdot n^-)u^+, \]

where

\[C^+ = - \frac{1}{2}(\nabla u^{+}\cdot n^+)v^+ - \frac{1}{2}(\nabla v^{+}\cdot n^+)u^+ \]

\[N^+ = \frac{1}{2}(\nabla u^{+}\cdot n^+)v^-+ \frac{1}{2}(\nabla v^{+}\cdot n^+)u^- \]

and similar for \(C^-\) and \(N^-\), just swapping \(+\) for \(-\), and vice versa. Note C only depends on the respective cell (either \(K^+\) or \(K^-\), but not both). Therefore our original form reads as:

\[- \sum_{e\in\mathcal{F}_h\cup\mathcal{F}_h^{\mathcal{B}}} \int_e \{\!\{\nabla u_h\}\!\}\cdot [\![v_h]\!] \,ds - \sum_{e\in\mathcal{F}_h\cup\mathcal{F}_h^{\mathcal{B}}} \int_e \{\!\{\nabla v_h\}\!\}\cdot [\![u_h]\!] \,ds = \\ = \sum_{e\in\mathcal{F}_h\cup\mathcal{F}_h^{\mathcal{B}}} \int_e C_e^+ + C_e^- + N_e^+ + N_e^- \, ds \]

There are 2 important things happening here:

  • For each edge \(e\) in the mesh, both cells \(K^+\) and \(K^-\) have added contributions, so when we move to cellwise computation, we need only to accommodate for the respective cell contributions, that is,

    \[ \sum_{K\in\mathcal{T}_h} \sum_{e\in K } \int_e C^K_e \, ds = \sum_{e\in\mathcal{F}_h\cup\mathcal{F}_h^{\mathcal{B}}} \int_e C_e^+ + C_e^- \, ds \]

    and the same for the \(N\).
  • For each term \(C\) or \(N\) we have the sum of the form and their respective "transpose" form (swapping trial and test functions), for instance, \(\frac{1}{2}(\nabla u^{+}\cdot n^+)v^+\) and \(\frac{1}{2}(\nabla v^{+}\cdot n^+)u^+\).

Now, defining

\[\text{IA}^K = - \sum_{e\in K } \int_e \frac{1}{2}(\nabla u^{K}\cdot n^K)v^K \, ds \]

and

\[\text{IAn}^K_e = \int_e \frac{1}{2}(\nabla u^{K}\cdot n^K)v^{K'} \, ds, \]

where \(K'\) is the neighbour cell of \(K\) with respect to the given edge/face \(e\), and observing that

\[\text{IA}^K + (\text{IA}^K)^t = \sum_{e\in K } \int_e C_e^K, \]

where \(()^t\) here means the transposition of trial and test basis function, we can finally write the original weakform as appears in the Vulpes code:

\[- \sum_{e\in\mathcal{F}_h\cup\mathcal{F}_h^{\mathcal{B}}} \int_e \{\!\{\nabla u\}\!\}\cdot [\![v]\!] + \{\!\{\nabla v\}\!\}\cdot [\![u]\!] \, ds = \sum_{K\in\mathcal{T}_h} \text{IA}^K + (\text{IA}^K)^t + \sum_{K\in\mathcal{T}_h} \sum_{e\in K } \text{IAn}^K_e + (\text{IAn}^K_e)^t, \]

A similar analysis can be done for other terms in the bilinear form, for instance with the penalty term:

\[\sum_{e\in\mathcal{F}_h\cup\mathcal{F}_h^{\mathcal{B}}} \int_e \gamma [\![u_h]\!]\cdot[\![v_h]\!]. \]

3) Code notations

From examples/assembly/laplacian/lap_dg_2d_main.cpp, Vulpes assembles the Laplacian with modal DG basis and the manufactured solution

\[u(x,y)=\sin(2\pi x)\sin(2\pi y), \qquad f(x,y)=8\pi^2\sin(2\pi x)\sin(2\pi y). \]

Core weak-form terms in code

The file defines the following expressions (symbol names in parentheses):

  • Volume stiffness (wf_stiff):

    \[\int_K \nabla u\cdot\nabla v\,dx \]

  • Interior/boundary consistency term on current cell block (wf_IA):

    \[\int_{\partial K} bc\,(\nabla u\cdot n)\,v\,ds \]

    with

    \[bc = \begin{cases} -\tfrac12 & \text{if face has neighbor (interior)},\\ -1 & \text{if boundary face}. \end{cases} \]

In vulpes, every time that there is an edge/facet integration that relies only on the current cell (so restricted to information in \(K\), integrated on \(\partial K\)), the output is always the sum of the integrals over all edges/facets.

  • Penalty term on current cell block (wf_SA):

    \[\int_{\partial K} \gamma\,u\,v\,ds \]

    where \(\gamma\) is created as a penalty expression with scaling proportional to polynomial order (configured with factor 10.0 in the example).
  • Neighbor coupling consistency term (wf_IAn):

    \[\int_e \tfrac12\,(\nabla u\cdot n)\,v_{\text{neigh}}\,ds \]

  • Neighbor coupling penalty term (wf_SAn):

    \[-\int_e \gamma\,u\,v_{\text{neigh}}\,ds \]

  • Volume RHS (wf_F):

    \[\int_K f v\,dx \]

  • Dirichlet boundary RHS contribution (wf_F_dirichlet):

    \[\int_{\partial K\cap\partial\Omega} u_{\text{exact}}\,(\gamma v - \nabla v\cdot n)\,ds \]

Assembled algebraic operators

The matrix and vectors are assembled as:

  • Global operator:

    \[A = K + IA + IA^t + SA + IAn + IAn^t + SAn, \]

    with block insertions over local cell dofs and neighbor-cell dofs.

In the Vulpes implementation, \(IA^t\) and \(IAn^t\) are assembled explicitly as transpose insertions of the corresponding local face blocks (equivalently, swapping test/trial roles in those terms).

Concretely, two things must happen simultaneously to assemble a transposed contribution correctly:

  1. Local block transposition — the raw local matrix buffer is reinterpreted in row-major order instead of the default column-major order. Because Eigen stores matrices in column-major by default, passing the same data pointer with la::RowMajor to insertValues causes PETSc to read the buffer as the transpose of the column-major matrix. This transposes the local block values.
  2. Global index swap — for an off-diagonal block, the row and column dof index arrays must also be swapped. For example, IAn is inserted at global position (iin, ii) (neighbor dof rows, current-cell dof columns). Its transpose IAn^t must be inserted at (ii, iin). Transposing only the local block without swapping the index arrays would transpose the block in place at the same global position, which is not the global matrix transpose.

In summary: assembling the transpose of a local contribution requires both la::RowMajor on the data buffer and swapped row/column index arrays.

  • Right-hand side:

    \[\mathrm{rhs} = F + F_{\text{Dirichlet}}. \]

The code also builds:

  • \(M_{prj}\) (mass-like projection matrix),
  • \(\mathrm{rhs}_{exact}\) (for projection of exact solution),
  • \(\mathrm{rhs}_{const}\) (for constant-mode handling).

Periodic case handling in Vulpes

If the mesh is periodic, the discrete Laplacian has a constant null space. The implementation:

  1. Pins one dof (constant mode of cell 0) in \(A\) and sets corresponding rhs entry to zero.
  2. After solving, enforces equal mean between numerical and exact projected solutions via a dedicated routine.

This removes the arbitrary additive constant and allows consistent error computation.

Practical interpretation

The implemented form is an interior-penalty DG Laplacian where:

  • the \(bc\) coefficient reproduces the interior ( \(-1/2\)) versus boundary ( \(-1\)) consistency scaling,
  • the penalty term enforces stability and weak boundary conditions,
  • neighbor terms are assembled explicitly using face-neighbor couplings.

So the code realizes a standard IPDG/SIPG-style discretization written in a cell-wise block assembly style suitable for polygonal meshes.

4) How to run the Laplacian convergence test

The 2D convergence example is in examples/assembly/laplacian/lap_dg_2d_main.cpp. A 3D sequential version is in lap_dg_3d_main.cpp. Single-run 2D and 3D MPI examples are in examples/assembly/laplacian/MPI/.

Parameter files

Each example can read its parameters from a JSON file. Example parameter files are provided in the source directory:

examples/assembly/laplacian/lap_2d_parameters.json
examples/assembly/laplacian/lap_3d_parameters.json
examples/assembly/laplacian/MPI/lap_2d_mpi_parameters.json
examples/assembly/laplacian/MPI/lap_3d_mpi_parameters.json

The 2D parameter file looks like:

{
"order": 2,
"n_lvls": 3,
"mesh_kind": "tria",
"output_path": "output/laplacian"
}

Copy or edit one of these files to define a run.

Running

A complete JSON file or every positional CLI parameter must be supplied. The programs do not search for a parameter file automatically.

From the build directory, pass the JSON file as the first argument:

cd build
./lap_dg_2d_main /path/to/lap_2d_parameters.json

Parameters can also be supplied directly on the command line, or used to override values after a JSON path. Positional order is order n_lvls mesh_kind output_path; all four are required without JSON:

./lap_dg_2d_main 1 6 voronoi_periodic output/laplacian
./lap_dg_2d_main /path/to/lap_2d_parameters.json 1 6 voronoi_periodic

To see all options:

./lap_dg_2d_main --help

For distributed assembly and execution, see MPI Laplacian Assembly.

The supported 2D mesh families are (see the meshes tutorial 01_Meshes_2D.md):

tria
sqr
poly_meshes
kway_kmeans
kway_metis
bisect_metis
bisect_kmeans
bisect_gnn
rect_tri_periodic
rect_sq_periodic
voronoi_periodic

The supported 3D mesh families are:

tet
hexa (alias: cart)
poly

Example

For a first-order DG convergence run on the periodic Voronoi mesh sequence:

cd build
./lap_dg_2d_main ../examples/assembly/laplacian/lap_2d_parameters.json 1 6 voronoi_periodic

To save the convergence output to a text file:

./lap_dg_2d_main ../examples/assembly/laplacian/lap_2d_parameters.json 1 6 voronoi_periodic \
> lap_conv_voronoi_periodic.txt

Output

For each mesh in the selected sequence, the code prints:

  • the mesh diameter
  • the \(L^2\) error computed from the projected exact solution
  • the \(L^2\) error computed by direct integration
  • the DG error

At the end of the run a formatted convergence table is printed and saved as a CSV to output/laplacian/convergence_{dim}d_p{order}_{mesh_kind}.csv.

VTK files for mesh and solution visualization are written to output/laplacian/ with the naming convention:

lap_2d_p{order}_{mesh_kind}_{mesh_index}_mesh.vtu -- mesh only
lap_2d_p{order}_{mesh_kind}_{mesh_index}.vtu -- solution, exact, error

5) Post-processing visualization in Paraview

You can view just the mesh file in Paraview. For instance output/laplacian/lap_2d_p1_voronoi_periodic_2_mesh.vtu. This file only contains mesh information, but no solution of the Laplace problem.

mesh plot

Figure: Voronoi periodic mesh used to obtain the solution of the Poisson problem. File: output/laplacian/lap_2d_p1_voronoi_periodic_2_mesh.vtu

To view the solution, or error, use the solution file for the corresponding order and mesh, for instance output/laplacian/lap_2d_p1_voronoi_periodic_2.vtu. It outputs the numerical solution \(u_h\), the projected exact solution, and the error field.

Important remark: scalar fields are exported from Vulpes in a higher dimension grid, with submeshing, to allow appropriate view of the higher order basis space. So, you will notice the DOF number will not match the actual problem DOF, and the grid will not be the same as the original grid, as it has sub-triangulations.

mesh plot

Figure: Numerical solution for the Poisson problem using polyDG with P1 on Voronoi periodic mesh. File: output/laplacian/lap_2d_p1_voronoi_periodic_2.vtu

Note below the submeshing strategy (asking Paraview to show the wireframe), here showing the numerical solution:

mesh plot

The error is computed based on the difference of the values of the modal coefficients, that is:

  • We project the exact solution to modal DG space
  • Calculate the difference for each mode with respect to the numerical solution
  • Project this modal different to physical space on a set of sub-triangular mesh.

Error

MPI Laplacian Assembly

Only a few lines differ from the serial assembly: wrap the mesh in ParallelMesh, finalize an MPI-aware DoF numbering, and assemble owned cells. The weak form and the SparseMatrix, Vector, and LinearSolver calls remain unchanged; Vulpes uses distributed PETSc objects underneath.

geometry::ParallelMesh<MeshType> mesh([&]() { return getMesh(cfg); });
dofs::DofHandler dof_handler(mesh);
dof_handler.distributeDofs(fe_space);
dof_handler.renumberMpiSubdomainWise();
dof_handler.finalize();
for (index_t cell_id = 0; cell_id < mesh.nOwnedCells(); ++cell_id) {
// same integration and insertion code as in serial
}

From the build directory:

OMP_NUM_THREADS=2 OPENBLAS_NUM_THREADS=1 \
mpirun -n 4 ./lap_dg_2d_mpi_main \
../examples/assembly/laplacian/MPI/lap_2d_mpi_parameters.json
OMP_NUM_THREADS=2 OPENBLAS_NUM_THREADS=1 \
mpirun -n 4 ./lap_dg_3d_mpi_main \
../examples/assembly/laplacian/MPI/lap_3d_mpi_parameters.json

The MPI-specific positional order is order mesh_kind nel dof_distrib_type [mesh_file] [output_path], also used for overrides after a JSON path. Instead of n_lvls, nel selects the mesh size (cells per direction for Cartesian meshes), mesh-file index, or agglomeration size/depth. load_file requires mesh_file and ignores nel; use --help for supported mesh kinds and selectors.

Open the exported .pvtu in ParaView, keeping its per-rank .vtu pieces alongside it. It contains u_h, u_exact, and u_error. Reported errors are the globally computed, matrix-based \(L^2\) error and \(H^1\) semi-error.

Warning: Keep dof_distrib_type = 2 for export so owned-cell coefficients are locally owned, and set thread counts before launching to control threads per rank.