Skip to content

Commit

Permalink
Merge pull request #67 from oskooi/metagrating_meep_adjoint_fix
Browse files Browse the repository at this point in the history
Fix eigenmode coefficient objective function in Meep metagrating optimization
  • Loading branch information
oskooi authored Jun 22, 2024
2 parents 18d8bc6 + 683c924 commit a065188
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
23 changes: 17 additions & 6 deletions Metagrating3D/metagrating_meep_opt_1d.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This python script is for maximizing the diffraction efficiency of the m=+1 order
# for a 3D metagrating with a 1D design.
# for a 1D metagrating.

import meep as mp
import meep.adjoint as mpa
Expand Down Expand Up @@ -30,7 +30,7 @@
# periodic boundary conditions
k_point = mp.Vector3()

# plane of incidence is XZ
# plane of incidence is XY
P_pol = True
src_cmpt = mp.Ex if P_pol else mp.Ez
src_pt = mp.Vector3(0,-0.5*sy+dpml+0.5*dsub)
Expand Down Expand Up @@ -93,9 +93,20 @@ def mapping(x):
k_point=k_point,
eps_averaging=False)

emc = mpa.EigenmodeCoefficient(sim,mp.Volume(center=mp.Vector3(0,0.5*sy-dpml),size=mp.Vector3(px,0)),
mode=1,eig_parity = mp.EVEN_Z if P_pol else mp.ODD_Z,
kpoint_func=lambda *not_used: kdiff)
emc = mpa.EigenmodeCoefficient(
sim,
mp.Volume(
center=mp.Vector3(0, 0.5*sy - dpml),
size=mp.Vector3(px, 0)
),
mode=1,
eig_parity=mp.EVEN_Z if P_pol else mp.ODD_Z,
kpoint_func=lambda *not_used: kdiff,
eig_vol=mp.Volume(
center=mp.Vector3(0, 0.5*sy - dpml),
size=mp.Vector3(1 / resolution, 0, 0)
)
)
ob_list = [emc]

opt = mpa.OptimizationProblem(simulation=sim,objective_functions=J,objective_arguments=ob_list,
Expand Down Expand Up @@ -140,4 +151,4 @@ def f(v, gradient):

x = np.sign(mapping(x)-0.5)/2+0.5
x = x.astype(int)
np.savetxt('optimized_pattern_1d.dat', x)
np.savetxt('optimized_pattern_1d.dat', x)
21 changes: 16 additions & 5 deletions Metagrating3D/metagrating_meep_opt_2d.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This python script is for maximizing the diffraction efficiency of the m=+1 order
# for a 3D metagrating with a 2D design.
# for a 2D metagrating.

import meep as mp
import meep.adjoint as mpa
Expand Down Expand Up @@ -95,9 +95,20 @@ def mapping(x):
k_point=k_point,
eps_averaging=False)

emc = mpa.EigenmodeCoefficient(sim,mp.Volume(center=mp.Vector3(0,0,0.5*sz-dpml),size=mp.Vector3(px,py,0)),
mode=1,eig_parity = mp.EVEN_Y if P_pol else mp.ODD_Y,
kpoint_func=lambda *not_used: kdiff)
emc = mpa.EigenmodeCoefficient(
sim,
mp.Volume(
center=mp.Vector3(0, 0, 0.5*sz - dpml),
size=mp.Vector3(px, py, 0)
),
mode=1,
eig_parity = mp.EVEN_Y if P_pol else mp.ODD_Y,
kpoint_func=lambda *not_used: kdiff,
eig_vol=mp.Volume(
center=mp.Vector3(0, 0, 0.5*sz - dpml),
size=mp.VEctor3(1 / resolution, 1 / resolution, 0)
)
)
ob_list = [emc]

opt = mpa.OptimizationProblem(simulation=sim,objective_functions=J,objective_arguments=ob_list,
Expand Down Expand Up @@ -150,4 +161,4 @@ def f(v, gradient):
x = np.sign(mapping(x)-0.5)/2+0.5
x = x.astype(int)
x = x.reshape(Nx,Ny)
np.savetxt('optimized_pattern_2d.dat', x)
np.savetxt('optimized_pattern_2d.dat', x)

0 comments on commit a065188

Please sign in to comment.