On this page you find an easy example of an elliptic partial differential equation (PDE) solved with Concepts. Starting with this PDE, we derive its associated variational formulation and solve it numerically. This example is the standard setting in the Concepts tutorials where you can find many extensions to this simple code.
We want to solve the following elliptic PDE with homogeneous Dirichlet boundary conditions
with the right hand side
To solve this problem with Concepts, we first need its variational formulation. Let
Using integration by parts (Gauss theorem), this is equivalent to
where
This is the variational formulation of our problem. Let now
Let
For
with the stiffness matrix
Concepts uses the hp-finite element method to approximate the solution of a PDE, which means it calculates the Galerkin approximation of the solution in the finite dimensional subspace
where
Now let us see how to calculate the FEM-approximation of the solution of the PDE with Concepts. First we have to include some headers to use the functions of specific Concepts packages.
@skip hp2D.hh @until concepts.hh
The package hp2D
includes all functions that are needed to build a representation of the hp-finite element space in two dimensions. The meta-package concepts
includes the data structures for Concepts matrices and linear solvers, allows you to represent the right hand side via its symbolic formula and is used for the graphical output of the mesh and the solution.
The instruction
allows us to use the datatype concepts::Real
just by typing Real
.
Next we start with the actual code, consisting of a main program.
First we build the geometry, a square with one vertex in the origin, and another one in the point
Now we prescribe homogeneous Dirichlet boundary conditions to all edges of attribute
With the mesh and the boundary conditions at hand we can build the hp-finite element space.
This is a finite element space on the square, with a twice refined mesh (a single refinement of a two dimensional mesh divides an element in the mesh into four elements, so in our case we have 16 subelements in our square) and polynomial degree six on every element.
Now we define the linear form on the right hand side of the variational formulation.
In the first line we define a representation of
In the next line we calculate the vector of the right hand side using the space space
and the linear form lform
.
We compute the stiffness and mass matrices in a similar way. We first initialize the bilinear forms as concepts::BilinearForm
The first one represents the bilinear form concepts::BilinearForm
represents the mapping
Now we build the matrices using a concepts::SparseMatrix
constructor that computes the matrix belonging the space space
and the bilinear forms la
and id
, respectively.
Before we solve the linear equation, we add the mass matrix into the stiffness matrix.
We solve the linear equation using a conjugate gradient (CG) solver which is appropriate as the the system matrix
Now we want to save the information about the mesh and the values of the solution on the integration points in a MAT-file that can be read by Matlab or gnu Octave.
First we set the integration rule to the trapezoidal rule and recompute the shape functions on this points.
Then we save the information about the mesh in a MAT-file named "firstSolution.mat" and add the values of the solution on the integration points as a vector named "u".
Finally we just leave the main program.
If you compile the program and run it, you compute a FE-approximation of the solution of the PDE. In the directory where you run the program you will find a MAT-file named "firstSolution.mat". If you use Matlab you can load this MAT-file and plot the solution entering the following instructions in your Matlab command line
This gives you the following visualization of the solution
You can visualize the vertexes and the edges of the mesh entering the following instructions in your Matlab command line