Skip to content

Examples / Creating meshes from pyvista surfaces #12

Description

@daavid00

The first block in the Creating meshes from pyvista surfaces example seems to work fine:

from itertools import pairwise
from shutil import which

import ogstools as ot
from ogstools.mesh import create

# an optional requirement (needs to be installed on system or via pip)
tetgen_present = which("tetgen") is not None

if tetgen_present:
    bounds = (-200, 210, -200, 210)
    args = {"bound2D": bounds, "amplitude": 100, "spread": 100, "n": 40}
    gaussians = [
        create.Gaussian2D(**args, height_offset=h) for h in [0, -100, -200]
    ]
    surfaces = [create.Surface(g, mat_id) for mat_id, g in enumerate(gaussians)]
    layers = [create.Layer(sf1, sf2) for sf1, sf2 in pairwise(surfaces)]
    ls = create.LayerSet(layers)

    mesh = ls.to_region_tetrahedron(40).mesh
else:
    mesh = None
info: Rasterising mesh...
info: Cellsize set to 40.000000
info: Writing raster with 11 x 11 pixels.
info: Result written to [/var/folders/zl/0cl8sz155gjfwfbs5fssc0y00000gp/T/ogstools_dmar/create_raster_file/6cb8e9cf433d49b3b344b1d8d6de241b50be3678c8ca400680b0ae6e0613289f.asc](https://file+.vscode-resource.vscode-cdn.net/var/folders/zl/0cl8sz155gjfwfbs5fssc0y00000gp/T/ogstools_dmar/create_raster_file/6cb8e9cf433d49b3b344b1d8d6de241b50be3678c8ca400680b0ae6e0613289f.asc)
info: Rasterising mesh...
info: Cellsize set to 40.000000
info: Writing raster with 11 x 11 pixels.
info: Result written to [/var/folders/zl/0cl8sz155gjfwfbs5fssc0y00000gp/T/ogstools_dmar/create_raster_file/6cb8e9cf433d49b3b344b1d8d6de241bedb7d42f47a54ffa80dfa3afecab4173.asc](https://file+.vscode-resource.vscode-cdn.net/var/folders/zl/0cl8sz155gjfwfbs5fssc0y00000gp/T/ogstools_dmar/create_raster_file/6cb8e9cf433d49b3b344b1d8d6de241bedb7d42f47a54ffa80dfa3afecab4173.asc)
info: Rasterising mesh...
info: Cellsize set to 40.000000
info: Writing raster with 11 x 11 pixels.
info: Result written to [/var/folders/zl/0cl8sz155gjfwfbs5fssc0y00000gp/T/ogstools_dmar/create_raster_file/72c1b01e7b12413c9d15ffe9c64e6da5ac09e9a3339143128b9220d40cfd59c8.asc](https://file+.vscode-resource.vscode-cdn.net/var/folders/zl/0cl8sz155gjfwfbs5fssc0y00000gp/T/ogstools_dmar/create_raster_file/72c1b01e7b12413c9d15ffe9c64e6da5ac09e9a3339143128b9220d40cfd59c8.asc)
info: Rasterising mesh...
info: Cellsize set to 40.000000
info: Writing raster with 11 x 11 pixels.
info: Result written to [/var/folders/zl/0cl8sz155gjfwfbs5fssc0y00000gp/T/ogstools_dmar/create_raster_file/72c1b01e7b12413c9d15ffe9c64e6da5fb1156897c1049e5b66b27ce8b391e28.asc](https://file+.vscode-resource.vscode-cdn.net/var/folders/zl/0cl8sz155gjfwfbs5fssc0y00000gp/T/ogstools_dmar/create_raster_file/72c1b01e7b12413c9d15ffe9c64e6da5fb1156897c1049e5b66b27ce8b391e28.asc)
info: Rasterising mesh...
info: Cellsize set to 40.000000
info: Writing raster with 11 x 11 pixels.
info: Result written to [/var/folders/zl/0cl8sz155gjfwfbs5fssc0y00000gp/T/ogstools_dmar/create_raster_file/febf056cca644f4faa275d0d7151db45f0fa99c3579842cb89624499c4512489.asc](https://file+.vscode-resource.vscode-cdn.net/var/folders/zl/0cl8sz155gjfwfbs5fssc0y00000gp/T/ogstools_dmar/create_raster_file/febf056cca644f4faa275d0d7151db45f0fa99c3579842cb89624499c4512489.asc)
info: Reading mesh '/var/folders/zl/0cl8sz155gjfwfbs5fssc0y00000gp/T/ogstools_dmar/create_raster/378b2293e9904765978d4b90544c6883.vtu' ... 
info: done.
info: No elements to remove
info: TetGenInterface::writeTetGenPoly() - 432 points and 902 surfaces successfully written.
info: Smesh was successfully written.

However, for the second block:

if tetgen_present:
    plotter = ot.plot.contourf(mesh, ot.variables.material_id)
    plotter.show()
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[2], [line 2](vscode-notebook-cell:?execution_count=2&line=2)
      1 if tetgen_present:
----> [2](vscode-notebook-cell:?execution_count=2&line=2)     plotter = ot.plot.contourf(mesh, ot.variables.material_id)
      3     plotter.show()

File ~/opengeosys/.venv/lib/python3.14/site-packages/ogstools/plot/contourplots.py:499, in contourf(meshes, variable, fig, ax, interactive, **kwargs)
    497 max_dim = max([mesh.GetMaxSpatialDimension() for mesh in _meshes])
    498 if interactive or max_dim == 3:
--> [499](https://file+.vscode-resource.vscode-cdn.net/Users/dmar/opengeosys/~/opengeosys/.venv/lib/python3.14/site-packages/ogstools/plot/contourplots.py:499)     plotters = [contourf_pv(mesh, variable, **kwargs) for mesh in _meshes]
    500     if interactive is False:
    501         for pl in plotters:

File ~/opengeosys/.venv/lib/python3.14/site-packages/ogstools/plot/contourplots_pv.py:43, in contourf_pv(mesh, variable, show_edges, opacities, lighting, categoric, **kwargs)
     26 """General 3D plot using pyvista
     27 
     28 :param mesh:        The mesh to be plotted with pyvista.
   (...)     40                     scene.
     41 """
     42 plot_mesh: pv.UnstructuredGrid = mesh.copy()
---> [43](https://file+.vscode-resource.vscode-cdn.net/Users/dmar/opengeosys/~/opengeosys/.venv/lib/python3.14/site-packages/ogstools/plot/contourplots_pv.py:43) variable_ = Variable.find(variable, mesh)
     45 # converting to float as the interactive plot seems to have trouble with int
     46 # taking magnitude of pint quantities to prevent numpy warning
     47 plot_var = variable_.replace(func=lambda x: variable_.func(x).astype(float))
...
--> [161](https://file+.vscode-resource.vscode-cdn.net/Users/dmar/opengeosys/~/opengeosys/.venv/lib/python3.14/site-packages/ogstools/variables/variable.py:161)     raise KeyError(error_msg)
    162 data_key = data_keys[matches.index(True)]
    163 if data_key == variable.difference.output_name:

KeyError: 'MaterialIDs not found in mesh. Available data names are []. '

Related to openjournals/joss-reviews#10973

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions