Skip to content
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

Fast Dispersion Fitter: keep poles slightly away from input freqs #1672

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Bug in plotting and computing tilted plane intersections of transformed 0 thickness geometries.
- `Simulation.to_gdspy()` and `Simulation.to_gdstk()` now place polygons in GDS layer `(0, 0)` when no `gds_layer_dtype_map` is provided instead of erroring.
- `task_id` now properly stored in `JaxSimulationData`.
- Bug in `FastDispersionFitter` when poles move close to input frequencies.

## [2.7.0rc1] - 2024-04-22

Expand Down
9 changes: 9 additions & 0 deletions tests/test_plugins/test_dispersion_fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ def test_lossy_dispersion(random_data, mock_remote_api):
)
medium3, rms3 = fitter.fit(advanced_param=advanced_param)

# test that poles can be close but not exactly equal to provided freqs
N = 2
wvl_um = np.linspace(0.5, 0.6, N)
n_data = np.ones(N) * 2
k_data = np.ones(N) * 0.5

fitter = FastDispersionFitter(wvl_um=wvl_um, n_data=n_data, k_data=k_data)
medium, rms_error = fitter.fit(max_num_poles=2, tolerance_rms=1e-3)


def test_constant_loss_tangent():
"""perform fitting on constant loss tangent material"""
Expand Down
13 changes: 13 additions & 0 deletions tidy3d/plugins/dispersion/fit_fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
# this avoids divide by zero errors with lossless poles
SCALE_FACTOR = 1.01

# when poles are close to omega, can cause invalid response function, and we reject model
OMEGA_POLE_CLOSE_ATOL = 1e-10


class AdvancedFastFitterParam(Tidy3dBaseModel):
"""Advanced fast fitter parameters."""
Expand Down Expand Up @@ -520,6 +523,16 @@ def iterate_fit(self) -> FastFitterData:
"""

model = self.iterate_poles()

# if any of the poles align with the test frequencies exactly, we reject the new model
if any(
np.isclose(omega, 1j * pole, rtol=0, atol=OMEGA_POLE_CLOSE_ATOL)
or np.isclose(omega, -1j * pole, rtol=0, atol=OMEGA_POLE_CLOSE_ATOL)
for omega in self.omega
for pole in self.complex_poles
):
return self
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we want to warn or anything here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks. @weiliangjin2021 , do you think we should warn here? I actually think the resulting model in these cases is as expected / as desired, so no need to warn the user. The fix is just preventing the fitter from trying to further improve a model which is already quite good, when the improvement would limit in this error. But I'm not sure exactly about your use case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could also use fp_eps instead of 1e-10. Might be more robust that way.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, since it's accurate enough, I think no need to warn.


model = model.fit_residues()

return model
Expand Down
Loading