ase_interface.py 2.45 KB
Newer Older
Gao, Xiang's avatar
Gao, Xiang committed
1
2
# -*- coding: utf-8 -*-
"""
3
4
Structure minimization and constant temperature MD using ASE interface
======================================================================
Gao, Xiang's avatar
Gao, Xiang committed
5

6
7
8
This example is modified from the official `home page` and
`Constant temperature MD`_ to use the ASE interface of TorchANI as energy
calculator.
Gao, Xiang's avatar
Gao, Xiang committed
9

10
11
.. _home page:
    https://wiki.fysik.dtu.dk/ase/
Gao, Xiang's avatar
Gao, Xiang committed
12
13
14
15
16
17
18
19
20
.. _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
21
from ase.optimize import BFGS
Gao, Xiang's avatar
Gao, Xiang committed
22
23
24
25
26
27
28
from ase import units
import torchani


###############################################################################
# Now let's set up a crystal
atoms = Diamond(symbol="C", pbc=True)
29
print(len(atoms), "atoms in the cell")
Gao, Xiang's avatar
Gao, Xiang committed
30
31
32
33
34
35
36
37
38

###############################################################################
# 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)

###############################################################################
39
40
41
42
43
# Now let's minimize the structure:
print("Begin minimizing...")
opt = BFGS(atoms)
opt.run(fmax=0.001)
print()
Gao, Xiang's avatar
Gao, Xiang committed
44
45
46


###############################################################################
47
48
# Now create a callback function that print interesting physical quantities:
def printenergy(a=atoms):
Gao, Xiang's avatar
Gao, Xiang committed
49
50
51
52
53
54
55
    """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))


56
57
58
59
60
61
###############################################################################
# We want to run MD with constant energy using the Langevin algorithm
# with a time step of 1 fs, the temperature 300K and the friction
# coefficient to 0.02 atomic units.
dyn = Langevin(atoms, 1 * units.fs, 300 * units.kB, 0.2)
dyn.attach(printenergy, interval=500)
Gao, Xiang's avatar
Gao, Xiang committed
62
63
64

###############################################################################
# Now run the dynamics:
65
print("Beginning dynamics...")
Gao, Xiang's avatar
Gao, Xiang committed
66
67
printenergy()
dyn.run(500)