Skip to content

Instantly share code, notes, and snippets.

@oskooi
oskooi / multilayer_validate.py
Created July 19, 2024 07:12
Plots the fields and computes the transmittance of the stack configuration obtained from optimization
from typing import Tuple
import matplotlib.pyplot as plt
import meep as mp
import numpy as np
RESOLUTION_UM = 800
WAVELENGTH_UM = 1.0
AIR_UM = 1.0
@oskooi
oskooi / binary_grating_adjoint_gradient.py
Last active June 17, 2024 04:43
Validates the adjoint gradient of the diffraction efficiency of a 1D grating.
"""Validates the adjoint gradient of the diffraction efficiency of a 1D grating.
The 2D test involves computing the gradient of the diffraction efficiency of
the first transmitted order in air with respect to the height of a 1D binary
grating. A linearly polarized planewave is from incident from the substrate on
which the grating is placed. The adjoint gradient is validated using the brute-force
finite difference via the directional derivative.
"""
rom enum import Enum
@oskooi
oskooi / binary_grating_1d_levelset.py
Created February 21, 2024 18:18
Validates the adjoint gradient of the diffraction efficiency of a 1D grating using subpixel smoothing
"""Validates the adjoint gradient of the diffraction efficiency of a 1D grating.
The 2D test involves using subpixel smoothing to compute the gradient of the
diffraction efficiency of the first transmitted order in air of a 1D grating
on a substrate given a normally incident planewave from the substrate. The
adjoint gradient is validated using the brute-force finite difference via the
directional derivative. The grating structure is represented as a level set.
"""
from enum import Enum
import argparse
from typing import Tuple
import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import meep as mp
import numpy as np
@oskooi
oskooi / freespace_reflector_cyl_n2f.py
Last active May 23, 2023 16:36
A test to verify the correctness of the near-to-far field transformation in cylindrical coordinates
import argparse
from typing import Tuple
import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
import meep as mp
import numpy as np
parser = argparse.ArgumentParser()
@oskooi
oskooi / led_disc_cyl_n2f.py
Last active May 20, 2023 16:40
Validating the near-to-far field transformation function (`greencyl`) in cylindrical coordinates
import argparse
from typing import Tuple
import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
import meep as mp
import numpy as np
parser = argparse.ArgumentParser()
"""
Checks that the constraint function with constraint L applied to a circle
with diameter d has a return value based on the convention that a violation
(L > d) is >=0 and a non-violation is ~0.
"""
from typing import Tuple
import matplotlib
matplotlib.use('agg')
@oskooi
oskooi / test_constraint_function_periodic.py
Last active May 1, 2023 17:42
Checks that the constraint function of a periodic design region is the same whether or not a test feature crosses its edge and wraps around back into the design region.
"""
Checks that the constraint function of a periodic design region
produces the same value whether or not a test feature crosses its
edge and wraps around back into the design region.
"""
from typing import Tuple
import matplotlib
matplotlib.use('agg')
@oskooi
oskooi / led_cyl_radpattern.py
Last active April 26, 2023 04:26
Radiation pattern of a point dipole within an finite-size LED
import argparse
import math
from typing import Tuple
import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
import meep as mp
import numpy as np
@oskooi
oskooi / freespace_cyl_flux.py
Created March 27, 2023 00:27
radiated flux from a point dipole at r=0 in cylindrical coordinates
import argparse
import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
import meep as mp
import numpy as np
def radiated_flux(res: float, dpml_r: float, dpml_z: float, sr: float,
sz: float, fcen: float, m: int, rpos: float) -> float:
"""Computes the radiated flux for an Er point source in vacuum."""