Reorganizing analysis of correlation functions.
[alexxy/gromacs.git] / src / gromacs / correlationfunctions / tests / expfit.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014, 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 exponential fitting 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/expfit.h"
46
47 #include <cmath>
48
49 #include <gtest/gtest.h>
50
51 #include "gromacs/fileio/xvgr.h"
52 #include "gromacs/utility/smalloc.h"
53
54 #include "testutils/refdata.h"
55 #include "testutils/testasserts.h"
56 #include "testutils/testfilemanager.h"
57
58 //! Number of data files for testing.
59 #define expTestNrTypes 3
60
61 namespace gmx
62 {
63
64 namespace
65 {
66
67 class ExpfitTest : public ::testing::Test
68 {
69
70     protected:
71         static int                 nrLines_;
72         static std::vector<real>   values_[expTestNrTypes];
73         static int                 nrColumns_;
74         static std::vector<real>   standardDev_;
75         static real                startTime_;
76         static real                endTime_;
77         static real                timeDeriv_;
78         test::TestReferenceData    refData_;
79         test::TestReferenceChecker checker_;
80         ExpfitTest( )
81             : checker_(refData_.rootChecker())
82         {
83         }
84
85         // Static initiation, only run once every test.
86         static void SetUpTestCase()
87         {
88             double   ** tempValues;
89             std::string fileName[expTestNrTypes];
90             fileName[0] = test::TestFileManager::getInputFilePath("testINVEXP.xvg");
91             fileName[1] = test::TestFileManager::getInputFilePath("testPRES.xvg");
92             fileName[2] = test::TestFileManager::getInputFilePath("testEXP.xvg");
93             for (int i = 0; i < expTestNrTypes; i++)
94             {
95                 const char * name = fileName[i].c_str();
96                 // TODO: this assumes all files have the same length.
97                 nrLines_     = read_xvg(name, &tempValues, &nrColumns_);
98
99                 // Generating standard deviation
100                 if (i == 0)
101                 {
102                     double fac = 1.0/nrLines_;
103                     for (int j = 0; j < nrLines_; j++)
104                     {
105                         standardDev_.push_back(fac);
106                     }
107                     timeDeriv_ = tempValues[0][1] - tempValues[0][0];
108                     startTime_ = tempValues[0][0];
109                     endTime_   = tempValues[0][nrLines_-1];
110                 }
111
112                 for (int j = 0; j  < nrLines_; j++)
113                 {
114                     values_[i].push_back((real)tempValues[1][j]);
115                 }
116
117                 // Free memory that was allocated in read_xvg
118                 for (int i = 0; i < nrColumns_; i++)
119                 {
120                     sfree(tempValues[i]);
121                     tempValues[i] = NULL;
122                 }
123                 sfree(tempValues);
124                 tempValues = NULL;
125             }
126         }
127
128         static void TearDownTestCase()
129         {
130         }
131
132         void test(int type, double result[], double tolerance, int testType)
133         {
134             int     nfitparm = effnNparams(type);
135
136             do_lmfit(nrLines_, &values_[testType][0], &standardDev_[0], timeDeriv_,
137                      NULL, startTime_, endTime_, NULL, false, type, result, 0);
138
139             checker_.setDefaultTolerance(test::relativeToleranceAsFloatingPoint(1, tolerance));
140             checker_.checkSequenceArray(nfitparm, result, "result");
141         }
142 };
143
144
145 //static var
146 int               ExpfitTest::nrLines_;
147 std::vector<real> ExpfitTest::values_[expTestNrTypes];
148 int               ExpfitTest::nrColumns_;
149 std::vector<real> ExpfitTest::standardDev_;
150 real              ExpfitTest::startTime_;
151 real              ExpfitTest::endTime_;
152 real              ExpfitTest::timeDeriv_;
153
154 TEST_F (ExpfitTest, EffnEXP1) {
155     double  param[] = {25};
156     test(effnEXP1, param, 1e-6, 0);
157 }
158
159 TEST_F (ExpfitTest, EffnEXP2) {
160     double  param[] = {35, 0.5};
161     test(effnEXP2, param, 1e-6, 0);
162 }
163
164 TEST_F (ExpfitTest, EffnEXP3) {
165     double param[] = {45, 0.5, 5};
166     test(effnEXP3, param, 1e-4, 0);
167 }
168
169 TEST_F (ExpfitTest, EffnEXP5) {
170     double  param[] = {0.5, 5, 0.5, 50, 0.002};
171     test(effnEXP5, param, 1e-4, 0);
172 }
173
174 TEST_F (ExpfitTest, EffnEXP7) {
175     double  param[] = {0.5, 5, -0.02, 0.5, 0.5, 50, -0.002};
176     test(effnEXP7, param, 1e-4, 0);
177 }
178
179 TEST_F (ExpfitTest, EffnEXP9) {
180     double  param[] = {2, 1200, -1, 300, 0.7, 70, 0.5, 6, -0.5};
181     test(effnEXP9, param, 4e-2, 0);
182 }
183
184 TEST_F (ExpfitTest, EffnERF) {
185     double  param[] = {0.5, 0.5, 0.5, 1};
186     test(effnERF, param, 1e-2, 0);
187 }
188
189 TEST_F (ExpfitTest, EffnERREST) {
190     double  param[] = {0.5, 0.7, 0.3};
191     test(effnERREST, param, 1e-4, 2);
192 }
193
194 TEST_F (ExpfitTest, EffnVAC) {
195     double param[] = {0.5, 0.05};
196     test(effnVAC, param, 1e-4, 0);
197 }
198
199 TEST_F (ExpfitTest, EffnPRES) {
200     double param[] = {0, 10, 4, 1, 0.5, 1};
201     test(effnPRES, param, 1e-4, 1);
202 }
203
204 }
205
206 }