GromacsWrapper (package gromacs) is a thin shell around the Gromacs tools for light-weight integration into python scripts or interactive use in ipython.
The following examples should simply convey the flavour of using the package. See the individual modules for more examples.
In python:
help(gromacs.g_dist)
gromacs.g_dist.help()
gromacs.g_dist.help(long=True)
In ipython:
gromacs.g_dist ?
Gromacs flags are given as python keyword arguments:
gromacs.g_dist(v=True, s='topol.tpr', f='md.xtc', o='dist.xvg', dist=1.2)
Input to stdin of the command can be supplied:
gromacs.make_ndx(f='topol.tpr', o='md.ndx',
input=('keep "SOL"', '"SOL" | r NA | r CL', 'name 2 solvent', 'q'))
Output of the command can be caught in a variable and analyzed:
rc, output, junk = gromacs.grompp(..., stdout=False) # collects command output
for line in output.split('\n'):
line = line.strip()
if line.startswith('System has non-zero total charge:'):
qtot = float(line[34:])
break
(See gromacs.cbook.grompp_qtot() for a more robust implementation of this application.)
A number of package-specific exceptions (GromacsError) and warnings (Gromacs*Warning, AutoCorrectionWarning, BadParameterWarning) can be raised.
If you want to stop execution at, for instance, a AutoCorrectionWarning or BadParameterWarning then use the python warnings filter:
import warnings
warnings.simplefilter('error', gromacs.AutoCorrectionWarning)
warnings.simplefilter('error', gromacs.BadParameterWarning)
This will make python raise an exception instead of moving on. The default is to always report, eg:
warnings.simplefilter('always', gromacs.BadParameterWarning)
The following exceptions are defined:
Error raised when a gromacs tool fails.
Returns error code in the errno attribute and a string in strerror. # TODO: return status code and possibly error message
Error raised when prerequisite data are not available.
For analysis with gromacs.analysis.core.Simulation this typically means that the analyze() method has to be run first.
The following warnings are defined:
The library uses python’s logging module to keep a history of what it has been doing. In particular, every wrapped Gromacs command logs its command line (including piped input) to the log file (configured in gromacs.config.logfilename). This facilitates debugging or simple re-use of command lines for very quick and dirty work. The logging facilty appends to the log file and time-stamps every entry. See gromacs.config for more details on configuration.
The package version can be queried with the gromacs.get_version() function.