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 (GromacsFailureWarning, GromacsImportWarning, GromacsValueWarning, 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.
Error raised when parsing of a file failed.
The following warnings are defined:
Warning about failure of a Gromacs tool.
Warns about problems with using a gromacs tool.
Warns about problems with the value of an option or variable.
Warns about cases when the code is choosing new values automatically.
Warns if some parameters or variables are unlikely to be appropriate or correct.
Warns when prerequisite data/files are not available.
Warns if usage is unexpected/documentation ambiguous.
Warns that results may possibly have low accuracy.
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.
It is also possible to capture output from Gromacs commands in a file instead of displaying it on screen, as described under Input and Output.
The package version can be queried with the gromacs.get_version() function.
Return current package version as a string.
Return current package version as a tuple (MAJOR, MINOR, PATCHLEVEL).
If the package was installed from a development version, the patch level will have the string “-dev” affixed to distinguish it from a release.