Skip to content

Commit 64d156f

Browse files
momchil-flexcopybara-bot
authored andcommitted
chore(tidy3d-client): bring back robust VTU planar axis detection fix (#3914)
Compute-RevId: 080b8c67e3bffd07374446037be265a627513fa3 Flex-RevId: dfe7bacef8c35d532e0169decbd7eba760ca6aa9
1 parent bd906ec commit 64d156f

File tree

4 files changed

+111
-5
lines changed

4 files changed

+111
-5
lines changed

docs/_ext/custom-robots.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44

55

66
def process_robots_txt(app, exception):
7-
if exception:
8-
return
97
# Get the path to the robots.txt file
108
robots_file = os.path.join(app.outdir, "robots.txt")
11-
if not os.path.isfile(robots_file):
12-
return
139
with open(robots_file) as f:
1410
contents = f.read()
1511

docs/_templates/module.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,9 @@
5050
{% endif %}
5151
{% endif %}
5252
{% endblock %}
53+
54+
55+
.. rubric:: Inherited Common Usage
56+
57+
.. include:: ../_custom_autosummary/{{ fullname }}.rst
58+
:optional:

tests/test_data/test_datasets.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,105 @@ def test_from_vtk():
11901190
_ = td.TriangularGridDataset.from_vtk("tests/data/gmsh_2d.vtk")
11911191

11921192

1193+
def test_triangular_from_vtu_near_planar_large_coordinates(tmp_path):
1194+
"""Regression for large-coordinate planar slices with small normal-axis jitter."""
1195+
pytest.importorskip("vtk")
1196+
import vtk
1197+
1198+
import tidy3d as td
1199+
1200+
tri_grid = td.TriangularGridDataset(
1201+
normal_axis=1,
1202+
normal_pos=0.0,
1203+
points=td.PointDataArray(
1204+
[
1205+
[100000.0, 0.0],
1206+
[101000.0, 0.0],
1207+
[100000.0, 690.0],
1208+
[101000.0, 690.0],
1209+
],
1210+
dims=("index", "axis"),
1211+
),
1212+
cells=td.CellDataArray([[0, 1, 2], [1, 2, 3]], dims=("cell_index", "vertex_index")),
1213+
values=td.IndexedDataArray(
1214+
[1.0, 2.0, 3.0, 4.0], coords={"index": np.arange(4)}, name="temp"
1215+
),
1216+
)
1217+
1218+
vtu_path = tmp_path / "near_planar_large_coords.vtu"
1219+
tri_grid.to_vtu(vtu_path)
1220+
1221+
# Inject tiny jitter in the nominally planar normal direction.
1222+
reader = vtk.vtkXMLUnstructuredGridReader()
1223+
reader.SetFileName(str(vtu_path))
1224+
reader.Update()
1225+
grid = reader.GetOutput()
1226+
points = grid.GetPoints()
1227+
for ind in range(points.GetNumberOfPoints()):
1228+
x, _, z = points.GetPoint(ind)
1229+
points.SetPoint(ind, x, 6e-6 if (ind % 2) == 0 else -6e-6, z)
1230+
points.Modified()
1231+
1232+
writer = vtk.vtkXMLUnstructuredGridWriter()
1233+
writer.SetFileName(str(vtu_path))
1234+
writer.SetInputData(grid)
1235+
writer.Write()
1236+
1237+
loaded = td.TriangularGridDataset.from_vtu(vtu_path, field="temp")
1238+
assert loaded.normal_axis == 1
1239+
assert abs(float(loaded.normal_pos)) < 1e-4
1240+
assert loaded.points.sizes["index"] == 4
1241+
1242+
1243+
def test_triangular_from_vtu_far_from_origin_small_extent(tmp_path):
1244+
"""Regression: tolerance must depend on extent, not absolute position."""
1245+
pytest.importorskip("vtk")
1246+
import vtk
1247+
1248+
import tidy3d as td
1249+
1250+
tri_grid = td.TriangularGridDataset(
1251+
normal_axis=1,
1252+
normal_pos=0.0,
1253+
points=td.PointDataArray(
1254+
[
1255+
[1e9, -2e9],
1256+
[1e9 + 2.0, -2e9],
1257+
[1e9, -2e9 + 1.0],
1258+
[1e9 + 2.0, -2e9 + 1.0],
1259+
],
1260+
dims=("index", "axis"),
1261+
),
1262+
cells=td.CellDataArray([[0, 1, 2], [1, 2, 3]], dims=("cell_index", "vertex_index")),
1263+
values=td.IndexedDataArray(
1264+
[1.0, 2.0, 3.0, 4.0], coords={"index": np.arange(4)}, name="temp"
1265+
),
1266+
)
1267+
1268+
vtu_path = tmp_path / "far_origin_small_extent.vtu"
1269+
tri_grid.to_vtu(vtu_path)
1270+
1271+
reader = vtk.vtkXMLUnstructuredGridReader()
1272+
reader.SetFileName(str(vtu_path))
1273+
reader.Update()
1274+
grid = reader.GetOutput()
1275+
points = grid.GetPoints()
1276+
for ind in range(points.GetNumberOfPoints()):
1277+
x, _, z = points.GetPoint(ind)
1278+
points.SetPoint(ind, x, 2e-7 if (ind % 2) == 0 else -2e-7, z)
1279+
points.Modified()
1280+
1281+
writer = vtk.vtkXMLUnstructuredGridWriter()
1282+
writer.SetFileName(str(vtu_path))
1283+
writer.SetInputData(grid)
1284+
writer.Write()
1285+
1286+
loaded = td.TriangularGridDataset.from_vtu(vtu_path, field="temp")
1287+
assert loaded.normal_axis == 1
1288+
assert abs(float(loaded.normal_pos)) < 1e-5
1289+
assert loaded.points.sizes["index"] == 4
1290+
1291+
11931292
def test_tetrahedral_from_vtk_obj_without_cell_types_array():
11941293
"""Regression test for VTK objects with missing ``GetCellTypesArray`` output."""
11951294
pytest.importorskip("vtk")

tidy3d/components/data/unstructured/triangular.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,12 @@ def _from_vtk_obj(
175175

176176
# detect zero size dimension
177177
bounds = np.max(points_numpy, axis=0) - np.min(points_numpy, axis=0)
178-
zero_dims = np.where(np.isclose(bounds, 0, atol=1e-6))[0]
178+
# VTU slices can accumulate small floating-point jitter in the nominally
179+
# zero-thickness direction. Scale tolerance with geometry extent (rather than
180+
# absolute coordinate value) to avoid origin-dependent behavior.
181+
size_scale = float(np.max(bounds)) if bounds.size > 0 else 0.0
182+
zero_dim_tol = max(1e-6, 2e-8 * size_scale)
183+
zero_dims = np.where(np.isclose(bounds, 0, atol=zero_dim_tol))[0]
179184

180185
if len(zero_dims) != 1:
181186
raise DataError(

0 commit comments

Comments
 (0)