Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjoint typehints #2202

Merged
merged 9 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions python/adjoint/objective.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"""Handling of objective functions and objective quantities."""
import abc
from collections import namedtuple
from typing import Callable, List, Union, Optional
smartalecH marked this conversation as resolved.
Show resolved Hide resolved

import numpy as np
from meep.simulation import py_v3_to_vec

import meep as mp

from .filter_source import FilteredSource
from . import MEEP_COMPONENTS

Grid = namedtuple("Grid", ["x", "y", "z", "w"])

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
34 changes: 21 additions & 13 deletions python/adjoint/optimization_problem.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import namedtuple
from typing import Callable, List, Union, Optional

import numpy as np
from autograd import grad, jacobian
Expand All @@ -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],
smartalecH marked this conversation as resolved.
Show resolved Hide resolved
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,
smartalecH marked this conversation as resolved.
Show resolved Hide resolved
geometry)

+ **`objective_functions` [ `list of ` ]** —
"""

self.sim = simulation

Expand Down
7 changes: 6 additions & 1 deletion python/adjoint/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Iterable, List, Tuple
from typing import Iterable, List, Tuple, Union
smartalecH marked this conversation as resolved.
Show resolved Hide resolved

import numpy as onp

Expand All @@ -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
smartalecH marked this conversation as resolved.
Show resolved Hide resolved
]


class DesignRegion:
def __init__(
Expand Down