5. Overview of YaffΒΆ

Yaff is a Python library that can be used to implement all sorts of force-field simulations. A useful simulation typically consists of four steps:

  1. Specification of the molecular system that will be simulated.
  2. Specification of the force field model used to compute energies and forces.
  3. An (iterative) simulation protocol, such as a Molecular Dynamics or a Monte Carlo simulation, a geometry optimization, or yet something else.
  4. Analysis of the output to extract meaningful numbers from the simulation.

Each step will be discussed in more detail in the following sections.

In Yaff, the conventional input file is replaced by an input script. This means that one must write one or more small main programs that specify what type of simulation is carried out. A minimalistic example, which covers all four steps, is given in the file data/examples/000_overview/simulation.py:

# import the yaff library
from yaff import *

# Control the amount of screen output and the unit system.
log.set_level(log.medium)
log.set_unitsys(log.joule)

# import the h5py library to write output in the HDF5 format.
import h5py as h5

# 1) specify the system (32 water molecules)
system = System.from_file('system.chk')

# 2) specify the force field (experimental FF)
ff = ForceField.generate(system, 'parameters.txt')

# Open an HDF5 trajectory file for step 3 and 4. If file exists, it is overwritten.
with h5.File('output.h5', mode='w') as f:
    # 3) Integrate Newton's equation of motion and write the trajectory in HDF5
    # format.
    hdf5_writer = HDF5Writer(f)
    xyz = XYZWriter('traj.xyz')
    verlet = VerletIntegrator(ff, 1*femtosecond, hooks=[hdf5_writer, xyz], temp0=300)
    verlet.run(100)

    # 4) perform an analysis, in this case an RDF computation for O-O pairs.
    indexes = system.get_indexes('O')
    rdf = RDF(4.5*angstrom, 0.1*angstrom, f, select0=indexes)
    rdf.plot('rdf.png')

Yaff internally works with atomic units, although other unit systems can be used in input and (some) output files. The units used for the screen output are controlled with the log.set_unitsys method. Output written in HDF5 files will always be in atomic units. When output is written to a format from other projects/programs, the units of that program/project will be used.

Numpy, Cython and h5py are used extensively in Yaff for numerical efficiency. The examples below often use Numpy too, assuming the following import statement:

import numpy as np, h5py as h5