Skip to content

Instantly share code, notes, and snippets.

@blyves
Created June 9, 2017 12:50
Show Gist options
  • Save blyves/fca51a00560ecf6b8975c88d5cd2f531 to your computer and use it in GitHub Desktop.
Save blyves/fca51a00560ecf6b8975c88d5cd2f531 to your computer and use it in GitHub Desktop.
import numpy
import EMpy
import pylab
def epsfunc(x_, y_):
"""Return a matrix describing a 2d material.
:param x_: x values
:param y_: y values
:return: 2d-matrix
"""
xx, yy = numpy.meshgrid(x_, y_)
E = numpy.where((numpy.abs(xx.T - 1.24e-6) <= .24e-6) *
(numpy.abs(yy.T - 1.11e-6) <= .11e-6),
3.4757**2,
1.446**2)
return E
wl = 1.55e-6
x = numpy.linspace(0, 2.48e-6, 125)
y = numpy.linspace(0, 2.22e-6, 112)
#For the plotting
x_1 = numpy.linspace(0, 2.48e-6, 124)
y_1 = numpy.linspace(0, 2.22e-6, 111)
X,Y = numpy.meshgrid(x_1,y_1) #for the plot of the E field
X_2,Y_2= numpy.meshgrid(x,y) #for the plot of the H field
neigs = 2
tol = 1e-8
boundary = '0000'
solver = EMpy.modesolvers.FD.VFDModeSolver(wl, x, y, epsfunc, boundary).solve(
neigs, tol)
#####################################################################################
#calculate the H field from the E field
##################################################################
######
#Define Variables needed for the calculation of the H filed from the E field
######
Ex = solver.modes[1].Ex
Ey = solver.modes[1].Ey
Ez = solver.modes[1].Ez
dx = 2.48e-6/124
dy = 2.22e-6/111
c = 299792458. # speed of light in vacuum
mu_0 = 4 * numpy.pi * 1e-7 # permeability
k = 2*numpy.pi / wl #wave number
angularFrequency = k * c
#Define the H grating
Hx = numpy.zeros(numpy.shape(Ex),complex)
Hy = numpy.zeros(numpy.shape(Ey),complex)
Hz = numpy.zeros(numpy.shape(Ez),complex)
def difference(E,index_x,index_y,axis):
#Calculates dE at a certain point in direction of the axis
#in x direction
if axis == 'x':
#to make it work at the end of the array
if index_x+1 == numpy.shape(E)[0]:
diff = E[index_x,index_y]-E[index_x-1,index_y]
else:
diff = E[index_x+1,index_y]-E[index_x,index_y]
#in y direction
if axis == 'y':
#to make it work at the end of the array
if index_y+1 == numpy.shape(E)[1]:
diff = E[index_x,index_y]-E[index_x,index_y-1]
else:
diff = E[index_x,index_y+1]-E[index_x,index_y]
return diff
#Calculates the Magnetic field from the Electric field
for i in range(len(x)-1):
for j in range(len(y)-1):
#dEx/dy-dEy/dx
Hz[i,j] = -1/(1j* angularFrequency*mu_0 )\
*(difference(Ey,i,j,'x')/dx\
-difference(Ex,i,j,'y')/dy )
#dEz/dy
Hx[i,j] = -1/(1j* angularFrequency*mu_0 )\
*difference(Ez,i,j,'y')/dy
#-dEz/dx
Hy[i,j] = -1/(1j* angularFrequency*mu_0 )\
*-difference(Ez,i,j,'x')/dx
#######################################################
#plot
#######################################################
#linear
fig = pylab.figure()
fig.add_subplot(3, 3, 1)
pylab.contourf(X.T*1e6,Y.T*1e6,abs(solver.modes[1].Ex),50)
pylab.title('Ex')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
fig.add_subplot(3, 3, 2)
pylab.contourf(X.T*1e6,Y.T*1e6,abs(solver.modes[1].Ey),50)
pylab.title('Ey')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
fig.add_subplot(3, 3, 3)
pylab.contourf(X.T*1e6,Y.T*1e6,abs(solver.modes[1].Ez),50)
pylab.title('Ez')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
fig.add_subplot(3, 3, 4)
pylab.contourf(X_2.T*1e6,Y_2.T*1e6,abs(solver.modes[1].Hx),50)
pylab.title('Hx')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
fig.add_subplot(3, 3, 5)
pylab.contourf(X_2.T*1e6,Y_2.T*1e6,abs(solver.modes[1].Hy),50)
pylab.title('Hy')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
fig.add_subplot(3, 3, 6)
pylab.contourf(X_2.T*1e6,Y_2.T*1e6,abs(solver.modes[1].Hz),50)
pylab.title('Hz')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
fig.add_subplot(3, 3, 7)
pylab.contourf(X.T*1e6,Y.T*1e6,abs(Hx),50)
pylab.title('Hx from E')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
fig.add_subplot(3, 3, 8)
pylab.contourf(X.T*1e6,Y.T*1e6,abs(Hy),50)
pylab.title('Hy from E')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
fig.add_subplot(3, 3, 9)
pylab.contourf(X.T*1e6,Y.T*1e6,abs(Hz),50)
pylab.title('Hz from E')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
pylab.tight_layout()
pylab.show()
#log
fig = pylab.figure()
fig.add_subplot(3, 3, 1)
pylab.contourf(X.T*1e6,Y.T*1e6,abs(solver.modes[1].Ex),levels=numpy.logspace(2,7,10), locator=pylab.LogLocator())
pylab.title('Ex')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
fig.add_subplot(3, 3, 2)
pylab.contourf(X.T*1e6,Y.T*1e6,abs(solver.modes[1].Ey),levels=numpy.logspace(2,7,10), locator=pylab.LogLocator())
pylab.title('Ey')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
fig.add_subplot(3, 3, 3)
pylab.contourf(X.T*1e6,Y.T*1e6,abs(solver.modes[1].Ez),levels=numpy.logspace(2,7,10), locator=pylab.LogLocator())
pylab.title('Ez')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
fig.add_subplot(3, 3, 4)
pylab.contourf(X_2.T*1e6,Y_2.T*1e6,abs(solver.modes[1].Hx),levels=numpy.logspace(2,7,10), locator=pylab.LogLocator())
pylab.title('Hx')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
fig.add_subplot(3, 3, 5)
pylab.contourf(X_2.T*1e6,Y_2.T*1e6,abs(solver.modes[1].Hy),levels=numpy.logspace(2,7,10), locator=pylab.LogLocator())
pylab.title('Hy')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
fig.add_subplot(3, 3, 6)
pylab.contourf(X_2.T*1e6,Y_2.T*1e6,abs(solver.modes[1].Hz),levels=numpy.logspace(2,7,10), locator=pylab.LogLocator())
pylab.title('Hz')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
fig.add_subplot(3, 3, 7)
pylab.contourf(X.T*1e6,Y.T*1e6,abs(Hx),levels=numpy.logspace(0,5,10), locator=pylab.LogLocator())
pylab.title('Hx from E')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
fig.add_subplot(3, 3, 8)
pylab.contourf(X.T*1e6,Y.T*1e6,abs(Hy),levels=numpy.logspace(0,5,10), locator=pylab.LogLocator())
pylab.title('Hy from E')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
fig.add_subplot(3, 3, 9)
pylab.contourf(X.T*1e6,Y.T*1e6,abs(Hz),levels=numpy.logspace(0,5,10), locator=pylab.LogLocator())
pylab.title('Hz from E')
pylab.xlabel('x [um]')
pylab.ylabel('y [um]')
pylab.colorbar()
pylab.tight_layout()
pylab.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment