Gromacs  2025-dev-20241003-bd59e46
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Using GROMACS as a library

Getting started

The GROMACS library (libgromacs) provides a few different alternatives for using it. These are listed here from the highest level of abstraction to the low-level functions.

If these do not fit your needs, you may need to modify the GROMACS source code yourself. In particular, it is currently relatively difficult to extend the functionality of mdrun without modifying the source code directly. If you think that some particular API would be necessary for your work, and think that it would be easy to expose, please drop a line on the developer discussion forum, or contribute the necessary changes on https://gitlab.com/gromacs/gromacs.

Linking against libgromacs

GROMACS is a bit picky on how the headers need to be used: depending on compilation options used for GROMACS, some preprocessor defines may need to be set, the required include path may also depend on compilation options, and some extra libraries may need to be linked. You will also likely need to use the same compiler (or sufficiently similar one that uses the same standard library) that was used to compile GROMACS.

To manage this more easily, GROMACS provides two mechanisms for getting the correct flags for compilation and linking against the GROMACS library:

These mechanisms are currently provided on a best-effort basis, but are not routinely tested on a wide range of configurations. Please report any issues with details of how GROMACS was built so that the mechanism can be improved. Known issues:

CMake find_package(GROMACS) details

The CMake machinery to support find_package(GROMACS) has two parts: a FindGROMACS.cmake find module (found in share/gromacs/template/cmake/ in the installation and share/template/cmake/ in the source tree), and actual package configuration files (gromacs-config.cmake and supporting files installed to share/cmake/ from input files in src/gromacs/).

FindGROMACS.cmake is a simple wrapper over the package configuration files, providing a somewhat more convenient interface to the machinery that supports multiple suffixed GROMACS installations in the same installation prefix (see GROMACS_SUFFIX variable below). This file is intended to be version-agnostic and remain both forward- and backward-compatible even between major GROMACS releases. All version-specific information and the actual details about the compilation and linking settings is in the package configuration files. Build systems willing to utilize FindGROMACS.cmake can create a local copy of it and use it like it is used in the installed share/gromacs/template/CMakeLists.txt. The package configuration files can also be used directly if desired, bypassing FindGROMACS.cmake.

Input options for influencing what to find:

GROMACS_SUFFIX (only for FindGROMACS.cmake)
This CMake variable can be set before calling find_package(GROMACS) to specify the GROMACS suffix to search for. If not set, an unsuffixed version is searched for. If using the package configuration files directly, the suffix must be set using find_package(GROMACS NAMES gromacs<suffix>).
GROMACS_PREFER_STATIC
This CMake variable can be set before calling find_package(GROMACS) to specify whether static or shared libraries are preferred if both are available. It does not affect which GROMACS installation is chosen, but if that installation has both static and shared libraries available (installed from two different builds with the same suffix), then this chooses the library to be returned in GROMACS_LIBRARIES.
GROMACS_DIR
This CMake (cache) variable is a standard mechanism provided by find_package, and can be used to specify a hint where to search for GROMACS. Also CMAKE_PREFIX_PATH can be used for this purpose; see CMake documentation for find_package for more details. GROMACS_DIR can also be set as an environment variable, and this is done by GMXRC.

Output variables that specify how the found libgromacs and header should be used:

GROMACS_INCLUDE_DIRS
List of include directories necessary to compile against the GROMACS headers. Currently, this includes the path to GROMACS headers.
GROMACS_LIBRARIES
List of libraries to link with to link against GROMACS. Under the hood, this uses imported CMake targets to represent libgromacs.
GROMACS_DEFINITIONS
List of compile definitions (with -D in front) that are required to compile the GROMACS headers.
GROMACS_IS_DOUBLE
Whether the found GROMACS was compiled in double precision.
GROMACS_CXX_FLAGS
Required compiler flags.

Additionally an imported target named Gromacs::libgromacs is provided and can be used with target_link_libraries(foo PRIVATE Gromacs::libgromacs).

Declared macros/functions that can be used for checking for correctness of some settings:

gromacs_check_double(GMX_DOUBLE)
Checks that the found GROMACS is in the expected precision. The parameter GMX_DOUBLE should be the name of a cache variable that specified whether double-precision was requested.
gromacs_check_compiler(LANG)
Checks that the found GROMACS was compiled with the same compiler that is used by the current CMake system. Currently only LANG=CXX is supported.

Notes on GROMACS API

The headers for the public GROMACS API are installed in include/gromacs/ under the installation directory. The layout reflects the source code layout under the src/gromacs/ directory (see Source code layout). The headers directly under include/gromacs/ do not contain any declarations, but instead include a collection of headers from subdirectories. You should prefer to include these convenience headers instead of individual headers from the subdirectories, since they are much more stable. The individual headers in the subdirectories can be renamed or moved, but the goal is to only rarely change the name of these top-level headers.

Pre-5.0 versions of GROMACS installed (nearly) all headers directly under include/gromacs/. Most of these headers still exist, but are no longer installed. The long-term goal is to reintroduce those parts of the API that make sense, but unfortunately this can take a long time. Thus, you should not expect much stability from the API in these headers.

For headers under other subdirectories, some effort has been put to design the API for stability. However, with limited development resources, and the focus of GROMACS being in high performance simulations, all the APIs are subject to change without notice. With each new release (with possible exception of patch releases), you should expect incompatible API changes.

The header version.h (installed as gromacs/version.h) provides defines that calling code can use to check the exact (released) version of GROMACS that installed the headers.

This Doxygen documentation only covers part of the API.