Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / trajectoryanalysis / analysismodule.cpp
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief
33  * Implements classes in analysismodule.h.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \ingroup module_trajectoryanalysis
37  */
38 #include "gromacs/trajectoryanalysis/analysismodule.h"
39
40 #include "gromacs/analysisdata/analysisdata.h"
41 #include "gromacs/fatalerror/gmxassert.h"
42
43 #include "analysismodule-impl.h"
44
45 namespace gmx
46 {
47
48 /********************************************************************
49  * TrajectoryAnalysisModuleData::Impl
50  */
51
52 TrajectoryAnalysisModuleData::Impl::Impl(
53         TrajectoryAnalysisModule *module,
54         const AnalysisDataParallelOptions &opt,
55         const SelectionCollection &selections)
56     : _selections(selections)
57 {
58     TrajectoryAnalysisModule::Impl::AnalysisDatasetContainer::const_iterator i;
59     for (i = module->_impl->_analysisDatasets.begin();
60          i != module->_impl->_analysisDatasets.end(); ++i)
61     {
62         _handles[i->first] = i->second->startData(opt);
63     }
64 }
65
66 TrajectoryAnalysisModuleData::Impl::~Impl()
67 {
68 }
69
70
71 void TrajectoryAnalysisModuleData::Impl::finishHandles()
72 {
73     // FIXME: Call finishData() for all handles even if one throws
74     HandleContainer::const_iterator i;
75     for (i = _handles.begin(); i != _handles.end(); ++i)
76     {
77         i->second->finishData();
78     }
79     _handles.clear();
80 }
81
82
83 /********************************************************************
84  * TrajectoryAnalysisModuleData
85  */
86
87 TrajectoryAnalysisModuleData::TrajectoryAnalysisModuleData(
88         TrajectoryAnalysisModule *module,
89         const AnalysisDataParallelOptions &opt,
90         const SelectionCollection &selections)
91     : _impl(new Impl(module, opt, selections))
92 {
93 }
94
95
96 TrajectoryAnalysisModuleData::~TrajectoryAnalysisModuleData()
97 {
98 }
99
100
101 void TrajectoryAnalysisModuleData::finishDataHandles()
102 {
103     _impl->finishHandles();
104 }
105
106
107 AnalysisDataHandle *TrajectoryAnalysisModuleData::dataHandle(const char *name)
108 {
109     Impl::HandleContainer::const_iterator i = _impl->_handles.find(name);
110     GMX_RELEASE_ASSERT(i != _impl->_handles.end(),
111                        "Data handle requested on unknown dataset");
112     return (i != _impl->_handles.end()) ? (*i).second : NULL;
113 }
114
115
116 Selection *TrajectoryAnalysisModuleData::parallelSelection(Selection *selection)
117 {
118     // TODO: Implement properly.
119     return selection;
120 }
121
122
123 std::vector<Selection *>
124 TrajectoryAnalysisModuleData::parallelSelections(const std::vector<Selection *> &selections)
125 {
126     std::vector<Selection *> newSelections;
127     newSelections.reserve(selections.size());
128     std::vector<Selection *>::const_iterator i = selections.begin();
129     for ( ; i != selections.end(); ++i)
130     {
131         newSelections.push_back(parallelSelection(*i));
132     }
133     return newSelections;
134 }
135
136
137 /********************************************************************
138  * TrajectoryAnalysisModuleDataBasic
139  */
140 TrajectoryAnalysisModuleDataBasic::TrajectoryAnalysisModuleDataBasic(
141         TrajectoryAnalysisModule *module,
142         const AnalysisDataParallelOptions &opt,
143         const SelectionCollection &selections)
144     : TrajectoryAnalysisModuleData(module, opt, selections)
145 {
146 }
147
148
149 void
150 TrajectoryAnalysisModuleDataBasic::finish()
151 {
152     finishDataHandles();
153 }
154
155
156 /********************************************************************
157  * TrajectoryAnalysisModule
158  */
159
160 TrajectoryAnalysisModule::TrajectoryAnalysisModule()
161     : _impl(new Impl)
162 {
163 }
164
165
166 TrajectoryAnalysisModule::~TrajectoryAnalysisModule()
167 {
168 }
169
170
171 void TrajectoryAnalysisModule::initOptionsDone(TrajectoryAnalysisSettings * /*settings*/)
172 {
173 }
174
175
176 void TrajectoryAnalysisModule::initAfterFirstFrame(const t_trxframe &/*fr*/)
177 {
178 }
179
180
181 TrajectoryAnalysisModuleData *
182 TrajectoryAnalysisModule::startFrames(const AnalysisDataParallelOptions &opt,
183                                       const SelectionCollection &selections)
184 {
185     return new TrajectoryAnalysisModuleDataBasic(this, opt, selections);
186 }
187
188
189 void TrajectoryAnalysisModule::finishFrames(TrajectoryAnalysisModuleData * /*pdata*/)
190 {
191 }
192
193
194 int TrajectoryAnalysisModule::datasetCount() const
195 {
196     return _impl->_datasetNames.size();
197 }
198
199
200 const std::vector<std::string> &TrajectoryAnalysisModule::datasetNames() const
201 {
202     return _impl->_datasetNames;
203 }
204
205
206 AbstractAnalysisData *TrajectoryAnalysisModule::datasetFromIndex(int index) const
207 {
208     if (index < 0 || index >= datasetCount())
209     {
210         return NULL;
211     }
212     Impl::DatasetContainer::const_iterator item
213         = _impl->_datasets.find(_impl->_datasetNames[index]);
214     GMX_RELEASE_ASSERT(item != _impl->_datasets.end(),
215                        "Inconsistent data set names");
216     return item->second;
217 }
218
219
220 AbstractAnalysisData *TrajectoryAnalysisModule::datasetFromName(const char *name) const
221 {
222     Impl::DatasetContainer::const_iterator item = _impl->_datasets.find(name);
223     if (item == _impl->_datasets.end())
224     {
225         return NULL;
226     }
227     return item->second;
228 }
229
230
231 void TrajectoryAnalysisModule::registerBasicDataset(AbstractAnalysisData *data,
232                                                     const char *name)
233 {
234     GMX_RELEASE_ASSERT(datasetFromName(name) == NULL,
235                        "Duplicate data set name registered");
236     _impl->_datasets[name] = data;
237     _impl->_datasetNames.push_back(name);
238 }
239
240
241 void TrajectoryAnalysisModule::registerAnalysisDataset(AnalysisData *data,
242                                                        const char *name)
243 {
244     registerBasicDataset(data, name);
245     _impl->_analysisDatasets[name] = data;
246 }
247
248 } // namespace gmx