Skip to content

Commit

Permalink
fix calculate_fd_gradient to deal with design pixel weights near 0 or…
Browse files Browse the repository at this point in the history
… 1 (#2073)
  • Loading branch information
mawc2019 authored May 26, 2022
1 parent 1265469 commit 0d89d88
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions python/adjoint/optimization_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ def calculate_fd_gradient(
"The requested number of gradients must be less than or equal to the total number of design parameters."
)

assert db < 0.2, "The step size of finite difference is too large."

# cleanup simulation object
self.sim.reset_meep()
self.sim.change_sources(self.forward_sources)
Expand All @@ -339,7 +341,10 @@ def calculate_fd_gradient(
self.sim.reset_meep()

# assign new design vector
b0[k] -= db
in_interior = True # b0[k] is not too close to the boundaries 0 and 1
if b0[k] < db or b0[k]+db > 1: in_interior = False # b0[k] is too close to 0 or 1

if b0[k] >= db: b0[k] -= db
self.design_regions[design_variables_idx].update_design_parameters(
b0)

Expand Down Expand Up @@ -367,7 +372,9 @@ def calculate_fd_gradient(
self.sim.reset_meep()

# assign new design vector
b0[k] += 2 * db # central difference rule...
if in_interior: b0[k] += 2 * db # central difference rule...
else: b0[k] += db # forward or backward difference...

self.design_regions[design_variables_idx].update_design_parameters(
b0)

Expand All @@ -394,7 +401,7 @@ def calculate_fd_gradient(
# estimate derivative
# -------------------------------------------- #
fd_gradient.append([
np.squeeze((fp[fi] - fm[fi]) / (2 * db))
np.squeeze((fp[fi] - fm[fi]) / db / (2 if in_interior else 1))
for fi in range(len(self.objective_functions))
])

Expand Down

0 comments on commit 0d89d88

Please sign in to comment.