-
Notifications
You must be signed in to change notification settings - Fork 65
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
Open
marcorudolphflex
wants to merge
1
commit into
develop
Choose a base branch
from
FXC-3693-triangle-mesh-support-for-adjoint
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat(tidy3d): FXC-3693-triangle-mesh-support-for-adjoint #2962
marcorudolphflex
wants to merge
1
commit into
develop
from
FXC-3693-triangle-mesh-support-for-adjoint
+604
−1
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
2bc3946 to
862e0c2
Compare
862e0c2 to
427fe09
Compare
Contributor
Diff CoverageDiff: origin/develop...HEAD, staged and unstaged changes
Summary
tidy3d/components/geometry/mesh.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Greptile Overview
Updated On: 2025-11-05 16:16:52 UTC
Greptile Summary
Implemented autograd support for
TriangleMeshgeometries by adding_compute_derivativesmethod that evaluates boundary sensitivities using surface integral formulation. Reused the permittivity-gradient infrastructure fromPolySlab, enabling per-vertex gradient accumulation via barycentric weighting.Key Changes
_compute_derivativesmethod intidy3d/components/geometry/mesh.py:705that computes adjoint derivatives for triangle mesh vertices_collect_surface_samples,_triangle_area_and_normal,_subdivision_count,_get_barycentric_samples,_build_barycentric_samples, and_triangle_tangent_basis_barycentric_cacheto avoid recomputing sampling patterns for repeated subdivision levelsTriangleMeshinto existing autograd test suite with comprehensive validation testsIssues Found
==) instead of tolerance-based comparison (<=) violate custom rule for floating-point precision handlingConfidence Score: 4/5
tidy3d/components/geometry/mesh.py:806andtests/test_components/autograd/test_autograd_triangle_mesh.py:134,163,202Important Files Changed
File Analysis
_compute_derivativesmethod for mesh-based adjoint optimization using surface integral evaluation with barycentric samplingSequence Diagram
sequenceDiagram participant User participant TriangleMesh participant DerivativeInfo participant SurfaceSampler participant Interpolator participant GradientAccumulator User->>TriangleMesh: _compute_derivatives(derivative_info) TriangleMesh->>TriangleMesh: validate mesh_dataset exists TriangleMesh->>TriangleMesh: check paths for "surface_mesh" TriangleMesh->>TriangleMesh: convert triangles to gradient dtype TriangleMesh->>TriangleMesh: check if mesh outside sim bounds alt mesh outside bounds TriangleMesh-->>User: return zero gradients end TriangleMesh->>DerivativeInfo: adaptive_vjp_spacing() DerivativeInfo-->>TriangleMesh: spacing (dx) TriangleMesh->>SurfaceSampler: _collect_surface_samples(triangles, spacing, bounds) loop for each triangle face SurfaceSampler->>SurfaceSampler: compute area and normal SurfaceSampler->>SurfaceSampler: compute tangent basis (perp1, perp2) SurfaceSampler->>SurfaceSampler: determine subdivisions from area/spacing SurfaceSampler->>SurfaceSampler: get barycentric samples (cached) SurfaceSampler->>SurfaceSampler: compute sample points = barycentric @ triangle SurfaceSampler->>SurfaceSampler: filter points inside sim bounds SurfaceSampler->>SurfaceSampler: accumulate samples with weights end SurfaceSampler-->>TriangleMesh: samples (points, normals, perps, weights, faces, barycentric) alt no samples collected TriangleMesh-->>User: return zero gradients end TriangleMesh->>DerivativeInfo: create_interpolators(dtype) DerivativeInfo-->>TriangleMesh: interpolators TriangleMesh->>DerivativeInfo: evaluate_gradient_at_points(samples, interpolators) DerivativeInfo->>Interpolator: interpolate E_der_map at points DerivativeInfo->>Interpolator: interpolate D_der_map at points DerivativeInfo-->>TriangleMesh: gradient values (g) TriangleMesh->>GradientAccumulator: initialize triangle_grads (zeros) TriangleMesh->>GradientAccumulator: compute contrib_vec = weights * g * normals loop for each vertex (0, 1, 2) GradientAccumulator->>GradientAccumulator: scale by barycentric weight GradientAccumulator->>GradientAccumulator: np.add.at per-vertex gradients end GradientAccumulator-->>TriangleMesh: triangle_grads TriangleMesh-->>User: vjps[("mesh_dataset", "surface_mesh")]