>
= Resources =
[[http://openmm.org/documentation.html|OpenMM Documentation (of these links, the User Guide is the most important)]]
[[http://builder.openmm.org/|OpenMM Script Builder (Useful for Running Simple Simulations)]]
[[http://pubs.acs.org/doi/abs/10.1021/jp910674d|AMOEBA Model Overview (useful for understanding multipole conventions used by OpenMM)]]
= Overview =
This tutorial will teach you to, first, implement custom force fields in OpenMM and, second, to run simple simulations using these force fields. Here we will use a model for CO2 as an example; however, the content of this tutorial should be applicable to many different force fields and systems.
For this tutorial, it's expected that you've read through the OpenMM User's Guide and are familiar with its contents. Furthermore, you should have already installed and tested OpenMM. Finally, for the examples in this tutorial you'll need the following files:
[[attachment:openmm_tutorial.tgz]]
Untar the file and make sure you can run the first example by executing the following commands:
{{{
$ tar -xvf openmm_tutorial.tgz
$ cd 01_flexible_isotropic_model
$ python run_co2.py
}}}
The sample output you get should be similar (but likely not identical) to the sample output code located in each directory.
= Implementing New Potentials =
== Electrostatics ==
He we will be using the files found in the first subdirectory, 01_flexible_isotropic_model. Looking inside the file sapt_co2.xml (which contains our force field), the following block of xml code represents the model for electrostatics:
{{{
code xml
}}}
This OpenMM Force encodes AMOEBA-style electrostatics and polarization, which is very similar to the functional forms used by ORIENT. See [[http://pubs.acs.org/doi/abs/10.1021/jp910674d|Ponder2010]] for a good reference on the functional forms and conventions used by AMOEBA.
Several points are worth mention. First, each multipole tag represents the permanent electrostatics for a particular atomtype. Each tag represents the following:
1. '''Type:''' Atomtype defined by the multipole tag. This should correspond to the atomtype name given in the section. Note that, while in general OpenMM allows the user to give arbitrary names to atomtypes, AmoebaMultipoleForce requires that all atom types be integers. In the example above, the first multipole tag is for carbon, atomtype 1.
1. '''kz''' Atomtype defining the z-axis of the local reference frame. In the example above, kz="2" implies that the local reference frame for carbon (atomtype 1) has a z axis pointing from the central carbon to atomtype 2 (oxygen). Note that, in this case, there are two matching atoms that could define the z-axis, and internally OpenMM might choose either one. It's up to to ensure that your electrostatic energies don't depend on which atom is chosen as the z-axis, but there should be no problem so long as your multipoles obey the local symmetry of the system.
1. '''kx''' Atomtype defining the xz-plane of the local reference frame. In the example above, neither atomtype 1 nor atomtype 2 should be sensitive to the choice of z-axis, so we have simply written kx="0". There isn't (and shouldn't be!) an atomtype 0; instead, this is a flag to tell OpenMM to use the global coordinate frame for the x-axis.
1. '''c0, d1, d2, etc.''' Cartesian multipole moments for the atomtype, expressed in the local reference frame.
{{{#!wiki important
Important
Depending on the axis convention used, you may need more flexibility in defining the z and x axes. See the following resources for details on how to do this:
* [[https://simtk.org/plugins/phpBB/viewtopicPhpbb.php?f=161&t=6316&p=16109&start=0&view=|Local reference definitions explained]]
* [[https://github.com/pandegroup/openmm/blob/master/wrappers/python/simtk/openmm/app/forcefield.py#L4456|Python code defining different local axis system conventions]]
}}}
{{{#!wiki important
Important
At the time of this writing, the scale factors represented in the !AmoebaMultipoleForce tag are hard coded in, and can't actually be modified in the xml file. See [[https://simtk.org/plugins/phpBB/viewtopicPhpbb.php?f=161&t=7427&p=19657&start=0&view=|the OpenMM forum]] for details.
}}}
To obtain parameters for the AmoebaMultipoleForce, it is necessary to convert the multipole moments output by ORIENT (which are usually given in atomic units, and in the global reference frame) into the input format for OpenMM (SI units, local reference frame). The following script will perform the necessary conversion, such that usually only the atomtype and kz/kx fields need manual editing:
[[attachment:generate_electrostatic_parametrs.tgz]]
This script is run as follows (again using co2 as an example):
{{{
code bash
$ python convert_mom_to_xml.py co2_ISA-GRID_L2.mom co2.axes
}}}
{{{#!wiki important
Important
My .axes file follows a slightly different format than ORIENT's. Also note that, for this system, the partial charges don't add up precisely to zero. Before simulating this system in OpenMM, you may need to manually edit the c0 fields such that you end up with a neutral system.
}}}
== Polarization ==
Polarization is also described using the !AmoebaMultipoleForce, and is given by the Polarize tags. Each polarize tag contains the following information:
1. '''type''': same as above
1. '''polarizability''': Isotropic induced dipole polarizability, given in units of nm^3
1. '''thole''': Dimensionless Thole parameter, following the damping function used by AMOEBA.
1. '''pgrp1, pgrp2, etc.''': Polarization groups for that atomtype. See [[http://pubs.acs.org/doi/abs/10.1021/ct4003702|Shi2013]] for details. In brief, the permanent multipoles of atoms within the same polarization group do not polarize each other. In this example, C and O both belong in the same polarization group.
{{{#!wiki warning
Warning
As of this writing, sometimes using a polarizability of exactly zero leads to divergences in the PME calculation. If you use a polarizability of exactly zero for an atom, and see infinite energies in the AmoebaMultipoleForce, using a very small polarizability (say 1e-9) will fix this problem.
}}}
== Non-electrostatic Pairwise Energies ==
=== Isotropic Models ===
Aside from electrostatic and polarization energies (described above), custom pair potentials in OpenMM can be implemented using CustomNonbondedForce. The [[http://docs.openmm.org/7.1.0/userguide/application.html#customnonbondedforce|OpenMM documentation]] does a good job of describing how to use this force and generate your own xml files, so you should read over this documentation before proceeding.
Below I've copied part of the sapt_co2.xml file, which shows the isotropic pair potential using a functional form given in [[http://pubs.acs.org/doi/abs/10.1021/acs.jctc.6b00209|VanVleet2016]]:
{{{
code xml
}}}
In particular, this section of the xml file describes the exponentially-decaying charge penetration terms and the damped dispersion expansion. The OpenMM documentation is quite good for CustomNonbondedForce, so consult this guide for further questions.
=== Anisotropic Models ===
Anisotropic pair potentials are also (albeit in a limited sense) available in OpenMM. The Schmidt group is actively working on a developing a new force type (CustomAnisotropicNonbondedForce) which will allow anisotropic force fields to be developed in a general sense. In the meantime, a workaround (which uses CustomHbondForce) is available, and can be used on small molecules. Note that CustomHbondForce has not been optimized very well for the CPU platform, and the GPU code for running the anisotropic potential (see subdirectory 02_flexible_anisotropic_potential in the example files given above) runs at least 100x faster than the CPU code. In short, contact the Schmidt group before implementing your own anisotropic potentials, as we soon hope to have better implementations for these models.
Using CustomHbondForce, the anisotropic pair potential for CO2 looks as follows:
{{{
code xml
Scale factor to avoid double counting:
For both Donor's and acceptors, class1 should be the particle, class2 should be the z-axis atom, and class3 should be the x-axis atom.
}}}
Several points are of note (most of which showcase why !CustomAnisotropicNonbonedForce is being developed in the first place). All interactions between Donor Particles (here C and each oxygen) are Acceptor Particles (again C and O1,O2) are counted, so a scale factor of 1/2 is necessary to avoid double counting. Second, !CustomHbondForce allows angles and dihedrals between atoms as input variables, and the local coordinate system can be calculated in this manner. Otherwise, this is a standard use of !CustomHbondForce, and the [[http://docs.openmm.org/7.1.0/userguide/application.html#customhbondforce|OpenMM documentation]] for this class does a good job of describing how to use it.
=== Long-range Corrections ===
As a final implementation note, it is generally important in simulations to use long-range corrections for both the electrostatic and dispersion terms. Available long-range corrections differ based on which force is being used, but I've put some good defaults in the run_co2.py script provided in the example directory above:
{{{
code python
# Set distance cutoffs, constraints, and other force-specific options for each
# force we might encounter
for force in system.getForces():
if isinstance(force, mm.CustomHbondForce):
force.setNonbondedMethod(mm.CustomHbondForce.CutoffPeriodic)
force.setCutoffDistance(two_body_cutoff)
elif isinstance(force, mm.CustomNonbondedForce):
force.setNonbondedMethod(mm.CustomNonbondedForce.CutoffPeriodic)
force.setCutoffDistance(two_body_cutoff)
force.setUseLongRangeCorrection(True)
elif isinstance(force, mm.AmoebaMultipoleForce):
force.setNonbondedMethod(mm.AmoebaMultipoleForce.PME)
elif isinstance(force, mm.NonbondedForce):
force.setNonbondedMethod(mm.NonbondedForce.LJPME)
force.setCutoffDistance(two_body_cutoff)
elif isinstance(force, mm.CustomManyParticleForce):
force.setNonbondedMethod(mm.CustomManyParticleForce.CutoffPeriodic)
force.setCutoffDistance(three_body_cutoff)
else:
pass
}}}
Here you can see that we're using PME for electrostatics. For dispersion, this script uses a cutoff with a set cutoff distance, and additionally corrects for energies beyond the cutoff using a long range correction term (called via the command "force.setUseLongRangeCorrection(True)" and as described in [[http://docs.openmm.org/7.0.0/api-python/classsimtk_1_1openmm_1_1openmm_1_1CustomNonbondedForce.html|the OpenMM documentation.]]
Finally, note that, for anisotropic force fields, CustomHbondForce has no option for a long-range correction. In the example directory 02_flexible_anisotropic_potential, I've subtracted off the isotropic dispersion energy from CustomHbondForce and added it to CustomNonbondedForce, which then allows me to add a long-range correction.
=== Combination Rules ===
In general, OpenMM assumes that you will use combination rules to describe cross-interactions in the pair potential. I have not tried using explicit cross-terms, but this is in principle possible. See [[https://raw.githubusercontent.com/pandegroup/openmm/master/wrappers/python/simtk/openmm/app/data/charmm_polar_2013.xml|this example xml file]] for an example where this is done.
== Non-electrostatic Many-Body Energies ==