def bestmeshIter_vary_pf(Blatt, Nmesh, path):
    '''The kmesh can be related to the reciprocal lattice B by  B = KM, where M is an integer 3x3 matrix
    So K = B Inv(M).  Change M one element at a time to minimize the errors in symmetry and the cost in S/V and Nmesh '''

    ##############################################################
    ########################## Script ############################
    #    print path.split('/')
    npathsegs = len(path.split('/'))
    #    print npathsegs
    vaspinputdir = '/'.join(
        path.split('/')[0:npathsegs - 3]
    ) + '/vaspinput/'  #up two levels, 2 are for spaces at beg and end
    #    print vaspinputdir
    M = zeros((3, 3), dtype=int)
    S = zeros((3, 3), dtype=fprec)
    B = lattice()
    A = lattice()
    K = lattice()
    status = ''
    pf_minsv = 0
    pf_sv2fcc = 0
    pf_maxpf = 0
    pf_pf2fcc = 0
    #kvecs_pf2fcc = identity(3)
    sym_maxpf = False
    sym_sv2fcc = False
    sym_minsv = False
    sym_pf2fcc = False
    print 'Target mesh number', Nmesh

    B.vecs = Blatt / 2 / pi  #Don't use 2pi constants in reciprocal lattice here
    #    B.pftarget = 0.7405 #default best packing fraction

    #############End BCT lattice
    eps = 1.0e-6

    B.Nmesh = Nmesh
    print 'B vectors (differ by 2pi from traditional)'
    print B.vecs  #
    #print 'B transpose'; print transpose(B.vecs)
    B.det = det(B.vecs)
    print 'Det of B', B.det
    print 'Orth Defect of B', orthdef(B.vecs)
    print 'Surf/vol of B', surfvol(B.vecs)
    pfB = packingFraction(B.vecs)
    print 'Packing fraction of B:', pfB
    [B.symops, B.nops] = getGroup(B.vecs)
    B.msymops = intsymops(B)  #integer sym operations in B basis
    #    print'Symmetry operators in basis of B'
    #    for i in range:
    #        print B.msymops[:,:,i];print
    #    printops_eigs(B)
    B.lattype = latticeType(B.nops)
    print 'Lattice type:', B.lattype
    A = lattice()
    A.vecs = trimSmall(inv(transpose(B.vecs)))
    [A.symops, A.nops] = getGroup(A.vecs)
    A.msymops = intsymops(A)
    print 'Real space lattice A'
    print A.vecs
    print 'Det A', det(A.vecs)
    pfA = packingFraction(A.vecs)
    print 'Packing fraction of A:', pfA

    #    print 'current dir for meshesfile', os.getcwd()
    meshesfile = open('meshesfile', 'w')
    #    meshesfile = open('meshesfile2','w')
    meshesfile.write('N target %i\n' % B.Nmesh)
    meshesfile.write('Format: pf then Nmesh then kmesh\n\n')

    pflist = []
    #    for pftry in frange(pfB/2,0.75,0.005):
    for pftry in frange(pfB / 2, 0.75, 0.01):
        #    for pftry in frange(.3,0.505,0.005):
        print '\nPacking fraction target', pftry
        B.pftarget = pftry
        pf_orth = 0
        pf_orth2fcc = 0
        sym_orth = False
        sym_orth2fcc = False
        #'--------------------------------------------------------------------------------------------------------'
        #'--------------------------------------------------------------------------------------------------------'
        M = zeros((3, 3), dtype=int)
        ctest = []
        type = 'maxpfsym'
        print type
        ctrials = [3]
        a = rint(Nmesh**(1 / 3.0))
        # f = int(Nmesh/a/a)
        randnums = zeros(9)
        print 'M scale a', a
        for c in ctrials:
            #            ri = [randint(5) for i in range(9)]
            #            M = array([[-a+ri[0], a/c +ri[1] , a/c+ri[2]],[a/c+ri[3],-a+ri[4],a/c+ri[5]],\
            #                        [a/c+ri[6],a/c+ri[7],-a+ri[8]]])  #bcc like best avg pf on 50: 0.66

            #HNF::::::
            M = array([[a, 0 , 0],
                       [a/c,a,0],\
                        [a/c,a/c,a]])  #bcc like best avg pf on 50: 0.66

            #
            #            M = array([[-a+1, a/c , a/c],[a/c,-a,a/c],[a/c,a/c,-a-1]])  #bcc like best avg pf on 50: 0.66
            #            M = array([[a, 0,0],[0,a,0],[0,0,a+3]])
            #            M = array([[-16 ,  1 ,  5 ],
            #                [6 ,  -10 ,  5],
            #                [-6  , -1  , 6  ]])
            #            M = array([[5, a/c , a/c],[a/c,0,a/c],[a/c,a/c,-5]]) #fcc like best avg pf on 50: 0.59
            M = rint(M * (B.Nmesh / abs(det(M)))**(1 / 3.0))
            print 'Start mesh trial'
            print M
            #            [M,K] = findmin(M,B,type)
            #            print 'Test trial M'; print M
            ctest.append(cost(M, B, type))
#        print'Trial costs',ctest
        cbest = ctrials[argmin(ctest)]
        #        print'Best c', cbest
        iternpf = 0
        itermaxnpf = 10
        itermaxsym = 5
        #        bestpf = 100
        #        NPFcost = 100;delNPFcost = -1 #initial values
        type = 'maxpfsym'
        print type
        delcost = -1
        lowcost = 1000  #initialize
        ####       while iternpf<itermaxnpf and delNPFcost <0 and abs(delNPFcost)>0.1 :
        while iternpf < itermaxnpf and delcost < -0.1:
            oldcost = cost(M, B, type)
            #            NPFcost = cost(M,B,'maxpf')
            #            delNPFcost = (NPFcost-oldNPFcost)/NPFcost :
            '''Here we let M vary in the search, but record pf and kvecs when we find min cost'''
            #            print 'cost(N,PF):', cost(M,B,type)
            #        while not symm and and iternpf<itermax:
            M = rint(M * (B.Nmesh / abs(det(M)))**(1 / 3.0))
            print 'Scaled M'
            print M
            iternpf += 1
            print 'Iteration', type, iternpf, '**********'
            [M, K] = findmin(M, B, type)
            M = rint(M)
            itersym = 0
            symm = False
            while not symm and itersym < itermaxsym:
                itersym += 1
                print 'Symmetry iteration', itersym, '-------'
                #                print 'Nmesh', abs(det(M)), 'packing', packingFraction(dot(B.vecs,inv(M)))
                #                M = rint(minkM(M,B))#; print'Mink reduced M'; print M
                for iop in range(B.nops):
                    M = rint(findmin_i(M, B, iop))
                    #                    M = rint(minkM(M,B))#; print'Mink reduced M'; print M
                    if abs(det(M) - B.Nmesh
                           ) / B.Nmesh > 0.15:  #how far off from target N
                        M = rint(M * (B.Nmesh / abs(det(M)))**(1 / 3.0))
                        print 'Scaled M'
                        print M
                K = lattice()
                K.vecs = trimSmall(dot(B.vecs, inv(M)))
                K.det = abs(det(K.vecs))
                K.Nmesh = B.det / K.det
                symm = checksymmetry(K.vecs, B)
                print 'Symmetry check', symm
                if symm:
                    newcost = cost(M, B, type)
                    if newcost - lowcost < 0:
                        lowcost = newcost
                        print 'New lowcost', newcost
                        pf_maxpf = packingFraction(K.vecs)
                        sym_maxpf = True
                        kvecs_maxpf = K.vecs
    #                    if pf_maxpf<bestpf: bestpf = pf_maxpf; bestM = M
                    print 'Packing fraction', pf_maxpf, 'vs original B', pfB
                    print 'Nmesh', K.Nmesh, 'vs target', B.Nmesh
                delcost = cost(M, B, type) - oldcost
        #write to files

#        meshesfile.write('Packing fraction target %f\n' % pftry)
        if symm and pf_maxpf not in pflist:
            pflist.append(pf_maxpf)
            #            meshesfile.write('Packing fraction achieved %f\n' % pf_maxpf)
            meshesfile.write('%12.8f  %8.3f \n' % (pf_maxpf, K.Nmesh))
            #            meshesfile.write('M\n')
            #            for i in range(3):
            #                for j in range(3):
            #                    meshesfile.write('%i6' %M[i,j])
            #                meshesfile.write('\n')
            ##            meshesfile.write('\n')

            #            meshesfile.write('k mesh\n')
            M = rint(dot(inv(K.vecs), B.vecs))
            for i in range(3):
                for j in range(3):
                    meshesfile.write('%i   ' % int(rint(M[i, j])))
                meshesfile.write('\n')
            meshesfile.write('\n')
            meshesfile.flush()

            M = rint(dot(inv(K.vecs), B.vecs)
                     )  #We assign K only when M is ideal, so remake the best M
            print 'Check M'
            print M
            print 'Check K'
            print K.vecs
            print 'Check B'
            print B.vecs
            print 'Check pf'
            print packingFraction(K.vecs)
            #create a dir and prepare for vasp run
            newdir = str(round(pf_maxpf, 4))
            newpath = path + newdir + '/'
            if not os.path.isdir(newpath):
                os.system('mkdir %s' % newpath)
            os.chdir(newpath)
            os.system('cp %s* %s' % (vaspinputdir, newpath))
            os.system('cp %sPOSCAR %s' % (path, newpath))
            print 'SKIPPING writekpts_vasp_M AND submission'
            #            writekpts_vasp_M(newpath,B,M,K)
            #            writekpts_vasp_pf(newpath,K.vecs,pf_maxpf,K.Nmesh)
            writejobfile(newpath)

            #            subprocess.call(['sbatch', 'vaspjob']) #!!!!!!! Submit jobs
            os.chdir(path)
        else:
            'do nothing'
#            meshesfile.write('Failed symmetry\n\n')
    meshesfile.close()

    #  Summary
    pfs = [pfB]
    pftypes = ['B_latt']
    ks = [B.vecs / a]  #one solutions is to simply divide B by an integer
    if not (sym_minsv or sym_sv2fcc or sym_maxpf or pf_pf2fcc or sym_orth
            or sym_orth2fcc):
        meshtype = 'B_latt_revert'
        #status += 'MHPrevert;'
        K.vecs = B.vecs / a
        K.det = abs(det(K.vecs))
        K.Nmesh = abs(B.det / K.det)
        pfmax = packingFraction(K.vecs)
    else:

        if sym_orth:
            pfs.append(pf_orth)
            pftypes.append('orth')
            ks.append(kvecs_orth)
        if sym_orth2fcc:
            pfs.append(pf_orth2fcc)
            pftypes.append('orth2fcc')
            ks.append(kvecs_orth2fcc)
        if sym_maxpf:
            pfs.append(pf_maxpf)
            pftypes.append('maxpf')
            ks.append(kvecs_maxpf)
        if sym_pf2fcc:
            pfs.append(pf_pf2fcc)
            pftypes.append('pf2fcc')
            ks.append(kvecs_pf2fcc)
    pfmax = max(pfs)
    meshtype = pftypes[argmax(pfs)]
    K.vecs = ks[argmax(pfs)]
    K.det = abs(det(K.vecs))
    K.Nmesh = B.det / K.det
    #    return [K.vecs, K.Nmesh, B.Nmesh, B.lattype, pfB, pf_orth, pf_orth2fcc, pf_maxpf, pf_minsv, pf_sv2fcc, pfmax, meshtype, fcctype(B),status]

    return [
        K.vecs, K.Nmesh, B.Nmesh, B.lattype, pfB, pf_orth, pf_orth2fcc,
        pf_maxpf, pf_pf2fcc, pfmax, meshtype,
        fcctype(B), cbest, status
    ]
Exemple #2
0
def bestmeshIter(Blatt, Nmesh):
    '''The kmesh can be related to the reciprocal lattice B by  B = KM, where M is an integer 3x3 matrix
    So K = B Inv(M).  Change M one element at a time to minimize the errors in symmetry and the cost in S/V and Nmesh '''

    ##############################################################
    ########################## Script ############################

    M = zeros((3, 3), dtype=int)
    S = zeros((3, 3), dtype=fprec)
    B = lattice()
    A = lattice()
    K = lattice()
    status = ''
    pf_minsv = 0
    pf_sv2fcc = 0
    pf_maxpf = 0
    pf_pf2fcc = 0
    #kvecs_pf2fcc = identity(3)
    sym_maxpf = False
    sym_sv2fcc = False
    sym_minsv = False
    sym_pf2fcc = False
    a = rint(Nmesh**(1 / 3.0))
    f = int(Nmesh / a / a)
    print 'Target mesh number', Nmesh

    B.vecs = Blatt / 2 / pi  #Don't use 2pi constants in reciprocal lattice here
    #    B.pftarget = 0.7405 #default best packing fraction
    B.pftarget = 0.35
    #############End BCT lattice
    eps = 1.0e-6

    B.Nmesh = Nmesh
    print 'B vectors (differ by 2pi from traditional)'
    print B.vecs  #
    #print 'B transpose'; print transpose(B.vecs)
    B.det = det(B.vecs)
    print 'Det of B', B.det
    print 'Orth Defect of B', orthdef(B.vecs)
    print 'Surf/vol of B', surfvol(B.vecs)
    pfB = packingFraction(B.vecs)
    print 'Packing fraction of B:', pfB
    [B.symops, B.nops] = getGroup(B.vecs)
    B.msymops = intsymops(B)  #integer sym operations in B basis
    #    printops_eigs(B)
    B.lattype = latticeType(B.nops)
    print 'Lattice type:', B.lattype
    A = lattice()
    A.vecs = trimSmall(inv(transpose(B.vecs)))
    [A.symops, A.nops] = getGroup(A.vecs)
    A.msymops = intsymops(A)
    print 'Real space lattice A'
    print A.vecs
    print 'Det A', det(A.vecs)
    pfA = packingFraction(A.vecs)
    print 'Packing fraction of A:', pfA

    pf_orth = 0
    pf_orth2fcc = 0
    sym_orth = False
    sym_orth2fcc = False
    if B.lattype in ['Orthorhombic', 'Tetragonal', 'Cubic']:
        cbest = ''  #need for passing other structures' results to main program
        [kvecs_orth, pf_orth, sym_orth] = orthsuper(B)
        M = rint(dot(inv(kvecs_orth), B.vecs)).astype(int)
        print
        print 'Try orth2FCC substitution.',
        kmesh2 = zeros((3, 3), dtype=float)
        scale = 2 / 4**(1 / 3.0)
        kmesh2[:, 0] = kvecs_orth[:, 1] / scale + kvecs_orth[:, 2] / scale
        kmesh2[:, 1] = kvecs_orth[:, 2] / scale + kvecs_orth[:, 0] / scale
        kmesh2[:, 2] = kvecs_orth[:, 0] / scale + kvecs_orth[:, 1] / scale
        sym_orth2fcc = checksymmetry(kmesh2, B)
        if sym_orth2fcc:
            pf = packingFraction(kmesh2)
            print
            print 'Packing fraction', pf, 'vs original B', pfB
            if pf > pf_orth:
                M = rint(dot(inv(kmesh2), B.vecs)).astype(int)
                print 'M'
                print M
            else:
                '    Packing fraction too small'
            kvecs_orth2fcc = kmesh2
            pf_orth2fcc = pf
        else:
            print ' It fails symmetry test'
#    sys.exit('stop')
    else:
        #'--------------------------------------------------------------------------------------------------------'
        #'--------------------------------------------------------------------------------------------------------'
        M = zeros((3, 3), dtype=int)
        ctest = []
        type = 'maxpfsym'
        print type
        ctrials = [3]
        for c in ctrials:
            M = array([[-a + 2, a / c, a / c], [a / c, -a, a / c],
                       [a / c, a / c,
                        -a - 2]])  #bcc like best avg pf on 50: 0.66
            #            M = array([[2, a/c , a/c],[a/c,0,a/c],[a/c,a/c,-2]]) #fcc like best avg pf on 50: 0.59
            M = rint(M * (B.Nmesh / abs(det(M)))**(1 / 3.0))
            print 'Start mesh trial'
            print M
            [M, K] = findmin(M, B, type)
            print 'Test trial M'
            print M
            ctest.append(cost(M, B, type))
#        print'Trial costs',ctest
        cbest = ctrials[argmin(ctest)]
        #        print'Best c', cbest
        iternpf = 0
        itermaxnpf = 10
        itermaxsym = 5
        #        bestpf = 100
        #        NPFcost = 100;delNPFcost = -1 #initial values
        type = 'maxpfsym'
        print type
        delcost = -1
        lowcost = 1000  #initialize
        ####       while iternpf<itermaxnpf and delNPFcost <0 and abs(delNPFcost)>0.1 :
        while iternpf < itermaxnpf and delcost < -0.1:
            oldcost = cost(M, B, type)
            #            NPFcost = cost(M,B,'maxpf')
            #            delNPFcost = (NPFcost-oldNPFcost)/NPFcost :
            '''Here we let M vary in the search, but record pf and kvecs when we find min cost'''
            #            print 'cost(N,PF):', cost(M,B,type)
            #        while not symm and and iternpf<itermax:
            M = rint(M * (B.Nmesh / abs(det(M)))**(1 / 3.0))
            print 'Scaled M'
            print M
            iternpf += 1
            print 'Iteration', type, iternpf, '**********'
            [M, K] = findmin(M, B, type)
            print M
            itersym = 0
            symm = False
            while not symm and itersym < itermaxsym:
                itersym += 1
                print 'Symmetry iteration', itersym, '-------'
                #                print 'Nmesh', abs(det(M)), 'packing', packingFraction(dot(B.vecs,inv(M)))
                M = minkM(M, B)  #; print'Mink reduced M'; print M
                for iop in range(B.nops):
                    M = findmin_i(M, B, iop)
                    if abs(det(M) - B.Nmesh
                           ) / B.Nmesh > 0.15:  #how far off from target N
                        M = rint(M * (B.Nmesh / abs(det(M)))**(1 / 3.0))
                        print 'Scaled M'
                        print M
                K = lattice()
                K.vecs = trimSmall(dot(B.vecs, inv(M)))
                K.det = abs(det(K.vecs))
                K.Nmesh = B.det / K.det
                symm = checksymmetry(K.vecs, B)
                print 'Symmetry check', symm
                if symm:
                    newcost = cost(M, B, type)
                    if newcost - lowcost < 0:
                        lowcost = newcost
                        print 'New lowcost', newcost
                        pf_maxpf = packingFraction(K.vecs)
                        sym_maxpf = True
                        kvecs_maxpf = K.vecs
    #                    if pf_maxpf<bestpf: bestpf = pf_maxpf; bestM = M
                    print 'Packing fraction', pf_maxpf, 'vs original B', pfB
                    print 'Nmesh', K.Nmesh, 'vs target', B.Nmesh
                    print
                    print 'Try FCC-like substitution.'
                    kmesh2 = zeros((3, 3), dtype=float)
                    scale = 2 / 4**(1 / 3.0)
                    kmesh2[:, 0] = K.vecs[:, 1] / scale + K.vecs[:, 2] / scale
                    kmesh2[:, 1] = K.vecs[:, 2] / scale + K.vecs[:, 0] / scale
                    kmesh2[:, 2] = K.vecs[:, 0] / scale + K.vecs[:, 1] / scale
                    #            M = rint(dot(inv(kmesh2),B.vecs)).astype(int) #set this for maxpf run
                    if checksymmetry(kmesh2, B):
                        sym_pf2fcc = True
                        kvecs_pf2fcc = kmesh2
                        pf_pf2fcc = packingFraction(kmesh2)
                        Mtemp = rint(dot(inv(kmesh2), B.vecs)).astype(int)
                        if cost(Mtemp, B, type) < lowcost:
                            lowcost = cost(M, B, type)
                            print 'New lowcost', lowcost
                            M = Mtemp
                            print 'M'
                            print Mtemp
                            print 'Packing fraction', pf_pf2fcc, 'vs original B', pfB

                            print
                        else:
                            print '    Packing fraction too small'
                    else:
                        print '    Fails to improve mesh'
                delcost = cost(M, B, type) - oldcost
#  Summary
    pfs = [pfB]
    pftypes = ['B_latt']
    ks = [B.vecs / a]
    if not (sym_minsv or sym_sv2fcc or sym_maxpf or pf_pf2fcc or sym_orth
            or sym_orth2fcc):
        meshtype = 'B_latt_revert'
        #status += 'MHPrevert;'
        K.vecs = B.vecs / a
        K.det = abs(det(K.vecs))
        K.Nmesh = abs(B.det / K.det)
        pfmax = packingFraction(K.vecs)
    else:

        if sym_orth:
            pfs.append(pf_orth)
            pftypes.append('orth')
            ks.append(kvecs_orth)
        if sym_orth2fcc:
            pfs.append(pf_orth2fcc)
            pftypes.append('orth2fcc')
            ks.append(kvecs_orth2fcc)
        if sym_maxpf:
            pfs.append(pf_maxpf)
            pftypes.append('maxpf')
            ks.append(kvecs_maxpf)
        if sym_pf2fcc:
            pfs.append(pf_pf2fcc)
            pftypes.append('pf2fcc')
            ks.append(kvecs_pf2fcc)
    pfmax = max(pfs)
    meshtype = pftypes[argmax(pfs)]
    K.vecs = ks[argmax(pfs)]
    K.det = abs(det(K.vecs))
    K.Nmesh = B.det / K.det
    #    return [K.vecs, K.Nmesh, B.Nmesh, B.lattype, pfB, pf_orth, pf_orth2fcc, pf_maxpf, pf_minsv, pf_sv2fcc, pfmax, meshtype, fcctype(B),status]

    return [
        K.vecs, K.Nmesh, B.Nmesh, B.lattype, pfB, pf_orth, pf_orth2fcc,
        pf_maxpf, pf_pf2fcc, pfmax, meshtype,
        fcctype(B), cbest, status
    ]
Exemple #3
0
def bestmeshIter(Blatt,Nmesh):
    '''The kmesh can be related to the reciprocal lattice B by  B = KM, where M is an integer 3x3 matrix
    So K = B Inv(M).  Change M one element at a time to minimize the errors in symmetry and the cost in S/V and Nmesh '''
    
    ##############################################################
    ########################## Script ############################
   
    M = zeros((3,3),dtype = int)
    S = zeros((3,3),dtype = fprec)
    B = lattice()
    A = lattice()
    K = lattice()
    status = ''
    pf_minsv = 0; pf_sv2fcc = 0; pf_maxpf = 0; pf_pf2fcc = 0; #kvecs_pf2fcc = identity(3)
    sym_maxpf = False;  sym_sv2fcc = False; sym_minsv = False; sym_pf2fcc = False
    a = rint(Nmesh**(1/3.0)); f = int(Nmesh/a/a)
    print 'Target mesh number', Nmesh
       
    B.vecs = Blatt/2/pi  #Don't use 2pi constants in reciprocal lattice here
#    B.pftarget = 0.7405 #default best packing fraction
    B.pftarget = 0.35
    #############End BCT lattice
    eps = 1.0e-6

    B.Nmesh = Nmesh
    print 'B vectors (differ by 2pi from traditional)';print B.vecs #
    #print 'B transpose'; print transpose(B.vecs)
    B.det = det(B.vecs)
    print 'Det of B', B.det
    print 'Orth Defect of B', orthdef(B.vecs)
    print 'Surf/vol of B', surfvol(B.vecs)
    pfB = packingFraction(B.vecs)
    print 'Packing fraction of B:', pfB  
    [B.symops,B.nops] = getGroup(B.vecs)
    B.msymops = intsymops(B) #integer sym operations in B basis
#    printops_eigs(B)
    B.lattype = latticeType(B.nops)
    print 'Lattice type:', B.lattype
    A = lattice()
    A.vecs = trimSmall(inv(transpose(B.vecs)))
    [A.symops,A.nops] = getGroup(A.vecs)    
    A.msymops = intsymops(A)
    print 'Real space lattice A'; print A.vecs
    print 'Det A', det(A.vecs)
    pfA = packingFraction(A.vecs)
    print 'Packing fraction of A:', pfA    
    
    pf_orth=0; pf_orth2fcc=0; sym_orth = False; sym_orth2fcc = False
    if B.lattype in ['Orthorhombic', 'Tetragonal','Cubic']:
        cbest = '' #need for passing other structures' results to main program 
        [kvecs_orth,pf_orth,sym_orth] = orthsuper(B)
        M = rint(dot(inv(kvecs_orth),B.vecs)).astype(int)
        print; print 'Try orth2FCC substitution.',
        kmesh2 = zeros((3,3),dtype = float)
        scale = 2/4**(1/3.0)
        kmesh2[:,0] = kvecs_orth[:,1]/scale + kvecs_orth[:,2]/scale
        kmesh2[:,1] = kvecs_orth[:,2]/scale + kvecs_orth[:,0]/scale
        kmesh2[:,2] = kvecs_orth[:,0]/scale + kvecs_orth[:,1]/scale   
        sym_orth2fcc = checksymmetry(kmesh2,B)
        if sym_orth2fcc:
            pf = packingFraction(kmesh2)
            print; print 'Packing fraction', pf, 'vs original B', pfB  
            if pf>pf_orth:
                M = rint(dot(inv(kmesh2),B.vecs)).astype(int)
                print 'M';print M
            else: 
                '    Packing fraction too small'            
            kvecs_orth2fcc = kmesh2
            pf_orth2fcc = pf
        else:
            print' It fails symmetry test'
#    sys.exit('stop')
    else:
#'--------------------------------------------------------------------------------------------------------'
#'--------------------------------------------------------------------------------------------------------'
        M = zeros((3,3),dtype=int)
        ctest = []
        type = 'maxpfsym'; print type
        ctrials = [3]
        for c in ctrials:        
            M = array([[-a+2, a/c , a/c],[a/c,-a,a/c],[a/c,a/c,-a-2]])  #bcc like best avg pf on 50: 0.66
#            M = array([[2, a/c , a/c],[a/c,0,a/c],[a/c,a/c,-2]]) #fcc like best avg pf on 50: 0.59
            M = rint(M * (B.Nmesh/abs(det(M)))**(1/3.0))
            print 'Start mesh trial'; print M              
            [M,K] = findmin(M,B,type)
            print 'Test trial M'; print M
            ctest.append(cost(M,B,type))
#        print'Trial costs',ctest                           
        cbest = ctrials[argmin(ctest)]
#        print'Best c', cbest       
        iternpf = 0
        itermaxnpf = 10
        itermaxsym = 5
     #        bestpf = 100
#        NPFcost = 100;delNPFcost = -1 #initial values
        type = 'maxpfsym'; print type
        delcost = -1; lowcost = 1000 #initialize
####       while iternpf<itermaxnpf and delNPFcost <0 and abs(delNPFcost)>0.1 :
        while iternpf<itermaxnpf and delcost < -0.1:
            oldcost = cost(M,B,type)
        #            NPFcost = cost(M,B,'maxpf')
#            delNPFcost = (NPFcost-oldNPFcost)/NPFcost :
            '''Here we let M vary in the search, but record pf and kvecs when we find min cost'''
#            print 'cost(N,PF):', cost(M,B,type)
     #        while not symm and and iternpf<itermax:
            M = rint(M * (B.Nmesh/abs(det(M)))**(1/3.0))
            print 'Scaled M';print M
            iternpf += 1
            print 'Iteration',type,iternpf, '**********'
            [M,K] = findmin(M,B,type) 
            print M
            itersym = 0            
            symm = False
            while not symm and itersym <itermaxsym: 
                itersym += 1
                print 'Symmetry iteration', itersym, '-------'         
#                print 'Nmesh', abs(det(M)), 'packing', packingFraction(dot(B.vecs,inv(M)))
                M = minkM(M,B)#; print'Mink reduced M'; print M    
                for iop in range(B.nops):
                    M = findmin_i(M,B,iop)
                    if abs(det(M)-B.Nmesh)/B.Nmesh > 0.15: #how far off from target N
                        M = rint(M * (B.Nmesh/abs(det(M)))**(1/3.0))
                        print 'Scaled M';print M                    
                K = lattice();K.vecs = trimSmall(dot(B.vecs,inv(M)));K.det = abs(det(K.vecs)); K.Nmesh = B.det/K.det                                       
                symm = checksymmetry(K.vecs,B)
                print 'Symmetry check', symm
                if symm:
                    newcost = cost(M,B,type)
                    if newcost - lowcost < 0: 
                        lowcost = newcost;
                        print'New lowcost',newcost              
                        pf_maxpf = packingFraction(K.vecs)
                        sym_maxpf = True
                        kvecs_maxpf = K.vecs
     #                    if pf_maxpf<bestpf: bestpf = pf_maxpf; bestM = M
                    print 'Packing fraction', pf_maxpf, 'vs original B', pfB  
                    print 'Nmesh', K.Nmesh, 'vs target', B.Nmesh 
                    print; print 'Try FCC-like substitution.'
                    kmesh2 = zeros((3,3),dtype = float)
                    scale = 2/4**(1/3.0)
                    kmesh2[:,0] = K.vecs[:,1]/scale + K.vecs[:,2]/scale
                    kmesh2[:,1] = K.vecs[:,2]/scale + K.vecs[:,0]/scale
                    kmesh2[:,2] = K.vecs[:,0]/scale + K.vecs[:,1]/scale 
        #            M = rint(dot(inv(kmesh2),B.vecs)).astype(int) #set this for maxpf run  
                    if checksymmetry(kmesh2,B):                       
                        sym_pf2fcc = True
                        kvecs_pf2fcc = kmesh2
                        pf_pf2fcc = packingFraction(kmesh2)                        
                        Mtemp = rint(dot(inv(kmesh2),B.vecs)).astype(int)
                        if cost(Mtemp,B,type) < lowcost: 
                            lowcost = cost(M,B,type);print'New lowcost',lowcost
                            M = Mtemp                        
                            print 'M';print Mtemp
                            print 'Packing fraction', pf_pf2fcc, 'vs original B', pfB  

                            print;
                        else:
                            print '    Packing fraction too small' 
                    else:
                        print '    Fails to improve mesh'    
                delcost = cost(M,B,type) - oldcost
 #  Summary     
    pfs = [pfB]
    pftypes = ['B_latt']  
    ks  = [B.vecs/a]   
    if not (sym_minsv or sym_sv2fcc or sym_maxpf or pf_pf2fcc or sym_orth or sym_orth2fcc):
         meshtype = 'B_latt_revert' ; #status += 'MHPrevert;'
         K.vecs = B.vecs/a; K.det = abs(det(K.vecs)); K.Nmesh = abs(B.det/K.det)
         pfmax = packingFraction(K.vecs)
    else:     

         if sym_orth:
             pfs.append(pf_orth)
             pftypes.append('orth')
             ks.append(kvecs_orth)
         if sym_orth2fcc:
            pfs.append(pf_orth2fcc)
            pftypes.append('orth2fcc')
            ks.append(kvecs_orth2fcc)                     
         if sym_maxpf:
             pfs.append(pf_maxpf)
             pftypes.append('maxpf')
             ks.append(kvecs_maxpf)    
         if sym_pf2fcc:
             pfs.append(pf_pf2fcc)
             pftypes.append('pf2fcc')
             ks.append(kvecs_pf2fcc)                    
    pfmax = max(pfs)
    meshtype = pftypes[argmax(pfs)]
    K.vecs = ks[argmax(pfs)]; K.det = abs(det(K.vecs)); K.Nmesh = B.det/K.det
#    return [K.vecs, K.Nmesh, B.Nmesh, B.lattype, pfB, pf_orth, pf_orth2fcc, pf_maxpf, pf_minsv, pf_sv2fcc, pfmax, meshtype, fcctype(B),status]

    return [K.vecs, K.Nmesh, B.Nmesh, B.lattype, pfB, pf_orth, pf_orth2fcc, pf_maxpf, pf_pf2fcc, pfmax, meshtype, fcctype(B),cbest,status]
def bestmeshIter_vary_N(Blatt, Nmesh, path):
    '''The kmesh can be related to the reciprocal lattice B by  B = KM, where M is an integer 3x3 matrix
    So K = B Inv(M).  Change M one element at a time to minimize the errors in symmetry and the cost in S/V and Nmesh '''

    ##############################################################
    ########################## Script ############################
    vaspinputdir = '/fslhome/bch/cluster_expansion/alir/AFLOWDATAf1_50e/vaspinput/'
    M = zeros((3, 3), dtype=int)
    S = zeros((3, 3), dtype=fprec)
    B = lattice()
    A = lattice()
    K = lattice()
    status = ''
    pf_minsv = 0
    pf_sv2fcc = 0
    pf_maxpf = 0
    pf_pf2fcc = 0
    #kvecs_pf2fcc = identity(3)
    sym_maxpf = False
    sym_sv2fcc = False
    sym_minsv = False
    sym_pf2fcc = False
    a = rint(Nmesh**(1 / 3.0))
    f = int(Nmesh / a / a)
    print 'Target mesh number', Nmesh

    B.vecs = Blatt / 2 / pi  #Don't use 2pi constants in reciprocal lattice here
    #    B.pftarget = 0.7405 #default best packing fraction

    #############End BCT lattice
    eps = 1.0e-6

    B.Nmesh = Nmesh
    print 'B vectors (differ by 2pi from traditional)'
    print B.vecs  #
    #print 'B transpose'; print transpose(B.vecs)
    B.det = det(B.vecs)
    print 'Det of B', B.det
    print 'Orth Defect of B', orthdef(B.vecs)
    print 'Surf/vol of B', surfvol(B.vecs)
    pfB = packingFraction(B.vecs)
    print 'Packing fraction of B:', pfB
    [B.symops, B.nops] = getGroup(B.vecs)
    B.msymops = intsymops(B)  #integer sym operations in B basis
    #    print'Symmetry operators in basis of B'
    #    for i in range:
    #        print B.msymops[:,:,i];print
    #    printops_eigs(B)
    B.lattype = latticeType(B.nops)
    print 'Lattice type:', B.lattype
    A = lattice()
    A.vecs = trimSmall(inv(transpose(B.vecs)))
    [A.symops, A.nops] = getGroup(A.vecs)
    A.msymops = intsymops(A)
    print 'Real space lattice A'
    print A.vecs
    print 'Det A', det(A.vecs)
    pfA = packingFraction(A.vecs)
    print 'Packing fraction of A:', pfA

    meshesfile = open('meshesfile', 'a')
    meshesfile.write('N target %i\n' % B.Nmesh)
    meshesfile.write('Format: pf then Nmesh then kmesh\n\n')

    pflist = []
    #    M0 = array([[2,   2,   2,],
    #                    [2,   2,   -2],
    #                    [-2,   2,   -2]])
    #
    #      0.74050000    64.000
    #-5   1   -3
    #6   2   2
    #3   1   -3
    M0 = array([[
        -5,
        1,
        -3,
    ], [6, 2, 2], [3, 1, -3]])

    #    for div in [256,128,64,32,16,8,4,2,1]:
    #        print '\nDivisor',div
    #        nMP = rint((Nmesh/div)**(1/3.0))
    #        M = array([[nMP,0,0],[0,nMP,0],[0,0,nMP]]);

    for fac in [1, 2, 3, 4, 5, 6, 7, 8]:
        print '\nMultiplier', fac

        M = fac * M0
        #        M = array([[4,12,-4],
        #                   [-11,4,-26],
        #                   [-26,-4,-11]]);
        K = lattice()
        K.vecs = trimSmall(dot(B.vecs, inv(M)))
        K.det = abs(det(K.vecs))
        K.Nmesh = B.det / K.det
        print 'Number of points', det(M)
        print 'Check M'
        print M
        print 'Check K'
        print K.vecs
        print 'Check B'
        print B.vecs
        print 'Check pf'
        print packingFraction(K.vecs)
        #create a dir and prepare for vasp run
        newdir = str(K.Nmesh)
        newpath = path + newdir + '/'
        if not os.path.isdir(newpath):
            os.system('mkdir %s' % newpath)
        os.chdir(newpath)
        os.system('cp %s* %s' % (vaspinputdir, newpath))
        os.system('cp %sPOSCAR %s' % (path, newpath))
        writekpts_vasp_M(newpath, B, M, K)
        #             writekpts_vasp_pf(newpath,K.vecs,pf_maxpf,K.Nmesh)
        writejobfile(newpath)

        #            print 'submitting job'
        subprocess.call(['sbatch', 'vaspjob'])  #!!!!!!! Submit jobs
        os.chdir(path)
def bestmeshIter_vary_pf(Blatt,Nmesh,path):
    '''The kmesh can be related to the reciprocal lattice B by  B = KM, where M is an integer 3x3 matrix
    So K = B Inv(M).  Change M one element at a time to minimize the errors in symmetry and the cost in S/V and Nmesh '''
    
    ##############################################################
    ########################## Script ############################
#    print path.split('/')
    npathsegs = len(path.split('/'))
#    print npathsegs
    vaspinputdir = '/'.join(path.split('/')[0:npathsegs-3])+'/vaspinput/' #up two levels, 2 are for spaces at beg and end
#    print vaspinputdir
    M = zeros((3,3),dtype = int)
    S = zeros((3,3),dtype = fprec)
    B = lattice()
    A = lattice()
    K = lattice()
    status = ''
    pf_minsv = 0; pf_sv2fcc = 0; pf_maxpf = 0; pf_pf2fcc = 0; #kvecs_pf2fcc = identity(3)
    sym_maxpf = False;  sym_sv2fcc = False; sym_minsv = False; sym_pf2fcc = False
    print 'Target mesh number', Nmesh
       
    B.vecs = Blatt/2/pi  #Don't use 2pi constants in reciprocal lattice here
#    B.pftarget = 0.7405 #default best packing fraction

    #############End BCT lattice
    eps = 1.0e-6

    B.Nmesh = Nmesh
    print 'B vectors (differ by 2pi from traditional)';print B.vecs #
    #print 'B transpose'; print transpose(B.vecs)
    B.det = det(B.vecs)
    print 'Det of B', B.det
    print 'Orth Defect of B', orthdef(B.vecs)
    print 'Surf/vol of B', surfvol(B.vecs)
    pfB = packingFraction(B.vecs)
    print 'Packing fraction of B:', pfB  
    [B.symops,B.nops] = getGroup(B.vecs)
    B.msymops = intsymops(B) #integer sym operations in B basis
#    print'Symmetry operators in basis of B'
#    for i in range:
#        print B.msymops[:,:,i];print 
#    printops_eigs(B)
    B.lattype = latticeType(B.nops)
    print 'Lattice type:', B.lattype
    A = lattice()
    A.vecs = trimSmall(inv(transpose(B.vecs)))
    [A.symops,A.nops] = getGroup(A.vecs)    
    A.msymops = intsymops(A)
    print 'Real space lattice A'; print A.vecs
    print 'Det A', det(A.vecs)
    pfA = packingFraction(A.vecs)
    print 'Packing fraction of A:', pfA    
    
#    print 'current dir for meshesfile', os.getcwd()
    meshesfile = open('meshesfile','w')
#    meshesfile = open('meshesfile2','w')
    meshesfile.write('N target %i\n' % B.Nmesh)
    meshesfile.write('Format: pf then Nmesh then kmesh\n\n')    
    
    pflist = []
#    for pftry in frange(pfB/2,0.75,0.005):
    for pftry in frange(pfB/2,0.75,0.01):
#    for pftry in frange(.3,0.505,0.005):
        print '\nPacking fraction target',pftry
        B.pftarget = pftry  
        pf_orth=0; pf_orth2fcc=0; sym_orth = False; sym_orth2fcc = False
#'--------------------------------------------------------------------------------------------------------'
#'--------------------------------------------------------------------------------------------------------'
        M = zeros((3,3),dtype=int)
        ctest = []
        type = 'maxpfsym'; print type
        ctrials = [3]
        a = rint(Nmesh**(1/3.0));# f = int(Nmesh/a/a)
        randnums = zeros(9)
        print 'M scale a',a
        for c in ctrials:
#            ri = [randint(5) for i in range(9)]
#            M = array([[-a+ri[0], a/c +ri[1] , a/c+ri[2]],[a/c+ri[3],-a+ri[4],a/c+ri[5]],\
#                        [a/c+ri[6],a/c+ri[7],-a+ri[8]]])  #bcc like best avg pf on 50: 0.66

#HNF::::::
            M = array([[a, 0 , 0],
                       [a/c,a,0],\
                        [a/c,a/c,a]])  #bcc like best avg pf on 50: 0.66

#           
#            M = array([[-a+1, a/c , a/c],[a/c,-a,a/c],[a/c,a/c,-a-1]])  #bcc like best avg pf on 50: 0.66
#            M = array([[a, 0,0],[0,a,0],[0,0,a+3]])
#            M = array([[-16 ,  1 ,  5 ],  
#                [6 ,  -10 ,  5],   
#                [-6  , -1  , 6  ]])
#            M = array([[5, a/c , a/c],[a/c,0,a/c],[a/c,a/c,-5]]) #fcc like best avg pf on 50: 0.59
            M = rint(M * (B.Nmesh/abs(det(M)))**(1/3.0))
            print 'Start mesh trial'; print M              
#            [M,K] = findmin(M,B,type)
#            print 'Test trial M'; print M
            ctest.append(cost(M,B,type))
#        print'Trial costs',ctest                           
        cbest = ctrials[argmin(ctest)]
#        print'Best c', cbest       
        iternpf = 0
        itermaxnpf = 10
        itermaxsym = 5
     #        bestpf = 100
#        NPFcost = 100;delNPFcost = -1 #initial values
        type = 'maxpfsym'; print type
        delcost = -1; lowcost = 1000 #initialize
####       while iternpf<itermaxnpf and delNPFcost <0 and abs(delNPFcost)>0.1 :
        while iternpf<itermaxnpf and delcost < -0.1:
            oldcost = cost(M,B,type)
        #            NPFcost = cost(M,B,'maxpf')
#            delNPFcost = (NPFcost-oldNPFcost)/NPFcost :
            '''Here we let M vary in the search, but record pf and kvecs when we find min cost'''
#            print 'cost(N,PF):', cost(M,B,type)
     #        while not symm and and iternpf<itermax:
            M = rint(M * (B.Nmesh/abs(det(M)))**(1/3.0))
            print 'Scaled M';print M
            iternpf += 1
            print 'Iteration',type,iternpf, '**********'
            [M,K] = findmin(M,B,type) 
            M = rint(M)
            itersym = 0            
            symm = False
            while not symm and itersym <itermaxsym: 
                itersym += 1
                print 'Symmetry iteration', itersym, '-------'         
#                print 'Nmesh', abs(det(M)), 'packing', packingFraction(dot(B.vecs,inv(M)))
#                M = rint(minkM(M,B))#; print'Mink reduced M'; print M    
                for iop in range(B.nops):
                    M = rint(findmin_i(M,B,iop))
#                    M = rint(minkM(M,B))#; print'Mink reduced M'; print M 
                    if abs(det(M)-B.Nmesh)/B.Nmesh > 0.15: #how far off from target N
                        M = rint(M * (B.Nmesh/abs(det(M)))**(1/3.0))
                        print 'Scaled M';print M                    
                K = lattice();K.vecs = trimSmall(dot(B.vecs,inv(M)));K.det = abs(det(K.vecs)); K.Nmesh = B.det/K.det                                       
                symm = checksymmetry(K.vecs,B)
                print 'Symmetry check', symm
                if symm:
                    newcost = cost(M,B,type)
                    if newcost - lowcost < 0: 
                        lowcost = newcost;
                        print'New lowcost',newcost              
                        pf_maxpf = packingFraction(K.vecs)
                        sym_maxpf = True
                        kvecs_maxpf = K.vecs
     #                    if pf_maxpf<bestpf: bestpf = pf_maxpf; bestM = M
                    print 'Packing fraction', pf_maxpf, 'vs original B', pfB  
                    print 'Nmesh', K.Nmesh, 'vs target', B.Nmesh 
                delcost = cost(M,B,type) - oldcost
        #write to files

#        meshesfile.write('Packing fraction target %f\n' % pftry)
        if symm and pf_maxpf not in pflist:
            pflist.append(pf_maxpf)
#            meshesfile.write('Packing fraction achieved %f\n' % pf_maxpf)
            meshesfile.write('%12.8f  %8.3f \n' % (pf_maxpf,K.Nmesh)) 
#            meshesfile.write('M\n')
#            for i in range(3):
#                for j in range(3):
#                    meshesfile.write('%i6' %M[i,j])
#                meshesfile.write('\n')
##            meshesfile.write('\n') 
                    
#            meshesfile.write('k mesh\n')
            M = rint(dot(inv(K.vecs),B.vecs))
            for i in range(3):
                for j in range(3):
                    meshesfile.write('%i   ' % int(rint(M[i,j])))
                meshesfile.write('\n')
            meshesfile.write('\n') 
            meshesfile.flush()  

            M = rint(dot(inv(K.vecs),B.vecs)) #We assign K only when M is ideal, so remake the best M
            print 'Check M'
            print M
            print 'Check K'
            print K.vecs 
            print 'Check B'
            print B.vecs
            print 'Check pf'
            print packingFraction(K.vecs) 
            #create a dir and prepare for vasp run
            newdir = str(round(pf_maxpf,4))
            newpath = path + newdir + '/'
            if not os.path.isdir(newpath):
                os.system('mkdir %s' % newpath)
            os.chdir(newpath)
            os.system ('cp %s* %s' % (vaspinputdir,newpath))
            os.system ('cp %sPOSCAR %s' % (path,newpath))  
            print 'SKIPPING writekpts_vasp_M AND submission'
#            writekpts_vasp_M(newpath,B,M,K)
#            writekpts_vasp_pf(newpath,K.vecs,pf_maxpf,K.Nmesh)
            writejobfile(newpath)
           
#            subprocess.call(['sbatch', 'vaspjob']) #!!!!!!! Submit jobs
            os.chdir(path)                      
        else:
            'do nothing'
#            meshesfile.write('Failed symmetry\n\n')     
    meshesfile.close()        
    
 #  Summary     
    pfs = [pfB]
    pftypes = ['B_latt']  
    ks  = [B.vecs/a] #one solutions is to simply divide B by an integer
    if not (sym_minsv or sym_sv2fcc or sym_maxpf or pf_pf2fcc or sym_orth or sym_orth2fcc):
         meshtype = 'B_latt_revert' ; #status += 'MHPrevert;'
         K.vecs = B.vecs/a; K.det = abs(det(K.vecs)); K.Nmesh = abs(B.det/K.det)
         pfmax = packingFraction(K.vecs)
    else:     

         if sym_orth:
             pfs.append(pf_orth)
             pftypes.append('orth')
             ks.append(kvecs_orth)
         if sym_orth2fcc:
            pfs.append(pf_orth2fcc)
            pftypes.append('orth2fcc')
            ks.append(kvecs_orth2fcc)                     
         if sym_maxpf:
             pfs.append(pf_maxpf)
             pftypes.append('maxpf')
             ks.append(kvecs_maxpf)    
         if sym_pf2fcc:
             pfs.append(pf_pf2fcc)
             pftypes.append('pf2fcc')
             ks.append(kvecs_pf2fcc)                    
    pfmax = max(pfs)
    meshtype = pftypes[argmax(pfs)]
    K.vecs = ks[argmax(pfs)]; K.det = abs(det(K.vecs)); K.Nmesh = B.det/K.det
#    return [K.vecs, K.Nmesh, B.Nmesh, B.lattype, pfB, pf_orth, pf_orth2fcc, pf_maxpf, pf_minsv, pf_sv2fcc, pfmax, meshtype, fcctype(B),status]

    return [K.vecs, K.Nmesh, B.Nmesh, B.lattype, pfB, pf_orth, pf_orth2fcc, pf_maxpf, pf_pf2fcc, pfmax, meshtype, fcctype(B),cbest,status]
def bestmeshIter_vary_N(Blatt,Nmesh,path):
    '''The kmesh can be related to the reciprocal lattice B by  B = KM, where M is an integer 3x3 matrix
    So K = B Inv(M).  Change M one element at a time to minimize the errors in symmetry and the cost in S/V and Nmesh '''
    
    ##############################################################
    ########################## Script ############################
    vaspinputdir = '/fslhome/bch/cluster_expansion/alir/AFLOWDATAf1_50e/vaspinput/'
    M = zeros((3,3),dtype = int)
    S = zeros((3,3),dtype = fprec)
    B = lattice()
    A = lattice()
    K = lattice()
    status = ''
    pf_minsv = 0; pf_sv2fcc = 0; pf_maxpf = 0; pf_pf2fcc = 0; #kvecs_pf2fcc = identity(3)
    sym_maxpf = False;  sym_sv2fcc = False; sym_minsv = False; sym_pf2fcc = False
    a = rint(Nmesh**(1/3.0)); f = int(Nmesh/a/a)
    print 'Target mesh number', Nmesh
       
    B.vecs = Blatt/2/pi  #Don't use 2pi constants in reciprocal lattice here
#    B.pftarget = 0.7405 #default best packing fraction

    #############End BCT lattice
    eps = 1.0e-6

    B.Nmesh = Nmesh
    print 'B vectors (differ by 2pi from traditional)';print B.vecs #
    #print 'B transpose'; print transpose(B.vecs)
    B.det = det(B.vecs)
    print 'Det of B', B.det
    print 'Orth Defect of B', orthdef(B.vecs)
    print 'Surf/vol of B', surfvol(B.vecs)
    pfB = packingFraction(B.vecs)
    print 'Packing fraction of B:', pfB  
    [B.symops,B.nops] = getGroup(B.vecs)
    B.msymops = intsymops(B) #integer sym operations in B basis
#    print'Symmetry operators in basis of B'
#    for i in range:
#        print B.msymops[:,:,i];print 
#    printops_eigs(B)
    B.lattype = latticeType(B.nops)
    print 'Lattice type:', B.lattype
    A = lattice()
    A.vecs = trimSmall(inv(transpose(B.vecs)))
    [A.symops,A.nops] = getGroup(A.vecs)    
    A.msymops = intsymops(A)
    print 'Real space lattice A'; print A.vecs
    print 'Det A', det(A.vecs)
    pfA = packingFraction(A.vecs)
    print 'Packing fraction of A:', pfA    
    
    meshesfile = open('meshesfile','a')
    meshesfile.write('N target %i\n' % B.Nmesh)
    meshesfile.write('Format: pf then Nmesh then kmesh\n\n')    
    
    pflist = []
#    M0 = array([[2,   2,   2,],
#                    [2,   2,   -2],
#                    [-2,   2,   -2]])
#    
#      0.74050000    64.000 
#-5   1   -3   
#6   2   2   
#3   1   -3   
    M0 = array([[-5,   1, -3,],
                    [6,   2,   2],
                    [3,   1,   -3]])
    
#    for div in [256,128,64,32,16,8,4,2,1]:
#        print '\nDivisor',div
#        nMP = rint((Nmesh/div)**(1/3.0))
#        M = array([[nMP,0,0],[0,nMP,0],[0,0,nMP]]);
 

    for fac in [1,2,3,4,5,6,7,8]: 
        print '\nMultiplier',fac       
        
        M = fac*M0
#        M = array([[4,12,-4],
#                   [-11,4,-26],
#                   [-26,-4,-11]]);
        K = lattice();K.vecs = trimSmall(dot(B.vecs,inv(M)));K.det = abs(det(K.vecs)); K.Nmesh = B.det/K.det             
        print 'Number of points',det(M)
        print 'Check M'
        print M
        print 'Check K'
        print K.vecs 
        print 'Check B'
        print B.vecs
        print 'Check pf'
        print packingFraction(K.vecs) 
        #create a dir and prepare for vasp run
        newdir = str(K.Nmesh)
        newpath = path + newdir + '/'
        if not os.path.isdir(newpath):
            os.system('mkdir %s' % newpath)
        os.chdir(newpath)
        os.system ('cp %s* %s' % (vaspinputdir,newpath))
        os.system ('cp %sPOSCAR %s' % (path,newpath))  
        writekpts_vasp_M(newpath,B,M,K)
#             writekpts_vasp_pf(newpath,K.vecs,pf_maxpf,K.Nmesh)
        writejobfile(newpath)

#            print 'submitting job'            
        subprocess.call(['sbatch', 'vaspjob']) #!!!!!!! Submit jobs
        os.chdir(path)