test_structure_optim.py 1.19 KB
Newer Older
1
2
3
4
5
6
7
import os
import unittest
import torch
import torchani
import copy
import pickle
from ase.optimize import BFGS
Gao, Xiang's avatar
Gao, Xiang committed
8
from ase import Atoms
9
10
11
12
13


path = os.path.dirname(os.path.realpath(__file__))


14
class TestStructureOptimization(torchani.testing.TestCase):
15
16
17

    def setUp(self):
        self.tolerance = 1e-6
18
        self.calculator = torchani.models.ANI1x(model_index=0).ase()
19
20
21
22
23
24

    def testRMSE(self):
        datafile = os.path.join(path, 'test_data/NeuroChemOptimized/all')
        with open(datafile, 'rb') as f:
            all_atoms = pickle.load(f)
            for atoms in all_atoms:
Gao, Xiang's avatar
Gao, Xiang committed
25
26
27
                # reconstructing Atoms object.
                # ASE does not support loading pickled object from older version
                atoms = Atoms(atoms.get_chemical_symbols(), positions=atoms.get_positions())
28
29
30
31
32
33
                old_coordinates = copy.deepcopy(atoms.get_positions())
                old_coordinates = torch.from_numpy(old_coordinates)
                atoms.set_calculator(self.calculator)
                opt = BFGS(atoms)
                opt.run()
                coordinates = atoms.get_positions()
34
                self.assertEqual(old_coordinates, coordinates)
35
36
37
38


if __name__ == '__main__':
    unittest.main()