Repository navigation
Step 7 Interpolating with the Mesh Handle
When a mesh with element-data is adapted, the data has to be interpolated to map it on the adapted mesh.
In this example we describe how to interpolate the data on a mesh using an interpolation callback.
You will find the code to this example in the tutorials/mesh_handle/t8_mesh_step7_interpolation.cxx and it creates the executable tutorials/mesh_handle/t8_mesh_step7_interpolation in your build directory.
We will start by creating a uniform hybrid hypercube mesh with the initial refinement level of 4 using handle_hypercube_hybrid_uniform_default. To use the interpolation functionality with the mesh handle, we have to define our mesh_class with the corresponding competences. For this example, we only need the most necessary ones, data_element_competences_basic and interpolate_data_mesh_competence_pack. The first one provides the functionality for element data as explained in step 5, while the second one provides the functionality needed for the interpolation.
For a deeper dive into mesh handle competences and competence packs, look into Step A of the mesh handle tutorials.
Note that the interpolation competence pack is templated on a struct holding the data that should be interpolated. In this example we only work with one double value, so the struct is simply
struct step7_element_data_type
{
double distance;
};To transfer it to the competence pack, we simply define the mesh type with t8_mesh_handle::interpolate_data_mesh_competence_pack<step7_element_data_type>:
using mesh_class
= t8_mesh_handle::mesh<t8_mesh_handle::data_element_competences_basic,
t8_mesh_handle::interpolate_data_mesh_competence_pack<step7_element_data_type>>;As in the previous example (Step 5), we will store the element data directly on the mesh. In this tutorial, we will store the distance of each element's centroid to the point (0.5, 0.5, 1) on the mesh. To learn how we store this data, go through Step 5.
When we export this initial mesh with the element data to ParaView, it should look like this:
Now we can start to adapt the mesh together with its element data. With the mesh handle, this can be done intuitively. That’s because the interpolation is just another mesh manipulation algorithm like adapt, partition,... .
The only difference is that similar to adaptation, you will need an interpolation callback function, i.e. a function that defines how exactly the data should be interpolated for each element.
In this example, we use adapt_callback_sphere from t8_mesh_tutorials_common.hxx with midpoint (0.5, 0.5, 1) to adapt our initial mesh. Elements are refined inside radius 0.2 and coarsened outside radius 0.4.
As already explained briefly above, an interpolation callback function defines how the element data is transferred from the old to the new mesh.
In our example, we define the function interpolate_callback for this
template <t8_mesh_handle::T8MeshType TMeshClass>
static void
interpolate_callback (const TMeshClass &mesh_old, TMeshClass &mesh_new, const int refine,
std::span<const typename TMeshClass::element_class> old_elements,
std::span<typename TMeshClass::element_class> new_elements);. As parameters, this function gets:
| parameter | description |
|---|---|
| mesh_old | The old mesh before adaptation. |
| mesh_new | The new mesh after adaptation. |
| refine | 1 if the element was refined, 0 if it was not touched, -1 if the family was coarsened. |
| old_elements | Span over the outgoing elements of mesh_old (the whole family on coarsening, a single element otherwise). |
| new_elements | Span over the incoming elements of mesh_new to write the interpolated data to (the children on refinement, a single element otherwise). |
In this function we need to set one interpolation rule for each of the three cases: coarsened, refined and unchanged.
These rules are implemented with 3 if statements and the functions .get_element_data() and .set_element_data() from the data_element_competences_basic.
For our example we implement the following rules:
If a family of elements is coarsened, the parent gets the average of the children's values:
if (refine == -1) {
T8_ASSERT (new_elements.size () == 1);
double sum = 0;
for (const auto &child : old_elements) {
sum += child.get_element_data ().distance;
}
new_elements[0].set_element_data (step7_element_data_type { sum / old_elements.size () });
}If the element is refined, we give the old value to each of the children.
if (refine == 1) {
T8_ASSERT (old_elements.size () == 1);
const step7_element_data_type parent_data = old_elements[0].get_element_data ();
for (auto &child : new_elements) {
child.set_element_data (parent_data);
}
}If the element is not touched, we just copy the old value onto the new element.
if (refine == 0) {
T8_ASSERT (old_elements.size () == 1 && new_elements.size () == 1);
new_elements[0].set_element_data (old_elements[0].get_element_data ());
}Once this callback function is implemented, the rest of the interpolation becomes intuitive. As in the previous steps we start by defining our adaptation parameters, then we calculate the initial element data with a loop over all elements.
After that, we set our adaption flag with .set_adapt(…), as well as the partition and ghost flags. Moreover, we set the new interpolation flag with .set_interpolate_callback(interpolate_callback<TMeshClass>).
In the end, we call .commit() to commit the mesh. This will first adapt and repartition the mesh. After that, it will automatically exchange all the ghost data. Lastly, it will interpolate the data automatically according to the repartitioned mesh.
During these steps, we also export the initial mesh (as seen above), as well as the adapted and interpolated mesh to compare the results in Paraview:
General
Step 0 Hello World
Step 1 Creating a coarse mesh
Step 2 Creating a uniform forest
Step 3 Adapting a forest
Step 4 Partition,-Balance,-Ghost
Step 5 Store element data
Step 6 Computing stencils
Step 7 Interpolation
Features
Step 2 Creating a uniform mesh
Step 3 Adapting a mesh
Step 4 Partition, Balance, Ghost
Step 5 Store Element Data
Step 7 Interpolating
Step A Mesh Competences
Prerequisites & Compilation
t8_time_forest_partition
t8_time_fractal
t8_time_new_refine
t8_time_partition
t8_time_prism
t8_time_set_join_by_vertices
Documentation
Tree Indexing
Element Indexing
Running on JUWELS using Slurm
Overview of the most used API functions
Known issues
Workflow - FreeCAD to t8code
Reproducing Scaling Results
Coding Guidelines
Contribution Workflow
CI Guidelines
Tips
Debugging with gdb
Debugging with valgrind
Development modules
Generating a code coverage report
Test driven development
Testing with GoogleTest
Writing C interface code