bdc43b4536c40b80cfe1e276d4b20e191ee34144
[alexxy/gromacs.git] / src / gromacs / analysisdata / abstractdata.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,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 /*! \internal \file
36  * \brief
37  * Implements gmx::AbstractAnalysisData.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_analysisdata
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/analysisdata/abstractdata.h"
45
46 #include <vector>
47
48 #include "gromacs/analysisdata/dataframe.h"
49 #include "gromacs/analysisdata/datamodule.h"
50 #include "gromacs/analysisdata/datamodulemanager.h"
51 #include "gromacs/utility/exceptions.h"
52 #include "gromacs/utility/gmxassert.h"
53 #include "gromacs/utility/uniqueptr.h"
54
55 #include "dataproxy.h"
56
57 namespace gmx
58 {
59
60 /********************************************************************
61  * AbstractAnalysisData::Impl
62  */
63
64 /*! \internal \brief
65  * Private implementation class for AbstractAnalysisData.
66  *
67  * \ingroup module_analysisdata
68  */
69 class AbstractAnalysisData::Impl
70 {
71     public:
72         Impl();
73
74         //! Column counts for each data set in the data.
75         std::vector<int>           columnCounts_;
76         //! Whether the data is multipoint.
77         bool                       bMultipoint_;
78         //! Manager for the added modules.
79         AnalysisDataModuleManager  modules_;
80 };
81
82 AbstractAnalysisData::Impl::Impl()
83     : bMultipoint_(false)
84 {
85     columnCounts_.push_back(0);
86 }
87
88
89 /********************************************************************
90  * AbstractAnalysisData
91  */
92 /*! \cond libapi */
93 AbstractAnalysisData::AbstractAnalysisData()
94     : impl_(new Impl())
95 {
96 }
97 //! \endcond
98
99 AbstractAnalysisData::~AbstractAnalysisData()
100 {
101 }
102
103 bool
104 AbstractAnalysisData::isMultipoint() const
105 {
106     return impl_->bMultipoint_;
107 }
108
109 int
110 AbstractAnalysisData::dataSetCount() const
111 {
112     return impl_->columnCounts_.size();
113 }
114
115 int
116 AbstractAnalysisData::columnCount(int dataSet) const
117 {
118     GMX_ASSERT(dataSet >= 0 && dataSet < dataSetCount(),
119                "Out of range data set index");
120     return impl_->columnCounts_[dataSet];
121 }
122
123 int
124 AbstractAnalysisData::columnCount() const
125 {
126     GMX_ASSERT(dataSetCount() == 1,
127                "Convenience method not available for multiple data sets");
128     return columnCount(0);
129 }
130
131
132 AnalysisDataFrameRef
133 AbstractAnalysisData::tryGetDataFrame(int index) const
134 {
135     if (index < 0 || index >= frameCount())
136     {
137         return AnalysisDataFrameRef();
138     }
139     return tryGetDataFrameInternal(index);
140 }
141
142
143 AnalysisDataFrameRef
144 AbstractAnalysisData::getDataFrame(int index) const
145 {
146     AnalysisDataFrameRef frame = tryGetDataFrame(index);
147     if (!frame.isValid())
148     {
149         GMX_THROW(APIError("Invalid frame accessed"));
150     }
151     return frame;
152 }
153
154
155 bool
156 AbstractAnalysisData::requestStorage(int nframes)
157 {
158     GMX_RELEASE_ASSERT(nframes >= -1, "Invalid number of frames requested");
159     if (nframes == 0)
160     {
161         return true;
162     }
163     return requestStorageInternal(nframes);
164 }
165
166
167 void
168 AbstractAnalysisData::addModule(AnalysisDataModulePointer module)
169 {
170     impl_->modules_.addModule(this, module);
171 }
172
173
174 void
175 AbstractAnalysisData::addColumnModule(int col, int span,
176                                       AnalysisDataModulePointer module)
177 {
178     GMX_RELEASE_ASSERT(col >= 0 && span >= 1,
179                        "Invalid columns specified for a column module");
180     boost::shared_ptr<AnalysisDataProxy> proxy(
181             new AnalysisDataProxy(col, span, this));
182     proxy->addModule(module);
183     addModule(proxy);
184 }
185
186
187 void
188 AbstractAnalysisData::applyModule(AnalysisDataModuleInterface *module)
189 {
190     impl_->modules_.applyModule(this, module);
191 }
192
193 /*! \cond libapi */
194 void
195 AbstractAnalysisData::setDataSetCount(int dataSetCount)
196 {
197     GMX_RELEASE_ASSERT(dataSetCount > 0, "Invalid data column count");
198     impl_->modules_.dataPropertyAboutToChange(
199             AnalysisDataModuleManager::eMultipleDataSets, dataSetCount > 1);
200     impl_->columnCounts_.resize(dataSetCount);
201 }
202
203 void
204 AbstractAnalysisData::setColumnCount(int dataSet, int columnCount)
205 {
206     GMX_RELEASE_ASSERT(dataSet >= 0 && dataSet < dataSetCount(),
207                        "Out of range data set index");
208     GMX_RELEASE_ASSERT(columnCount > 0, "Invalid data column count");
209
210     bool bMultipleColumns = columnCount > 1;
211     for (int i = 0; i < dataSetCount() && !bMultipleColumns; ++i)
212     {
213         if (i != dataSet && this->columnCount(i) > 1)
214         {
215             bMultipleColumns = true;
216         }
217     }
218     impl_->modules_.dataPropertyAboutToChange(
219             AnalysisDataModuleManager::eMultipleColumns, bMultipleColumns);
220     impl_->columnCounts_[dataSet] = columnCount;
221 }
222
223 void
224 AbstractAnalysisData::setMultipoint(bool bMultipoint)
225 {
226     impl_->modules_.dataPropertyAboutToChange(
227             AnalysisDataModuleManager::eMultipoint, bMultipoint);
228     impl_->bMultipoint_ = bMultipoint;
229 }
230
231 AnalysisDataModuleManager &AbstractAnalysisData::moduleManager()
232 {
233     return impl_->modules_;
234 }
235
236 const AnalysisDataModuleManager &AbstractAnalysisData::moduleManager() const
237 {
238     return impl_->modules_;
239 }
240 //! \endcond
241
242 } // namespace gmx