Skip to content

Instantly share code, notes, and snippets.

@oskooi
Last active February 17, 2023 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oskooi/69581f94472e5547467f6235f26fcbee to your computer and use it in GitHub Desktop.
Save oskooi/69581f94472e5547467f6235f26fcbee to your computer and use it in GitHub Desktop.
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
src_cmpt = mp.Er
res = 50.
dpml = 1.
s = 5.
m = +1
fcen = 1.
cell_size = mp.Vector3(s+dpml,0,s+2*dpml)
pml_layers = [
mp.PML(dpml, direction=mp.R),
mp.PML(dpml, direction=mp.Z),
]
sources = [
mp.Source(
src=mp.ContinuousSource(fcen),
center=mp.Vector3(),
component=src_cmpt,
),
]
sim = mp.Simulation(
resolution=res,
cell_size=cell_size,
dimensions=mp.CYLINDRICAL,
m=m,
sources=sources,
boundary_layers=pml_layers,
)
sim.run(until=56.5)
fig, ax = plt.subplots()
sim.plot2D(ax=ax)
if mp.am_master():
fig.savefig(
'cyl_plot2D.png',
dpi=150,
bbox_inches='tight',
)
er = sim.get_array(
component=mp.Er,
center=mp.Vector3(),
size=mp.Vector3(0,0,s),
cmplx=True,
)
ep = sim.get_array(
component=mp.Ep,
center=mp.Vector3(),
size=mp.Vector3(0,0,s),
cmplx=True,
)
ez = sim.get_array(
component=mp.Ez,
center=mp.Vector3(),
size=mp.Vector3(0,0,s),
cmplx=True,
)
z = np.linspace(-0.5*s,0.5*s,len(er))
fig, ax = plt.subplots(ncols=3)
fig.subplots_adjust(wspace=0.4)
fig.suptitle('PR #2383')
ax[0].semilogy(z,np.abs(np.real(er)),'bo-.',label='$|\Re(E_r)|$')
ax[0].semilogy(z,np.abs(np.imag(er)),'ro--',label='$|\Im(E_r)|$')
ax[0].set_xlabel('z')
ax[0].set_ylabel('$E_r$ at $r=0$')
ax[0].set_title('$E_r$')
ax[0].legend(loc='lower center')
ax[1].semilogy(z,np.abs(np.real(ep)),'bo-.',label='$|\Re(E_\phi)|$')
ax[1].semilogy(z,np.abs(np.imag(ep)),'ro--',label='$|\Im(E_\phi)|$')
ax[1].set_xlabel('z')
ax[1].set_ylabel('$E_\phi$ at $r=0$')
ax[1].set_title('$E_\phi$')
ax[1].legend(loc='lower center')
ax[2].semilogy(z,np.abs(np.real(ez)),'bo-.',label='$|\Re(E_z)|$')
ax[2].semilogy(z,np.abs(np.imag(ez)),'ro--',label='$|\Im(E_z)|$')
ax[2].set_xlabel('z')
ax[2].set_ylabel('$E_z$ at $r=0$')
ax[2].set_title('$E_z$')
ax[2].legend(loc='lower center')
if mp.am_master():
fig.savefig(
f'cyl_Er_Ep_Ez_real_imag.png',
dpi=150,
bbox_inches='tight'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment