.. 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 or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_howto_postprocessing_plot_calculate_diff.py: Calculate differences between meshes ==================================== This example shows how to calculate differences between meshes. .. GENERATED FROM PYTHON SOURCE LINES 9-15 .. code-block:: Python import ogstools as ot from ogstools import examples .. GENERATED FROM PYTHON SOURCE LINES 50-56 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 contain the variable of interest, the difference will work fine. .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. code-block:: Python mesh_series = examples.load_meshseries_THM_2D_PVD().scale(spatial="km") mesh1 = mesh_series.mesh(0) mesh2 = mesh_series.mesh(-1) .. GENERATED FROM PYTHON SOURCE LINES 63-65 The following call will return a mesh containing the difference of the variable between the two provided meshes. Then we plot the difference: .. GENERATED FROM PYTHON SOURCE LINES 67-70 .. code-block:: Python mesh_diff = ot.mesh.difference(mesh1, mesh2, ot.variables.temperature) fig = ot.plot.contourf(mesh_diff, ot.variables.temperature) .. image-sg:: /auto_examples/howto_postprocessing/images/sphx_glr_plot_calculate_diff_001.png :alt: plot calculate diff :srcset: /auto_examples/howto_postprocessing/images/sphx_glr_plot_calculate_diff_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 71-75 Difference between two meshes of different topology --------------------------------------------------- This examples shall demonstrate how we can compare meshes of different size and/or topology. To demonstrate this, we create two artificial meshes. .. GENERATED FROM PYTHON SOURCE LINES 77-80 .. code-block:: Python quad_mesh = custom_mesh(lengths=0.8, element_order=1, quads=True) tri_mesh = custom_mesh(lengths=1, element_order=2, quads=False) .. GENERATED FROM PYTHON SOURCE LINES 81-83 They are visibly quite similar, aside from the difference in cell type and size: .. GENERATED FROM PYTHON SOURCE LINES 85-87 .. code-block:: Python fig = ot.plot.contourf([quad_mesh, tri_mesh], ot.variables.stress["xx"]) .. image-sg:: /auto_examples/howto_postprocessing/images/sphx_glr_plot_calculate_diff_002.png :alt: plot calculate diff :srcset: /auto_examples/howto_postprocessing/images/sphx_glr_plot_calculate_diff_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 88-92 To better quantify it we form the difference and plot the result. The base mesh of which we subtract from provides the topology to show the differences. Thus we see data here on the quad mesh. Be aware that comparing different topologies is inherently affected by interpolation. .. GENERATED FROM PYTHON SOURCE LINES 94-97 .. code-block:: Python diff_mesh = ot.mesh.difference(quad_mesh, tri_mesh, ot.variables.stress) fig = ot.plot.contourf(diff_mesh, ot.variables.stress.difference["xx"]) .. image-sg:: /auto_examples/howto_postprocessing/images/sphx_glr_plot_calculate_diff_003.png :alt: plot calculate diff :srcset: /auto_examples/howto_postprocessing/images/sphx_glr_plot_calculate_diff_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /builds/ogs/tools/ogstools/docs/examples/howto_postprocessing/plot_calculate_diff.py:94: RuntimeWarning: The topologies of base_mesh and subtract_mesh aren't identical. In order to compute difference, subtract_mesh will be spatially resampled. diff_mesh = ot.mesh.difference(quad_mesh, tri_mesh, ot.variables.stress) .. GENERATED FROM PYTHON SOURCE LINES 98-101 Doing it the other way around shows the difference on the tri-mesh. Here, we see, that the subtracted mesh was smaller then the base mesh and thus, the which are outside of the domain of the second mesh are masked. .. GENERATED FROM PYTHON SOURCE LINES 103-106 .. code-block:: Python diff_mesh = ot.mesh.difference(tri_mesh, quad_mesh, ot.variables.stress) fig = ot.plot.contourf(diff_mesh, ot.variables.stress.difference["xx"]) .. image-sg:: /auto_examples/howto_postprocessing/images/sphx_glr_plot_calculate_diff_004.png :alt: plot calculate diff :srcset: /auto_examples/howto_postprocessing/images/sphx_glr_plot_calculate_diff_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /builds/ogs/tools/ogstools/docs/examples/howto_postprocessing/plot_calculate_diff.py:103: RuntimeWarning: The topologies of base_mesh and subtract_mesh aren't identical. In order to compute difference, subtract_mesh will be spatially resampled. diff_mesh = ot.mesh.difference(tri_mesh, quad_mesh, ot.variables.stress) .. GENERATED FROM PYTHON SOURCE LINES 107-112 Differences between multiple 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 114-135 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} \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 137-147 .. code-block:: Python meshes_1 = [mesh1] * 3 meshes_2 = [mesh2] * 3 mesh_diff_pair_wise = ot.mesh.difference_pairwise( meshes_1, meshes_2, ot.variables.temperature ) print(f"Length of mesh_list1: {len(meshes_1)}") print(f"Length of mesh_list2: {len(meshes_2)}") print(f"Shape of mesh_diff_pair_wise: {mesh_diff_pair_wise.shape}") .. rst-class:: sphx-glr-script-out .. code-block:: none Length of mesh_list1: 3 Length of mesh_list2: 3 Shape of mesh_diff_pair_wise: (3,) .. GENERATED FROM PYTHON SOURCE LINES 148-167 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 169-177 .. code-block:: Python mesh_list = [mesh1, mesh2, mesh1, mesh2] mesh_diff_matrix = ot.mesh.difference_matrix( mesh_list, variable=ot.variables.temperature ) print(f"Length of mesh_list1: {len(mesh_list)}") print(f"Shape of mesh_list1: {mesh_diff_matrix.shape}") .. rst-class:: sphx-glr-script-out .. code-block:: none Length of mesh_list1: 4 Shape of mesh_list1: (4, 4) .. GENERATED FROM PYTHON SOURCE LINES 178-202 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 204-214 .. code-block:: Python mesh_list_matrix_1 = [mesh1, mesh2, mesh1] mesh_list_matrix_2 = [mesh2, mesh1] mesh_diff_matrix = ot.mesh.difference_matrix( mesh_list_matrix_1, mesh_list_matrix_2, ot.variables.temperature ) print(f"Length of mesh_list_matrix_1: {len(mesh_list_matrix_1)}") print(f"Length of mesh_list_matrix_2: {len(mesh_list_matrix_2)}") print(f"Shape of mesh_diff_matrix: {mesh_diff_matrix.shape}") .. rst-class:: sphx-glr-script-out .. code-block:: none Length of mesh_list_matrix_1: 3 Length of mesh_list_matrix_2: 2 Shape of mesh_diff_matrix: (3, 2) .. GENERATED FROM PYTHON SOURCE LINES 215-219 Difference between two meshseries ---------------------------------- It is possible to calculate the difference between two MeshSeries objects. First, an artificial data set will be generated by copying an example data set and increasing the pressure values by 25%. .. GENERATED FROM PYTHON SOURCE LINES 221-225 .. code-block:: Python ms = examples.load_meshseries_THM_2D_PVD().scale(spatial="km", time="d") ms2 = ms.copy() ms2.point_data["pressure"] *= 1.25 .. GENERATED FROM PYTHON SOURCE LINES 226-227 Now a difference between original and altered MeshSeries can be computed: .. GENERATED FROM PYTHON SOURCE LINES 229-231 .. code-block:: Python diff_ms = ot.MeshSeries.difference(ms2, ms, ot.variables.pressure) .. GENERATED FROM PYTHON SOURCE LINES 232-234 As a reference, the last steps of the original and altered MeshSeries can be plotted: .. GENERATED FROM PYTHON SOURCE LINES 236-238 .. code-block:: Python fig_T = ot.plot.contourf(ms[-1], ot.variables.pressure, show_edges=False) .. image-sg:: /auto_examples/howto_postprocessing/images/sphx_glr_plot_calculate_diff_005.png :alt: plot calculate diff :srcset: /auto_examples/howto_postprocessing/images/sphx_glr_plot_calculate_diff_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 239-241 .. code-block:: Python fig_T_2 = ot.plot.contourf(ms2[-1], ot.variables.pressure, show_edges=False) .. image-sg:: /auto_examples/howto_postprocessing/images/sphx_glr_plot_calculate_diff_006.png :alt: plot calculate diff :srcset: /auto_examples/howto_postprocessing/images/sphx_glr_plot_calculate_diff_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 242-246 The MeshSeries object returned by the difference() method, can be used in the same way as any other one. However, to plot the pressure difference either use the string "pressure_difference" (see comment in the code block below) or the derived variable. ot.variable.pressure.difference. .. GENERATED FROM PYTHON SOURCE LINES 248-253 .. code-block:: Python fig_T_diff = ot.plot.contourf( diff_ms[-1], ot.variables.pressure.difference, show_edges=True ) # fig_T_diff = ot.plot.contourf(diff_ms[-1], "pore_pressure_difference") .. image-sg:: /auto_examples/howto_postprocessing/images/sphx_glr_plot_calculate_diff_007.png :alt: plot calculate diff :srcset: /auto_examples/howto_postprocessing/images/sphx_glr_plot_calculate_diff_007.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.513 seconds) .. _sphx_glr_download_auto_examples_howto_postprocessing_plot_calculate_diff.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://binder.opengeosys.org/v2/gh/bilke/binder-ogs-requirements/6.5.7-0.8.0?urlpath=git-pull%3Frepo%3Dhttps%253A%252F%252Fgitlab.opengeosys.org%252Fogs%252Ftools%252Fogstools%26urlpath%3Dlab%252Ftree%252Fogstools/docs/examples/howto_postprocessing/plot_calculate_diff.py :alt: Launch binder :width: 150 px .. 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 `