def is_test_coord_passed(x,m,coordX,coordY,coordZ): terms, E, H = fieldnlay(x, m, coordX,coordY,coordZ) Er = np.absolute(E) Eabs = np.sqrt(Er[0, :, 0]**2 + Er[0, :, 1]**2 + Er[0, :, 2]**2) analytic_E = (3/(m[0,0]**2+2)).real for value in Eabs: #print(value, value-analytic_E) if ( value-analytic_E > 10e-7 ): print(bcolors.FAIL+"Test failed: value="+str(value)+" for m="+str(m[0,0]) +" instead of analytic Eabs="+str(analytic_E)) print("Coords",coord) return False return True
def GetField(crossplane, npts, factor, x, m, pl): """ crossplane: XZ, YZ, XY npts: number of point in each direction factor: ratio of plotting size to outer size of the particle x: size parameters for particle layers m: relative index values for particle layers """ scan = np.linspace(-factor * x[-1], factor * x[-1], npts) zero = np.zeros(npts * npts, dtype=np.float64) if crossplane == 'XZ': coordX, coordZ = np.meshgrid(scan, scan) coordX.resize(npts * npts) coordZ.resize(npts * npts) coordY = zero coordPlot1 = coordX coordPlot2 = coordZ elif crossplane == 'YZ': coordY, coordZ = np.meshgrid(scan, scan) coordY.resize(npts * npts) coordZ.resize(npts * npts) coordX = zero coordPlot1 = coordY coordPlot2 = coordZ elif crossplane == 'XY': coordX, coordY = np.meshgrid(scan, scan) coordX.resize(npts * npts) coordY.resize(npts * npts) coordZ = zero coordPlot1 = coordY coordPlot2 = coordX coord = np.vstack((coordX, coordY, coordZ)).transpose() terms, E, H = fieldnlay(np.array([x]), np.array([m]), coord, pl=pl) Ec = E[0, :, :] Hc = H[0, :, :] P = [] P = np.array(map(lambda n: np.linalg.norm(np.cross(Ec[n], Hc[n])).real, range(0, len(E[0])))) # for n in range(0, len(E[0])): # P.append(np.linalg.norm( np.cross(Ec[n], np.conjugate(Hc[n]) ).real/2 )) return Ec, Hc, P, coordPlot1, coordPlot2
def GetField(crossplane, npts, factor, x, m, pl): """ crossplane: XZ, YZ, XY, or XYZ (half is XZ, half is YZ) npts: number of point in each direction factor: ratio of plotting size to outer size of the particle x: size parameters for particle layers m: relative index values for particle layers """ coordX, coordY, coordZ, scan = GetCoords(crossplane, npts, factor, x) terms, E, H = fieldnlay(x, m, coordX, coordY, coordZ, pl=pl) if len(E.shape) > 2: E = E[0, :, :] H = H[0, :, :] S = np.cross(E, np.conjugate(H)).real print(S) if crossplane=='XZ': Sx = np.resize(S[:, 2], (npts, npts)).T Sy = np.resize(S[:, 0], (npts, npts)).T elif crossplane == 'YZ': Sx = np.resize(S[:, 2], (npts, npts)).T Sy = np.resize(S[:, 1], (npts, npts)).T elif crossplane == 'XY': Sx = np.resize(S[:, 1], (npts, npts)).T Sy = np.resize(S[:, 0], (npts, npts)).T elif crossplane=='XYZ': # Upper half: XZ; Lower half: YZ Sx = np.resize(S[:, 2], (npts, npts)).T Sy = np.resize(S[:, 0], (npts, npts)).T Sy[scan<0] = np.resize(S[:, 1], (npts, npts)).T[scan<0] else: print("Unknown crossplane") import sys sys.exit() return E, H, S, scan, Sx, Sy
def GetFlow3D(x0, y0, z0, max_length, max_angle, x, m, pl): # Initial position flow_x = [x0] flow_y = [y0] flow_z = [z0] max_step = x[-1] / 3 min_step = x[0] / 2000 # max_step = min_step step = min_step * 2.0 if max_step < min_step: max_step = min_step coord = np.vstack(([flow_x[-1]], [flow_y[-1]], [flow_z[-1]])).transpose() terms, E, H = fieldnlay(np.array([x]), np.array([m]), coord, pl=pl) Ec, Hc = E[0, 0, :], H[0, 0, :] S = np.cross(Ec, Hc.conjugate()).real Snorm_prev = S / np.linalg.norm(S) Sprev = S length = 0 dpos = step count = 0 while length < max_length: count = count + 1 if (count > 4000): # Limit length of the absorbed power streamlines break if step < max_step: step = step * 2.0 r = np.sqrt(flow_x[-1]**2 + flow_y[-1]**2 + flow_z[-1]**2) while step > min_step: # Evaluate displacement from previous poynting vector dpos = step dx = dpos * Snorm_prev[0] dy = dpos * Snorm_prev[1] dz = dpos * Snorm_prev[2] # Test the next position not to turn\chang size for more than # max_angle coord = np.vstack(([flow_x[-1] + dx], [flow_y[-1] + dy], [flow_z[-1] + dz])).transpose() terms, E, H = fieldnlay(np.array([x]), np.array([m]), coord, pl=pl) Ec, Hc = E[0, 0, :], H[0, 0, :] Eth = max(np.absolute(Ec)) / 1e10 Hth = max(np.absolute(Hc)) / 1e10 for i in range(0, len(Ec)): if abs(Ec[i]) < Eth: Ec[i] = 0 + 0j if abs(Hc[i]) < Hth: Hc[i] = 0 + 0j S = np.cross(Ec, Hc.conjugate()).real if not np.isfinite(S).all(): break Snorm = S / np.linalg.norm(S) diff = (S - Sprev) / max(np.linalg.norm(S), np.linalg.norm(Sprev)) if np.linalg.norm(diff) < max_angle: # angle = angle_between(Snorm, Snorm_prev) # if abs(angle) < max_angle: break step = step / 2.0 # 3. Save result Sprev = S Snorm_prev = Snorm dx = dpos * Snorm_prev[0] dy = dpos * Snorm_prev[1] dz = dpos * Snorm_prev[2] length = length + step flow_x.append(flow_x[-1] + dx) flow_y.append(flow_y[-1] + dy) flow_z.append(flow_z[-1] + dz) return np.array(flow_x), np.array(flow_y), np.array(flow_z)
print "m =", m npts = 281 factor = 2.5 scan = np.linspace(-factor * x[0, 2], factor * x[0, 2], npts) coordX, coordZ = np.meshgrid(scan, scan) coordX.resize(npts * npts) coordZ.resize(npts * npts) coordY = np.zeros(npts * npts, dtype=np.float64) coord = np.vstack((coordX, coordY, coordZ)).transpose() terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2 = scattnlay(x, m) terms, E, H = fieldnlay(x, m, coord) print("Qabs = " + str(Qabs)) Er = np.absolute(E) Hr = np.absolute(H) # |E|/|Eo| Eabs = np.sqrt(Er[0, :, 0]**2 + Er[0, :, 1]**2 + Er[0, :, 2]**2) Eangle = np.angle(E[0, :, 0]) / np.pi * 180 Habs = np.sqrt(Hr[0, :, 0]**2 + Hr[0, :, 1]**2 + Hr[0, :, 2]**2) Hangle = np.angle(H[0, :, 1]) / np.pi * 180 result = np.vstack((coordX, coordY, coordZ, Eabs)).transpose() result2 = np.vstack((coordX, coordY, coordZ, Eangle)).transpose() try:
def GetField(crossplane, npts, factor, x, m, pl): """ crossplane: XZ, YZ, XY, or XYZ (half is XZ, half is YZ) npts: number of point in each direction factor: ratio of plotting size to outer size of the particle x: size parameters for particle layers m: relative index values for particle layers """ scan = np.linspace(-factor * x[-1], factor * x[-1], npts) zero = np.zeros(npts * npts, dtype=np.float64) if crossplane == 'XZ': coordX, coordZ = np.meshgrid(scan, scan) coordX.resize(npts * npts) coordZ.resize(npts * npts) coordY = zero coordPlot1 = coordX coordPlot2 = coordZ elif crossplane == 'YZ': coordY, coordZ = np.meshgrid(scan, scan) coordY.resize(npts * npts) coordZ.resize(npts * npts) coordX = zero coordPlot1 = coordY coordPlot2 = coordZ elif crossplane == 'XY': coordX, coordY = np.meshgrid(scan, scan) coordX.resize(npts * npts) coordY.resize(npts * npts) coordZ = zero coordPlot1 = coordY coordPlot2 = coordX elif crossplane == 'XYZ': coordX, coordZ = np.meshgrid(scan, scan) coordY, coordZ = np.meshgrid(scan, scan) coordPlot1, coordPlot2 = np.meshgrid(scan, scan) coordPlot1.resize(npts * npts) coordPlot2.resize(npts * npts) half = npts // 2 # coordX = np.copy(coordX) # coordY = np.copy(coordY) coordX[:, :half] = 0 coordY[:, half:] = 0 coordX.resize(npts * npts) coordY.resize(npts * npts) coordZ.resize(npts * npts) else: print("Unknown crossplane") import sys sys.exit() terms, E, H = fieldnlay(np.array([x]), np.array([m]), coordX, coordY, coordZ, pl=pl) Ec = E[0, :, :] Hc = H[0, :, :] P = np.array( list( map( lambda n: np.linalg.norm( np.cross(Ec[n], np.conjugate(Hc[n]) # Hc[n] )).real, range(0, len(E[0]))))) print(P) # for n in range(0, len(E[0])): # P.append(np.linalg.norm( np.cross(Ec[n], np.conjugate(Hc[n]) ).real/2 )) return Ec, Hc, P, coordPlot1, coordPlot2
print "x =", x print "m =", m npts = 281 factor = 2.5 scan = np.linspace(-factor * x[2], factor * x[2], npts) coordX, coordZ = np.meshgrid(scan, scan) coordX.resize(npts * npts) coordZ.resize(npts * npts) coordY = np.zeros(npts * npts, dtype=np.float64) terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2 = scattnlay(x, m) terms, E, H = fieldnlay(x, m, coordX, coordY, coordZ) print("Qabs = " + str(Qabs)) Er = np.absolute(E) Hr = np.absolute(H) # |E|/|Eo| Eabs = np.sqrt(Er[:, 0]**2 + Er[:, 1]**2 + Er[:, 2]**2) Eangle = np.angle(E[:, 0]) / np.pi * 180 Habs = np.sqrt(Hr[:, 0]**2 + Hr[:, 1]**2 + Hr[:, 2]**2) Hangle = np.angle(H[:, 1]) / np.pi * 180 result = np.vstack((coordX, coordY, coordZ, Eabs)).transpose() result2 = np.vstack((coordX, coordY, coordZ, Eangle)).transpose() try:
print "m =", m npts = 501 scan = np.linspace(-4.0*x[0, 0], 4.0*x[0, 0], npts) coordX, coordY = np.meshgrid(scan, scan) coordX.resize(npts*npts) coordY.resize(npts*npts) coordZ = np.zeros(npts*npts, dtype = np.float64) coord = np.vstack((coordX, coordY, coordZ)).transpose() start_time = time.time() terms, E, H = fieldnlay(x, m, coord) elapsed_time = time.time() - start_time print "Time: ", elapsed_time Er = np.absolute(E) # |E|/|Eo| Eh = np.sqrt(Er[0, :, 0]**2 + Er[0, :, 1]**2 + Er[0, :, 2]**2) result = np.vstack((coordX, coordY, coordZ, Eh)).transpose() try: import matplotlib.pyplot as plt from matplotlib import cm from matplotlib.colors import LogNorm
m = np.ones((1, 1), dtype = np.complex128) m[0, 0] = n1/nm print "x =", x print "m =", m npts = 1001 scan = np.linspace(-3.0*x[0, -1], 3.0*x[0, -1], npts) coordX, coordZ = np.meshgrid(scan, scan) coordX.resize(npts*npts) coordZ.resize(npts*npts) coordY = np.zeros(npts*npts, dtype = np.float64) terms, E, H = fieldnlay(x, m, coordX, coordY, coordZ) Er = np.absolute(E) # |E|/|Eo| Eh = np.sqrt(Er[0, :, 0]**2 + Er[0, :, 1]**2 + Er[0, :, 2]**2) result = np.vstack((coordX, coordY, coordZ, Eh)).transpose() try: import matplotlib.pyplot as plt from matplotlib import cm from matplotlib.colors import LogNorm min_tick = 0.5 max_tick = 1.0
def GetFlow3D(x0, y0, z0, max_length, max_angle, x, m, pl): # Initial position flow_x = [x0] flow_y = [y0] flow_z = [z0] max_step = x[-1] / 3 min_step = x[0] / 2000 # max_step = min_step step = min_step * 2.0 if max_step < min_step: max_step = min_step coord = np.vstack(([flow_x[-1]], [flow_y[-1]], [flow_z[-1]])).transpose() terms, E, H = fieldnlay(np.array([x]), np.array([m]), coord, pl=pl) Ec, Hc = E[0, 0, :], H[0, 0, :] S = np.cross(Ec, Hc.conjugate()).real Snorm_prev = S / np.linalg.norm(S) Sprev = S length = 0 dpos = step count = 0 while length < max_length: count = count + 1 if (count > 4000): # Limit length of the absorbed power streamlines break if step < max_step: step = step * 2.0 r = np.sqrt(flow_x[-1]**2 + flow_y[-1]**2 + flow_z[-1]**2) while step > min_step: # Evaluate displacement from previous poynting vector dpos = step dx = dpos * Snorm_prev[0] dy = dpos * Snorm_prev[1] dz = dpos * Snorm_prev[2] # Test the next position not to turn\chang size for more than # max_angle coord = np.vstack(([flow_x[-1] + dx], [flow_y[-1] + dy], [flow_z[-1] + dz])).transpose() terms, E, H = fieldnlay(np.array([x]), np.array([m]), coord, pl=pl) Ec, Hc = E[0, 0, :], H[0, 0, :] Eth = max(np.absolute(Ec)) / 1e10 Hth = max(np.absolute(Hc)) / 1e10 for i in xrange(0, len(Ec)): if abs(Ec[i]) < Eth: Ec[i] = 0 + 0j if abs(Hc[i]) < Hth: Hc[i] = 0 + 0j S = np.cross(Ec, Hc.conjugate()).real if not np.isfinite(S).all(): break Snorm = S / np.linalg.norm(S) diff = (S - Sprev) / max(np.linalg.norm(S), np.linalg.norm(Sprev)) if np.linalg.norm(diff) < max_angle: # angle = angle_between(Snorm, Snorm_prev) # if abs(angle) < max_angle: break step = step / 2.0 # 3. Save result Sprev = S Snorm_prev = Snorm dx = dpos * Snorm_prev[0] dy = dpos * Snorm_prev[1] dz = dpos * Snorm_prev[2] length = length + step flow_x.append(flow_x[-1] + dx) flow_y.append(flow_y[-1] + dy) flow_z.append(flow_z[-1] + dz) return np.array(flow_x), np.array(flow_y), np.array(flow_z)
m = np.ones((1, 1), dtype=np.complex128) m[0, 0] = n1 / nm nptsx = np.int32(extent * resolution) nptsy = np.int32(extent * resolution) scanx = np.linspace(-extent / 2, extent / 2, nptsx, endpoint=True) * twopi scany = np.linspace(-extent / 2, extent / 2, nptsy, endpoint=True) * twopi coordX, coordY = np.meshgrid(scanx, scany) coordX.resize(nptsx * nptsy) coordY.resize(nptsx * nptsy) coordZ = np.ones(nptsx * nptsy, dtype=np.float64) * distance * twopi coord = np.vstack((coordX, coordY, coordZ)).transpose() terms, E, H = scattnlay.fieldnlay(x, m, coord) # take the x-component of the electric field Ex = E[:, :, 0].reshape(nptsx, nptsy) # normalize by the background field (free space propagation) Ex /= np.exp(1j * 2 * np.pi * distance * nm) # plot the phase (np.angle) of the x-component of the electric field ax = plt.subplot(111) mapper = plt.imshow(np.angle(Ex)) plt.colorbar(mapper, ax=ax, label="phase [rad]") plt.title("phase retardation introduced by a dielectric sphere") plt.show()
m = np.ones((1, 1), dtype = np.complex128) m[0, 0] = n1/nm nptsx = extent*resolution nptsy = extent*resolution scanx = np.linspace(-extent/2, extent/2, nptsx, endpoint=True)*twopi scany = np.linspace(-extent/2, extent/2, nptsy, endpoint=True)*twopi coordX, coordY = np.meshgrid(scanx, scany) coordX.resize(nptsx*nptsy) coordY.resize(nptsx*nptsy) coordZ = np.ones(nptsx*nptsy, dtype=np.float64)*distance*twopi coord = np.vstack((coordX, coordY, coordZ)).transpose() terms, E, H = scattnlay.fieldnlay(x, m, coord) # take the x-component of the electric field Ex = E[:,:,0].reshape(nptsx, nptsy) # normalize by the background field (free space propagation) Ex /= np.exp(1j*2*np.pi*distance*nm) # plot the phase (np.angle) of the x-component of the electric field ax = plt.subplot(111) mapper = plt.imshow(np.angle(Ex)) plt.colorbar(mapper, ax=ax, label="phase [rad]") plt.title("phase retardation introduced by a dielectric sphere") plt.show()
print(" Qsca = " + str(Qsca)+" terms = "+str(terms)) terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2 = scattnlay( np.array([x]), np.array([m]), mp=True) print("mp Qsca = " + str(Qsca)+" terms = "+str(terms)) scan = np.linspace(-factor*x[-1], factor*x[-1], npts) zero = np.zeros(npts*npts, dtype = np.float64) coordX, coordZ = np.meshgrid(scan, scan) coordX.resize(npts * npts) coordZ.resize(npts * npts) coordY = zero terms, E, H = fieldnlay( np.array([x]), np.array([m]), coordX, coordY, coordZ, mp=isMP, nmax=nmax ) Ec = E[0, :, :] Er = np.absolute(Ec) Eabs2 = (Er[:, 0]**2 + Er[:, 1]**2 + Er[:, 2]**2) Eabs_data = np.resize(Eabs2, (npts, npts)) label = r'$|E|^2$' pos = plt.imshow(Eabs_data.T, cmap='gnuplot', # cmap='jet', vmin=0., vmax=14 ) plt.colorbar(pos) print(np.min(Eabs_data), np.max(Eabs_data)," terms = "+str(terms), ' size=', Eabs_data.size)
x = np.array([radius * twopi], dtype=np.float64) # Set the refractive index of the sphere, normalized to that of the medium m = np.array([n1 / nm], dtype=np.complex128) nptsx = int(extent * resolution) nptsy = int(extent * resolution) scanx = np.linspace(-extent / 2, extent / 2, nptsx, endpoint=True) * twopi scany = np.linspace(-extent / 2, extent / 2, nptsy, endpoint=True) * twopi coordX, coordY = np.meshgrid(scanx, scany) coordX.resize(nptsx * nptsy) coordY.resize(nptsx * nptsy) coordZ = np.ones(nptsx * nptsy, dtype=np.float64) * distance * twopi terms, E, H = scattnlay.fieldnlay(x, m, coordX, coordY, coordZ) # take the x-component of the electric field Ex = E[:, 0].reshape(nptsx, nptsy) # normalize by the background field (free space propagation) Ex /= np.exp(2j * np.pi * distance * nm) # plot the phase (np.angle) of the x-component of the electric field ax = plt.subplot(111) mapper = plt.imshow(np.angle(Ex)) plt.colorbar(mapper, ax=ax, label="phase [rad]") plt.title("phase retardation introduced by a dielectric sphere") plt.show()
np.array([x]), np.array([m])) print("Qsca = " + str(Qsca)) terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2 = scattnlay(np.array([x]), np.array([m]), mp=True) print("mp Qsca = " + str(Qsca)) scan = np.linspace(-factor * x[-1], factor * x[-1], npts) zero = np.zeros(npts * npts, dtype=np.float64) coordX, coordZ = np.meshgrid(scan, scan) coordX.resize(npts * npts) coordZ.resize(npts * npts) coordY = zero terms, E, H = fieldnlay(np.array([x]), np.array([m]), coordX, coordY, coordZ, mp=True) Ec = E[0, :, :] Er = np.absolute(Ec) Eabs2 = (Er[:, 0]**2 + Er[:, 1]**2 + Er[:, 2]**2) Eabs_data = np.resize(Eabs2, (npts, npts)) label = r'$|E|^2$' plt.imshow(Eabs_data, cmap='jet', vmin=0., vmax=14) print(np.min(Eabs_data), np.max(Eabs_data)) plt.savefig("R" + str(total_r) + "mkm_mp.jpg") # plt.show()
x = np.array([radius*twopi], dtype = np.float64) # Set the refractive index of the sphere, normalized to that of the medium m = np.array([n1/nm], dtype = np.complex128) nptsx = int(extent*resolution) nptsy = int(extent*resolution) scanx = np.linspace(-extent/2, extent/2, nptsx, endpoint=True)*twopi scany = np.linspace(-extent/2, extent/2, nptsy, endpoint=True)*twopi coordX, coordY = np.meshgrid(scanx, scany) coordX.resize(nptsx*nptsy) coordY.resize(nptsx*nptsy) coordZ = np.ones(nptsx*nptsy, dtype=np.float64)*distance*twopi terms, E, H = scattnlay.fieldnlay(x, m, coordX, coordY, coordZ) # take the x-component of the electric field Ex = E[:,0].reshape(nptsx, nptsy) # normalize by the background field (free space propagation) Ex /= np.exp(2j*np.pi*distance*nm) # plot the phase (np.angle) of the x-component of the electric field ax = plt.subplot(111) mapper = plt.imshow(np.angle(Ex)) plt.colorbar(mapper, ax=ax, label="phase [rad]") plt.title("phase retardation introduced by a dielectric sphere") plt.show()