-
Notifications
You must be signed in to change notification settings - Fork 63
ability to read unstructured data from legacy vtk format #2569
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
Conversation
Diff CoverageDiff: origin/develop...HEAD, staged and unstaged changes
Summary
tidy3d/components/data/unstructured/base.pyLines 486-499 486 @staticmethod
487 @requires_vtk
488 def _read_vtkLegacyFile(fname: str):
489 """Load a grid from a legacy `.vtk` file."""
! 490 reader = vtk["mod"].vtkGenericDataObjectReader()
! 491 reader.SetFileName(fname)
! 492 reader.Update()
! 493 grid = reader.GetOutput()
494
! 495 return grid
496
497 @classmethod
498 @abstractmethod
499 @requires_vtk Lines 592-601 592 -------
593 UnstructuredGridDataset
594 Unstructured data.
595 """
! 596 grid = cls._read_vtkLegacyFile(file)
! 597 return cls._from_vtk_obj(
598 grid,
599 field=field,
600 remove_degenerate_cells=remove_degenerate_cells,
601 remove_unused_points=remove_unused_points, Lines 636-644 636 log.warning(
637 "No point data is found in a VTK object. '.values' will be initialized to zeros."
638 )
639 values_numpy = np.zeros(num_points)
! 640 values_coords = {"index": np.arange(num_points)}
641 values_name = None
642
643 else:
644 field_ind = field if isinstance(field, str) else 0 tidy3d/components/data/unstructured/tetrahedral.pyLines 111-126 111 # verify cell_types
112 cells_types = vtk["vtk_to_numpy"](vtk_obj.GetCellTypesArray())
113 invalid_cells = cells_types != cls._vtk_cell_type()
114 if any(invalid_cells):
! 115 if ignore_invalid_cells:
! 116 cell_offsets = vtk["vtk_to_numpy"](vtk_obj.GetCells().GetOffsetsArray())
! 117 valid_cell_offsets = cell_offsets[:-1][invalid_cells == 0]
! 118 cells_numpy = cells_numpy[
119 np.ravel(valid_cell_offsets[:, None] + np.arange(4, dtype=int)[None, :])
120 ]
121 else:
! 122 raise DataError("Only tetrahedral 'vtkUnstructuredGrid' is currently supported")
123
124 # pack point and cell information into Tidy3D arrays
125 num_cells = len(cells_numpy) // cls._cell_num_vertices()
126 cells_numpy = np.reshape(cells_numpy, (num_cells, cls._cell_num_vertices())) tidy3d/components/data/unstructured/triangular.pyLines 147-161 147 # verify cell_types
148 cell_offsets = vtk["vtk_to_numpy"](cells_vtk.GetOffsetsArray())
149 invalid_cells = np.diff(cell_offsets) != cls._cell_num_vertices()
150 if any(invalid_cells):
! 151 if ignore_invalid_cells:
! 152 valid_cell_offsets = cell_offsets[:-1][invalid_cells == 0]
! 153 cells_numpy = cells_numpy[
154 np.ravel(valid_cell_offsets[:, None] + np.arange(3, dtype=int)[None, :])
155 ]
156 else:
! 157 raise DataError(
158 "Only triangular 'vtkUnstructuredGrid' or 'vtkPolyData' can be converted into "
159 "'TriangularGridDataset'."
160 ) |
3583dbe
to
e486b44
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 files reviewed, 1 comment
Edit PR Review Bot Settings | Greptile
np.ravel(valid_cell_offsets[:, None] + np.arange(4, dtype=int)[None, :]) | ||
] | ||
else: | ||
raise DataError("Only tetrahedral 'vtkUnstructuredGrid' is currently supported") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Error message should specify which invalid cell types were found to help debugging
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me and seems to work in my separate meshing pipeline PR for both 3D and 2D meshes. Just needs some tests yeah.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Daniil. This looks good.
In terms of testing, how do you plan on doing this? I'm should probably test a function in a different PR and I didn't really know how to do it.
Closing in favor of #2493 |
.vtk
in addition to existing.vtu
todo:
Greptile Summary
Enhances VTK file support by adding legacy
.vtk
format compatibility and flexible cell type handling in unstructured grids..vtk
) files in addition to existing.vtu
formatignore_invalid_cells
parameter to gracefully handle non-triangular/-tetrahedral cells instead of erroring_from_vtk_obj
to support mixed cell types whenignore_invalid_cells=True