correct normal for box minimum face in shape gradient #3050
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.
@yaugenst-flex , this is the error I was mentioning this morning. I was going to include it in my boundary handling PR but just in case that takes a little bit to fully get in, I figured it was worth having this fix in sooner since it's needed for correct PEC handling.
Greptile Overview
Greptile Summary
This PR fixes a critical bug in the
Boxgeometry's shape gradient computation where the normal vector for the minimum face was incorrectly pointing inward (+1) instead of outward (-1). Theevaluate_gradient_at_pointsmethod requires outward-pointing normals for correct PEC (Perfect Electric Conductor) boundary handling in adjoint optimization.Key changes:
tidy3d/components/geometry/base.py:2743to conditionally set normal direction based onmin_max_indexmin_max_index == 0(minimum face), normal now points in -1 direction (outward from lower bound)min_max_index == 1(maximum face), normal continues to point in +1 direction (outward from upper bound)Confidence Score: 5/5
Important Files Changed
File Analysis
Sequence Diagram
sequenceDiagram participant Client as Adjoint Client participant BoxFaces as Box._derivative_faces participant BoxFace as Box._derivative_face participant Eval as DerivativeInfo.evaluate_gradient_at_points Client->>BoxFaces: Compute shape gradients BoxFaces->>BoxFaces: Loop over min_max_index BoxFaces->>BoxFace: Call with min_max_index and axis_normal BoxFace->>BoxFace: Get coord from bounds_normal[min_max_index] BoxFace->>BoxFace: Build grid_points on face Note over BoxFace: Critical Fix: Set normal direction alt min_max_index equals 0 BoxFace->>BoxFace: Set normals[:, axis_normal] = -1 else min_max_index equals 1 BoxFace->>BoxFace: Set normals[:, axis_normal] = +1 end BoxFace->>Eval: Call with spatial_coords and normals Note over Eval: Requires outward-pointing normals for correct PEC handling Eval-->>BoxFace: Return gradient values BoxFace->>BoxFace: Compute vjp_value with integration_weight BoxFace-->>BoxFaces: Return vjp_value BoxFaces-->>Client: Return vjp_faces array