.. 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. or to run this example in your browser via Binder .. 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 ======================================================= For this example we use an existing mesh file and add the required arrays. .. GENERATED FROM PYTHON SOURCE LINES 9-14 .. code-block:: Python import numpy as np import ogstools as ot from ogstools import examples .. GENERATED FROM PYTHON SOURCE LINES 15-20 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 20-25 .. code-block:: Python mesh = ot.Mesh(examples.mechanics_2D) mesh.cell_data.remove("MaterialIDs") .. GENERATED FROM PYTHON SOURCE LINES 26-27 One way to set-up new Material IDs is by defining a function. .. GENERATED FROM PYTHON SOURCE LINES 27-33 .. 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 34-38 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 38-41 .. 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 42-43 Then the array is attached to the mesh. .. GENERATED FROM PYTHON SOURCE LINES 43-48 .. 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 49-57 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 57-61 .. code-block:: Python y = mesh.points[:, 1] p = 1e6 * y / (np.min(y) - np.max(y)) + 4e6 .. GENERATED FROM PYTHON SOURCE LINES 62-63 The array is added to the mesh. .. GENERATED FROM PYTHON SOURCE LINES 63-67 .. 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 68-69 This also works with multidimensional data. .. GENERATED FROM PYTHON SOURCE LINES 69-72 .. code-block:: Python eps = np.zeros((len(mesh.points), 4)) mesh.point_data.set_array(eps, "epsilon_0") .. GENERATED FROM PYTHON SOURCE LINES 73-77 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 77-80 .. 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 81-83 Finally, the array is added to the mesh and can be saved to disc using pyvista/meshio. .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. code-block:: Python mesh.point_data.set_array(p0, "initial_pressure") .. GENERATED FROM PYTHON SOURCE LINES 86-91 It is important to note that at the interface we have to deal with the ambiguity of setting initial pressures / material data. Therefore, it might be useful to have an alternative approach to set the initial pressures. In the following we assume that all nodes connected to bentonite cells have bentonite pressure: .. GENERATED FROM PYTHON SOURCE LINES 91-103 .. code-block:: Python bentonite = mesh.extract_cells(mesh["MaterialIDs"] == 0) p0_new = -1e6 * mesh.points[:, 1] p0_new[bentonite["vtkOriginalPointIds"]] = -1.2e8 mesh.point_data.set_array(p0_new, "initial_pressure_second-variant") # import pyvista as pv # # pv.save_meshio("bulk_mesh.vtu", mesh) fig = mesh.plot_contourf("initial_pressure") fig = mesh.plot_contourf("initial_pressure_second-variant") .. rst-class:: sphx-glr-horizontal * .. 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-multi-img * .. image-sg:: /auto_examples/howto_preprocessing/images/sphx_glr_plot_initial_properties_and_variables_004.png :alt: plot initial properties and variables :srcset: /auto_examples/howto_preprocessing/images/sphx_glr_plot_initial_properties_and_variables_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.936 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:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://binder.opengeosys.org/v2/gh/bilke/binder-ogs-requirements/6.5.6-0.7.1?urlpath=git-pull%3Frepo%3Dhttps%253A%252F%252Fgitlab.opengeosys.org%252Fogs%252Ftools%252Fogstools%26urlpath%3Dlab%252Ftree%252Fogstools/docs/examples/howto_preprocessing/plot_initial_properties_and_variables.py :alt: Launch binder :width: 150 px .. 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 `