.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/howto_preprocessing/plot_initial_properties_and_variables.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_preprocessing_plot_initial_properties_and_variables.py: Setting initial properties and variables in bulk meshes ======================================================= .. sectionauthor:: Jörg Buchwald (Helmholtz Centre for Environmental Research GmbH - UFZ) For this example we use an existing mesh file and add the required arrays. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import numpy as np import ogstools as ot from ogstools import examples .. GENERATED FROM PYTHON SOURCE LINES 17-22 Setting up material ids and other material properties ----------------------------------------------------- A bulk mesh file is loaded. If it already contains old Material IDs we delete them first. .. GENERATED FROM PYTHON SOURCE LINES 22-27 .. code-block:: Python mesh = ot.Mesh(examples.mechanics_vtu) mesh.cell_data.remove("MaterialIDs") .. GENERATED FROM PYTHON SOURCE LINES 28-29 One way to set-up new Material IDs is by defining a function. .. GENERATED FROM PYTHON SOURCE LINES 29-35 .. code-block:: Python def material_ids(pt: np.array) -> int: if np.sqrt((pt[0] - 150) ** 2 + (pt[1] + 650) ** 2 + pt[2] ** 2) < 90: return 0 return 1 .. GENERATED FROM PYTHON SOURCE LINES 36-40 The function is then used to create an array of type np.int32 and same size as the number of elements in the mesh. The numpy array is later implicitly converted into a VTK array of VTK-type Int32 which is the required by OpenGeoSys for Material IDs. .. GENERATED FROM PYTHON SOURCE LINES 40-43 .. code-block:: Python ccp = mesh.cell_centers().points mat_ids = np.array([material_ids(pt) for pt in ccp], dtype=np.int32) .. GENERATED FROM PYTHON SOURCE LINES 44-45 Then the array is attached to the mesh. .. GENERATED FROM PYTHON SOURCE LINES 45-50 .. code-block:: Python mesh.cell_data.set_array(mat_ids, "MaterialIDs") fig = mesh.plot_contourf(ot.variables.material_id) .. image-sg:: /auto_examples/howto_preprocessing/images/sphx_glr_plot_initial_properties_and_variables_001.png :alt: plot initial properties and variables :srcset: /auto_examples/howto_preprocessing/images/sphx_glr_plot_initial_properties_and_variables_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 51-59 Likewise material properties / MeshElement parameters can be set in the mesh using standard numpy type floating point arrays. Creating variable/points fields ------------------------------- In this example we use the y-coordinates to create a linear pressure gradient along the y-axis. .. GENERATED FROM PYTHON SOURCE LINES 59-63 .. code-block:: Python y = mesh.points[:, 1] p = 1e6 * y / (np.min(y) - np.max(y)) + 4e6 .. GENERATED FROM PYTHON SOURCE LINES 64-65 The array is added to the mesh. .. GENERATED FROM PYTHON SOURCE LINES 65-69 .. code-block:: Python mesh.point_data.set_array(p, "pressure_gradient") fig = mesh.plot_contourf("pressure_gradient") .. image-sg:: /auto_examples/howto_preprocessing/images/sphx_glr_plot_initial_properties_and_variables_002.png :alt: plot initial properties and variables :srcset: /auto_examples/howto_preprocessing/images/sphx_glr_plot_initial_properties_and_variables_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 70-71 This also works with multidimensional data. .. GENERATED FROM PYTHON SOURCE LINES 71-74 .. code-block:: Python eps = np.zeros((len(mesh.points), 4)) mesh.point_data.set_array(eps, "epsilon_0") .. GENERATED FROM PYTHON SOURCE LINES 75-79 Mapping element data to node data --------------------------------- First, we convert the cell data to point data. Subsequently, a new array is created and filled point-by-point. .. GENERATED FROM PYTHON SOURCE LINES 79-82 .. code-block:: Python mat_ids = mesh.cell_data_to_point_data().point_data["MaterialIDs"] p0 = np.where(mat_ids.astype(int) == 0, -1.2e8, -1e6 * mesh.points[:, 1]) .. GENERATED FROM PYTHON SOURCE LINES 83-85 Finally, the array is added to the mesh and can be saved to disc using pyvista/meshio. .. GENERATED FROM PYTHON SOURCE LINES 85-92 .. code-block:: Python mesh.point_data.set_array(p0, "initial_pressure") # import pyvista as pv # # pv.save_meshio("bulk_mesh.vtu", mesh) fig = mesh.plot_contourf("initial_pressure") .. image-sg:: /auto_examples/howto_preprocessing/images/sphx_glr_plot_initial_properties_and_variables_003.png :alt: plot initial properties and variables :srcset: /auto_examples/howto_preprocessing/images/sphx_glr_plot_initial_properties_and_variables_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.752 seconds) .. _sphx_glr_download_auto_examples_howto_preprocessing_plot_initial_properties_and_variables.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_initial_properties_and_variables.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_initial_properties_and_variables.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_initial_properties_and_variables.zip `