test_energies.py 1.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
import torch
import torchani
import unittest
import os
import pickle


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


class TestEnergies(unittest.TestCase):

14
    def setUp(self):
15
        self.tolerance = 5e-5
16
17
        aev_computer = torchani.SortedAEV()
        prepare = torchani.PrepareInput(aev_computer.species)
18
        nnp = torchani.models.NeuroChemNNP(aev_computer.species)
Gao, Xiang's avatar
Gao, Xiang committed
19
20
21
        shift_energy = torchani.EnergyShifter(aev_computer.species)
        self.model = torch.nn.Sequential(prepare, aev_computer,
                                         nnp, shift_energy)
22
23

    def _test_molecule(self, coordinates, species, energies):
Gao, Xiang's avatar
Gao, Xiang committed
24
25
26
27
28
29
        # generate a random permute
        atoms = len(species)
        randperm = torch.randperm(atoms)
        coordinates = coordinates.index_select(1, randperm)
        species = [species[i] for i in randperm.tolist()]

30
        _, energies_ = self.model((species, coordinates))
Gao, Xiang's avatar
Gao, Xiang committed
31
        max_diff = (energies - energies_.squeeze()).abs().max().item()
32
33
34
35
36
37
38
39
40
41
42
43
        self.assertLess(max_diff, self.tolerance)

    def testGDB(self):
        for i in range(N):
            datafile = os.path.join(path, 'test_data/{}'.format(i))
            with open(datafile, 'rb') as f:
                coordinates, species, _, _, energies, _ = pickle.load(f)
                self._test_molecule(coordinates, species, energies)


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