Examples and tutorials » Partial differential equations » Solving PDEs on surface meshes

Solve reaction–diffusion on a triangulated unit sphere.

A surface mesh has two-dimensional cells whose coordinates lie in three-dimensional space. This example builds a closed spherical surface and solves a scalar reaction–diffusion equation on it. The same variational syntax as in a planar problem uses tangential gradients and surface measure.

The mathematical problem

Let $\Gamma=\{x\in\mathbb R^3:|x|=1\}$ be the unit sphere. Solve

\[ -\Delta_\Gamma u+u=f,\qquad f(x,y,z)=7xy. \]

The operator $\Delta_\Gamma$ is the Laplace–Beltrami operator. Since $-\Delta_\Gamma(xy)=6xy$ on the unit sphere, the exact smooth-surface solution is $u_{\mathrm{ex}}=xy$ .

Multiplying by $v\in H^1(\Gamma)$ and integrating over the closed surface gives

\[ \int_\Gamma\nabla_\Gamma u\cdot\nabla_\Gamma v\,ds +\int_\Gamma uv\,ds=\int_\Gamma fv\,ds. \]

There is no exterior boundary on which to prescribe a boundary condition. The positive reaction term removes the constant kernel of the surface Laplacian.

1. Build the surface geometry

A tetrahedral cube is centered at the origin. Its boundary is extracted with skin(), then each surface vertex is projected radially onto the unit sphere.

  Mesh volume = Mesh().UniformGrid(Polytope::Type::Tetrahedron, {nodes, nodes, nodes});
  volume.scale(2.0 / (nodes - 1));
  volume.displace(VectorFunction{-1, -1, -1});
  volume.getConnectivity().compute(2, 3);
  Mesh mesh = volume.skin();
  for (Index vertex = 0; vertex < mesh.getVertexCount(); ++vertex)
  {
    Math::SpatialPoint point = mesh.getVertexCoordinates(vertex);
    point.normalize();
    mesh.setVertexCoordinates(vertex, point);
  }

The default result has 602 vertices and 1200 triangular facets. Its topological dimension is two and its embedding dimension is three. The flat facets define a polyhedral approximation $\Gamma_h$ to the sphere. Interior points of a facet generally lie inside the sphere.

2. Define the surface problem

P1 defines a continuous field affine on each facet. Rodin uses the rectangular geometric Jacobian to evaluate the tangential gradient and the surface measure used by the weak form.

  P1 vh(mesh);
  TrialFunction u(vh);
  TestFunction v(vh);
  RealFunction source = [](const Point& point) { return 7 * point.x() * point.y(); };
  Problem reactionDiffusion(u, v);
  reactionDiffusion = Integral(Grad(u), Grad(v)) + Integral(u, v) - Integral(source, v);

The formula is assembled on $\Gamma_h$ , with the source evaluated at its integration points. Comparing with $xy$ therefore includes both finite element error and geometric approximation error. Higher geometric accuracy requires refinement or a curved representation of the mesh.

3. Solve and visualize the field

The stiffness plus reaction matrix is symmetric positive definite, so conjugate gradients is appropriate. The mesh and scalar field are exported together after convergence is checked.

  Solver::CG solver(reactionDiffusion);
  solver.setTolerance(1e-10).solve();
  if (!solver.success())
    return EXIT_FAILURE;

After the error and residual checks below pass, export the field.

  IO::XDMF xdmf("SurfaceMesh");
  xdmf.grid().setMesh(mesh).add("u", u.getSolution());
  xdmf.write().close();
Image
Computed reaction–diffusion field on the triangulated sphere. Color represents the scalar field on the original triangulated geometry.

Open SurfaceMesh.xdmf in ParaView, color by u, and select Surface With Edges to inspect the geometry. Positive and negative lobes reflect the sign of $xy$ . The surface is displayed in its three-dimensional embedding.

4. Check geometry and field refinement

The executable records the relative algebraic residual and the field error

\[ e_h=\frac{\lVert u_h-xy\rVert_{L^2(\Gamma_h)}} {\lVert xy\rVert_{L^2(\Gamma_h)}}. \]

Here $xy$ is the ambient polynomial evaluated on each flat facet. The norm is therefore taken on the actual mesh surface, using that extension of the smooth-sphere solution. It includes the effect of both field discretization and approximate geometry. Sixth-order triangle quadrature evaluates the squared-error polynomial.

  const auto& system = reactionDiffusion.getLinearSystem();
  const Real residual =
    (system.getOperator() * system.getSolution() - system.getVector()).norm() /
    std::max(Real(1), system.getVector().norm());
  constexpr size_t ErrorQuadratureOrder = 6;
  Real squaredError = 0, squaredReference = 0;
  for (auto cell = mesh.getCell(); cell; ++cell)
  {
    const auto& formula =
      QF::QuadratureFormula::get(ErrorQuadratureOrder, cell->getGeometry());
    const auto& quadrature = cell->getQuadrature(formula);
    for (size_t q = 0; q < quadrature.getSize(); ++q)
    {
      const auto& point = quadrature.getPoint(q);
      const Real reference = point.x() * point.y();
      const Real difference = u.getSolution().getValue(point) - reference;
      const Real weight = formula.getWeight(q) * point.getDistortion();
      squaredError += weight * difference * difference;
      squaredReference += weight * reference * reference;
    }
  }
  const Real relativeError = std::sqrt(squaredError / squaredReference);

An optional positive integer argument selects the number of background vertices per axis, with at least three required. The following checked sequence halves the original cube-grid spacing at each step:

Vertices per axisSurface verticesTrianglesSurface areaRelative field error
6152300 $12.27686$ $0.0940273$
116021200 $12.49211$ $0.0251292$
2124024800 $12.54768$ $0.00640798$
Image
Measured relative field error and relative surface-area error against the analytic sphere area. The dashed curve is a quadratic reference anchored at the finest field error.

The field errors decrease by factors $3.74$ and $3.92$ , approaching quadratic behavior on this sequence. The area approaches $4\pi$ . Every assembled residual is below $6.1\times10^{-11}$ after normalization by $\max(1,\lVert b\rVert_2)$ .

Run the three cases one at a time in separate directories to retain their XDMF/HDF5 output and SurfaceMesh.csv statistics. The executable checks solver success, finite measurements and a relative residual below $10^{-9}$ before writing the field.

Complete program

The program generates its mesh at startup.

See SurfaceMesh.cpp, the mesh creation guide, and the I/O guide for related operations.