test_ase.py 1.2 KB
Newer Older
Gao, Xiang's avatar
Gao, Xiang committed
1
2
from ase.lattice.cubic import Diamond
from ase.md.langevin import Langevin
3
from ase import units
Gao, Xiang's avatar
Gao, Xiang committed
4
5
6
7
from ase.calculators.test import numeric_force
import torch
import torchani
import unittest
8
9
10
11
12
import os

path = os.path.dirname(os.path.realpath(__file__))
N = 97
tol = 5e-5
Gao, Xiang's avatar
Gao, Xiang committed
13
14
15


def get_numeric_force(atoms, eps):
16
    fn = torch.zeros((len(atoms), 3), dtype=torch.double)
Gao, Xiang's avatar
Gao, Xiang committed
17
18
19
20
21
22
23
24
    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):

25
26
    def testWithNumericalForceWithPBCEnabled(self):
        atoms = Diamond(symbol="C", pbc=True)
Gao, Xiang's avatar
Gao, Xiang committed
27
28
29
30
31
32
33
34
35
36
37
        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
38
39
40
        if avgf > 0:
            self.assertLess(df / avgf, 0.1)

Gao, Xiang's avatar
Gao, Xiang committed
41
42
43

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