| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
| 5 | * | 
|---|
| 6 | * | 
|---|
| 7 | *   This file is part of MoleCuilder. | 
|---|
| 8 | * | 
|---|
| 9 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 10 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 11 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 12 | *    (at your option) any later version. | 
|---|
| 13 | * | 
|---|
| 14 | *    MoleCuilder 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 | 
|---|
| 17 | *    GNU General Public License for more details. | 
|---|
| 18 | * | 
|---|
| 19 | *    You should have received a copy of the GNU General Public License | 
|---|
| 20 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | /* | 
|---|
| 24 | * PairPotential_LennardJonesUnitTest.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Jul 05, 2013 | 
|---|
| 27 | *      Author: heber | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // include config.h | 
|---|
| 31 | #ifdef HAVE_CONFIG_H | 
|---|
| 32 | #include <config.h> | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | using namespace std; | 
|---|
| 36 |  | 
|---|
| 37 | #include <cppunit/CompilerOutputter.h> | 
|---|
| 38 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
| 39 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
| 40 |  | 
|---|
| 41 | #include "PairPotential_LennardJonesUnitTest.hpp" | 
|---|
| 42 |  | 
|---|
| 43 | #include <boost/assign.hpp> | 
|---|
| 44 |  | 
|---|
| 45 | #include "CodePatterns/Assert.hpp" | 
|---|
| 46 |  | 
|---|
| 47 | #include "FunctionApproximation/FunctionArgument.hpp" | 
|---|
| 48 | #include "Potentials/Specifics/PairPotential_LennardJones.hpp" | 
|---|
| 49 | #include "Potentials/helpers.hpp" | 
|---|
| 50 |  | 
|---|
| 51 | using namespace boost::assign; | 
|---|
| 52 |  | 
|---|
| 53 | #ifdef HAVE_TESTRUNNER | 
|---|
| 54 | #include "UnitTestMain.hpp" | 
|---|
| 55 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 56 |  | 
|---|
| 57 | /********************************************** Test classes **************************************/ | 
|---|
| 58 |  | 
|---|
| 59 | // Registers the fixture into the 'registry' | 
|---|
| 60 | CPPUNIT_TEST_SUITE_REGISTRATION( PairPotential_LennardJonesTest ); | 
|---|
| 61 |  | 
|---|
| 62 |  | 
|---|
| 63 | void PairPotential_LennardJonesTest::setUp() | 
|---|
| 64 | { | 
|---|
| 65 | // failing asserts should be thrown | 
|---|
| 66 | ASSERT_DO(Assert::Throw); | 
|---|
| 67 |  | 
|---|
| 68 | // data is taken from gnuplot via | 
|---|
| 69 | // g(x)=4*epsilon*((sigma/x)**12 - (sigma/x)**6) | 
|---|
| 70 | // set table "lj.dat" | 
|---|
| 71 | // set samples 10 | 
|---|
| 72 | // plot [1.89012:4.46595] g(x) | 
|---|
| 73 | epsilon = 0.897888; | 
|---|
| 74 | sigma = 1.92953; | 
|---|
| 75 | input += | 
|---|
| 76 | 1.89012, | 
|---|
| 77 | 2.17632, | 
|---|
| 78 | 2.46253, | 
|---|
| 79 | 2.74873, | 
|---|
| 80 | 3.03493, | 
|---|
| 81 | 3.32114, | 
|---|
| 82 | 3.60734, | 
|---|
| 83 | 3.89354, | 
|---|
| 84 | 4.17975, | 
|---|
| 85 | 4.46595; | 
|---|
| 86 | output += | 
|---|
| 87 | 0.535795, | 
|---|
| 88 | -0.897154, | 
|---|
| 89 | -0.638834, | 
|---|
| 90 | -0.378315, | 
|---|
| 91 | -0.221526, | 
|---|
| 92 | -0.132813, | 
|---|
| 93 | -0.0821442, | 
|---|
| 94 | -0.0524131, | 
|---|
| 95 | -0.0344245, | 
|---|
| 96 | -0.0232099; | 
|---|
| 97 |  | 
|---|
| 98 | CPPUNIT_ASSERT_EQUAL( input.size(), output.size() ); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 |  | 
|---|
| 102 | void PairPotential_LennardJonesTest::tearDown() | 
|---|
| 103 | { | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | /** UnitTest for operator() | 
|---|
| 107 | */ | 
|---|
| 108 | void PairPotential_LennardJonesTest::operatorTest() | 
|---|
| 109 | { | 
|---|
| 110 | PairPotential_LennardJones::ParticleTypes_t types = | 
|---|
| 111 | boost::assign::list_of<PairPotential_LennardJones::ParticleType_t> | 
|---|
| 112 | (0)(1) | 
|---|
| 113 | ; | 
|---|
| 114 | PairPotential_LennardJones lj(types, epsilon, sigma); | 
|---|
| 115 | for (size_t index = 0; index < input.size(); ++index) { | 
|---|
| 116 | argument_t arg(argument_t::indices_t(0,1), argument_t::types_t(0,1), input[index]); | 
|---|
| 117 | CPPUNIT_ASSERT( | 
|---|
| 118 | Helpers::isEqual( | 
|---|
| 119 | output[index], | 
|---|
| 120 | lj( FunctionModel::list_of_arguments_t(1, FunctionModel::arguments_t(1,arg)) )[0], | 
|---|
| 121 | 1.e-4/std::numeric_limits<double>::epsilon() // only compare four digits | 
|---|
| 122 | ) | 
|---|
| 123 | ); | 
|---|
| 124 | } | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | /** UnitTest for derivative() | 
|---|
| 128 | */ | 
|---|
| 129 | void PairPotential_LennardJonesTest::derivativeTest() | 
|---|
| 130 | { | 
|---|
| 131 | PairPotential_LennardJones::ParticleTypes_t types = | 
|---|
| 132 | boost::assign::list_of<PairPotential_LennardJones::ParticleType_t> | 
|---|
| 133 | (0)(1) | 
|---|
| 134 | ; | 
|---|
| 135 | PairPotential_LennardJones lj(types, epsilon, sigma); | 
|---|
| 136 | argument_t arg(argument_t::indices_t(0,1), argument_t::types_t(0,1), pow(2,1./6.)*sigma); | 
|---|
| 137 | // check for zero derivative at minimum | 
|---|
| 138 | CPPUNIT_ASSERT( | 
|---|
| 139 | Helpers::isEqual( | 
|---|
| 140 | 0., | 
|---|
| 141 | lj.derivative( | 
|---|
| 142 | FunctionModel::list_of_arguments_t(1, FunctionModel::arguments_t(1,arg)) | 
|---|
| 143 | )[0], | 
|---|
| 144 | 1.e+6 | 
|---|
| 145 | ) | 
|---|
| 146 | ); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 |  | 
|---|
| 150 | /** UnitTest for parameter_derivative() | 
|---|
| 151 | */ | 
|---|
| 152 | void PairPotential_LennardJonesTest::parameter_derivativeTest() | 
|---|
| 153 | { | 
|---|
| 154 | PairPotential_LennardJones::ParticleTypes_t types = | 
|---|
| 155 | boost::assign::list_of<PairPotential_LennardJones::ParticleType_t> | 
|---|
| 156 | (0)(1) | 
|---|
| 157 | ; | 
|---|
| 158 | PairPotential_LennardJones lj(types, epsilon, sigma); | 
|---|
| 159 | argument_t arg(argument_t::indices_t(0,1), argument_t::types_t(0,1), pow(2,1./6.)*sigma); | 
|---|
| 160 | CPPUNIT_ASSERT( | 
|---|
| 161 | Helpers::isEqual( | 
|---|
| 162 | -1., | 
|---|
| 163 | lj.parameter_derivative( | 
|---|
| 164 | FunctionModel::list_of_arguments_t(1, FunctionModel::arguments_t(1,arg)), | 
|---|
| 165 | 0 | 
|---|
| 166 | )[0], | 
|---|
| 167 | 1.e+6 | 
|---|
| 168 | ) | 
|---|
| 169 | ); | 
|---|
| 170 | CPPUNIT_ASSERT( | 
|---|
| 171 | Helpers::isEqual( | 
|---|
| 172 | 0., | 
|---|
| 173 | lj.parameter_derivative( | 
|---|
| 174 | FunctionModel::list_of_arguments_t(1, FunctionModel::arguments_t(1,arg)), | 
|---|
| 175 | 1 | 
|---|
| 176 | )[0], | 
|---|
| 177 | 1.e+6 | 
|---|
| 178 | ) | 
|---|
| 179 | ); | 
|---|
| 180 | } | 
|---|