Sliced array driving in finiterectlat-constant-driving.py

Former-commit-id: 72b9caa2a3396606b2aaff8ffcd578702926d10d
This commit is contained in:
Marek Nečada 2020-05-04 15:26:26 +03:00
parent 4bf3bb1bb1
commit a7c95d0ee0
1 changed files with 155 additions and 110 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3
import math
from qpms.argproc import ArgParser
from qpms.argproc import ArgParser, make_dict_action, sslice
figscale=3
ap = ArgParser(['rectlattice2d_finite', 'single_particle', 'single_lMax', 'single_omega'])
@ -15,6 +15,13 @@ ap.add_argument("-S", "--symmetry-adapted", default=None, help="Use a symmetry-a
ap.add_argument("-d", "--ccd-distance", type=float, default=math.nan, help='Far-field "CCD" distance from the sample')
ap.add_argument("-D", "--ccd-size", type=float, default=math.nan, help='Far-field "CCD" width and heighth')
ap.add_argument("-R", "--ccd-resolution", type=int, default=101, help='Far-field "CCD" resolution')
ap.add_argument("--xslice", default={None:None}, nargs=2,
action=make_dict_action(argtype=sslice, postaction='append', first_is_key=True),
)
ap.add_argument("--yslice", default={None:None}, nargs=2,
action=make_dict_action(argtype=sslice, postaction='append', first_is_key=True),
)
#ap.add_argument("--irrep", type=str, default="none", help="Irrep subspace (irrep index from 0 to 7, irrep label, or 'none' for no irrep decomposition")
@ -29,8 +36,8 @@ px, py = a.period
particlestr = ("sph" if a.height is None else "cyl") + ("_r%gnm" % (a.radius*1e9))
if a.height is not None: particlestr += "_h%gnm" % (a.height * 1e9)
defaultprefix = "cd_%s_p%gnmx%gnm_%dx%d_m%s_n%g_k_%g_%g_f%geV_L%d_micro-%s" % (
particlestr, px*1e9, py*1e9, Nx, Ny, str(a.material), a.refractive_index, a.wavevector[0], a.wavevector[1], a.eV, a.lMax, "SO3" if a.symmetry_adapted is None else a.symmetry_adapted)
defaultprefix = "cd_%s_p%gnmx%gnm_%dx%d_m%s_n%s_k_%g_%g_f%geV_L%d_micro-%s" % (
particlestr, px*1e9, py*1e9, Nx, Ny, str(a.material), str(a.background), a.wavevector[0], a.wavevector[1], a.eV, a.lMax, "SO3" if a.symmetry_adapted is None else a.symmetry_adapted)
logging.info("Default file prefix: %s" % defaultprefix)
import numpy as np
@ -44,6 +51,21 @@ from qpms import FinitePointGroup, ScatteringSystem, BesselType, eV, hbar
from qpms.symmetries import point_group_info
eh = eV/hbar
# Check slice ranges and generate all corresponding combinations
slicepairs = []
slicelabels = set(a.xslice.keys()) | set(a.yslice.keys())
for label in slicelabels:
rowslices = a.xslice.get(label, None)
colslices = a.yslice.get(label, None)
# TODO check validity of the slices.
if rowslices is None:
rowslices = [slice(None, None, None)]
if colslices is None:
colslices = [slice(None, None, None)]
for rs in rowslices:
for cs in colslices:
slicepairs.append((rs, cs))
def realdipfieldlabels(yp):
if yp == 0: return 'x'
if yp == 1: return 'y'
@ -113,6 +135,27 @@ ss, ssw = ScatteringSystem.create(particles=particles, medium=medium, omega=omeg
wavenumber = ap.background_epsmu.k(omega) # Currently, ScatteringSystem does not "remember" frequency nor wavenumber
# Mapping between ss particles and grid positions
positions = ss.positions
xpositions = np.unique(positions[:,0])
assert(len(xpositions) == Nx)
ypositions = np.unique(positions[:,1])
assert(len(ypositions == Ny))
# particle positions as integer indices
posmap = np.empty((positions.shape[0],2), dtype=int)
invposmap = np.empty((Nx, Ny), dtype=int)
for i, pos in enumerate(positions):
posmap[i,0] = np.searchsorted(xpositions, positions[i,0])
posmap[i,1] = np.searchsorted(ypositions, positions[i,1])
invposmap[posmap[i,0], posmap[i, 1]] = i
def fullvec2grid(fullvec, swapxy=False):
arr = np.empty((Nx,Ny,nelem), dtype=complex)
for pi, offset in enumerate(ss.fullvec_poffsets):
ix, iy = posmap[pi]
arr[ix, iy] = fullvec[offset:offset+nelem]
return np.swapaxes(arr, 0, 1) if swapxy else arr
outfile_tmp = defaultprefix + ".tmp" if a.output is None else a.output + ".tmp"
@ -140,10 +183,20 @@ else:
driving_full[y,y::nelem] = phases
scattered_full = np.zeros((nelem, ss.fecv_size),dtype=complex)
# Apply the driving on the specified slices only
nsp = len(slicepairs)
driving_full_sliced = np.zeros((nsp,) + driving_full.shape, dtype=complex)
p1range = np.arange(nelem)
for spi in range(nsp):
xs, ys = slicepairs[spi]
driven_pi = invposmap[xs, ys].flatten()
driven_y = ((driven_pi * nelem)[:,None] + p1range[None,:]).flatten()
driving_full_sliced[spi][:, driven_y] = driving_full[:, driven_y]
scattered_full = np.zeros((nsp, nelem, ss.fecv_size),dtype=complex)
scattered_ir = [None for iri in range(ss.nirreps)]
ir_contained = np.ones((nelem, ss.nirreps), dtype=bool)
ir_contained = np.ones((nsp, nelem, ss.nirreps), dtype=bool)
for iri in range(ss.nirreps):
logging.info("processing irrep %d/%d" % (iri, ss.nirreps))
@ -154,21 +207,22 @@ for iri in range(ss.nirreps):
#translation_matrix = ss.translation_matrix_packed(wavenumber, iri, BesselType.REGULAR) + np.eye(ss.saecv_sizes[iri])
#logging.info("auxillary translation matrix created")
scattered_ir[iri] = np.zeros((nelem, ss.saecv_sizes[iri]), dtype=complex)
scattered_ir_unpacked = np.zeros((nelem, ss.fecv_size), dtype=complex)
scattered_ir[iri] = np.zeros((nsp, nelem, ss.saecv_sizes[iri]), dtype=complex)
scattered_ir_unpacked = np.zeros((nsp, nelem, ss.fecv_size), dtype=complex)
for y in range(nelem):
ã = driving_full[y]
ãi = cleanarray(ss.pack_vector(ã, iri), copy=False)
if np.all(ãi == 0):
ir_contained[y, iri] = False
continue
= ssw.apply_Tmatrices_full(ã)
Tãi = ss.pack_vector(, iri)
fi = LU(Tãi)
scattered_ir[iri][y] = fi
scattered_ir_unpacked[y] = ss.unpack_vector(fi, iri)
scattered_full[y] += scattered_ir_unpacked[y]
for spi in range(nsp):
for y in range(nelem):
ã = driving_full_sliced[spi,y]
ãi = cleanarray(ss.pack_vector(ã, iri), copy=False)
if np.all(ãi == 0):
ir_contained[spi, y, iri] = False
continue
= ssw.apply_Tmatrices_full(ã)
Tãi = ss.pack_vector(, iri)
fi = LU(Tãi)
scattered_ir[iri][spi, y] = fi
scattered_ir_unpacked[spi, y] = ss.unpack_vector(fi, iri)
scattered_full[spi, y] += scattered_ir_unpacked[spi, y]
if a.save_gradually:
iriout = outfile_tmp + ".%d" % iri
np.savez(iriout, iri=iri, meta=vars(a),
@ -188,9 +242,10 @@ if not math.isnan(a.ccd_distance):
ccd_y = np.linspace(-ccd_size/2, ccd_size/2, a.ccd_resolution)
ccd_grid = np.meshgrid(ccd_x, ccd_y, (a.ccd_distance,), indexing='ij')
ccd_points = np.swapaxes(np.stack(ccd_grid, axis=-1).squeeze(axis=-2), 0,1) # First axis is y, second is x, because of imshow...
ccd_fields = np.empty((nelem,) + ccd_points.shape, dtype=complex)
for y in range(nelem):
ccd_fields[y] = ssw.scattered_E(scattered_full[y], ccd_points, btyp=BesselType.HANKEL_PLUS)
ccd_fields = np.empty((nsp, nelem,) + ccd_points.shape, dtype=complex)
for spi in range(nsp):
for y in range(nelem):
ccd_fields[spi, y] = ssw.scattered_E(scattered_full[spi, y], ccd_points, btyp=BesselType.HANKEL_PLUS)
logging.info("Far fields done")
outfile = defaultprefix + ".npz" if a.output is None else a.output
@ -211,104 +266,94 @@ np.savez(outfile, meta=vars(a),
logging.info("Saved to %s" % outfile)
if a.plot or (a.plot_out is not None):
positions = ss.positions
xpositions = np.unique(positions[:,0])
assert(len(xpositions) == Nx)
ypositions = np.unique(positions[:,1])
assert(len(ypositions == Ny))
# particle positions as integer indices
posmap = np.empty((positions.shape[0],2), dtype=int)
for i, pos in enumerate(positions):
posmap[i,0] = np.searchsorted(xpositions, positions[i,0])
posmap[i,1] = np.searchsorted(ypositions, positions[i,1])
if a.plot or (a.plot_out is not None):
def fullvec2grid(fullvec, swapxy=False):
arr = np.empty((Nx,Ny,nelem), dtype=complex)
for pi, offset in enumerate(ss.fullvec_poffsets):
ix, iy = posmap[pi]
arr[ix, iy] = fullvec[offset:offset+nelem]
return np.swapaxes(arr, 0, 1) if swapxy else arr
import matplotlib
matplotlib.use('pdf')
from matplotlib import pyplot as plt, cm
from matplotlib.backends.backend_pdf import PdfPages
t, l, m = bspec.tlm()
phasecm = cm.twilight
pmcm = cm.bwr
abscm = cm.plasma
fig, axes = plt.subplots(nelem, 12 if math.isnan(a.ccd_distance) else 16, figsize=(figscale*(12 if math.isnan(a.ccd_distance) else 16), figscale*nelem))
for yp in range(0,3): # TODO xy-dipoles instead?
axes[0,4*yp+0].set_title("abs / (E,1,%s)" % realdipfieldlabels(yp))
axes[0,4*yp+1].set_title("arg / (E,1,%s)" % realdipfieldlabels(yp))
axes[0,4*yp+2].set_title("Fabs / (E,1,%s)" % realdipfieldlabels(yp))
axes[0,4*yp+3].set_title("Farg / (E,1,%s)" % realdipfieldlabels(yp))
if not math.isnan(a.ccd_distance):
#axes[0,12].set_title("$E_{xy}$ @ $z = %g; \phi$" % a.ccd_distance)
#axes[0,13].set_title("$E_{xy}$ @ $z = %g; \phi + \pi/2$" % a.ccd_distance)
axes[0,12].set_title("$|E_{x}|^2$ @ $z = %g\,\mathrm{m}$" % a.ccd_distance)
axes[0,13].set_title("$|E_{y}|^2$ @ $z = %g\,\mathrm{m}$" % a.ccd_distance)
axes[0,14].set_title("$|E_x + E_y|^2$ @ $z = %g\,\mathrm{m}$" % a.ccd_distance)
axes[0,15].set_title("$|E_{z}|^2$ @ $z = %g\,\mathrm{m}$" % a.ccd_distance)
for gg in range(12,16):
axes[-1,gg].set_xlabel("$x/\mathrm{m}$")
for y in range(nelem):
fulvec = scattered_full[y]
if a.symmetry_adapted is not None:
driving_nonzero_y = [j for j in range(nelem) if abs(fvcs1[y,j]) > 1e-5]
driving_descr = ss1.irrep_names[iris1[y]]+'\n'+', '.join(('$'+cplx_nicestr(fvcs1[y,j])+'$' +
"(%s,%d,%+d)" % (("E" if t[j] == 2 else "M"), l[j], m[j]) for j in
driving_nonzero_y)) # TODO shorten the complex number precision
else:
driving_descr = "%s,%d,%+d"%('E' if t[y]==2 else 'M', l[y], m[y],)
axes[y,0].set_ylabel(driving_descr)
axes[y,-1].yaxis.set_label_position("right")
axes[y,-1].set_ylabel("$y/\mathrm{m}$\n"+driving_descr)
vecgrid = fullvec2grid(fulvec, swapxy=True)
vecgrid_ff = np.fft.fftshift(np.fft.fft2(vecgrid, axes=(0,1)),axes=(0,1))
lemax = np.amax(abs(vecgrid))
for yp in range(0,3):
if(np.amax(abs(realdipfields(vecgrid,yp))) > lemax*1e-5):
axes[y,yp*4].imshow(abs(realdipfields(vecgrid,yp)), vmin=0, interpolation='none')
axes[y,yp*4].text(0.5, 0.5, '%g' % np.amax(abs(realdipfields(vecgrid,yp))), horizontalalignment='center', verticalalignment='center', transform=axes[y,yp*4].transAxes)
axes[y,yp*4+1].imshow(np.angle(realdipfields(vecgrid,yp)), vmin=-np.pi, vmax=np.pi, cmap=phasecm, interpolation='none')
axes[y,yp*4+2].imshow(abs(realdipfields(vecgrid_ff,yp)), vmin=0, interpolation='none')
axes[y,yp*4+3].imshow(np.angle(realdipfields(vecgrid_ff,yp)), vmin=-np.pi, vmax=np.pi, cmap=phasecm, interpolation='none')
else:
for c in range(0,4):
axes[y,yp*4+c].tick_params(bottom=False, left=False, labelbottom=False, labelleft=False)
if not math.isnan(a.ccd_distance):
fxye=(-ccd_size/2, ccd_size/2, -ccd_size/2, ccd_size/2)
e2vmax = np.amax(np.linalg.norm(ccd_fields[y], axis=-1)**2)
xint = abs(ccd_fields[y,...,0])**2
yint = abs(ccd_fields[y,...,1])**2
xyint = abs(ccd_fields[y,...,0] + ccd_fields[y,...,1])**2
zint = abs(ccd_fields[y,...,2])**2
xintmax = np.amax(xint)
yintmax = np.amax(yint)
zintmax = np.amax(zint)
xyintmax = np.amax(xyint)
axes[y, 12].imshow(xint, origin="lower", extent=fxye, cmap=abscm, interpolation='none')
axes[y, 13].imshow(yint, origin="lower", extent=fxye, cmap=abscm, interpolation='none')
axes[y, 14].imshow(xyint, origin="lower", extent=fxye, cmap=abscm, interpolation='none')
axes[y, 15].imshow(zint, origin='lower', extent=fxye, cmap=abscm, interpolation='none')
axes[y, 12].text(0.5, 0.5, '%g\n%g' % (xintmax,xintmax/e2vmax),
horizontalalignment='center', verticalalignment='center', transform=axes[y,12].transAxes)
axes[y, 13].text(0.5, 0.5, '%g\n%g' % (yintmax,yintmax/e2vmax),
horizontalalignment='center', verticalalignment='center', transform=axes[y,13].transAxes)
axes[y, 14].text(0.5, 0.5, '%g\n%g' % (xyintmax,xyintmax/e2vmax),
horizontalalignment='center', verticalalignment='center', transform=axes[y,14].transAxes)
axes[y, 15].text(0.5, 0.5, '%g\n%g' % (zintmax,zintmax/e2vmax),
horizontalalignment='center', verticalalignment='center', transform=axes[y,15].transAxes)
for gg in range(12,16):
axes[y,gg].yaxis.tick_right()
for gg in range(12,15):
axes[y,gg].yaxis.set_major_formatter(plt.NullFormatter())
plotfile = defaultprefix + ".pdf" if a.plot_out is None else a.plot_out
fig.savefig(plotfile)
pp = PdfPages(plotfile)
for spi in range(nsp):
fig, axes = plt.subplots(nelem, 12 if math.isnan(a.ccd_distance) else 16, figsize=(figscale*(12 if math.isnan(a.ccd_distance) else 16), figscale*nelem))
for yp in range(0,3): # TODO xy-dipoles instead?
axes[0,4*yp+0].set_title("abs / (E,1,%s)" % realdipfieldlabels(yp))
axes[0,4*yp+1].set_title("arg / (E,1,%s)" % realdipfieldlabels(yp))
axes[0,4*yp+2].set_title("Fabs / (E,1,%s)" % realdipfieldlabels(yp))
axes[0,4*yp+3].set_title("Farg / (E,1,%s)" % realdipfieldlabels(yp))
if not math.isnan(a.ccd_distance):
#axes[0,12].set_title("$E_{xy}$ @ $z = %g; \phi$" % a.ccd_distance)
#axes[0,13].set_title("$E_{xy}$ @ $z = %g; \phi + \pi/2$" % a.ccd_distance)
axes[0,12].set_title("$|E_{x}|^2$ @ $z = %g\,\mathrm{m}$" % a.ccd_distance)
axes[0,13].set_title("$|E_{y}|^2$ @ $z = %g\,\mathrm{m}$" % a.ccd_distance)
axes[0,14].set_title("$|E_x + E_y|^2$ @ $z = %g\,\mathrm{m}$" % a.ccd_distance)
axes[0,15].set_title("$|E_{z}|^2$ @ $z = %g\,\mathrm{m}$" % a.ccd_distance)
for gg in range(12,16):
axes[-1,gg].set_xlabel("$x/\mathrm{m}$")
for y in range(nelem):
fulvec = scattered_full[spi,y]
if a.symmetry_adapted is not None:
driving_nonzero_y = [j for j in range(nelem) if abs(fvcs1[y,j]) > 1e-5]
driving_descr = ss1.irrep_names[iris1[y]]+'\n'+', '.join(('$'+cplx_nicestr(fvcs1[y,j])+'$' +
"(%s,%d,%+d)" % (("E" if t[j] == 2 else "M"), l[j], m[j]) for j in
driving_nonzero_y)) # TODO shorten the complex number precision
else:
driving_descr = "%s,%d,%+d"%('E' if t[y]==2 else 'M', l[y], m[y],)
axes[y,0].set_ylabel(driving_descr)
axes[y,-1].yaxis.set_label_position("right")
axes[y,-1].set_ylabel("$y/\mathrm{m}$\n"+driving_descr)
vecgrid = fullvec2grid(fulvec, swapxy=True)
vecgrid_ff = np.fft.fftshift(np.fft.fft2(vecgrid, axes=(0,1)),axes=(0,1))
lemax = np.amax(abs(vecgrid))
for yp in range(0,3):
if(np.amax(abs(realdipfields(vecgrid,yp))) > lemax*1e-5):
axes[y,yp*4].imshow(abs(realdipfields(vecgrid,yp)), vmin=0, interpolation='none')
axes[y,yp*4].text(0.5, 0.5, '%g' % np.amax(abs(realdipfields(vecgrid,yp))), horizontalalignment='center', verticalalignment='center', transform=axes[y,yp*4].transAxes)
axes[y,yp*4+1].imshow(np.angle(realdipfields(vecgrid,yp)), vmin=-np.pi, vmax=np.pi, cmap=phasecm, interpolation='none')
axes[y,yp*4+2].imshow(abs(realdipfields(vecgrid_ff,yp)), vmin=0, interpolation='none')
axes[y,yp*4+3].imshow(np.angle(realdipfields(vecgrid_ff,yp)), vmin=-np.pi, vmax=np.pi, cmap=phasecm, interpolation='none')
else:
for c in range(0,4):
axes[y,yp*4+c].tick_params(bottom=False, left=False, labelbottom=False, labelleft=False)
if not math.isnan(a.ccd_distance):
fxye=(-ccd_size/2, ccd_size/2, -ccd_size/2, ccd_size/2)
e2vmax = np.amax(np.linalg.norm(ccd_fields[spi,y], axis=-1)**2)
xint = abs(ccd_fields[spi,y,...,0])**2
yint = abs(ccd_fields[spi,y,...,1])**2
xyint = abs(ccd_fields[spi,y,...,0] + ccd_fields[spi,y,...,1])**2
zint = abs(ccd_fields[spi,y,...,2])**2
xintmax = np.amax(xint)
yintmax = np.amax(yint)
zintmax = np.amax(zint)
xyintmax = np.amax(xyint)
axes[y, 12].imshow(xint, origin="lower", extent=fxye, cmap=abscm, interpolation='none')
axes[y, 13].imshow(yint, origin="lower", extent=fxye, cmap=abscm, interpolation='none')
axes[y, 14].imshow(xyint, origin="lower", extent=fxye, cmap=abscm, interpolation='none')
axes[y, 15].imshow(zint, origin='lower', extent=fxye, cmap=abscm, interpolation='none')
axes[y, 12].text(0.5, 0.5, '%g\n%g' % (xintmax,xintmax/e2vmax),
horizontalalignment='center', verticalalignment='center', transform=axes[y,12].transAxes)
axes[y, 13].text(0.5, 0.5, '%g\n%g' % (yintmax,yintmax/e2vmax),
horizontalalignment='center', verticalalignment='center', transform=axes[y,13].transAxes)
axes[y, 14].text(0.5, 0.5, '%g\n%g' % (xyintmax,xyintmax/e2vmax),
horizontalalignment='center', verticalalignment='center', transform=axes[y,14].transAxes)
axes[y, 15].text(0.5, 0.5, '%g\n%g' % (zintmax,zintmax/e2vmax),
horizontalalignment='center', verticalalignment='center', transform=axes[y,15].transAxes)
for gg in range(12,16):
axes[y,gg].yaxis.tick_right()
for gg in range(12,15):
axes[y,gg].yaxis.set_major_formatter(plt.NullFormatter())
fig.text(0, 0, str(slicepairs[spi]), horizontalalignment='left', verticalalignment='bottom')
pp.savefig()
pp.close()
exit(0)