File: test_cp2k_dcd.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 (73 lines) | stat: -rw-r--r-- 2,123 bytes parent folder | download | duplicates (2)
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
"""Test suit for the CP2K ASE calulator.

http://www.cp2k.org
Author: Ole Schuett <ole.schuett@mat.ethz.ch>
"""

import subprocess

import numpy as np
import pytest

from ase.build import molecule
from ase import io
from ase.io.cp2k import iread_cp2k_dcd
from ase.calculators.calculator import compare_atoms


inp = """\
&MOTION
  &PRINT
    &TRAJECTORY SILENT
      FORMAT DCD_ALIGNED_CELL
    &END TRAJECTORY
  &END PRINT
  &MD
    STEPS 5
  &END MD
&END MOTION
&GLOBAL
  RUN_TYPE MD
&END GLOBAL
"""


@pytest.mark.calculator_lite
@pytest.mark.calculator('cp2k')
def test_dcd(factory, factories):
    # (Should the cp2k_main executable live on the cp2k factory?)
    exes = factories.executables

    cp2k_main = exes.get('cp2k_main')
    if cp2k_main is None:
        pytest.skip('Please define "cp2k_main" in testing executables.  '
                    'It should point to the main cp2k executable '
                    '(not the shell)')

    calc = factory.calc(label='test_dcd', max_scf=1, inp=inp)
    h2 = molecule('H2', calculator=calc)
    h2.center(vacuum=2.0)
    h2.set_pbc(True)
    energy = h2.get_potential_energy()
    assert energy is not None
    subprocess.check_call([cp2k_main, '-i', 'test_dcd.inp', '-o',
                           'test_dcd.out'])
    h2_end = io.read('test_dcd-pos-1.dcd')
    assert (h2_end.symbols == 'X').all()
    traj = io.read('test_dcd-pos-1.dcd', ref_atoms=h2,
                   index=slice(0, None), aligned=True)
    ioITraj = io.iread('test_dcd-pos-1.dcd', ref_atoms=h2,
                       index=slice(0, None), aligned=True)

    with open('test_dcd-pos-1.dcd', 'rb') as fd:
        itraj = iread_cp2k_dcd(fd, indices=slice(0, None),
                               ref_atoms=h2, aligned=True)
        for i, iMol in enumerate(itraj):
            ioIMol = next(ioITraj)
            assert compare_atoms(iMol, traj[i]) == []
            assert compare_atoms(iMol, ioIMol) == []
            assert iMol.get_pbc().all()

    traj = io.read('test_dcd-pos-1.dcd', ref_atoms=h2, index=slice(0, None))
    pbc = [mol.get_pbc() for mol in traj]
    assert not np.any(pbc)