Skip to content

Commit

Permalink
more fix FieldData
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerflex committed Aug 27, 2024
1 parent 505adb7 commit b211dc8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
4 changes: 2 additions & 2 deletions tests/test_components/test_autograd.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
TEST_POLYSLAB_SPEED = False

# whether to run numerical gradient tests, off by default because it runs real simulations
RUN_NUMERICAL = True
RUN_NUMERICAL = False

TEST_MODES = ("pipeline", "adjoint", "speed")
TEST_MODE = "speed" if TEST_POLYSLAB_SPEED else "pipeline"
Expand Down Expand Up @@ -618,7 +618,7 @@ def test_polyslab_axis_ops(axis):


@pytest.mark.skipif(not RUN_NUMERICAL, reason="Numerical gradient tests runs through web API.")
@pytest.mark.parametrize("structure_key, monitor_key", (("medium", "field_vol"),))
@pytest.mark.parametrize("structure_key, monitor_key", (("medium", "mode"),))
def test_autograd_numerical(structure_key, monitor_key):
"""Test an objective function through tidy3d autograd."""

Expand Down
36 changes: 33 additions & 3 deletions tidy3d/components/data/monitor_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,14 +1093,44 @@ def shift_value(coords) -> float:
# Define source dataset
# Offset coordinates by source center since local coords are assumed in CustomCurrentSource

# import pdb; pdb.set_trace()

for freq0 in tuple(self.field_components.values())[0].coords["f"]:
src_field_components = {}
for name, field_component in self.field_components.items():
field_component = field_component.sel(f=freq0)
forward_amps = field_component.values
values = -1j * forward_amps

# values = -1j * forward_amps
# # rms_error = 0.0012
# # |grad| / |grad_num| = 0.6020

# values = 1 * forward_amps
# # rms_error = 2.0000
# # |grad| / |grad_num| = 0.2679

# values = +1j * forward_amps
# # rms_error = 2.0000
# # |grad| / |grad_num| = 0.6020

# values = -1 * forward_amps
# # rms_error = 0.0013
# # |grad| / |grad_num| = 0.2679

# values = -1j * np.conj(forward_amps)
# # rms_error = 2.0000
# # |grad| / |grad_num| = 0.0193

values = np.conj(forward_amps)
# rms_error = 0.0012
# |grad| / |grad_num| = 0.9772

# values = +1j * np.conj(forward_amps)
# # rms_error = 0.0013
# # |grad| / |grad_num| = 0.0193

# values = -1 * np.conj(forward_amps)
# # rms_error = 2.0000
# # |grad| / |grad_num| = 0.9772

if "H" in name:
values *= -1
coords = dict(field_component.coords.copy())
Expand Down

0 comments on commit b211dc8

Please sign in to comment.