Note
Go to the end to download the full example code. or to run this example in your browser via Binder
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.
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
Conversion#
Using 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.
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")
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
Let’s plot the domain mesh and mark the subdomains.
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)

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:
meshes.save(model_dir)
[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')]
Total running time of the script: (0 minutes 1.708 seconds)