Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vtkConvexPointSet cell type in pyvista/meshio? #108

Open
amine-aboufirass opened this issue Jan 22, 2020 · 15 comments
Open

vtkConvexPointSet cell type in pyvista/meshio? #108

amine-aboufirass opened this issue Jan 22, 2020 · 15 comments
Labels
mesh-creation Topics around creating a PyVista mesh meshio Usage of external meshio library

Comments

@amine-aboufirass
Copy link

I tried to use pyvista's Plotter object and save_meshio method to plot and write an unstructured grid into a vtk file. I used fipy to generate the grid, retrieved the underlying vtk object, instantiated a pyvista unstructured grid from the low level vtk object and then tried to plot and save into a file.

Unfortunately, the plot was blank, and writing the file threw an error:


KeyError Traceback (most recent call last)
in
----> 1 pyvista.save_meshio('test.vtk', ugrid)

~\AppData\Local\Continuum\miniconda3\envs\CGF_1\lib\site-packages\pyvista\utilities\fileio.py in save_meshio(filename, mesh, file_format, **kwargs)
367 cells[cell_type].append(cell)
368 mapper[cell_type].append(i)
--> 369 cells = {vtk_to_meshio_type[k]: np.vstack(v) for k, v in cells.items()}
370 mapper = {vtk_to_meshio_type[k]: v for k, v in mapper.items()}
371

~\AppData\Local\Continuum\miniconda3\envs\CGF_1\lib\site-packages\pyvista\utilities\fileio.py in (.0)
367 cells[cell_type].append(cell)
368 mapper[cell_type].append(i)
--> 369 cells = {vtk_to_meshio_type[k]: np.vstack(v) for k, v in cells.items()}
370 mapper = {vtk_to_meshio_type[k]: v for k, v in mapper.items()}
371

KeyError: 41

Here is the original code:

from fipy import CylindricalGrid2D
import pyvista

mesh = CylindricalGrid2D(dr=0.1, dz=0.25, nr=3, nz=0.1)
ugrid= pyvista.UnstructuredGrid(mesh.VTKCellDataSet._vtk_obj)
plotter = pyvista.Plotter()
plotter.set_background('white')
plotter.add_mesh(ugrid, style='wireframe', color='black')
plotter.add_bounding_box(color='red')
plotter.show_grid(color="red")
plotter.view_xy()
plotter.show()
pyvista.save_meshio('test.vtk', ugrid)

I'm suspecting the KeyError might be related to the fact that these cylindrical grids appear to contain VTK_CONVEX_POINT_SET cell types. What's the status of these cell types in pyvista? Can they be plotted and written out to files?

@amine-aboufirass
Copy link
Author

amine-aboufirass commented Jan 22, 2020

I think this might indeed have something to do with the cell type which is 41. Is there a way to overwrite cell type after the grid has been created in pyvista? A temporary solution might be to assign a similar cell type to the ugrid for plotting purposes. If not could we consider adding support for this cell type until it is deprecated?

@banesullivan banesullivan added mesh-creation Topics around creating a PyVista mesh vtk-special use cases that leverage special VTK features labels Jan 22, 2020
@banesullivan
Copy link
Member

banesullivan commented Jan 22, 2020

The KeyError is a meshio issue as meshio probably doesn't support that cell type. Pinging @keurfonluu. Either way, we should add a try/except there to catch unsupported cell types on meshio's side and yield a more user-friendly error.

As for the blank rendering scene in PyVista... I'd need more details on the actual object being passed from mesh.VTKCellDataSet._vtk_obj... is this indeed a vtkUnstructuredGrid mesh? Can you:

import vtk
to_wrap = mesh.VTKCellDataSet._vtk_obj
assert isinstance(to_wrap, vtk.vtkUnstructuredGrid)

if so, maybe use VTK to write that data object to a VTK format, zip it, and post that here:

writer = vtk.vtkUnstructuredGridWriter()
writer.SetFileName("test.vtk")
writer.SetInputData(to_wrap)
writer.Write()

@banesullivan
Copy link
Member

banesullivan commented Jan 22, 2020

Actually, I managed to get FiPy running... took a minute and some editing of their source code......

The mesh they pass back is indeed an instance of a vtkUnstructuredGrid object, so all good there. But something isn't right with it. If you try to directly save that mesh and load it in ParaView, it is even blank. This makes me think that the mesh's cells are ill-formed.

Is there any library or software that you can pass that object to with success?

@banesullivan
Copy link
Member

banesullivan commented Jan 22, 2020

And to confirm, PyVista does support the vtkConvexPointSet cell type as PyVista is nothing more than a Pythonic interface back to VTK. This means that FiPy is improperly making those meshes. I recommend raising this issue with FiPy.

import pyvista as pv

def sample_convex_cell():
    import vtk
    points = vtk.vtkPoints()
    points.InsertNextPoint( 0, 0, 0);
    points.InsertNextPoint( 1, 0, 0);
    points.InsertNextPoint( 1, 1, 0);
    points.InsertNextPoint( 0, 1, 0);
    points.InsertNextPoint( 0, 0, 1);
    points.InsertNextPoint( 1, 0, 1);
    points.InsertNextPoint( 1, 1, 1);
    points.InsertNextPoint( 0, 1, 1);
    points.InsertNextPoint( 0.5, 0, 0);
    points.InsertNextPoint( 1, 0.5, 0);
    points.InsertNextPoint( 0.5, 1, 0);
    points.InsertNextPoint( 0, 0.5, 0);
    points.InsertNextPoint( 0.5, 0.5, 0);

    cps = vtk.vtkConvexPointSet()
    for i in range(13):
        cps.GetPointIds().InsertId(i,i);

    ug = vtk.vtkUnstructuredGrid()
    ug.Allocate(1,1)
    ug.InsertNextCell(cps.GetCellType(), cps.GetPointIds())
    ug.SetPoints(points)

    assert isinstance (ug.GetCell(0), vtk.vtkConvexPointSet)
    return ug

pv.UnstructuredGrid(sample_convex_cell()).plot(show_edges=True)

download


As for meshio, again support for that is out of our scope, but we will add a more user-friendly error message for cell types that meshio doesn't support

@banesullivan banesullivan removed the vtk-special use cases that leverage special VTK features label Jan 22, 2020
@banesullivan banesullivan changed the title VTK_CONVEX_POINT_SET cell type supported in pyvista? vtkConvexPointSet cell type supported in pyvista/meshio Jan 22, 2020
@banesullivan banesullivan changed the title vtkConvexPointSet cell type supported in pyvista/meshio vtkConvexPointSet cell type in pyvista/meshio? Jan 22, 2020
@banesullivan banesullivan modified the milestone: meshio Jan 22, 2020
@banesullivan banesullivan added the meshio Usage of external meshio library label Jan 22, 2020
@keurfonluu
Copy link

The problem seems to be due to fipy.meshes.cylindricalUniformGrid2D.cylindricalUniformGrid2D (called by fipy.CylindricalGrid2D when input arguments are uniform).

You can plot a grid generated with fipy.meshes.cylindricalNonUniformGrid2D.cylindricalNonUniformGrid2D with pyvista, though. There is still an issue with meshio due to the fact that this function returns polygons. I'll look into that.

Note that you do not need meshio to export to VTK a mesh that can be plotted by pyvista (e.g. mesh.save("mesh.vtk")).

@banesullivan
Copy link
Member

Thanks, @keurfonluu!

ah yep, I missed that @awa5114, was trying to save as a VTK file. This is totally doable without meshio (meshio is for additional formats)

@amine-aboufirass
Copy link
Author

amine-aboufirass commented Jan 23, 2020

@keurfonluu I was not able to plot cylindricalNonUniformGrid2D, the name is not recognized on import:

AttributeError: module 'fipy.meshes.cylindricalNonUniformGrid2D' has no attribute 'cylindricalNonUniformGrid2D'

Do you have a working example with pyvista where this actually plots?

@banesullivan that's good to hear that pyvista inherits vtk capability such that cell type 41 is still supported. However, I'm a bit skeptical regarding your comment:

The mesh they pass back is indeed an instance of a vtkUnstructuredGrid object, so all good there. But something isn't right with it. If you try to directly save that mesh and load it in ParaView, it is even blank. This makes me think that the mesh's cells are ill-formed.

Though I have raised it with fipy's main man @guyer. I was also able to run the example code you provided successfully so I am more inclined to think it may be an issue on fipy's side. There may be some interest in fipy for a pyvista viewer class. I could probably submit a pull request for that given some guidance from @guyer and yourself. Please see usnistgov/fipy#693 for more information.

Finally, you mention the following:

The KeyError is a meshio issue as meshio probably doesn't support that cell type. Pinging @keurfonluu. Either way, we should add a try/except there to catch unsupported cell types on meshio's side and yield a more user-friendly error.

I guess adding a try/except would be nice, but wouldn't it make more sense to catch it at the source and have meshio take care of the error handling? It seems a bit unnatural to have pyvista handle exceptions that are occurring at the meshio level? Perhaps something for @nschloe?

@nschloe
Copy link

nschloe commented Jan 23, 2020

I think it's perfectly fine for any library that uses meshio to handle it's exceptions. meshio probably gives a meshio.ReadError(), and you can just catch that. Remember that an exception is an expected error.

If you think that meshio throws needlessly, you can always file a issue against meshio, for example for the support of VTK_CONVEX_POINT_SETs.

@amine-aboufirass
Copy link
Author

amine-aboufirass commented Jan 23, 2020

@nschloe fair enough. In that case we should consider adding it to pyvista?

@guyer
Copy link

guyer commented Jan 23, 2020

@banesullivan: "and some editing of their source code". Can you share?

@banesullivan
Copy link
Member

@nschloe fair enough. In that case we should consider adding it to pyvista?

@awa5114, i think pyvista/pyvista#559 should have handled this!
If more is needed, let us know

Sent with GitHawk

@banesullivan
Copy link
Member

@banesullivan: "and some editing of their source code". Can you share?

@guyer, sure! I’ll see if I can retrace my steps and make a PR/contribute to usnistgov/fipy#693. I was feeling particularly hacky when I did this and did a few random things just to make the script work, nothing serious.

Sent with GitHawk

@amine-aboufirass
Copy link
Author

@banesullivan pyvista/pyvista#559 handles only the KeyError stemming from meshio. It does not handle the fipy plotting issue right? That would require something to be done at thè fipy level?

@banesullivan
Copy link
Member

@awa5114, that’s correct. There’s nothing we can do on PyVista side about the problematic mesh being passed from fipy.
Though if I manage to get some free time, I might be able to help you all set up those VTK conversion routines

Sent with GitHawk

@keurfonluu
Copy link

keurfonluu commented Jan 23, 2020

@keurfonluu I was not able to plot cylindricalNonUniformGrid2D, the name is not recognized on import:

AttributeError: module 'fipy.meshes.cylindricalNonUniformGrid2D' has no attribute 'cylindricalNonUniformGrid2D'

My bad, the function name starts with an uppercase so it should rather be

import numpy
import pyvista
from fipy.meshes.cylindricalNonUniformGrid2D import CylindricalNonUniformGrid2D

mesh = CylindricalNonUniformGrid2D(
    dx = numpy.logspace(1., 2., 20),
    dy = numpy.full(20, 10.),
)
ugrid = pyvista.UnstructuredGrid(mesh.VTKCellDataSet._vtk_obj)
ugrid.plot(show_edges = True, notebook = False)

I guess adding a try/except would be nice, but wouldn't it make more sense to catch it at the source and have meshio take care of the error handling? It seems a bit unnatural to have pyvista handle exceptions that are occurring at the meshio level? Perhaps something for @nschloe?

meshio already catches its own errors i.e. errors that happen when trying to read a file to a meshio.Mesh or writing a meshio.Mesh to a file. This KeyError happens when pyvista converts a pyvista.UnstructuredGrid to a meshio.Mesh (which uses an internal dict from meshio), so it is actually within pyvista's scope, not meshio.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mesh-creation Topics around creating a PyVista mesh meshio Usage of external meshio library
Projects
None yet
Development

No branches or pull requests

5 participants