clang-tidy: google tests applicable
[alexxy/gromacs.git] / src / gromacs / correlationfunctions / tests / autocorr.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,2016,2017,2018, 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 test of autocorrelation function routines
38  *
39  * \author Anders G&auml;rden&auml;s <anders.gardenas@gmail.com>
40  * \author David van der Spoel <david.vanderspoel@icm.uu.se>
41  * \ingroup module_correlationfunctions
42  */
43 #include "gmxpre.h"
44
45 #include "gromacs/correlationfunctions/autocorr.h"
46
47 #include <cmath>
48
49 #include <memory>
50
51 #include <gtest/gtest.h>
52
53 #include "gromacs/compat/make_unique.h"
54 #include "gromacs/correlationfunctions/expfit.h"
55 #include "gromacs/fft/fft.h"
56 #include "gromacs/utility/gmxassert.h"
57 #include "gromacs/utility/smalloc.h"
58
59 #include "testutils/refdata.h"
60 #include "testutils/testasserts.h"
61 #include "testutils/testfilemanager.h"
62
63 #include "correlationdataset.h"
64
65 namespace gmx
66 {
67 namespace
68 {
69
70 //! Definition of pointer to class containing test data.
71 typedef std::unique_ptr<CorrelationDataSet> CorrelationDataSetPointer;
72
73 class AutocorrTest : public ::testing::Test
74 {
75     protected:
76
77         static int                                  nrFrames_;
78         static CorrelationDataSetPointer            data_;
79         // Need raw pointer for passing this to C routines
80         static t_pargs                            * tempArgs_;
81
82         test::TestReferenceData                     refData_;
83         test::TestReferenceChecker                  checker_;
84
85         // Use erefdataCreateMissing for creating new files
86         AutocorrTest( )
87             : checker_(refData_.rootChecker())
88         {
89 #if GMX_DOUBLE
90             checker_.setDefaultTolerance(test::relativeToleranceAsFloatingPoint(1, 1e-6));
91 #else
92             checker_.setDefaultTolerance(test::relativeToleranceAsFloatingPoint(1, 1e-3));
93 #endif
94         }
95
96         // Static initiation, only run once every test.
97         static void SetUpTestCase()
98         {
99             int         n        = 0;
100             std::string fileName = "testCOS3.xvg";
101             data_                = gmx::compat::make_unique<CorrelationDataSet>(fileName);
102             nrFrames_            = data_->getNrLines();
103             tempArgs_            = add_acf_pargs(&n, nullptr);
104         }
105
106         static void TearDownTestCase()
107         {
108             sfree(tempArgs_);
109             tempArgs_ = nullptr;
110             gmx_fft_cleanup();
111         }
112
113         void test(unsigned long mode, bool bNormalize)
114         {
115             bool              bAverage      = true;
116             bool              bVerbose      = false;
117             int               nrRestart     = 1;
118             int               dim           = getDim(mode);
119             std::vector<real> result;
120
121             for (int i = 0; i < nrFrames_; i++)
122             {
123                 for (int m = 0; m < dim; m++)
124                 {
125                     result.push_back(data_->getValue(m, i));
126                 }
127             }
128             real *ptr = result.data();
129             low_do_autocorr(nullptr, nullptr, nullptr,   nrFrames_, 1,
130                             get_acfnout(), &ptr, data_->getDt(), mode,
131                             nrRestart, bAverage, bNormalize,
132                             bVerbose, data_->getStartTime(), data_->getEndTime(),
133                             effnNONE);
134
135             double testResult = 0;
136             for (int i = 0; i < get_acfnout(); i++)
137             {
138                 testResult += result[i];
139             }
140             checker_.checkSequenceArray(get_acfnout(), ptr,
141                                         "AutocorrelationFunction");
142             checker_.checkReal(testResult, "Integral");
143         }
144
145         int getDim(unsigned long type)
146         {
147             switch (type)
148             {
149                 case eacNormal:
150                     return 1;
151                 case eacVector:
152                     return 3;
153                 case eacCos:
154                     return 1;
155                 case eacRcross:
156                     return 3;
157                 case eacP0:
158                     return 3;
159                 case eacP1:
160                     return 3;
161                 case eacP2:
162                     return 3;
163                 case eacP3:
164                     return 3;
165                 case eacP4:
166                     return 3;
167                 case eacIden:
168                     return 1;
169                 default:
170                     GMX_RELEASE_ASSERT(false, "Invalid auto correlation option");
171                     return -1;
172             }
173
174         }
175
176 };
177
178 int                         AutocorrTest::nrFrames_;
179 CorrelationDataSetPointer   AutocorrTest::data_;
180 t_pargs                   * AutocorrTest::tempArgs_;
181
182 TEST_F (AutocorrTest, EacNormal)
183 {
184     test(eacNormal, true);
185 }
186
187 TEST_F (AutocorrTest, EacNoNormalize)
188 {
189     test(eacNormal, false);
190 }
191
192 TEST_F (AutocorrTest, EacCos)
193 {
194     test(eacCos, true);
195 }
196
197 TEST_F (AutocorrTest, EacVector)
198 {
199     test(eacVector, true);
200 }
201
202 TEST_F (AutocorrTest, EacRcross)
203 {
204     test(eacRcross, true);
205 }
206
207 TEST_F (AutocorrTest, EacP0)
208 {
209     test(eacP0, true);
210 }
211
212 TEST_F (AutocorrTest, EacP1)
213 {
214     test(eacP1, true);
215 }
216
217 TEST_F (AutocorrTest, EacP2)
218 {
219     test(eacP2, true);
220 }
221
222 TEST_F (AutocorrTest, EacP3)
223 {
224     test(eacP3, true);
225 }
226
227 TEST_F (AutocorrTest, EacP4)
228 {
229     test(eacP4, true);
230 }
231
232
233 }  // namespace
234
235 }  // namespace gmx