From e3207741c825723a0af8403c487ed59915393767 Mon Sep 17 00:00:00 2001 From: Ardavan Oskooi Date: Fri, 6 Aug 2021 22:00:11 +0000 Subject: [PATCH 1/5] decimation option for adjoint solver --- python/adjoint/objective.py | 3 +++ python/adjoint/optimization_problem.py | 4 ++++ python/adjoint/utils.py | 2 ++ python/adjoint/wrapper.py | 2 ++ 4 files changed, 11 insertions(+) diff --git a/python/adjoint/objective.py b/python/adjoint/objective.py index 8da8db33b..f0483abe1 100644 --- a/python/adjoint/objective.py +++ b/python/adjoint/objective.py @@ -138,6 +138,7 @@ def register_monitors(self, frequencies): 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 @@ -215,6 +216,7 @@ def register_monitors(self, frequencies): self._frequencies, where=self.volume, yee_grid=False, + decimation_factor=self.decimation_factor ) return self._monitor @@ -313,6 +315,7 @@ def register_monitors(self, frequencies): self._frequencies, *self.Near2FarRegions, yee_grid=True, + decimation_factor=self.decimation_factor ) return self._monitor diff --git a/python/adjoint/optimization_problem.py b/python/adjoint/optimization_problem.py index 31e2522ac..bde80ae8d 100644 --- a/python/adjoint/optimization_problem.py +++ b/python/adjoint/optimization_problem.py @@ -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, ): @@ -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 @@ -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 ] @@ -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 ] diff --git a/python/adjoint/utils.py b/python/adjoint/utils.py index 110a4c4c2..698c546b3 100644 --- a/python/adjoint/utils.py +++ b/python/adjoint/utils.py @@ -61,6 +61,7 @@ def install_design_region_monitors( simulation: mp.Simulation, design_regions: List[DesignRegion], frequencies: List[float], + decimation_factor: int ) -> List[mp.DftFields]: """Installs DFT field monitors at the design regions of the simulation.""" design_region_monitors = [ @@ -69,6 +70,7 @@ def install_design_region_monitors( frequencies, where=design_region.volume, yee_grid=True, + decimation_factor=decimation_factor ) for design_region in design_regions ] return design_region_monitors diff --git a/python/adjoint/wrapper.py b/python/adjoint/wrapper.py index d36d1b89d..4d51f4aea 100644 --- a/python/adjoint/wrapper.py +++ b/python/adjoint/wrapper.py @@ -161,6 +161,7 @@ def _run_fwd_simulation(self, design_variables): self.simulation, self.design_regions, self.frequencies, + self.decimation_factor ) self.simulation.init_sim() sim_run_args = { @@ -193,6 +194,7 @@ def _run_adjoint_simulation(self, monitor_values_grad): self.simulation, self.design_regions, self.frequencies, + self.decimation_factor ) self.simulation.init_sim() sim_run_args = { From 41d55a5128d7eac138dd2140c1b869070af883b6 Mon Sep 17 00:00:00 2001 From: Ardavan Oskooi Date: Fri, 6 Aug 2021 22:47:10 +0000 Subject: [PATCH 2/5] fixes --- python/adjoint/objective.py | 9 +++------ python/adjoint/wrapper.py | 6 ++---- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/python/adjoint/objective.py b/python/adjoint/objective.py index f0483abe1..c78f76e67 100644 --- a/python/adjoint/objective.py +++ b/python/adjoint/objective.py @@ -137,8 +137,7 @@ def register_monitors(self, 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 + yee_grid=True ) self._normal_direction = self._monitor.normal_direction return self._monitor @@ -215,8 +214,7 @@ def register_monitors(self, frequencies): [self.component], self._frequencies, where=self.volume, - yee_grid=False, - decimation_factor=self.decimation_factor + yee_grid=False ) return self._monitor @@ -314,8 +312,7 @@ def register_monitors(self, frequencies): self._monitor = self.sim.add_near2far( self._frequencies, *self.Near2FarRegions, - yee_grid=True, - decimation_factor=self.decimation_factor + yee_grid=True ) return self._monitor diff --git a/python/adjoint/wrapper.py b/python/adjoint/wrapper.py index 4d51f4aea..c84961fc2 100644 --- a/python/adjoint/wrapper.py +++ b/python/adjoint/wrapper.py @@ -160,8 +160,7 @@ def _run_fwd_simulation(self, design_variables): design_region_monitors = utils.install_design_region_monitors( self.simulation, self.design_regions, - self.frequencies, - self.decimation_factor + self.frequencies ) self.simulation.init_sim() sim_run_args = { @@ -193,8 +192,7 @@ def _run_adjoint_simulation(self, monitor_values_grad): design_region_monitors = utils.install_design_region_monitors( self.simulation, self.design_regions, - self.frequencies, - self.decimation_factor + self.frequencies ) self.simulation.init_sim() sim_run_args = { From f4a2365d68c1c2c964bdd08b6fe0726cdf0b4ada Mon Sep 17 00:00:00 2001 From: Ardavan Oskooi Date: Fri, 6 Aug 2021 23:16:42 +0000 Subject: [PATCH 3/5] more fixes --- python/adjoint/objective.py | 6 +++--- python/adjoint/optimization_problem.py | 4 ++-- python/adjoint/utils.py | 2 -- python/adjoint/wrapper.py | 4 ++-- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/python/adjoint/objective.py b/python/adjoint/objective.py index c78f76e67..8da8db33b 100644 --- a/python/adjoint/objective.py +++ b/python/adjoint/objective.py @@ -137,7 +137,7 @@ def register_monitors(self, frequencies): self._monitor = self.sim.add_mode_monitor( frequencies, mp.ModeRegion(center=self.volume.center, size=self.volume.size), - yee_grid=True + yee_grid=True, ) self._normal_direction = self._monitor.normal_direction return self._monitor @@ -214,7 +214,7 @@ def register_monitors(self, frequencies): [self.component], self._frequencies, where=self.volume, - yee_grid=False + yee_grid=False, ) return self._monitor @@ -312,7 +312,7 @@ def register_monitors(self, frequencies): self._monitor = self.sim.add_near2far( self._frequencies, *self.Near2FarRegions, - yee_grid=True + yee_grid=True, ) return self._monitor diff --git a/python/adjoint/optimization_problem.py b/python/adjoint/optimization_problem.py index bde80ae8d..244103116 100644 --- a/python/adjoint/optimization_problem.py +++ b/python/adjoint/optimization_problem.py @@ -217,7 +217,7 @@ def prepare_forward_run(self): self.frequencies, where=dr.volume, yee_grid=True, - decimation_factor=self.decimation_factor + decimation_factor=self.decimation_factor, ) for dr in self.design_regions ] @@ -305,7 +305,7 @@ def adjoint_run(self): self.frequencies, where=dr.volume, yee_grid=True, - decimation_factor=self.decimation_factor + decimation_factor=self.decimation_factor, ) for dr in self.design_regions ] diff --git a/python/adjoint/utils.py b/python/adjoint/utils.py index 698c546b3..110a4c4c2 100644 --- a/python/adjoint/utils.py +++ b/python/adjoint/utils.py @@ -61,7 +61,6 @@ def install_design_region_monitors( simulation: mp.Simulation, design_regions: List[DesignRegion], frequencies: List[float], - decimation_factor: int ) -> List[mp.DftFields]: """Installs DFT field monitors at the design regions of the simulation.""" design_region_monitors = [ @@ -70,7 +69,6 @@ def install_design_region_monitors( frequencies, where=design_region.volume, yee_grid=True, - decimation_factor=decimation_factor ) for design_region in design_regions ] return design_region_monitors diff --git a/python/adjoint/wrapper.py b/python/adjoint/wrapper.py index c84961fc2..d36d1b89d 100644 --- a/python/adjoint/wrapper.py +++ b/python/adjoint/wrapper.py @@ -160,7 +160,7 @@ def _run_fwd_simulation(self, design_variables): design_region_monitors = utils.install_design_region_monitors( self.simulation, self.design_regions, - self.frequencies + self.frequencies, ) self.simulation.init_sim() sim_run_args = { @@ -192,7 +192,7 @@ def _run_adjoint_simulation(self, monitor_values_grad): design_region_monitors = utils.install_design_region_monitors( self.simulation, self.design_regions, - self.frequencies + self.frequencies, ) self.simulation.init_sim() sim_run_args = { From d443dc4286392ab3d621c4803099d1de545a5445 Mon Sep 17 00:00:00 2001 From: Ardavan Oskooi Date: Sat, 7 Aug 2021 00:08:32 +0000 Subject: [PATCH 4/5] decimation option for EigenmodeCoefficient, FourierFields, Near2FarFields --- python/adjoint/objective.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/python/adjoint/objective.py b/python/adjoint/objective.py index 8da8db33b..02f0d24ca 100644 --- a/python/adjoint/objective.py +++ b/python/adjoint/objective.py @@ -121,6 +121,7 @@ def __init__(self, mode, forward=True, kpoint_func=None, + decimation_factor=1, **kwargs): super().__init__(sim) self.volume = volume @@ -131,6 +132,7 @@ 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) @@ -138,6 +140,7 @@ def register_monitors(self, frequencies): 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 @@ -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) @@ -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 @@ -301,11 +306,12 @@ 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) @@ -313,6 +319,7 @@ def register_monitors(self, frequencies): self._frequencies, *self.Near2FarRegions, yee_grid=True, + decimation_factor=self.decimation_factor, ) return self._monitor From a47fc2142066ad8c0564ffd0aac03c9568213e4d Mon Sep 17 00:00:00 2001 From: Ardavan Oskooi Date: Sat, 7 Aug 2021 01:01:31 +0000 Subject: [PATCH 5/5] add decimation to unit tests in test_adjoint_solver.py --- python/tests/test_adjoint_solver.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/python/tests/test_adjoint_solver.py b/python/tests/test_adjoint_solver.py index bd72ccd2f..12c6a50bf 100644 --- a/python/tests/test_adjoint_solver.py +++ b/python/tests/test_adjoint_solver.py @@ -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) @@ -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 @@ -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 @@ -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])