## 5 Examples
### Allen-Cahn Equation
The finite difference discretization of the Laplacian results in the well-known second-order central difference stencil  
\[\Delta\phi\approx\frac{\phi_{i+1}-2\phi_{i}+\phi_{i-1}}{\Delta x^{2}}. \tag{23}\]  
The nonlinear right-hand side \(f(\phi)\) must be updated every time step using the current value of \(\phi\). As such, we do not need to perform any assembly and can even avoid forming a global system of equations. Instead, we rely on (23) for the Laplacian, which can be handily vectorized. There is also no need to perform any mapping between the reference and physical domain, as explained in Section 3.3. For the finite element discretization, we need to perform all these steps, resulting in a global nonlinear system of equations for each time step. The discrete form of Equation (22) then reads  
\[\underline{M}\partial_{t}\underline{\phi}+\underline{K}\underline{\phi}-F( \underline{\phi})=0. \tag{24}\]  
where we introduced the mass matrix \(\underline{M}\) and the stiffness matrix \(\underline{K}\) for the Laplacian. These represent the spatially discretized differential operators that act on the vector of degrees of freedom \(\underline{\phi}\). The algebraic terms that are nonlinear in \(\underline{\phi}\) are gathered in the discrete vector \(F(\underline{\phi})\). For the sake of comparison regarding efficiency, we require the resulting fields of both schemes to be (nearly) identical apart from floating point errors. Given this requirement, we note that the finite difference formulation lacks an analogous term to the finite element mass matrix. We consequently require \(M\) to be the identity matrix in an equivalent finite element formulation, given that all other terms are equal. The latter can easily be verified for a stiffness matrix assembled with first-order Lagrange polynomials and a collocation method. The derivation of such an equivalent scheme was covered in Section 3.3. Using collocated finite elements is chosen here for the sake of comparison as well as for computational efficiency. The resulting mass matrix can then be inverted trivially by taking the element-wise inverse instead of computing the full inverse. Such an operation is considerably more expensive and should thus be avoided if possible. To compare both schemes regarding efficiency, we implement both schemes from scratch within the Julia programming language [72]. Due to its flexibility, high-level syntax, and simultaneous, granular control over various performance aspects via its rich type system, Julia has gained considerable momentum in the past few years within the scientific community. We carefully set up both schemes using analogous data structures to enable a side-to-side comparison of the computational complexity. The most high-level parts of the codes are given in Listing 1. We also include the functions that are called within each time step to solve the semidiscrete system, to give a high-level view of which steps are necessary and how they are implemented in particular. Both semidiscrete systems use in-place operations to avoid memory allocations. For the CG-FEM code, we implement a full mesh topology to solve the problem with a first-order method, although both discretizations consist of Cartesian meshes. One could in this case assume a globally constant Jacobian and, thus, save a considerable amount of arithmetic complexity. However, this would skew the results regarding performance and would not make full use of the flexibility of the FEM. It becomes immediately apparent from the comparison that solving the Allen-Cahn equation using finite elements requires an assembly process that is noticeably more complex. The only arrays that need to be stored for the FD version are the grid coordinates and the solution array. Because the latter can be arranged in memory such that it represents the Cartesian topology of the grid, one can simply point to the neighbors of a vertex in memory without having to look up the vertex-vertex connectivity. This is not the case for the FEM.