File: test_add_candidates.py

package info (click to toggle)
python-ase 3.21.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,936 kB
  • sloc: python: 122,428; xml: 946; makefile: 111; javascript: 47
file content (60 lines) | stat: -rw-r--r-- 1,969 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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)