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

fix calculate_fd_gradient to deal with design pixel weights near 0 or 1 #2073

Merged
merged 1 commit into from
May 26, 2022
Merged
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
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