Fixes for clang-tidy-9
[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.
5  * Copyright (c) 2019,2020, 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 test of autocorrelation function routines
39  *
40  * \author Anders G&auml;rden&auml;s <anders.gardenas@gmail.com>
41  * \author David van der Spoel <david.vanderspoel@icm.uu.se>
42  * \ingroup module_correlationfunctions
43  */
44 #include "gmxpre.h"
45
46 #include "gromacs/correlationfunctions/autocorr.h"
47
48 #include <cmath>
49
50 #include <memory>
51
52 #include <gtest/gtest.h>
53
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     static int                       nrFrames_;
77     static CorrelationDataSetPointer data_;
78     // Need raw pointer for passing this to C routines
79     static t_pargs* tempArgs_;
80
81     test::TestReferenceData    refData_;
82     test::TestReferenceChecker checker_;
83
84     // Use erefdataCreateMissing for creating new files
85     AutocorrTest() : checker_(refData_.rootChecker())
86     {
87 #if GMX_DOUBLE
88         checker_.setDefaultTolerance(test::relativeToleranceAsFloatingPoint(1, 1e-6));
89 #else
90         checker_.setDefaultTolerance(test::relativeToleranceAsFloatingPoint(1, 1e-3));
91 #endif
92     }
93
94     // Static initiation, only run once every test.
95     static void SetUpTestCase()
96     {
97         int         n        = 0;
98         std::string fileName = "testCOS3.xvg";
99         data_                = std::make_unique<CorrelationDataSet>(fileName);
100         nrFrames_            = data_->getNrLines();
101         tempArgs_            = add_acf_pargs(&n, nullptr);
102     }
103
104     static void TearDownTestCase()
105     {
106         sfree(tempArgs_);
107         tempArgs_ = nullptr;
108         gmx_fft_cleanup();
109     }
110
111     void test(unsigned long mode, bool bNormalize)
112     {
113         bool              bAverage  = true;
114         bool              bVerbose  = false;
115         int               nrRestart = 1;
116         int               dim       = getDim(mode);
117         std::vector<real> result;
118
119         for (int i = 0; i < nrFrames_; i++)
120         {
121             for (int m = 0; m < dim; m++)
122             {
123                 result.push_back(data_->getValue(m, i));
124             }
125         }
126         real* ptr = result.data();
127         low_do_autocorr(nullptr, nullptr, nullptr, nrFrames_, 1, get_acfnout(), &ptr,
128                         data_->getDt(), mode, nrRestart, bAverage, bNormalize, bVerbose,
129                         data_->getStartTime(), data_->getEndTime(), effnNONE);
130
131         double testResult = 0;
132         for (int i = 0; i < get_acfnout(); i++)
133         {
134             testResult += result[i];
135         }
136         checker_.checkSequenceArray(get_acfnout(), ptr, "AutocorrelationFunction");
137         checker_.checkReal(testResult, "Integral");
138     }
139
140     static int getDim(unsigned long type)
141     {
142         switch (type)
143         {
144             case eacNormal: return 1;
145             case eacVector: return 3;
146             case eacCos:
147                 return 1;
148                 // Several intended fall-throughs follow
149             case eacRcross:
150             case eacP0:
151             case eacP1:
152             case eacP2:
153             case eacP3:
154             case eacP4: return 3;
155             case eacIden: return 1;
156             default: GMX_RELEASE_ASSERT(false, "Invalid auto correlation option"); return -1;
157         }
158     }
159 };
160
161 int                       AutocorrTest::nrFrames_;
162 CorrelationDataSetPointer AutocorrTest::data_;
163 t_pargs*                  AutocorrTest::tempArgs_;
164
165 TEST_F(AutocorrTest, EacNormal)
166 {
167     test(eacNormal, true);
168 }
169
170 TEST_F(AutocorrTest, EacNoNormalize)
171 {
172     test(eacNormal, false);
173 }
174
175 TEST_F(AutocorrTest, EacCos)
176 {
177     test(eacCos, true);
178 }
179
180 TEST_F(AutocorrTest, EacVector)
181 {
182     test(eacVector, true);
183 }
184
185 TEST_F(AutocorrTest, EacRcross)
186 {
187     test(eacRcross, true);
188 }
189
190 TEST_F(AutocorrTest, EacP0)
191 {
192     test(eacP0, true);
193 }
194
195 TEST_F(AutocorrTest, EacP1)
196 {
197     test(eacP1, true);
198 }
199
200 TEST_F(AutocorrTest, EacP2)
201 {
202     test(eacP2, true);
203 }
204
205 TEST_F(AutocorrTest, EacP3)
206 {
207     test(eacP3, true);
208 }
209
210 TEST_F(AutocorrTest, EacP4)
211 {
212     test(eacP4, true);
213 }
214
215
216 } // namespace
217
218 } // namespace gmx