Skip to content

Instantly share code, notes, and snippets.

@oskooi
Last active January 17, 2023 05:19
Show Gist options
  • Save oskooi/7763b54ed9a0a3fc328966e70da9836a to your computer and use it in GitHub Desktop.
Save oskooi/7763b54ed9a0a3fc328966e70da9836a to your computer and use it in GitHub Desktop.
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."""
cell_size = mp.Vector3(s+dpml_r,0,s+2*dpml_z)
pml_layers = [
mp.PML(dpml_r, direction=mp.R),
mp.PML(dpml_z, direction=mp.Z),
]
sources = [
mp.Source(
src=mp.GaussianSource(fcen,fwidth=0.1*abs(fcen),cutoff=10.0),
center=mp.Vector3(),
component=mp.Er,
),
]
sim = mp.Simulation(
resolution=res,
cell_size=cell_size,
dimensions=mp.CYLINDRICAL,
m=m,
sources=sources,
boundary_layers=pml_layers,
)
# run for long enough for fields to enter PMLs
# such that presence of PMLs has an effect on
# the fields in the non-PML region
sim.run(until=130.)
er = sim.get_array(
component=mp.Er,
center=mp.Vector3(0.5*s,0,0),
size=mp.Vector3(s,0,s),
cmplx=True,
)
return er
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'-res',
type=float,
default=50.,
help='resolution (default: 50.0)',
)
parser.add_argument(
'-dpml_r',
type=float,
default=1.,
help='PML thickness in r direction (default: 1.0)',
)
parser.add_argument(
'-dpml_z',
type=float,
default=1.,
help='PML thickness in z direction (default: 1.0)',
)
parser.add_argument(
'-s',
type=float,
default=5.,
help='thickess of non-PML region (default: 5.0)',
)
args = parser.parse_args()
er1 = dipole_emission(args.res, args.dpml_r, args.dpml_z, args.s, -1, +1.)
er2 = dipole_emission(args.res, args.dpml_r, args.dpml_z, args.s, -1, -1.)
er3 = dipole_emission(args.res, args.dpml_r, args.dpml_z, args.s, +1, +1.)
er4 = dipole_emission(args.res, args.dpml_r, args.dpml_z, args.s, +1, -1.)
c = [ ['-1', '+1'], ['-1', '-1'], ['+1', '+1'], ['+1', '-1'] ]
print(f"norm1:, {c[0]} {norm(er1):.2f}, {c[1]} {norm(er2):.2f}, "
f"{norm(er1-conj(er2)):.2f}, {norm(er1-er2):.2f}")
print(f"norm2:, {c[0]} {norm(er1):.2f}, {c[2]} {norm(er3):.2f}, "
f"{norm(er1-conj(er3)):.2f}, {norm(er1-er3):.2f}")
print(f"norm3:, {c[0]} {norm(er1):.2f}, {c[3]} {norm(er4):.2f}, "
f"{norm(er1-conj(er4)):.2f}, {norm(er1-er4):.2f}")
print(f"norm4:, {c[1]} {norm(er2):.2f}, {c[2]} {norm(er3):.2f}, "
f"{norm(er2-conj(er3)):.2f}, {norm(er2-er3):.2f}")
print(f"norm5:, {c[1]} {norm(er2):.2f}, {c[3]} {norm(er4):.2f}, "
f"{norm(er2-conj(er4)):.2f}, {norm(er2-er4):.2f}")
print(f"norm6:, {c[2]} {norm(er3):.2f}, {c[3]} {norm(er4):.2f}, "
f"{norm(er3-conj(er4)):.2f}, {norm(er3-er4):.2f}")
@oskooi
Copy link
Author

oskooi commented Jan 16, 2023

output from running the script with default parameters:

norm1:, ['-1', '+1'] 0.30, ['-1', '-1'] 0.30, 0.00, 0.60
norm2:, ['-1', '+1'] 0.30, ['+1', '+1'] 0.30, 0.60, 0.00
norm3:, ['-1', '+1'] 0.30, ['+1', '-1'] 0.30, 0.00, 0.60
norm4:, ['-1', '-1'] 0.30, ['+1', '+1'] 0.30, 0.00, 0.60
norm5:, ['-1', '-1'] 0.30, ['+1', '-1'] 0.30, 0.60, 0.00
norm6:, ['+1', '+1'] 0.30, ['+1', '-1'] 0.30, 0.00, 0.60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment