2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2017,2019,2020,2021, 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.
37 * Declares classes for accessing data frame information.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_analysisdata
43 #ifndef GMX_ANALYSISDATA_DATAFRAME_H
44 #define GMX_ANALYSISDATA_DATAFRAME_H
48 #include "gromacs/utility/arrayref.h"
49 #include "gromacs/utility/basedefinitions.h"
50 #include "gromacs/utility/flags.h"
51 #include "gromacs/utility/gmxassert.h"
52 #include "gromacs/utility/real.h"
58 * Value type for representing a single value in analysis data objects.
60 * Default copy constructor and assignment operator are used and work as
63 * Methods in this class do not throw.
65 * Non-const methods are provided for use within the library only; currently
66 * it is not possible to access a non-const AnalysisDataValue through the
70 * \ingroup module_analysisdata
72 class AnalysisDataValue
76 * Constructs an unset value.
78 AnalysisDataValue() : value_(0.0), error_(0.0) {}
80 * Constructs a value object with the given value.
82 * The constructed object is marked as set and present.
84 explicit AnalysisDataValue(real value) : value_(value), error_(0.0)
87 flags_.set(efPresent);
91 * Direct access to the value.
93 * Assigning a value to this does not mark the value as set; setValue()
94 * must be used for this.
96 real& value() { return value_; }
98 * Direct access to the error estimate.
100 * Assigning a value to this does not mark the error estimate as set;
101 * setValue() must be used for this.
103 real& error() { return error_; }
104 //! Returns the value for this value.
105 real value() const { return value_; }
106 //! Returns the error estimate for this value, or zero if not set.
107 real error() const { return error_; }
109 * Returns whether this value has been set.
111 * If this method returns false, the return value of value() and
112 * error() are undefined.
114 bool isSet() const { return flags_.test(efSet); }
116 * Returns whether the error estimate for this value has been set.
118 * If this method returns false, but isSet() returns true, error()
121 bool hasError() const { return flags_.test(efErrorSet); }
123 * Returns whether this value has been marked as present.
125 * If this method returns false, it is up to the source data to define
126 * whether isSet() may return true.
128 bool isPresent() const { return flags_.test(efPresent); }
130 //! Clears and unsets this value.
131 void clear() { *this = AnalysisDataValue(); }
133 void setValue(real value, bool bPresent = true)
137 flags_.set(efPresent, bPresent);
139 //! Sets this value and its error estimate.
140 void setValue(real value, real error, bool bPresent = true)
145 flags_.set(efErrorSet);
146 flags_.set(efPresent, bPresent);
148 //! Set only error estimate for this value.
149 void setError(real error)
152 flags_.set(efErrorSet);
156 //! Possible flags for \a flags_.
159 efSet = 1 << 0, //!< Value has been set.
160 efErrorSet = 1 << 1, //!< Error estimate has been set.
161 efPresent = 1 << 2 //!< Value is set as present.
164 //! Value for this value.
166 //! Error estimate for this value, zero if not set.
168 //! Status flags for thise value.
169 FlagsTemplate<Flag> flags_;
172 //! Shorthand for reference to an array of data values.
173 typedef ArrayRef<const AnalysisDataValue> AnalysisDataValuesRef;
177 * Value type for storing frame-level information for analysis data.
179 * Default copy constructor and assignment operator are used and work as
181 * Typically new objects of this type are only constructed internally by the
182 * library and in classes that are derived from AbstractAnalysisData.
184 * Methods in this class do not throw, but may contain asserts for incorrect
187 * Note that it is not possible to change the contents of an initialized
188 * object, except by assigning a new object to replace it completely.
191 * \ingroup module_analysisdata
193 class AnalysisDataFrameHeader
197 * Constructs an invalid frame header.
199 * Return values of other methods than isValid() are unspecified for
200 * the constructed object.
202 AnalysisDataFrameHeader();
204 * Constructs a frame header from given values.
206 * \param[in] index Index of the frame. Must be >= 0.
207 * \param[in] x x coordinate for the frame.
208 * \param[in] dx Error estimate for x.
210 AnalysisDataFrameHeader(int index, real x, real dx);
213 * Returns whether the frame header corresponds to a valid frame.
215 * If returns false, return values of other methods are not specified.
217 bool isValid() const { return index_ >= 0; }
219 * Returns zero-based index of the frame.
221 * The return value is >= 0 for valid frames.
222 * Should not be called for invalid frames.
226 GMX_ASSERT(isValid(), "Tried to access invalid frame header");
230 * Returns the x coordinate for the frame.
232 * Should not be called for invalid frames.
236 GMX_ASSERT(isValid(), "Tried to access invalid frame header");
240 * Returns error in the x coordinate for the frame (if applicable).
242 * All data do not provide error estimates.
243 * Typically returns zero in those cases.
245 * Should not be called for invalid frames.
249 GMX_ASSERT(isValid(), "Tried to access invalid frame header");
260 /*! \cond libinternal */
261 /*! \libinternal \brief
262 * Value type for internal indexing of point sets.
264 * This class contains the necessary data to split an array of
265 * AnalysisDataValue objects into point sets. It is always specified in the
266 * context of an array of AnalysisDataValues: the point set specified by this
267 * class contains valueCount() values, starting from the array index
269 * The value at location valueOffset() corresponds to column firstColumn().
270 * It is not necessary for code using the analysis data framework to know of
271 * this class, but it is declared in a public header to allow using it in other
274 * Default copy constructor and assignment operator are used and work as
276 * Typically new objects of this type are only constructed internally by the
277 * library and in classes that are derived from AbstractAnalysisData.
279 * Methods in this class do not throw, but may contain asserts for incorrect
282 * Note that it is not possible to change the contents of an initialized
283 * object, except by assigning a new object to replace it completely.
286 * \ingroup module_analysisdata
288 class AnalysisDataPointSetInfo
291 //! Construct point set data object with the given values.
292 AnalysisDataPointSetInfo(int valueOffset, int valueCount, int dataSetIndex, int firstColumn) :
293 valueOffset_(valueOffset), valueCount_(valueCount), dataSetIndex_(dataSetIndex), firstColumn_(firstColumn)
295 GMX_ASSERT(valueOffset >= 0, "Negative value offsets are invalid");
296 GMX_ASSERT(valueCount >= 0, "Negative value counts are invalid");
297 GMX_ASSERT(dataSetIndex >= 0, "Negative data set indices are invalid");
298 GMX_ASSERT(firstColumn >= 0, "Negative column indices are invalid");
301 //! Returns the offset of the first value in the referenced value array.
302 int valueOffset() const { return valueOffset_; }
303 //! Returns the number of values in this point set.
304 int valueCount() const { return valueCount_; }
305 //! Returns the data set index for this point set.
306 int dataSetIndex() const { return dataSetIndex_; }
307 //! Returns the index of the first column in this point set.
308 int firstColumn() const { return firstColumn_; }
317 //! Shorthand for reference to an array of point set data objects.
318 typedef ArrayRef<const AnalysisDataPointSetInfo> AnalysisDataPointSetInfosRef;
324 * Value type wrapper for non-mutable access to a set of data column values.
326 * Default copy constructor and assignment operator are used and work as
328 * Typically new objects of this type are only constructed internally by the
329 * library and in classes that are derived from AbstractAnalysisData.
331 * Methods in this class do not throw, but may contain asserts for incorrect
334 * The design of the interfaces is such that all objects of this type should be
335 * valid, i.e., header().isValid() should always return true.
337 * Note that it is not possible to change the contents of an initialized
338 * object, except by assigning a new object to replace it completely.
341 * \ingroup module_analysisdata
343 class AnalysisDataPointSetRef
347 * Constructs a point set reference from given values.
349 * \param[in] header Header for the frame.
350 * \param[in] pointSetInfo Information about the point set.
351 * \param[in] values Values for each column.
353 * The first element of the point set should be found from \p values
354 * using the offset in \p pointSetInfo.
356 AnalysisDataPointSetRef(const AnalysisDataFrameHeader& header,
357 const AnalysisDataPointSetInfo& pointSetInfo,
358 const AnalysisDataValuesRef& values);
360 * Constructs a point set reference from given values.
362 * \param[in] header Header for the frame.
363 * \param[in] values Values for each column.
365 * The first element in \p values should correspond to the first
368 AnalysisDataPointSetRef(const AnalysisDataFrameHeader& header,
369 const std::vector<AnalysisDataValue>& values);
371 * Constructs a point set reference to a subset of columns.
373 * \param[in] points Point set to use as source.
374 * \param[in] firstColumn First column index to include.
375 * \param[in] columnCount Number of columns to include.
377 * Creates a point set that contains \p columnCount columns starting
378 * from \p firstColumn from \p points, or a subset if all requested
379 * columns are not present in \p points. If the requested column range
380 * and the range in \p points do not intersect, the result has
381 * columnCount() == 0.
383 * \p firstColumn is relative to the whole data set, i.e., not relative
384 * to points.firstColumn().
386 * Mainly intended for internal use.
388 AnalysisDataPointSetRef(const AnalysisDataPointSetRef& points, int firstColumn, int columnCount);
391 * Returns the frame header for the frame of this point set.
393 const AnalysisDataFrameHeader& header() const { return header_; }
394 //! \copydoc AnalysisDataFrameHeader::index()
395 int frameIndex() const { return header_.index(); }
396 //! \copydoc AnalysisDataFrameHeader::x()
397 real x() const { return header_.x(); }
398 //! \copydoc AnalysisDataFrameHeader::dx()
399 real dx() const { return header_.dx(); }
400 //! Returns zero-based index of the dataset that this set is part of.
401 int dataSetIndex() const { return dataSetIndex_; }
402 //! Returns zero-based index of the first column included in this set.
403 int firstColumn() const { return firstColumn_; }
404 //! Returns the number of columns included in this set.
405 int columnCount() const { return ssize(values()); }
406 //! Returns zero-based index of the last column included in this set (inclusive).
407 int lastColumn() const { return firstColumn_ + columnCount() - 1; }
409 * Returns reference container for all values.
411 * First value in the returned container corresponds to firstColumn().
413 const AnalysisDataValuesRef& values() const { return values_; }
415 * Returns data value for a column in this set.
417 * \param[in] i Zero-based column index relative to firstColumn().
418 * Should be >= 0 and < columnCount().
422 GMX_ASSERT(i >= 0 && i < columnCount(), "Out of range data access");
423 return values()[i].value();
426 * Returns error estimate for a column in this set if applicable.
428 * \param[in] i Zero-based column index relative to firstColumn().
429 * Should be >= 0 and < columnCount().
431 * Currently, this method returns zero if the source data does not
436 GMX_ASSERT(i >= 0 && i < columnCount(), "Out of range data access");
437 return values()[i].error();
440 * Returns whether a column is present in this set.
442 * \param[in] i Zero-based column index relative to firstColumn().
443 * Should be >= 0 and < columnCount().
445 * If present(i) returns false, it is depends on the source data
446 * whether y(i) and/or dy(i) are defined.
448 bool present(int i) const
450 GMX_ASSERT(i >= 0 && i < columnCount(), "Out of range data access");
451 return values()[i].isPresent();
454 * Returns true if all points in this point set are present.
456 * That is, if present() would return true for all points.
458 bool allPresent() const;
461 AnalysisDataFrameHeader header_;
464 AnalysisDataValuesRef values_;
469 * Value type wrapper for non-mutable access to a data frame.
471 * Default copy constructor and assignment operator are used and work as
473 * Typically new objects of this type are only constructed internally by the
474 * library and in classes that are derived from AbstractAnalysisData.
476 * Methods in this class do not throw, but may contain asserts for incorrect
479 * Note that it is not possible to change the contents of an initialized
480 * object, except by assigning a new object to replace it completely.
483 * \ingroup module_analysisdata
485 class AnalysisDataFrameRef
489 * Constructs an invalid frame reference.
491 * Return values of other methods than isValid() are unspecified for
492 * the constructed object.
494 AnalysisDataFrameRef();
496 * Constructs a frame reference from given values.
498 * \param[in] header Header for the frame.
499 * \param[in] values Values for each column.
500 * \param[in] pointSets Point set data.
502 AnalysisDataFrameRef(const AnalysisDataFrameHeader& header,
503 const AnalysisDataValuesRef& values,
504 const AnalysisDataPointSetInfosRef& pointSets);
506 * Constructs a frame reference from given values.
508 * \param[in] header Header for the frame.
509 * \param[in] values Values for each column.
510 * \param[in] pointSets Point set data.
512 AnalysisDataFrameRef(const AnalysisDataFrameHeader& header,
513 const std::vector<AnalysisDataValue>& values,
514 const std::vector<AnalysisDataPointSetInfo>& pointSets);
516 * Constructs a frame reference to a subset of columns.
518 * \param[in] frame Frame to use as source.
519 * \param[in] firstColumn First column index to include.
520 * \param[in] columnCount Number of columns to include.
522 * Creates a frame reference that contains \p columnCount columns
523 * starting from \p firstColumn from \p frame, or a subset if all
524 * requested columns are not present in \p frame.
526 * Mainly intended for internal use.
528 AnalysisDataFrameRef(const AnalysisDataFrameRef& frame, int firstColumn, int columnCount);
531 * Returns whether the object refers to a valid frame.
533 * If returns false, return values of other methods are not specified.
535 bool isValid() const { return header().isValid(); }
536 //! Returns the header for this frame.
537 const AnalysisDataFrameHeader& header() const { return header_; }
538 //! \copydoc AnalysisDataFrameHeader::index()
539 int frameIndex() const { return header().index(); }
540 //! \copydoc AnalysisDataFrameHeader::x()
541 real x() const { return header().x(); }
542 //! \copydoc AnalysisDataFrameHeader::dx()
543 real dx() const { return header().dx(); }
545 * Returns the number of point sets for this frame.
547 * Returns zero for an invalid frame.
549 int pointSetCount() const { return ssize(pointSets_); }
551 * Returns point set reference for a given point set.
553 * Should not be called for invalid frames.
555 AnalysisDataPointSetRef pointSet(int index) const
557 GMX_ASSERT(isValid(), "Invalid data frame accessed");
558 GMX_ASSERT(index >= 0 && index < pointSetCount(), "Out of range data access");
559 return AnalysisDataPointSetRef(header_, pointSets_[index], values_);
562 * Convenience method for accessing a column value in simple data.
564 * \copydetails AnalysisDataPointSetRef::y()
566 real y(int i) const { return singleColumnValue(i).value(); }
568 * Convenience method for accessing error for a column value in simple
571 * \copydetails AnalysisDataPointSetRef::dy()
573 real dy(int i) const { return singleColumnValue(i).error(); }
575 * Convenience method for accessing present status for a column in
578 * \copydetails AnalysisDataPointSetRef::present()
580 bool present(int i) const { return singleColumnValue(i).isPresent(); }
582 * Returns true if all points in this frame are present.
584 bool allPresent() const;
587 //! Helper method for accessing single columns in simple data.
588 const AnalysisDataValue& singleColumnValue(int i) const
590 GMX_ASSERT(isValid(), "Invalid data frame accessed");
591 GMX_ASSERT(pointSets_.size() == 1U && pointSets_[0].firstColumn() == 0,
592 "Convenience method not available for multiple point sets");
593 GMX_ASSERT(i >= 0 && i < ssize(values_), "Out of range data access");
597 AnalysisDataFrameHeader header_;
598 AnalysisDataValuesRef values_;
599 AnalysisDataPointSetInfosRef pointSets_;