Skip to content

Commit

Permalink
MSH.show: add possibility to show cell-data
Browse files Browse the repository at this point in the history
  • Loading branch information
MuellerSeb committed Aug 18, 2019
1 parent d39438e commit ffd7604
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 27 deletions.
23 changes: 19 additions & 4 deletions ogs5py/fileclasses/msh/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,15 +1020,28 @@ def generate(self, generator="rectangular", **kwargs):
"""
self._dict = getattr(gen, generator)(**kwargs)

def show(self, show_material_id=True):
def show(
self,
show_cell_data=None,
show_material_id=False,
show_element_id=False,
):
"""
Display the mesh colored by its material ID.
Parameters
----------
show_cell_data : ndarray or dict, optional
Here you can specify additional
element/cell data sorted by their IDs.
It can be a dictionary with data-name as key
and the ndarray as value. Default: None
show_material_id : bool, optional
Here you can specify if the mesh should be colored by material_id.
Default: True
Here you can specify if the material_id should be shown.
Default: False
show_element_id : bool, optional
Here you can specify if the element_id should be shown.
Default: False
Notes
-----
Expand All @@ -1037,7 +1050,9 @@ def show(self, show_material_id=True):
"""
from ogs5py.fileclasses.msh.viewer import show_mesh

show_mesh(self._dict, show_material_id=show_material_id)
show_mesh(
self._dict, show_cell_data, show_material_id, show_element_id
)

def set_material_id(
self, material_id=0, element_id=None, element_mask=None
Expand Down
73 changes: 50 additions & 23 deletions ogs5py/fileclasses/msh/viewer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# -*- coding: utf-8 -*-
"""
Viewer for ogs5py-mesh.
@author: sebastian
"""
"""Viewer for an ogs5py mesh."""
from __future__ import division, print_function, absolute_import
import os
import tempfile
Expand All @@ -20,7 +16,9 @@
MAYA_AVAIL = False


def show_mesh(mesh, show_material_id=True):
def show_mesh(
mesh, show_cell_data=None, show_material_id=False, show_element_id=False
):
"""
Display a given mesh colored by its material ID.
Expand All @@ -44,9 +42,16 @@ def show_mesh(mesh, show_material_id=True):
contains material ids for each element sorted by element types
element_id : dictionary
contains element ids for each element sorted by element types
show_cell_data : ndarray or dict, optional
Here you can specify additional element/cell data sorted by their IDs.
It can be a dictionary with data-name as key and the ndarray as value.
Default: None
show_material_id : bool, optional
Here you can specify if the mesh should be colored by material_id.
Default: True
Here you can specify if the material_id should be shown.
Default: False
show_element_id : bool, optional
Here you can specify if the element_id should be shown.
Default: False
Notes
-----
Expand All @@ -56,27 +61,26 @@ def show_mesh(mesh, show_material_id=True):
# stop if mayavi is not installed
if not MAYA_AVAIL:
print("Could not import 'mayavi'!")
print(
"..if you are running an IPython console"
+ ", don't run it under qt5. Mayavi still uses qt4."
)
return

if show_cell_data is not None:
if not isinstance(show_cell_data, dict):
cell_data_name = "add_data"
else:
cell_data_name = list(show_cell_data)[0]

# close all mayavi scenes
mlab.close(all=True)
# set the bounds for the color range
min_id = np.inf
max_id = 0.0
for matid in mesh["material_id"]:
min_id = np.min((min_id, np.min(mesh["material_id"][matid])))
max_id = np.max((max_id, np.max(mesh["material_id"][matid])))
id_no = int(max_id - min_id + 1)
# create a temp-file which contains a vtk version of the mesh
vtkfile = tempfile.NamedTemporaryFile(suffix=".vtu")
# export the mesh to the temp vtk file
export_mesh(vtkfile.name, mesh, export_material_id=show_material_id)
print("temp vtk file for mayavi:")
print(vtkfile.name)
export_mesh(
vtkfile.name,
mesh,
export_material_id=show_material_id,
export_element_id=show_element_id,
cell_data_by_id=show_cell_data,
)
# load the vtk file to mayavi's mlab
data_source = mlab.pipeline.open(vtkfile.name)
# create a surface out of the vtk source
Expand All @@ -87,7 +91,19 @@ def show_mesh(mesh, show_material_id=True):
surface.actor.property.interpolation = "flat"
# settings for the material ID
# surface.parent.scalar_lut_manager.lut_mode = "Set1"
if show_material_id:
if show_cell_data is not None:
surface.parent.parent._cell_scalars_name_changed(cell_data_name)
surface.parent.parent.update()
surface.parent.scalar_lut_manager.shadow = True
surface.parent.scalar_lut_manager.show_scalar_bar = True
elif show_material_id:
# set the bounds for the color range
min_id = np.inf
max_id = 0.0
for matid in mesh["material_id"]:
min_id = np.min((min_id, np.min(mesh["material_id"][matid])))
max_id = np.max((max_id, np.max(mesh["material_id"][matid])))
id_no = int(max_id - min_id + 1)
surface.parent.parent._cell_scalars_name_changed("material_id")
surface.parent.parent.update()
surface.parent.scalar_lut_manager.use_default_range = False
Expand All @@ -99,6 +115,17 @@ def show_mesh(mesh, show_material_id=True):
surface.parent.scalar_lut_manager.shadow = True
surface.parent.scalar_lut_manager.show_scalar_bar = True
surface.parent.scalar_lut_manager.scalar_bar.label_format = "%.0f"
elif show_element_id:
surface.parent.parent._cell_scalars_name_changed("element_id")
surface.parent.parent.update()
surface.parent.scalar_lut_manager.number_of_labels = 2
surface.parent.scalar_lut_manager.use_default_name = False
surface.parent.scalar_lut_manager.data_name = "Element ID"
surface.parent.scalar_lut_manager.shadow = True
surface.parent.scalar_lut_manager.show_scalar_bar = True
surface.parent.scalar_lut_manager.scalar_bar.label_format = "%.0f"
else:
surface.actor.mapper.scalar_visibility = False
# give it a name
surface.name = "OGS mesh"
# show it
Expand Down

0 comments on commit ffd7604

Please sign in to comment.