File: test_pickle2json.py

package info (click to toggle)
python-ase 3.22.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,344 kB
  • sloc: python: 126,379; xml: 946; makefile: 111; javascript: 47
file content (46 lines) | stat: -rw-r--r-- 1,239 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
import pickle

import pytest

from ase.build import molecule
from ase.calculators.emt import EMT
from ase.vibrations import Vibrations
from ase.vibrations.pickle2json import main as pickle2json_main


def test_pickle2json(testdir):
    atoms = molecule('H2O')
    atoms.calc = EMT()
    name = 'vib'
    vib = Vibrations(atoms, name=name)
    vib.run()

    forces_dct = dict(vib.cache)
    assert len(forces_dct) > 0

    # Create old-style pickle cache:
    for key, value in vib.cache.items():
        with (testdir / f'vib.{key}.pckl').open('wb') as fd:
            array = value['forces']
            pickle.dump(array, fd)

    vib.cache.clear()
    assert dict(vib.cache) == {}

    # When there are old pickles but no JSON files, run() should complain:
    with pytest.raises(RuntimeError, match='Found old pickle'):
        vib.run()

    picklefiles = [str(path) for path in testdir.glob('vib.*.pckl')]

    pickle2json_main(picklefiles)

    # Read forces after back-conversion:
    newforces_dct = dict(vib.cache)
    assert len(newforces_dct) > 0

    assert set(forces_dct) == set(newforces_dct)

    for key in forces_dct:
        assert forces_dct[key]['forces'] == pytest.approx(
            newforces_dct[key]['forces'])