Skip to content

Commit

Permalink
decimation option for adjoint solver (NanoComp#1722)
Browse files Browse the repository at this point in the history
* decimation option for adjoint solver

* fixes

* more fixes

* decimation option for EigenmodeCoefficient, FourierFields, Near2FarFields

* add decimation to unit tests in test_adjoint_solver.py
  • Loading branch information
oskooi authored Aug 11, 2021
1 parent 8f85993 commit bda7464
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
11 changes: 9 additions & 2 deletions python/adjoint/objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def __init__(self,
mode,
forward=True,
kpoint_func=None,
decimation_factor=1,
**kwargs):
super().__init__(sim)
self.volume = volume
Expand All @@ -131,13 +132,15 @@ def __init__(self,
self._monitor = None
self._normal_direction = None
self._cscale = None
self.decimation_factor = decimation_factor

def register_monitors(self, frequencies):
self._frequencies = np.asarray(frequencies)
self._monitor = self.sim.add_mode_monitor(
frequencies,
mp.ModeRegion(center=self.volume.center, size=self.volume.size),
yee_grid=True,
decimation_factor=self.decimation_factor,
)
self._normal_direction = self._monitor.normal_direction
return self._monitor
Expand Down Expand Up @@ -203,10 +206,11 @@ def __call__(self):


class FourierFields(ObjectiveQuantity):
def __init__(self, sim, volume, component):
def __init__(self, sim, volume, component, decimation_factor=1):
super().__init__(sim)
self.volume = volume
self.component = component
self.decimation_factor = decimation_factor

def register_monitors(self, frequencies):
self._frequencies = np.asarray(frequencies)
Expand All @@ -215,6 +219,7 @@ def register_monitors(self, frequencies):
self._frequencies,
where=self.volume,
yee_grid=False,
decimation_factor=self.decimation_factor,
)
return self._monitor

Expand Down Expand Up @@ -301,18 +306,20 @@ def __call__(self):


class Near2FarFields(ObjectiveQuantity):
def __init__(self, sim, Near2FarRegions, far_pts):
def __init__(self, sim, Near2FarRegions, far_pts, decimation_factor=1):
super().__init__(sim)
self.Near2FarRegions = Near2FarRegions
self.far_pts = far_pts #list of far pts
self._nfar_pts = len(far_pts)
self.decimation_factor = decimation_factor

def register_monitors(self, frequencies):
self._frequencies = np.asarray(frequencies)
self._monitor = self.sim.add_near2far(
self._frequencies,
*self.Near2FarRegions,
yee_grid=True,
decimation_factor=self.decimation_factor,
)
return self._monitor

Expand Down
4 changes: 4 additions & 0 deletions python/adjoint/optimization_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def __init__(
decay_dt=50,
decay_fields=[mp.Ez],
decay_by=1e-6,
decimation_factor=1,
minimum_run_time=0,
maximum_run_time=None,
):
Expand Down Expand Up @@ -128,6 +129,7 @@ def __init__(
self.decay_by = decay_by
self.decay_fields = decay_fields
self.decay_dt = decay_dt
self.decimation_factor = decimation_factor
self.minimum_run_time = minimum_run_time
self.maximum_run_time = maximum_run_time

Expand Down Expand Up @@ -215,6 +217,7 @@ def prepare_forward_run(self):
self.frequencies,
where=dr.volume,
yee_grid=True,
decimation_factor=self.decimation_factor,
) for dr in self.design_regions
]

Expand Down Expand Up @@ -302,6 +305,7 @@ def adjoint_run(self):
self.frequencies,
where=dr.volume,
yee_grid=True,
decimation_factor=self.decimation_factor,
) for dr in self.design_regions
]

Expand Down
21 changes: 14 additions & 7 deletions python/tests/test_adjoint_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,23 @@ def forward_simulation(design_params,mon_type,frequencies=None, use_complex=Fals
sources=sources,
geometry=geometry,
force_complex_fields=use_complex,
k_point = k)
k_point=k)
if not frequencies:
frequencies = [fcen]

if mon_type.name == 'EIGENMODE':
mode = sim.add_mode_monitor(frequencies,
mp.ModeRegion(center=mp.Vector3(0.5*sxy-dpml-0.1),size=mp.Vector3(0,sxy-2*dpml,0)),
yee_grid=True)
yee_grid=True,
decimation_factor=10)

elif mon_type.name == 'DFT':
mode = sim.add_dft_fields([mp.Ez],
frequencies,
center=mp.Vector3(1.25),
size=mp.Vector3(0.25,1,0),
yee_grid=False)
yee_grid=False,
decimation_factor=10)

sim.run(until_after_sources=50)

Expand Down Expand Up @@ -131,14 +133,17 @@ def adjoint_solver(design_params, mon_type, frequencies=None, use_complex=False,
sources=sources,
geometry=geometry,
force_complex_fields=use_complex,
k_point = k)
k_point=k)

if not frequencies:
frequencies = [fcen]

if mon_type.name == 'EIGENMODE':
obj_list = [mpa.EigenmodeCoefficient(sim,
mp.Volume(center=mp.Vector3(0.5*sxy-dpml-0.1),
size=mp.Vector3(0,sxy-2*dpml,0)),1)]
size=mp.Vector3(0,sxy-2*dpml,0)),
1,
decimation_factor=5)]

def J(mode_mon):
return npa.abs(mode_mon)**2
Expand All @@ -147,7 +152,8 @@ def J(mode_mon):
obj_list = [mpa.FourierFields(sim,
mp.Volume(center=mp.Vector3(1.25),
size=mp.Vector3(0.25,1,0)),
mp.Ez)]
mp.Ez,
decimation_factor=5)]

def J(mode_mon):
return npa.abs(mode_mon[:,4,10])**2
Expand All @@ -158,7 +164,8 @@ def J(mode_mon):
objective_arguments = obj_list,
design_regions = [matgrid_region],
frequencies=frequencies,
decay_fields=[mp.Ez])
decay_fields=[mp.Ez],
decimation_factor=10)

f, dJ_du = opt([design_params])

Expand Down

0 comments on commit bda7464

Please sign in to comment.