General concepts » A first problem

Build, run, and inspect a complete Poisson solve.

This guide turns the mesh-and-space setup from First steps into a complete application. It uses the same executable as the Poisson tutorial, which develops the weak form and explains the code step by step.

The problem being solved

On the unit square $\Omega=(0,1)^2$ , the scalar field satisfies

\[ -\Delta u = 1 \quad\text{in }\Omega, \qquad u=0 \quad\text{on }\partial\Omega. \]

Continuous, piecewise linear finite elements approximate the field on a mesh of 256 vertices and 450 triangles. The boundary values are prescribed; the solve determines the interior field.

The application source

Save Poisson.cpp as poisson.cpp. The mesh is scaled onto the unit square before the problem is assembled. The solver status is checked before the solution is exported.

Build and run

Save this CMakeLists.txt alongside poisson.cpp. The IO target is needed for XDMF and its HDF5 data files.

cmake_minimum_required(VERSION 3.16)
project(PoissonExample LANGUAGES CXX)

find_package(Rodin REQUIRED)

add_executable(poisson poisson.cpp)
target_compile_features(poisson PRIVATE cxx_std_20)
target_link_libraries(poisson PRIVATE
  Rodin::Geometry
  Rodin::Assembly
  Rodin::Variational
  Rodin::Solver
  Rodin::IO
)

Configure and build from the application directory.

cmake -S . -B build
cmake --build build

Run from a separate directory to keep generated data together.

mkdir -p results
cd results
../build/poisson

A successful run writes Poisson.xdmf and its referenced HDF5 files into results. A failed solver returns EXIT_FAILURE before export.

Inspect the solution

Open Poisson.xdmf in ParaView, select Apply, and color the mesh by u. The boundary values should be zero and the interior values positive. On this mesh the computed nodal maximum is approximately $0.072862$ .

The visualization section of the tutorial shows the computed field and explains how to display it as a surface. The XDMF file describes the data; the HDF5 files contain it, so the files must remain together.

Modify one part of the problem

Once the baseline solve runs, change one ingredient at a time. The weak form, boundary data, and solver must remain compatible.

Prescribed nonzero values

Replace the zero boundary data with a function $g(x,y)=x^2$ .

RealFunction g([](const Point& p) { return p.x() * p.x(); });
poisson = Integral(Grad(u), Grad(v)) - Integral(f, v)
           + DirichletBC(u, g);

Selected boundary regions

DirichletBC(u, Zero()).on(boundaryAttribute) restricts the condition to boundary entities carrying that attribute. The mesh must already have the intended boundary attributes before the condition is constructed. Boundary regions without prescribed values retain the natural condition implied by the weak form. See Boundary conditions.

A spatially varying source

Replace the constant source with a coordinate-dependent function, for example $f(x,y)=xy$ .

RealFunction f([](const Point& p) { return p.x() * p.y(); });

Rebuild the problem expression using this source before solving it.

Solver controls

For this symmetric positive definite problem, CG exposes an iteration budget and a tolerance.

CG solver(poisson);
solver.setMaxIterations(1000).setTolerance(1e-10).solve();

The solver settings are selected according to the algebraic convergence requirements of the discretization. Check solver.success() after the solve. A direct alternative is SparseLU; a standalone solver can also be passed to poisson.solve(solver). The solver guide explains the supported interfaces.

Continue learning

Read the Poisson tutorial for the derivation and the role of each form-language expression. Then continue with Core concepts or linear elasticity for a vector-valued problem.