Tidy: modernize-use-nullptr
[gromacs.git] / src / gromacs / correlationfunctions / tests / expfit.cpp
blobeb93463fd9ced3b794b38bb0000d1f6969cc49c0
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2014,2015,2017, 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.
35 /*! \internal \file
36 * \brief
37 * Implements test of exponential fitting routines
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
43 #include "gmxpre.h"
45 #include "gromacs/correlationfunctions/expfit.h"
47 #include <cmath>
49 #include <gtest/gtest.h>
51 #include "gromacs/fileio/oenv.h"
52 #include "gromacs/fileio/xvgr.h"
53 #include "gromacs/utility/smalloc.h"
55 #include "testutils/refdata.h"
56 #include "testutils/testasserts.h"
57 #include "testutils/testfilemanager.h"
59 namespace gmx
62 namespace
65 class ExpfitData
67 public:
68 int nrLines_;
69 std::vector<real> x_, y_;
70 real startTime_, endTime_, dt_;
73 class ExpfitTest : public ::testing::Test
76 protected:
77 static std::vector<ExpfitData> data_;
78 test::TestReferenceData refData_;
79 test::TestReferenceChecker checker_;
80 ExpfitTest( )
81 : checker_(refData_.rootChecker())
85 // Static initiation, only run once every test.
86 static void SetUpTestCase()
88 double ** tempValues = nullptr;
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)
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++)
106 ed.x_.push_back((real)tempValues[0][j]);
107 ed.y_.push_back((real)tempValues[1][j]);
109 data_.push_back(ed);
111 // Free memory that was allocated in read_xvg
112 for (int j = 0; j < nrColumns; j++)
114 sfree(tempValues[j]);
115 tempValues[j] = nullptr;
117 sfree(tempValues);
118 tempValues = nullptr;
122 static void TearDownTestCase()
126 void test(int type, double result[], double tolerance,
127 unsigned int testType)
129 int nfitparm = effnNparams(type);
130 gmx_output_env_t *oenv;
132 if (testType >= data_.size())
134 GMX_THROW(InvalidInputError("testType out of range"));
136 output_env_init_default(&oenv);
137 do_lmfit(data_[testType].nrLines_,
138 &(data_[testType].y_[0]),
139 nullptr,
140 data_[testType].dt_,
141 &(data_[testType].x_[0]),
142 data_[testType].startTime_,
143 data_[testType].endTime_,
144 oenv, false, type, result, 0, nullptr);
145 output_env_done(oenv);
146 checker_.setDefaultTolerance(test::relativeToleranceAsFloatingPoint(1, tolerance));
147 checker_.checkSequenceArray(nfitparm, result, "result");
152 //static var
153 std::vector<ExpfitData> ExpfitTest::data_;
155 TEST_F (ExpfitTest, EffnEXP1) {
156 double param[] = {25};
157 test(effnEXP1, param, 1e-5, 0);
160 TEST_F (ExpfitTest, EffnEXP2) {
161 double param[] = {35, 0.5};
162 test(effnEXP2, param, 3e-5, 0);
165 TEST_F (ExpfitTest, EffnEXPEXP) {
166 double param[] = {5, 0.5, 45};
167 test(effnEXPEXP, param, 1e-2, 0);
170 TEST_F (ExpfitTest, EffnEXP5) {
171 double param[] = {0.5, 5, 0.5, 50, 0.002};
172 test(effnEXP5, param, 1e-2, 2);
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);
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);
185 TEST_F (ExpfitTest, EffnERF) {
186 double param[] = {80, 120, 180, 5};
187 test(effnERF, param, 1e-1, 3);
190 TEST_F (ExpfitTest, EffnERREST) {
191 double param[] = {1, 0.9, 100};
192 test(effnERREST, param, 5e-3, 4);
195 TEST_F (ExpfitTest, EffnVAC) {
196 double param[] = {0.6, 0.1};
197 test(effnVAC, param, 0.05, 0);
200 TEST_F (ExpfitTest, EffnPRES) {
201 double param[] = {0.6, 10, 7, 1, 0.25, 2};
202 test(effnPRES, param, 1e-4, 1);