File: test_morse.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 (66 lines) | stat: -rw-r--r-- 1,902 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
# fmt: off
import numpy as np
import pytest
from scipy.optimize import check_grad

from ase import Atoms
from ase.build import bulk
from ase.calculators.fd import (
    calculate_numerical_forces,
    calculate_numerical_stress,
)
from ase.calculators.morse import MorsePotential, fcut, fcut_d
from ase.vibrations import Vibrations

De = 5.
Re = 3.
rho0 = 2.


def test_gs_minimum_energy():
    atoms = Atoms('H2', positions=[[0, 0, 0], [0, 0, Re]])
    atoms.calc = MorsePotential(epsilon=De, r0=Re)
    assert atoms.get_potential_energy() == -De


def test_gs_vibrations(testdir):
    # check ground state vibrations
    atoms = Atoms('H2', positions=[[0, 0, 0], [0, 0, Re]])
    atoms.calc = MorsePotential(epsilon=De, r0=Re, rho0=rho0)
    vib = Vibrations(atoms)
    vib.run()


def test_cutoff():
    # check that fcut_d is the derivative of fcut
    r1 = 2.0
    r2 = 3.0
    r = np.linspace(r1 - 0.5, r2 + 0.5, 100)
    for R in r:
        assert check_grad(fcut, fcut_d, np.array([R]), r1, r2) < 1e-5


def test_forces_and_stress():
    atoms = bulk('Cu', cubic=True)
    atoms.calc = MorsePotential(A=4.0, epsilon=1.0, r0=2.55)
    atoms.rattle(0.1)

    forces = atoms.get_forces()
    numerical_forces = calculate_numerical_forces(atoms, eps=1e-5)
    np.testing.assert_allclose(forces, numerical_forces, atol=1e-5)

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


def fake_neighbor_list(*args, **kwargs):
    raise RuntimeError('test_neighbor_list')


def test_override_neighbor_list():
    with pytest.raises(RuntimeError, match='test_neighbor_list'):
        atoms = bulk('Cu', cubic=True)
        atoms.calc = MorsePotential(A=4.0, epsilon=1.0, r0=2.55,
                                    neighbor_list=fake_neighbor_list)
        _ = atoms.get_potential_energy()