Esempio n. 1
0
    def iterate(self, method="hessian", supercell=None, q=None, nrattle=0):
        """Returns a list of possible configurations, one for each eigenmode in the
        system, that has `supercell` is compatible with the specified `q`
        vector.

        Args:
            method (str): desired method for computing the eigenvalues and
              eigenvectors. Possible choices are :meth:`hessian` or
              :meth:`dynmat`.
            supercell (numpy.ndarray): supercell matrix to use in generating the
              configs.
            q (numpy.ndarray): q-vector that the resulting supercell should be
              compatible with.
            nrattle (int): number of additional, empty configs to include via
              :meth:`quippy.Atoms.rattle`.
        """
        if method == "hessian":
            dmd = self.hessian()
        elif method == "vasp_hessian":
            dmd = self.vasp_hessian()
        else:
            dmd = self.dynmat(supercell, q)

        hname = "hessian1"
        seed = quippy.Atoms()
        seed.copy_from(dmd["template"])
        result = quippy.AtomsList()
        result.append(dmd["template"])

        #Delete the force, energy and virial information from the copy, so that
        #we don't duplicate it all over.
        del seed.params["dft_energy"]
        del seed.params["dft_virial"]
        del seed.properties["dft_force"]

        for l, v in zip(dmd["eigvals"], dmd["eigvecs"]):
            atc = seed.copy()

            #Add this eigenvector to its own configuration.
            atc.add_property(hname, 0.0, n_cols=3)
            H = np.reshape(v.real, (atc.n, 3)).T
            setattr(atc, hname, H)

            #Same thing for the eigenvalue. Then save it to the group folder
            #structure.
            atc.params.set_value(hname, l)
            atc.params.set_value("n_hessian", 1)
            #atc.add_property("force", 0.0, n_cols=3)
            result.append(atc)

        for i in range(nrattle):
            atRand = seed.copy()
            quippy.randomise(atRand.pos, 0.2)
            result.append(atRand)

        # atz = seed.copy()
        # atz.add_property("dft_force", 0.0, n_cols=3)
        #atz.params.set_value("dft_energy",

        return result
Esempio n. 2
0
def main(suffix, fxyz, rc, species, nmax, lmax, awidth, nframes, cutoff_dexp,
         cutoff_scale):

    suffix = str(suffix)
    fxyz = str(fxyz)
    cutoff = float(rc)
    species = sorted([int(species) for species in species.split(',')])
    nmax = int(nmax)
    lmax = int(lmax)
    awidth = float(awidth)
    nframes = int(nframes)
    if nframes == 0: nframes = None
    nspecies = len(species)
    nn = nmax**2 * (lmax + 1)
    nab = nspecies * (nspecies + 1) / 2
    cutoff_dexp = int(cutoff_dexp)
    cutoff_scale = float(cutoff_scale)

    frames = qp.AtomsList(fxyz, stop=nframes)
    nframes = len(frames)

    soapstr = Template('average=F normalise=T soap cutoff_dexp=$cutoff_dexp \
                        cutoff_scale=$cutoff_scale central_reference_all_species=F \
                        central_weight=1.0 covariance_sigma0=0.0 atom_sigma=$awidth \
                        cutoff=$rc cutoff_transition_width=0.5 n_max=$nmax l_max=$lmax \
                        n_species=$nspecies species_Z=$species n_Z=$ncentres Z=$centres'
                       )
    soapstr = soapstr.safe_substitute(rc=rc, nmax=nmax, lmax=lmax, awidth=awidth, \
                                      cutoff_dexp=cutoff_dexp, cutoff_scale=cutoff_scale)
    soapstr = Template(soapstr)

    gsoaps = get_soaps(soapstr, rc, species, nspecies, nmax, lmax, nn, nab)
    soaps_list = gsoaps(frames)

    p = {}
    for i in xrange(nframes):
        p[i] = soaps_list[i]

    f = open(suffix + '.pckl', 'wb')
    pickle.dump(p, f)
    f.close()
Esempio n. 3
0
def main(filename, nmax, lmax, coff, cotw, gs, centerweight, prefix=""):
    start_time = datetime.now()

    filename = filename[0]
    # sets a few defaults
    if prefix == "": prefix = filename
    if prefix.endswith('.xyz'): prefix = prefix[:-4]
    prefix = prefix + "-n" + str(nmax) + "-l" + str(lmax) + "-c" + str(
        coff) + "-g" + str(gs)

    print >> sys.stderr, "using output prefix =", prefix
    # Reads input file using quippy
    print >> sys.stderr, "Reading input file", filename
    (first, last) = (0, 0)
    # reads the whole thing
    if first == 0: first = None
    if last == 0: last = None
    al = quippy.AtomsList(filename, start=first, stop=last)
    print >> sys.stderr, len(al.n), " Configurations Read"

    # determines "kit" (i.e. max n and kind of atoms present)
    spkit = {}
    for at in al:
        atspecies = {}
        for z in at.z:
            if z in atspecies: atspecies[z] += 1
            else: atspecies[z] = 1

        for (z, nz) in atspecies.iteritems():
            if z in spkit:
                if nz > spkit[z]: spkit[z] = nz
            else:
                spkit[z] = nz

    # species string
    zsp = spkit.keys()
    zsp.sort()
    lspecies = 'n_species=' + str(len(zsp)) + ' species_Z={ '
    for z in zsp:
        lspecies = lspecies + str(z) + ' '
    lspecies = lspecies + '}'
    print lspecies
    fout = open(prefix + ".soap", "w")
    for at in al:
        fout.write("%d\n" % (len(at.z)))
        at.set_cutoff(coff)
        at.calc_connect()
        nel = np.bincount(at.z)

        # ok, it appears you *have* to do this atom by atom. let's do that, but then re-sort in the same way as in the input
        soaps = {}
        sz = {}
        for z in at.z:
            if z in sz: sz[z] += 1
            else: sz[z] = 1
        for (z, nz) in sz.iteritems():
            soapstr = ("soap central_reference_all_species=F central_weight=" +
                       str(centerweight) +
                       "  covariance_sigma0=0.0 atom_sigma=" + str(gs) +
                       " cutoff=" + str(coff) + " cutoff_transition_width=" +
                       str(cotw) + " n_max=" + str(nmax) + " l_max=" +
                       str(lmax) + ' ' + lspecies + ' Z=' + str(z))
            desc = quippy.descriptors.Descriptor(soapstr)
            print soapstr
            soaps[z] = desc.calc(at)["descriptor"]
        for z in at.z:
            fout.write("%3s  " % (atomicno_to_sym(z)))
            np.savetxt(fout, [soaps[z][len(soaps[z]) - sz[z]]])
            sz[z] -= 1

        print sz

    fout.close()
Esempio n. 4
0
        # Wait for instructions from the master process.
        pool.wait()
        sys.exit(0)

    print_logo()

    print "Start dirty parallelisation for cluster " \
          "with a pool of {} mpi processes: {}".format(pool.size,ctime())

    print "Computing the global {} kernel of {} " \
      "from index {} to {} (None -> default, i.e. begining/end)".format(global_kernel_type, filename,first,last)

    # run commands in parallel
    st = time()

    frames = qp.AtomsList(filename,start=first,stop=last)
    Nframe = len(frames)

    if args.chunklen:
        aa = args.chunklen.split(',')
        if len(aa) > 1:
            xchunklen, ychunklen = int(aa[0]), int(aa[1])
        elif len(aa) == 1:
            xchunklen, ychunklen = int(aa[0]), int(aa[0])
        else:
            raise Exception('chunk lenght format not recognised')
    else:
        print 'Default chunk lenght'
        xchunklen, ychunklen = Nframe // 5, Nframe // 5

    xNchunk = Nframe // xchunklen
Esempio n. 5
0
    # format of the output name
    if prefix == "": prefix = filename
    if prefix.endswith('.xyz'): prefix = prefix[:-4]
    prefix += "-n" + str(nmax) + "-l" + str(lmax) + "-c" + str(cutoff) + \
              "-g" + str(gaussian_width) + "-cw" + str(centerweight) + \
              "-cotw" + str(cutoff_transition_width)

    fn_env_kernels = prefix + '-env_kernels.pck'

    print "using output prefix =", prefix

    st = time.time()
    ################################################################################
    # Reads the file and create a list of quippy frames object
    atoms1 = qp.AtomsList(filename, start=xlim[0], stop=xlim[1])
    print 'Reading {} input atomic structure from {} with index {}: done {}'.format(
        len(atoms1), filename, xlim, s2hms(time.time() - st))

    if xlim != ylim:
        atoms2 = qp.AtomsList(filename, start=ylim[0], stop=ylim[1])
        print 'Reading {} input atomic structure from {} with index {}: done {}'.format(
            len(atoms2), filename, ylim, s2hms(time.time() - st))
    else:
        atoms2 = None
        print 'no atoms 2, Computing upper triangular sub matrix'
    soap_params = {
        'centerweight': centerweight,
        'gaussian_width': gaussian_width,
        'cutoff': cutoff,
        'cutoff_transition_width': cutoff_transition_width,
Esempio n. 6
0
def main(filename, nmax, lmax, coff, cotw, gs, centerweight, gspecies, redfiles, prefix=""):
    start_time = datetime.now()
    filename = filename[0]
    # sets a few defaults
    if prefix=="": prefix=filename
    if prefix.endswith('.xyz'): prefix=prefix[:-4]
    prefix=prefix+"-n"+str(nmax)+"-l"+str(lmax)+"-c"+str(coff)+"-g"+str(gs)
    print >> sys.stderr, "using output prefix =", prefix
    # Reads input file using quippy
    print >> sys.stderr, "Reading input file", filename
    (first,last)=(0,0); # reads the whole thing 
    if first==0: first=None; 
    if last==0: last=None
    al = quippy.AtomsList(filename, start=first, stop=last);
    print >> sys.stderr, len(al.n) , " Configurations Read"
    
    # determines "kit" (i.e. max n and kind of atoms present)
    spkit = {}
    for at in al:
        atspecies = {}
        for z in at.z:      
            if z in atspecies: atspecies[z]+=1
            else: atspecies[z] = 1
            
        for (z, nz) in atspecies.iteritems():
            if z in spkit:
                if nz>spkit[z]: spkit[z] = nz
            else:
                spkit[z] = nz
    
    # species string 
    if gspecies is None:
        zsp=spkit.keys()
        zsp.sort()
        print "No species provided"
    else: zsp=map(int,gspecies.split(",")) 

    # If dimensionality reduction is called, stores the variables
    cur = False
    if redfiles is not None:
        cur = True
        # Does not matter in which sequence the files are provided
        try:
            idxsel = np.loadtxt(redfiles[0], dtype=int)
            chol = np.loadtxt(redfiles[1])
        except ValueError:
            idxsel = np.loadtxt(redfiles[1], dtype=int)
            chol = np.loadtxt(redfiles[0])

        # Checks if all dimensions are the same
        assert idxsel.shape[0] == chol.shape[0] == chol.shape[1], "Wrong dimensions, cannot perform dimensionality reduction"
        
    lspecies = 'n_species='+str(len(zsp))+' species_Z={ '
    for z in zsp: lspecies = lspecies + str(z) + ' '
    lspecies = lspecies + '}'   
    print "Using the following species:", ",".join([str(z) for z in zsp])
    if not cur:
        fout=open(prefix+".soap","w")
    else:
        fout=open(prefix+"_red.soap","w") # To avoid giving the same name to the reduced soap vector
    counter = 0
    for at in al:
        fout.write("%d\n" % (len(at.z)))
        at.set_cutoff(coff);
        at.calc_connect();
        nel = np.bincount(at.z)
        
        # ok, it appears you *have* to do this atom by atom. let's do that, but then re-sort in the same way as in the input
        soaps = {}
        sz = {}
        for z in at.z: 
            if z in sz: sz[z]+=1
            else: sz[z]=1
        for (z, nz) in sz.iteritems():
            soapstr=("soap central_reference_all_species=F central_weight="+str(centerweight)+
           "  covariance_sigma0=0.0 atom_sigma="+str(gs)+" cutoff="+str(coff)+" cutoff_transition_width="+str(cotw)+
           " n_max="+str(nmax)+" l_max="+str(lmax)+' '+lspecies+' Z='+str(z))
            desc = quippy.descriptors.Descriptor(soapstr )
            soaps[z] = desc.calc(at)["descriptor"]
        for z in at.z:
            fout.write("%3s  " % (atomicno_to_sym(z)))
            if cur:
                single_soap = soaps[z][len(soaps[z])-sz[z]]
                red_soap = np.dot(single_soap[idxsel], chol)
                np.savetxt(fout, [red_soap])
            else: np.savetxt(fout, [ soaps[z][len(soaps[z])-sz[z]] ])
            sz[z] -=1
        counter +=1
        sys.stderr.write("SOAP vectors calculated: %d / %d   \r" %(counter, len(al.n)) )
    sys.stderr.write("\n")
        
    fout.close() 
Esempio n. 7
0
                    default=1.0 * units.fs)
parser.add_argument('-dt_traj',
                    '--traj_interval',
                    help='Trajectory write frequency',
                    type=int,
                    default=10)
# ***** End input parameters *****

params = parser.parse_args()

calc = quippy.Potential('IP TS', param_filename="ts_params.xml")
tipatoms = params.tipatoms

folder, config_filename = os.path.split(params.config_filename)
os.chdir(folder)
at = quippy.AtomsList(config_filename, start=-1)[0]

# automatically set spring constant to default Gc value if not manually set
if params.k_spring == -1:
    if 'c' in folder:
        params.k_spring = 0.35
    elif 'a' in folder:
        params.k_spring = 0.80
        print("Inferred k_spring %f" % params.k_spring)
    else:
        print("Please specify the spring constant.")
        sys.exit(1)

print("Running MD with following parameters:")
print(params)
Esempio n. 8
0
def main(fnamexyz,
         cutoffdist,
         fnamesim,
         zenv,
         nenv,
         dcut,
         selectgroupxyz2print=[],
         dcutisgood=False,
         isploting=False):

    pyplot = isploting
    plotdendo = True

    # Generate the file name for the new dist matrices that are without dummy atoms
    nsim = len(nenv)
    fname = []
    fnamecorr = []
    fnamemathematica = []
    cnt = 0
    for fn in fnamesim:
        target = '_' + str(nenv[cnt])
        fname.append(fn[:fn.find(target)] + '.sim')
        fnamecorr.append(fn[:fn.find(target)] + '.corrmt')
        fnamemathematica.append(fn[:fn.find(target)] +
                                '.mathematica-cluster.dat')
        cnt += 1
        print(fn[:fn.find(target)])

    print >> sys.stderr, "Reading input file", fnamexyz
    mols = quippy.AtomsList(fnamexyz)
    print >> sys.stderr, len(mols.n), " Configurations Read"

    distmatrix = []
    clist = []
    nbclst = []
    clusterlist = []
    nspiecies = []
    Z = []
    dendo = []

    for it in range(nsim):
        print '############'
        print "Read distance matrix " + fnamesim[it] + " with dummy atoms"
        olddistmatrix = np.loadtxt(fnamesim[it])

        fsim = open(fnamesim[it], 'r')
        head = fsim.readline()
        head = head.strip('#')
        head = head.strip('\n')
        # print '##########' , head
        fsim.close()

        # Removes dummy atoms rows/columns from distance matrix
        newdistmatrix1, dummylist = env_corr.rmdummyfromsim(
            fnamexyz, olddistmatrix, zenv[it], nenv[it])
        print len(newdistmatrix1), ' Real ', zenv[it], ' atom '
        print 'Removes : ', len(dummylist), ' rows/colomns from distmatrix'
        print newdistmatrix1.shape

        # Write the distmatrix without dummy atoms
        np.savetxt(fname[it], newdistmatrix1, header=head)

        # get list of the cluster groups idx (same order as in dist mat)
        clist1, Z1 = env_corr.clusterdistmat(fname[it],
                                             newdistmatrix1,
                                             0.,
                                             mode='average',
                                             plot=plotdendo)
        dendo1 = sc.dendrogram(Z1)
        # Write mathematica cluster structure
        cluster.mathematica_cluster(Z1, newdistmatrix1, fnamemathematica[it])

        # get nb of cluster groups
        nbclst1 = len(np.unique(clist1))
        # print a==nbclst1
        distmatrix.append(newdistmatrix1)
        clist.append(clist1)
        nbclst.append(nbclst1)
        Z.append(Z1)
        dendo.append(dendo1)

        # Link the cluster groups with atom's frame and respective position in it
        if dcutisgood:
            clusterlist1, nspiecies1 = env_corr.linkgroup2atmidx(
                mols, clist1, zenv[it], nenv[it])
            clusterlist.append(clusterlist1)
            # count nb of species atom in each frame
            nspiecies.append(nspiecies1)

    # Compute the correlation between the species of 2 cluster
    # look for the groups of the atoms (from cluster2) in the environment of an
    # atom from a group from cluster1
    if dcutisgood:
        corrmtx = []
        for it in range(1, nsim):
            if len(selectgroupxyz2print) == 0:
                selectgroupxyz2print = []
                for i in range(1, nsim + 1):
                    selectgroupxyz2print.append([])

            corrmtx1, corr1, idx1, idx2 = env_corr.getcorrofcluster(
                mols, cutoffdist, fnamexyz, clusterlist[0], clusterlist[it],
                nbclst[0], nbclst[it], zenv[0], zenv[it], nspiecies[0],
                nspiecies[it], selectgroupxyz2print[it - 1])

            # idx1 = dendo[0]['leaves']
            # idx2 = dendo[it]['leaves']
            print len(idx1), len(idx2)
            print corrmtx1.shape
            # corrmtx1 = corrmtx1[idx1,:]
            # corrmtx1 = corrmtx1[:,idx2]

            corrmtx.append(corrmtx1)
            head1 = 'From ' + fnamexyz + '  Correlation matrix between environment of atom ' + str(
                zenv[0]) + ' and ' + str(zenv[it])
            print '############'
            print head1
            head2 = 'From ' + fnamexyz + '  Dendogram ordering of atom ' + str(
                zenv[0])
            head3 = 'From ' + fnamexyz + '  Dendogram ordering of atom ' + str(
                zenv[it])

            np.savetxt(fnamecorr[it], corrmtx1, header=head1)
            np.savetxt(fnamecorr[it] + str(zenv[0]), idx1, header=head2)
            np.savetxt(fnamecorr[it] + str(zenv[it]), idx2, header=head3)

            # print corrmtx1

    if pyplot:
        plt.show()
Esempio n. 9
0
debug = True
save_traj = True
n_steps_heat = 20000
n_steps_equil = 10000
n_steps_liquid = 5000
T_melt = 5000.0
T_equil = 2000.0

liquid_traj_filename = "model-" + model.name + "-test-liquid-liquid_traj.xyz"

if __builtin__.do_io:
    print liquid_traj_filename

try:
    liquid_traj = quippy.AtomsList(liquid_traj_filename)
    if __builtin__.do_io:
        print "read %s from file" % liquid_traj_filename
except:
    if __builtin__.do_io:
        print "generating %s" % liquid_traj_filename
    if 'LIQUID_NO_RUN' in os.environ:
        sys.exit(1)

    # specify that we will use model.calculator to compute forces, energies and stresses
    bulk.set_calculator(model.calculator)
    print "doing bulk"
    print bulk.get_potential_energy()
    print "done"
    try:
        model.calculator.print_()
Esempio n. 10
0
def main():
    parser = argparse.ArgumentParser(
        description="""Computes the similarity matrix between a set of atomic structures based on SOAP descriptors and an optimal assignment of local environments.""")

    parser.add_argument("filename", nargs=1, help="Name of the formatted xyz input file")
    parser.add_argument("--kit", type=str, default="None", help="Dictionary-style kit specification (e.g. --kit '{4:1,6:10}' or 'auto' or 'None' (default)")
    parser.add_argument("--continue_from", type=str, default=None, help="Load kernel and compute missing (nan) entries")

    args = parser.parse_args()


    with open("alchemy.pickle","rb") as alchem_file:
        alchem_rules = pickle.load(alchem_file)
    #from collections import defaultdict
    #alchem_rules = defaultdict(lambda: 1) # TODO: WARNING. Using 1 as alchem rule
    #print "ALCHEM RULES ARE 1"

    xyz_file = args.filename[0]
    kernel_file = os.path.splitext(xyz_file)[0] + ".k.pkz"

    # Load xyz file with molecules and convert to quippy format
    all_mols = quippy.AtomsList(xyz_file)

    # Determine kit to use
    kitstr = args.kit
    if kitstr == "auto":
        kit = getkit(all_mols)
        print "Using kit %s" % kit
    elif kitstr != "None" and kitstr != "none":
        kit = ast.literal_eval(str(args.kit))
        print "Using kit %s" % kit
    else:
        kit = None


    # Compute descriptor for each molecule
    print "Computing descriptors"
    descriptors = []
    for mol in all_mols:
        descriptors.append(PickableMol(SoapMol(alchem_rules, mol, kit = kit)))

    # Create kernel matrix
    N = len(all_mols)
    if args.continue_from is None:
        kernel_matrix = np.ones((N, N)) * np.nan
    else:
        with gzip.open(args.continue_from, "rb") as f:
            kernel_matrix = pickle.load(f)
        assert kernel_matrix.shape == (N,N), "Can't continue, loaded kernel has wrong dimension"
        assert not np.any(np.isnan(np.diag(kernel_matrix))), "Can't continue, diagonal has missing elements"
        assert not np.any(np.diag(kernel_matrix) == 1.0), "Can't continue if diagonal already normalised"

    # Setup multiprocessing
    job_queue = multiprocessing.Queue()
    res_queue = multiprocessing.Queue()
    num_processes = 4
    chunk_size = 100
    usekit = (kit is not None)
    if usekit:
        alchem = get_alchem_mat(alchem_rules, kit)
    else:
        alchem = alchem_rules
    workers = [
            multiprocessing.Process(
                target=worker, args=(job_queue, res_queue, alchem, descriptors, usekit)) \
            for _ in range(num_processes)
            ]

    # Start all workers
    for w in workers:
        w.start()

    # First compute unnormalised diagonal (used for normalisation)
    if args.continue_from is None:
        print "Computing diagonal"
        for (row, col) in zip(range(N), range(N)):
            job_queue.put([(row, col),])
        # Retrieve results
        num_jobs_finished = 0
        while num_jobs_finished < N:
            row, col, res = res_queue.get()
            kernel_matrix[row,col] = res
            num_jobs_finished += 1
    else:
        print "Diagonal already computed, skipping"

    # Compute off-diagonal elements (with normalisation)
    print "Computing off-diagonal elements"
    # Submit jobs to queue
    if args.continue_from is None:
        all_jobs = list(itertools.combinations(range(N),2))
    else:
        unfinished = np.transpose(np.nonzero(np.isnan(kernel_matrix)))
        all_jobs = [(row, col) for row, col in unfinished if row<col]

    for chunk in grouper(chunk_size, all_jobs, (None, None)):
        job_queue.put(chunk)

    # Retrieve results
    num_jobs_finished = 0
    total_jobs = len(all_jobs)
    while num_jobs_finished < total_jobs:
        row, col, res = res_queue.get()
        inorm = 1.0/np.sqrt(kernel_matrix[row,row]*kernel_matrix[col,col])
        kernel_matrix[row,col] = res*inorm
        kernel_matrix[col,row] = res*inorm
        num_jobs_finished += 1
        if (num_jobs_finished % 10000) == 0:
            print "%s job %d/%d complete (%d%%)" %\
                (time.strftime("%Y-%m-%d %H:%M"),
                num_jobs_finished,
                total_jobs,
                100.0*float(num_jobs_finished)/float(total_jobs))
        if (num_jobs_finished % 1000000) == 0:
            # Save kernel matrix to file
            with gzip.open(kernel_file, "wb") as f:
                pickle.dump(kernel_matrix, f)

    print "Done computing off-diagonal, saving kernel"
    # Diagonal is normalised to unity
    kernel_matrix[np.diag_indices(N)] = 1.0

    # Save kernel matrix to file
    with gzip.open(kernel_file, "wb") as f:
        pickle.dump(kernel_matrix, f)

    print "Joining workers"
    for w in workers:
        job_queue.put([(None, None),])
    for w in workers:
        w.join()