Skip to content

Instantly share code, notes, and snippets.

@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."""
@oskooi
oskooi / freespace_3d_flux.py
Created March 27, 2023 00:23
radiated flux of a point dipole in 3d Cartesian coordinates
import argparse
import meep as mp
import numpy as np
parser = argparse.ArgumentParser()
parser.add_argument(
'-res',
type=float,
help='resolution (pixels/μm)',
default=100.0,
@oskooi
oskooi / cyl_point_source_plot_r0.py
Last active February 17, 2023 03:24
Plots the fields at r=0 for a point source in vacuum in cylindrical coordinates.
"""
Plots the fields at r=0 for a point source in vacuum in cylindrical coordinates.
"""
import meep as mp
import numpy as np
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt