diff --git a/CHANGELOG.md b/CHANGELOG.md index 38032051f4..aecd223652 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- Minor gradient direction and normalization fixes for polyslab, field monitors, and diffraction monitors in autograd. + ## [2.7.5] - 2024-10-16 ### Added diff --git a/docs/notebooks b/docs/notebooks index dbfc67aed5..e24f0c4d19 160000 --- a/docs/notebooks +++ b/docs/notebooks @@ -1 +1 @@ -Subproject commit dbfc67aed533cb656ab2eeb3c1fad2e54e7ca906 +Subproject commit e24f0c4d191c8636345a1deb9f8651fe5033c5ca diff --git a/tidy3d/components/data/monitor_data.py b/tidy3d/components/data/monitor_data.py index 361d448036..bfa6ce0e1c 100644 --- a/tidy3d/components/data/monitor_data.py +++ b/tidy3d/components/data/monitor_data.py @@ -1037,7 +1037,7 @@ def to_adjoint_point_sources(self, fwidth: float) -> List[PointDipole]: for freq0 in field_component.coords["f"]: omega0 = 2 * np.pi * freq0 - scaling_factor = 1 / (MU_0 * omega0) + scaling_factor = 33 / (MU_0 * omega0) forward_amp = self.get_amplitude(field_component.sel(f=freq0)) @@ -1076,7 +1076,7 @@ def to_adjoint_field_sources(self, fwidth: float) -> List[CustomCurrentSource]: for name, field_component in self.field_components.items(): # get the VJP values at frequency and apply adjoint phase field_component = field_component.sel(f=freq0) - values = -1j * field_component.values + values = 2 * -1j * field_component.values # make source go backwards if "H" in name: @@ -2974,7 +2974,7 @@ def adjoint_source_amp(self, amp: DataArray, fwidth: float) -> PlaneWave: theta_data, phi_data = self.angles angle_sel_kwargs = dict(orders_x=int(order_x), orders_y=int(order_y), f=float(freq0)) angle_theta = float(theta_data.sel(**angle_sel_kwargs)) - angle_phi = np.pi + float(phi_data.sel(**angle_sel_kwargs)) + angle_phi = float(phi_data.sel(**angle_sel_kwargs)) # if the angle is nan, this amplitude is set to 0 in the fwd pass, so should skip adj if np.isnan(angle_theta): @@ -3000,7 +3000,7 @@ def adjoint_source_amp(self, amp: DataArray, fwidth: float) -> PlaneWave: center=self.monitor.center, source_time=GaussianPulse( amplitude=abs(src_amp), - phase=np.pi + np.angle(src_amp), + phase=np.angle(src_amp), freq0=freq0, fwidth=fwidth, ), diff --git a/tidy3d/components/geometry/base.py b/tidy3d/components/geometry/base.py index 45a6453cbe..4f0f30a704 100644 --- a/tidy3d/components/geometry/base.py +++ b/tidy3d/components/geometry/base.py @@ -2433,7 +2433,7 @@ def derivative_face( eps_xyz = [derivative_info.eps_data[f"eps_{dim}{dim}"] for dim in "xyz"] # number of cells from the edge of data to register "inside" (index = num_cells_in - 1) - num_cells_in = 3 + num_cells_in = 4 # if not enough data, just use best guess using eps in medium and simulation needs_eps_approx = any(len(eps.coords[dim_normal]) <= num_cells_in for eps in eps_xyz) diff --git a/tidy3d/components/geometry/polyslab.py b/tidy3d/components/geometry/polyslab.py index c085faca0c..7a3c7ba98b 100644 --- a/tidy3d/components/geometry/polyslab.py +++ b/tidy3d/components/geometry/polyslab.py @@ -1425,7 +1425,7 @@ def compute_derivative_vertices(self, derivative_info: DerivativeInfo) -> Traced vjps_edges = 0.0 # perform D-normal integral - contrib_D = delta_eps_inv * D_der_norm + contrib_D = -delta_eps_inv * D_der_norm vjps_edges += contrib_D # perform E-perpendicular integrals @@ -1438,12 +1438,9 @@ def compute_derivative_vertices(self, derivative_info: DerivativeInfo) -> Traced edge_areas = edge_lengths # correction to edge area based on sidewall distance along slab axis - dim_axis = "xyz"[self.axis] - field_coords_axis = derivative_info.E_der_map[f"E{dim_axis}"].coords[dim_axis] - if len(field_coords_axis) > 1: - slab_height = abs(float(np.squeeze(np.diff(self.slab_bounds)))) - if not np.isinf(slab_height): - edge_areas *= slab_height + slab_height = abs(float(np.squeeze(np.diff(self.slab_bounds)))) + if not np.isinf(slab_height): + edge_areas *= slab_height vjps_edges *= edge_areas @@ -1452,6 +1449,7 @@ def compute_derivative_vertices(self, derivative_info: DerivativeInfo) -> Traced vjps_edges_in_plane = vjps_edges.values.reshape((num_vertices, 1)) * normal_vectors_in_plane vjps_vertices = vjps_edges_in_plane + np.roll(vjps_edges_in_plane, axis=0, shift=-1) + vjps_vertices /= 2.0 # each vertex is effected only 1/2 by each edge # sign change if counter clockwise, because normal direction is flipped if self.is_ccw: