-
Notifications
You must be signed in to change notification settings - Fork 66
feat(tidy3d): FXC-3693-triangle-mesh-support-for-adjoint #2962
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
feat(tidy3d): FXC-3693-triangle-mesh-support-for-adjoint #2962
Conversation
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.
5 files reviewed, 4 comments
862e0c2 to
427fe09
Compare
Diff CoverageDiff: origin/develop...HEAD, staged and unstaged changes
Summary
tidy3d/components/data/data_array.pytidy3d/components/geometry/mesh.pytidy3d/components/geometry/primitives.py |
0f34a50 to
3a8d240
Compare
26bbd4e to
e124a1e
Compare
e124a1e to
ac9b7f5
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.
10 files reviewed, 4 comments
yaugenst-flex
left a comment
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 @marcorudolphflex this is awesome!
a052631 to
ed0c3ca
Compare
b898b2a to
daee328
Compare
Greptile Overview
Greptile Summary
This PR implements autograd support for
TriangleMeshgeometries, enabling gradient-based optimization of arbitrary mesh surfaces in inverse design workflows.Key changes:
_compute_derivativesinTriangleMeshthat samples triangle surfaces adaptively, evaluates boundary gradients via the existing permittivity-gradient infrastructure, and accumulates sensitivities onto per-vertex data arrays using barycentric interpolationDataArray.__init__to convert numpy object arrays containing autograd boxes, enabling traced geometry parameters to flow through mesh constructionto_stlexport method for TriangleMesh with binary/ASCII format supportIssues found:
np.iscloseor<=) instead of==Confidence Score: 4/5
Important Files Changed
File Analysis
Sequence Diagram
sequenceDiagram participant User participant Autograd participant TriangleMesh participant DerivativeInfo participant Sampling participant Interpolator participant GradAccum User->>Autograd: Define objective with TriangleMesh parameters Autograd->>TriangleMesh: Forward pass (construct geometry) TriangleMesh->>TriangleMesh: _triangles_to_trimesh (strip autograd boxes) Note over TriangleMesh: Convert ArrayBox to static arrays<br/>for trimesh compatibility Autograd->>TriangleMesh: Backward pass (_compute_derivatives) TriangleMesh->>TriangleMesh: Validate mesh_dataset exists TriangleMesh->>DerivativeInfo: Check simulation_bounds alt Mesh outside simulation TriangleMesh-->>Autograd: Return zero gradients else Mesh intersects simulation TriangleMesh->>Sampling: _collect_surface_samples(triangles, spacing, bounds) loop For each triangle face Sampling->>Sampling: Compute area and normal alt Collapsed axis (2D simulation) Sampling->>Sampling: _triangle_plane_segments (intersect with plane) Sampling->>Sampling: Sample along intersection segments else Full 3D Sampling->>Sampling: _subdivision_count (adaptive based on area + edges) Sampling->>Sampling: _get_barycentric_samples (cached patterns) end Sampling->>Sampling: Filter samples inside simulation bounds end Sampling-->>TriangleMesh: Return {points, normals, perps, weights, faces, barycentric} TriangleMesh->>DerivativeInfo: create_interpolators if needed DerivativeInfo-->>Interpolator: Build field interpolators TriangleMesh->>DerivativeInfo: evaluate_gradient_at_points(samples, interpolators) DerivativeInfo->>Interpolator: Interpolate fields at sample points Interpolator-->>DerivativeInfo: Return gradient values g DerivativeInfo-->>TriangleMesh: Boundary gradient g TriangleMesh->>GradAccum: Accumulate per-vertex contributions loop For each vertex (0, 1, 2) GradAccum->>GradAccum: scaled = (weights * g * normals) * barycentric[:, vertex] GradAccum->>GradAccum: np.add.at(triangle_grads[:, vertex], faces, scaled) end GradAccum-->>TriangleMesh: triangle_grads array TriangleMesh-->>Autograd: vjps[("mesh_dataset", "surface_mesh")] end Autograd-->>User: Final gradients w.r.t. mesh verticesContext used:
dashboard- Use a tolerance-based comparison (e.g., np.isclose) for floating-point numbers instead of direct equ... (source)dashboard- In changelogs, enclose code identifiers (class, function names) in backticks and use specific names ... (source)