File: test_singlepointcalc.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 (44 lines) | stat: -rw-r--r-- 1,511 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
# fmt: off
from ase.build import fcc111
from ase.calculators.emt import EMT
from ase.constraints import FixAtoms
from ase.io import read


def test_singlepointcalc(testdir):
    """This test makes sure that the forces returned from a
    SinglePointCalculator are immutable. Previously, successive calls to
    atoms.get_forces(apply_constraint=x), with x alternating between True and
    False, would get locked into the constrained variation."""

    def check_forces():
        """Makes sure the unconstrained forces stay that way."""
        forces = atoms.get_forces(apply_constraint=False)
        funconstrained = float(forces[0, 0])

        forces = atoms.get_forces(apply_constraint=True)

        forces = atoms.get_forces(apply_constraint=False)
        funconstrained2 = float(forces[0, 0])

        assert funconstrained2 == funconstrained

    atoms = fcc111('Cu', (2, 2, 1), vacuum=10.)
    atoms[0].x += 0.2
    atoms.set_constraint(FixAtoms(indices=[atom.index for atom in atoms]))

    # First run the tes with EMT and save a force component.
    atoms.calc = EMT()
    check_forces()
    f = float(atoms.get_forces(apply_constraint=False)[0, 0])

    # Save and reload with a SinglePointCalculator.
    atoms.write('singlepointtest.traj')
    atoms = read('singlepointtest.traj')
    check_forces()

    # Manually change a value.
    forces = atoms.get_forces(apply_constraint=False)
    forces[0, 0] = 42.
    forces = atoms.get_forces(apply_constraint=False)
    assert forces[0, 0] == f