Skip to content

Commit 98cea00

Browse files
Gregory Robertsyaugenst-flex
authored andcommitted
fix[adjoint]: correct scaling for adjoint sources created from field monitors
1 parent a25cb6c commit 98cea00

File tree

7 files changed

+464
-59
lines changed

7 files changed

+464
-59
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
### Fixed
1616
- Compatibility with `xarray>=2025.03`.
1717
- Inaccurate gradient when auto-grabbing permittivities for structures using `td.PolySlab` when using dispersive material models.
18+
- Fixed scaling for adjoint sources when differentiating with respect to `FieldData` to account for the mesh size of the monitor and thus the created source. This aligns adjoint gradient magnitudes with numerical finite difference gradients for field data.
1819

1920
## [2.8.1] - 2025-03-20
2021

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,10 @@ ignore = [
275275

276276
[tool.pytest.ini_options]
277277
# TODO: remove --assert=plain when https://github.com/scipy/scipy/issues/22236 is resolved
278-
addopts = "--cov=tidy3d --doctest-modules -n auto --dist worksteal --assert=plain"
278+
addopts = "--cov=tidy3d --doctest-modules -n auto --dist worksteal --assert=plain -m 'not numerical' "
279+
markers = [
280+
"numerical: marks numerical tests for adjoint gradients that require running simulations (deselect with '-m \"not numerical\"')",
281+
]
279282
env = ["MPLBACKEND=Agg", "OMP_NUM_THREADS=1"]
280283
doctest_optionflags = "NORMALIZE_WHITESPACE ELLIPSIS"
281284
norecursedirs = [

tests/conftest.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
2+
from pathlib import Path
23

34
import autograd
5+
import matplotlib as mpl
46
import matplotlib.pyplot as plt
57
import numpy as np
68
import psutil
@@ -68,3 +70,33 @@ def pytest_xdist_auto_num_workers(config):
6870
if os.getenv("GITHUB_ACTIONS"):
6971
return cores
7072
return max(1, cores - 1)
73+
74+
75+
@pytest.fixture
76+
def mpl_config_noninteractive():
77+
"""Configure matplotlib non-interactive backend for all tests in this module."""
78+
original_backend = mpl.get_backend()
79+
mpl.use("Agg")
80+
yield
81+
plt.close("all")
82+
mpl.use(original_backend)
83+
84+
85+
@pytest.fixture
86+
def mpl_config_interactive():
87+
"""Configure matplotlib interactive backend for all tests in this module."""
88+
original_backend = mpl.get_backend()
89+
mpl.use("TkAgg")
90+
yield
91+
mpl.use(original_backend)
92+
93+
94+
@pytest.fixture
95+
def dir_name(request):
96+
return request.param
97+
98+
99+
@pytest.fixture
100+
def create_directory(dir_name):
101+
if dir_name is not None:
102+
directory = Path(dir_name).mkdir(parents=True, exist_ok=True)

0 commit comments

Comments
 (0)