common_aev_test.py 1.18 KB
Newer Older
Gao, Xiang's avatar
Gao, Xiang committed
1
2
3
import unittest
import torch
import torchani
Gao, Xiang's avatar
Gao, Xiang committed
4
import os
Gao, Xiang's avatar
Gao, Xiang committed
5
6
7
8
9
10
11

tolerance = 1e-5


class _TestAEVBase(unittest.TestCase):

    def setUp(self):
Gao, Xiang's avatar
Gao, Xiang committed
12
13
14
15
        path = os.path.dirname(os.path.realpath(__file__))
        const_file = os.path.join(path, '../torchani/resources/ani-1x_8x/rHCNO-5.2R_16-3.5A_a4-8.params')  # noqa: E501
        consts = torchani.neurochem.Constants(const_file)
        self.aev_computer = torchani.AEVComputer(**consts)
Gao, Xiang's avatar
Gao, Xiang committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
        self.radial_length = self.aev_computer.radial_length
        self.debug = False

    def assertAEVEqual(self, expected_radial, expected_angular, aev, tolerance=tolerance):
        radial = aev[..., :self.radial_length]
        angular = aev[..., self.radial_length:]
        radial_diff = expected_radial - radial
        if self.debug:
            aid = 1
            print(torch.stack([expected_radial[0, aid, :], radial[0, aid, :], radial_diff.abs()[0, aid, :]], dim=1))
        radial_max_error = torch.max(torch.abs(radial_diff)).item()
        angular_diff = expected_angular - angular
        angular_max_error = torch.max(torch.abs(angular_diff)).item()
        self.assertLess(radial_max_error, tolerance)
        self.assertLess(angular_max_error, tolerance)