From 1910dd30cd5825e4c3b5a3def23215cd71b9408a Mon Sep 17 00:00:00 2001 From: smartalecH Date: Sun, 14 Aug 2022 09:04:39 -0400 Subject: [PATCH 1/7] precommit fixes --- python/adjoint/objective.py | 43 ++++++++++++++++++++------ python/adjoint/optimization_problem.py | 34 ++++++++++++-------- python/adjoint/utils.py | 7 ++++- 3 files changed, 60 insertions(+), 24 deletions(-) diff --git a/python/adjoint/objective.py b/python/adjoint/objective.py index 07aeea618..266a1fb7c 100644 --- a/python/adjoint/objective.py +++ b/python/adjoint/objective.py @@ -1,6 +1,7 @@ """Handling of objective functions and objective quantities.""" import abc from collections import namedtuple +from typing import Callable, List, Union, Optional import numpy as np from meep.simulation import py_v3_to_vec @@ -8,6 +9,7 @@ import meep as mp from .filter_source import FilteredSource +from . import MEEP_COMPONENTS Grid = namedtuple("Grid", ["x", "y", "z", "w"]) @@ -161,15 +163,18 @@ class EigenmodeCoefficient(ObjectiveQuantity): def __init__( self, - sim, - volume, - mode, - forward=True, - kpoint_func=None, - kpoint_func_overlap_idx=0, - decimation_factor=0, + sim: mp.Simulation, + volume: mp.Volume, + mode: int, + forward: Optional[bool] = True, + kpoint_func: Optional[Callable] = None, + kpoint_func_overlap_idx: Optional[idx] = 0, + decimation_factor: Optional[idx] = 0, **kwargs ): + """ + + **`sim` [ `Simulation` ]** — + """ super().__init__(sim) if kpoint_func_overlap_idx not in [0, 1]: raise ValueError( @@ -268,7 +273,15 @@ def __call__(self): class FourierFields(ObjectiveQuantity): - def __init__(self, sim, volume, component, yee_grid=False, decimation_factor=0): + def __init__( + self, + sim: mp.Simulation, + volume: mp.Volume, + component: List[MEEP_COMPONENTS], + yee_grid: Optional[bool] = False, + decimation_factor: Optional[idx] = 0, + ): + """ """ super().__init__(sim) self.volume = sim._fit_volume_to_simulation(volume) self.component = component @@ -354,7 +367,14 @@ def __call__(self): class Near2FarFields(ObjectiveQuantity): - def __init__(self, sim, Near2FarRegions, far_pts, decimation_factor=0): + def __init__( + self, + sim: mp.Simulation, + Near2FarRegions: mp.Near2FarRegions, + far_pts: List[mp.Vector3], + decimation_factor: Optional[idx] = 0, + ): + """ """ super().__init__(sim) self.Near2FarRegions = Near2FarRegions self.far_pts = far_pts # list of far pts @@ -420,7 +440,10 @@ def __call__(self): class LDOS(ObjectiveQuantity): - def __init__(self, sim, decimation_factor=0, **kwargs): + def __init__( + self, sim: mp.Simulation, decimation_factor: Optional[idx] = 0, **kwargs + ): + """ """ super().__init__(sim) self.decimation_factor = decimation_factor self.srckwarg = kwargs diff --git a/python/adjoint/optimization_problem.py b/python/adjoint/optimization_problem.py index 707eb8beb..a28caa45e 100644 --- a/python/adjoint/optimization_problem.py +++ b/python/adjoint/optimization_problem.py @@ -1,4 +1,5 @@ from collections import namedtuple +from typing import Callable, List, Union, Optional import numpy as np from autograd import grad, jacobian @@ -24,20 +25,27 @@ class OptimizationProblem: def __init__( self, - simulation, - objective_functions, - objective_arguments, - design_regions, - frequencies=None, - fcen=None, - df=None, - nf=None, - decay_by=1e-11, - decimation_factor=0, - minimum_run_time=0, - maximum_run_time=None, - finite_difference_step=utils.FD_DEFAULT, + simulation: mp.Simulation, + objective_functions: List[Callable], + objective_arguments: List[ObjectiveQuantity], + design_regions: [DesignRegion], + frequencies: Optional[Union[float, List[float]]] = None, + fcen: Optional[float] = None, + df: Optional[float] = None, + nf: Optional[int] = None, + decay_by: Optional[float] = 1e-11, + decimation_factor: Optional[int] = 0, + minimum_run_time: Optional[float] = 0, + maximum_run_time: Optional[float] = None, + finite_difference_step: Optional[float] = utils.FD_DEFAULT, ): + """ + + **`simulation` [ `Simulation` ]** — The corresponding Meep + `Simulation` object that describeds the problem (e.g. sources, + geometry) + + + **`objective_functions` [ `list of ` ]** — + """ self.sim = simulation diff --git a/python/adjoint/utils.py b/python/adjoint/utils.py index 84f8f9866..4ae290397 100644 --- a/python/adjoint/utils.py +++ b/python/adjoint/utils.py @@ -1,4 +1,4 @@ -from typing import Iterable, List, Tuple +from typing import Iterable, List, Tuple, Union import numpy as onp @@ -22,6 +22,11 @@ # default finite difference step size when calculating Aᵤ FD_DEFAULT = 1e-3 +# Type hint for all components +MEEP_COMPONENTS = Union[ + mp.Ex, mp.Ey, mp.Ez, mp.Hx, mp.Hy, mp.Hz, mp.Dx, mp.Dy, mp.Dz, mp.Bx, mp.By, mp.Bz +] + class DesignRegion: def __init__( From 9713c5e8f84868080ea7f49d9d478b8143920e92 Mon Sep 17 00:00:00 2001 From: Alec Hammond Date: Wed, 14 Sep 2022 14:48:11 -0700 Subject: [PATCH 2/7] add more typehints --- python/adjoint/objective.py | 3 +-- python/adjoint/optimization_problem.py | 27 +++++++++++++++----------- python/adjoint/utils.py | 5 ----- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/python/adjoint/objective.py b/python/adjoint/objective.py index 266a1fb7c..bb6b02550 100644 --- a/python/adjoint/objective.py +++ b/python/adjoint/objective.py @@ -9,7 +9,6 @@ import meep as mp from .filter_source import FilteredSource -from . import MEEP_COMPONENTS Grid = namedtuple("Grid", ["x", "y", "z", "w"]) @@ -277,7 +276,7 @@ def __init__( self, sim: mp.Simulation, volume: mp.Volume, - component: List[MEEP_COMPONENTS], + component: List[int], yee_grid: Optional[bool] = False, decimation_factor: Optional[idx] = 0, ): diff --git a/python/adjoint/optimization_problem.py b/python/adjoint/optimization_problem.py index a28caa45e..2a45bd34b 100644 --- a/python/adjoint/optimization_problem.py +++ b/python/adjoint/optimization_problem.py @@ -1,5 +1,5 @@ from collections import namedtuple -from typing import Callable, List, Union, Optional +from typing import Callable, List, Union, Optional, Tuple import numpy as np from autograd import grad, jacobian @@ -115,7 +115,12 @@ def __init__( self.gradient = [] - def __call__(self, rho_vector=None, need_value=True, need_gradient=True, beta=None): + def __call__(self, + rho_vector=None: List[List[float]], + need_value=True: bool, + need_gradient=True: bool, + beta=None: float + ) -> Tuple[List[float],List[float]]: """Evaluate value and/or gradient of objective function.""" if rho_vector: self.update_design(rho_vector=rho_vector, beta=beta) @@ -147,7 +152,7 @@ def __call__(self, rho_vector=None, need_value=True, need_gradient=True, beta=No return self.f0, self.gradient - def get_fdf_funcs(self): + def get_fdf_funcs(self) -> Tuple[Callable, Callable]: """construct callable functions for objective function value and gradient Returns @@ -310,11 +315,11 @@ def calculate_gradient(self): def calculate_fd_gradient( self, - num_gradients=1, - db=1e-4, - design_variables_idx=0, - filter=None, - ): + num_gradients=1: int, + db=1e-4: float, + design_variables_idx=0: int, + filter=None: Callable, + ) -> List[float]: """ Estimate central difference gradients. @@ -448,7 +453,7 @@ def calculate_fd_gradient( return fd_gradient, fd_gradient_idx - def update_design(self, rho_vector, beta=None): + def update_design(self, rho_vector: List[float], beta=None: float) -> None: """Update the design permittivity function. rho_vector ....... a list of numpy arrays that maps to each design region @@ -465,11 +470,11 @@ def update_design(self, rho_vector, beta=None): self.sim.reset_meep() self.current_state = "INIT" - def get_objective_arguments(self): + def get_objective_arguments(self) -> List[float]: """Return list of evaluated objective arguments.""" return [m.get_evaluation() for m in self.objective_arguments] - def plot2D(self, init_opt=False, **kwargs): + def plot2D(self, init_opt=False, **kwargs) -> None: """Produce a graphical visualization of the geometry and/or fields, as appropriately autodetermined based on the current state of progress. diff --git a/python/adjoint/utils.py b/python/adjoint/utils.py index 4ae290397..17822ce42 100644 --- a/python/adjoint/utils.py +++ b/python/adjoint/utils.py @@ -22,11 +22,6 @@ # default finite difference step size when calculating Aᵤ FD_DEFAULT = 1e-3 -# Type hint for all components -MEEP_COMPONENTS = Union[ - mp.Ex, mp.Ey, mp.Ez, mp.Hx, mp.Hy, mp.Hz, mp.Dx, mp.Dy, mp.Dz, mp.Bx, mp.By, mp.Bz -] - class DesignRegion: def __init__( From 2822eeef15df696273fa93488d56f604b4f09c41 Mon Sep 17 00:00:00 2001 From: Alec Hammond Date: Fri, 16 Sep 2022 09:21:54 -0700 Subject: [PATCH 3/7] fix precommit errors --- python/adjoint/objective.py | 10 +++++----- python/adjoint/optimization_problem.py | 23 ++++++++++++----------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/python/adjoint/objective.py b/python/adjoint/objective.py index bb6b02550..8e4f37a93 100644 --- a/python/adjoint/objective.py +++ b/python/adjoint/objective.py @@ -167,8 +167,8 @@ def __init__( mode: int, forward: Optional[bool] = True, kpoint_func: Optional[Callable] = None, - kpoint_func_overlap_idx: Optional[idx] = 0, - decimation_factor: Optional[idx] = 0, + kpoint_func_overlap_idx: Optional[int] = 0, + decimation_factor: Optional[int] = 0, **kwargs ): """ @@ -278,7 +278,7 @@ def __init__( volume: mp.Volume, component: List[int], yee_grid: Optional[bool] = False, - decimation_factor: Optional[idx] = 0, + decimation_factor: Optional[int] = 0, ): """ """ super().__init__(sim) @@ -371,7 +371,7 @@ def __init__( sim: mp.Simulation, Near2FarRegions: mp.Near2FarRegions, far_pts: List[mp.Vector3], - decimation_factor: Optional[idx] = 0, + decimation_factor: Optional[int] = 0, ): """ """ super().__init__(sim) @@ -440,7 +440,7 @@ def __call__(self): class LDOS(ObjectiveQuantity): def __init__( - self, sim: mp.Simulation, decimation_factor: Optional[idx] = 0, **kwargs + self, sim: mp.Simulation, decimation_factor: Optional[int] = 0, **kwargs ): """ """ super().__init__(sim) diff --git a/python/adjoint/optimization_problem.py b/python/adjoint/optimization_problem.py index 0cf3f4d8a..07ce51530 100644 --- a/python/adjoint/optimization_problem.py +++ b/python/adjoint/optimization_problem.py @@ -115,12 +115,13 @@ def __init__( self.gradient = [] - def __call__(self, - rho_vector=None: List[List[float]], - need_value=True: bool, - need_gradient=True: bool, - beta=None: float - ) -> Tuple[List[float],List[float]]: + def __call__( + self, + rho_vector: List[List[float]] = None, + need_value: bool = True, + need_gradient: bool = True, + beta: float = None, + ) -> Tuple[List[float], List[float]]: """Evaluate value and/or gradient of objective function.""" if rho_vector: self.update_design(rho_vector=rho_vector, beta=beta) @@ -318,10 +319,10 @@ def calculate_gradient(self): def calculate_fd_gradient( self, - num_gradients=1: int, - db=1e-4: float, - design_variables_idx=0: int, - filter=None: Callable, + num_gradients: int = 1, + db: float = 1e-4, + design_variables_idx: int = 0, + filter: Callable = None, ) -> List[float]: """ Estimate central difference gradients. @@ -460,7 +461,7 @@ def calculate_fd_gradient( return fd_gradient, fd_gradient_idx - def update_design(self, rho_vector: List[float], beta=None: float) -> None: + def update_design(self, rho_vector: List[float], beta: float = None) -> None: """Update the design permittivity function. rho_vector ....... a list of numpy arrays that maps to each design region From ce41a73466d63356408590730672432e4d41ac3c Mon Sep 17 00:00:00 2001 From: Alec Hammond Date: Wed, 21 Sep 2022 12:08:19 -0700 Subject: [PATCH 4/7] fix last few typos --- python/adjoint/objective.py | 4 ++-- python/adjoint/optimization_problem.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/python/adjoint/objective.py b/python/adjoint/objective.py index 8e4f37a93..074fda530 100644 --- a/python/adjoint/objective.py +++ b/python/adjoint/objective.py @@ -369,13 +369,13 @@ class Near2FarFields(ObjectiveQuantity): def __init__( self, sim: mp.Simulation, - Near2FarRegions: mp.Near2FarRegions, + Near2FarRegions: mp.Near2FarRegion, far_pts: List[mp.Vector3], decimation_factor: Optional[int] = 0, ): """ """ super().__init__(sim) - self.Near2FarRegions = Near2FarRegions + self.Near2FarRegions = mp.Near2FarRegion self.far_pts = far_pts # list of far pts self._nfar_pts = len(far_pts) self.decimation_factor = decimation_factor diff --git a/python/adjoint/optimization_problem.py b/python/adjoint/optimization_problem.py index 07ce51530..fb3c4c834 100644 --- a/python/adjoint/optimization_problem.py +++ b/python/adjoint/optimization_problem.py @@ -6,7 +6,7 @@ import meep as mp -from . import LDOS, DesignRegion, utils +from . import LDOS, DesignRegion, utils, ObjectiveQuantity class OptimizationProblem: @@ -38,6 +38,7 @@ def __init__( minimum_run_time: Optional[float] = 0, maximum_run_time: Optional[float] = None, finite_difference_step: Optional[float] = utils.FD_DEFAULT, + step_funcs: list = None, ): """ + **`simulation` [ `Simulation` ]** — The corresponding Meep From 66f6ba4f025f8fe547751142c17d6897b1821df7 Mon Sep 17 00:00:00 2001 From: Alec Hammond Date: Wed, 21 Sep 2022 13:04:07 -0700 Subject: [PATCH 5/7] fix one last typo --- python/adjoint/objective.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/adjoint/objective.py b/python/adjoint/objective.py index 074fda530..98e4e85a5 100644 --- a/python/adjoint/objective.py +++ b/python/adjoint/objective.py @@ -369,13 +369,13 @@ class Near2FarFields(ObjectiveQuantity): def __init__( self, sim: mp.Simulation, - Near2FarRegions: mp.Near2FarRegion, + Near2FarRegions: List[mp.Near2FarRegion], far_pts: List[mp.Vector3], decimation_factor: Optional[int] = 0, ): """ """ super().__init__(sim) - self.Near2FarRegions = mp.Near2FarRegion + self.Near2FarRegions = Near2FarRegions self.far_pts = far_pts # list of far pts self._nfar_pts = len(far_pts) self.decimation_factor = decimation_factor From 4a480c9207268fb2cb71c3caf73ef994346e04d7 Mon Sep 17 00:00:00 2001 From: Alec Hammond Date: Thu, 22 Sep 2022 11:22:29 -0700 Subject: [PATCH 6/7] remove unused imports --- python/adjoint/objective.py | 2 +- python/adjoint/utils.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python/adjoint/objective.py b/python/adjoint/objective.py index 98e4e85a5..6a9a4fb65 100644 --- a/python/adjoint/objective.py +++ b/python/adjoint/objective.py @@ -1,7 +1,7 @@ """Handling of objective functions and objective quantities.""" import abc from collections import namedtuple -from typing import Callable, List, Union, Optional +from typing import Callable, List, Optional import numpy as np from meep.simulation import py_v3_to_vec diff --git a/python/adjoint/utils.py b/python/adjoint/utils.py index 17822ce42..84f8f9866 100644 --- a/python/adjoint/utils.py +++ b/python/adjoint/utils.py @@ -1,4 +1,4 @@ -from typing import Iterable, List, Tuple, Union +from typing import Iterable, List, Tuple import numpy as onp From bd593121d9773c1b8a8ef687b417bfd8920f9df1 Mon Sep 17 00:00:00 2001 From: Alec Hammond Date: Thu, 22 Sep 2022 11:24:50 -0700 Subject: [PATCH 7/7] fix other typos --- python/adjoint/optimization_problem.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/adjoint/optimization_problem.py b/python/adjoint/optimization_problem.py index fb3c4c834..42f20b0aa 100644 --- a/python/adjoint/optimization_problem.py +++ b/python/adjoint/optimization_problem.py @@ -28,7 +28,7 @@ def __init__( simulation: mp.Simulation, objective_functions: List[Callable], objective_arguments: List[ObjectiveQuantity], - design_regions: [DesignRegion], + design_regions: List[DesignRegion], frequencies: Optional[Union[float, List[float]]] = None, fcen: Optional[float] = None, df: Optional[float] = None, @@ -42,7 +42,7 @@ def __init__( ): """ + **`simulation` [ `Simulation` ]** — The corresponding Meep - `Simulation` object that describeds the problem (e.g. sources, + `Simulation` object that describes the problem (e.g. sources, geometry) + **`objective_functions` [ `list of ` ]** —