File: test_tersoff.py

package info (click to toggle)
python-ase 3.26.0-3
  • 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 (249 lines) | stat: -rw-r--r-- 7,992 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"""Tests for ``Tersoff``."""

import numpy as np
import pytest

from ase import Atoms
from ase.build import bulk
from ase.calculators.calculator import PropertyNotImplementedError
from ase.calculators.fd import (
    calculate_numerical_forces,
    calculate_numerical_stress,
)
from ase.calculators.tersoff import Tersoff, TersoffParameters


@pytest.fixture
def si_parameters():
    """Fixture providing the Silicon parameters.

    Parameters taken from: Tersoff, Phys Rev B, 37, 6991 (1988)
    """
    return {
        ('Si', 'Si', 'Si'): TersoffParameters(
            A=3264.7,
            B=95.373,
            lambda1=3.2394,
            lambda2=1.3258,
            lambda3=1.3258,
            beta=0.33675,
            gamma=1.00,
            m=3.00,
            n=22.956,
            c=4.8381,
            d=2.0417,
            h=0.0000,
            R=3.00,
            D=0.20,
        )
    }


@pytest.fixture(name='atoms_si')
def fixture_atoms_si(
    si_parameters: dict[tuple[str, str, str], TersoffParameters],
) -> Atoms:
    """Make Atoms for Si with a small displacement on the first atom."""
    atoms = bulk('Si', a=5.43, cubic=True)

    # pertubate first atom to get substantial forces
    atoms.positions[0] += [0.03, 0.02, 0.01]

    atoms.calc = Tersoff(si_parameters)

    return atoms


def test_initialize_from_params_from_dict(si_parameters):
    """Test initializing Tersoff calculator from dictionary of parameters."""
    calc = Tersoff(si_parameters)
    assert calc.parameters == si_parameters
    diamond = bulk('Si', 'diamond', a=5.43)
    diamond.calc = calc
    potential_energy = diamond.get_potential_energy()
    np.testing.assert_allclose(potential_energy, -9.260818674314585, atol=1e-8)


def test_set_parameters(
    si_parameters: dict[tuple[str, str, str], TersoffParameters],
) -> None:
    """Test updating parameters of the Tersoff calculator."""
    calc = Tersoff(si_parameters)
    key = ('Si', 'Si', 'Si')

    calc.set_parameters(key, m=2.0)
    assert calc.parameters[key].m == 2.0

    calc.set_parameters(key, R=2.90, D=0.25)
    assert calc.parameters[key].R == 2.90
    assert calc.parameters[key].D == 0.25

    new_params = TersoffParameters(
        m=si_parameters[key].m,
        gamma=si_parameters[key].gamma,
        lambda3=si_parameters[key].lambda3,
        c=si_parameters[key].c,
        d=si_parameters[key].d,
        h=si_parameters[key].h,
        n=si_parameters[key].n,
        beta=si_parameters[key].beta,
        lambda2=si_parameters[key].lambda2,
        B=si_parameters[key].B,
        R=3.00,  # Reset cutoff radius
        D=si_parameters[key].D,
        lambda1=si_parameters[key].lambda1,
        A=si_parameters[key].A,
    )
    calc.set_parameters(key, params=new_params)
    assert calc.parameters[key] == new_params


def test_isolated_atom(si_parameters: dict) -> None:
    """Test if an isolated atom can be computed correctly."""
    atoms = Atoms('Si')
    atoms.calc = Tersoff(si_parameters)
    energy = atoms.get_potential_energy()
    energies = atoms.get_potential_energies()
    forces = atoms.get_forces()
    np.testing.assert_almost_equal(energy, 0.0)
    np.testing.assert_allclose(energies, [0.0], rtol=1e-5)
    np.testing.assert_allclose(forces, [[0.0] * 3], rtol=1e-5)
    with pytest.raises(PropertyNotImplementedError):
        atoms.get_stress()


def test_unary(atoms_si: Atoms) -> None:
    """Test if energy, forces, and stress of a unary system agree with LAMMPS.

    The reference values are obtained in the following way.

    >>> from ase.calculators.lammpslib import LAMMPSlib
    >>>
    >>> atoms = bulk('Si', a=5.43, cubic=True)
    >>> atoms.positions[0] += [0.03, 0.02, 0.01]
    >>> lmpcmds = ['pair_style tersoff', 'pair_coeff * * Si.tersoff Si']
    >>> atoms.calc = LAMMPSlib(lmpcmds=lmpcmds)
    >>> energy = atoms.get_potential_energy()
    >>> energies = atoms.get_potential_energies()
    >>> forces = atoms.get_forces()
    >>> stress = atoms.get_stress()

    """
    energy_ref = -37.03237572778589
    energies_ref = [
        -4.62508202,
        -4.62242901,
        -4.63032346,
        -4.63028909,
        -4.63037555,
        -4.63147495,
        -4.63040683,
        -4.63199482,
    ]
    forces_ref = [
        [-4.63805736e-01, -3.17112011e-01, -1.79345801e-01],
        [+2.34142607e-01, +2.29060580e-01, +2.24142706e-01],
        [-2.79544489e-02, +1.31289732e-03, +3.99485914e-04],
        [+1.85144670e-02, +1.48017753e-02, +8.47421196e-03],
        [+2.06558877e-03, -1.86613107e-02, +3.98039278e-04],
        [+8.68756690e-02, -5.15405628e-02, +7.32472691e-02],
        [+2.06388309e-03, +1.30960793e-03, -9.30764103e-03],
        [+1.48097970e-01, +1.40829025e-01, -1.18008270e-01],
    ]
    stress_ref = [
        -0.00048610,
        -0.00056779,
        -0.00061684,
        -0.00342602,
        -0.00231541,
        -0.00124569,
    ]

    energy = atoms_si.get_potential_energy()
    energies = atoms_si.get_potential_energies()
    forces = atoms_si.get_forces()
    stress = atoms_si.get_stress()
    np.testing.assert_almost_equal(energy, energy_ref)
    np.testing.assert_allclose(energies, energies_ref, rtol=1e-5)
    np.testing.assert_allclose(forces, forces_ref, rtol=1e-5)
    np.testing.assert_allclose(stress, stress_ref, rtol=1e-5)


def test_binary(datadir) -> None:
    """Test if energy, forces, and stress of a binary system agree with LAMMPS.

    The reference values are obtained in the following way.

    >>> from ase.calculators.lammpslib import LAMMPSlib
    >>>
    >>> atoms = bulk('Si', a=5.43, cubic=True)
    >>> atoms.symbols[1] = 'C'
    >>> atoms.symbols[2] = 'C'
    >>> atoms.positions[0] += [0.03, 0.02, 0.01]
    >>> lmpcmds = ['pair_style tersoff', 'pair_coeff * * SiC.tersoff Si C']
    >>> atoms.calc = LAMMPSlib(lmpcmds=lmpcmds)
    >>> energy = atoms.get_potential_energy()
    >>> energies = atoms.get_potential_energies()
    >>> forces = atoms.get_forces()
    >>> stress = atoms.get_stress()

    """
    atoms = bulk('Si', a=5.43, cubic=True)
    atoms.symbols[1] = 'C'
    atoms.symbols[2] = 'C'

    # pertubate first atom to get substantial forces
    atoms.positions[0] += [0.03, 0.02, 0.01]

    potential_file = datadir / 'tersoff' / 'SiC.tersoff'
    atoms.calc = Tersoff.from_lammps(potential_file)

    energy_ref = -28.780184609451915
    energies_ref = [
        -4.33637575,
        -2.02218449,
        -1.80044260,
        -4.12192108,
        -4.12650203,
        -4.12473794,
        -4.12677193,
        -4.12124880,
    ]
    forces_ref = [
        [+6.40479511, +6.64830387, +6.83733140],
        [+6.93259841, -7.29932178, -7.32986722],
        [-7.09646214, +7.17384006, +7.15478087],
        [-6.63906798, -6.62943844, -6.64034900],
        [-6.48323569, +6.55237593, -6.52463929],
        [+6.64668748, +6.56474714, -6.55390547],
        [-6.47026652, -6.50615747, +6.53977908],
        [+6.70495132, -6.50434930, +6.51686963],
    ]
    stress_ref = [
        +0.35188635,
        +0.35366730,
        +0.35444532,
        -0.11629806,
        +0.11665436,
        +0.11668285,
    ]

    energy = atoms.get_potential_energy()
    energies = atoms.get_potential_energies()
    forces = atoms.get_forces()
    stress = atoms.get_stress()
    np.testing.assert_almost_equal(energy, energy_ref)
    np.testing.assert_allclose(energies, energies_ref, rtol=1e-5)
    np.testing.assert_allclose(forces, forces_ref, rtol=1e-5)
    np.testing.assert_allclose(stress, stress_ref, rtol=1e-5)


def test_forces_and_stress(atoms_si: Atoms) -> None:
    """Test if analytical forces and stress agree with numerical ones."""
    forces = atoms_si.get_forces()
    numerical_forces = calculate_numerical_forces(atoms_si, eps=1e-5)
    np.testing.assert_allclose(forces, numerical_forces, atol=1e-5)

    stress = atoms_si.get_stress()
    numerical_stress = calculate_numerical_stress(atoms_si, eps=1e-5)
    np.testing.assert_allclose(stress, numerical_stress, atol=1e-5)