install
$ pip install torchani
I recommend creating a new project with poetry and install it to manage libraries dependencies.
$ mkdir <new-project>
$ cd <new-project>
$ poetry init
$ poetry config virtualenvs.in-project true --local
$ poetry add torchani
$ poetry install
In the smith compute nodes, we don’t have internet access and cannot download models from the outside via API.
So, we have to contruct the trained NN model manually from NeuroChem files.
Clone this repository inclding trained models.
$ git clone git@github.com:aiqm/torchani.git
Sample code to calculate enegies and forces from the trained model. https://aiqm.github.io/torchani/examples/load_from_neurochem.html
import os
import torch
import torchani
import ase
path = '<path-to-torchani>'
const_file = os.path.join(path, 'torchani/resources/ani-1x_8x/rHCNO-5.2R_16-3.5A_a4-8.params')
consts = torchani.neurochem.Constants(const_file)
aev_computer = torchani.AEVComputer(**consts)
sae_file = os.path.join(path, 'torchani/resources/ani-1x_8x/sae_linfit.dat')
energy_shifter = torchani.neurochem.load_sae(sae_file)
model_prefix = os.path.join(path, 'torchani/resources/ani-1x_8x/train')
ensemble = torchani.neurochem.load_model_ensemble(consts.species, model_prefix, 8)
coordinates = torch.tensor([[[0.03192167, 0.00638559, 0.01301679],
[-0.83140486, 0.39370209, -0.26395324],
[-0.66518241, -0.84461308, 0.20759389],
[0.45554739, 0.54289633, 0.81170881],
[0.66091919, -0.16799635, -0.91037834]]],
requires_grad=True)
species = consts.species_to_tensor(['C', 'H', 'H', 'H', 'H']).unsqueeze(0)
methane = ase.Atoms(['C', 'H', 'H', 'H', 'H'], positions=coordinates.squeeze().detach().numpy())
nnp1 = torchani.nn.Sequential(aev_computer, ensemble, energy_shifter)
energy = nnp1((species, coordinates)).energies
derivative = torch.autograd.grad(energy.sum(), coordinates)[0]
force = -derivative
print('Energy:', energy.item())
print('Force:', force.squeeze())
=> Energy: -40.459022105724976
Force: tensor([[ 0.0306, -0.1316, -0.0527],
[-0.1293, 0.1639, -0.0774],
[ 0.0856, -0.0429, 0.0408],
[ 0.0268, 0.0060, 0.0381],
[-0.0138, 0.0046, 0.0511]])