예제 #1
0
def test_optimizer(systems, optimizer, calculator, prefix='', db=None,
                   eggbox=0.0):
    """Test optimizer on systems."""

    for name, atoms in systems:
        if db is not None:
            optname = optimizer.__name__
            id = db.reserve(optimizer=optname, name=name)
            if id is None:
                continue
        atoms = atoms.copy()
        tag = '{}{}-{}'.format(prefix, optname, name)
        atoms.calc = calculator(txt=tag + '.txt')
        error, nsteps, texcl, tincl = run_test(atoms, optimizer, tag,
                                               eggbox=eggbox)

        if db is not None:
            db.write(atoms,
                     id=id,
                     optimizer=optname,
                     name=name,
                     error=error,
                     n=nsteps,
                     t=texcl,
                     T=tincl,
                     eggbox=eggbox)
    def write(self, outfile: Union[str, BinaryIO, TextIO]):
        """
        Writes structure container to a file.

        Parameters
        ----------
        outfile
            output file name or file object
        """
        # Write cluster space to tempfile
        temp_cs_file = tempfile.NamedTemporaryFile()
        self.cluster_space.write(temp_cs_file.name)

        # Write fit structures as an ASE db in tempfile
        temp_db_file = tempfile.NamedTemporaryFile()
        if self._structure_list:
            db = ase.db.connect(temp_db_file.name, type='db', append=False)

        for fit_structure in self._structure_list:
            data_dict = {
                'user_tag': fit_structure.user_tag,
                'properties': fit_structure.properties,
                'cluster_vector': fit_structure.cluster_vector
            }
            db.write(fit_structure.structure, data=data_dict)

        with tarfile.open(outfile, mode='w') as handle:
            handle.add(temp_db_file.name, arcname='database')
            handle.add(temp_cs_file.name, arcname='cluster_space')
예제 #3
0
def prepare_db():
    atoms = bulk("Al", a=4.15)
    atoms = atoms * (4, 4, 4)
    for i in range(0, 42):
        if (i < 21):
            atoms[i].symbol = "Si"
        else:
            atoms[i].symbol = "Mg"

    symbols = [atom.symbol for atom in atoms]
    random.shuffle(symbols)
    for symb, atom in zip(symbols, atoms):
        atom.symbol = symb

    db = ase.db.connect(db_name())
    for i in range(8):
        db.write(atoms)
예제 #4
0
def store_node(args, dirname, names):
    db_filenaem, pub_data = args

    db = ase.db.connect(db_filename)
    for name in names:
        if name.endswith('.traj'):
            sys.stdout.write('.')
            dsplit = dirname.split('/')
            if len(dsplit) == 6:  #adsorbed structure
                #print(len(dsplit))
                publication, dft_code, dft_functional, reaction, substrate, facet = dsplit
            elif len(dsplit) == 4:  # gas phase molecule
                publication, dft_code, dft_functional, reaction = dsplit
                substrate, facet = '', ''
                # have to use string because ase.db does not accept None
            else:
                continue

            if not facet == 'None':
                facet = '({facet})'.format(**locals())
            username = publication.split('_')[0]

            atoms = ase.io.read(os.path.join(dirname, name))
            adsorbate = os.path.splitext(name)[0]
            db.write(
                atoms,
                publication=publication,
                dft_code=dft_code,
                dft_functional=dft_functional,
                reaction=reaction,
                substrate=substrate,
                facet=facet,
                username=username,
                adsorbate=adsorbate,
                publication_volume=pub_data.get('volume', ''),
                publication_publisher=pub_data.get('publisher', ''),
                publication_doi=pub_data.get('doi', ''),
                publication_title=pub_data.get('title', ''),
                publication_url=pub_data.get('url', ''),
                publication_journal=pub_data.get('journal', ''),
                publication_authors=pub_data.get('authors', ''),
                publication_year=pub_data.get('year', ''),
                publication_number=pub_data.get('number', ''),
                publication_pages=pub_data.get('pages', ''),
            )
예제 #5
0
def main():
    all_atoms, is_common_arr = get_all_atoms()
    common_atoms = [a for a, c in zip(all_atoms, is_common_arr) if c]
    uncommon_atoms = [a for a, c in zip(all_atoms, is_common_arr) if not c]

    fold_id = generate_folds(len(common_atoms), 5)

    print("Writing to DB")
    with ase.db.connect(os.path.join(msgnet.defaults.datadir, "matproj.db"),
                        append=False) as db:
        for atom_keyval, fold in zip(common_atoms, fold_id):
            atom = atom_keyval[0]
            keyval = atom_keyval[1]
            keyval["fold"] = int(fold)
            db.write(atom, key_value_pairs=keyval)
        for atom, keyval in uncommon_atoms:
            keyval["fold"] = "None"
            db.write(atom, key_value_pairs=keyval)
예제 #6
0
def dump_bulks(output_file_name='bulks.db'):
    with get_mongo_collection('atoms') as collection:
        docs = list(tqdm(collection.find({'fwname.calculation_type': 'unit cell optimization'}),
                         desc='pulling from FireWorks'))

    db = ase.db.connect(output_file_name)
    for doc in tqdm(docs, desc='writing to database'):
        atoms = make_atoms_from_doc(doc)
        _ = db.write(atoms, mpid=doc['fwname']['mpid'])
예제 #7
0
def main():
    atoms = bulk("Al", crystalstructure="fcc", a=4.6, cubic=False)
    atoms = atoms * (2, 2, 2)
    print(len(atoms))
    db = ase.db.connect("test_db.db")
    tid = db.write(atoms,
                   name="testCase%d" % (np.random.randint(0, 1000000000)),
                   started=False,
                   queued=False)
    print("Test ID: %d" % (tid))
예제 #8
0
 def write(self, skip=[], goto_reaction=None):
     for key_values in self.read(skip=skip, goto_reaction=goto_reaction):
         with CathubSQLite(self.cathub_db) as db:
             id = db.check(key_values['chemical_composition'], key_values['reaction_energy'])
             #print('Allready in reaction db with row id = {}'.format(id))
             if id is None:
                 id = db.write(key_values)
                 print('Written to reaction db row id = {}'.format(id))
             elif self.update:                    
                 db.update(id, key_values)
                 print('Updated reaction db row id = {}'.format(id))
             else:
                 print('Allready in reaction db with row id = {}'.format(id))
예제 #9
0
파일: mp.py 프로젝트: utf/materials-toolbox
def save_structures(entries, ids, cif=False, conv=False, ref=False, db=None):
    """
    Args:
        ids (list): indexes of the entries to save
    """
    fmt = 'cif' if cif else 'poscar'
    if db:
        db = ase.db.connect(db)
    for i in ids:
        e = entries[i - 1]
        sym = SpacegroupAnalyzer(e.structure)
        if conv:
            struct = sym.get_conventional_standard_structure()
        else:
            struct = sym.get_primitive_standard_structure()

        if db is not None:
            db.write(AseAtomsAdaptor.get_atoms(struct))
        else:
            formula = e.data['pretty_formula']
            filename = 'POSCAR_{}_{}'.format(formula, i)
            struct.to(filename=filename, fmt=fmt)
예제 #10
0
def test_optimizer(systems, optimizer, calculator, prefix='', db=None):
    for atoms in systems:
        formula = atoms.get_chemical_formula()
        if db is not None:
            optname = optimizer.__name__
            id = db.reserve(optimizer=optname, name=formula)
            if id is None:
                continue
        atoms = atoms.copy()
        tag = '{}{}-{}'.format(prefix, optname, formula)
        atoms.calc = calculator(txt=tag + '.txt')
        error, nsteps, texcl, tincl = run_test(atoms, optimizer, tag)

        if db is not None:
            db.write(atoms,
                     id=id,
                     optimizer=optname,
                     name=formula,
                     error=error,
                     n=nsteps,
                     t=texcl,
                     T=tincl)
예제 #11
0
def main(argv):
    relax_atoms = (argv[1] == "atoms")
    runID = int(argv[0])
    print("Running job: %d" % (runID))
    db_name = db_name_atoms
    #db_name = "/home/ntnu/davidkl/Documents/GPAWTutorials/ceTest.db"
    db = ase.db.connect(db_name)

    new_run = not db.get(id=runID).key_value_pairs["started"]
    # Update the databse
    db.update(runID, started=True, converged=False)

    atoms = db.get_atoms(id=runID)

    calc = EAM(potential="/home/davidkl/Documents/EAM/mg-al-set.eam.alloy")
    atoms.set_calculator(calc)
    init_energy = atoms.get_potential_energy()

    logfile = "CE_eam/ceEAM%d.log" % (runID)
    traj = "CE_eam/ceEAM%d.traj" % (runID)
    trajObj = Trajectory(traj, 'w', atoms)

    if (relax_atoms):
        relaxer = BFGS(atoms, logfile=logfile)
        relaxer.attach(trajObj)
        relaxer.run(fmax=0.025)
        energy = atoms.get_potential_energy()
    else:
        res = minimize(target_function, x0=4.05, args=(atoms, ))
        a = res["x"]
        atoms = set_cell_parameter(atoms, a)
        energy = atoms.get_potential_energy()
        print("Final energy: {}, final a_la: {}".format(energy, a))
    row = db.get(id=runID)
    del db[runID]
    kvp = row.key_value_pairs
    kvp["init_energy"] = init_energy
    runID = db.write(atoms, key_value_pairs=kvp)
    db.update(runID, converged=True)
    print("Energy: %.2E eV/atom" % (energy / len(atoms)))
    print("Initial energy: %.2E eV/atom" % (init_energy / len(atoms)))
예제 #12
0
def add_vasp_structure(dbname, folder, system_id=''):
   """Add a VASP structure into a database.

   Keyword arguments:
   dbname -- database filename
   folder -- path with VASP outputs
   system_id -- type of calculation you performed

   """
   import sys
   import os
   import re
   import ase.db
   from ase.io import read
   from ase.io.vasp import read_vasp_out


   start_dir = os.path.realpath(".")
   db = ase.db.connect(dbname)
   os.chdir(folder)

   structure = read_vasp_out("OUTCAR")

   fin = open("INCAR"); INCAR = fin.readlines(); fin.close()
   fin = open("KPOINTS"); KPOINTS = fin.readlines(); fin.close()
   fin = open("POTCAR");  POTCARf = fin.read();      fin.close();
   POTCAR = re.findall('TITEL  = (.*)',POTCARf)
   fin = open("CONTCAR"); CONTCAR= fin.readlines(); fin.close()

   datain = {"INCAR" : INCAR,
             "KPOINTS" : KPOINTS,
             "POTCAR_TITEL" : POTCAR,
             "CONTCAR" : CONTCAR}

   str_path = os.path.realpath(".")
   #str_system = str(type_system)
   os.chdir(start_dir)

   row_id = db.write(structure,data=datain, structure_path=str_path,system_id=system_id)
   print ("Structure added with id:", row_id)
   return row_id
예제 #13
0
def add_VASP_structure(dbname, folder):
    #-#
    from ase.io import read
    from ase.io.vasp import read_vasp_out
    import ase.db
    import sys, os, re

    start_dir = os.path.realpath(".")
    db = ase.db.connect(dbname)
    os.chdir(folder)

    structure = read_vasp_out("OUTCAR")

    fin = open("INCAR")
    INCAR = fin.readlines()
    fin.close()
    fin = open("KPOINTS")
    KPOINTS = fin.readlines()
    fin.close()
    fin = open("POTCAR")
    POTCARf = fin.read()
    fin.close()
    POTCAR = re.findall('TITEL  = (.*)', POTCARf)
    fin = open("CONTCAR")
    CONTCAR = fin.readlines()
    fin.close()

    datain = {
        "INCAR": INCAR,
        "KPOINTS": KPOINTS,
        "POTCAR_TITEL": POTCAR,
        "CONTCAR": CONTCAR
    }
    str_path = os.path.realpath(".")
    os.chdir(start_dir)

    row_id = db.write(structure, data=datain, structure_path=str_path)
    print("Structure added with id:", row_id)
    return row_id
예제 #14
0
def test_db(name):
    print(name)
    db = ase.db.connect(name, append=False)
    db.write(Atoms(), x=1, data={'a': 1})
    db.update(1, y=2, data={'b': 2})
    db.update(1, delete_keys=['x'])
    row = db.get(1)
    print(row.y, row.data)
    assert 'x' not in row
    db.update(1, atoms=Atoms('H'))
    row = db.get(1)
    print(row.y, row.data, row.numbers)
    assert (row.numbers == [1]).all()
    assert sorted(row.data) == ['a', 'b']

    db.write(Atoms(), id=1)
    row = db.get(1)
    assert len(row.data) == 0
    assert len(row.key_value_pairs) == 0
    assert len(row.numbers) == 0

    # N = 100
    N = 5
    for i in range(N):
        db.write(Atoms('H10'), i=i, data={'c': 3})

    t0 = time()
    for id in range(2, 2 + N):
        db.update(id, z=3)
    print(time() - t0)

    # This should be faster for large N:
    t0 = time()
    with db:
        for id in range(2, 2 + N):
            db.update(id, z=3)
    print(time() - t0)
예제 #15
0
from time import time
import ase.db
from ase import Atoms

for name in ['x.json', 'x.db']:
    print(name)
    db = ase.db.connect(name, append=False)
    db.write(Atoms(), x=1, data={'a': 1})
    db.update(1, y=2, data={'b': 2})
    db.update(1, delete_keys=['x'])
    row = db.get(1)
    print(row.y, row.data)
    assert 'x' not in row
    db.update(1, atoms=Atoms('H'))
    row = db.get(1)
    print(row.y, row.data, row.numbers)
    assert (row.numbers == [1]).all()
    assert sorted(row.data) == ['a', 'b']

    db.write(Atoms(), id=1)
    row = db.get(1)
    assert len(row.data) == 0
    assert len(row.key_value_pairs) == 0
    assert len(row.numbers) == 0

    # N = 100
    N = 5
    for i in range(N):
        db.write(Atoms('H10'), i=i, data={'c': 3})

    t0 = time()
예제 #16
0
def main(argv):
    print("Dry-run", gp.dry_run)
    if (len(argv) > 2):
        print("Usage: python almg.py paramID")
        return

    possible_dbs = {
        "vilje": "/home/ntnu/davidkl/GPAWTutorial/Exercises/AlMg/AlMg.db",
        "laptop": "AlMg.db"
    }

    # Find which database to use
    db_name = None
    for key in possible_dbs:
        if (os.path.isfile(possible_dbs[key])):
            db_name = possible_dbs[key]
            break
    if (db_name is None):
        raise RuntimeError("Could not find database")

    #db_name = "/home/ntnu/davidkl/GPAWTutorial/Exercises/AlMg/AlMg.db"
    db = ase.db.connect(db_name)
    runID = int(argv[0])

    # Read parameters from the database
    con = sq.connect(db_name)
    cur = con.cursor()
    cur.execute(
        "SELECT hspacing,relax,atomID,kpts,nbands,latticeConst,cutoff,traj,fmax,tags FROM runs WHERE ID=?",
        (runID, ))
    params = cur.fetchall()[0]
    con.close()

    save_pov = False
    run_sim = True
    swap_atoms = False
    useOnlyUnitCellFilter = True
    repeatFCCStructure = True  # If False, ASE find_optimal_cell_shape will be used to create the unit cell
    h_spacing = params[0]
    relax = params[1]
    atom_row_id = params[2]
    Nkpts = params[3]
    nbands = params[4]
    cutoff = params[6]
    old_traj = params[7]
    fmax = params[8]
    tags = params[9]

    # Lattice parameter
    a = float(params[5])

    if (old_traj != "none"):
        # Read the atoms from the trajectory file
        print("Reading atoms from trajectory file")
        traj = Trajectory(old_traj)
        atoms = traj[-1]
    elif (atom_row_id < 0):
        print(
            "Building supercell using find_optimal_cell_shape_pure_python from ASE"
        )
        # Target primitive cell
        atoms = build.bulk("Al", crystalstructure="fcc", a=a)

        # Create a supercell consisting of 32 atoms
        if (not "test" in tags):
            # Skip this if the run is a test run. For some reason the target_shape="fcc" does not work
            # using sc instead
            if (repeatFCCStructure):
                atoms = atoms * (4, 4, 2)
            else:
                P = build.find_optimal_cell_shape_pure_python(
                    atoms.cell, 32, "sc")
                atoms = build.make_supercell(atoms, P)

        # Replace some atoms with Mg atoms
        n_mg_atoms = int(0.2 * len(atoms))

        for i in range(n_mg_atoms):
            atoms[i].set("symbol", "Mg")
    else:
        print("Reading atoms object from the database")
        # Read atoms from database
        atoms = db.get_atoms(selection=atom_row_id)

    if (swap_atoms):
        from ase.io import write
        for i in range(0, 2 * len(atoms)):
            first = np.random.randint(0, len(atoms))
            second = np.random.randint(0, len(atoms))
            firstSymbol = atoms[first].symbol
            atoms[first].symbol = atoms[second].symbol
            atoms[second].symbol = firstSymbol
        atoms.write("almg_swap.xyz")

    if (save_pov):
        from ase.io import write
        write("Al.pov", atoms * (3, 3, 1), rotation="-10z,-70x")
        return

    if (run_sim):
        kpts = {"size": (Nkpts, Nkpts, Nkpts), "gamma": True}  # Monkhorst pack
        kpts = (Nkpts, Nkpts, Nkpts)

        if (cutoff > 0):
            mode = PW(cutoff)
        else:
            mode = "fd"
        #calc = GPAW( mode="fd", h=h_spacing, xc="PBE", nbands=nbands, kpts=kpts, basis="dzp", poissonsolver=PoissonSolver(relax="GS", eps=1E-7) )
        densityConv = 1E-2  # Default 1E-4
        wfsConv = 5E-3
        cnvg = {"density": 1E-2, "eigenstates": 5E-3}
        calc = GPAW(mode=mode, xc="PBE", nbands=nbands, kpts=kpts)
        calc.set(convergence=cnvg)
        atoms.set_calculator(calc)

        logfile = "none"
        trajfile = "none"
        if (relax):
            from ase.optimize import QuasiNewton, BFGS
            #from ase.optimize.precon import PreconLBFGS

            uid = rnd.randint(0, 10000000)
            # First relax only the unit cell
            logfile = "relaxation_%d.log" % (uid)
            trajfile = "trajectory_%d.traj" % (uid)

            print("Logfile: %s, Trajectory file: %s" % (logfile, trajfile))

            traj = Trajectory(trajfile, 'w', atoms)

            if (not useOnlyUnitCellFilter):
                for num_kpts in [Nkpts]:
                    kpts = (num_kpts, num_kpts, num_kpts)
                    calc.set(kpts=kpts)
                    energy = atoms.get_potential_energy()

                    # Relax unit cell
                    strfilter = StrainFilter(atoms)
                    relaxer = BFGS(strfilter, logfile=logfile)
                    relaxer.attach(traj)
                    convergence = np.abs(0.01 * energy)
                    relaxer.run(
                        fmax=convergence
                    )  # NOTE: Uses generalized forces = volume*stress

                    # Relax atoms within the unit cell
                    relaxer = BFGS(atoms, logfile=logfile)
                    relaxer.attach(traj)
                    relaxer.run(fmax=fmax)
            else:
                # Optimize both simultaneously
                uf = UnitCellFilter(atoms)
                relaxer = PreconLBFGS(uf, logfile=logfile)
                relaxer.attach(traj)
                relaxer.run(fmax=fmax)

        energy = atoms.get_potential_energy()
        print("Energy %.2f eV/atom" % (energy))
        lastID = db.write(atoms, relaxed=True)

        # Update the database
        con = sq.connect(db_name)
        cur = con.cursor()
        cur.execute(
            "UPDATE runs SET status='finished',resultID=?,logfile=?,traj=? WHERE ID=?",
            (lastID, logfile, trajfile, runID))
        con.commit()
        con.close()
예제 #17
0
def main(argv):
    relax_mode = "both"  # both, cell, positions
    system = "AlMg"
    runID = int(argv[0])
    nkpt = int(argv[1])

    single_point = False
    if (len(argv) >= 3):
        single_point = (int(argv[2]) == 1)
    print("Running job: %d" % (runID))
    db_paths = [
        "/home/ntnu/davidkl/GPAWTutorial/CE/almg_fcc_vac.db",
        "almg_fcc_vac.db", "/home/davidkl/GPAWTutorial/CE/almg_fcc_vac.db"
    ]
    for path in db_paths:
        if (os.path.isfile(path)):
            db_name = path
            break
    #db_name = "almgsi_test_db.db"
    db = ase.db.connect(db_name)
    name = db.get(id=runID).key_value_pairs["name"]
    new_run = not db.get(id=runID).key_value_pairs["started"]

    # Update the databse
    db.update(runID, started=True, converged=False)
    db.update(runID, nkpt=nkpt)

    atoms = db.get_atoms(id=runID)
    atoms = delete_vacancies(atoms)

    if (len(atoms) == 1):
        nbands = -10
    else:
        nbands = "120%"
    kpts = (nkpt, nkpt, nkpt)
    try:
        restart_name = SaveRestartFiles.restart_name(name)
        atoms, calc = gp.restart(restart_name)
    except:
        calc = gp.GPAW(mode=gp.PW(500), xc="PBE", kpts=kpts, nbands=nbands)
        atoms.set_calculator(calc)

    if (single_point):
        calc = gp.GPAW(mode=gp.PW(500), xc="PBE", kpts=kpts, nbands=nbands)
        atoms.set_calculator(calc)

    logfile = "almg_fcc_vac{}.log".format(name)
    traj = "almg_bcc{}.traj".format(name)
    db.update(runID, trajfile=traj)
    trajObj = Trajectory(traj, 'w', atoms)

    #storeBest = SaveToDB(db_name,runID,name,mode=relax_mode)
    save_calc = SaveRestartFiles(calc, name)
    update_db_info = UpdateDBInfo(db_name, runID, atoms)
    volume = atoms.get_volume()

    try:
        precon = Exp(mu=1.0, mu_c=1.0)
        fmax = 0.025
        smax = 0.003
        if (relax_mode == "both"):
            relaxer = PreconLBFGS(atoms,
                                  logfile=logfile,
                                  use_armijo=True,
                                  variable_cell=True)
        elif (relax_mode == "positions"):
            #relaxer = SciPyFminCG( atoms, logfile=logfile )
            relaxer = BFGS(atoms, logfile=logfile)
        elif (relax_mode == "cell"):
            str_f = StrainFilter(atoms, mask=[1, 1, 1, 0, 0, 0])
            relaxer = BFGS(str_f, logfile=logfile)
            fmax = smax * volume

        relaxer.attach(trajObj)
        #relaxer.attach( storeBest, interval=1, atoms=atoms )
        relaxer.attach(save_calc, interval=1)
        relaxer.attach(update_db_info, interval=1)
        if (not single_point):
            if (relax_mode == "both"):
                relaxer.run(fmax=fmax, smax=smax)
            else:
                relaxer.run(fmax=fmax)
        energy = atoms.get_potential_energy()

        orig_atoms = db.get_atoms(runID)
        single_p_calc = SinglePointCalculator(orig_atoms, energy=energy)
        orig_atoms.set_calculator(single_p_calc)
        kvp = db.get(name=name).key_value_pairs
        del db[runID]
        newID = db.write(orig_atoms, key_value_pairs=kvp)

        if (relax_mode == "positions"):
            db.update(newID, converged_force=True)
        elif (relax_mode == "cell"):
            db.update(newID, converged_stress=True)
        else:
            db.update(newID, converged_stress=True, converged_force=True)

        db.update(newID, single_point=single_point)
        db.update(newID, restart_file=SaveRestartFiles.restart_name(name))
        row = db.get(id=newID)
        conv_force = row.get("converged_force", default=0)
        conv_stress = row.get("converged_stress", default=0)
        if ((conv_force == 1) and (conv_stress == 1) and (nkpt == 4)):
            db.update(newID, converged=True)
    except Exception as exc:
        print(exc)
예제 #18
0
def get_reactions(columns='all', n_results=20, write_db=False, **kwargs):
    """
    Get reactions from server

    Give key value strings as arguments
    """
    if write_db or columns == 'all':
        columns = all_columns['reactions']
    queries = {}
    for key, value in kwargs.items():
        key = map_column_names(key)
        if key == 'distinct':
            if value in [True, 'True', 'true']:
                queries.update({key: True})
                continue
        if isinstance(value, int) or isinstance(value, float):
            queries.update({key: value})
        else:
            queries.update({key: '{0}'.format(value)})

    subtables = []
    if write_db:
        subtables = ['reactionSystems', 'publication']
    else:
        subtables = []
    data = query(table='reactions',
                 subtables=subtables,
                 columns=columns,
                 n_results=n_results,
                 queries=queries)

    if not write_db:
        return data

    print('Writing result to Reactions.db')
    unique_ids = []
    for row in data['reactions']['edges']:
        with CathubSQLite('Reactions.db') as db:
            row = row['node']
            key_values = {}
            for key in all_columns['reactions']:
                v = row[key]
                # if isinstance(v, unicode):
                #    v = v.encode('utf-8')
                try:
                    v = json.loads(v)
                except BaseException:
                    pass
                key_values[convert(key)] = v
            ase_ids = {}
            energy_corrections = {}

            for row_rs in row['reactionSystems']:
                if row_rs['name'] == 'N/A':
                    continue
                ase_ids[row_rs['name']] = row_rs['aseId']
                energy_corrections[row_rs['name']] = row_rs['energyCorrection']

            if not ase_ids:
                ase_ids = None
                energy_corrections = None
            else:
                unique_ids += ase_ids.values()
            key_values['ase_ids'] = ase_ids
            key_values['energy_corrections'] = ase_ids

            # publications
            pub_key_values = {}
            row_p = row['publication']
            for key in all_columns['publications']:
                pub_key_values[convert(key)] = row_p[key]
            db.write_publication(pub_key_values)

            # reactions and reaction_systems
            id = db.check(key_values['chemical_composition'],
                          key_values['reaction_energy'])
            if id is None:
                id = db.write(key_values)
            else:
                db.update(id, key_values)

    if ase_ids is not None:
        # Ase structures
        with ase.db.connect('Reactions.db') as ase_db:
            con = ase_db.connection
            cur = con.cursor()
            cur.execute('SELECT unique_id from systems;')
            unique_ids0 = cur.fetchall()
            unique_ids0 = [un[0] for un in unique_ids0]
            unique_ids = [un for un in unique_ids if un not in unique_ids0]
            for unique_id in list(set(unique_ids)):
                # if ase_db.count('unique_id={}'.format(unique_id)) == 0:
                atomsrow = get_atomsrow_by_id(unique_id)
                ase_db.write(atomsrow)

    print('Writing complete!')

    return data
예제 #19
0
def get_reactions(n_results=20, write_db=False, **kwargs):
    queries = {}
    for key, value in kwargs.items():
        key = map_column_names(key)
        if key == 'distinct':
            if value in ['True', 'true']:
                # WARNING: undefined variable name 'query_dict'
                query_dict.update({key: True})
                continue
        try:
            value = int(value)
            queries.update({key: value})
        except BaseException:
            queries.update({key: '{0}'.format(value)})

    subtables = []
    # if write_local:
    subtables = ['reactionSystems', 'publication']

    data = query(table='reactions',
                 subtables=subtables,
                 columns=all_columns['reactions'],
                 n_results=n_results,
                 queries=queries)

    if not write_db:
        return data

    for row in data['data']['reactions']['edges']:
        with CathubSQLite('Reactions.db') as db:
            row = row['node']
            key_values = {}
            for key in all_columns['reactions']:
                v = row[key]
                # if isinstance(v, unicode):
                #    v = v.encode('utf-8')
                try:
                    v = json.loads(v)
                except BaseException:
                    pass
                key_values[convert(key)] = v
            ase_ids = {}
            energy_corrections = {}

            for row_rs in row['reactionSystems']:
                if row_rs['name'] == 'N/A':
                    continue
                ase_ids[row_rs['name']] = row_rs['aseId']
                energy_corrections[row_rs['name']] = row_rs['energyCorrection']

            if not ase_ids:
                ase_ids = None
                energy_corrections = None
            key_values['ase_ids'] = ase_ids
            key_values['energy_corrections'] = ase_ids

            # publications
            pub_key_values = {}
            row_p = row['publication']
            for key in all_columns['publications']:
                pub_key_values[convert(key)] = row_p[key]
            db.write_publication(pub_key_values)

            # reactions and reaction_systems
            id = db.check(key_values['chemical_composition'],
                          key_values['reaction_energy'])
            if id is None:
                id = db.write(key_values)
            else:
                db.update(id, key_values)
        if ase_ids is not None:
            # Ase structures
            with ase.db.connect('Reactions.db') as ase_db:
                for unique_id in ase_ids.values():
                    if ase_db.count('unique_id={}'.format(unique_id)) == 0:
                        atomsrow = get_atomsrow_by_id(unique_id)
                        ase_db.write(atomsrow)

    return data
예제 #20
0
            'fwname.calculation_type': 'unit cell optimization',
            'fwname.vasp_settings.gga': 'RP',
            'fwname.vasp_settings.pp': 'PBE',
            'fwname.vasp_settings.xc': {
                '$exists': False
            },
            'fwname.vasp_settings.pp_version': '5.4',
            'fwname.vasp_settings.encut': 500,
            'fwname.vasp_settings.isym': 0
        }),
             desc='pulling from FireWorks'))

mpids = set()
db = ase.db.connect('bulks.db')
for doc in tqdm(docs, desc='writing to database'):
    atoms = make_atoms_from_doc(doc)
    n_elements = len(set(atoms.symbols))

    if n_elements <= 3:
        mpid = doc['fwname']['mpid']
        if mpid not in mpids:
            mpids.add(mpid)
            _ = db.write(atoms,
                         mpid=doc['fwname']['mpid'],
                         n_elements=n_elements)

        else:
            warnings.warn(
                'Found a duplicate MPID:  %s; adding the first one' % mpid,
                RuntimeWarning)
             total=n_sites))
docs_by_id = {doc['_id']: doc for doc in all_docs}

# Make up an energy
for mpid, _samples in tqdm(samples.items(), desc='bulks'):
    sampled_bulk_mean = norm.rvs(loc=bulk_mean, scale=bulk_std)
    for surface, sites in tqdm(_samples.items(), desc='surfaces'):
        sampled_surface_mean = norm.rvs(loc=sampled_bulk_mean,
                                        scale=surface_std)
        for site in tqdm(sites, desc='sites'):
            sampled_energy = norm.rvs(loc=sampled_surface_mean, scale=site_std)

            # Make the atoms object
            doc = docs_by_id[site['mongo_id']]
            atoms = make_atoms_from_doc(doc)

            # Grab meta info
            miller = tuple(site['miller'])
            shift = round(site['shift'], 2)
            top = site['top']
            data = {
                'adsorption_energy': sampled_energy,
                'mpid': mpid,
                'miller': miller,
                'shift': shift,
                'top': top
            }

            # Write to the DB
            db.write(atoms, data=data)