Apply clang-format-11
[alexxy/gromacs.git] / src / gromacs / analysisdata / arraydata.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.
5  * Copyright (c) 2015,2017,2019,2020,2021, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 /*! \internal \file
37  * \brief
38  * Implements classes in arraydata.h.
39  *
40  * \author Teemu Murtola <teemu.murtola@gmail.com>
41  * \ingroup module_analysisdata
42  */
43 #include "gmxpre.h"
44
45 #include "arraydata.h"
46
47 #include <algorithm>
48
49 #include "gromacs/analysisdata/dataframe.h"
50 #include "gromacs/analysisdata/datamodulemanager.h"
51 #include "gromacs/utility/exceptions.h"
52 #include "gromacs/utility/gmxassert.h"
53
54 namespace gmx
55 {
56
57 AbstractAnalysisArrayData::AbstractAnalysisArrayData() :
58     rowCount_(0), pointSetInfo_(0, 0, 0, 0), xstep_(1.0), bUniformX_(true), bReady_(false)
59 {
60     xvalue_.push_back(0);
61 }
62
63 AbstractAnalysisArrayData::~AbstractAnalysisArrayData() {}
64
65
66 AnalysisDataFrameRef AbstractAnalysisArrayData::tryGetDataFrameInternal(int index) const
67 {
68     if (!isAllocated())
69     {
70         return AnalysisDataFrameRef();
71     }
72     return AnalysisDataFrameRef(AnalysisDataFrameHeader(index, xvalue(index), 0.0),
73                                 makeConstArrayRef(value_).subArray(index * columnCount(), columnCount()),
74                                 constArrayRefFromArray(&pointSetInfo_, 1));
75 }
76
77
78 bool AbstractAnalysisArrayData::requestStorageInternal(int /*nframes*/)
79 {
80     return true;
81 }
82
83
84 void AbstractAnalysisArrayData::setColumnCount(int ncols)
85 {
86     GMX_RELEASE_ASSERT(!isAllocated(), "Cannot change column count after data has been allocated");
87     AbstractAnalysisData::setColumnCount(0, ncols);
88     pointSetInfo_ = AnalysisDataPointSetInfo(0, ncols, 0, 0);
89 }
90
91
92 void AbstractAnalysisArrayData::setRowCount(int rowCount)
93 {
94     GMX_RELEASE_ASSERT(rowCount > 0, "Invalid number of rows");
95     GMX_RELEASE_ASSERT(!isAllocated(), "Cannot change row count after data has been allocated");
96     GMX_RELEASE_ASSERT(bUniformX_ || xvalue_.empty() || rowCount == ssize(xvalue_),
97                        "X axis set with setXAxisValue() does not match the row count");
98     xvalue_.resize(rowCount);
99     if (bUniformX_ && rowCount > rowCount_)
100     {
101         for (int i = rowCount_; i < rowCount; ++i)
102         {
103             xvalue_[i] = xvalue_[0] + i * xstep_;
104         }
105     }
106     rowCount_ = rowCount;
107 }
108
109
110 void AbstractAnalysisArrayData::allocateValues()
111 {
112     GMX_RELEASE_ASSERT(!isAllocated(), "Can only allocate values once");
113     GMX_RELEASE_ASSERT(rowCount() > 0 && columnCount() > 0,
114                        "Row and column counts must be set before allocating values");
115     value_.resize(rowCount() * columnCount());
116     std::vector<AnalysisDataValue>::iterator i;
117     for (i = value_.begin(); i != value_.end(); ++i)
118     {
119         i->setValue(0.0);
120     }
121 }
122
123
124 void AbstractAnalysisArrayData::setXAxis(real start, real step)
125 {
126     GMX_RELEASE_ASSERT(!bReady_, "X axis cannot be set after data is finished");
127     xvalue_[0] = start;
128     xstep_     = step;
129     bUniformX_ = true;
130     for (int i = 0; i < rowCount_; ++i)
131     {
132         xvalue_[i] = start + i * xstep_;
133     }
134 }
135
136
137 void AbstractAnalysisArrayData::setXAxisValue(int row, real value)
138 {
139     GMX_RELEASE_ASSERT(!bReady_, "X axis cannot be set after data is finished");
140     if (rowCount_ > 0)
141     {
142         GMX_RELEASE_ASSERT(row >= 0 && row < rowCount(), "Row index out of range");
143     }
144     else if (row >= ssize(xvalue_))
145     {
146         xvalue_.resize(row + 1);
147     }
148     bUniformX_   = false;
149     xstep_       = 0.0;
150     xvalue_[row] = value;
151 }
152
153
154 void AbstractAnalysisArrayData::valuesReady()
155 {
156     GMX_RELEASE_ASSERT(isAllocated(), "There must be some data");
157     if (bReady_)
158     {
159         return;
160     }
161     bReady_ = true;
162
163     AnalysisDataModuleManager& modules = moduleManager();
164     modules.notifyDataStart(this);
165     for (int i = 0; i < rowCount(); ++i)
166     {
167         AnalysisDataFrameHeader header(i, xvalue(i), 0);
168         modules.notifyFrameStart(header);
169         modules.notifyPointsAdd(AnalysisDataPointSetRef(
170                 header, pointSetInfo_, makeConstArrayRef(value_).subArray(i * columnCount(), columnCount())));
171         modules.notifyFrameFinish(header);
172     }
173     modules.notifyDataFinish();
174 }
175
176
177 void AbstractAnalysisArrayData::copyContents(const AbstractAnalysisArrayData* src,
178                                              AbstractAnalysisArrayData*       dest)
179 {
180     GMX_RELEASE_ASSERT(src->isAllocated(), "Source data must not be empty");
181     GMX_RELEASE_ASSERT(!dest->isAllocated(), "Destination data must not be allocated");
182     dest->setColumnCount(src->columnCount());
183     dest->setRowCount(src->rowCount());
184     dest->allocateValues();
185     dest->xstep_     = src->xstep_;
186     dest->bUniformX_ = src->bUniformX_;
187     std::copy(src->xvalue_.begin(), src->xvalue_.end(), dest->xvalue_.begin());
188     std::copy(src->value_.begin(), src->value_.end(), dest->value_.begin());
189 }
190
191 } // namespace gmx