9c1dd7ebc3adc9d39a180228817da5d41ce3fdb6
[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,2019, 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/correlationfunctions/expfit.h"
54 #include "gromacs/fft/fft.h"
55 #include "gromacs/utility/gmxassert.h"
56 #include "gromacs/utility/smalloc.h"
57
58 #include "testutils/refdata.h"
59 #include "testutils/testasserts.h"
60 #include "testutils/testfilemanager.h"
61
62 #include "correlationdataset.h"
63
64 namespace gmx
65 {
66 namespace
67 {
68
69 //! Definition of pointer to class containing test data.
70 typedef std::unique_ptr<CorrelationDataSet> CorrelationDataSetPointer;
71
72 class AutocorrTest : public ::testing::Test
73 {
74 protected:
75     static int                       nrFrames_;
76     static CorrelationDataSetPointer data_;
77     // Need raw pointer for passing this to C routines
78     static t_pargs* tempArgs_;
79
80     test::TestReferenceData    refData_;
81     test::TestReferenceChecker checker_;
82
83     // Use erefdataCreateMissing for creating new files
84     AutocorrTest() : checker_(refData_.rootChecker())
85     {
86 #if GMX_DOUBLE
87         checker_.setDefaultTolerance(test::relativeToleranceAsFloatingPoint(1, 1e-6));
88 #else
89         checker_.setDefaultTolerance(test::relativeToleranceAsFloatingPoint(1, 1e-3));
90 #endif
91     }
92
93     // Static initiation, only run once every test.
94     static void SetUpTestCase()
95     {
96         int         n        = 0;
97         std::string fileName = "testCOS3.xvg";
98         data_                = std::make_unique<CorrelationDataSet>(fileName);
99         nrFrames_            = data_->getNrLines();
100         tempArgs_            = add_acf_pargs(&n, nullptr);
101     }
102
103     static void TearDownTestCase()
104     {
105         sfree(tempArgs_);
106         tempArgs_ = nullptr;
107         gmx_fft_cleanup();
108     }
109
110     void test(unsigned long mode, bool bNormalize)
111     {
112         bool              bAverage  = true;
113         bool              bVerbose  = false;
114         int               nrRestart = 1;
115         int               dim       = getDim(mode);
116         std::vector<real> result;
117
118         for (int i = 0; i < nrFrames_; i++)
119         {
120             for (int m = 0; m < dim; m++)
121             {
122                 result.push_back(data_->getValue(m, i));
123             }
124         }
125         real* ptr = result.data();
126         low_do_autocorr(nullptr, nullptr, nullptr, nrFrames_, 1, get_acfnout(), &ptr,
127                         data_->getDt(), mode, nrRestart, bAverage, bNormalize, bVerbose,
128                         data_->getStartTime(), data_->getEndTime(), effnNONE);
129
130         double testResult = 0;
131         for (int i = 0; i < get_acfnout(); i++)
132         {
133             testResult += result[i];
134         }
135         checker_.checkSequenceArray(get_acfnout(), ptr, "AutocorrelationFunction");
136         checker_.checkReal(testResult, "Integral");
137     }
138
139     int getDim(unsigned long type)
140     {
141         switch (type)
142         {
143             case eacNormal: return 1;
144             case eacVector: return 3;
145             case eacCos: return 1;
146             case eacRcross: return 3;
147             case eacP0: return 3;
148             case eacP1: return 3;
149             case eacP2: return 3;
150             case eacP3: return 3;
151             case eacP4: return 3;
152             case eacIden: return 1;
153             default: GMX_RELEASE_ASSERT(false, "Invalid auto correlation option"); return -1;
154         }
155     }
156 };
157
158 int                       AutocorrTest::nrFrames_;
159 CorrelationDataSetPointer AutocorrTest::data_;
160 t_pargs*                  AutocorrTest::tempArgs_;
161
162 TEST_F(AutocorrTest, EacNormal)
163 {
164     test(eacNormal, true);
165 }
166
167 TEST_F(AutocorrTest, EacNoNormalize)
168 {
169     test(eacNormal, false);
170 }
171
172 TEST_F(AutocorrTest, EacCos)
173 {
174     test(eacCos, true);
175 }
176
177 TEST_F(AutocorrTest, EacVector)
178 {
179     test(eacVector, true);
180 }
181
182 TEST_F(AutocorrTest, EacRcross)
183 {
184     test(eacRcross, true);
185 }
186
187 TEST_F(AutocorrTest, EacP0)
188 {
189     test(eacP0, true);
190 }
191
192 TEST_F(AutocorrTest, EacP1)
193 {
194     test(eacP1, true);
195 }
196
197 TEST_F(AutocorrTest, EacP2)
198 {
199     test(eacP2, true);
200 }
201
202 TEST_F(AutocorrTest, EacP3)
203 {
204     test(eacP3, true);
205 }
206
207 TEST_F(AutocorrTest, EacP4)
208 {
209     test(eacP4, true);
210 }
211
212
213 } // namespace
214
215 } // namespace gmx