0e72c3cfcd471ea5e2f02debbe1fa73c46b38ad3
[alexxy/gromacs.git] / docs / doxygen / user / usinglibrary.md
1 Using \Gromacs as a library {#page_usinglibrary}
2 ===========================
3
4 Getting started
5 ===============
6
7 The \Gromacs library (`libgromacs`) provides a few different alternatives for
8 using it.  These are listed here from the highest level of abstraction to the
9 low-level functions.
10  - If you are writing a trajectory analysis tool, please see
11    \ref page_analysisframework.  \ref page_analysistemplate should contain
12    all the ingredients to get started.
13    If you have an existing tool written using the analysis template from 4.5 or
14    4.6 (using the selection engine added in 4.5), you need to do some
15    conversion work to get this work with the new template.  This is mostly
16    straightforward, but requires some studying to understand the new framework.
17  - If you are writing a command line tool for some other purpose, you can use
18    the facilities provided by \ref module_commandline.  There are a few
19    different alternatives, depending on how much control you want to give
20    \Gromacs:
21     - For C++ code, you can implement gmx::CommandLineOptionsModuleInterface and
22       use gmx::runCommandLineModule() to execute it.  This interface assumes
23       the use of the gmx::Options mechanism for declaring command-line options
24       (see \ref module_options).
25       For a lower-level interface, gmx::CommandLineModuleInterface can be used,
26       but this requires you to implement `-h` output and command-line parsing
27       yourself (possibly using classes that \Gromacs provides).
28     - For C code, you can use gmx_run_cmain() to wrap an existing C main
29       method.  The only constraint on the provided main method is that it
30       should use parse_common_args() for argument processing.
31       This approach should allow you to convert existing C tools written
32       against pre-5.0 \Gromacs (e.g., using the analysis template from 4.0 or
33       earlier) to the new version.
34     - If you want more control (for example, you do not want the default
35       command line options added by \Gromacs), you can directly initialize
36       \Gromacs using gmx::initForCommandLine() before calling other \Gromacs
37       routines.  This allows you to write your own handling for command line
38       options from scratch.  This is also discussed in \ref module_commandline.
39  - For most control, you can use gmx::init() to do basic initialization, create
40    your own implementation for gmx::ProgramContextInterface, and set that using
41    gmx::setProgramContext().  This allows you to customize how the \Gromacs
42    library shows the name of the program in messages, as well as how it locates
43    its own data files.
44
45 If these do not fit your needs, you may need to modify the \Gromacs source code
46 yourself.  In particular, it is currently relatively difficult to extend the
47 functionality of `mdrun` without modifying the source code directly.
48 If you think that some particular API would be necessary for your work, and
49 think that it would be easy to expose, please drop a line on the
50 `gmx-developers` mailing list, or contribute the necessary changes on
51 http://gerrit.gromacs.org/.
52
53 Linking against `libgromacs`
54 ============================
55
56 \Gromacs is a bit picky on how the headers need to be used: depending on
57 compilation options used for \Gromacs, some preprocessor defines may need to be
58 set, the required include path may also depend on compilation options, and some
59 extra libraries may need to be linked.  You will also likely need to use the
60 same compiler (or sufficiently similar one that uses the same standard library)
61 that was used to compile \Gromacs.
62
63 To manage this more easily, \Gromacs provides two mechanisms for getting the
64 correct flags for compilation and linking against the \Gromacs library:
65  - `pkg-config`: \Gromacs installs `libgromacs.pc` file (suffixed with the
66    library suffix) for use with `pkg-config` if that is present on the system.
67    Sourcing `GMXRC` adjusts the `pkg-config` search path such that these files
68    are found automatically.
69    See `Makefile.pkg` installed with the analysis template for one example of
70    how to use it (to use it with a differently suffixed \Gromacs, just replace
71    `libgromacs` with `libgromacs`<em>_suffix</em> in the `pkg-config` calls).
72  - CMake package configuration files and a find module that allow
73    `find_package(GROMACS)` to work.  See below for details about how to use
74    this in CMake.  Sourcing `GMXRC` sets an environment variable that allows
75    CMake to find the configuration file automatically.
76    See `CMakeLists.txt` installed with the analysis template for one example of
77    how to use it.
78
79 These mechanisms are currently provided on a best-effort basis, but are not
80 routinely tested on a wide range of configurations.  Please report any issues
81 with details of how \Gromacs was built so that the mechanism can be improved.
82 Known issues:
83  - `pkg-config` files are not relocatable, i.e., they hard-code the
84    installation prefix as an absolute path.
85  - Installing both static and shared libraries with the same suffix to the same
86    installation prefix is guaranteed to work only if both are built with
87    exactly the same configuration options (except for `BUILD_SHARED_LIBS`) from
88    exactly the same version.  There are several files that are shared between
89    the installations in such a case, and the latter installation will overwrite
90    those from the former.
91  - Further, if both static and shared libraries have been installed in the past
92    to a prefix, then future installations to the same prefix should also
93    install both static and shared libraries.  Otherwise, some obsolete CMake
94    package configuration files will be left behind which can lead to finding
95    the old library.  Alternatively, you can delete `share/cmake/` from the
96    installation directory before doing the install.
97  - If a mechanism other than the CMake-generated `install` target is used to
98    install \Gromacs over an existing installation, and the build type (e.g.,
99    Release vs.\ Debug) does not match what was previously installed, some
100    obsolete CMake import target definition files are left behind in
101    `share/cmake/`, and may cause failures whey trying to use the package
102    configuration files.
103  - If \Gromacs is built with `GMX_BUILD_OWN_FFTW=ON`, the CMake-generated
104    import definitions for `libgromacs` reference a `gmxfftw` target that was
105    used in the build to reference the `fftw` library.  As this library only
106    exists in the \Gromacs build tree, and the CMake machinery does not write
107    any import definitions for it anywhere, linking will fail with errors about
108    not being able to find a `gmxfftw` library.  So the CMake package
109    configuration files can only be used with `GMX_BUILD_OWN_FFTW=OFF`.
110
111 CMake `find_package(GROMACS)` details
112 -------------------------------------
113
114 The CMake machinery to support `find_package(GROMACS)` has two parts: a
115 `FindGROMACS.cmake` find module (found in `share/gromacs/template/cmake/` in
116 the installation and `share/template/cmake/` in the source tree), and actual
117 package configuration files (`gromacs-config.cmake` and supporting files
118 installed to `share/cmake/` from input files in `src/gromacs/`).
119
120 `FindGROMACS.cmake` is a simple wrapper over the package configuration files,
121 providing a somewhat more convenient interface to the machinery that supports
122 multiple suffixed \Gromacs installations in the same installation prefix (see
123 `GROMACS_SUFFIX` variable below).  This file is intended to be version-agnostic
124 and remain both forward- and backward-compatible even between major \Gromacs
125 releases.  All version-specific information and the actual details about the
126 compilation and linking settings is in the package configuration files.
127 Build systems willing to utilize `FindGROMACS.cmake` can create a local copy of
128 it and use it like it is used in the installed
129 `share/gromacs/template/CMakeLists.txt`.
130 The package configuration files can also be used directly if desired, bypassing
131 `FindGROMACS.cmake`.
132
133 Input options for influencing what to find:
134
135 <dl>
136 <dt>`GROMACS_SUFFIX` (only for `FindGROMACS.cmake`)</dt>
137 <dd>This CMake variable can be set before calling `find_package(GROMACS)` to
138 specify the \Gromacs suffix to search for.  If not set, an unsuffixed version
139 is searched for.  If using the package configuration files directly, the suffix
140 must be set using `find_package(GROMACS NAMES gromacs<suffix>)`.</dd>
141 <dt>`GROMACS_PREFER_STATIC`</dt>
142 <dd>This CMake variable can be set before calling `find_package(GROMACS)` to
143 specify whether static or shared libraries are preferred if both are available.
144 It does not affect which \Gromacs installation is chosen, but if that
145 installation has both static and shared libraries available (installed from two
146 different builds with the same suffix), then this chooses the library to be
147 returned in `GROMACS_LIBRARIES`.</dd>
148 <dt>`GROMACS_DIR`</dt>
149 <dd>This CMake (cache) variable is a standard mechanism provided by
150 `find_package`, and can be used to specify a hint where to search for \Gromacs.
151 Also `CMAKE_PREFIX_PATH` can be used for this purpose; see CMake documentation
152 for `find_package` for more details.
153 `GROMACS_DIR` can also be set as an environment variable, and this is done by
154 `GMXRC`.</dd>
155 </dl>
156
157 Output variables that specify how the found `libgromacs` and header should be
158 used:
159
160 <dl>
161 <dt>`GROMACS_INCLUDE_DIRS`</dt>
162 <dd>List of include directories necessary to compile against the \Gromacs
163 headers.  Currently, this includes the path to \Gromacs headers, as well as the
164 path to Boost headers that were used to compile \Gromacs.</dd>
165 <dt>`GROMACS_LIBRARIES`</dt>
166 <dd>List of libraries to link with to link against \Gromacs.
167 Under the hood, this uses imported CMake targets to represent `libgromacs`.</dd>
168 <dt>`GROMACS_DEFINITIONS`</dt>
169 <dd>List of compile definitions (with `-D` in front) that are required to
170 compile the \Gromacs headers.</dd>
171 <dt>`GROMACS_IS_DOUBLE`</dt>
172 <dd>Whether the found \Gromacs was compiled in double precision.</dd>
173 </dl>
174
175 Declared macros/functions that can be used for checking for correctness of some
176 settings:
177
178 <dl>
179 <dt>`gromacs_check_double(GMX_DOUBLE)`</dt>
180 <dd>Checks that the found \Gromacs is in the expected precision.
181 The parameter `GMX_DOUBLE` should be the name of a cache variable that
182 specified whether double-precision was requested.</dd>
183 <dt>`gromacs_check_compiler(LANG)`<dt>
184 <dd>Checks that the found \Gromacs was compiled with the same compiler
185 that is used by the current CMake system.
186 Currently only `LANG=CXX` is supported.</dd>
187 </dl>
188
189 Notes on \Gromacs API
190 =====================
191
192 The headers for the public \Gromacs API are installed in `include/gromacs/`
193 under the installation directory.  The layout reflects the source code layout
194 under the `src/gromacs/` directory (see \linktodevmanual{codelayout,Source
195 code layout}).  The headers
196 directly under `include/gromacs/` do not contain any declarations, but instead
197 include a collection of headers from subdirectories.
198 You should prefer to include these convenience headers instead of individual
199 headers from the subdirectories, since they are much more stable.  The
200 individual headers in the subdirectories can be renamed or moved, but the goal
201 is to only rarely change the name of these top-level headers.
202
203 Pre-5.0 versions of \Gromacs installed (nearly) all headers directly under
204 `include/gromacs/`.  Most of these headers still exist, but now under
205 `include/gromacs/legacyheaders/`.  The long-term goal is to move these to
206 proper module hierarchy or get rid of them, but unfortunately this can take a
207 long time.  Thus, you should not expect much stability from the API in these
208 headers.  Some have already been moved, so if you do not find your favorite
209 header there, try searching for a declaration from the other subdirectories.
210
211 For headers under other subdirectories, some effort has been put to design the
212 API for stability.  However, with limited development resources, and the focus
213 of \Gromacs being in high performance simulations, all the APIs are subject to
214 change without notice.  With each new release (with possible exception of patch
215 releases), you should expect incompatible API changes.  This is in particular
216 true until the planned reorganization of the `legacyheaders/` subdirectory is
217 complete.
218
219 The header version.h (installed as `gromacs/version.h`) provides defines that
220 calling code can use to check the exact (released) version of \Gromacs that
221 installed the headers.
222
223 This Doxygen documentation only covers part of the API.  In particular, nearly
224 all of `include/gromacs/legacyheaders/` is undocumented, as well as code
225 recently moved from there.