test_ase.py 8.61 KB
Newer Older
Gao, Xiang's avatar
Gao, Xiang committed
1
2
from ase.lattice.cubic import Diamond
from ase.md.langevin import Langevin
Gao, Xiang's avatar
Gao, Xiang committed
3
from ase import units, Atoms
Gao, Xiang's avatar
Gao, Xiang committed
4
5
6
7
from ase.calculators.test import numeric_force
import torch
import torchani
import unittest
Gao, Xiang's avatar
Gao, Xiang committed
8
import numpy
9
import itertools
Gao, Xiang's avatar
Gao, Xiang committed
10
import math
11
12
13
14
15
16
import os
import pickle

path = os.path.dirname(os.path.realpath(__file__))
N = 97
tol = 5e-5
Gao, Xiang's avatar
Gao, Xiang committed
17
18
19
20
21
22
23
24
25
26
27
28


def get_numeric_force(atoms, eps):
    fn = torch.zeros((len(atoms), 3))
    for i in range(len(atoms)):
        for j in range(3):
            fn[i, j] = numeric_force(atoms, i, j, eps)
    return fn


class TestASE(unittest.TestCase):

Gao, Xiang's avatar
Gao, Xiang committed
29
30
    def _testForce(self, pbc):
        atoms = Diamond(symbol="C", pbc=pbc)
Gao, Xiang's avatar
Gao, Xiang committed
31
32
33
34
35
36
37
38
39
40
41
        builtin = torchani.neurochem.Builtins()
        calculator = torchani.ase.Calculator(
            builtin.species, builtin.aev_computer,
            builtin.models, builtin.energy_shifter)
        atoms.set_calculator(calculator)
        dyn = Langevin(atoms, 5 * units.fs, 30000000 * units.kB, 0.002)
        dyn.run(100)
        f = torch.from_numpy(atoms.get_forces())
        fn = get_numeric_force(atoms, 0.001)
        df = (f - fn).abs().max()
        avgf = f.abs().mean()
Gao, Xiang's avatar
Gao, Xiang committed
42
43
44
45
46
47
48
49
50
        if avgf > 0:
            self.assertLess(df / avgf, 0.1)

    def testForceWithPBCEnabled(self):
        self._testForce(True)

    def testForceWithPBCDisabled(self):
        self._testForce(False)

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    def testANIDataset(self):
        builtin = torchani.neurochem.Builtins()
        calculator = torchani.ase.Calculator(
            builtin.species, builtin.aev_computer,
            builtin.models, builtin.energy_shifter)
        default_neighborlist_calculator = torchani.ase.Calculator(
            builtin.species, builtin.aev_computer,
            builtin.models, builtin.energy_shifter, True)
        nnp = torch.nn.Sequential(
            builtin.aev_computer,
            builtin.models,
            builtin.energy_shifter
        )
        for i in range(N):
            datafile = os.path.join(path, 'test_data/ANI1_subset/{}'.format(i))
            with open(datafile, 'rb') as f:
                coordinates, species, _, _, _, _ = pickle.load(f)
                coordinates = coordinates[0]
                species = species[0]
                species_str = [builtin.consts.species[i] for i in species]

                atoms = Atoms(species_str, positions=coordinates)
                atoms.set_calculator(calculator)
                energy1 = atoms.get_potential_energy() / units.Hartree
                forces1 = atoms.get_forces() / units.Hartree

                atoms2 = Atoms(species_str, positions=coordinates)
                atoms2.set_calculator(default_neighborlist_calculator)
                energy2 = atoms2.get_potential_energy() / units.Hartree
                forces2 = atoms2.get_forces() / units.Hartree

                coordinates = torch.tensor(coordinates,
                                           requires_grad=True).unsqueeze(0)
                _, energy3 = nnp((torch.from_numpy(species).unsqueeze(0),
                                  coordinates))
                forces3 = -torch.autograd.grad(energy3.squeeze(),
                                               coordinates)[0].numpy()
                energy3 = energy3.item()

                self.assertLess(abs(energy1 - energy2), tol)
                self.assertLess(abs(energy1 - energy3), tol)

                diff_f12 = torch.tensor(forces1 - forces2).abs().max().item()
                self.assertLess(diff_f12, tol)
                diff_f13 = torch.tensor(forces1 - forces3).abs().max().item()
                self.assertLess(diff_f13, tol)

Gao, Xiang's avatar
Gao, Xiang committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    def testForceAgainstDefaultNeighborList(self):
        atoms = Diamond(symbol="C", pbc=False)
        builtin = torchani.neurochem.Builtins()

        calculator = torchani.ase.Calculator(
            builtin.species, builtin.aev_computer,
            builtin.models, builtin.energy_shifter)
        default_neighborlist_calculator = torchani.ase.Calculator(
            builtin.species, builtin.aev_computer,
            builtin.models, builtin.energy_shifter, True)

        atoms.set_calculator(calculator)
        dyn = Langevin(atoms, 5 * units.fs, 50 * units.kB, 0.002)

        def test_energy(a=atoms):
            a = a.copy()
            a.set_calculator(calculator)
            e1 = a.get_potential_energy()
            a.set_calculator(default_neighborlist_calculator)
            e2 = a.get_potential_energy()
118
            self.assertLess(abs(e1 - e2), tol)
Gao, Xiang's avatar
Gao, Xiang committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

        dyn.attach(test_energy, interval=1)
        dyn.run(500)

    def testTranslationalInvariancePBC(self):
        atoms = Atoms('CH4', [[0, 0, 0],
                              [1, 0, 0],
                              [0, 1, 0],
                              [0, 0, 1],
                              [0, 1, 1]],
                      cell=[2, 2, 2], pbc=True)

        builtin = torchani.neurochem.Builtins()
        calculator = torchani.ase.Calculator(
            builtin.species, builtin.aev_computer,
            builtin.models, builtin.energy_shifter)
        atoms.set_calculator(calculator)
        e = atoms.get_potential_energy()

        for _ in range(100):
            positions = atoms.get_positions()
            translation = (numpy.random.rand(3) - 0.5) * 2
            atoms.set_positions(positions + translation)
            self.assertEqual(e, atoms.get_potential_energy())
Gao, Xiang's avatar
Gao, Xiang committed
143

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    def assertTensorEqual(self, a, b):
        self.assertLess((a - b).abs().max().item(), 1e-6)

    def testPBCConnersSeeEachOther(self):
        species = torch.tensor([[0, 0]])
        neighborlist = torchani.ase.NeighborList(cell=[10, 10, 10], pbc=True)

        xyz1 = torch.tensor([0.1, 0.1, 0.1])
        xyz2s = [
            torch.tensor([9.9, 0.0, 0.0]),
            torch.tensor([0.0, 9.9, 0.0]),
            torch.tensor([0.0, 0.0, 9.9]),
            torch.tensor([9.9, 9.9, 0.0]),
            torch.tensor([0.0, 9.9, 9.9]),
            torch.tensor([9.9, 0.0, 9.9]),
            torch.tensor([9.9, 9.9, 9.9]),
        ]

        for xyz2 in xyz2s:
            coordinates = torch.stack([xyz1, xyz2]).unsqueeze(0)
            s, _, D = neighborlist(species, coordinates, 1)
            self.assertListEqual(list(s.shape), [1, 2, 1])
            neighbor_coordinate = D[0][0].squeeze() + xyz1
            mirror = xyz2
            for i in range(3):
                if mirror[i] > 5:
                    mirror[i] -= 10
            self.assertTensorEqual(neighbor_coordinate, mirror)

    def testPBCSurfaceSeeEachOther(self):
        species = torch.tensor([[0, 0]])
        neighborlist = torchani.ase.NeighborList(cell=[10, 10, 10], pbc=True)

        for i in range(3):
            xyz1 = torch.tensor([5.0, 5.0, 5.0])
            xyz1[i] = 0.1
            xyz2 = xyz1.clone()
            xyz2[i] = 9.9

            coordinates = torch.stack([xyz1, xyz2]).unsqueeze(0)
            s, _, D = neighborlist(species, coordinates, 1)
            self.assertListEqual(list(s.shape), [1, 2, 1])
            neighbor_coordinate = D[0][0].squeeze() + xyz1
            xyz2[i] = -0.1
            self.assertTensorEqual(neighbor_coordinate, xyz2)

    def testPBCEdgesSeeEachOther(self):
        species = torch.tensor([[0, 0]])
        neighborlist = torchani.ase.NeighborList(cell=[10, 10, 10], pbc=True)

        for i, j in itertools.combinations(range(3), 2):
            xyz1 = torch.tensor([5.0, 5.0, 5.0])
            xyz1[i] = 0.1
            xyz1[j] = 0.1
            for new_i, new_j in [[0.1, 9.9], [9.9, 0.1], [9.9, 9.9]]:
                xyz2 = xyz1.clone()
                xyz2[i] = new_i
                xyz2[j] = new_i

            coordinates = torch.stack([xyz1, xyz2]).unsqueeze(0)
            s, _, D = neighborlist(species, coordinates, 1)
            self.assertListEqual(list(s.shape), [1, 2, 1])
            neighbor_coordinate = D[0][0].squeeze() + xyz1

            if xyz2[i] > 5:
                xyz2[i] = -0.1
            if xyz2[j] > 5:
                xyz2[j] = -0.1

            self.assertTensorEqual(neighbor_coordinate, xyz2)

Gao, Xiang's avatar
Gao, Xiang committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
    def testNonRectangularPBCConnersSeeEachOther(self):
        species = torch.tensor([[0, 0]])
        neighborlist = torchani.ase.NeighborList(
            cell=[10, 10, 10 * math.sqrt(2), 90, 45, 90], pbc=True)

        xyz1 = torch.tensor([0.1, 0.1, 0.05])
        xyz2 = torch.tensor([10.0, 0.1, 0.1])
        mirror = torch.tensor([0.0, 0.1, 0.1])

        coordinates = torch.stack([xyz1, xyz2]).unsqueeze(0)
        s, _, D = neighborlist(species, coordinates, 1)
        self.assertListEqual(list(s.shape), [1, 2, 1])
        neighbor_coordinate = D[0][0].squeeze() + xyz1
        for i in range(3):
            if mirror[i] > 5:
                mirror[i] -= 10
        self.assertTensorEqual(neighbor_coordinate, mirror)

Gao, Xiang's avatar
Gao, Xiang committed
233
234
235

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