.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/howto_postprocessing/plot_calculate_diff.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_howto_postprocessing_plot_calculate_diff.py: Calculate differences between meshes ==================================== .. sectionauthor:: Feliks Kiszkurno (Helmholtz Centre for Environmental Research GmbH - UFZ) This example explains how to use functions from meshlib to calculate differences between meshes. .. GENERATED FROM PYTHON SOURCE LINES 12-19 .. code-block:: Python import ogstools as ogs from ogstools import examples variable = ogs.variables.temperature .. GENERATED FROM PYTHON SOURCE LINES 25-31 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. .. GENERATED FROM PYTHON SOURCE LINES 33-40 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 variable of interest, the difference will work fine. .. GENERATED FROM PYTHON SOURCE LINES 40-44 .. code-block:: Python mesh_series = examples.load_meshseries_THM_2D_PVD() mesh1 = mesh_series.mesh(0) mesh2 = mesh_series.mesh(-1) .. GENERATED FROM PYTHON SOURCE LINES 45-52 The following call will return a mesh containing the difference of the variable between the two provided meshes: .. math:: \text{Mesh}_1 - \text{Mesh}_2 .. GENERATED FROM PYTHON SOURCE LINES 52-55 .. code-block:: Python mesh_diff = mesh1.difference(mesh2, variable) .. GENERATED FROM PYTHON SOURCE LINES 56-57 This returned object will be a PyVista UnstructuredGrid object: .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: Python print(f"Type of mesh_diff: {type(mesh_diff)}") .. rst-class:: sphx-glr-script-out .. code-block:: none Type of mesh_diff: .. GENERATED FROM PYTHON SOURCE LINES 62-87 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: .. math:: \text{List}_{1} = \begin{bmatrix} A_{1} & B_{1} & C_{1}\\ \end{bmatrix} and .. math:: \text{List}_{2} = \begin{bmatrix} A_{2} & B_{2} & C_{2}\\ \end{bmatrix} The output array will contain following differences between meshes: .. math:: \begin{bmatrix} A_{1}-A_{2} & B_{1}-B_{2} & C_{1}-C_{2}\\ \end{bmatrix} and will have the same shape as the input lists. As in the example below: .. GENERATED FROM PYTHON SOURCE LINES 87-95 .. code-block:: Python meshes_1 = [mesh1] * 3 meshes_2 = [mesh2] * 3 mesh_diff_pair_wise = ogs.meshlib.difference_pairwise( meshes_1, meshes_2, variable ) .. GENERATED FROM PYTHON SOURCE LINES 96-98 .. code-block:: Python print(f"Length of mesh_list1: {len(meshes_1)}") .. rst-class:: sphx-glr-script-out .. code-block:: none Length of mesh_list1: 3 .. GENERATED FROM PYTHON SOURCE LINES 99-101 .. code-block:: Python print(f"Length of mesh_list2: {len(meshes_2)}") .. rst-class:: sphx-glr-script-out .. code-block:: none Length of mesh_list2: 3 .. GENERATED FROM PYTHON SOURCE LINES 102-104 .. code-block:: Python print(f"Shape of mesh_diff_pair_wise: {mesh_diff_pair_wise.shape}") .. rst-class:: sphx-glr-script-out .. code-block:: none Shape of mesh_diff_pair_wise: (3,) .. GENERATED FROM PYTHON SOURCE LINES 105-124 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: .. math:: \text{List} = \begin{bmatrix} A & B & C\\ \end{bmatrix} The output array will contain following differences between meshes: .. math:: \begin{bmatrix} A-A & B-A & C-A\\ A-B & B-B & C-B \\ A-C & B-C & C-C \\ \end{bmatrix} and will have shape of (len(mesh_list), len(mesh_list)). As in the following example: .. GENERATED FROM PYTHON SOURCE LINES 124-129 .. code-block:: Python mesh_list = [mesh1, mesh2, mesh1, mesh2] mesh_diff_matrix = ogs.meshlib.difference_matrix(mesh_list, variable=variable) .. GENERATED FROM PYTHON SOURCE LINES 130-132 .. code-block:: Python print(f"Length of mesh_list1: {len(mesh_list)}") .. rst-class:: sphx-glr-script-out .. code-block:: none Length of mesh_list1: 4 .. GENERATED FROM PYTHON SOURCE LINES 133-135 .. code-block:: Python print(f"Shape of mesh_list1: {mesh_diff_matrix.shape}") .. rst-class:: sphx-glr-script-out .. code-block:: none Shape of mesh_list1: (4, 4) .. GENERATED FROM PYTHON SOURCE LINES 136-160 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: .. math:: \text{List}_1 = \begin{bmatrix} A_1 & B_1 & C_1\\ \end{bmatrix} .. math:: \text{List}_2 = \begin{bmatrix} A_2 & B_2 \\ \end{bmatrix} The output array will contain following differences between meshes: .. math:: \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} and will have a shape of (len(mesh_list_matrix_1), len(mesh_list_matrix_2)). As in the following example: .. GENERATED FROM PYTHON SOURCE LINES 160-168 .. code-block:: Python mesh_list_matrix_1 = [mesh1, mesh2, mesh1] mesh_list_matrix_2 = [mesh2, mesh1] mesh_diff_matrix = ogs.meshlib.difference_matrix( mesh_list_matrix_1, mesh_list_matrix_2, variable ) .. GENERATED FROM PYTHON SOURCE LINES 169-171 .. code-block:: Python print(f"Length of mesh_list_matrix_1: {len(mesh_list_matrix_1)}") .. rst-class:: sphx-glr-script-out .. code-block:: none Length of mesh_list_matrix_1: 3 .. GENERATED FROM PYTHON SOURCE LINES 172-174 .. code-block:: Python print(f"Length of mesh_list_matrix_2: {len(mesh_list_matrix_2)}") .. rst-class:: sphx-glr-script-out .. code-block:: none Length of mesh_list_matrix_2: 2 .. GENERATED FROM PYTHON SOURCE LINES 175-176 .. code-block:: Python print(f"Shape of mesh_diff_matrix: {mesh_diff_matrix.shape}") .. rst-class:: sphx-glr-script-out .. code-block:: none Shape of mesh_diff_matrix: (3, 2) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.055 seconds) .. _sphx_glr_download_auto_examples_howto_postprocessing_plot_calculate_diff.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_calculate_diff.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_calculate_diff.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_calculate_diff.zip `