File: test_element_operators.py

package info (click to toggle)
python-ase 3.26.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,484 kB
  • sloc: python: 148,112; xml: 2,728; makefile: 110; javascript: 47
file content (83 lines) | stat: -rw-r--r-- 2,426 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# fmt: off
import numpy as np

from ase import Atoms
from ase.ga import get_raw_score, set_raw_score
from ase.ga.element_crossovers import OnePointElementCrossover
from ase.ga.element_mutations import (
    MoveDownMutation,
    MoveLeftMutation,
    MoveRightMutation,
    MoveUpMutation,
    RandomElementMutation,
)


def test_element_operators(seed):

    # set up the random number generator
    rng = np.random.RandomState(seed)

    a1 = Atoms('SrSrSrBaClClClClBrBrBrBr')
    a1.info['confid'] = 1
    a2 = Atoms('CaCaMgBaFFFFFFFF')
    a2.info['confid'] = 2

    cations = ['Sr', 'Ba', 'Ca', 'Mg']
    anions = ['Cl', 'F', 'Br']
    op = OnePointElementCrossover([cations, anions], [3, 2], [.25, .5],
                                  rng=rng)

    a3, _desc = op.get_new_individual([a1, a2])

    syms = a3.get_chemical_symbols()
    assert len({i for i in syms if i in cations}) < 4
    assert len({i for i in syms if i in anions}) < 3

    op = RandomElementMutation([cations, anions], [3, 2], [.25, .5], rng=rng)
    a4, _desc = op.get_new_individual([a1])
    syms = a4.get_chemical_symbols()

    assert len({i for i in syms if i in cations}) < 4
    assert len({i for i in syms if i in anions}) < 3

    op = RandomElementMutation(anions, 2, .5, rng=rng)
    a4, _desc = op.get_new_individual([a2])
    syms = a4.get_chemical_symbols()

    assert len({i for i in syms if i in anions}) == 2

    a1 = Atoms('SrSrClClClCl')
    a1.info['confid'] = 1
    op = MoveDownMutation(cations, 2, .5, rng=rng)
    a2, _desc = op.get_new_individual([a1])
    a2.info['confid'] = 2

    syms = a2.get_chemical_symbols()
    assert 'Ba' in syms
    assert len(set(syms)) == 3

    op = MoveUpMutation(cations, 1, 1., rng=rng)
    a3, _desc = op.get_new_individual([a2])
    syms = a3.get_chemical_symbols()
    assert 'Ba' not in syms
    assert len(set(syms)) == 2

    cations = ['Co', 'Ni', 'Cu']
    a1 = Atoms('NiNiBrBr')
    a1.info['confid'] = 1
    op = MoveRightMutation(cations, 1, 1., rng=rng)
    a2, _desc = op.get_new_individual([a1])
    a2.info['confid'] = 2
    syms = a2.get_chemical_symbols()

    assert len(set(syms)) == 2
    assert len([i for i in syms if i == 'Cu']) == 2

    op = MoveLeftMutation(cations, 2, .5, rng=rng)
    a3, _desc = op.get_new_individual([a2])
    syms = a3.get_chemical_symbols()

    assert len(set(syms)) == 3
    set_raw_score(a3, 5.0)
    assert get_raw_score(a3) == 5.0