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'])
|