Skip to content

Instantly share code, notes, and snippets.

@oskooi
Last active January 17, 2023 02:33
Show Gist options
  • Save oskooi/d47db9093cb475442d07ee31d1eca58d to your computer and use it in GitHub Desktop.
Save oskooi/d47db9093cb475442d07ee31d1eca58d to your computer and use it in GitHub Desktop.
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)
pml_layers = [
mp.PML(dpml_r, direction=mp.R),
mp.PML(dpml_z, direction=mp.Z),
]
src_pt = mp.Vector3()
sources = [
mp.Source(
src=mp.GaussianSource(fcen,fwidth=0.1*abs(fcen),cutoff=10.0),
center=src_pt,
component=mp.Er,
),
]
sim = mp.Simulation(
resolution=res,
cell_size=cell_size,
dimensions=mp.CYLINDRICAL,
m=m,
sources=sources,
boundary_layers=pml_layers,
)
flux_plus_z = sim.add_flux(
fcen,
0,
1,
mp.FluxRegion(
center=mp.Vector3(0.5*s,0,0.5*s),
size=mp.Vector3(s,0,0),
)
)
flux_plus_r = sim.add_flux(
fcen,
0,
1,
mp.FluxRegion(
center=mp.Vector3(s,0,0),
size=mp.Vector3(0,0,s),
)
)
flux_minus_z = sim.add_flux(
fcen,
0,
1,
mp.FluxRegion(
center=mp.Vector3(0.5*s,0,-0.5*s),
size=mp.Vector3(s,0,0),
weight=-1.0,
)
)
sim.run(
until_after_sources=mp.stop_when_fields_decayed(
50,
mp.Er,
src_pt,
1e-7,
)
)
flux_plusz = mp.get_fluxes(flux_plus_z)[0]
flux_plusr = mp.get_fluxes(flux_plus_r)[0]
flux_minusz = mp.get_fluxes(flux_minus_z)[0]
flux_tot = flux_plusz + flux_plusr + flux_minusz
print(f"flux0:, {sim.meep_time():5.1f}, {flux_plusz:.10f}, "
f"{flux_plusr:.10f}, {flux_minusz:.10f}, {flux_tot:.10f}")
return flux_tot
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'-res',
type=float,
default=50.,
help='resolution (default: 25.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()
flux1 = radiated_flux(args.res, args.dpml_r, args.dpml_z, args.s, -1, +1.)
flux2 = radiated_flux(args.res, args.dpml_r, args.dpml_z, args.s, -1, -1.)
flux3 = radiated_flux(args.res, args.dpml_r, args.dpml_z, args.s, +1, +1.)
flux4 = radiated_flux(args.res, args.dpml_r, args.dpml_z, args.s, +1, -1.)
print(f"flux:, [-1, +1], {flux1}")
print(f"flux:, [-1, -1], {flux2}")
print(f"flux:, [+1, +1], {flux3}")
print(f"flux:, [+1, -1], {flux4}")
@oskooi
Copy link
Author

oskooi commented Jan 17, 2023

output from running the script with the default parameters:

flux:, [-1, +1], 0.10145817669081678
flux:, [-1, -1], 0.10145817669081678
flux:, [+1, +1], 0.09822574882730273
flux:, [+1, -1], 0.09822574882730273

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