2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 * Implements test of exponential fitting routines
39 * \author Anders Gärdenäs <anders.gardenas@gmail.com>
40 * \author David van der Spoel <david.vanderspoel@icm.uu.se>
41 * \ingroup module_correlationfunctions
45 #include "gromacs/correlationfunctions/expfit.h"
49 #include <gtest/gtest.h>
51 #include "gromacs/fileio/xvgr.h"
52 #include "gromacs/utility/smalloc.h"
54 #include "testutils/refdata.h"
55 #include "testutils/testasserts.h"
56 #include "testutils/testfilemanager.h"
58 //! Number of data files for testing.
59 #define expTestNrTypes 3
67 class ExpfitTest : public ::testing::Test
72 static std::vector<real> values_[expTestNrTypes];
73 static int nrColumns_;
74 static std::vector<real> standardDev_;
75 static real startTime_;
77 static real timeDeriv_;
78 test::TestReferenceData refData_;
79 test::TestReferenceChecker checker_;
81 : checker_(refData_.rootChecker())
85 // Static initiation, only run once every test.
86 static void SetUpTestCase()
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++)
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_);
99 // Generating standard deviation
102 double fac = 1.0/nrLines_;
103 for (int j = 0; j < nrLines_; j++)
105 standardDev_.push_back(fac);
107 timeDeriv_ = tempValues[0][1] - tempValues[0][0];
108 startTime_ = tempValues[0][0];
109 endTime_ = tempValues[0][nrLines_-1];
112 for (int j = 0; j < nrLines_; j++)
114 values_[i].push_back((real)tempValues[1][j]);
117 // Free memory that was allocated in read_xvg
118 for (int i = 0; i < nrColumns_; i++)
120 sfree(tempValues[i]);
121 tempValues[i] = NULL;
128 static void TearDownTestCase()
132 void test(int type, double result[], double tolerance, int testType)
134 int nfitparm = effnNparams(type);
136 do_lmfit(nrLines_, &values_[testType][0], &standardDev_[0], timeDeriv_,
137 NULL, startTime_, endTime_, NULL, false, type, result, 0);
139 checker_.setDefaultTolerance(test::relativeToleranceAsFloatingPoint(1, tolerance));
140 checker_.checkSequenceArray(nfitparm, result, "result");
146 int ExpfitTest::nrLines_;
147 //cppcheck-suppress arrayIndexOutOfBounds fixed in 1.68-dev
148 std::vector<real> ExpfitTest::values_[expTestNrTypes];
149 int ExpfitTest::nrColumns_;
150 std::vector<real> ExpfitTest::standardDev_;
151 real ExpfitTest::startTime_;
152 real ExpfitTest::endTime_;
153 real ExpfitTest::timeDeriv_;
155 TEST_F (ExpfitTest, EffnEXP1) {
156 double param[] = {25};
157 test(effnEXP1, param, 1e-6, 0);
160 TEST_F (ExpfitTest, EffnEXP2) {
161 double param[] = {35, 0.5};
162 test(effnEXP2, param, 1e-6, 0);
165 TEST_F (ExpfitTest, EffnEXP3) {
166 double param[] = {45, 0.5, 5};
167 test(effnEXP3, param, 1e-4, 0);
170 TEST_F (ExpfitTest, EffnEXP5) {
171 double param[] = {0.5, 5, 0.5, 50, 0.002};
172 test(effnEXP5, param, 1e-4, 0);
175 TEST_F (ExpfitTest, EffnEXP7) {
176 double param[] = {0.5, 5, -0.02, 0.5, 0.5, 50, -0.002};
177 test(effnEXP7, param, 1e-4, 0);
180 TEST_F (ExpfitTest, EffnEXP9) {
181 double param[] = {2, 1200, -1, 300, 0.7, 70, 0.5, 6, -0.5};
182 test(effnEXP9, param, 4e-2, 0);
185 TEST_F (ExpfitTest, EffnERF) {
186 double param[] = {0.5, 0.5, 0.5, 1};
187 test(effnERF, param, 1e-2, 0);
190 TEST_F (ExpfitTest, EffnERREST) {
191 double param[] = {0.5, 0.7, 0.3};
192 test(effnERREST, param, 1e-4, 2);
195 TEST_F (ExpfitTest, EffnVAC) {
196 double param[] = {0.5, 0.05};
197 test(effnVAC, param, 1e-4, 0);
200 TEST_F (ExpfitTest, DISABLED_EffnPRES) {
201 //TODO: This test is prodocues NaNs and INFs. Fix and then reactivate.
202 double param[] = {0, 10, 4, 1, 0.5, 1};
203 test(effnPRES, param, 1e-4, 1);