File: test_setpos.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 (29 lines) | stat: -rw-r--r-- 887 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
def test_setpos():
    import numpy as np
    from ase.build import molecule
    from ase.constraints import FixAtoms


    def array_almost_equal(a1, a2, tol=np.finfo(type(1.0)).eps):
        """Replacement for old numpy.testing.utils.array_almost_equal."""
        return (np.abs(a1 - a2) < tol).all()


    m = molecule('H2')
    c = FixAtoms(indices=[atom.index for atom in m])
    m.set_constraint(c)

    pos1 = m.get_positions()
    # shift z-coordinates by 1.
    pos = m.get_positions()
    pos[:, 2] += 1.

    m.set_positions(pos)
    # note that set_positions fails silently to set the new positions
    # due to the presence of constraints!
    assert array_almost_equal(pos1, m.get_positions())

    m.positions = pos
    # atoms.positions allows one to set the new positions
    # even in the presence of constraints!
    assert array_almost_equal(pos, m.get_positions())