.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_user-guide/plot_msh2vtu.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_user-guide_plot_msh2vtu.py: Meshes from gmsh (msh2vtu) ========================== Here you find, how you can generate OGS-compatible meshes from a .msh file. This is accomplished by extracting domain-, boundary- and physical groups from the gmsh mesh. This requires the mesh entities to be assigned to a physical group in the gmsh mesh. .. GENERATED FROM PYTHON SOURCE LINES 12-21 .. code-block:: Python from pathlib import Path from tempfile import mkdtemp import ogstools as ot from ogstools import examples model_dir = Path(mkdtemp()) msh = examples.msh_geolayers_2d .. GENERATED FROM PYTHON SOURCE LINES 22-32 Conversion ---------- Using :class:`~ogstools.meshlib.meshes.Meshes`. :meth:`~ogstools.meshlib.meshes.Meshes.from_gmsh` we can generate unstructured grids from a given .msh file. As OGS wants to have the MaterialIDs numbered beginning from zero, you usually want to set reindex to True. The return value is a dict with the mesh names pointing to the corresponding meshes. Here we print the filenames and the number of cells. For a purely file based approach, OGSTools also provides `msh2vtu` as a command line application. Please, call it with `--help` for info about the usage. .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: Python meshes = ot.Meshes.from_gmsh(filename=msh, reindex=True, log=False) print(*[f"{name}: {mesh.n_cells=}" for name, mesh in meshes.items()], sep="\n") .. rst-class:: sphx-glr-script-out .. code-block:: none domain: mesh.n_cells=2520 Left: mesh.n_cells=8 Bottom: mesh.n_cells=120 Right: mesh.n_cells=8 Top: mesh.n_cells=120 SedimentLayer1: mesh.n_cells=69 SedimentLayer2: mesh.n_cells=146 SedimentLayer3: mesh.n_cells=230 RockBed: mesh.n_cells=2075 .. GENERATED FROM PYTHON SOURCE LINES 38-39 Let's plot the domain mesh and mark the subdomains. .. GENERATED FROM PYTHON SOURCE LINES 41-49 .. code-block:: Python domain = meshes["domain"] fig = ot.plot.contourf(domain, ot.variables.material_id, show_edges=False) style = {"size": 32, "backgroundcolor": "lightgrey", "ha": "center"} for name, mesh in meshes.items(): text_xy = [mesh.center[0], 0.5 * (mesh.center[1] + mesh.bounds[2])] fig.axes[0].annotate(name.split("_")[-1], text_xy, **style) .. image-sg:: /auto_user-guide/images/sphx_glr_plot_msh2vtu_001.png :alt: plot msh2vtu :srcset: /auto_user-guide/images/sphx_glr_plot_msh2vtu_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 50-56 Note regarding saving --------------------- If you want to save the meshes to be used in a OGS simulation, make sure to save the meshes in the following way, so that they are conforming to OGS standards: .. GENERATED FROM PYTHON SOURCE LINES 58-59 .. code-block:: Python meshes.save(model_dir) .. rst-class:: sphx-glr-script-out .. code-block:: none [PosixPath('/tmp/tmpzu6_8r4m/domain.vtu'), PosixPath('/tmp/tmpzu6_8r4m/Left.vtu'), PosixPath('/tmp/tmpzu6_8r4m/Bottom.vtu'), PosixPath('/tmp/tmpzu6_8r4m/Right.vtu'), PosixPath('/tmp/tmpzu6_8r4m/Top.vtu'), PosixPath('/tmp/tmpzu6_8r4m/SedimentLayer1.vtu'), PosixPath('/tmp/tmpzu6_8r4m/SedimentLayer2.vtu'), PosixPath('/tmp/tmpzu6_8r4m/SedimentLayer3.vtu'), PosixPath('/tmp/tmpzu6_8r4m/RockBed.vtu')] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.708 seconds) .. _sphx_glr_download_auto_user-guide_plot_msh2vtu.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//builds/yQxD4hQx/0/ogs/tools/ogstools/docs/auto_user-guide/plot_msh2vtu.py :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_msh2vtu.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_msh2vtu.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_msh2vtu.zip `