Skip to content

Instantly share code, notes, and snippets.

@oskooi
oskooi / bug_cyl_fields.py
Last active January 20, 2023 06:23
Determines whether the fields in a cylindrical-coordinate simulation with m=±1 are purely real
import argparse
import numpy as np
import meep as mp
def dipole_emission(res: float, dpml_r: float, dpml_z: float, s: float,
m: int, fcen: float) -> float:
"""Computes the complex fields from an Er point source at r=z=0 in vacuum."""
cell_size = mp.Vector3(s+dpml_r,0,s+2*dpml_z)
@oskooi
oskooi / freespace_cyl_dft_fields.py
Last active January 19, 2023 05:02
Computes the DFT fields from an Er point source at r=z=0 in vacuum.
import argparse
from numpy.linalg import norm
from numpy import conj
import meep as mp
def dipole_emission(res: float, dpml_r: float, dpml_z: float, s: float,
m: int, fcen: float) -> float:
"""Computes the DFT fields from an Er point source at r=z=0 in vacuum."""
@oskooi
oskooi / freespace_cyl_flux.py
Last active January 17, 2023 02:33
Computes the radiated flux for an Er point source at r=z=0 in vacuum.
import meep as mp
import argparse
def radiated_flux(res: float, dpml_r: float, dpml_z: float, s: float,
m: int, fcen: float) -> float:
"""Computes the radiated flux for an Er point source at r=z=0 in vacuum."""
cell_size = mp.Vector3(s+dpml_r,0,s+2*dpml_z)
@oskooi
oskooi / freespace_cyl.py
Last active January 17, 2023 05:19
Computes the (complex) fields from an Er point source at r=0 in vacuum.
import argparse
from numpy.linalg import norm
from numpy import conj
import meep as mp
def dipole_emission(res: float, dpml_r: float, dpml_z: float, s: float,
m: int, fcen: float) -> float:
"""Computes the complex fields from an Er point source at r=z=0 in vacuum."""
@oskooi
oskooi / freespace_mirror_sidewall_cyl.py
Created December 29, 2022 21:48
radiated flux from an Er point dipole source at r=z=0 in a vacuum cavity with metallic sidewalls
import meep as mp
import argparse
def radiated_flux(res: float, dpml_r: float, s: float) -> float:
"""Computes the radiated flux for an Er point source at r=0 in a cavity
in vacuum with metallic sidewalls."""
cell_size = mp.Vector3(s+dpml_r,0,s)
@oskooi
oskooi / mode_converter_meep_opt.py
Created October 24, 2022 22:40
Topology optimization of the waveguide mode converter with minimum linewidth constraint using Meep's adjoint solver.
"""Topology optimization of the waveguide mode converter using
Meep's adjoint solver from A M. Hammond et al., Optics Express,
Vol. 30, pp. 4467-4491 (2022). doi.org/10.1364/OE.442074
The worst-case optimization is based on minimizing the maximum
of R + (1-T) where R is $|S_{11}|^2$ for mode 1 and T is $|S_{21}|^2$
for mode 2 across six different wavelengths. The minimum linewidth
criteria is 90 nm. The optimization uses the method of moving
asymptotes (MMA) algorithm from NLopt.
"""Computes the worst-case reflectance and transmittance of a mode converter.
This script uses Meep to compute the reflectance and transmittance over a
number of sampled wavelengths of a mode converter imported as an image.
Each wavelength is a separate simulation to ensure the eigenmode source
is correct. The results are printed in CSV format including the
worst-case values.
"""
import numpy as np
import meep as mp
import argparse
import numpy as np
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
parser = argparse.ArgumentParser()
parser.add_argument(
@oskooi
oskooi / antenna_free_space_radiation.py
Last active April 28, 2022 15:50
Meep simulation for computing the radiation pattern of an antenna above a PEC ground plane and comparing the result to analytic theory
# Computes the radiation pattern of an antenna in free space.
import meep as mp
import math
import numpy as np
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
@oskooi
oskooi / antenna_ground_plane.py
Created April 25, 2022 04:11
Radiation pattern of an antenna above a perfect electric conductor ground plane computed using one or two antennas (method of images)
import meep as mp
import numpy as np
import math
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
resolution = 50 # pixels/μm