3a33b0d2cbf5bb6d0c98ee7d89f7d29607b28067
[alexxy/gromacs.git] / src / gromacs / trajectoryanalysis.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2011,2012,2013,2014, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \defgroup module_trajectoryanalysis Framework for Trajectory Analysis (trajectoryanalysis)
36  * \ingroup group_analysismodules
37  * \brief
38  * Provides functionality for implementing trajectory analysis modules.
39  *
40  * This module implements a framework for implementing flexible trajectory
41  * analysis routines.  It provides a base class for implementing analysis as
42  * reusable modules that can be used from different contexts and can also
43  * support per-frame parallelization.  It integrally uses functionality from the
44  * following modules:
45  *  - \ref module_options
46  *  - \ref module_analysisdata
47  *  - \ref module_selection
48  *
49  * The main interface of this module is the gmx::TrajectoryAnalysisModule class.
50  * Analysis modules should derive from this class, and override the necessary
51  * virtual methods to provide the actual initialization and analysis routines.
52  * Classes gmx::TrajectoryAnalysisSettings and gmx::TopologyInformation (in
53  * addition to classes declared in the above-mentioned modules) are used to pass
54  * information to and from these methods.  gmx::TrajectoryAnalysisModuleData can
55  * be used in advanced scenarios where the tool requires custom thread-local
56  * data for parallel analysis.
57  *
58  * The sequence charts below provides an overview of how the trajectory
59  * analysis modules typically interact with other components.
60  * The first chart provides an overview of the call sequence of the most
61  * important methods in gmx::TrajectoryAnalysisModule.
62  * There is a runner, which is responsible for doing the work that is shared
63  * between all trajectory analysis (such as reading the trajectory and
64  * processing selections).  The runner then calls different methods in the
65  * analysis module at appropriate points to perform the module-specific tasks.
66  * The analysis module is responsible for creating and managing
67  * gmx::AnalysisData objects, and the chart shows the most important
68  * interactions with this module as well.  Interactions with options (for
69  * command-line option processing) and selections is not shown for brevity: see
70  * \ref module_options for an overview of how options work, and the second
71  * chart for a more detailed view of how selections are accessed from an
72  * analysis module.
73  * \msc
74  *     runner,
75  *     module [ URL="\ref gmx::TrajectoryAnalysisModule" ],
76  *     data [ label="analysis data", URL="\ref module_analysisdata" ];
77  *
78  *     runner box module [ label="caller owns runner and module objects" ];
79  *     module => data [ label="create (in constructor)" ];
80  *     runner => module [ label="initOptions()",
81  *                        URL="\ref gmx::TrajectoryAnalysisModule::initOptions()" ];
82  *     runner => runner [ label="parse user input" ];
83  *     runner => module [ label="optionsFinished()",
84  *                        URL="\ref gmx::TrajectoryAnalysisModule::optionsFinished()" ];
85  *     runner => runner [ label="initialize topology\nand selections" ];
86  *     runner => module [ label="initAnalysis()",
87  *                        URL="\ref gmx::TrajectoryAnalysisModule::initAnalysis()" ];
88  *     module => data [ label="initialize" ];
89  *     runner => runner [ label="read frame 0" ];
90  *     runner => module [ label="initAfterFirstFrame()",
91  *                        URL="\ref gmx::TrajectoryAnalysisModule::initAfterFirstFrame()" ];
92  *     --- [ label="loop over frames starts" ];
93  *     runner => runner [ label="initialize frame 0" ];
94  *     runner => module [ label="analyzeFrame(0)",
95  *                        URL="\ref gmx::TrajectoryAnalysisModule::analyzeFrame()" ];
96  *     module => data [ label="add data",
97  *                      URL="\ref gmx::AnalysisDataHandle" ];
98  *     module => data [ label="finishFrame()",
99  *                      URL="\ref gmx::AnalysisDataHandle::finishFrame()" ];
100  *     data => data [ label="process frame 0" ];
101  *     runner => runner [ label="read and initialize frame 1" ];
102  *     runner => module [ label="analyzeFrame(1)",
103  *                         URL="\ref gmx::TrajectoryAnalysisModule::analyzeFrame()" ];
104  *     ...;
105  *     --- [ label="loop over frames ends" ];
106  *     runner => module [ label="finishAnalysis()",
107  *                        URL="\ref gmx::TrajectoryAnalysisModule::finishAnalysis()" ];
108  *     module => data [ label="post-process data" ];
109  *     runner => module [ label="writeOutput()",
110  *                        URL="\ref gmx::TrajectoryAnalysisModule::writeOutput()" ];
111  * \endmsc
112  *
113  * The second chart below shows the interaction with selections and options
114  * with focus on selection options.  The gmx::TrajectoryAnalysisModule object
115  * creates one or more gmx::Selection variables, and uses gmx::SelectionOption
116  * to indicate them as the destination for selections.  This happens in
117  * gmx::TrajectoryAnalysisModule::initOptions().  After the options have been
118  * parsed (includes parsing any options present on the command-line or read
119  * from files, but not those provided interactively),
120  * gmx::TrajectoryAnalysisModule::optionsFinished() can adjust the selections
121  * using gmx::SelectionOptionInfo.  This is done like this to allow the
122  * analysis module to influence the interactive prompt of selections based on
123  * what command-line options were given.  After optionsFinished() returns, the
124  * interactive selection prompt is presented if necessary.  After this point,
125  * all access to selections from the analysis module is through the
126  * gmx::Selection variables: the runner is responsible for calling methods in
127  * the selection library, and these methods update the content referenced by
128  * the gmx::Selection variables.  See documentation of
129  * gmx::TrajectoryAnalysisModule for details of what the selections contain at
130  * each point.
131  * \msc
132  *     runner,
133  *     options [ label="Options", URL="\ref module_options" ],
134  *     selection [ label="selections", URL="\ref module_selection" ],
135  *     module [ label="module", URL="\ref gmx::TrajectoryAnalysisModule" ];
136  *
137  *     runner box selection [ label="all these objects are owned by the framework" ];
138  *     runner => module [ label="initOptions()",
139  *                        URL="\ref gmx::TrajectoryAnalysisModule::initOptions()" ];
140  *     module => options [ label="addOption(SelectionOption)",
141  *                         URL="\ref gmx::SelectionOption" ];
142  *     module => options [ label="addOption() (other options)",
143  *                         URL="\ref gmx::Options::addOption()" ];
144  *     ...;
145  *     runner << module;
146  *     runner => options [ label="parse command-line parameters" ];
147  *     options => selection [ label="parse selections" ];
148  *     selection -> module [ label="initialize Selection variables",
149  *                           URL="\ref gmx::Selection" ];
150  *     runner << options;
151  *     runner => module [ label="optionsFinished()",
152  *                        URL="\ref gmx::TrajectoryAnalysisModule::optionsFinished()" ];
153  *     module => selection [ label="adjust SelectionOptions",
154  *                           URL="\ref gmx::SelectionOptionInfo" ];
155  *     runner << module;
156  *     runner => selection [ label="prompt missing selections" ];
157  *     selection -> module [ label="initialize Selection variables",
158  *                         URL="\ref gmx::Selection" ];
159  *     runner => selection [ label="compile selections" ];
160  *     selection -> module [ label="change content referenced\nby Selection variables" ];
161  *     runner => module [ label="initAnalysis()",
162  *                        URL="\ref gmx::TrajectoryAnalysisModule::initAnalysis()" ];
163  *     ...;
164  *     --- [ label="loop over frames starts" ];
165  *     runner => runner [ label="read and initialize frame 0" ];
166  *     runner => selection [ label="evaluate selections for frame 0" ];
167  *     selection -> module [ label="change content referenced\nby Selection variables" ];
168  *     ...;
169  * \endmsc
170  *
171  * The final chart shows the flow within the frame loop in the case of parallel
172  * (threaded) execution and the interaction with the \ref module_analysisdata
173  * module in this case.  Although parallelization has not yet been implemented,
174  * it has influenced the design and needs to be understood if one wants to
175  * write modules that can take advantage of the parallelization once it gets
176  * implemented.  The parallelization takes part over frames: analyzing a single
177  * frame is one unit of work.  When the frame loop is started,
178  * gmx::TrajectoryAnalysisModule::startFrames() is called for each thread, and
179  * initializes an object that contains thread-local data needed during the
180  * analysis.  This includes selection information, gmx::AnalysisDataHandle
181  * objects, and possibly other module-specific variables.  Then, the runner
182  * reads the frames in sequence and passes the work into the different threads,
183  * together with the appropriate thread-local data object.
184  * The gmx::TrajectoryAnalysisModule::analyzeFrame() calls are only allowed to modify
185  * the thread-local data object; everything else is read-only.  For any output,
186  * they pass the information to gmx::AnalysisData, which takes care of ordering
187  * the data from different frames such that it gets processed in the right
188  * order.
189  * When all frames are analyzed, gmx::TrajectoryAnalysisModule::finishFrames()
190  * is called for each thread-local data object to destroy them and to
191  * accumulate possible results from them into the main
192  * gmx::TrajectoryAnalysisModule object.
193  * Note that in the diagram, some part of the work attributed for the runner
194  * (e.g., evaluating selections) will actually be carried out in the analysis
195  * threads before gmx::TrajectoryAnalysisModule::analyzeFrame() gets called.
196  * \msc
197  *     runner,
198  *     module [ label="module object" ],
199  *     thread1 [ label="analysis\nthread 1" ],
200  *     thread2 [ label="analysis\nthread 2" ],
201  *     data [ label="analysis data", URL="\ref module_analysisdata" ];
202  *
203  *     module box thread2 [ label="single TrajectoryAnalysisModule object",
204  *                          URL="\ref gmx::TrajectoryAnalysisModule" ];
205  *     ...;
206  *     --- [ label="loop over frames starts" ];
207  *     runner => thread1 [ label="startFrames()",
208  *                         URL="\ref gmx::TrajectoryAnalysisModule::startFrames()" ];
209  *     thread1 => data [ label="startData()",
210  *                       URL="\ref gmx::AnalysisData::startData()" ];
211  *     runner << thread1 [ label="pdata1" ];
212  *     runner => thread2 [ label="startFrames()",
213  *                         URL="\ref gmx::TrajectoryAnalysisModule::startFrames()" ];
214  *     thread2 => data [ label="startData()",
215  *                       URL="\ref gmx::AnalysisData::startData()" ];
216  *     runner << thread2 [ label="pdata2" ];
217  *     |||;
218  *     runner => runner [ label="initialize frame 0" ];
219  *     runner => thread1 [ label="analyzeFrame(0, pdata1)",
220  *                         URL="\ref gmx::TrajectoryAnalysisModule::analyzeFrame()" ];
221  *     runner => runner [ label="read and initialize frame 1" ];
222  *     runner => thread2 [ label="analyzeFrame(1, pdata2)",
223  *                         URL="\ref gmx::TrajectoryAnalysisModule::analyzeFrame()" ];
224  *     thread1 => data [ label="add data",
225  *                       URL="\ref gmx::AnalysisDataHandle" ];
226  *     thread2 => data [ label="add data",
227  *                       URL="\ref gmx::AnalysisDataHandle" ];
228  *     thread2 => data [ label="finishFrame(1)",
229  *                       URL="\ref gmx::AnalysisDataHandle::finishFrame()" ];
230  *     runner << thread2 [ label="analyzeFrame() (frame 1)" ];
231  *     runner => runner [ label="read and initialize frame 2" ];
232  *     runner => thread2 [ label="analyzeFrame(2)",
233  *                         URL="\ref gmx::TrajectoryAnalysisModule::analyzeFrame()" ];
234  *     thread1 => data [ label="finishFrame(0)",
235  *                       URL="\ref gmx::AnalysisDataHandle::finishFrame()" ];
236  *     data => data [ label="process frame 0" ];
237  *     data => data [ label="process frame 1" ];
238  *     runner << thread1 [ label="analyzeFrame() (frame 0)" ];
239  *     ...;
240  *     runner => thread1 [ label="finishFrames(pdata1)",
241  *                         URL="\ref gmx::TrajectoryAnalysisModule::finishFrames()" ];
242  *     thread1 => data [ label="finishData()",
243  *                       URL="\ref gmx::AnalysisData::finishData()" ];
244  *     thread1 -> module [ label="accumulate results" ];
245  *     runner << thread1;
246  *     runner => thread2 [ label="finishFrames(pdata2)",
247  *                         URL="\ref gmx::TrajectoryAnalysisModule::finishFrames()" ];
248  *     thread2 => data [ label="finishData()",
249  *                       URL="\ref gmx::AnalysisData::finishData()" ];
250  *     thread2 -> module [ label="accumulate results" ];
251  *     runner << thread2;
252  *     --- [ label="loop over frames ends" ];
253  *     ...;
254  * \endmsc
255  *
256  * In addition to the framework for defining analysis modules, this module also
257  * provides gmx::TrajectoryAnalysisCommandLineRunner, which implements a
258  * command-line program that runs a certain analysis module.
259  *
260  * Internally, the module also defines a set of trajectory analysis modules that
261  * can currently be accessed only through gmx::registerTrajectoryAnalysisModules.
262  *
263  * For an example of how to implement an analysis tool using the framework, see
264  * \ref template.cpp.
265  *
266  * \author Teemu Murtola <teemu.murtola@gmail.com>
267  */
268 /*! \file
269  * \brief
270  * Public API convenience header for trajectory analysis framework
271  *
272  * \author Teemu Murtola <teemu.murtola@gmail.com>
273  * \inpublicapi
274  * \ingroup module_trajectoryanalysis
275  */
276 #ifndef GMX_TRAJECTORYANALYSIS_H
277 #define GMX_TRAJECTORYANALYSIS_H
278
279 #include "analysisdata.h"
280 #include "options.h"
281 #include "selection.h"
282
283 #include "fileio/trx.h"
284 #include "selection/nbsearch.h"
285 #include "topology/topology.h"
286 #include "trajectoryanalysis/analysismodule.h"
287 #include "trajectoryanalysis/analysissettings.h"
288 #include "trajectoryanalysis/cmdlinerunner.h"
289 #include "utility/arrayref.h"
290 #include "utility/exceptions.h"
291
292 #endif