-
Notifications
You must be signed in to change notification settings - Fork 66
Improve speed of Box derivative computation when in GeometryGroup #2867
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
base: develop
Are you sure you want to change the base?
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.
4 files reviewed, 1 comment
|
||
print("Overlaps (deg):") | ||
print(f"Box vs. PolySlab Adjoint: {box_polyslab_overlap_deg}") | ||
print(f"Box vs. PolySlab Finite Differnece: {fd_overlap_deg}") |
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.
syntax: Typo: 'Differnece' should be 'Difference'
print(f"Box vs. PolySlab Finite Differnece: {fd_overlap_deg}") | |
print(f"Box vs. PolySlab Finite Difference: {fd_overlap_deg}") |
Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/test_components/autograd/numerical/test_autograd_box_polyslab_numerical.py
Line: 395:395
Comment:
**syntax:** Typo: 'Differnece' should be 'Difference'
```suggestion
print(f"Box vs. PolySlab Finite Difference: {fd_overlap_deg}")
```
How can I resolve this? If you propose a fix, please make it concise.
Diff CoverageDiff: origin/develop...HEAD, staged and unstaged changes
Summary
tidy3d/components/geometry/base.pyLines 2494-2502 2494
2495 is_2d_map.append(np.isclose(extents[axis_idx], 0.0))
2496
2497 if np.all(is_2d_map):
! 2498 return 0.0
2499
2500 is_2d = np.any(is_2d_map)
2501
2502 sim_bounds_normal, sim_bounds_perp = self.pop_axis( |
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.
+99 / -346
love to see it 😄
Caught some minor issues but overall looks great, thanks @groberts-flex
# permittivity data | ||
eps_xyz = [derivative_info.eps_data[f"eps_{dim}{dim}"] for dim in "xyz"] | ||
if min_max_index == 0: | ||
if coord_normal_face <= derivative_info.simulation_bounds[0][axis_normal]: |
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.
I remember we discussed this at some point but I need a refresher 😆 So now we zero gradients for faces that sit at the simulation boundaries, right? It's an edge case, but I feel like these should be strict comparisons?
integration_bounds_perp = ( | ||
np.maximum(bounds_perp[nonzero_dim][0], sim_bounds_perp[nonzero_dim][0]), | ||
np.minimum(bounds_perp[nonzero_dim][1], sim_bounds_perp[nonzero_dim][1]), |
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.
It might be good to check here if the resulting interval is valid. If a box lies completely outside of the domain I think we'd end up with lower > upper
here? And then spacing_to_grid_points
will create invalid data for the interpolators I think.
Connect box face derivatives to
evaluate_gradient_at_points
inDerivativeInfo
so that interpolators can be shared when using inside aGeometryGroup
.Greptile Overview
Updated On: 2025-10-06 19:50:07 UTC
Summary
This PR optimizes the performance of Box geometry derivative computation when used inside a GeometryGroup by refactoring the implementation to leverage the existing `evaluate_gradient_at_points` infrastructure in `DerivativeInfo`. The key changes include:Major refactoring of Box derivative computation: The
_derivative_face
method intidy3d/components/geometry/base.py
was completely rewritten, removing 380+ lines of complex field integration code and replacing it with a simpler grid-based approach that builds spatial grids and evaluates gradients at specific points.Performance optimization through interpolator sharing: By connecting to the
evaluate_gradient_at_points
method, the new implementation allows interpolators to be shared when multiple Box geometries are processed together in a GeometryGroup, significantly improving computational efficiency.Code simplification: The refactoring eliminates the need for PEC-specific handling, complex epsilon data management, and manual interpolation logic, making the codebase more maintainable while leveraging existing gradient evaluation frameworks.
Comprehensive testing: A new test file was added to validate that the optimized Box gradient implementation produces mathematically equivalent results to the established PolySlab approach across different dimensional configurations (2D and 3D cases).
Test cleanup: Obsolete Box-specific test functions were removed since the functionality is now covered by the broader gradient evaluation system.
This change fits into the broader Tidy3D architecture by unifying Box derivative computation with the existing
DerivativeInfo
framework, enabling better resource sharing and performance optimization when working with multiple geometries in electromagnetic simulations.Changed Files
CHANGELOG.md
tidy3d/components/geometry/base.py
_derivative_face
method to use grid-based approach withevaluate_gradient_at_points
tests/test_components/autograd/numerical/test_autograd_box_polyslab_numerical.py
tests/test_components/test_geometry.py
Confidence score: 4/5
Sequence Diagram