Skip to content

Commit a886cef

Browse files
marcorudolphflexmahlau-flex
authored andcommitted
fix(tidy3d): FXC-4824-fix-corrupted-data-in-vtk-arrays
1 parent 14468d6 commit a886cef

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

tidy3d/components/data/unstructured/base.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ def _get_values_from_vtk(
729729
f" of grid points ({num_points})."
730730
)
731731

732-
values_numpy = vtk["vtk_to_numpy"](array_vtk)
732+
values_numpy = np.array(vtk["vtk_to_numpy"](array_vtk), copy=True)
733733
values_name = array_vtk.GetName()
734734

735735
# vtk doesn't support complex numbers
@@ -1260,12 +1260,17 @@ def _interp_vtk(
12601260
array_id = 0 if self.values.name is None else self.values.name
12611261

12621262
# TODO: generalize this
1263-
values_numpy = vtk["vtk_to_numpy"](interpolated.GetPointData().GetAbstractArray(array_id))
1263+
values_numpy = np.array(
1264+
vtk["vtk_to_numpy"](interpolated.GetPointData().GetAbstractArray(array_id)), copy=True
1265+
)
12641266

12651267
# fill points without interpolated values
12661268
if fill_value != 0:
1267-
mask = vtk["vtk_to_numpy"](
1268-
interpolated.GetPointData().GetAbstractArray("vtkValidPointMask")
1269+
mask = np.array(
1270+
vtk["vtk_to_numpy"](
1271+
interpolated.GetPointData().GetAbstractArray("vtkValidPointMask")
1272+
),
1273+
copy=True,
12691274
)
12701275
values_numpy[mask != 1] = fill_value
12711276

tidy3d/components/data/unstructured/tetrahedral.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,24 @@ def _from_vtk_obj(
105105
"""Initialize from a vtkUnstructuredGrid instance."""
106106

107107
# read point, cells, and values info from a vtk instance
108-
cells_numpy = vtk["vtk_to_numpy"](vtk_obj.GetCells().GetConnectivityArray())
109-
points_numpy = vtk["vtk_to_numpy"](vtk_obj.GetPoints().GetData())
108+
cells_numpy = np.array(
109+
vtk["vtk_to_numpy"](vtk_obj.GetCells().GetConnectivityArray()),
110+
copy=True,
111+
)
112+
points_numpy = np.array(vtk["vtk_to_numpy"](vtk_obj.GetPoints().GetData()), copy=True)
110113
values = cls._get_values_from_vtk(
111114
vtk_obj, len(points_numpy), field, values_type, expect_complex
112115
)
113116

114117
# verify cell_types
115-
cells_types = vtk["vtk_to_numpy"](vtk_obj.GetCellTypesArray())
118+
cells_types = np.array(vtk["vtk_to_numpy"](vtk_obj.GetCellTypesArray()), copy=True)
116119
invalid_cells = cells_types != cls._vtk_cell_type()
117120
if any(invalid_cells):
118121
if ignore_invalid_cells:
119-
cell_offsets = vtk["vtk_to_numpy"](vtk_obj.GetCells().GetOffsetsArray())
122+
cell_offsets = np.array(
123+
vtk["vtk_to_numpy"](vtk_obj.GetCells().GetOffsetsArray()),
124+
copy=True,
125+
)
120126
valid_cell_offsets = cell_offsets[:-1][invalid_cells == 0]
121127
cells_numpy = cells_numpy[
122128
np.ravel(

tidy3d/components/data/unstructured/triangular.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,13 @@ def _from_vtk_obj(
149149
elif isinstance(vtk_obj, vtk["mod"].vtkUnstructuredGrid):
150150
cells_vtk = vtk_obj.GetCells()
151151

152-
cells_numpy = vtk["vtk_to_numpy"](cells_vtk.GetConnectivityArray())
152+
cells_numpy = np.array(
153+
vtk["vtk_to_numpy"](cells_vtk.GetConnectivityArray()),
154+
copy=True,
155+
)
153156

154157
# verify cell_types
155-
cell_offsets = vtk["vtk_to_numpy"](cells_vtk.GetOffsetsArray())
158+
cell_offsets = np.array(vtk["vtk_to_numpy"](cells_vtk.GetOffsetsArray()), copy=True)
156159
invalid_cells = np.diff(cell_offsets) != cls._cell_num_vertices()
157160
if np.any(invalid_cells):
158161
if ignore_invalid_cells:
@@ -166,7 +169,7 @@ def _from_vtk_obj(
166169
"'TriangularGridDataset'."
167170
)
168171

169-
points_numpy = vtk["vtk_to_numpy"](vtk_obj.GetPoints().GetData())
172+
points_numpy = np.array(vtk["vtk_to_numpy"](vtk_obj.GetPoints().GetData()), copy=True)
170173

171174
# data values are read directly into Tidy3D array
172175
values = cls._get_values_from_vtk(
@@ -250,7 +253,7 @@ def plane_slice(self, axis: Axis, pos: float) -> XrDataArray:
250253

251254
# perform slicing in vtk and get unprocessed points and values
252255
slice_vtk = self._plane_slice_raw(axis=axis, pos=pos)
253-
points_numpy = vtk["vtk_to_numpy"](slice_vtk.GetPoints().GetData())
256+
points_numpy = np.array(vtk["vtk_to_numpy"](slice_vtk.GetPoints().GetData()), copy=True)
254257
values = self._get_values_from_vtk(
255258
slice_vtk,
256259
len(points_numpy),

0 commit comments

Comments
 (0)