Skip to content

Commit

Permalink
ASE neighborlist computer (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
zasdfgbnm committed Oct 27, 2018
1 parent 7c25379 commit 84fc8d8
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 1 deletion.
7 changes: 7 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ NeuroChem
.. automodule:: torchani.neurochem.trainer


ASE Interface
=============

.. automodule:: torchani.ase
.. autoclass:: torchani.ase.NeighborList
:members:

Ignite Helpers
==============

Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
'numpy': ('http://docs.scipy.org/doc/numpy/', None),
'torch': ('https://pytorch.org/docs/master/', None),
'ignite': ('https://pytorch.org/ignite/', None),
'ase': ('https://wiki.fysik.dtu.dk/ase/', None),
}

latex_documents = [
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'nose',
'tensorboardX',
'tqdm',
'ase',
],
}

Expand Down
7 changes: 7 additions & 0 deletions tests/test_aev.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,12 @@ def testPadding(self):
self._assertAEVEqual(expected_radial, expected_angular, aev_)


class TestAEVASENeighborList(TestAEV):

def setUp(self):
super(TestAEVASENeighborList, self).setUp()
self.aev_computer.neighborlist = torchani.ase.NeighborList()


if __name__ == '__main__':
unittest.main()
6 changes: 6 additions & 0 deletions torchani/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@

__all__ = ['AEVComputer', 'EnergyShifter', 'ANIModel', 'Ensemble',
'ignite', 'utils', 'neurochem', 'data']

try:
from . import ase # noqa: F401
__all__.append('ase')
except ImportError:
pass
82 changes: 82 additions & 0 deletions torchani/ase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# -*- coding: utf-8 -*-
"""Tools for interfacing with `ASE`_.
.. _ASE:
https://wiki.fysik.dtu.dk/ase
"""

import math
import torch
import ase.neighborlist
from . import utils


class NeighborList:
"""ASE neighborlist computer
Arguments:
cell: same as in :class:`ase.Atoms`
pbc: same as in :class:`ase.Atoms`
"""

def __init__(self, cell=None, pbc=None):
self.pbc = pbc
self.cell = cell

def __call__(self, species, coordinates, cutoff):
conformations = species.shape[0]
max_atoms = species.shape[1]
neighbor_species = []
neighbor_distances = []
neighbor_vecs = []
for i in range(conformations):
s = species[i].unsqueeze(0)
c = coordinates[i].unsqueeze(0)
s, c = utils.strip_redundant_padding(s, c)
s = s.squeeze()
c = c.squeeze()
atoms = s.shape[0]
atoms_object = ase.Atoms(
'C'*atoms, # chemical symbols are not important here
positions=c.numpy(),
pbc=self.pbc,
cell=self.cell)
idx1, idx2, d, D = ase.neighborlist.neighbor_list(
'ijdD', atoms_object, cutoff)
idx1 = torch.from_numpy(idx1).to(coordinates.device)
idx2 = torch.from_numpy(idx2).to(coordinates.device)
d = torch.from_numpy(d).to(coordinates.device) \
.to(coordinates.dtype)
D = torch.from_numpy(D).to(coordinates.device) \
.to(coordinates.dtype)
neighbor_species1 = []
neighbor_distances1 = []
neighbor_vecs1 = []
for i in range(atoms):
this_atom_indices = (idx1 == i).nonzero().flatten()
neighbor_indices = idx2[this_atom_indices]
neighbor_species1.append(s[neighbor_indices])
neighbor_distances1.append(d[this_atom_indices])
neighbor_vecs1.append(D.index_select(0, this_atom_indices))
for i in range(max_atoms - atoms):
neighbor_species1.append(torch.full((1,), -1))
neighbor_distances1.append(torch.full((1,), math.inf))
neighbor_vecs1.append(torch.full((1, 3), 0))
neighbor_species1 = torch.nn.utils.rnn.pad_sequence(
neighbor_species1, padding_value=-1)
neighbor_distances1 = torch.nn.utils.rnn.pad_sequence(
neighbor_distances1, padding_value=math.inf)
neighbor_vecs1 = torch.nn.utils.rnn.pad_sequence(
neighbor_vecs1, padding_value=0)
neighbor_species.append(neighbor_species1)
neighbor_distances.append(neighbor_distances1)
neighbor_vecs.append(neighbor_vecs1)
neighbor_species = torch.nn.utils.rnn.pad_sequence(
neighbor_species, batch_first=True, padding_value=-1)
neighbor_distances = torch.nn.utils.rnn.pad_sequence(
neighbor_distances, batch_first=True, padding_value=math.inf)
neighbor_vecs = torch.nn.utils.rnn.pad_sequence(
neighbor_vecs, batch_first=True, padding_value=0)
return neighbor_species.permute(0, 2, 1), \
neighbor_distances.permute(0, 2, 1), \
neighbor_vecs.permute(0, 2, 1, 3)
2 changes: 1 addition & 1 deletion torchani/neurochem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, filename):
name = line[0]
value = line[1]
if name == 'Rcr' or name == 'Rca':
setattr(self, name, torch.tensor(float(value)))
setattr(self, name, float(value))
elif name in ['EtaR', 'ShfR', 'Zeta',
'ShfZ', 'EtaA', 'ShfA']:
value = [float(x.strip()) for x in value.replace(
Expand Down

0 comments on commit 84fc8d8

Please sign in to comment.