Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Alec Hammond committed Apr 11, 2024
1 parent ebe69b3 commit e14534f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 41 deletions.
12 changes: 6 additions & 6 deletions python/adjoint/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
convolution filters (kernels), projection operators, and morphological
transforms.
"""

import sys
from typing import List, Tuple, Union

Expand Down Expand Up @@ -329,9 +330,7 @@ def conic_filter(
"""
Nx, Ny, X, Y = mesh_grid(radius, Lx, Ly, resolution, periodic_axes)
x = x.reshape(Nx, Ny) # Ensure the input is 2d
h = npa.where(
X**2 + Y**2 < radius**2, (1 - np.sqrt(abs(X**2 + Y**2)) / radius), 0
)
h = npa.where(X**2 + Y**2 < radius**2, (1 - np.sqrt(abs(X**2 + Y**2)) / radius), 0)
return convolve_design_weights_and_kernel(x, h, periodic_axes)


Expand Down Expand Up @@ -706,7 +705,6 @@ def tanh_projection(x: np.ndarray, beta: float, eta: float) -> np.ndarray:
)



def smoothed_projection(
x_smoothed: ArrayLikeType,
beta: float,
Expand Down Expand Up @@ -848,9 +846,11 @@ def smoothed_projection(
# Finally, we project the extents of our range.
x_plus_eff_projected = tanh_projection(x_plus_eff, beta=beta, eta=eta)
x_minus_eff_projected = tanh_projection(x_minus_eff, beta=beta, eta=eta)

# Only apply smoothing to interfaces
x_projected_smoothed = (1 - fill_factor) * x_minus_eff_projected + (fill_factor) * x_plus_eff_projected
x_projected_smoothed = (1 - fill_factor) * x_minus_eff_projected + (
fill_factor
) * x_plus_eff_projected
return npa.where(
needs_smoothing,
x_projected_smoothed,
Expand Down
52 changes: 17 additions & 35 deletions python/examples/waveguide_crossing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
optimization.
"""


from typing import Callable, List, Optional, Tuple

import meep.adjoint as mpa
Expand Down Expand Up @@ -244,7 +243,7 @@ def nlopt_fom(

data.append(
[
np.squeeze(x.copy().reshape(Nx,Ny)),
np.squeeze(x.copy().reshape(Nx, Ny)),
np.squeeze(mapping(x).copy().reshape(Nx, Ny)),
np.squeeze(backprop_gradient.reshape(Nx, Ny)),
]
Expand Down Expand Up @@ -362,7 +361,7 @@ def run_shape_optimization(
results.append(np.real(f0))
data.append(
[
np.squeeze(x_final.copy().reshape(Nx,Ny)),
np.squeeze(x_final.copy().reshape(Nx, Ny)),
np.squeeze(mapping(x_final).copy().reshape(Nx, Ny)),
np.squeeze(final_backprop_gradient.reshape(Nx, Ny)),
]
Expand Down Expand Up @@ -471,7 +470,7 @@ def run_topology_optimization(
results.append(np.real(f0))
data.append(
[
np.squeeze(x_final.copy().reshape(Nx,Ny)),
np.squeeze(x_final.copy().reshape(Nx, Ny)),
np.squeeze(mapping(x_final).copy().reshape(Nx, Ny)),
np.squeeze(final_backprop_gradient.reshape(Nx, Ny)),
]
Expand Down Expand Up @@ -601,16 +600,16 @@ def analyze_FOM_convergence(
function of optimization iteration.
"""
# print("Running shape optimization WITHOUT smoothing...")
# _, results, _, _ = run_shape_optimization(
# beta=beta,
# resolution=resolution,
# maxeval=maxeval,
# use_smoothed_projection=False,
# plot_results=False,
# output_filename_prefix=f"without_smoothing_grad_{beta}",
# damping_factor=damping_factor,
# )
print("Running shape optimization WITHOUT smoothing...")
_, results, _, _ = run_shape_optimization(
beta=beta,
resolution=resolution,
maxeval=maxeval,
use_smoothed_projection=False,
plot_results=False,
output_filename_prefix=f"without_smoothing_grad_{beta}",
damping_factor=damping_factor,
)
print("Running shape optimization WITH smoothing...")
(
_,
Expand Down Expand Up @@ -639,24 +638,7 @@ def analyze_FOM_convergence(


if __name__ == "__main__":
# run_shape_optimization(resolution=25.0, beta=np.inf, maxeval=30)

# run_topology_optimization(
# resolution=25.0, beta_evolution=[8, 32, np.inf], maxeval=10
# )

analyze_FOM_convergence(resolution=30, beta=64, maxeval=200, damping_factor=0.0)
# data = np.load("temp.npz")
# results=data["results"]
# results_smoothed=data["results_smoothed"]
# plt.figure(figsize=(5.2, 2.0), constrained_layout=True)
# plt.loglog(results, "o-", label="W/o smoothing")
# plt.loglog(results_smoothed, "o-", label="W/ smoothing")
# plt.legend()
# plt.xlabel("Optimization iteration")
# plt.ylabel("FOM")
# plt.show()

# analyze_gradient_convergence(
# beta_range=np.logspace(1, 3, base=10, num=10), resolution=20
# )

analyze_gradient_convergence(
beta_range=np.logspace(1, 3, base=10, num=10), resolution=20
)

0 comments on commit e14534f

Please sign in to comment.