1. Introduction
The numerical solution of PDEs is one of the most important problems in computational mathematics. In real-life cases, these equations serve as a mathematical model of different phenomena. To simulate them, the corresponding equations have to be solved. For this, usually, we have to perform a numerical solution procedure.
From a modeling perspective, the primary challenge in these simulations lies in defining and incorporating realistic boundary conditions. At the same time, the basis of any numerical solution is an appropriate discretization. Typically, this is applied to both spatial and temporal variables, provided that we deal with time-dependent problems.
Our focus will be spatial discretization. The computational methods include mainly the finite element [
1], finite difference (FD) [
2], and spectral or Fourier-type methods [
3]. Alternatively, in the case of simple linear differential operators, a family of mesh-free methods (e.g., the method of fundamental solutions [
4,
5]) can also be applied. However, for complex spatial domains, which is common in realistic scenarios, both Fourier-type and traditional finite difference discretizations often fall short.
Accordingly, several attempts were made to extend finite difference methods for non-uniform grids, see [
6,
7].
At the same time, in the engineering practice, for the numerical solution of PDEs, usually finite element-based discretizations are used. More precisely, one should call them Galerkin-type discretizations, which include the non-conforming or the discontinuous Galerkin-type discretizations. However, a significant drawback of this approach is its time-consuming nature. Constructing a finite element discretization involves generating a comprehensive data structure that includes a hierarchical organization of vertices, edges, and faces, along with their interrelations. Moreover, within such a framework, one should assemble all data into a large algebraic equation. In the case of (local) linear differential operators, we obtain a large linear algebraic system with a sparse matrix. Here, the full discretization (i.e., the construction of the above matrix) can take more time compared to the solution of the resulting system. This can extremely slow down the simulation procedure, especially when the computational domain changes dynamically over time, such as in moving boundary problems. Addressing these issues remains a significant challenge in computational mathematics.
A main source of the enhanced computing time is that the above finite element assembling procedure can hardly be vectorized. Accordingly, to speed up the assembling, several procedures were proposed, which are also called sequencing. In [
8], the authors introduced a vectorized algorithm for this purpose, which was then developed further in [
9] in another way to reduce computing time. Another conventional way to speed up finite element assembling is to perform parallel processing and computations on GPUs. Regarding this, several attempts were also made, see, e.g., [
10,
11]. Whether all of these results led indeed to a significant speed-up or not, they all need some extra effort. In each case, one has to build up an extra structure to support the algorithm. In concrete terms, usually, the authors employ a kind of graph coloring algorithm to partition elements into non-overlapping sets. In this way, the elemental matrices can be assembled into a global finite element matrix in parallel without conflicts. In such procedures, a suitable sparse format of the global matrix is needed [
11].
The aim of the present work is to propose a vectorized algorithm for assembling the discretization matrix of the Laplace operator in general domains.
The efficient (accurate and fast) discretization of the Laplacian operator has great importance in a number of physical models. For the distribution of electric potential, for the viscosity term in fluid dynamics, and for the modeling of irrotational flows using velocity potentials (e.g., in the case of linear wave propagation), one always makes use of this operator. Moreover, in many source-free and steady-state models, the Laplacian becomes zero. Our algorithm optimizes finite difference methods for general geometric configurations around a given grid point, enabling its application in diverse domains using only point data. We will demonstrate the performance of the proposed algorithm first for the solution of Laplace equations. Note that in a preceding work, instead of pointwise optimization, in the case of a lower-order FD approximation, we made use of a neural network [
12] to automatize the optimization. The algorithm is then integrated into the numerical solution of a Stefan problem, serving as a critical component. This classic moving boundary problem, first proposed in 1891 to model the melting of ice, has since been applied to modeling crystallization and simulating the dynamics of two-phase materials [
13]. In all cases, the evolution of the interface between the different materials or phases is determined by a jump relation between the normal components of the gradient. This becomes the key source of the dynamics, and can be derived as a conservation principle [
14,
15]. A number of studies first investigated the corresponding one-dimensional case [
16].
Regarding the realistic multidimensional model, a few simulation procedures were proposed, beginning in the 1980s [
17], and developed later [
18]. With advancements in computational capacity and renewed interest in modeling melting ice surfaces, several new simulation algorithms have been developed [
15]. Our approach is related to these by completing them with an efficient discretization procedure.
The outline of our contribution is the following. After this short introduction, we set the mathematical problem to solve. Then, the details of our pointwise optimization algorithm are given. The performance is demonstrated and analyzed in a case where the analytic solution of a Poisson problem is known. This method is then incorporated as a building block into the numerical solution of a two-dimensional two-phase Stefan problem. Here, a detailed algorithm is also given for moving the mesh in each time step. On this new mesh, the spatial discretization of the Laplacian has to be computed. Finally, the full simulation process is shown in a model problem illustrating the evolution of the grids and the interface between the two phases. Additionally, we summarize the results in a short discussion.
2. Materials and Methods
The proposed algorithm will be performed on the two-dimensional bounded domain
to solve numerically the problem
where
and
are given. A generic grid point in our finite difference method will be denoted by
.
In the following, vector quantities along with their components will be denotes with boldface characters.
2.1. Problem Statement and Mathematical Background
The Laplacian of a function will be approximated in based on the corresponding function values in its neighboring grid points. Our structured mesh points can be recognized as the vertices of a generic quadrilateral mesh. Using such a mesh, one discretize and a number of software tools are available to perform the approximation.
For a classical survey on these, we refer to [
19].
Here, the neighbors
and
of
are determined by the edges of all quadrilaterals containing
. Similarly,
and
are determined by the diagonals containing
. For a visualization of these, see
Figure 1. At the same time, we do not need the entire structure of such a mesh, including the relation of quadrilaterals, edges and vertices.
We note that within the framework of a classical finite element method, we have to perform the following steps.
- (i)
In the domain, we have to define a mesh, numbering its elements, construct a whole mesh structure, numbering the elements (now the quadrangles), and generate the corresponding adjacency matrices:
- (a)
for each node, the number of each subdomain (element) containing this node;
- (b)
for each element, the list of its nodes;
- (c)
for each element, the list of its neighboring elements.
- (ii)
By mapping a reference element to each of the physical ones, one has to evaluate the element stiffness matrices, computing Jacobians for this mapping.
- (iii)
In the assembling procedure, applying a loop, for each node, we have to collect the above contributions from the adjacent elements (given by the matrix in (a)).
To explain our approach, we consider two different discretizations of
in the grid point
. If only the data at
and
are used, we employ the approximation
Otherwise, if all the data in
are used, we change (
2) accordingly to obtain
Note that in the the presence of a uniform rectangular grid with the parameters
and
, as shown in
Figure 1, left, the optimal approximation in (
2) can be given as
so that the error becomes
, assuming that
u is four times differentiable. By changing the geometry in
Figure 1, left, in any way, this accuracy is lost.
In a similar way, using a square grid with
and all the nine-point data in (
3), the optimal approximation in (
3) becomes
In this case, provided that
, the error becomes
. For further details and extensions, we refer to [
20]. Again, for a general geometry, such as for the one shown on the right of
Figure 1, this approximation order is no longer valid.
In any case, we collect the coefficients in (
4) or (
5) into a matrix
, which is called the finite difference discretization matrix. Here, the entry
is the coefficient of
in the approximation of
, either in (
4) or in (
5). In this way,
becomes a sparse matrix with either at most 5 or 9 non-zero elements per row; see
Figure 2. In the case of regular geometries and homogeneous boundary conditions,
is a positive definite (or semidefinite) symmetric one.
To compare the above cases, in
Figure 2, we also give the position of the non-zero elements and the total number of non-zeros (denoted with
nz) in the matrices
corresponding to the discretizations in (
4) and (
5), respectively.
The conventional way to find optimal parameters in (
2) and (
3) is to use Taylor expansion and determine the coefficients to obtain the desired approximation order. In a general case, this approach completely fails since then the accuracy in (
4) and (
5) are not available and for lower-order accuracy, multiple solutions exist. In this way, we somehow have to optimize the coefficients in (
2) and (
3).
In a previous study [
12], instead of pointwise optimization procedures, a neural network was utilized, which “learns” this process and can quickly compute the corresponding coefficients. That study focused on the five-point approximation in (
4). With the matrix
at hand, the final form of the FD discretization of the problem in (
1) is the linear system
where
denotes the discretization of the source term
f with
. The solution
is then called a finite difference approximation of
u in (
1).
2.2. The Pointwise Optimization
To achieve possibly the highest-order approximation in (
2) and (
3), these should hold with equality for some polynomials until a certain order.
For example, if we have a uniform mesh with the grid size
h and the coefficients are scaled as
, then to obtain an approximation order
, the equality
should hold for all polynomials
p up to order
N. Henceforth, we focus to this approximation with nine grid points.
At the same time, for general mesh geometries, the approximation order is decreased and, accordingly, (
7) is not valid any more for all polynomials up to order
N. The principal problem here is to determine them, which should be taken into account in (
3). Also, we demand that the right-hand side of (
3) is zero for any constant function. This immediately gives the constraint
for the coefficients in (
3). To satisfy (
7), we will first consider the set of polynomials
which span the space of first- and second-order ones. Additionally, we take
which are the harmonic polynomials up to order five. To motivate this choice, we recall that in a number of real-life cases, we have to compute with harmonic functions, such that
. In other words, we can only have a certain accuracy for approximating the solution of
, if we can perform it in the case of
. In this way, an approximation in (
5) has the form
Performing it for all polynomials
above, for the geometrical setup given with
, we obtain the following optimization task:
Find the vector of coefficients
in (
9) such that
, where
- -
with ;
- -
, so that .
Using the coefficient 100 in the first-order polynomials
and
and the coefficient 10 in the second-order ones
and
in (
8), we force more accuracy in the case of the lower-order polynomials. This procedure significantly improves the approximation in (
9).
The optimization task in (
9) is solved in the least-square sense for each point
using the
np.linalg.lstsq subroutine in Python. This can be performed for all grid points independently. In this way, we can easily vectorize the procedure in the following way:
We create a matrix, where each row contains the geometry around a fixed grid point.
The optimization procedure is applied (independently) to the rows of this matrix using the np.apply_along_axis subroutine in Python.
Accordingly, this final step could also be executed with parallel processing.
2.3. Numerical Experiments
We have first tested the performance of the above optimization task on a fixed domain.
2.3.1. The Model Problem
We investigate the numerical solution of the test problem
where the computational domain is given with
such that
on the left,
on the right,
on the bottom and
on the top of
.
We will compare the numerical solution with the analytic one .
2.3.2. Spatial Discretization
We utilize a scale of non-uniform structured grids in
. In each case, the vertical positions of the grid points will be uniformly distributed, while in the horizontal direction, we fix only the number of grid points. Here, the grid spacing is refined in the vicinity of the top boundary. This is a usual situation in the case of modeling water waves, as the dynamics are primary influenced by the motion of the free surface. Two different grids are shown in
Figure 3.
2.3.3. Numerical Results
To validate our method for approximating the Laplacian, we evaluate the accuracy of the numerical solution of (
10). Here, we measure the accuracy in the discrete
-norm depending on the number of grid points. The results, displayed in
Table 1, clearly demonstrate the efficiency of the proposed vectorized discretization method. For a relatively fine grid with 2420 grid points, the construction of the FD discretization matrix
took approximately one-sixth of a second.
2.4. Application to the Numerical Solution of the Stefan Problem
We introduce the Stefan problem and specify the boundary and initial conditions we apply. Then, the simulation algorithm is given in details. Finally, we present and discuss the results of the numerical experiments.
2.4.1. General Setting
Stefan problems have been used as a mathematical model of different physical phenomena. In a number of applications, this describes the interaction of different phases or materials. This can be a dissolution of a chemical compound into an alloy [
18], the solidification of a binary alloy [
21], a chrystallization process [
22] or just the melting and freezing of ice [
23].
Taking a minimal model, the unknown function
in the Stefan problem corresponds to the temperature in a melting system [
24].
At each time
, the domain
is separated into the two disjoint ones
so that
. Here,
represents the domain filled with ice, while
represents the domain filled with water. Accordingly, the set
gives their common boundary.
We also introduce the notation
for the solutions on the subdomains above, where, for the sake of simplicity, we omit the time parameter
t. With these, the governing equations can be given as
Here,
and
are the diffusion coefficients in the corresponding domains, where usually
. The evolution of the common boundary is given by
where
is considered as a level set parametrized with
t and
denotes the latent heat of solidification [
25]. Physically, this gives the speed of the interface between the ice and liquid water. The symbol
is for the jump operator, which is defined on the common boundary
with
where
and
denote the corresponding (opposite) outward normals.
Note again that depending on the model, the unknown could be the concentration of some chemical compound or the degree of crystallinity. Also, in a more involved model, multiple phases (even for ice [
23]) or a separate nucleation process [
22] may occur.
2.4.2. Specific Initial and Boundary Conditions
The simulation will be initiated using the initial condition
During the time steps, the top boundary condition is set to be
such that, initially, it is given by
We also set up the bottom boundary condition as
which, initially, takes the value
The right and left boundary conditions are homogeneous Neumann-type, as
Note that beyond to this specific case, the algorithm will be given in case of general Neumann-type boundary data.
2.4.3. Construction of the Grid
In order to track the evolution of the computational domains and , we apply a structured quadrilateral grid. Since the mesh is moving only vertically, we fix the x coordinates of the grid points. Their number—in the interior—is denoted with such that the horizontal grid spacing is . To incorporate the Neumann boundary conditions on the left and right, we also include a ghost grid point near to both the left and the right boundaries.
We also fix the number of interior grid points in the vertical direction; this is
both in
and
. Additionally, we place grid points on the fixed top and bottom boundaries as well as to the moving interface. According to (
2) and (
3), we apply a row-wise numbering for the grid points. To visualize the notations above, the grid point structure and the numbering are shown in
Figure 4.
During the simulations, the interface evolves, requiring adjustments to the vertical positions of the grid points. For a fixed horizontal position, we choose a uniform vertical mesh. In this way, regarding the computing time, generating a new grid is a rather cheap operation. At the same time, the vertical distribution of the grid points is non-uniform. In the vicinity of the interface
, we apply a finer grid. This refinement allows for a more accurate approximation of the evolution of
. In concrete terms, for computing the jump terms (
12), a fine grid will make our interpolation procedure more accurate. Note that a similar procedure can be used if free surface water waves are simulated [
26].
2.5. Principles of the Algorithm for Solving (11)–(13)
The construction of the main building blocks of our algorithm follows the classical setting to solve moving boundary problems. Accordingly, the sketch of our algorithm is the following, where the main steps are repeated to simulate the evolution of the heat distribution.
- (i)
We have a diffusion problem both on the top domain and on the bottom domain with some initial data.
- (a)
We construct the discretization of the Laplacian using the pointwise optimization algorithm in
Section 2.2.
- (b)
We incorporate the Dirichlet- and Neumann-type boundary conditions in
Section 2.4.2.
- (c)
We perform a time step using discrete problems on the two subdomains.
- (ii)
We have the new Neumann boundary data on the moving surface.
- (a)
We move the surface according to Equation (
13).
- (b)
We interpolate the solutions on the two new domains.
2.5.1. STEP (i) (b)
We now discuss step (i) (b). According to the implicit Euler time discretization, we have the general scheme
where the matrix
contains data from the virtual outward points. To incorporate them into the scheme, we will use the given Neumann boundary data. To simplify the notations, the algorithm will be presented on the left side of the computational domain.
Here, a generic interior node has the index
K and its left virtual neighbors have the indices
and
. Yielding the components of matrices and vectors in the subscript, and using the notation
we can express the component
K of the product
as
Here, we have to eliminate the sum
Using the second-order approximations
we can rewrite (
17) as
such that we can eliminate the sum in (
18). Accordingly, we perform the following:
on the right-hand side of (
16), we have to add
times the first three terms in (
19) modifying
;
we have to modify the matrix elements
and
by adding the entries
and
, respectively, corresponding to the virtual grid points as shown in the next three terms in (
19).
Additionally, we have to incorporate the Dirichlet boundary conditions. Regarding the common boundary, the situation is easy: we do not have to change anything except for leaving the virtual points located here. On the top of the top domain, we have to collect the Dirichlet data
associated with the interior point with index
K. Then, on the right-hand side of (
16), we have to keep
modifying with this the source term
again, while the rest has to be moved to the left-hand side. The new source term is denoted with
.
Performing a similar process on the bottom face of the bottom domain, we finally obtain the linear system
where
contains only entries corresponding to interior elements, which are modified according to the Neumann boundary data in (
17).
Similarly, the new source term, according to the incorporation of Neumann and Dirichlet data, is denoted with .
2.5.2. STEP (i) (c)
For performing a time step, we only have to compute the numerical solution of the system in (
20). Here, we simply use the built-in linear solver of Python. It is crucial that we apply implicit time stepping. In this way, we can maintain the stability of the method.
2.5.3. STEP (ii) (a)
We denote by the approximation of . As we have a quadrilateral mesh, becomes a broken line. It is determined by a number of points such that their horizontal coordinates are fixed and uniformly distributed.
According to (
13), we can approximate the evolution of
at each point
using the first-order approximation of (
12) as
which also defines a time stepping. Observe that using only (
21) for the given grid points results in general points on
, which do not necessarily have
x-coordinates fitting to the mesh,
Figure 4.
Before dealing with this problem, we should first compute
, for which we need the gradients
. With these, for the jump term in (
21), we have
Another chief problem is that function values are given in the grid points, where the outward normal of is not defined.
Instead, to move each broken line segment of
, we compute the normal vector
in the midpoint of these sections. These are given as
, see
Figure 4. We have to approximate the gradients here, which is performed in the following way.
We first develop a bilinear interpolation of the numerical solution on the quadrilateral with the vertices
lying on the top of the zero level set as follows.
where the coefficients
are obtained by solving a linear system. Shifting the origin to
and evaluating (
22) here, we first observe that
. Similarly, relating point values and the right-hand side of (
22) at the points
, we obtain the equalities
where the number in the bracket of the right-hand side yields the vector component. Similarly, we interpolate the numerical solution on the quadrilaterals lying on the bottom of
to obtain
Corresponding to the previous case, we obtain
Then, by (
22) and (
23), we obtain
Next, we move the segment according to (
21). That is, we compute
using (
24), (
25) and shift the segment between
and
with
in (
26).
In this way, applying it on two neighboring segments between
and
and between
and
, we have two new vertices of
on the fixed vertical lines. Therefore, instead, we take their average, which gives the location of the interface
on the corresponding vertical line, as shown in
Figure 4. Also, the main steps of the procedure in step (ii) (a) are shown in the block diagram in
Figure 5.
2.5.4. STEP (ii) (b)
Finally, we have the new grid points of
and the fixed ones on the bottom and the top of
. Between these, we again generate the new grid points according to
Section 2.4.3 such that the consecutive vertical distances follow a geometric series.
On the top domain
, on a fixed vertical line, we then have the new grid point
of the interface
(see
Figure 4) and the generated new ones
Also, we have the approximations
Accordingly, in the new points
, we use the linear interpolation between the values in (
27), replacing the values
, which constitutes the starting point of our new time step from
to
.
2.5.5. Numerical Results
Our model problem represents the physical scenario of a melting volume with heat applied from the bottom. This is modeled using positive Dirichlet boundary conditions on the fixed bottom of . Additionally, negative Dirichlet boundary conditions are applied to the top surface of to account for the outward heat flow due to a negative temperature gradient.
The initial condition and the corresponding initial grid points are shown in
Figure 6. The regions and grid point corresponding to positive temperature are depicted with red and those with negative temperature with blue color. The discretization of the interface
is defined by the green grid points.
In practical scenarios involving the modeling of ice layer melting, the characteristic horizontal dimension is often significantly larger than their average thickness. In this way, their interface becomes flat and horizontal [
23], corresponding to the numerical experiments in this manuscript.
Elapsing the time, the material in
is melting such that the zero temperature level is moving upwards. This is also shown in the numerical simulations in
Figure 7. Since we applied homogeneous Neumann boundary conditions, starting from the left and right boundary, the front will be flattened. Also, by the warm-up both from the bottom and the top boundary, corresponding to the boundary conditions in (
14) and (
15), the “cold” region shrinks significantly.
The simulation data are summarized as follows.
Diffusion coefficients:
- -
on the top domain;
- -
on the bottom domain.
Number of interior grid points:
- -
—horizontal;
- -
—vertical: 24 + 24 interior points taking twice the common interface.
Time step: .
Number of time steps: 30.
Simulation time of one step: 1.05 s.
Software and hardware: Python code on a Laptop with Intel i7-6700 processor with 2.6 GHz.
3. Discussion and Conclusions
An optimization-based FD discretization of the two-dimensional Laplace operator was developed. The core of the optimization procedure was the least-square solution of an overdetermined system, where a special weighting of the corresponding equations was utilized. The pointwise execution of this procedure provides a vectorized alternative to the conventional assembly procedure.
The use and benefits of this algorithm are demonstrated on the Stefan problem, which is a mathematical model of the dynamics of two-phase materials with a moving interface. With this, the computational domains also evolve with time. Consequently, at each time step of the numerical simulations, a new spatial discretization must be performed, making the speed of this process critical to the overall efficiency of the numerical method. The proposed algorithm is notably fast, relying solely on neighboring relationships between grid points. Furthermore, it is effective on non-uniform and non-rectangular grids, combining the strengths of traditional finite difference and finite element methods.
The primary challenge in such simulations is that the interface often becomes unstable. This instability can lead to spurious oscillations on the free surface,
, which may grow over time and ultimately disrupt the simulation. To mitigate this issue, several stabilization techniques have been proposed, see [
18]. To validate our approach in this context, the choice of a flat, common interface proves to be highly suitable.
Also, the algorithm proposed here is rather quick and uses only neighboring relations between the grid points. At the same time, it also works on non-uniform and non-rectangular grids, merging the advances of classical finite difference and finite element methods. Note that using classical finite element on such grids implies only first-order convergence [
27].
The main application field of this new algorithm is the family of problems with moving boundaries or interfaces, including the more complex Stefan problems mentioned at the beginning of
Section 2.4.1. Another very important family of such problems arises by modeling free-surface water waves. As the waves evolve, the domain filled with water will change step-by-step. When forecasting tsunamis [
28] in real time, it is highly important to reduce the computing time to an affordable level for these 3-dimensional problems.
The disadvantage of the method is that the optimization procedure highly depends on the weighting, which was applied for the harmonic polynomials to obtain the optimal coefficients. Another related problem is that in the proposed algorithm, we cannot guarantee a fixed spatial convergence (or consistency) order. For irregular geometries, even the optimal coefficients will not deliver a very accurate approximation of the differential operator.
A natural extension of this work is to apply the method for approximating the surface Laplacian (or Laplace–Beltrami operator), a significant topic in current research. Also, the principle in
Section 2.2 is applicable for any non-linear differential operator
D. For this, we have to modify the overdetermined system in the optimization step, where on the left-hand side of (
7), we have to apply
D.