diff --git a/misc/infiniterectlat-k0realfreqsvd.py b/misc/infiniterectlat-k0realfreqsvd.py new file mode 100755 index 0000000..251a720 --- /dev/null +++ b/misc/infiniterectlat-k0realfreqsvd.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python3 + +import math +from qpms.argproc import ArgParser + +ap = ArgParser(['rectlattice2d', 'single_particle', 'single_lMax', 'omega_seq']) +ap.add_argument("-o", "--output", type=str, required=False, help='output path (if not provided, will be generated automatically)') +ap.add_argument("-O", "--plot-out", type=str, required=False, help="path to plot output (optional)") +ap.add_argument("-P", "--plot", action='store_true', help="if -p not given, plot to a default path") +ap.add_argument("-s", "--singular_values", type=int, default=10, help="Number of singular values to plot") + +a=ap.parse_args() + +import logging +logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO) + +px, py = a.period + +#Important! The particles are supposed to be of D2h/D4h symmetry +thegroup = 'D4h' if px == py else 'D2h' + +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 = "%s_p%gnmx%gnm_m%s_n%g_f(%g..%g..%g)eV_L%d_SVGamma" % ( + particlestr, px*1e9, py*1e9, str(a.material), a.refractive_index, *(a.eV_seq), a.lMax) +logging.info("Default file prefix: %s" % defaultprefix) + + +import numpy as np +import qpms +from qpms.cybspec import BaseSpec +from qpms.cytmatrices import CTMatrix, TMatrixGenerator +from qpms.qpms_c import Particle, pgsl_ignore_error +from qpms.cymaterials import EpsMu, EpsMuGenerator, LorentzDrudeModel, lorentz_drude +from qpms.cycommon import DebugFlags, dbgmsg_enable +from qpms import FinitePointGroup, ScatteringSystem, BesselType, eV, hbar +from qpms.cyunitcell import unitcell +#from qpms.symmetries import point_group_info # TODO +eh = eV/hbar + +# not used; TODO: +irrep_labels = {"B2''":"$B_2''$", + "B2'":"$B_2'$", + "A1''":"$A_1''$", + "A1'":"$A_1'$", + "A2''":"$A_2''$", + "B1''":"$B_1''$", + "A2'":"$A_2'$", + "B1'":"$B_1'$"} + +dbgmsg_enable(DebugFlags.INTEGRATION) + +a1 = ap.direct_basis[0] +a2 = ap.direct_basis[1] + +#Particle positions +orig_x = [0] +orig_y = [0] +orig_xy = np.stack(np.meshgrid(orig_x,orig_y),axis=-1) + +omegas = ap.omegas + +logging.info("%d frequencies from %g to %g eV" % (len(omegas), omegas[0]/eh, omegas[-1]/eh)) + +bspec = BaseSpec(lMax = a.lMax) +nelem = len(bspec) +# The parameters here should probably be changed (needs a better qpms_c.Particle implementation) +pp = Particle(orig_xy[0][0], tmgen=ap.tmgen, bspec=bspec) +par = [pp] + +u = unitcell(a1, a2, par, refractive_index=a.refractive_index) +eta = (np.pi / u.Area)**.5 + +wavenumbers = np.empty(omegas.shape) +SVs = np.empty(omegas.shape+(nelem,)) +beta = np.array([0.,0.]) +for i, omega in enumerate(omegas): + wavenumbers[i] = ap.background_epsmu.k(omega).real # Currently, ScatteringSystem does not "remember" frequency nor wavenumber + with pgsl_ignore_error(15): # avoid gsl crashing on underflow; maybe not needed + ImTW = u.evaluate_ImTW(eta, omega, beta) + SVs[i] = np.linalg.svd(ImTW, compute_uv = False) + +outfile = defaultprefix + ".npz" if a.output is None else a.output +np.savez(outfile, meta=vars(a), omegas=omegas, wavenumbers=wavenumbers, SVs=SVs, unitcell_area=u.Area + ) +logging.info("Saved to %s" % outfile) + + +if a.plot or (a.plot_out is not None): + import matplotlib + matplotlib.use('pdf') + from matplotlib import pyplot as plt + + fig = plt.figure() + ax = fig.add_subplot(111) + for i in range(a.singular_values): + ax.plot(omegas/eh, SVs[:,-1-i]) + ax.set_xlabel('$\hbar \omega / \mathrm{eV}$') + ax.set_ylabel('Singular values') + + plotfile = defaultprefix + ".pdf" if a.plot_out is None else a.plot_out + fig.savefig(plotfile) + +exit(0) + diff --git a/qpms/argproc.py b/qpms/argproc.py index 0bde35b..5f4ccf5 100644 --- a/qpms/argproc.py +++ b/qpms/argproc.py @@ -26,6 +26,7 @@ class ArgParser: 'rectlattice2d_periods': lambda ap: ap.add_argument("-p", "--period", type=float, nargs='+', required=True, help='square/rectangular lattice periods', metavar=('px','[py]')), 'rectlattice2d_counts': lambda ap: ap.add_argument("--size", type=int, nargs=2, required=True, help='rectangular array size (particle column, row count)', metavar=('NCOLS', 'NROWS')), 'single_frequency_eV': lambda ap: ap.add_argument("-f", "--eV", type=float, required=True, help='radiation angular frequency in eV'), + 'seq_frequency_eV': lambda ap: ap.add_argument("-f", "--eV-seq", type=float, nargs=3, required=True, help='uniform radiation angular frequency sequence in eV', metavar=('FIRST', 'INCREMENT', 'LAST')), 'single_material': lambda ap: ap.add_argument("-m", "--material", help='particle material (Au, Ag, ... for Lorentz-Drude or number for constant refractive index)', default='Au', required=True), 'single_radius': lambda ap: ap.add_argument("-r", "--radius", type=float, required=True, help='particle radius (sphere or cylinder)'), 'single_height': lambda ap: ap.add_argument("-H", "--height", type=float, help='cylindrical particle height; if not provided, particle is assumed to be spherical'), @@ -45,6 +46,7 @@ class ArgParser: 'single_particle': ("Single particle definition (shape [currently spherical or cylindrical]) and materials, incl. background)", ('background',), ('single_material', 'single_radius', 'single_height', 'single_lMax_extend'), ('_eval_single_tmgen',)), 'single_lMax': ("Single particle lMax definition", (), ('single_lMax',), ()), 'single_omega': ("Single angular frequency", (), ('single_frequency_eV',), ('_eval_single_omega',)), + 'omega_seq': ("Equidistant real frequency range", (), ('seq_frequency_eV',), ('_eval_omega_seq',)), 'lattice2d': ("Specification of a generic 2d lattice (spanned by the x,y axes)", (), ('lattice2d_basis',), ('_eval_lattice2d',)), 'rectlattice2d': ("Specification of a rectangular 2d lattice; conflicts with lattice2d", (), ('rectlattice2d_periods',), ('_eval_rectlattice2d',)), 'rectlattice2d_finite': ("Specification of a rectangular 2d lattice; conflicts with lattice2d", ('rectlattice2d',), ('rectlattice2d_counts',), ()), @@ -119,6 +121,13 @@ class ArgParser: from .constants import eV, hbar self.omega = self.args.eV * eV / hbar + def _eval_omega_seq(self): # feature: omega_seq + import numpy as np + from .constants import eV, hbar + start, step, stop = self.args.eV_seq + self.omegas = np.arange(start, stop, step) + self.omegas *= eV/hbar + def _eval_lattice2d(self): # feature: lattice2d l = len(self.args.basis_vectors) if l != 2: raise ValueError('Two basis vectors must be specified (have %d)' % l)