-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
125 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Constant temperature MD using ASE interface | ||
=========================================== | ||
This example is modified from the official `Constant temperature MD`_ to use | ||
the ASE interface of TorchANI as energy calculator. | ||
.. _Constant temperature MD: | ||
https://wiki.fysik.dtu.dk/ase/tutorials/md/md.html#constant-temperature-md | ||
""" | ||
|
||
|
||
############################################################################### | ||
# To begin with, let's first import the modules we will use: | ||
from ase.lattice.cubic import Diamond | ||
from ase.md.langevin import Langevin | ||
from ase import units | ||
import torchani | ||
|
||
|
||
############################################################################### | ||
# Now let's set up a crystal | ||
atoms = Diamond(symbol="C", pbc=True) | ||
|
||
############################################################################### | ||
# Now let's create a calculator from builtin models: | ||
builtin = torchani.neurochem.Builtins() | ||
calculator = torchani.ase.Calculator(builtin.species, builtin.aev_computer, | ||
builtin.models, builtin.energy_shifter) | ||
atoms.set_calculator(calculator) | ||
|
||
############################################################################### | ||
# We want to run MD with constant energy using the Langevin algorithm | ||
# with a time step of 5 fs, the temperature 50K and the friction | ||
# coefficient to 0.02 atomic units. | ||
dyn = Langevin(atoms, 5 * units.fs, 50 * units.kB, 0.002) | ||
|
||
|
||
############################################################################### | ||
# Let's print energies every 50 steps: | ||
def printenergy(a=atoms): # store a reference to atoms in the definition. | ||
"""Function to print the potential, kinetic and total energy.""" | ||
epot = a.get_potential_energy() / len(a) | ||
ekin = a.get_kinetic_energy() / len(a) | ||
print('Energy per atom: Epot = %.3feV Ekin = %.3feV (T=%3.0fK) ' | ||
'Etot = %.3feV' % (epot, ekin, ekin / (1.5 * units.kB), epot + ekin)) | ||
|
||
|
||
dyn.attach(printenergy, interval=50) | ||
|
||
############################################################################### | ||
# Now run the dynamics: | ||
printenergy() | ||
dyn.run(500) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from ase.lattice.cubic import Diamond | ||
from ase.md.langevin import Langevin | ||
from ase import units | ||
from ase.calculators.test import numeric_force | ||
import torch | ||
import torchani | ||
import unittest | ||
|
||
|
||
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): | ||
|
||
def testForceWithPBCEnabled(self): | ||
atoms = Diamond(symbol="C", pbc=True) | ||
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() | ||
self.assertLess(df / avgf, 0.1) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters