Beispiel #1
0
def test_add_candidates():
    import pytest
    from ase.build import fcc111
    from ase.ga.data import PrepareDB
    from ase.ga.data import DataConnection
    from ase.ga.offspring_creator import OffspringCreator
    from ase.ga import set_raw_score

    import os

    db_file = 'gadb.db'
    if os.path.isfile(db_file):
        os.remove(db_file)

    db = PrepareDB(db_file)

    slab1 = fcc111('Ag', size=(2, 2, 2))
    db.add_unrelaxed_candidate(slab1)

    slab2 = fcc111('Cu', size=(2, 2, 2))
    set_raw_score(slab2, 4)
    db.add_relaxed_candidate(slab2)
    assert slab2.info['confid'] == 3

    db = DataConnection(db_file)
    assert db.get_number_of_unrelaxed_candidates() == 1

    slab3 = db.get_an_unrelaxed_candidate()
    old_confid = slab3.info['confid']
    slab3[0].symbol = 'Au'
    db.add_unrelaxed_candidate(slab3, 'mutated: Parent {0}'.format(old_confid))
    new_confid = slab3.info['confid']
    # confid should update when using add_unrelaxed_candidate
    assert old_confid != new_confid
    slab3[1].symbol = 'Au'
    db.add_unrelaxed_step(slab3, 'mutated: Parent {0}'.format(new_confid))
    # confid should not change when using add_unrelaxed_step
    assert slab3.info['confid'] == new_confid

    with pytest.raises(AssertionError):
        db.add_relaxed_step(slab3)
    set_raw_score(slab3, 3)
    db.add_relaxed_step(slab3)

    slab4 = OffspringCreator.initialize_individual(slab1,
                                                   fcc111('Au', size=(2, 2, 2)))
    set_raw_score(slab4, 67)
    db.add_relaxed_candidate(slab4)
    assert slab4.info['confid'] == 7

    more_slabs = []
    for m in ['Ni', 'Pd', 'Pt']:
        slab = fcc111(m, size=(2, 2, 2))
        slab = OffspringCreator.initialize_individual(slab1, slab)
        set_raw_score(slab, sum(slab.get_masses()))
        more_slabs.append(slab)
    db.add_more_relaxed_candidates(more_slabs)
    assert more_slabs[1].info['confid'] == 9

    os.remove(db_file)
    pure_slabs.append(slab)

# The population size should be at least the number of different compositions
pop_size = 2 * len(slab)

# We prepare the db and write a few constants that we are going to use later
db = PrepareDB('hull.db',
               population_size=pop_size,
               reference_energies=refs,
               metals=metals,
               lattice_constants=lattice_constants)

# We add the pure slabs to the database as relaxed because we have already
# set the raw_score
for slab in pure_slabs:
    db.add_relaxed_candidate(slab,
                             atoms_string=''.join(slab.get_chemical_symbols()))

# Now we create the rest of the candidates for the initial population
for i in range(pop_size - 2):
    # How many of each metal is picked at random, making sure that
    # we do not pick pure slabs
    nA = random.randint(0, len(slab) - 2)
    nB = len(slab) - 2 - nA
    symbols = [metals[0]] * nA + [metals[1]] * nB + metals

    # Making a generic slab with the correct lattice constant
    slab = fcc111('X',
                  size=(2, 4, 3),
                  a=get_avg_lattice_constant(symbols),
                  vacuum=5,
                  orthogonal=True)
Beispiel #3
0
from ase.ga import set_raw_score

import os

db_file = 'gadb.db'
if os.path.isfile(db_file):
    os.remove(db_file)

db = PrepareDB(db_file)

slab1 = fcc111('Ag', size=(2, 2, 2))
db.add_unrelaxed_candidate(slab1)

slab2 = fcc111('Cu', size=(2, 2, 2))
set_raw_score(slab2, 4)
db.add_relaxed_candidate(slab2)
assert slab2.info['confid'] == 3

db = DataConnection(db_file)
assert db.get_number_of_unrelaxed_candidates() == 1

slab3 = db.get_an_unrelaxed_candidate()
old_confid = slab3.info['confid']
slab3[0].symbol = 'Au'
db.add_unrelaxed_candidate(slab3, 'mutated: Parent {0}'.format(old_confid))
new_confid = slab3.info['confid']
# confid should update when using add_unrelaxed_candidate
assert old_confid != new_confid
slab3[1].symbol = 'Au'
db.add_unrelaxed_step(slab3, 'mutated: Parent {0}'.format(new_confid))
# confid should not change when using add_unrelaxed_step