2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2017,2018,2019,2020, 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"
51 #include <gtest/gtest.h>
53 #include "gromacs/fileio/oenv.h"
54 #include "gromacs/fileio/xvgr.h"
55 #include "gromacs/utility/smalloc.h"
57 #include "testutils/refdata.h"
58 #include "testutils/testasserts.h"
59 #include "testutils/testfilemanager.h"
71 std::vector<real> x_, y_;
72 real startTime_, endTime_, dt_;
75 class ExpfitTest : public ::testing::Test
79 static std::vector<ExpfitData> data_;
80 test::TestReferenceData refData_;
81 test::TestReferenceChecker checker_;
82 ExpfitTest() : checker_(refData_.rootChecker()) {}
84 // Static initiation, only run once every test.
85 static void SetUpTestCase()
87 double** tempValues = nullptr;
88 std::vector<std::string> fileName;
89 fileName.push_back(test::TestFileManager::getInputFilePath("testINVEXP.xvg"));
90 fileName.push_back(test::TestFileManager::getInputFilePath("testPRES.xvg"));
91 fileName.push_back(test::TestFileManager::getInputFilePath("testINVEXP79.xvg"));
92 fileName.push_back(test::TestFileManager::getInputFilePath("testERF.xvg"));
93 fileName.push_back(test::TestFileManager::getInputFilePath("testERREST.xvg"));
94 for (std::vector<std::string>::iterator i = fileName.begin(); i < fileName.end(); ++i)
96 const char* name = i->c_str();
99 ed.nrLines_ = read_xvg(name, &tempValues, &nrColumns);
100 ed.dt_ = tempValues[0][1] - tempValues[0][0];
101 ed.startTime_ = tempValues[0][0];
102 ed.endTime_ = tempValues[0][ed.nrLines_ - 1];
103 for (int j = 0; j < ed.nrLines_; j++)
105 ed.x_.push_back(static_cast<real>(tempValues[0][j]));
106 ed.y_.push_back(static_cast<real>(tempValues[1][j]));
110 // Free memory that was allocated in read_xvg
111 for (int j = 0; j < nrColumns; j++)
113 sfree(tempValues[j]);
114 tempValues[j] = nullptr;
117 tempValues = nullptr;
121 static void TearDownTestCase() {}
123 void test(int type, double result[], double tolerance, unsigned int testType)
125 int nfitparm = effnNparams(type);
126 gmx_output_env_t* oenv;
128 if (testType >= data_.size())
130 GMX_THROW(InvalidInputError("testType out of range"));
132 output_env_init_default(&oenv);
133 do_lmfit(data_[testType].nrLines_,
134 &(data_[testType].y_[0]),
137 &(data_[testType].x_[0]),
138 data_[testType].startTime_,
139 data_[testType].endTime_,
146 output_env_done(oenv);
147 checker_.setDefaultTolerance(test::relativeToleranceAsFloatingPoint(1, tolerance));
148 checker_.checkSequenceArray(nfitparm, result, "result");
154 std::vector<ExpfitData> ExpfitTest::data_;
156 // TODO calling test() leads to a fatal error, which we could in
157 // principle test for.
159 TEST_F(ExpfitTest, EffnEXP1)
161 double param[] = { 25 };
162 test(effnEXP1, param, 1e-5, 0);
165 TEST_F(ExpfitTest, EffnEXP2)
167 double param[] = { 35, 0.5 };
168 test(effnEXP2, param, 3e-5, 0);
171 TEST_F(ExpfitTest, EffnEXPEXP)
173 double param[] = { 5, 0.5, 45 };
174 test(effnEXPEXP, param, 1e-2, 0);
177 TEST_F(ExpfitTest, EffnEXP5)
179 double param[] = { 0.5, 5, 0.5, 50, 0.002 };
180 test(effnEXP5, param, 1e-2, 2);
183 TEST_F(ExpfitTest, EffnEXP7)
185 double param[] = { 0.1, 2, 0.5, 30, 0.3, 50, -0.002 };
186 test(effnEXP7, param, 1e-2, 2);
189 TEST_F(ExpfitTest, EffnEXP9)
191 double param[] = { 0.4, 5, 0.2, 30, 0.1, 70, 0.2, 200, -0.05 };
192 test(effnEXP9, param, 4e-2, 2);
195 TEST_F(ExpfitTest, EffnERF)
197 double param[] = { 80, 120, 180, 5 };
198 test(effnERF, param, 1e-1, 3);
201 TEST_F(ExpfitTest, EffnERREST)
203 double param[] = { 1, 0.9, 100 };
204 test(effnERREST, param, 5e-3, 4);
207 TEST_F(ExpfitTest, EffnVAC)
209 double param[] = { 0.6, 0.1 };
210 test(effnVAC, param, 0.05, 0);
213 TEST_F(ExpfitTest, EffnPRES)
215 double param[] = { 0.6, 10, 7, 1, 0.25, 2 };
216 test(effnPRES, param, 1e-4, 1);