2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * directory and at http://www.gromacs.org.
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.
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.
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.
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.
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.
37 * Implements classes in analysismodule.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_trajectoryanalysis
42 #include "gromacs/trajectoryanalysis/analysismodule.h"
46 #include "gromacs/analysisdata/analysisdata.h"
47 #include "gromacs/selection/selection.h"
48 #include "gromacs/utility/exceptions.h"
49 #include "gromacs/utility/gmxassert.h"
54 /********************************************************************
55 * TrajectoryAnalysisModule::Impl
59 * Private implementation class for TrajectoryAnalysisModule.
61 * \ingroup module_trajectoryanalysis
63 class TrajectoryAnalysisModule::Impl
66 //! Container that associates a data set with its name.
67 typedef std::map<std::string, AbstractAnalysisData *> DatasetContainer;
68 //! Container that associates a AnalysisData object with its name.
69 typedef std::map<std::string, AnalysisData *> AnalysisDatasetContainer;
71 //! Initializes analysis module data with given name and description.
72 Impl(const char *name, const char *description)
73 : name_(name), description_(description)
77 //! Name of the module.
79 //! Description of the module.
80 std::string description_;
81 //! List of registered data set names.
82 std::vector<std::string> datasetNames_;
84 * Keeps all registered data sets.
86 * This container also includes datasets from \a analysisDatasets_.
88 DatasetContainer datasets_;
89 //! Keeps registered AnalysisData objects.
90 AnalysisDatasetContainer analysisDatasets_;
93 /********************************************************************
94 * TrajectoryAnalysisModuleData::Impl
98 * Private implementation class for TrajectoryAnalysisModuleData.
100 * \ingroup module_trajectoryanalysis
102 class TrajectoryAnalysisModuleData::Impl
105 //! Container that associates a data handle to its AnalysisData object.
106 typedef std::map<const AnalysisData *, AnalysisDataHandle>
109 //! \copydoc TrajectoryAnalysisModuleData::TrajectoryAnalysisModuleData()
110 Impl(TrajectoryAnalysisModule *module,
111 const AnalysisDataParallelOptions &opt,
112 const SelectionCollection &selections);
114 //! Keeps a data handle for each AnalysisData object.
115 HandleContainer handles_;
116 //! Stores thread-local selections.
117 const SelectionCollection &selections_;
120 TrajectoryAnalysisModuleData::Impl::Impl(
121 TrajectoryAnalysisModule *module,
122 const AnalysisDataParallelOptions &opt,
123 const SelectionCollection &selections)
124 : selections_(selections)
126 TrajectoryAnalysisModule::Impl::AnalysisDatasetContainer::const_iterator i;
127 for (i = module->impl_->analysisDatasets_.begin();
128 i != module->impl_->analysisDatasets_.end(); ++i)
130 handles_.insert(std::make_pair(i->second, i->second->startData(opt)));
135 /********************************************************************
136 * TrajectoryAnalysisModuleData
139 TrajectoryAnalysisModuleData::TrajectoryAnalysisModuleData(
140 TrajectoryAnalysisModule *module,
141 const AnalysisDataParallelOptions &opt,
142 const SelectionCollection &selections)
143 : impl_(new Impl(module, opt, selections))
148 TrajectoryAnalysisModuleData::~TrajectoryAnalysisModuleData()
153 void TrajectoryAnalysisModuleData::finishDataHandles()
155 // FIXME: Call finishData() for all handles even if one throws
156 Impl::HandleContainer::iterator i;
157 for (i = impl_->handles_.begin(); i != impl_->handles_.end(); ++i)
159 i->second.finishData();
161 impl_->handles_.clear();
166 TrajectoryAnalysisModuleData::dataHandle(const AnalysisData &data)
168 Impl::HandleContainer::const_iterator i = impl_->handles_.find(&data);
169 GMX_RELEASE_ASSERT(i != impl_->handles_.end(),
170 "Data handle requested on unknown dataset");
175 Selection TrajectoryAnalysisModuleData::parallelSelection(const Selection &selection)
177 // TODO: Implement properly.
183 TrajectoryAnalysisModuleData::parallelSelections(const SelectionList &selections)
185 // TODO: Consider an implementation that does not allocate memory every time.
186 SelectionList newSelections;
187 newSelections.reserve(selections.size());
188 SelectionList::const_iterator i = selections.begin();
189 for (; i != selections.end(); ++i)
191 newSelections.push_back(parallelSelection(*i));
193 return newSelections;
197 /********************************************************************
198 * TrajectoryAnalysisModuleDataBasic
205 * Basic thread-local trajectory analysis data storage class.
207 * Most simple tools should only require data handles and selections to be
208 * thread-local, so this class implements just that.
210 * \ingroup module_trajectoryanalysis
212 class TrajectoryAnalysisModuleDataBasic : public TrajectoryAnalysisModuleData
216 * Initializes thread-local storage for data handles and selections.
218 * \param[in] module Analysis module to use for data objects.
219 * \param[in] opt Data parallelization options.
220 * \param[in] selections Thread-local selection collection.
222 TrajectoryAnalysisModuleDataBasic(TrajectoryAnalysisModule *module,
223 const AnalysisDataParallelOptions &opt,
224 const SelectionCollection &selections);
226 virtual void finish();
229 TrajectoryAnalysisModuleDataBasic::TrajectoryAnalysisModuleDataBasic(
230 TrajectoryAnalysisModule *module,
231 const AnalysisDataParallelOptions &opt,
232 const SelectionCollection &selections)
233 : TrajectoryAnalysisModuleData(module, opt, selections)
239 TrajectoryAnalysisModuleDataBasic::finish()
247 /********************************************************************
248 * TrajectoryAnalysisModule
251 TrajectoryAnalysisModule::TrajectoryAnalysisModule(const char *name,
252 const char *description)
253 : impl_(new Impl(name, description))
258 TrajectoryAnalysisModule::~TrajectoryAnalysisModule()
263 void TrajectoryAnalysisModule::optionsFinished(
264 Options * /*options*/,
265 TrajectoryAnalysisSettings * /*settings*/)
270 void TrajectoryAnalysisModule::initAfterFirstFrame(const t_trxframe & /*fr*/)
275 TrajectoryAnalysisModuleDataPointer
276 TrajectoryAnalysisModule::startFrames(const AnalysisDataParallelOptions &opt,
277 const SelectionCollection &selections)
279 return TrajectoryAnalysisModuleDataPointer(
280 new TrajectoryAnalysisModuleDataBasic(this, opt, selections));
284 void TrajectoryAnalysisModule::finishFrames(TrajectoryAnalysisModuleData * /*pdata*/)
289 const char *TrajectoryAnalysisModule::name() const
291 return impl_->name_.c_str();
295 const char *TrajectoryAnalysisModule::description() const
297 return impl_->description_.c_str();
301 int TrajectoryAnalysisModule::datasetCount() const
303 return impl_->datasetNames_.size();
307 const std::vector<std::string> &TrajectoryAnalysisModule::datasetNames() const
309 return impl_->datasetNames_;
313 AbstractAnalysisData &TrajectoryAnalysisModule::datasetFromIndex(int index) const
315 if (index < 0 || index >= datasetCount())
317 GMX_THROW(APIError("Out of range data set index"));
319 Impl::DatasetContainer::const_iterator item
320 = impl_->datasets_.find(impl_->datasetNames_[index]);
321 GMX_RELEASE_ASSERT(item != impl_->datasets_.end(),
322 "Inconsistent data set names");
323 return *item->second;
327 AbstractAnalysisData &TrajectoryAnalysisModule::datasetFromName(const char *name) const
329 Impl::DatasetContainer::const_iterator item = impl_->datasets_.find(name);
330 if (item == impl_->datasets_.end())
332 GMX_THROW(APIError("Unknown data set name"));
334 return *item->second;
338 void TrajectoryAnalysisModule::registerBasicDataset(AbstractAnalysisData *data,
341 GMX_RELEASE_ASSERT(data != NULL, "Attempting to register NULL data");
342 // TODO: Strong exception safety should be possible to implement.
343 GMX_RELEASE_ASSERT(impl_->datasets_.find(name) == impl_->datasets_.end(),
344 "Duplicate data set name registered");
345 impl_->datasets_[name] = data;
346 impl_->datasetNames_.push_back(name);
350 void TrajectoryAnalysisModule::registerAnalysisDataset(AnalysisData *data,
353 // TODO: Strong exception safety should be possible to implement.
354 registerBasicDataset(data, name);
355 impl_->analysisDatasets_[name] = data;