meshio icon indicating copy to clipboard operation
meshio copied to clipboard

[BUG] VTU importer does not seem to correctly handle empty meshes.

Open Andlon opened this issue 2 years ago • 0 comments

Describe the bug The VTU importer does not seem to handle empty meshes correctly.

To Reproduce Here's an exported .vtu from ParaView, containing an empty unstructured mesh. ParaView seems to have added some unnecessary data (the InformationKey entries etc.), but I wasn't quite sure what I could just remove directly without compromising the correctness of the format.

<VTKFile type="UnstructuredGrid" version="1.0" byte_order="LittleEndian" header_type="UInt64">
  <UnstructuredGrid>
    <FieldData>
      <DataArray type="Float64" Name="TimeValue" NumberOfTuples="1" format="ascii" RangeMin="0" RangeMax="0">
        0
      </DataArray>
    </FieldData>
    <Piece NumberOfPoints="0" NumberOfCells="0">
      <PointData Vectors="displacement">
        <DataArray type="Float64" Name="displacement" NumberOfComponents="3" format="ascii" RangeMin="1e+299" RangeMax="-1e+299">
          <InformationKey name="L2_NORM_RANGE" location="vtkDataArray" length="2">
            <Value index="0">
              1e+299
            </Value>
            <Value index="1">
              -1e+299
            </Value>
          </InformationKey>
        </DataArray>
        <DataArray type="Float64" Name="velocity" NumberOfComponents="3" format="ascii" RangeMin="1e+299" RangeMax="-1e+299">
          <InformationKey name="L2_NORM_RANGE" location="vtkDataArray" length="2">
            <Value index="0">
              1e+299
            </Value>
            <Value index="1">
              -1e+299
            </Value>
          </InformationKey>
        </DataArray>
      </PointData>
      <CellData>
      </CellData>
      <Points>
        <DataArray type="Float64" Name="Points" NumberOfComponents="3" format="ascii" RangeMin="1e+299" RangeMax="-1e+299">
          <InformationKey name="L2_NORM_FINITE_RANGE" location="vtkDataArray" length="2">
            <Value index="0">
              1e+299
            </Value>
            <Value index="1">
              -1e+299
            </Value>
          </InformationKey>
          <InformationKey name="L2_NORM_RANGE" location="vtkDataArray" length="2">
            <Value index="0">
              1e+299
            </Value>
            <Value index="1">
              -1e+299
            </Value>
          </InformationKey>
        </DataArray>
      </Points>
      <Cells>
        <DataArray type="Int64" Name="connectivity" format="ascii" RangeMin="1e+299" RangeMax="-1e+299">
        </DataArray>
        <DataArray type="Int64" Name="offsets" format="ascii" RangeMin="1e+299" RangeMax="-1e+299">
        </DataArray>
        <DataArray type="UInt8" Name="types" format="ascii" RangeMin="1e+299" RangeMax="-1e+299">
        </DataArray>
      </Cells>
    </Piece>
  </UnstructuredGrid>
</VTKFile>

Running for example meshio convert file.vtu other_file.vtk on the above file contents gives me:

Traceback (most recent call last):
  File "/home/andreas/miniconda3/bin/meshio", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/andreas/miniconda3/lib/python3.11/site-packages/meshio/_cli/_main.py", line 52, in main
    return args.func(args)
           ^^^^^^^^^^^^^^^
  File "/home/andreas/miniconda3/lib/python3.11/site-packages/meshio/_cli/_convert.py", line 53, in convert
    mesh = read(args.infile, file_format=args.input_format)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andreas/miniconda3/lib/python3.11/site-packages/meshio/_helpers.py", line 71, in read
    return _read_file(Path(filename), file_format)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andreas/miniconda3/lib/python3.11/site-packages/meshio/_helpers.py", line 103, in _read_file
    return reader_map[file_format](str(path))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andreas/miniconda3/lib/python3.11/site-packages/meshio/vtu/_vtu.py", line 576, in read
    reader = VtuReader(filename)
             ^^^^^^^^^^^^^^^^^^^
  File "/home/andreas/miniconda3/lib/python3.11/site-packages/meshio/vtu/_vtu.py", line 441, in __init__
    self.cells, self.cell_data = _organize_cells(
                                 ^^^^^^^^^^^^^^^^
  File "/home/andreas/miniconda3/lib/python3.11/site-packages/meshio/vtu/_vtu.py", line 147, in _organize_cells
    cls, cell_data = vtk_cells_from_data(
                     ^^^^^^^^^^^^^^^^^^^^
  File "/home/andreas/miniconda3/lib/python3.11/site-packages/meshio/_vtk_common.py", line 112, in vtk_cells_from_data
    meshio_type = vtk_to_meshio_type[types[start]]
                                     ~~~~~^^^^^^^
IndexError: index 0 is out of bounds for axis 0 with size 0

Diagnose

$ pip freeze | grep meshio
meshio==5.3.4

Andlon avatar May 19 '23 17:05 Andlon