There are at least three different strategies for performing a discretization in time:
Use finite differences for time derivatives to arrive at a recursive set of spatial problems that can be discretized by the finite element method.
Discretize in space by finite elements first, and then solve the resulting system of ordinary differential equations (ODEs) by some standard library for ODEs.
Discretize in space and time simultaneously by space-time finite elements.
With the first strategy, we discretize in time prior to the space discretization, while the second strategy consists of doing exactly the opposite. It should come as no surprise that in many situations these two strategies end up in exactly the same systems to be solved, but this is not always the case. Also the third approach often reproduces standard finite difference schemes such as the Backward Euler and the Crank-Nicolson schemes for lower-order elements, but offers an interesting framework for deriving higher-order methods. In this chapter we shall be concerned with the first strategy, which is the most common strategy as it turns the time-dependent PDE problem to a sequence of stationary problems for which efficient finite element solution strategies often are available. The second strategy would naturally employ well-known ODE software, which are available as user-friendly routines in Python. However, these routines are presently not efficient enough for PDE problems in 2D and 3D. The first strategy gives complete hands-on control of the implementation and the computational efficiency in time and space.
We shall use a simple diffusion problem to illustrate the basic principles of how a time-dependent PDE is solved by finite differences in time and finite elements in space. Of course, instead of finite elements, we may employ other types of basis functions, such as global polynomials. Our model problem reads
Here, u(x,t) is the unknown function, α is a constant, and f(x,t) and I(x) are given functions. We have assigned the particular boundary condition (3) to minimize the details on handling boundary conditions in the finite element method.
Remark. For systems of PDEs the strategy for discretization in time may have great impact on
overall efficiency and accuracy. The Navier-Stokes equations for
an incompressible Newtonian fluid is a prime example where many methods have been proposed
and where there are notable differences between the different methods. Furthermore,
the differences often depend significantly on the application.
Discretization in time before discretization in space allows for manipulations
of the equations and schemes that are very efficient compared to
schemes based on discretizing in space first.
The schemes are so-called operator-splitting schemes or projection based schemes. These schemes do, however,
suffer from loss of accuracy particularly in terms of errors associated with the boundaries.
The numerical error is caused by the splitting of the equations which leads to non-trivial splitting
of the boundary conditions.
The discretization strategy is to first apply a simple finite difference scheme in time and derive a recursive set of spatially continuous PDE problems, one at each time level. For each spatial PDE problem we can set up a variational formulation and employ the finite element method for solution.
We can apply a finite difference method in time to (1). First we need 'a mesh' in time, here taken as uniform with mesh points tn=nΔt, n=0,1,…,Nt. A Forward Euler scheme consists of sampling (1) at tn and approximating the time derivative by a forward difference [D+tu]n≈(un+1−un)/Δt. This approximation turns (1) into a differential equation that is discrete in time, but still continuous in space. With a finite difference operator notation we can write the time-discrete problem as
for n=1,2,…,Nt−1. Writing this equation out in detail and isolating the unknown un+1 on the left-hand side, demonstrates that the time-discrete problem is a recursive set of problems that are continuous in space:
Given u0=I, we can use (5) to compute u1,u2,…,uNt.
More precise notation.
For absolute clarity in the various stages of the discretizations, we introduce ue(x,t) as the exact solution of the space-and time-continuous partial differential equation (1) and uen(x) as the time-discrete approximation, arising from the finite difference method in time (4). More precisely, ue fulfills
while uen+1, with a superscript, is the solution of the time-discrete equations
Note that, as before, N denotes the number of degrees of freedom in the spatial domain. The number of time points is denoted by Nt. We define a space V spanned by the basis functions {ψi}i∈Is.
A Galerkin method or a weighted residual method with weighting functions wi can now be formulated. We insert (8) and (9) in (7) to obtain the residual
The weighted residual principle,
results in
From now on we use the Galerkin method so W=V. Isolating the unknown un+1 on the left-hand side gives
As usual in spatial finite element problems involving second-order derivatives, we apply integration by parts on the term ∫(∇2un)vdx:
The last term vanishes because we have the Neumann condition ∂un/∂n=0 for all n. Our discrete problem in space and time then reads
This is the variational formulation of our recursive set of spatial problems.
Nonzero Dirichlet boundary conditions.
As in stationary problems, we can introduce a boundary function B(x,t) to take care of nonzero Dirichlet conditions:
In a program it is only necessary to have the two variables un+1
and un at the same time at a given time step. It is therefore
unnatural to use the index n in computer code. Instead a natural
variable naming is u
for un+1, the new unknown, and u_n
for
un, the solution at the previous time level. When we have several
preceding (already computed) time levels, it is natural to number them
like u_nm1
, u_nm2
, u_nm3
, etc., backwards in time, corresponding to
un−1, un−2, and un−3. Essentially, this means a one-to-one
mapping of notation in mathematics and software, except for un+1.
We shall therefore, to make the distance between mathematics and code
as small as possible, often introduce just u for un+1 in the
mathematical notation. Equation
(10) with this new naming convention is
consequently expressed as
This variational form can alternatively be expressed by the inner product notation:
To simplify the notation for the solution at recent previous time steps
and avoid notation like u_nm1
, u_nm2
, u_nm3
, etc., we will let u1 denote the solution at previous time step,
u2 is the solution two time steps ago, etc.
In the following, we adopt the previously introduced convention that the unknowns cn+1j are written as cj, while the known cnj from the previous time level is simply written as cnj. To derive the equations for the new unknown coefficients cj, we insert
This is a linear system ∑jAi,jcj=bi with
and
It is instructive and convenient for implementations to write the linear system on the form
where
We realize that M is the matrix arising from a term with the zero-th derivative of u, and called the mass matrix, while K is the matrix arising from a Laplace term ∇2u. The K matrix is often known as the stiffness matrix. (The terms mass and stiffness stem from the early days of finite elements when applications to vibrating structures dominated. The mass matrix arises from the mass times acceleration term in Newton's second law, while the stiffness matrix arises from the elastic forces (the "stiffness") in that law. The mass and stiffness matrix appearing in a diffusion have slightly different mathematical formulas compared to the classic structure problem.)
Remark. The mathematical symbol f has two meanings, either the function f(x,t) in the PDE or the f vector in the linear system to be solved at each time level.
We observe that M and K can be precomputed so that we can avoid computing the matrix entries at every time level. Instead, some matrix-vector multiplications will produce the linear system to be solved. The computational algorithm has the following steps:
Compute M and K.
Initialize u0 by interpolation or projection
For n=1,2,…,Nt:
a. compute b=Mc1−ΔtKc1+Δtf
b. solve Mc=b
c. set c1=c
In case of finite element basis functions, interpolation of the initial condition at the nodes means cnj=I(xj). Otherwise one has to solve the linear system
where xi denotes an interpolation point. Projection (or Galerkin's method) implies solving a linear system with M as coefficient matrix:
Let us go through a computational example and demonstrate the algorithm from the previous section. We consider a 1D problem
We use a Galerkin method with basis functions
These basis functions fulfill (19), which is not a requirement (there are no Dirichlet conditions in this problem), but helps to make the approximation good.
Since the initial condition (18) lies in the space V where we seek the approximation, we know that a Galerkin or least squares approximation of the initial condition becomes exact. Therefore, the initial condition can be expressed as
while cni=0 for i≠1,10.
The M and K matrices are easy to compute since the basis functions are orthogonal on [0,L]. Hence, we only need to compute the diagonal entries. We get
which is computed as
import sympy as sym
x, L = sym.symbols('x L')
i = sym.symbols('i', integer=True)
sym.integrate(sym.cos(i*x*sym.pi/L)**2, (x,0,L))
which means L if i=0 and L/2 otherwise. Similarly, the diagonal entries of the K matrix are computed as
sym.integrate(sym.diff(cos(i*x*sym.pi/L),x)**2, (x,0,L))
so
The equation system becomes
The first equation leads to c0=0 for any n since we start with c00=0 and K0,0=0. The others imply
With the notation cni for ci at the n-th time level, we can apply the relation above recursively and get
Since only two of the coefficients are nonzero at time t=0, we have the closed-form discrete solution
Here [D−tu]n≈(un−un−1)/Δt. Written out, and collecting the unknown un on the left-hand side and all the known terms on the right-hand side, the time-discrete differential equation becomes
From equation (22) we can compute u1,u2,…,uNt, if we have a start u0=I from the initial condition. However, (22) is a partial differential equation in space and needs a solution method based on discretization in space. For this purpose we use an expansion as in (8)-(9).
Inserting (8)-(9) in (22), multiplying by any v∈V (or ψi∈V), and integrating by parts, as we did in the Forward Euler case, results in the variational form
Expressed with u for the unknown un and un for the previous time level, as we have done before, the variational form becomes
or with the more compact inner product notation,
Inserting u=∑jcjψi and un=∑jcnjψi, and choosing v to be the basis functions ψi∈V, i=0,…,N, together with doing some algebra, lead to the following linear system to be solved at each time level:
where M, K, and f are as in the Forward Euler case and we use the previously introduced notation c={ci} and c1={cni}.
This time we really have to solve a linear system at each time level. The computational algorithm goes as follows.
Compute M, K, and A=M+ΔtK
Initialize u0 by interpolation or projection
For n=1,2,…,Nt:
a. compute b=Mc1+Δtf
b. solve Ac=b
c. set c1=c
In case of finite element basis functions, interpolation of the initial condition at the nodes means cnj=I(xj). Otherwise one has to solve the linear system ∑jψj(xi)cj=I(xi), where xi denotes an interpolation point. Projection (or Galerkin's method) implies solving a linear system with M as coefficient matrix: ∑jMi,jcnj=(I,ψi), i∈Is.
Using a Forward Euler discretization in time, the variational form at a time level becomes
The Dirichlet condition u=u0 at ∂ΩD can be incorporated through a boundary function B(x)=u0(x) and demanding that the basis functions ψj=0 at ∂ΩD. The expansion for un is written as
Inserting this expansion in the variational formulation and letting it hold for all test functions v∈V, i.e., all basis functions ψi leads to the linear system
When using finite elements, each basis function φi is associated with a node xi. We have a collection of nodes {xi}i∈Ib on the boundary ∂ΩD. Suppose Unk is the known Dirichlet value at xk at time tn (Unk=u0(xk,tn)). The appropriate boundary function is then
The unknown coefficients cj are associated with the rest of the nodes, which have numbers ν(i), i∈Is={0,…,N}. The basis functions of V are chosen as ψi=φν(i), i∈Is, and all of these vanish at the boundary nodes as they should. The expansion for un+1 and un become
The equations for the unknown coefficients {cj}j∈Is become
Instead of introducing a boundary function B we can work with basis functions associated with all the nodes and incorporate the Dirichlet conditions by modifying the linear system. Let Is be the index set that counts all the nodes: {0,1,…,N=Nn−1}. The expansion for un is then ∑j∈Iscnjφj and the variational form becomes
We introduce the matrices M and K with entries Mi,j=∫Ωφiφjdx and Ki,j=∫Ωα∇φi⋅∇φjdx, respectively. In addition, we define the vectors c, c1, and f with entries ci, c1,i, and ∫Ωfφidx−∫∂ΩNgφids, respectively. The equation system can then be written as
When M, K, and f are assembled without paying attention to Dirichlet boundary conditions, we need to replace equation k by ck=Uk for k corresponding to all boundary nodes (k∈Ib). The modification of M consists in setting Mk,j=0, j∈Is, and the Mk,k=1. Alternatively, a modification that preserves the symmetry of M can be applied. At each time level one forms b=Mc1−ΔtKc1+Δtf and sets bk=Un+1k, k∈Ib, and solves the system Mc=b.
In case of a Backward Euler method, the system becomes (26). We can write the system as Ac=b, with A=M+ΔtK and b=Mc1+f. Both M and K needs to be modified because of Dirichlet boundary conditions, but the diagonal entries in K should be set to zero and those in M to unity. In this way, for k∈Ib we have Ak,k=1. The right-hand side must read bk=Unk for k∈Ib (assuming the unknown is sought at time level tn).
We shall address the one-dimensional initial-boundary value problem
A physical interpretation may be that u is the temperature deviation from a constant mean temperature in a body Ω that is subject to an oscillating temperature (e.g., day and night, or seasonal, variations) at x=0.
We use a Backward Euler scheme in time and P1 elements of constant length h in space. Incorporation of the Dirichlet condition at x=0 through modifying the linear system at each time level means that we carry out the computations as explained in the section Discretization in time by a Backward Euler scheme and get a system (26). The M and K matrices computed without paying attention to Dirichlet boundary conditions become
and
The right-hand side of the variational form contains no source term (f) and no boundary term from the integration by parts (ux=0 at x=L and we compute as if ux=0 at x=0 too) and we are therefore left with Mc1. However, we must incorporate the Dirichlet boundary condition c0=asinωtn. Let us assume that our numbering of nodes is such that Is={0,1,…,N=Nn−1}. The Dirichlet condition can then be incorporated by ensuring that this is the first equation in the linear system. To this end, the first row in K and M is set to zero, but the diagonal entry M0,0 is set to 1. The right-hand side is b=Mc1, and we set b0=asinωtn. We can write the complete linear system as
The Dirichlet boundary condition can alternatively be implemented through a boundary function B(x,t)=asinωtφ0(x):
Now, N=Nn−2 and the c vector contains values of u at nodes 1,2,…,Nn−1. The right-hand side gets a contribution