-
Notifications
You must be signed in to change notification settings - Fork 26
/
aimnet2ase.py
70 lines (58 loc) · 2.49 KB
/
aimnet2ase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import torch
import torch.nn.functional as F
import ase.calculators.calculator
import numpy as np
class AIMNet2Calculator(ase.calculators.calculator.Calculator):
""" ASE calculator for AIMNet2 model
Arguments:
model (:class:`torch.nn.Module`): AIMNet2 model
charge (int or float): molecular charge. Default: 0
"""
implemented_properties = ['energy', 'forces', 'free_energy', 'charges']
def __init__(self, model, charge=0):
super().__init__()
self.model = model
self.charge = charge
self.device = next(model.parameters()).device
cutoff = max(v.item() for k, v in model.state_dict().items() if k.endswith('aev.rc_s'))
self.cutoff = float(cutoff)
self._t_numbers = None
self._t_charge = None
def do_reset(self):
self._t_numbers = None
self._t_charge = None
self.charge = 0.0
def set_charge(self, charge):
self.charge = float(charge)
def _make_input(self):
coord = torch.as_tensor(self.atoms.positions).to(torch.float).to(self.device).unsqueeze(0)
if self._t_numbers is None:
self._t_numbers = torch.as_tensor(self.atoms.numbers).to(torch.long).to(self.device).unsqueeze(0)
self._t_charge = torch.tensor([self.charge], dtype=torch.float, device=self.device)
d = dict(coord=coord, numbers=self._t_numbers, charge=self._t_charge)
return d
def _eval_model(self, d, forces=True):
prev = torch.is_grad_enabled()
torch._C._set_grad_enabled(forces)
if forces:
d['coord'].requires_grad_(True)
_out = self.model(d)
ret = dict(energy=_out['energy'].item(), charges=_out['charges'].detach()[0].cpu().numpy())
if forces:
if 'forces' in _out:
f = _out['forces'][0]
else:
f = - torch.autograd.grad(_out['energy'], d['coord'])[0][0]
ret['forces'] = f.detach().cpu().numpy()
torch._C._set_grad_enabled(prev)
return ret
def calculate(self, atoms=None, properties=['energy'],
system_changes=ase.calculators.calculator.all_changes):
super().calculate(atoms, properties, system_changes)
_in = self._make_input()
do_forces = 'forces' in properties
_out = self._eval_model(_in, do_forces)
self.results['energy'] = _out['energy']
self.results['charges'] = _out['charges']
if do_forces:
self.results['forces'] = _out['forces']