In this example we will solve the heat equation with Dirichlet boundary condition
\begin{eqnarray*} - \partial_t u - \Delta u &=& f &\text{ in } \Omega \times (t_{begin}, t_{end}) \\ u &=& g &\text{ on } \Gamma \times (t_{begin}, t_{end}) \\ u &=& u_0 &\text{ on } \Omega \times t_{begin} \end{eqnarray*}
whereby \( \Omega \subset \mathbb{R}^d, \quad 1 \le d \le 3 \) denotes the domain and \( \Gamma = \{ x \in \partial \Omega | \exists i \in \{1, ..., d\}: x_i = 0 \} \) a subset of its boundary. We solve the problem in the time interval \( (t_{begin}, t_{end}) \) with Dirichlet boundary conditions on \( \Gamma \) and set
\begin{eqnarray*} f(x,t) &=& -1 \\ g(x,t) &=& 0 \\ u_0(x) &=& 0. \end{eqnarray*}
In this example we use the implicit Euler scheme
\[ \frac{u^{new} - u^{old}}{\tau} - \Delta u^{new} = f(\cdot, t^{old} + \tau) \]
where \( \tau = t^{new} - t^{old} \) is the timestep size between the old and new problem time. \( u^{new} \) is the searched solution at \( t = t^{new} \), \( u^{old} \) is the solution at \( t = t^{old} \), which is already known from the last timestep. If we bring all terms that depend on \( u^{old} \) to the right hand side, the equation reads
\[ \frac{u^{new}}{\tau} - \Delta u^{new} = - \frac{u^{old}}{\tau} + f(\cdot, t^{old} + \tau). \]
We will now look at the source code of the example in detail. You can also find the complete source code for all files below.
First we include the required AMDiS headers. AMDiS.hpp
is a collection of headers that are commonly used. In this example we also use the ProblemStat
and ProblemInstat
classes, which we explicitly include. We will also use the predefined operators in LocalOperators.hpp
.
On the following lines we define the grid manager and finite element space used in this example - a structured axis-aligned cube grid with nodal (lagrange) basis functions of order 2. We set name aliases for ease of notation for the basis and problem types. Note that GRIDDIM
is a constant passed by the buildsystem. By default we build the example for \( GRIDDIM = 2 \).
Most AMDiS classes and functions are found in the AMDiS namespace. To make life easier for us we therefore tell the compiler to look for our classes in this namespace.
Our example begins with a call to the main
function. Any command line argument we pass can be used with the argc
and argv
arguments. As every AMDiS program requires us to set up an Environment
at the start we do that here.
Now we create the stationary base problem using the ProblemStat
class, as well as the instationary problem using the ProblemInstat
class and initialize both. Note that we name both problem instances heat and initialize everything. We also set up an AdaptInfo
object that controls the adaptation procedure later on.
Next we add the operators that define our problem. Note that we store references to the inverse of the time step size invTau
and the last solution prob.solution()
so we use the correct values every timestep. See the operator reference page for descriptions of the operators used here.
Lastly we set the constraints: We have dirichlet boundary conditions on the left and lower part of the boundary, as indicated by the predicate function and prescribe a value of zero.
With everything set up we can now create a helper object AdaptInstationary
that performs the time-stepping scheme automatically using the parameters given by the AdaptInfo
object we created earlier. We only need to call the adapt()
function.
With that the example ends. We return 0 to indicate a successful run.
We describe the options used by the example's parameter file heat.dat.2d
. For a full description of all options see the reference page.
Parameters for the grid "heatMesh":
heat->mesh
specifies the name of the grid used by the problem named "heat".
Parameters for the solver employed by the problem "heat":
Parameters for the output generated by the problem "heat":
Parameters for the time stepping scheme:
Using a console we navigate to the examples directory where the heat.2d
file resides and run the file while also passing the necessary arguments.
Running heat.2d
in bash could look like this:
In this section we show some sample output of heat.2d
using the default arguments.
The example produces a series of output blocks like the one below for each time step. In each of them the current simulation time and time step size is printed before the inner iteration begins. Some performance information is printed while the iteration is in progress. After one step the inner iteration finishes and an output file is generated.
time = 0.1, timestep = 0.1 begin of iteration number: 1 ============================= markElements needed 7.7e-08 seconds 4225 local DOFs fill-in of assembled matrix: 66049 assemble needed 0.0135029 seconds === GeneralizedPCGSolver === rate=0.395649, T=0.00769608, TIT=0.000481005, IT=17 solution of discrete system needed 0.0489367 seconds end of iteration number: 1 ============================= writeFiles needed 0.00137441 seconds ...
Using ParaView we can inspect the output file and obtain a visualization that looks like this.
The complete source code for the example and the parameter file can be found here: