test_energies.py 1.34 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
15
    def setUp(self, dtype=torchani.default_dtype,
              device=torchani.default_device):
16
        self.tolerance = 5e-5
17
        aev_computer = torchani.SortedAEV(
18
            dtype=dtype, device=torch.device('cpu'))
19
20
21
22
        prepare = torchani.PrepareInput(aev_computer.species,
                                        aev_computer.device)
        nnp = torchani.models.NeuroChemNNP(aev_computer.species)
        self.model = torch.nn.Sequential(prepare, aev_computer, nnp)
23
24
25

    def _test_molecule(self, coordinates, species, energies):
        shift_energy = torchani.EnergyShifter(torchani.buildin_sae_file)
26
        energies_ = self.model((species, coordinates)).squeeze()
27
28
29
30
31
32
33
34
35
36
37
38
39
40
        energies_ = shift_energy.add_sae(energies_, species)
        max_diff = (energies - energies_).abs().max().item()
        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()