In examples/non_uniform_heat_equation.cpp is a DDC example implementing a forward finite-difference solver for the heat equation over a rectangle 2D domain with periodic boundary conditions and non-uniform space discretization.
As usual, the file starts with a few includes that will be used in the code.
For non-uniform discretization, differences arise compared to uniform discretization, particularly in terms of dimension labeling, domain construction, and even in the resolution of the problem.
Just like the uniform case, we start by defining types that we later use to name our dimensions.
However, we then define the discretization as non uniform.
We do the same thing for the second dimension Y
.
And for this case, we kept an uniform discretization for the time dimension.
Just like the uniform case, we define the main characteristics of the domain.
The first step to create a DiscreteDomain
with non-uniform spatial discretization is to create a C++ iterator containing the actual coordinates of the points along each dimension. For tutorial purposes, we have constructed a function generate_random_vector
that generates a vector with random data ranging from the lower bound to the higher one to populate our vectors.
For the X
dimension, we want to build 4 domains:
x_domain
: the main domain from the start (included) to end (included) but excluding "ghost" points.ghosted_x_domain
: the domain including all "ghost" points.x_pre_ghost
: the "ghost" points that come before the main domain.x_post_ghost
: the "ghost" points that come after the main domain.To do so, we have to create the iterator for the main domain.
For the initialization of ghost points in the non-uniform case, it was necessary to explicitly describe the position of the pre-ghost and post-ghost points. For the pre-ghost, it was necessary to start from the first coordinate along x and subtract the difference between the last and the penultimate coordinate of the x dimension to maintain periodicity. For the post-ghost point, take the last point and add the difference between the first and second points along the x dimension.
Then we can create our 4 domains using the init_discretization
function with the init_ghosted
function that takes the vectors as parameters.
To summarize, in this section we saw how to build a domain with non-uniform space discretization:
init_discretization
function with init_ghosted
if you need ghost points or with init
for a regular domain.For the Y
dimension, we do the same. We start by defining our 3 vectors.
And we use them to build the 4 domains in the same way as for the X dimension.
Then we handle the domains for the simulated time dimension. We first give the simulated time at which to start and end the simulation.
The CFL conditions are more challenging to achieve for the case of non-uniform discretization. Since the spatial steps are not uniform, we first need to find the maximum of the inverse of the square of the spatial step for the X
and Y
dimensions. And then we obtain the minimum value for dt
.
We can calculate the number of time steps and build the DiscreteDomain
for the time dimension.
Allocation and initialization are the same as for the uniform case. Let's focus on resolving the numerical scheme. The main difference in solving the numerical equation is that we need to account for the fact that the values of dx and dy on the left and right sides are different. We use the functions distance_at_left
and distance_at_right
to solve the equation.