Beispiel #1
0
def dispersion_abtransition_gapbounds(lp, abvals=None):
    """

    Parameters
    ----------
    lp

    Returns
    -------

    """
    # prepare the values of inversion symmetry breaking
    if abvals is None:
        # abvals = np.arange(0, 2.4, 0.1)
        abvals = np.arange(0, 1.0, 0.025)

    meshfn = le.find_meshfn(lp)
    lat = lattice_class.Lattice(lp)
    lat.load(check=lp['check'])
    lp['ABDelta'] = np.max(abvals)
    mglat = MagneticGyroLattice(lat, lp)
    outname = dio.prepdir(meshfn) + 'dispersion_abtrans' + mglat.lp[
        'meshfn_exten'] + '_gapbounds'
    gapbounds_glob = glob.glob(outname + '.txt')
    if gapbounds_glob:
        # Load the text file and save the image
        gapbounds = np.loadtxt(outname + '.txt')
        plot_gapbounds(gapbounds[:, 0], gapbounds[:, 1:], outname + '.png', lp)
    else:
        # Compute the dispersions and save the image (occurs inside dispersion_abtransition
        omegas, kx, ky = dispersion_abtransition(lp,
                                                 save_plots=False,
                                                 return_omegas=True)
def dice_glat(lp, args):
    """make the OmK arrays to load when computing cherns for diced networks
        Example usage:
        python run_series.py -pro gyro_lattice_class -opts LT/hexagonal/-N/11/-shape/square/-dice_glat/-gridspacing/3.0 -var weakbond_val n1.0:0.1:0.05

    Parameters
    ----------
    lp
    args

    Returns
    -------

    """
    import lepm.line_segments as lsegs
    # Create a glat with bonds that are weakened in a gridlike fashion (bonds crossing gridlines are weak)
    meshfn = le.find_meshfn(lp)
    lp['meshfn'] = meshfn
    lat = lattice_class.Lattice(lp)
    lat.load()

    # Find where bonds cross gridlines
    gridspacing = args.gridspacing
    lp['OmKspec'] = 'gridlines{0:0.2f}'.format(gridspacing).replace('.', 'p') + \
                    'strong{0:0.3f}'.format(lp['Omk']).replace('.', 'p').replace('-', 'n') + \
                    'weak{0:0.3f}'.format(args.weakbond_val).replace('.', 'p').replace('-', 'n')
    maxval = max(np.max(np.abs(lat.xy[:, 0])), np.max(np.abs(lat.xy[:, 1]))) + 1
    gridright = np.arange(gridspacing, maxval, gridspacing)
    gridleft = -gridright
    gridvals = np.hstack((gridleft, 0, gridright))
    # Draw grid
    gridlinesH = np.array([[-maxval, gridv, maxval, gridv] for gridv in gridvals])
    gridlinesV = np.array([[gridv, -maxval, gridv, maxval] for gridv in gridvals])
    gridsegs = np.vstack((gridlinesH, gridlinesV))
    print 'np.shape(gridlines) = ', np.shape(gridsegs)
    # Make the bond linesegments
    xy = lat.xy
    bondsegs = np.array([[xy[b[0], 0], xy[b[0], 1], xy[b[1], 0], xy[b[1], 1]] for b in lat.BL])
    # get crossings
    print 'gridsegs = ', gridsegs
    does_intersect = lsegs.linesegs_intersect_linesegs(bondsegs, gridsegs)
    tmp_glat = GyroLattice(lat, lp)
    OmK = copy.deepcopy(tmp_glat.OmK)
    print 'Altering weak bonds --> ', args.weakbond_val
    for bond in lat.BL[does_intersect]:
        OmK[bond[0], np.where(lat.NL[bond[0]] == bond[1])] = args.weakbond_val
    glat = GyroLattice(lat, lp, OmK=OmK)
    glat.plot_OmK()
    if args.dice_eigval:
        glat.save_eigval_eigvect()
        glat.save_DOSmovie()
def localization(lp, args):
    """Load a periodic lattice from file, provide physics, and seek exponential localization of modes

    Example usage:
    python run_series.py -pro gyro_lattice_class -opts LT/hucentroid/-periodic/-NP/20/-localization/-save_eig -var AB 0.0:0.05:1.0
    python run_series.py -pro gyro_lattice_class -opts LT/hyperuniform/-periodic/-NP/50/-localization/-save_eig -var Vpin 0.1/0.5/1.0/2.0/4.0/6.0
    python run_series.py -pro gyro_lattice_class -opts LT/hyperuniform/-periodic/-NP/50/-DOSmovie/-save_eig -var Vpin 0.1/0.5/1.0/2.0/4.0/6.0

    """
    meshfn = le.find_meshfn(lp)
    lp['meshfn'] = meshfn
    lat = lattice_class.Lattice(lp)
    lat.load()
    extent = 2 * max(np.max(lat.xy[:, 0]), np.max(lat.xy[:, 1]))
    glat = GyroLattice(lat, lp)
    glat.save_localization(attribute=True, save_images=args.save_images, save_eigvect_eigval=args.save_eig)
    glat.plot_ill_dos(vmax=4. / extent, xlim=(0., 14.), ticks=[0, 2. / extent, 4. / extent],
                      cbar_ticklabels=[0, r'$2/L$', r'$4/L$'], cbar_labelpad=15)
Beispiel #4
0
    def ensure_all_gyro_lattices(self):
        """Ensure that all gyro lattices called for by self.meshfns are loaded.
        """
        # todo: add the ability to change glat.lp parameters (like Omk) at function call-- specify as list of lp's or as single lp
        print 'Ensuring all gyro lattices...'
        for ii in range(len(self.meshfns)):
            meshfn = self.meshfns[ii]
            if isinstance(meshfn, list):
                try:
                    meshfn = meshfn[0]
                except IndexError:
                    print 'self.meshfns = ', self.meshfns
                    print 'meshfn = ', meshfn
                    raise IndexError('list index out of range')
            print 'Ensuring ', meshfn
            try:
                self.gyro_lattices[ii]
                append_glat = False
            except IndexError:
                append_glat = True

            try:
                # Check if lp['meshfn'] of gyro_lattice matches meshfn
                self.gyro_lattices[ii].lp['meshfn']
                # print('Already have gyro_lattice defined for ii='+str(ii)+': ', test)
            except IndexError:
                lp = le.load_params(dio.prepdir(meshfn), 'lattice_params')
                lat = lattice_class.Lattice(lp=lp)
                lat.load(meshfn=meshfn)
                lp['Omk'] = -1.0
                lp['Omg'] = -1.0
                if append_glat:
                    self.gyro_lattices.append(
                        gyro_lattice_class.GyroLattice(lat, lp))
                else:
                    self.gyro_lattices[ii] = gyro_lattice_class.GyroLattice(
                        lat, lp)

                glat = gyro_lattice_class.GyroLattice(
                    lat, self.gyro_lattices[ii].lp)
                self.gyro_lattices[ii] = glat
 """
 # Collate botts for one lattice with a mgyro_lattice parameter that varies between instances of that lattice
 lp_master = copy.deepcopy(lp)
 paramV = sf.string_sequence_to_numpy_array(args.paramV, dtype=float)
 if args.lpparam == 'delta':
     # vary delta between lattices
     deltaV = np.arange(0.7, 1.31, 0.1)
     deltaV = np.hstack((0.667, deltaV))
     bmgcollcoll = BottMagneticGyroCollectionCollection()
     for delta in deltaV:
         lp = copy.deepcopy(lp_master)
         lp['delta_lattice'] = '{0:0.3f}'.format(delta)
         meshfn = le.find_meshfn(lp)
         lp['meshfn'] = meshfn
         print '\n\n\nlp[meshfn] = ', lp['meshfn']
         lat = lattice_class.Lattice(lp)
         lat.load()
         mgc = magnetic_gyro_collection.MagneticGyroCollection()
         lp_submaster = copy.deepcopy(lp)
         # Need only to add one single mgryo_lattice
         # for glatpval in paramV:
         #     lpii = copy.deepcopy(lp_submaster)
         #     lpii[args.glatparam] = glatpval
         #     mglat = magnetic_gyro_lattice_class.MagneticGyroLattice(lat, lpii)
         #     mglat.load()
         #     mgc.add_mgyro_lattice(mglat)
         lpii = copy.deepcopy(lp_submaster)
         lpii[args.glatparam] = paramV[0]
         mglat = magnetic_gyro_lattice_class.MagneticGyroLattice(lat, lpii)
         mglat.load()
         mgc.add_mgyro_lattice(mglat)
Beispiel #6
0
        'origin': origin,
        'NP_load': args.NP_load,
        'percolation_density': args.percolation_density,
        'thres': args.thres,
        'trimbound_thres': args.trimbound_thres,
        'aratio': args.aratio,
        'ABDelta': args.ABDelta,
        'spreading_time': args.spreading_time,
        'spreading_dt': args.spreading_dt,
        'kicksz': args.kicksz,
        'intparam': args.intparam,
    }

    #######################
    print 'LatticeTop = ', latticetop
    lattice = lattice_class.Lattice(lp)
    print '\nBuilding lattice...'
    lattice.build()
    print '\nSaving lattice...'
    lattice.save(skip_polygons=args.skip_polygons, check=lp['check'])

    if args.nice_plot:
        print 'Saving nice BW plot...'
        lattice.plot_BW_lat(meshfn=dio.prepdir(lattice.lp['meshfn']))

    infodir = lattice.lp['meshfn'] + '/'
    if args.bondL_histogram:
        print 'Creating bond length histogram...'
        lattice.bond_length_histogram(fig=None,
                                      ax=None,
                                      outdir=infodir,
        dmyk = 0
        for netconf in netconfv:
            print 'considering network configuration = ', netconf
            for pinconf in confv:
                lpnew = copy.deepcopy(lp)
                cpnew = copy.deepcopy(cp)

                lpnew['NH'] = int(np.sqrt(NN))
                lpnew['NV'] = int(np.sqrt(NN))
                lpnew['NP_load'] = NN
                lpnew['conf'] = netconf
                lpnew['pinconf'] = pinconf
                lpnew['V0_pin_gauss'] = vpin

                # cpnew['omegac'] = bcfns.gap_midpoints_honeycomb(delta * np.pi)

                lat = lattice_class.Lattice(lp=lpnew)

                try:
                    lat.load()
                except IOError:
                    lat.build()
                    lat.save(skip_polygons=True)

                nsitesii = len(lat.xy)
                print 'kk, ii, jj, dmyk = ', kk, ii, jj, dmyk
                print 'vpin->', vpin, ' NN->', NN

                glat = gyro_lattice_class.GyroLattice(lat, lp=lpnew)
                chern = kitaev_chern_class.KitaevChern(glat, cp=cpnew)
                chern.get_kitaev_chern(skip_paramsregs=True)
Beispiel #8
0
def dispersion_abtransition(lp,
                            save_plots=True,
                            return_omegas=False,
                            abvals=None,
                            fullspectrum=False):
    """

    Parameters
    ----------
    lp : dictionary
        lattice parameter dictionary
    return_omegas : bool
        Return a list of the frequencies in the dispersion
    abvals : n x 1 float array or list
        The Delta_AB values for each spectrum

    Returns
    -------

    """
    # prepare the values of inversion symmetry breaking
    if abvals is None:
        # abvals = np.arange(0, 2.4, 0.1)
        abvals = np.arange(0, 1.0, 0.025)

    # Prepare magnetic gyro lattice
    meshfn = le.find_meshfn(lp)
    lp['meshfn'] = meshfn
    lpmaster = copy.deepcopy(lp)
    # create a place to put images
    outdir = dio.prepdir(
        meshfn) + 'dispersion_abtransition_aol' + sf.float2pstr(
            lp['aoverl']) + '/'
    dio.ensure_dir(outdir)
    fs = 20

    if return_omegas:
        omegas_out = []
        kx_out = []
        ky_out = []

    # go through each ab and make the image
    ii = 0
    for ab in abvals:
        lp = copy.deepcopy(lpmaster)
        lp['ABDelta'] = ab
        lat = lattice_class.Lattice(lp)
        lat.load(check=lp['check'])
        glat = MagneticGyroLattice(lat, lp)
        # glat.get_eigval_eigvect(attribute=True)
        fig, ax = leplt.initialize_portrait(ax_pos=[0.12, 0.2, 0.76, 0.6])
        omegas, kx, ky = glat.infinite_dispersion(save=False,
                                                  nkxvals=50,
                                                  nkyvals=50,
                                                  outdir=outdir,
                                                  save_plot=False)
        if return_omegas:
            omegas_out.append(omegas)
            kx_out.append(kx)
            ky_out.append(ky)

        if save_plots:
            # plot and save it
            title = r'$\Delta_{AB} =$' + '{0:0.2f}'.format(ab)
            for jj in range(len(ky)):
                for kk in range(len(omegas[0, jj, :])):
                    ax.plot(kx,
                            omegas[:, jj, kk],
                            'k-',
                            lw=max(0.5, 30. / (len(kx) * len(ky))))

            ax.set_title(title, fontsize=fs)
            ax.set_xlabel(r'$k$ $[\langle \ell \rangle ^{-1}]$', fontsize=fs)
            ax.set_ylabel(r'$\omega$', fontsize=fs)
            for tick in ax.xaxis.get_major_ticks():
                tick.label.set_fontsize(fs)
            for tick in ax.yaxis.get_major_ticks():
                tick.label.set_fontsize(fs)
            if ii == 0:
                ylims = ax.get_ylim()

            if fullspectrum:
                ax.set_ylim(-ylims[1] * 1.2, ylims[1] * 1.2)
            else:
                ax.set_ylim(0., ylims[1] * 1.2)

            # Save it
            name = outdir + 'dispersion{0:04d}'.format(ii)
            plt.savefig(name + '.png', dpi=300)
            plt.close('all')

        ii += 1

    # Save a movie of the dispersions
    movname = dio.prepdir(
        meshfn) + 'dispersion_abtrans' + glat.lp['meshfn_exten']
    if save_plots:
        # Turn images into a movie
        imgname = outdir + 'dispersion'
        lemov.make_movie(imgname,
                         movname,
                         indexsz='04',
                         framerate=4,
                         imgdir=outdir,
                         rm_images=True,
                         save_into_subdir=True)

    if return_omegas:
        # Save the gap bounds and return the arrays of kx, ky, and omega
        omegas, kx, ky = np.array(omegas_out), np.array(kx_out), np.array(
            ky_out)
        # Get gap bounds:
        botmin = []
        botmax = []
        topmin = []
        topmax = []
        for band in omegas:
            botmin.append(np.min(band[:, :, 2]))
            botmax.append(np.max(band[:, :, 2]))
            topmin.append(np.min(band[:, :, 3]))
            topmax.append(np.max(band[:, :, 3]))

        botmin = np.array(botmin)
        botmax = np.array(botmax)
        topmin = np.array(topmin)
        topmax = np.array(topmax)
        gapbounds = np.dstack((abvals, botmin, botmax, topmin, topmax))[0]
        outfn = movname + '_gapbounds.txt'
        print 'saving to ', outfn
        header = 'Delta_AB, band min frequency, band max frequency, band 2 min, band 2 max...'
        np.savetxt(outfn, gapbounds, header=header)
        plot_gapbounds(abvals, gapbounds, movname + '_gapbounds.png', lp)
        return omegas, kx, ky
    else:
        return None
Beispiel #9
0
        'theta_lattice': args.theta,
        'eta': args.eta,
        'huID': args.hyperuniform_number,
        'z': args.target_z,
        'Ndefects': args.Ndefects,
        'Bvec': args.Bvec,
        'dislocxy': (args.dislocation_xy.split('/')),
        'cutz_method': args.cutz_method,
        'origin': np.array([0., 0.]),
        'source': 'hexner',
        'percolation_density': args.percolation_density,
    }

    lat = lattice_class.Lattice(lp=lp,
                                xy=np.array([]),
                                NL=np.array([]),
                                KL=np.array([]),
                                BL=np.array([]),
                                polygons=None)
    lat.load()

    # xyload, NLload, KLload, meshfn = le.find_lattice(lp)
    # xy = xyload.astype(float)
    # NL = NLload.astype(int)
    # KL = KLload.astype(int)
    # BL = le.NL2BL(NL, KL)
    xy = lat.xy
    NL = lat.NL
    KL = lat.KL
    BL = lat.BL
    meshfn = lat.lp['meshfn']
Beispiel #10
0
def twistbcs(lp):
    """Load a periodic lattice from file, twist the BCs by phases theta_twist and phi_twist with vals finely spaced
    between 0 and 2pi. Then compute the berry curvature associated with |alpha(theta, phi)>

    Example usage:
    python haldane_lattice_class.py -twistbcs -N 3 -LT hexagonal -shape square -periodic

    Parameters
    ----------
    lp

    Returns
    -------

    """
    lp['periodicBC'] = True
    meshfn = le.find_meshfn(lp)
    lp['meshfn'] = meshfn
    lat = lattice_class.Lattice(lp)
    lat.load()
    # Make a big array of the eigvals: N x N x thetav x phiv
    # thetav = np.arange(0., 2. * np.pi, 0.5)
    # phiv = np.arange(0., 2. * np.pi, 0.5)

    # First just test for two values of theta and two of phi
    thetav = [0., 0.01]
    phiv = [0., 0.01]
    eigvects = {}
    ii = 0
    for theta in thetav:
        eigvects[ii] = {}
        jj = 0
        for phi in phiv:
            lpnew = copy.deepcopy(lp)
            lpnew['theta_twist'] = theta
            lpnew['phi_twist'] = phi
            hlat = HaldaneLattice(lat, lpnew)
            ev, evec = hlat.get_eigval_eigvect(attribute=True)
            if ii == 0 and jj == 0:
                eigval = copy.deepcopy(ev)
                ill = hlat.get_ill()
            eigvects[ii][jj] = evec
            jj += 1
        ii += 1

    # Dot the derivs of eigvects together
    # Ensure that there is a nonzero-amplitude wannier with matching phase
    dtheta = eigvects[1][0] - eigvects[0][0]
    dphi = eigvects[0][1] - eigvects[0][0]

    thetamov_fn = dio.prepdir(
        hlat.lp['meshfn']
    ) + 'twistbc_theta' + hlat.lp['meshfn_exten'] + '_dos_ev.mov'
    if not glob.glob(thetamov_fn):
        # Plot differences
        fig, DOS_ax, eig_ax = leplt.initialize_eigvect_DOS_header_plot(
            ev,
            hlat.lattice.xy,
            sim_type='haldane',
            colorV=ill,
            colormap='viridis',
            linewidth=0,
            cax_label=r'$\xi^{-1}$',
            cbar_nticks=2,
            xlabel_pad=10,
            ylabel_pad=10,
            cbar_tickfmt='%0.3f')
        DOS_ax.set_title(r'$\partial_\phi \psi$')
        hlat.lattice.plot_BW_lat(fig=fig,
                                 ax=eig_ax,
                                 save=False,
                                 close=False,
                                 axis_off=False,
                                 title='')
        outdir = dio.prepdir(
            hlat.lp['meshfn']
        ) + 'twistbc_theta' + hlat.lp['meshfn_exten'] + '/'
        dio.ensure_dir(outdir)
        for ii in range(len(ev)):
            fig, [scat_fg, scat_fg2, p, f_mark, lines_12_st], cw_ccw = \
                hlatpfns.construct_haldane_eigvect_DOS_plot(hlat.lattice.xy, fig, DOS_ax, eig_ax, eigval,
                                                            dtheta, ii, hlat.lattice.NL, hlat.lattice.KL,
                                                            marker_num=0, color_scheme='default', normalization=1.)
            print 'saving theta ', ii
            plt.savefig(outdir + 'dos_ev' + '{0:05}'.format(ii) + '.png')
            scat_fg.remove()
            scat_fg2.remove()
            p.remove()
            f_mark.remove()
            lines_12_st.remove()

        plt.close('all')

        imgname = outdir + 'dos_ev'
        movname = dio.prepdir(
            hlat.lp['meshfn']
        ) + 'twistbc_theta' + hlat.lp['meshfn_exten'] + '_dos_ev'
        lemov.make_movie(imgname, movname, rm_images=False)

    phimov_fn = dio.prepdir(
        hlat.lp['meshfn']
    ) + 'twistbc_phi' + hlat.lp['meshfn_exten'] + '_dos_ev.mov'
    if not glob.glob(phimov_fn):
        fig, DOS_ax, eig_ax = leplt.initialize_eigvect_DOS_header_plot(
            ev,
            hlat.lattice.xy,
            sim_type='haldane',
            colorV=ill,
            colormap='viridis',
            linewidth=0,
            cax_label=r'$\xi^{-1}$',
            cbar_nticks=2,
            xlabel_pad=10,
            ylabel_pad=10,
            cbar_tickfmt='%0.3f')
        DOS_ax.set_title(r'$\partial_\phi \psi$')
        outdir = dio.prepdir(
            hlat.lp['meshfn']) + 'twistbc_phi' + hlat.lp['meshfn_exten'] + '/'
        dio.ensure_dir(outdir)
        for ii in range(len(ev)):
            fig, [scat_fg, scat_fg2, p, f_mark, lines_12_st], cw_ccw = \
                hlatpfns.construct_haldane_eigvect_DOS_plot(hlat.lattice.xy, fig, DOS_ax, eig_ax, eigval,
                                                            dphi, ii, hlat.lattice.NL, hlat.lattice.KL,
                                                            marker_num=0, color_scheme='default', normalization=1.)
            print 'saving phi ', ii
            plt.savefig(outdir + 'dos_ev' + '{0:05}'.format(ii) + '.png')
            scat_fg.remove()
            scat_fg2.remove()
            p.remove()
            f_mark.remove()
            lines_12_st.remove()

        plt.close('all')

        imgname = outdir + 'dos_ev'
        movname = dio.prepdir(
            hlat.lp['meshfn']
        ) + 'twistbc_phi' + hlat.lp['meshfn_exten'] + '_dos_ev'
        lemov.make_movie(imgname, movname, rm_images=False)

    # Check
    # print 'shape(dtheta) = ', np.shape(dtheta)
    # print 'shape(dphi) = ', np.shape(dphi)
    # le.plot_complex_matrix(dtheta, show=True, name='dtheta')
    # le.plot_complex_matrix(dphi, show=True, name='dphi')

    fig, ax = leplt.initialize_nxmpanel_fig(4, 1, wsfrac=0.6, x0frac=0.3)
    # < dphi | dtheta >
    dpdt = np.einsum('ij...,ij...->i...', dtheta, dphi.conj())
    # < dtheta | dphi >
    dtdp = np.einsum('ij...,ij...->i...', dphi, dtheta.conj())
    print 'dtdp = ', dtdp
    ax[0].plot(np.arange(len(dtdp)), dtdp, '-')
    ax[1].plot(np.arange(len(dpdt)), dpdt, '-')
    hc = 2. * np.pi * 1j * (dpdt - dtdp)
    ax[2].plot(np.arange(len(lat.xy)), hc, '.-')
    # Plot cumulative sum
    sumhc = np.cumsum(hc)
    ax[3].plot(np.arange(len(lat.xy)), sumhc, '.-')

    ax[2].set_xlabel(r'Eigvect number')
    ax[0].set_ylabel(
        r'$\langle \partial_{\theta} \alpha_i | \partial_{\phi} \alpha_i \rangle$'
    )
    ax[2].set_xlabel(r'Eigvect number')
    ax[1].set_ylabel(
        r'$\langle \partial_{\phi} \alpha_i | \partial_{\theta} \alpha_i \rangle$'
    )
    ax[2].set_xlabel(r'Eigvect number')
    ax[2].set_ylabel(r'$c_H$')
    ax[3].set_xlabel(r'Eigvect number')
    ax[3].set_ylabel(r'$\sum_{E_\alpha < E_c} c_H$')
    outdir = '/Users/npmitchell/Dropbox/Soft_Matter/GPU/twistbc_test/'
    dio.ensure_dir(outdir)
    print 'saving ', outdir + 'test' + hlat.lp['meshfn_exten'] + '.png'
    plt.savefig(outdir + 'test' + hlat.lp['meshfn_exten'] + '.png')

    # ### Now do the same thing but with different values of theta, phi
    # First just test for two values of theta and two of phi
    thetav = [1., 1.01]
    phiv = [1., 1.01]
    eigvects = {}
    ii = 0
    for theta in thetav:
        eigvects[ii] = {}
        jj = 0
        for phi in phiv:
            lpnew = copy.deepcopy(lp)
            lpnew['theta_twist'] = theta
            lpnew['phi_twist'] = phi
            hlat = HaldaneLattice(lat, lpnew)
            ev, evec = hlat.get_eigval_eigvect(attribute=True)
            if ii == 0 and jj == 0:
                eigval = copy.deepcopy(ev)
                ill = hlat.get_ill()
            eigvects[ii][jj] = evec
            jj += 1
        ii += 1

    # Dot the derivs of eigvects together
    dtheta = eigvects[1][0] - eigvects[0][0]
    dphi = eigvects[0][1] - eigvects[0][0]

    # Plot differences
    thetamov_fn = dio.prepdir(
        hlat.lp['meshfn']
    ) + 'twistbc_theta' + hlat.lp['meshfn_exten'] + '_dos_ev.mov'
    if not glob.glob(thetamov_fn):
        fig, DOS_ax, eig_ax = leplt.initialize_eigvect_DOS_header_plot(
            ev,
            hlat.lattice.xy,
            sim_type='haldane',
            colorV=ill,
            colormap='viridis',
            linewidth=0,
            cax_label=r'$\xi^{-1}$',
            cbar_nticks=2,
            xlabel_pad=10,
            ylabel_pad=10,
            cbar_tickfmt='%0.3f')
        DOS_ax.set_title(r'$\partial_\theta \psi$')
        hlat.lattice.plot_BW_lat(fig=fig,
                                 ax=eig_ax,
                                 save=False,
                                 close=False,
                                 axis_off=False,
                                 title='')
        outdir = dio.prepdir(
            hlat.lp['meshfn']
        ) + 'twistbc_theta' + hlat.lp['meshfn_exten'] + '/'
        dio.ensure_dir(outdir)
        for ii in range(len(ev)):
            fig, [scat_fg, scat_fg2, p, f_mark, lines_12_st], cw_ccw = \
                hlatpfns.construct_haldane_eigvect_DOS_plot(hlat.lattice.xy, fig, DOS_ax, eig_ax, eigval,
                                                            dtheta, ii, hlat.lattice.NL, hlat.lattice.KL,
                                                            marker_num=0, color_scheme='default', normalization=1.)
            print 'saving theta ', ii
            plt.savefig(outdir + 'dos_ev' + '{0:05}'.format(ii) + '.png')
            scat_fg.remove()
            scat_fg2.remove()
            p.remove()
            f_mark.remove()
            lines_12_st.remove()

        plt.close('all')

        imgname = outdir + 'dos_ev'
        movname = dio.prepdir(
            hlat.lp['meshfn']
        ) + 'twistbc_theta' + hlat.lp['meshfn_exten'] + '_dos_ev'
        lemov.make_movie(imgname, movname, rm_images=False)

    # Now do phi
    phimov_fn = dio.prepdir(
        hlat.lp['meshfn']
    ) + 'twistbc_phi' + hlat.lp['meshfn_exten'] + '_dos_ev.mov'
    if not glob.glob(phimov_fn):
        fig, DOS_ax, eig_ax = leplt.initialize_eigvect_DOS_header_plot(
            ev,
            hlat.lattice.xy,
            sim_type='haldane',
            colorV=ill,
            colormap='viridis',
            linewidth=0,
            cax_label=r'$\xi^{-1}$',
            cbar_nticks=2,
            xlabel_pad=10,
            ylabel_pad=10,
            cbar_tickfmt='%0.3f')
        DOS_ax.set_title(r'$\partial_\phi \psi$')
        hlat.lattice.plot_BW_lat(fig=fig,
                                 ax=eig_ax,
                                 save=False,
                                 close=False,
                                 axis_off=False,
                                 title='')
        outdir = dio.prepdir(
            hlat.lp['meshfn']) + 'twistbc_phi' + hlat.lp['meshfn_exten'] + '/'
        dio.ensure_dir(outdir)
        for ii in range(len(ev)):
            fig, [scat_fg, scat_fg2, p, f_mark, lines_12_st], cw_ccw = \
                hlatpfns.construct_haldane_eigvect_DOS_plot(hlat.lattice.xy, fig, DOS_ax, eig_ax, eigval,
                                                            dphi, ii, hlat.lattice.NL, hlat.lattice.KL,
                                                            marker_num=0, color_scheme='default', normalization=1.)
            print 'saving phi ', ii
            plt.savefig(outdir + 'dos_ev' + '{0:05}'.format(ii) + '.png')
            scat_fg.remove()
            scat_fg2.remove()
            p.remove()
            f_mark.remove()
            lines_12_st.remove()

        plt.close('all')

        imgname = outdir + 'dos_ev'
        movname = dio.prepdir(
            hlat.lp['meshfn']
        ) + 'twistbc_phi' + hlat.lp['meshfn_exten'] + '_dos_ev'
        lemov.make_movie(imgname, movname, rm_images=False)

    # Check
    # print 'shape(dtheta) = ', np.shape(dtheta)
    # print 'shape(dphi) = ', np.shape(dphi)
    # le.plot_complex_matrix(dtheta, show=True, name='dtheta')
    # le.plot_complex_matrix(dphi, show=True, name='dphi')

    fig, ax = leplt.initialize_nxmpanel_fig(4, 1, wsfrac=0.6, x0frac=0.3)
    # < dphi | dtheta >
    dpdt = np.einsum('ij...,ij...->i...', dtheta, dphi.conj())
    # < dtheta | dphi >
    dtdp = np.einsum('ij...,ij...->i...', dphi, dtheta.conj())
    print 'dtdp = ', dtdp
    ax[0].plot(np.arange(len(dtdp)), dtdp, '-')
    ax[1].plot(np.arange(len(dpdt)), dpdt, '-')
    hc = 2. * np.pi * 1j * (dpdt - dtdp)
    ax[2].plot(np.arange(len(lat.xy)), hc, '.-')
    # Plot cumulative sum
    sumhc = np.cumsum(hc)
    ax[3].plot(np.arange(len(lat.xy)), sumhc, '.-')

    ax[2].set_xlabel(r'Eigvect number')
    ax[0].set_ylabel(
        r'$\langle \partial_{\theta} \alpha_i | \partial_{\phi} \alpha_i \rangle$'
    )
    ax[2].set_xlabel(r'Eigvect number')
    ax[1].set_ylabel(
        r'$\langle \partial_{\phi} \alpha_i | \partial_{\theta} \alpha_i \rangle$'
    )
    ax[2].set_xlabel(r'Eigvect number')
    ax[2].set_ylabel(r'$c_H$')
    ax[3].set_xlabel(r'Eigvect number')
    ax[3].set_ylabel(r'$\sum_{E_\alpha < E_c} c_H$')
    outdir = '/Users/npmitchell/Dropbox/Soft_Matter/GPU/twistbc_test/'
    dio.ensure_dir(outdir)
    plt.savefig(outdir + 'test' + hlat.lp['meshfn_exten'] + '_theta1p0.png')

    sys.exit()
    ########################################
    # Now test for more theta values, more phi values
    thetav = np.arange(0., 0.14, 0.1)
    phiv = np.arange(0., 0.14, 0.1)
    eigvects = np.zeros((len(lat.xy), len(lat.xy), len(thetav), len(phiv)),
                        dtype=complex)
    ii = 0
    for theta in thetav:
        jj = 0
        for phi in phiv:
            lpnew = copy.deepcopy(lp)
            lpnew['theta_twist'] = theta
            lpnew['phi_twist'] = phi
            hlat = HaldaneLattice(lat, lpnew)
            ev, evec = hlat.get_eigval_eigvect()
            eigvects[:, :, ii, jj] = evec
            jj += 1
        ii += 1

    # Dot the derivs of eigvects together
    print 'eigvects = ', eigvects
    dtheta = np.diff(eigvects, axis=2)
    dphi = np.diff(eigvects, axis=3)
    print 'dtheta = ', dtheta

    print 'shape(dtheta) = ', np.shape(dtheta)
    print 'shape(dphi) = ', np.shape(dphi)
    le.plot_complex_matrix(dtheta[:, :, 0, 0], show=True)
    le.plot_complex_matrix(dphi[:, :, 0, 0], show=True)

    dtheta = dtheta[:, :, :, 0:np.shape(dtheta)[3] - 1]
    dphi = dphi[:, :, 0:np.shape(dphi)[2] - 1, :]
    print 'shape(dtheta) = ', np.shape(dtheta)
    print 'shape(dphi) = ', np.shape(dphi)

    for ii in range(np.shape(dtheta)[-1]):
        le.plot_complex_matrix(dtheta[:, :, ii, 0], show=True)

    fig, ax = leplt.initialize_nxmpanel_fig(3, 1)
    for ii in range(np.shape(dphi)[-1]):
        for jj in range(np.shape(dphi)[-1]):
            # < dphi | dtheta >
            dpdt = np.dot(dtheta[:, :, ii, jj], dphi[:, :, ii, jj].conj().T)
            # < dtheta | dphi >
            dtdp = np.dot(dphi[:, :, ii, jj], dtheta[:, :, ii, jj].conj().T)
            print 'np.shape(dpdt) = ', np.shape(dpdt)
            print 'np.shape(dtdp) = ', np.shape(dtdp)
            ax[0].plot(np.arange(len(dtdp)), dtdp, '-')
            ax[1].plot(np.arange(len(dpdt)), dpdt, '-')

            hc = 2. * np.pi * 1j * (dpdt - dtdp)
            ax[2].plot(np.arange(len(lat.xy)), hc, '.-')

    ax[0].set_xlabel(r'$\theta$')
    ax[0].set_ylabel(
        r'$\langle \partial_{\theta} \alpha_i | \partial_{\phi} \alpha_i \rangle$'
    )
    ax[1].set_xlabel(r'$\phi$')
    ax[1].set_ylabel(
        r'$\langle \partial_{\phi} \alpha_i | \partial_{\theta} \alpha_i \rangle$'
    )
    ax[2].set_xlabel(r'Eigvect number')
    ax[2].set_ylabel(r'$\partial_{\phi} \alpha_i$')
    plt.show()
def polygon_phases_tune_junction(lp, args, nmode=None):
    """Plot the phase differences ccw around each polygon in the network for many glats as junction coupling
    is increased. Do this for the nth mode as the scaling parameter evolves
    (typically the bond strength between particles that are nearer than some threshold).

    Example usage:
    python gyro_lattice_class.py -polygon_phases_tune_junction -LT hexjunction2triads -N 1 -OmKspec union0p000in0p100 -alph 0.1 -periodic
    python gyro_lattice_class.py -polygon_phases_tune_junction -LT spindle -N 2 -OmKspec union0p000in0p100 -alph 0.0 -periodic -aratio 0.1
    python gyro_lattice_class.py -polygon_phases_tune_junction -LT spindle -N 4 -OmKspec union0p000in0p100 -alph 0.0 -periodic -aratio 0.1

    # for making lattices
    python ./build/make_lattice.py -LT spindle -N 4 -periodic -skip_gyroDOS -aratio 0.1

    Parameters
    ----------
    lp
    args
    nmode : int, int list, or None

    Returns
    -------

    """
    cmap = lecmaps.ensure_cmap('bbr0')
    nkvals = 50
    if lp['LatticeTop'] in ['hexjunctiontriad', 'spindle', 'hexjunction2triads']:
        kvals = -np.unique(np.round(np.logspace(-1, 1., nkvals), 2))  # [::-1]
        dist_thres = lp['OmKspec'].split('union')[-1].split('in')[-1]
        lpmaster = copy.deepcopy(lp)
        lat = lattice_class.Lattice(lp)
        lat.load()
        if nmode is None:
            todo = np.arange(len(lat.xy[:, 0]))
        elif type(nmode) == int:
            todo = [nmode]
        else:
            todo = nmode

        ##########################################################################
        # First collect eigenvalue flow
        eigvals, first = [], True
        for (kval, dmyi) in zip(kvals, np.arange(len(kvals))):
            lp = copy.deepcopy(lpmaster)
            lp['OmKspec'] = 'union' + sf.float2pstr(kval, ndigits=3) + 'in' + dist_thres
            # lat = lattice_class.Lattice(lp)
            glat = GyroLattice(lat, lp)
            eigval, eigvect = glat.eig_vals_vects(attribute=True)
            if first:
                eigvals = np.zeros((len(kvals), len(eigval)), dtype=float)

            eigvals[dmyi, :] = np.imag(eigval)
            first = False

        ##########################################################################

        lp = copy.deepcopy(lpmaster)
        glat = GyroLattice(lat, lp)
        # add meshfn without OmKspecunion part
        mfe = glat.lp['meshfn_exten']
        if mfe[0:13] == '_OmKspecunion':
            meshfnextenstr = mfe.split(mfe.split('_')[1])[-1]
        else:
            raise RuntimeError('Handle this case here -- should be easy: split meshfn_exten to pop OmKspec out')

        for ii in todo[::-1]:
            modefn = dio.prepdir(lat.lp['meshfn']) + 'glat_mode_phases_scaling_tune_junction_mode{0:05d}'.format(ii) +\
                     meshfnextenstr + '_nkvals{0:04}'.format(nkvals) + '.mov'
            globmodefn = glob.glob(modefn)
            if not globmodefn:
                modedir = dio.prepdir(lat.lp['meshfn']) + 'glat_mode_phases_tune_junction_mode{0:05d}'.format(ii) + \
                          meshfnextenstr + '/'
                dio.ensure_dir(modedir)
                previous_ev = None
                first = True
                dmyi = 0
                for kval in kvals:
                    lp = copy.deepcopy(lpmaster)
                    lp['OmKspec'] = 'union' + sf.float2pstr(kval, ndigits=3) + 'in' + dist_thres
                    # lat = lattice_class.Lattice(lp)
                    glat = GyroLattice(lat, lp)
                    eigval, eigvect = glat.eig_vals_vects(attribute=True)

                    # plot the nth mode
                    # fig, DOS_ax, eax = leplt.initialize_eigvect_DOS_header_plot(eigval, glat.lattice.xy,
                    #                                                             sim_type='gyro', cbar_nticks=2,
                    #                                                             cbar_tickfmt='%0.3f')
                    fig, dos_ax, eax, ax1, cbar_ax = \
                        leplt.initialize_eigvect_DOS_header_twinplot(eigval, glat.lattice.xy, sim_type='gyro',
                                                                     ax0_pos=[0.0, 0.10, 0.45, 0.55],
                                                                     ax1_pos=[0.65, 0.15, 0.3, 0.60],
                                                                     header_pos=[0.1, 0.78, 0.4, 0.20],
                                                                     xlabel_pad=8, fontsize=8)

                    cax = plt.axes([0.455, 0.10, 0.02, 0.55])

                    # Get the theta that minimizes the difference between the present and previous eigenvalue
                    # IN ORDER TO CONNECT MODES PROPERLY
                    if previous_ev is not None:
                        realxy = np.real(previous_ev)
                        thetas, eigvects = gdh.phase_fix_nth_gyro(glat.eigvect, ngyro=0, basis='XY')
                        # only look at neighboring modes
                        # (presumes sufficient resolution to disallow simultaneous crossings)
                        mmin = max(modenum - 2, 0)
                        mmax = min(modenum + 2, len(eigvects))
                        modenum = gdh.phase_difference_minimum(eigvects[mmin:mmax], realxy, basis='XY')
                        modenum += mmin
                        # print 'thetas = ', thetas
                        # if theta < 1e-9:
                        #     print 'problem with theta'
                        #     sys.exit()
                    else:
                        thetas, eigvects = gdh.phase_fix_nth_gyro(glat.eigvect, ngyro=0, basis='XY')
                        modenum = ii

                    # Plot the lattice with bonds
                    glat.lattice.plot_BW_lat(fig=fig, ax=eax, save=False, close=False, axis_off=False, title='')
                    # plot excitation
                    fig, [scat_fg, scat_fg2, pp, f_mark, lines_12_st], cw_ccw = \
                        leplt.construct_eigvect_DOS_plot(glat.lattice.xy, fig, dos_ax, eax, eigval, eigvects,
                                                         modenum, 'gyro', glat.lattice.NL, glat.lattice.KL,
                                                         marker_num=0, color_scheme='default', sub_lattice=-1,
                                                         amplify=1., title='')
                    # Plot the polygons colored by phase
                    polys = glat.lattice.get_polygons()
                    patches, colors = [], []
                    for poly in polys:
                        addv = np.array([0., 0.])
                        # build up positions, taking care of periodic boundaries
                        xys = np.zeros_like(glat.lattice.xy[poly], dtype=float)
                        xys[0] = glat.lattice.xy[poly[0]]
                        for (site, qq) in zip(poly[1:], range(len(poly) - 1)):
                            if latfns.bond_is_periodic(poly[qq], site, glat.lattice.BL):
                                toadd = latfns.get_periodic_vector(poly[qq], site,
                                                                   glat.lattice.PVx, glat.lattice.PVy,
                                                                   glat.lattice.NL, glat.lattice.KL)
                                if np.shape(toadd)[0] > 1:
                                    raise RuntimeError('Handle the case of multiple periodic bonds between ii jj here')
                                else:
                                    addv += toadd[0]
                            xys[qq + 1] = glat.lattice.xy[site] + addv
                            print 'site, poly[qq - 1] = ', (site, poly[qq])
                            print 'addv = ', addv

                        xys = np.array(xys)
                        polygon = Polygon(xys, True)
                        patches.append(polygon)

                        # Check the polygon
                        # plt.close('all')
                        # plt.plot(xys[:, 0], xys[:, 1], 'b-')
                        # plt.show()

                        # Get mean phase difference in this polygon
                        # Use weighted arithmetic mean of (cos(angle), sin(angle)), then take the arctangent.
                        yinds = 2 * np.array(poly) + 1
                        xinds = 2 * np.array(poly)
                        weights = glatfns.calc_magevecs_full(eigvect[modenum])
                        # To take mean, follow
                        # https://en.wikipedia.org/wiki/Mean_of_circular_quantities#Mean_of_angles
                        # with weights from
                        # https://en.wikipedia.org/wiki/Weighted_arithmetic_mean#Mathematical_definition
                        # First take differences in angles
                        phis = np.arctan2(np.real(eigvects[modenum, yinds]), np.real(eigvects[modenum, xinds]))
                        print 'phis = ', phis
                        phis = np.mod(phis, np.pi * 2)
                        print 'phis = ', phis
                        # Now convert to vectors, take mean of both x and y components. Then grab atan2(y,x) of result.
                        xx, yy = np.mean(np.cos(np.diff(phis))), np.mean(np.sin(np.diff(phis)))
                        dphi = np.arctan2(yy, xx)
                        print 'dphi = ', dphi
                        colors.append(dphi)

                    # sys.exit()
                    pp = PatchCollection(patches, alpha=0.4, cmap=cmap)
                    pp.set_array(np.array(colors))
                    eax.add_collection(pp)
                    pp.set_clim([-np.pi, np.pi])
                    cbar = fig.colorbar(pp, cax=cax)

                    # Store this current eigvector as 'previous_ev'
                    previous_ev = eigvects[modenum]

                    # Plot where in evolution we are tracking
                    ngyros = int(np.shape(eigvals)[1] * 0.5)
                    halfev = eigvals[:, ngyros:]
                    for row in halfev.T:
                        ax1.loglog(np.abs(kvals), row, 'b-')

                    trackmark = ax1.plot(np.abs(kval), np.abs(np.imag(eigval))[modenum], 'ro')
                    ax1.set_xlabel(r"vertex coupling, $\Omega_k'$")
                    ax1.set_ylabel(r"frequency, $\omega$")
                    eax.xaxis.set_ticks([])
                    eax.yaxis.set_ticks([])
                    cbar.set_ticks([-np.pi, 0, np.pi])
                    cbar.set_ticklabels([r'-$\pi$', 0, r'$\pi$'])
                    cbar.set_label(r'phase, $\Delta \phi$')

                    dos_ax.set_xlim(xmin=0)
                    plt.savefig(modedir + 'DOS_' + '{0:05}'.format(dmyi) + '.png', dpi=200)

                    # remove plotted excitation
                    scat_fg.remove()
                    scat_fg2.remove()
                    pp.remove()
                    if f_mark is not None:
                        f_mark.remove()
                    lines_12_st.remove()
                    eax.cla()

                    dmyi += 1
                    first = False

                # Make movie
                imgname = modedir + 'DOS_'
                movname = modedir[:-1] + '_nkvals{0:04}'.format(nkvals)
                lemov.make_movie(imgname, movname, indexsz='05', framerate=5, rm_images=True, save_into_subdir=True,
                                 imgdir=modedir)
def mode_scaling_tune_junction(lp, args, nmode=None):
    """First plot scaling for all modes as junction coupling is increased. Then, plot the nth mode as the
    scaling parameter evolves (typically the bond strength between particles that are nearer than some threshold).

    Example usage:
    python gyro_lattice_class.py -mode_scaling_tune_junction -LT hexjunction2triads -N 1 -OmKspec union0p000in0p100 -alph 0.1 -periodic
    python gyro_lattice_class.py -mode_scaling_tune_junction -LT spindle -N 2 -OmKspec union0p000in0p100 -alph 0.0 -periodic -aratio 0.1
    python gyro_lattice_class.py -mode_scaling_tune_junction -LT spindle -N 4 -OmKspec union0p000in0p100 -alph 0.0 -periodic -aratio 0.1

    python ./build/make_lattice.py -LT spindle -N 1 -periodic -check -skip_polygons -skip_gyroDOS -aratio 0.1
    python ./build/make_lattice.py -LT spindle -N 2 -periodic -check -skip_polygons -skip_gyroDOS -aratio 0.1
    python ./build/make_lattice.py -LT spindle -N 4 -periodic -skip_polygons -skip_gyroDOS -aratio 0.1
    python ./build/make_lattice.py -LT spindle -N 6 -periodic -skip_polygons -skip_gyroDOS -aratio 0.1 -check
    python ./build/make_lattice.py -LT spindle -N 8 -periodic -skip_polygons -skip_gyroDOS -aratio 0.1 -check

    Parameters
    ----------
    lp
    args
    nmode : int, int list, or None

    Returns
    -------

    """
    nkvals = 50
    if lp['LatticeTop'] in ['hexjunctiontriad', 'spindle', 'hexjunction2triads']:
        kvals = -np.unique(np.round(np.logspace(-1, 1., nkvals), 2))  # [::-1]
        dist_thres = lp['OmKspec'].split('union')[-1].split('in')[-1]
        lpmaster = copy.deepcopy(lp)
        lat = lattice_class.Lattice(lp)
        lat.load()
        if nmode is None:
            todo = np.arange(len(lat.xy[:, 0]))
        elif type(nmode) == int:
            todo = [nmode]
        else:
            todo = nmode

        ##########################################################################
        outfn = dio.prepdir(lat.lp['meshfn']) + 'glat_eigval_scaling_tune_junction'
        eigvals, first = [], True
        for (kval, dmyi) in zip(kvals, np.arange(len(kvals))):
            lp = copy.deepcopy(lpmaster)
            lp['OmKspec'] = 'union' + sf.float2pstr(kval, ndigits=3) + 'in' + dist_thres
            # lat = lattice_class.Lattice(lp)
            glat = GyroLattice(lat, lp)
            eigval, eigvect = glat.eig_vals_vects(attribute=True)
            if first:
                eigvals = np.zeros((len(kvals), len(eigval)), dtype=float)

            eigvals[dmyi, :] = np.imag(eigval)
            first = False

        # Plot flow of modes
        print 'eigvals = ', eigvals
        fig, axes = leplt.initialize_2panel_centy(Wfig=90, Hfig=65, x0frac=0.17, wsfrac=0.38)
        ax, ax1 = axes[0], axes[1]
        ymax = np.max(eigvals, axis=1)
        for kk in range(int(0.5 * len(eigvals[0])), len(eigvals[0])):
            ydat = eigvals[:, kk]
            ax.loglog(np.abs(kvals), ydat, 'b-')

        ax.set_ylim(ymin=0.1)
        ax.set_ylabel('frequency, $\omega$')
        ax.set_xlabel("coupling, $\Omega_k'$")
        if lp['LatticeTop'] == 'hexjunction2triads':
            ax.text(0.5, 0.9, 'Spectrum formation \n in double honeycomb junction',
                    ha='center', va='center', transform=fig.transFigure)
        elif lp['LatticeTop'] == 'spindle':
            if lp['NH'] == lp['NV']:
                nstr = str(int(lp['NH']))
            else:
                nstr = str(int(lp['NH'])) + ', ' + str(int(lp['NV']))
            ax.text(0.5, 0.9, 'Spectrum formation \n ' + r'in spindle lattice ($N=$' + nstr + ')',
                    ha='center', va='center', transform=fig.transFigure)

        lat.plot_BW_lat(fig=fig, ax=ax1, save=False, close=False, title='')
        plt.savefig(outfn + '_kmin' + sf.float2pstr(np.min(kvals)) + '_kmax' + sf.float2pstr(np.max(kvals)) + '.pdf')
        plt.show()
        ##########################################################################

        for ii in todo:
            modefn = dio.prepdir(lat.lp['meshfn']) + 'glat_mode_scaling_tune_junction_mode{0:05d}'.format(ii) +\
                     '_nkvals{0:04}'.format(nkvals) + '.mov'
            globmodefn = glob.glob(modefn)
            if not globmodefn:
                modedir = dio.prepdir(lat.lp['meshfn']) + 'glat_mode_scaling_tune_junction_mode{0:05d}/'.format(ii)
                dio.ensure_dir(modedir)
                previous_ev = None
                first = True
                dmyi = 0
                for kval in kvals:
                    lp = copy.deepcopy(lpmaster)
                    lp['OmKspec'] = 'union' + sf.float2pstr(kval, ndigits=3) + 'in' + dist_thres
                    # lat = lattice_class.Lattice(lp)
                    glat = GyroLattice(lat, lp)
                    eigval, eigvect = glat.eig_vals_vects(attribute=True)

                    # plot the nth mode
                    # fig, DOS_ax, eax = leplt.initialize_eigvect_DOS_header_plot(eigval, glat.lattice.xy,
                    #                                                             sim_type='gyro', cbar_nticks=2,
                    #                                                             cbar_tickfmt='%0.3f')
                    fig, dos_ax, eax, ax1, cbar_ax = \
                        leplt.initialize_eigvect_DOS_header_twinplot(eigval, glat.lattice.xy, sim_type='gyro',
                                                                     ax0_pos=[0.0, 0.10, 0.6, 0.60],
                                                                     ax1_pos=[0.6, 0.15, 0.3, 0.60],
                                                                     header_pos=[0.1, 0.78, 0.4, 0.20],
                                                                     xlabel_pad=8, fontsize=8)

                    # Get the theta that minimizes the difference between the present and previous eigenvalue
                    if previous_ev is not None:
                        realxy = np.real(previous_ev)
                        thetas, eigvects = gdh.phase_fix_nth_gyro(glat.eigvect, ngyro=0, basis='XY')
                        modenum = gdh.phase_difference_minimum(eigvects, realxy, basis='XY')
                        # print 'thetas = ', thetas
                        # if theta < 1e-9:
                        #     print 'problem with theta'
                        #     sys.exit()
                    else:
                        thetas, eigvects = gdh.phase_fix_nth_gyro(glat.eigvect, ngyro=0, basis='XY')
                        modenum = ii

                    glat.lattice.plot_BW_lat(fig=fig, ax=eax, save=False, close=False, axis_off=False, title='')
                    fig, [scat_fg, scat_fg2, pp, f_mark, lines_12_st], cw_ccw = \
                        leplt.construct_eigvect_DOS_plot(glat.lattice.xy, fig, dos_ax, eax, eigval, eigvects,
                                                         modenum, 'gyro', glat.lattice.NL, glat.lattice.KL,
                                                         marker_num=0, color_scheme='default', sub_lattice=-1,
                                                         amplify=1., title='')

                    # fig, ax_tmp, [scat_fg, pp, f_mark, lines12_st] = \
                    #     glat.plot_eigvect_excitation(ii, eigval=eigval, eigvect=eigvect, ax=eax, plot_lat=first,
                    #                                  theta=theta, normalization=1.0)  # color=lecmaps.blue())

                    # Store this current eigvector as 'previous_ev'
                    previous_ev = eigvects[modenum]

                    # scat_fg.remove()
                    # scat_fg2.remove()
                    # pp.remove()
                    # if f_mark is not None:
                    #     f_mark.remove()
                    # lines_12_st.remove()
                    # eax.cla()

                    # Plot where in evolution we are tracking
                    ngyros = int(np.shape(eigvals)[1] * 0.5)
                    halfev = eigvals[:, ngyros:]
                    for row in halfev.T:
                        ax1.loglog(np.abs(kvals), row, 'b-')

                    trackmark = ax1.plot(np.abs(kval), np.abs(np.imag(eigval))[modenum], 'ro')
                    ax1.set_xlabel(r"vertex coupling, $\Omega_k'$")
                    ax1.set_ylabel(r"frequency, $\omega$")

                    dos_ax.set_xlim(xmin=0)
                    plt.savefig(modedir + 'DOS_' + '{0:05}'.format(dmyi) + '.png', dpi=200)

                    dmyi += 1
                    first = False

                # Make movie
                imgname = modedir + 'DOS_'
                movname = modedir[:-1] + '_nkvals{0:04}'.format(nkvals)
                lemov.make_movie(imgname, movname, indexsz='05', framerate=5, rm_images=True, save_into_subdir=True,
                                 imgdir=modedir)
def dispersion_abtransition(lp, invert_bg=False, color1=None, color2=None, color_thres=None,
                            nkxvals=50, dab_step=0.005, dpi=100, plot_positive_only=True, ab_max=2.6):
    """

    Parameters
    ----------
    lp

    Returns
    -------

    """
    if invert_bg:
        plt.style.use('dark_background')

    if invert_bg:
        if color1 is None:
            # blue
            color1 = '#70a6ff'
        if color2 is None:
            # red
            color2 = '#ff7777'  # '#90354c'
        # print 'color1, 2 = ', color1, color2
        # sys.exit()
    else:
        cmap = lecmaps.diverging_cmap(250, 10, l=30)
        color1 = cmap(0.)
        color2 = cmap(1.)

    meshfn = le.find_meshfn(lp)
    lp['meshfn'] = meshfn
    lpmaster = copy.deepcopy(lp)
    ablist = np.arange(0, ab_max + dab_step, dab_step)
    # create a place to put images
    outdir = dio.prepdir(meshfn) + 'dispersion_abtransition/'
    dio.ensure_dir(outdir)
    fs = 20

    # go through each ab and make the image
    ii = 0
    for ab in ablist:
        print 'ab = ', ab
        lp = copy.deepcopy(lpmaster)
        lp['ABDelta'] = ab
        lat = lattice_class.Lattice(lp)
        lat.load(check=lp['check'])

        glat = GyroLattice(lat, lp)
        # glat.get_eigval_eigvect(attribute=True)
        fig, ax = leplt.initialize_portrait(ax_pos=[0.12, 0.2, 0.76, 0.6])
        omegas, kx, ky = glat.infinite_dispersion(save=False, nkxvals=nkxvals, nkyvals=50, outdir=outdir, save_plot=False)

        # plot and save it
        title = r'$\Delta_{AB} =$' + '{0:0.2f}'.format(ab)
        for jj in range(len(ky)):
            for kk in range(len(omegas[0, jj, :])):
                if color_thres is None or ab > 0.26:
                    if invert_bg:
                        ax.plot(kx, omegas[:, jj, kk], 'w-', lw=max(1., 30. / (len(kx) * len(ky))))
                    else:
                        ax.plot(kx, omegas[:, jj, kk], 'k-', lw=max(1., 30. / (len(kx) * len(ky))))
                else:
                    # if positive frequencies, color top band blue
                    if len(np.where(omegas[:, jj, kk] > 0)[0]) > 0.5 * len(omegas[:, jj, kk]):
                        # color top band blue
                        if len(np.where(np.abs(omegas[:, jj, kk]) > color_thres)[0]) > 0.5 * len(omegas[:, jj, kk]):
                            ax.plot(kx, omegas[:, jj, kk], '-', lw=max(1., 30. / (len(kx) * len(ky))), color=color1)
                        else:
                            ax.plot(kx, omegas[:, jj, kk], '-', lw=max(1., 30. / (len(kx) * len(ky))), color=color2)

                    # if negative frequencies, color bottom band blue
                    if len(np.where(omegas[:, jj, kk] < 0)[0]) > 0.5 * len(omegas[:, jj, kk]):
                        # color bottom band blue
                        if len(np.where(np.abs(omegas[:, jj, kk]) > color_thres)[0]) > 0.5 * len(omegas[:, jj, kk]):
                            ax.plot(kx, omegas[:, jj, kk], '-', lw=max(1., 30. / (len(kx) * len(ky))), color=color2)
                        else:
                            ax.plot(kx, omegas[:, jj, kk], '-', lw=max(1., 30. / (len(kx) * len(ky))), color=color1)

        ax.text(0.5, 1.15, title, fontsize=fs, ha='center', va='center', transform=ax.transAxes)
        ax.set_xlim(-np.pi, np.pi)
        ax.xaxis.set_ticks([-np.pi, 0, np.pi])
        ax.xaxis.set_ticklabels([r'$-\pi$', 0, r'$\pi$'])
        ax.set_xlabel(r'$k_x$', fontsize=fs)
        ax.set_ylabel(r'$\omega$', fontsize=fs)
        for tick in ax.xaxis.get_major_ticks():
            tick.label.set_fontsize(fs)
        for tick in ax.yaxis.get_major_ticks():
            tick.label.set_fontsize(fs)
        if ii == 0:
            ylims = ax.get_ylim()

        if plot_positive_only:
            ax.set_ylim(0., ylims[1])
        else:
            ax.set_ylim(ylims[0], ylims[1])

        # Save it
        name = outdir + 'dispersion{0:04d}'.format(ii)
        plt.savefig(name + '.png', dpi=dpi)
        plt.close('all')

        ii += 1

    # Turn images into a movie
    imgname = outdir + 'dispersion'
    movname = dio.prepdir(meshfn) + 'dispersion_abtrans' + glat.lp['meshfn_exten']
    lemov.make_movie(imgname, movname, indexsz='04', framerate=10, imgdir=outdir, rm_images=True,
                     save_into_subdir=True)
def gap_scaling(lp, args):
    """

    Parameters
    ----------
    lp
    args

    Returns
    -------

    """
    par1 = 'delta'
    par1v = np.pi * np.arange(0.667, 1.25, 0.3)
    par2 = 'Omg'
    par2v = np.arange(1.0, 100., 10.) ** 2
    if args.N == 10:
        print 'set N == 10: choosing ev1, ev2'
        ev1 = 299
        ev2 = 300
    elif args.N == 8:
        print 'set N == 8: choosing ev1, ev2'
        ev1 = 191
        ev2 = 192
    elif args.N == 6:
        print 'set N == 6: choosing ev1, ev2'
        ev1 = 107
        ev2 = 108

    gapsz = np.zeros((len(par1v), len(par2v)), dtype=float)
    eigvals = np.zeros((2, len(par2v)), dtype=float)
    kk = 0
    for p1 in par1v:
        lp[par1] = p1
        lat = lattice_class.Lattice(lp)
        print 'Building lattice...'
        lat.build()

        diff = np.zeros(len(par2v))
        jj = 0
        for p2 in par2v:
            lp[par2] = p2
            glat = GyroLattice(lat, lp)
            eigval, eigvect = glat.eig_vals_vects(attribute=False)
            print 'eigval[ev1] = ', eigval[ev1]
            print 'eigval[ev2] = ', eigval[ev2]
            sys.exit()
            eigvals[0, jj] = np.imag(eigval[ev1])
            eigvals[1, jj] = np.imag(eigval[ev2])
            diff[jj] = np.abs(np.imag(eigval[ev1] - eigval[ev2]))
            jj += 1

        gapsz[kk] = diff
        plt.plot(par2v, np.log10(abs(diff)), '.-', label=str(par1v))
        plt.xlabel(r'$\Omega_g$')
        plt.ylabel(r'Gap width $\log_{10} W$')
        # plt.pause(0.01)
        plt.show()

        plt.clf()
        print 'par2v = ', par2v
        # print 'eigvals = ', eigvals
        plt.plot(par2v, eigvals[0], '.-')
        plt.plot(par2v, eigvals[1], '.-')
        plt.show()

        kk += 1

    plt.show()
Beispiel #15
0
def build_select_region(lp):
    """

    Parameters
    ----------
    lp

    Returns
    -------

    """
    lpnew = copy.deepcopy(lp)
    lpnew['LatticeTop'] = lp['LatticeTop'].split('selregion_')[-1]
    lpnew['check'] = False
    print 'lpnew[LatticeTop] = ', lpnew['LatticeTop']
    lattice = lattice_class.Lattice(lpnew)
    print '\nBuilding lattice...'
    lattice.build()
    # xy, NL, KL, BL, PVx, PVy, PVxydict, LVUC, BBox, LL, LV, UC, lattice_exten = build_kagome_isocent(lp)
    xy = lattice.xy
    NL = lattice.NL
    KL = lattice.KL
    BL = lattice.BL
    PVx = lattice.PVx
    PVy = lattice.PVy
    PVxydict = lattice.PVxydict
    try:
        LVUC = lattice.lp['LVUC']
        LV = lattice.lp['LV']
        UC = lattice.lp['UC']
    except:
        LVUC = 'none'
        LV = 'none'
        UC = 'none'

    LL = lattice.lp['LL']
    old_lattice_exten = lattice.lp['lattice_exten']

    # Display lattice
    ax = le.display_lattice_2D(xy,
                               BL,
                               NL=NL,
                               KL=KL,
                               PVxydict=PVxydict,
                               PVx=PVx,
                               PVy=PVy,
                               title='Choose roi polygon',
                               xlimv=None,
                               ylimv=None,
                               colorz=True,
                               ptcolor=None,
                               ptsize=10,
                               close=False,
                               colorpoly=False,
                               viewmethod=False,
                               labelinds=False,
                               colormap='BlueBlackRed',
                               bgcolor='#FFFFFF',
                               axis_off=False,
                               linewidth=0.0,
                               edgecolors=None,
                               check=False)

    # let user draw ROI
    roi = roipoly(ax=ax, roicolor='r')

    print 'roi = ', roi
    print 'x = ', roi.allxpoints
    print 'y = ', roi.allypoints
    roi = np.dstack((roi.allxpoints, roi.allypoints))[0]
    inpoly = dh.inds_in_polygon(xy, roi)
    print 'inpoly = ', inpoly
    if lp['check']:
        plt.plot(xy[inpoly, 0], xy[inpoly, 1], 'b.')
        plt.show()

    xy, NL, KL, BL = le.remove_pts(inpoly, xy, BL, check=lp['check'])
    if lp['periodicBC']:
        PV = le.PVxydict2PV(PVxydict)
        PVxydict = le.BL2PVxydict(BL, xy, PV)
        # If cropping the points has cut off all periodic BCs, update lp to reflect this
        if len(PVxydict) == 0:
            lp['periodicBC'] = False

    if LVUC is not None and LVUC is not 'none':
        LVUC = LVUC[inpoly]

    BBox = roi
    lattice_exten = 'selregion_' + old_lattice_exten + '_NP{0:06d}'.format(
        len(xy))
    xy -= np.mean(xy, axis=0)
    if lp['check']:
        ax = le.display_lattice_2D(xy,
                                   BL,
                                   NL=NL,
                                   KL=KL,
                                   PVxydict=PVxydict,
                                   PVx=PVx,
                                   PVy=PVy,
                                   title='Cropped network',
                                   xlimv=None,
                                   ylimv=None,
                                   colorz=True,
                                   ptcolor=None,
                                   ptsize=10,
                                   close=False,
                                   colorpoly=False,
                                   viewmethod=False,
                                   labelinds=False,
                                   colormap='BlueBlackRed',
                                   bgcolor='#FFFFFF',
                                   axis_off=False,
                                   linewidth=0.0,
                                   edgecolors=None,
                                   check=False)

    return xy, NL, KL, BL, PVx, PVy, PVxydict, LVUC, BBox, LL, LV, UC, lattice_exten, lp