Skip to content

Conversation

@yaugenst-flex
Copy link
Collaborator

@yaugenst-flex yaugenst-flex commented Jun 18, 2025

This PR removes the "mysterious" factors in the adjoint gradient calculation for CustomPoleResidue and refactors the derivative logic for both PoleResidue and CustomPoleResidue. Tested on the gold nanoantenna notebook.

The PR removes more code than it adds so you know it's good 😉

Greptile Summary

Refactors adjoint gradient calculations for PoleResidue models to use analytical derivatives instead of automatic differentiation, improving mathematical precision and code clarity.

  • Replaced autograd with analytical derivative implementation in tidy3d/components/medium.py for PoleResidue calculations
  • Introduced _get_vjps_from_params helper method to consolidate derivative logic for both PoleResidue and CustomPoleResidue
  • Updated derivative testing in tests/test_autograd.py to properly handle complex conjugates
  • Removed arbitrary numerical factors from CustomPoleResidue derivative calculations

@yaugenst-flex yaugenst-flex self-assigned this Jun 18, 2025
@yaugenst-flex yaugenst-flex force-pushed the yaugenst-flex/refactor-poleresidue branch from 4033adc to 8c478c4 Compare June 18, 2025 18:11
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, 2 comments
Edit PR Review Bot Settings | Greptile

@github-actions
Copy link
Contributor

Diff Coverage

Diff: origin/develop...HEAD, staged and unstaged changes

  • tidy3d/components/medium.py (100%)

Summary

  • Total: 24 lines
  • Missing: 0 lines
  • Coverage: 100%

@momchil-flex momchil-flex added 2.9 will go into version 2.9.* rc2 2nd pre-release labels Jun 19, 2025
Copy link
Collaborator

@tylerflex tylerflex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approved, thanks yannick!

@yaugenst-flex yaugenst-flex force-pushed the yaugenst-flex/refactor-poleresidue branch from 8c478c4 to 3ffca81 Compare June 23, 2025 11:25
Copy link
Contributor

@groberts-flex groberts-flex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great, I like this! like the simplification and re-use!

The derivative logic for both `PoleResidue` and the spatially-varying `CustomPoleResidue` has been refactored to use an analytical formula. This is achieved by:
- Replacing the `autograd`-based implementations in both classes with the analytical formulas for the pole-residue model derivatives.
- Creating a single, shared `staticmethod` (`_get_vjps_from_params`) in the base `PoleResidue` class to house this logic.
- Updating the `_compute_derivatives` methods in both classes to be simple wrappers that call this new helper.
@yaugenst-flex yaugenst-flex force-pushed the yaugenst-flex/refactor-poleresidue branch from 3ffca81 to a280a6d Compare June 24, 2025 20:44
@yaugenst-flex yaugenst-flex enabled auto-merge (rebase) June 24, 2025 20:46
@yaugenst-flex yaugenst-flex merged commit 994bdc2 into develop Jun 25, 2025
46 of 48 checks passed
@yaugenst-flex yaugenst-flex deleted the yaugenst-flex/refactor-poleresidue branch June 25, 2025 05:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.9 will go into version 2.9.* rc2 2nd pre-release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants