4.2.1. Continuous basis functions, spatial discretization and solution procedure
Until now, we always have allocated the unknown function \(u\) with define_scalar_field("u","C2"). While the first argument is just the name, the second argument needs more elaboration. It defines the finite element space where the function is defined upon. In pyoomph, there are two kinds on continuous spaces, namely "C1" and "C2". Discontinuous spaces "D0" (constant per element) and "DL" (affine linear per element, cf. Section 4.4.8) are also available, but not discussed here. To understand these two continuous spaces, let us delve slightly into the basics of the finite element method. In fact, the unknown function \(u\) is spatially discretized by so-called shape functions \(\psi_l(\vec{x})\).
This linear expansion in the shape functions obviously separates the spatial dependency of the function \(u\) into amplitudes \(u_l\) and spatially varying basis functions \(\psi_l\). The second argument in define_scalar_field() selects the particular choice of these shape functions \(\psi_l\), namely "C1" for linear basis functions and "C2" for basis functions of second order, i.e. quadratic ones.
In principle, the basis functions are quite arbitrary. In fact, one could select e.g. \(\psi_l(\vec{x})=\exp(i\vec{k}_l\cdot{x})\) to obtain a Fourier decomposition. However, since the conventional finite element method is solved in the spatial domain, not in the spectral one, this choice of the basis functions is problematic. Instead, it is beneficial to choose them that the support only covers a few neighboring elements, i.e. they should be zero almost everywhere in the entire domain, except in the vicinity of a single position. Thereby, the degrees of freedom, i.e. \(u_l\), are sufficiently localized in space. This idea leads to the conclusion that the basis functions should be defined in a piece-wise manner - zero almost everywhere, but non-zero in the vicinity of a position \(\vec{x}_l\) associated with the degree of freedom \(u_l\).
The simplest idea on a one-dimensional domain, discretized by positions at \(x_1<x_2<\ldots <x_n\) is hence to use linear slopes, i.e. the \(l\)-th basis function corresponding to the point \(x_l\) reads
where the missing points \(x_{0}\) and \(x_{n+1}\) are not required if \(x\) is confined to the range of the domain, i.e. between \(x_1\) and \(x_l\). The basis functions are shown for a 1d mesh in figure Fig. 4.9.
Fig. 4.9 Top: linear basis functions ("C1"). Bottom: quadratic basis functions ("C2"), where the dashed parabolas are the shape functions at the intermediate nodes in the center of each element.
These functions are in fact used if the space "C1" is used. Let us see how the weak form of the Poisson equation (4.6) in one spatial dimension reads with these basis functions:
The derivative of \(u(x)\) is not required, just the derivative of the shape functions \(\psi_l\), which can be calculated easily except on \(x_{l-1}\), \(x_l\) and \(x_{l+1}\), where the derivative is not defined due to the piece-wise nature of the chosen basis functions. However, these points are are null set with respect to the spatial integration \(\left(.,\,.\right)\) so that it is not required to consider these points within the integration.
Finally, we have not yet addressed the test function \(v\). As mentioned before, the above weak form has to hold for all (quite arbitrary) choices of \(v\). In the discretization (4.8), we have used \(n\) unknows \(u_l\) (for \(l=1,\ldots,n\)). So to get a discretized system of equations, we should choose \(n\) linear independent test functions \(v_k\) (with \(k=1,\ldots,n\)). Furthermore, we have to make sure that the mass matrix \(\mathbf{M}=(M_{lk})=(\phi_l,v_k)\) has a full rank. If the \(l\)-th row of this matrix is entirely zero, it means that we have selected our \(n\) test functions \(v_k\) in a manner that there is no support for the degree of freedom \(u_l\). Thereby, we would not obtain a discretized equation for this degree of freedom.
The trivial choice of the test functions is the Galerkin method, where we just take the same basis functions, i.e. \(v_k=\psi_k\). Thereby, both requirements on the test functions hold automatically. The Poisson equation hence reads
Due to the reasonable choice of our basis functions (and hence test functions), the integrands are zero almost everywhere except for the neighborhood of the corresponding point \(x_k\). Thus, the spatial integrals can be restricted to the support of each \(\psi_k\):
The Neumann flux terms appear only at the boundaries for \(k=1\) and \(k=n\), which is indicated by the Kronecker deltas.
The next benefit of the choice of the basis functions is that also \(\psi_l\) are zero almost everywhere, i.e. the sum over \(l\) can be simplified depending on \(k\). This eventually gives the system of equations
where the integrals \((.\,.)\) only have to be carried out over a small section of the entire domain where both arguments are non-zero. Denoting the symmetric stiffness matrix \(\mathbf{K}=(K_{lk})=(\partial_x \psi_l,\partial_k \psi_l)\), the vector of degrees of freedom \(\vec{u}=(u_l)\) and the vector comprising the right hand side \(\vec{b}\), one can rewrite this as matrix equation
In principle this equation can be solved by inverting \(\mathbf{K}\), but due to the pure Neumann conditions and the shift-invariance \(u_l\to u_l+\text{const}\) of the Poisson equation, \(\operatorname{det}(\mathbf{K})\) is actually \(0\). We have discussed how one can overcome this issue in Section 4.1.5.
Let us now see how Dirichlet boundary conditions are treated in discretized equations. When we impose a Dirichlet condition at the right side, i.e. we set \(u(x_n)=a\), it means that the amplitude \(u_n\) of the expansion (4.8) is fixed by \(u_n=a\). Hence, it is not an unknown anymore. Therefore, we just remove the \(n\)-th equation from the system (4.9). Removing this equation will automatically remove all connection to the Neumann flux at the right side, i.e. \(j_\text{N}(x_n)\). This reflects the fact that one can either impose a Dirichlet or a Neumann condition at the boundaries. Imposing both simultaneously, i.e. a Cauchy condition, is not feasible (cf. Section 4.1.7). However, when this equation is removed, \(\mathbf{K}\) will become invertible, i.e. \(\operatorname{det}(\mathbf{K})\neq 0\). Furthermore, in the \((n-1)\)-th equation, the occurrence of \(u_n\) can be replaced by \(a\), which connects the entire system to the value of the Dirichlet condition. A unique solution is now feasible and depends on the value \(a\).
This is exactly what happens internally in pyoomph when a one-dimensional Poisson equation on the space "C1" is solved. First of all, the mesh is constructed with a storage of \(u_l\) at each node located at \(x_l\) for \(l=1,\ldots,n\). Then, the Dirichlet boundary condition at \(x_n\) is applied setting the value of \(u_n=a\) and removing it from the system. Finally, the spatial integrals over the shape functions are carried out numerically (using Gauss quadrature, cf. Section 13.1) and the resulting system is solved with a linear solver back-end. We will discuss this more generally in the next section.
At the end of this section, we still have to elaborate on the space "C2" and how the shape functions are defined in higher spatial dimensions. With the second order basis functions, i.e. "C2", the approximated discretized solution is not piece-wise linear, but piece-wise quadratic. Since a parabola requires three points \((x,u)\) to be uniquely defined, additional points \(x_{l+1/2}\) are introduced between each interval \(x_l\) and \(x_{l+1}\), each of them associated with an own degree of freedom \(u_{l+1/2}\). The basis functions \(\phi_l\) and the test functions \(v_k=\phi_k\) are now quadratic, but the rest of the approach is essentially the same, except that also the new degrees of freedom at \(l+1/2\) have to be added to the discretized system. The quadratic basis functions of the space "C2" for a 1d mesh are also depicted in Fig. 4.9.
In higher dimensions, we limit ourselves to plot the basis functions, which are plotted for 2d elements in Fig. 4.10 and Fig. 4.11. In three dimensions, it is generalized analogously.
Fig. 4.10 First order shape functions ("C1") on triangular and quadrilateral elements.
Fig. 4.11 Second order shape functions ("C2") on triangular and quadrilateral elements.