f4d8515d31829e74acce606cfa28765c958f071a
[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,
128                         nullptr,
129                         nullptr,
130                         nrFrames_,
131                         1,
132                         get_acfnout(),
133                         &ptr,
134                         data_->getDt(),
135                         mode,
136                         nrRestart,
137                         bAverage,
138                         bNormalize,
139                         bVerbose,
140                         data_->getStartTime(),
141                         data_->getEndTime(),
142                         effnNONE);
143
144         double testResult = 0;
145         for (int i = 0; i < get_acfnout(); i++)
146         {
147             testResult += result[i];
148         }
149         checker_.checkSequenceArray(get_acfnout(), ptr, "AutocorrelationFunction");
150         checker_.checkReal(testResult, "Integral");
151     }
152
153     static int getDim(unsigned long type)
154     {
155         switch (type)
156         {
157             case eacNormal: return 1;
158             case eacVector: return 3;
159             case eacCos:
160                 return 1;
161                 // Several intended fall-throughs follow
162             case eacRcross:
163             case eacP0:
164             case eacP1:
165             case eacP2:
166             case eacP3:
167             case eacP4: return 3;
168             case eacIden: return 1;
169             default: GMX_RELEASE_ASSERT(false, "Invalid auto correlation option"); return -1;
170         }
171     }
172 };
173
174 int                       AutocorrTest::nrFrames_;
175 CorrelationDataSetPointer AutocorrTest::data_;
176 t_pargs*                  AutocorrTest::tempArgs_;
177
178 TEST_F(AutocorrTest, EacNormal)
179 {
180     test(eacNormal, true);
181 }
182
183 TEST_F(AutocorrTest, EacNoNormalize)
184 {
185     test(eacNormal, false);
186 }
187
188 TEST_F(AutocorrTest, EacCos)
189 {
190     test(eacCos, true);
191 }
192
193 TEST_F(AutocorrTest, EacVector)
194 {
195     test(eacVector, true);
196 }
197
198 TEST_F(AutocorrTest, EacRcross)
199 {
200     test(eacRcross, true);
201 }
202
203 TEST_F(AutocorrTest, EacP0)
204 {
205     test(eacP0, true);
206 }
207
208 TEST_F(AutocorrTest, EacP1)
209 {
210     test(eacP1, true);
211 }
212
213 TEST_F(AutocorrTest, EacP2)
214 {
215     test(eacP2, true);
216 }
217
218 TEST_F(AutocorrTest, EacP3)
219 {
220     test(eacP3, true);
221 }
222
223 TEST_F(AutocorrTest, EacP4)
224 {
225     test(eacP4, true);
226 }
227
228
229 } // namespace
230
231 } // namespace gmx