Removed simple.h from nb_kernel_sse4_1_XX
[gromacs.git] / src / gromacs / trajectoryanalysis / analysismodule.cpp
blob06a2b84f872ccf64ca600eb4da66cea1b43568db
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,2014,2015, 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.
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.
35 /*! \internal \file
36 * \brief
37 * Implements classes in analysismodule.h.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_trajectoryanalysis
42 #include "gmxpre.h"
44 #include "analysismodule.h"
46 #include <utility>
48 #include "gromacs/analysisdata/analysisdata.h"
49 #include "gromacs/selection/selection.h"
50 #include "gromacs/utility/exceptions.h"
51 #include "gromacs/utility/gmxassert.h"
53 namespace gmx
56 /********************************************************************
57 * TrajectoryAnalysisModule::Impl
60 /*! \internal \brief
61 * Private implementation class for TrajectoryAnalysisModule.
63 * \ingroup module_trajectoryanalysis
65 class TrajectoryAnalysisModule::Impl
67 public:
68 //! Container that associates a data set with its name.
69 typedef std::map<std::string, AbstractAnalysisData *> DatasetContainer;
70 //! Container that associates a AnalysisData object with its name.
71 typedef std::map<std::string, AnalysisData *> AnalysisDatasetContainer;
73 //! Initializes analysis module data with given name and description.
74 Impl(const char *name, const char *description)
75 : name_(name), description_(description)
79 //! Name of the module.
80 std::string name_;
81 //! Description of the module.
82 std::string description_;
83 //! List of registered data set names.
84 std::vector<std::string> datasetNames_;
85 /*! \brief
86 * Keeps all registered data sets.
88 * This container also includes datasets from \a analysisDatasets_.
90 DatasetContainer datasets_;
91 //! Keeps registered AnalysisData objects.
92 AnalysisDatasetContainer analysisDatasets_;
95 /********************************************************************
96 * TrajectoryAnalysisModuleData::Impl
99 /*! \internal \brief
100 * Private implementation class for TrajectoryAnalysisModuleData.
102 * \ingroup module_trajectoryanalysis
104 class TrajectoryAnalysisModuleData::Impl
106 public:
107 //! Container that associates a data handle to its AnalysisData object.
108 typedef std::map<const AnalysisData *, AnalysisDataHandle>
109 HandleContainer;
111 //! \copydoc TrajectoryAnalysisModuleData::TrajectoryAnalysisModuleData()
112 Impl(TrajectoryAnalysisModule *module,
113 const AnalysisDataParallelOptions &opt,
114 const SelectionCollection &selections);
116 //! Checks whether the given AnalysisData has been initialized.
117 bool isInitialized(const AnalysisData &data) const;
119 //! Keeps a data handle for each AnalysisData object.
120 HandleContainer handles_;
121 //! Stores thread-local selections.
122 const SelectionCollection &selections_;
125 TrajectoryAnalysisModuleData::Impl::Impl(
126 TrajectoryAnalysisModule *module,
127 const AnalysisDataParallelOptions &opt,
128 const SelectionCollection &selections)
129 : selections_(selections)
131 TrajectoryAnalysisModule::Impl::AnalysisDatasetContainer::const_iterator i;
132 for (i = module->impl_->analysisDatasets_.begin();
133 i != module->impl_->analysisDatasets_.end(); ++i)
135 AnalysisDataHandle handle;
136 if (isInitialized(*i->second))
138 handle = i->second->startData(opt);
140 handles_.insert(std::make_pair(i->second, handle));
144 bool TrajectoryAnalysisModuleData::Impl::isInitialized(
145 const AnalysisData &data) const
147 for (int i = 0; i < data.dataSetCount(); ++i)
149 if (data.columnCount(i) > 0)
151 // If not all of the column counts are set, startData() in the
152 // constructor asserts, so that does not need to be checked here.
153 return true;
156 return false;
160 /********************************************************************
161 * TrajectoryAnalysisModuleData
164 TrajectoryAnalysisModuleData::TrajectoryAnalysisModuleData(
165 TrajectoryAnalysisModule *module,
166 const AnalysisDataParallelOptions &opt,
167 const SelectionCollection &selections)
168 : impl_(new Impl(module, opt, selections))
173 TrajectoryAnalysisModuleData::~TrajectoryAnalysisModuleData()
178 void TrajectoryAnalysisModuleData::finishDataHandles()
180 // FIXME: Call finishData() for all handles even if one throws
181 Impl::HandleContainer::iterator i;
182 for (i = impl_->handles_.begin(); i != impl_->handles_.end(); ++i)
184 if (i->second.isValid())
186 i->second.finishData();
189 impl_->handles_.clear();
193 AnalysisDataHandle
194 TrajectoryAnalysisModuleData::dataHandle(const AnalysisData &data)
196 Impl::HandleContainer::const_iterator i = impl_->handles_.find(&data);
197 GMX_RELEASE_ASSERT(i != impl_->handles_.end(),
198 "Data handle requested on unknown dataset");
199 return i->second;
203 Selection TrajectoryAnalysisModuleData::parallelSelection(const Selection &selection)
205 // TODO: Implement properly.
206 return selection;
210 SelectionList
211 TrajectoryAnalysisModuleData::parallelSelections(const SelectionList &selections)
213 // TODO: Consider an implementation that does not allocate memory every time.
214 SelectionList newSelections;
215 newSelections.reserve(selections.size());
216 SelectionList::const_iterator i = selections.begin();
217 for (; i != selections.end(); ++i)
219 newSelections.push_back(parallelSelection(*i));
221 return newSelections;
225 /********************************************************************
226 * TrajectoryAnalysisModuleDataBasic
229 namespace
232 /*! \brief
233 * Basic thread-local trajectory analysis data storage class.
235 * Most simple tools should only require data handles and selections to be
236 * thread-local, so this class implements just that.
238 * \ingroup module_trajectoryanalysis
240 class TrajectoryAnalysisModuleDataBasic : public TrajectoryAnalysisModuleData
242 public:
243 /*! \brief
244 * Initializes thread-local storage for data handles and selections.
246 * \param[in] module Analysis module to use for data objects.
247 * \param[in] opt Data parallelization options.
248 * \param[in] selections Thread-local selection collection.
250 TrajectoryAnalysisModuleDataBasic(TrajectoryAnalysisModule *module,
251 const AnalysisDataParallelOptions &opt,
252 const SelectionCollection &selections);
254 virtual void finish();
257 TrajectoryAnalysisModuleDataBasic::TrajectoryAnalysisModuleDataBasic(
258 TrajectoryAnalysisModule *module,
259 const AnalysisDataParallelOptions &opt,
260 const SelectionCollection &selections)
261 : TrajectoryAnalysisModuleData(module, opt, selections)
266 void
267 TrajectoryAnalysisModuleDataBasic::finish()
269 finishDataHandles();
272 } // namespace
275 /********************************************************************
276 * TrajectoryAnalysisModule
279 TrajectoryAnalysisModule::TrajectoryAnalysisModule(const char *name,
280 const char *description)
281 : impl_(new Impl(name, description))
286 TrajectoryAnalysisModule::~TrajectoryAnalysisModule()
291 void TrajectoryAnalysisModule::optionsFinished(
292 TrajectoryAnalysisSettings * /*settings*/)
297 void TrajectoryAnalysisModule::initAfterFirstFrame(
298 const TrajectoryAnalysisSettings & /*settings*/,
299 const t_trxframe & /*fr*/)
304 TrajectoryAnalysisModuleDataPointer
305 TrajectoryAnalysisModule::startFrames(const AnalysisDataParallelOptions &opt,
306 const SelectionCollection &selections)
308 return TrajectoryAnalysisModuleDataPointer(
309 new TrajectoryAnalysisModuleDataBasic(this, opt, selections));
313 void TrajectoryAnalysisModule::finishFrames(TrajectoryAnalysisModuleData * /*pdata*/)
318 const char *TrajectoryAnalysisModule::name() const
320 return impl_->name_.c_str();
324 const char *TrajectoryAnalysisModule::description() const
326 return impl_->description_.c_str();
330 int TrajectoryAnalysisModule::datasetCount() const
332 return impl_->datasetNames_.size();
336 const std::vector<std::string> &TrajectoryAnalysisModule::datasetNames() const
338 return impl_->datasetNames_;
342 AbstractAnalysisData &TrajectoryAnalysisModule::datasetFromIndex(int index) const
344 if (index < 0 || index >= datasetCount())
346 GMX_THROW(APIError("Out of range data set index"));
348 Impl::DatasetContainer::const_iterator item
349 = impl_->datasets_.find(impl_->datasetNames_[index]);
350 GMX_RELEASE_ASSERT(item != impl_->datasets_.end(),
351 "Inconsistent data set names");
352 return *item->second;
356 AbstractAnalysisData &TrajectoryAnalysisModule::datasetFromName(const char *name) const
358 Impl::DatasetContainer::const_iterator item = impl_->datasets_.find(name);
359 if (item == impl_->datasets_.end())
361 GMX_THROW(APIError("Unknown data set name"));
363 return *item->second;
367 void TrajectoryAnalysisModule::registerBasicDataset(AbstractAnalysisData *data,
368 const char *name)
370 GMX_RELEASE_ASSERT(data != NULL, "Attempting to register NULL data");
371 // TODO: Strong exception safety should be possible to implement.
372 GMX_RELEASE_ASSERT(impl_->datasets_.find(name) == impl_->datasets_.end(),
373 "Duplicate data set name registered");
374 impl_->datasets_[name] = data;
375 impl_->datasetNames_.push_back(name);
379 void TrajectoryAnalysisModule::registerAnalysisDataset(AnalysisData *data,
380 const char *name)
382 // TODO: Strong exception safety should be possible to implement.
383 registerBasicDataset(data, name);
384 impl_->analysisDatasets_[name] = data;
388 void TrajectoryAnalysisModule::finishFrameSerial(int frameIndex)
390 Impl::AnalysisDatasetContainer::const_iterator data;
391 for (data = impl_->analysisDatasets_.begin();
392 data != impl_->analysisDatasets_.end();
393 ++data)
395 data->second->finishFrameSerial(frameIndex);
399 } // namespace gmx