74fb2ebd8edf820e391a2713be44ac1b6f219132
[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,2015, 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/legacyheaders/oenv.h"
53 #include "gromacs/utility/smalloc.h"
54
55 #include "testutils/refdata.h"
56 #include "testutils/testasserts.h"
57 #include "testutils/testfilemanager.h"
58
59 namespace gmx
60 {
61
62 namespace
63 {
64
65 class ExpfitData
66 {
67     public:
68         int               nrLines_;
69         std::vector<real> x_, y_;
70         real              startTime_, endTime_, dt_;
71 };
72
73 class ExpfitTest : public ::testing::Test
74 {
75
76     protected:
77         static std::vector<ExpfitData> data_;
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 = NULL;
89             std::vector<std::string> fileName;
90             fileName.push_back(test::TestFileManager::getInputFilePath("testINVEXP.xvg"));
91             fileName.push_back(test::TestFileManager::getInputFilePath("testPRES.xvg"));
92             fileName.push_back(test::TestFileManager::getInputFilePath("testINVEXP79.xvg"));
93             fileName.push_back(test::TestFileManager::getInputFilePath("testERF.xvg"));
94             fileName.push_back(test::TestFileManager::getInputFilePath("testERREST.xvg"));
95             for (std::vector<std::string>::iterator i = fileName.begin(); i < fileName.end(); ++i)
96             {
97                 const char * name = i->c_str();
98                 int          nrColumns;
99                 ExpfitData   ed;
100                 ed.nrLines_   = read_xvg(name, &tempValues, &nrColumns);
101                 ed.dt_        = tempValues[0][1] - tempValues[0][0];
102                 ed.startTime_ = tempValues[0][0];
103                 ed.endTime_   = tempValues[0][ed.nrLines_-1];
104                 for (int j = 0; j  < ed.nrLines_; j++)
105                 {
106                     ed.x_.push_back((real)tempValues[0][j]);
107                     ed.y_.push_back((real)tempValues[1][j]);
108                 }
109                 data_.push_back(ed);
110
111                 // Free memory that was allocated in read_xvg
112                 for (int j = 0; j < nrColumns; j++)
113                 {
114                     sfree(tempValues[j]);
115                     tempValues[j] = NULL;
116                 }
117                 sfree(tempValues);
118                 tempValues = NULL;
119             }
120         }
121
122         static void TearDownTestCase()
123         {
124         }
125
126         void test(int type, double result[], double tolerance,
127                   unsigned int testType)
128         {
129             int          nfitparm = effnNparams(type);
130             output_env_t oenv;
131
132             if (testType >= data_.size())
133             {
134                 GMX_THROW(InvalidInputError("testType out of range"));
135             }
136             output_env_init_default(&oenv);
137             do_lmfit(data_[testType].nrLines_,
138                      &(data_[testType].y_[0]),
139                      NULL,
140                      data_[testType].dt_,
141                      &(data_[testType].x_[0]),
142                      data_[testType].startTime_,
143                      data_[testType].endTime_,
144                      oenv, false, type, result, 0, NULL);
145             output_env_done(oenv);
146             checker_.setDefaultTolerance(test::relativeToleranceAsFloatingPoint(1, tolerance));
147             checker_.checkSequenceArray(nfitparm, result, "result");
148         }
149 };
150
151
152 //static var
153 std::vector<ExpfitData> ExpfitTest::data_;
154
155 TEST_F (ExpfitTest, EffnEXP1) {
156     double  param[] = {25};
157     test(effnEXP1, param, 1e-5, 0);
158 }
159
160 TEST_F (ExpfitTest, EffnEXP2) {
161     double  param[] = {35, 0.5};
162     test(effnEXP2, param, 3e-5, 0);
163 }
164
165 TEST_F (ExpfitTest, EffnEXPEXP) {
166     double param[] = {5, 0.5, 45};
167     test(effnEXPEXP, param, 1e-2, 0);
168 }
169
170 TEST_F (ExpfitTest, EffnEXP5) {
171     double  param[] = {0.5, 5, 0.5, 50, 0.002};
172     test(effnEXP5, param, 1e-2, 2);
173 }
174
175 TEST_F (ExpfitTest, EffnEXP7) {
176     double  param[] = {0.1, 2, 0.5, 30, 0.3, 50, -0.002};
177     test(effnEXP7, param, 1e-2, 2);
178 }
179
180 TEST_F (ExpfitTest, EffnEXP9) {
181     double  param[] = {0.4, 5, 0.2, 30, 0.1, 70, 0.2, 200, -0.05};
182     test(effnEXP9, param, 4e-2, 2);
183 }
184
185 TEST_F (ExpfitTest, EffnERF) {
186     double  param[] = {80, 120, 180, 5};
187     test(effnERF, param, 1e-1, 3);
188 }
189
190 TEST_F (ExpfitTest, EffnERREST) {
191     double  param[] = {1, 0.9, 100};
192     test(effnERREST, param, 5e-3, 4);
193 }
194
195 TEST_F (ExpfitTest, EffnVAC) {
196     double param[] = {30, 0.0};
197     test(effnVAC, param, 0.05, 0);
198 }
199
200 TEST_F (ExpfitTest, EffnPRES) {
201     double param[] = {0.6, 10, 7, 1, 0.25, 2};
202     test(effnPRES, param, 1e-4, 1);
203 }
204
205 }
206
207 }