-
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
tests/test_components/autograd/numerical/test_autograd_box_polyslab_numerical.py
Outdated
Show resolved
Hide resolved
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( Lines 2521-2529 2521
2522 def compute_integration_weight(grid_points):
2523 grid_spacing = grid_points[1] - grid_points[0]
2524 if grid_spacing == 0.0:
! 2525 integration_weight = 1.0 / len(grid_points)
2526 else:
2527 integration_weight = grid_points[1] - grid_points[0]
2528
2529 return integration_weight Lines 2541-2549 2541 np.minimum(bounds_perp[nonzero_dim][1], sim_bounds_perp[nonzero_dim][1]),
2542 )
2543
2544 if not verify_integration_interval(integration_bounds_perp):
! 2545 return 0.0
2546
2547 grid_points_linear = spacing_to_grid_points(
2548 adaptive_spacing, integration_bounds_perp[0], integration_bounds_perp[1]
2549 ) Lines 2570-2578 2570 ),
2571 )
2572
2573 if not np.all([verify_integration_interval(b) for b in integration_bounds_perp]):
! 2574 return 0.0
2575
2576 grid_points_perp_1 = spacing_to_grid_points(
2577 adaptive_spacing, integration_bounds_perp[0][0], integration_bounds_perp[0][1]
2578 ) |
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
4ef336e
to
a0def89
Compare
a0def89
to
0cfde39
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.
One last thing I spotted but then it's ready to go I think.
dims_perp, bounds_perp, zero_dimension | ||
# set up grid points to pass into evaluate_gradient_at_points | ||
grid_points[:, axis_perp[nonzero_dim]] = grid_points_linear | ||
grid_points[:, axis_perp[zero_dim]] = bounds_perp[0][zero_dim] |
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.
Just noticed this, I think here since you always take the first perpendicular axis, this would break if the zero-extent axis is the second entry, right? I think the fix would just be grid_points[:, axis_perp[zero_dim]] = bounds_perp[zero_dim][0]
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