Differences between meshes#

Section author: Feliks Kiszkurno (Helmholtz Centre for Environmental Research GmbH - UFZ)

This example explains how to use functions from meshlib to calculate differences between meshes.

from ogstools.meshlib import difference, difference_matrix, difference_pairwise
from ogstools.meshplotlib.examples import meshseries_THM_2D
from ogstools.propertylib import presets

mesh_property = presets.temperature

0. Introduction#

This example shows how to calculate differences between meshes. It is possible to calculate the difference between multiple meshes at the same time. Multiple meshes can be provided either as list or Numpy arrays. 4 ways of calculating the difference are presented here.

1. Difference between two meshes#

The simplest case is calculating the difference between two meshes. For this example, we read two different timesteps from a MeshSeries. It is not required that they belong to the same MeshSeries object. As long, as the meshes share the same topology and contain the mesh_property of interest, the difference will work fine.

mesh1 = meshseries_THM_2D.read(0)
mesh2 = meshseries_THM_2D.read(-1)

The following call will return a mesh containing the difference of the mesh_property between the two provided meshes:

\[\text{Mesh}_1 - \text{Mesh}_2\]
mesh_diff = difference(mesh1, mesh2, mesh_property)

This returned object will be a PyVista UnstructuredGrid object:

print(f"Type of mesh_diff: {type(mesh_diff)}")
Type of mesh_diff: <class 'pyvista.core.pointset.UnstructuredGrid'>

2. Pairwise difference#

In order to calculate pairwise differences, two lists or arrays of equal length have to be provided as the input. They can contain an arbitrary number of different meshes, as long as their length is equal.

Consider the two following lists:

\[\begin{split}\text{List}_{1} = \begin{bmatrix} A_{1} & B_{1} & C_{1}\\ \end{bmatrix}\end{split}\]

and

\[\begin{split}\text{List}_{2} = \begin{bmatrix} A_{2} & B_{2} & C_{2}\\ \end{bmatrix}\end{split}\]

The output array will contain following differences between meshes:

\[\begin{split}\begin{bmatrix} A_{1}-A_{2} & B_{1}-B_{2} & C_{1}-C_{2}\\ \end{bmatrix}\end{split}\]

and will have the same shape as the input lists. As in the example below:

meshes_1 = [mesh1] * 3
meshes_2 = [mesh2] * 3

mesh_diff_pair_wise = difference_pairwise(meshes_1, meshes_2, mesh_property)
print(f"Length of mesh_list1: {len(meshes_1)}")
Length of mesh_list1: 3
print(f"Length of mesh_list2: {len(meshes_2)}")
Length of mesh_list2: 3
print(f"Shape of mesh_diff_pair_wise: {mesh_diff_pair_wise.shape}")
Shape of mesh_diff_pair_wise: (3,)

3. Matrix difference - one array#

If only one list or array is provided, the differences between every possible pair of objects in the array will be returned.

Consider following list:

\[\begin{split}\text{List} = \begin{bmatrix} A & B & C\\ \end{bmatrix}\end{split}\]

The output array will contain following differences between meshes:

\[\begin{split}\begin{bmatrix} A-A & B-A & C-A\\ A-B & B-B & C-B \\ A-C & B-C & C-C \\ \end{bmatrix}\end{split}\]

and will have shape of (len(mesh_list), len(mesh_list)). As in the following example:

mesh_list = [mesh1, mesh2, mesh1, mesh2]

mesh_diff_matrix = difference_matrix(mesh_list, mesh_property=mesh_property)
print(f"Length of mesh_list1: {len(mesh_list)}")
Length of mesh_list1: 4
print(f"Shape of mesh_list1: {mesh_diff_matrix.shape}")
Shape of mesh_list1: (4, 4)

4. Matrix difference - two arrays of different length#

Unlike difference_pairwise(), difference_matrix() can accept two lists/arrays of different lengths. As in Section 3, the differences between all possible pairs of meshes in both lists/arrays will be calculated.

Consider following lists:

\[\begin{split}\text{List}_1 = \begin{bmatrix} A_1 & B_1 & C_1\\ \end{bmatrix}\end{split}\]
\[\begin{split}\text{List}_2 = \begin{bmatrix} A_2 & B_2 \\ \end{bmatrix}\end{split}\]

The output array will contain following differences between meshes:

\[\begin{split}\begin{bmatrix} A_1-A_2 & A_1-B_2 \\ B_1-A_2 & B_1-B_2 \\ C_1-A_2 & C_1-B_2 \\ \end{bmatrix}\end{split}\]

and will have a shape of (len(mesh_list_matrix_1), len(mesh_list_matrix_2)). As in the following example:

mesh_list_matrix_1 = [mesh1, mesh2, mesh1]
mesh_list_matrix_2 = [mesh2, mesh1]

mesh_diff_matrix = difference_matrix(
    mesh_list_matrix_1, mesh_list_matrix_2, mesh_property
)
print(f"Length of mesh_list_matrix_1: {len(mesh_list_matrix_1)}")
Length of mesh_list_matrix_1: 3
print(f"Length of mesh_list_matrix_2: {len(mesh_list_matrix_2)}")
Length of mesh_list_matrix_2: 2
print(f"Shape of mesh_diff_matrix: {mesh_diff_matrix.shape}")
Shape of mesh_diff_matrix: (3, 2)

Total running time of the script: (0 minutes 0.100 seconds)