| [154e88] | 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 | * CompoundPotentialUnitTest.cpp | 
|---|
|  | 25 | * | 
|---|
|  | 26 | *  Created on: May 08, 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 "CompoundPotentialUnitTest.hpp" | 
|---|
|  | 42 |  | 
|---|
|  | 43 | #include <boost/assign.hpp> | 
|---|
|  | 44 |  | 
|---|
|  | 45 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 46 |  | 
|---|
|  | 47 | #include "FunctionApproximation/FunctionArgument.hpp" | 
|---|
|  | 48 | #include "Potentials/CompoundPotential.hpp" | 
|---|
|  | 49 | #include "Potentials/helpers.hpp" | 
|---|
|  | 50 | #include "Potentials/PotentialFactory.hpp" | 
|---|
|  | 51 | #include "Potentials/PotentialRegistry.hpp" | 
|---|
| [7e5b94] | 52 | #include "Potentials/Specifics/ConstantPotential.hpp" | 
|---|
| [154e88] | 53 | #include "Potentials/Specifics/PairPotential_Morse.hpp" | 
|---|
|  | 54 |  | 
|---|
|  | 55 | using namespace boost::assign; | 
|---|
|  | 56 |  | 
|---|
|  | 57 | #ifdef HAVE_TESTRUNNER | 
|---|
|  | 58 | #include "UnitTestMain.hpp" | 
|---|
|  | 59 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
|  | 60 |  | 
|---|
|  | 61 | /********************************************** Test classes **************************************/ | 
|---|
|  | 62 |  | 
|---|
|  | 63 | // Registers the fixture into the 'registry' | 
|---|
|  | 64 | CPPUNIT_TEST_SUITE_REGISTRATION( CompoundPotentialTest ); | 
|---|
|  | 65 |  | 
|---|
|  | 66 |  | 
|---|
|  | 67 | void CompoundPotentialTest::setUp() | 
|---|
|  | 68 | { | 
|---|
|  | 69 | // failing asserts should be thrown | 
|---|
|  | 70 | ASSERT_DO(Assert::Throw); | 
|---|
|  | 71 |  | 
|---|
|  | 72 | // register MorsePotential with registry | 
|---|
|  | 73 | { | 
|---|
|  | 74 | PairPotential_Morse::ParticleTypes_t types = | 
|---|
|  | 75 | boost::assign::list_of<PairPotential_Morse::ParticleType_t> | 
|---|
|  | 76 | (0)(1) | 
|---|
|  | 77 | ; | 
|---|
|  | 78 | EmpiricalPotential *morse = | 
|---|
|  | 79 | PotentialFactory::getInstance().createInstance( | 
|---|
|  | 80 | std::string("morse"), types); | 
|---|
|  | 81 | PotentialRegistry::getInstance().registerInstance(morse); | 
|---|
|  | 82 | morse = NULL; | 
|---|
|  | 83 | } | 
|---|
| [7e5b94] | 84 | // register ConstantPotential with registry | 
|---|
|  | 85 | { | 
|---|
|  | 86 | ConstantPotential::ParticleTypes_t types; | 
|---|
|  | 87 | EmpiricalPotential *constant = | 
|---|
|  | 88 | PotentialFactory::getInstance().createInstance( | 
|---|
|  | 89 | std::string("constant"), types); | 
|---|
|  | 90 | PotentialRegistry::getInstance().registerInstance(constant); | 
|---|
|  | 91 | constant = NULL; | 
|---|
|  | 92 | } | 
|---|
| [154e88] | 93 |  | 
|---|
| [7c1091] | 94 | // create graph (i.e. this simulates a water molecule) | 
|---|
| [154e88] | 95 | { | 
|---|
|  | 96 | // add nodes | 
|---|
|  | 97 | nodes += | 
|---|
| [7c1091] | 98 | std::make_pair(FragmentNode(0,2),1), | 
|---|
|  | 99 | std::make_pair(FragmentNode(1,1),2); | 
|---|
| [154e88] | 100 |  | 
|---|
|  | 101 | // add edges | 
|---|
|  | 102 | edges += | 
|---|
| [7c1091] | 103 | std::make_pair(FragmentEdge(0,1),2); | 
|---|
| [154e88] | 104 |  | 
|---|
|  | 105 | // construct graph | 
|---|
|  | 106 | graph = new HomologyGraph(nodes, edges); | 
|---|
|  | 107 | } | 
|---|
|  | 108 |  | 
|---|
|  | 109 | // data is taken from gnuplot via set table "morse.dat" with | 
|---|
|  | 110 | // g(x)=D*(1- exp(-a*(x-c)))**2+d | 
|---|
|  | 111 | a = 0.897888; | 
|---|
|  | 112 | c = 2.92953; | 
|---|
|  | 113 | d = -78.9883; | 
|---|
|  | 114 | D = 0.196289; | 
|---|
|  | 115 | input += | 
|---|
|  | 116 | 1.89012, | 
|---|
|  | 117 | 2.17632, | 
|---|
|  | 118 | 2.46253, | 
|---|
|  | 119 | 2.74873, | 
|---|
|  | 120 | 3.03493, | 
|---|
|  | 121 | 3.32114, | 
|---|
|  | 122 | 3.60734, | 
|---|
|  | 123 | 3.89354, | 
|---|
|  | 124 | 4.17974, | 
|---|
|  | 125 | 4.46595; | 
|---|
|  | 126 | output += | 
|---|
| [7c1091] | 127 | 2.*0.467226+d, | 
|---|
|  | 128 | 2.*0.183388+d, | 
|---|
|  | 129 | 2.*0.0532649+d, | 
|---|
|  | 130 | 2.*0.00609808+d, | 
|---|
|  | 131 | 2.*0.00160056+d, | 
|---|
|  | 132 | 2.*0.0172506+d, | 
|---|
|  | 133 | 2.*0.0407952+d, | 
|---|
|  | 134 | 2.*0.0658475+d, | 
|---|
|  | 135 | 2.*0.0893157+d, | 
|---|
|  | 136 | 2.*0.109914+d; | 
|---|
| [154e88] | 137 |  | 
|---|
|  | 138 | CPPUNIT_ASSERT_EQUAL( input.size(), output.size() ); | 
|---|
|  | 139 | } | 
|---|
|  | 140 |  | 
|---|
|  | 141 |  | 
|---|
|  | 142 | void CompoundPotentialTest::tearDown() | 
|---|
|  | 143 | { | 
|---|
|  | 144 | delete graph; | 
|---|
|  | 145 | PotentialFactory::purgeInstance(); | 
|---|
|  | 146 | PotentialRegistry::getInstance().cleanup(); | 
|---|
|  | 147 | PotentialRegistry::purgeInstance(); | 
|---|
|  | 148 | } | 
|---|
|  | 149 |  | 
|---|
|  | 150 | /** UnitTest for operator() | 
|---|
|  | 151 | */ | 
|---|
|  | 152 | void CompoundPotentialTest::operatorTest() | 
|---|
|  | 153 | { | 
|---|
|  | 154 | CompoundPotential compound(*graph); | 
|---|
|  | 155 | CompoundPotential::parameters_t params; | 
|---|
| [7e5b94] | 156 | params += d,a,c,D; | 
|---|
| [154e88] | 157 | compound.setParameters(params); | 
|---|
|  | 158 | for (size_t index = 0; index < input.size(); ++index) { | 
|---|
| [7c1091] | 159 | argument_t firstarg(argument_t::indices_t(0,1), argument_t::types_t(0,1), input[index]); | 
|---|
|  | 160 | argument_t secondarg(argument_t::indices_t(0,2), argument_t::types_t(0,1), input[index]); | 
|---|
|  | 161 | FunctionModel::arguments_t args; | 
|---|
|  | 162 | args += firstarg,secondarg; | 
|---|
|  | 163 | const double result = compound( args )[0]; | 
|---|
| [154e88] | 164 | CPPUNIT_ASSERT( | 
|---|
|  | 165 | Helpers::isEqual( | 
|---|
|  | 166 | output[index], | 
|---|
| [7e5b94] | 167 | result, | 
|---|
| [154e88] | 168 | 1.e-4/std::numeric_limits<double>::epsilon() // only compare four digits | 
|---|
|  | 169 | ) | 
|---|
|  | 170 | ); | 
|---|
|  | 171 | } | 
|---|
|  | 172 | } | 
|---|
|  | 173 |  | 
|---|
|  | 174 | /** UnitTest for derivative() | 
|---|
|  | 175 | */ | 
|---|
|  | 176 | //void CompoundPotentialTest::derivativeTest() | 
|---|
|  | 177 | //{ | 
|---|
|  | 178 | //  CompoundPotential compound(*graph); | 
|---|
|  | 179 | //  CompoundPotential::parameters_t params; | 
|---|
| [7e5b94] | 180 | //  params += d,a,c,D; | 
|---|
| [154e88] | 181 | //  compound.setParameters(params); | 
|---|
| [7c1091] | 182 | //  argument_t firstarg(argument_t::indices_t(0,1), argument_t::types_t(0,1), input[index]); | 
|---|
|  | 183 | //  argument_t secondarg(argument_t::indices_t(0,2), argument_t::types_t(0,1), input[index]); | 
|---|
|  | 184 | //  FunctionModel::arguments_t args; | 
|---|
|  | 185 | //  args += firstarg,secondarg; | 
|---|
|  | 186 | //  const double result = compound.derivative( args )[0] | 
|---|
| [154e88] | 187 | //  CPPUNIT_ASSERT( | 
|---|
|  | 188 | //      Helpers::isEqual( | 
|---|
|  | 189 | //          0., | 
|---|
| [7e5b94] | 190 | //          result, | 
|---|
| [154e88] | 191 | //          1.e+6 | 
|---|
|  | 192 | //      ) | 
|---|
|  | 193 | //  ); | 
|---|
|  | 194 | //} | 
|---|
|  | 195 |  | 
|---|
|  | 196 |  | 
|---|
|  | 197 | /** UnitTest for parameter_derivative() | 
|---|
|  | 198 | */ | 
|---|
|  | 199 | void CompoundPotentialTest::parameter_derivativeTest() | 
|---|
|  | 200 | { | 
|---|
|  | 201 | CompoundPotential compound(*graph); | 
|---|
|  | 202 | CompoundPotential::parameters_t params; | 
|---|
| [7e5b94] | 203 | params += d,a,c,D; | 
|---|
| [154e88] | 204 | compound.setParameters(params); | 
|---|
| [7c1091] | 205 | argument_t firstarg(argument_t::indices_t(0,1), argument_t::types_t(0,1), c); | 
|---|
|  | 206 | argument_t secondarg(argument_t::indices_t(0,2), argument_t::types_t(0,1), c); | 
|---|
|  | 207 | FunctionModel::arguments_t args; | 
|---|
|  | 208 | args += firstarg,secondarg; | 
|---|
| [7e5b94] | 209 | { | 
|---|
|  | 210 | const double result = | 
|---|
|  | 211 | compound.parameter_derivative( | 
|---|
| [7c1091] | 212 | args, | 
|---|
| [7e5b94] | 213 | 0)[0]; | 
|---|
|  | 214 | CPPUNIT_ASSERT( | 
|---|
|  | 215 | Helpers::isEqual( | 
|---|
|  | 216 | 1., | 
|---|
|  | 217 | result, | 
|---|
|  | 218 | 1.e+6 | 
|---|
|  | 219 | ) | 
|---|
|  | 220 | ); | 
|---|
|  | 221 | } | 
|---|
|  | 222 | { | 
|---|
|  | 223 | const double result = | 
|---|
|  | 224 | compound.parameter_derivative( | 
|---|
| [7c1091] | 225 | args, | 
|---|
| [7e5b94] | 226 | 1)[0]; | 
|---|
|  | 227 | CPPUNIT_ASSERT( | 
|---|
|  | 228 | Helpers::isEqual( | 
|---|
|  | 229 | 0., | 
|---|
|  | 230 | result, | 
|---|
|  | 231 | 1.e+6 | 
|---|
|  | 232 | ) | 
|---|
|  | 233 | ); | 
|---|
|  | 234 | } | 
|---|
|  | 235 | { | 
|---|
|  | 236 | const double result = | 
|---|
|  | 237 | compound.parameter_derivative( | 
|---|
| [7c1091] | 238 | args, | 
|---|
| [7e5b94] | 239 | 2)[0]; | 
|---|
|  | 240 | CPPUNIT_ASSERT( | 
|---|
|  | 241 | Helpers::isEqual( | 
|---|
|  | 242 | 0., | 
|---|
|  | 243 | result, | 
|---|
|  | 244 | 1.e+6 | 
|---|
|  | 245 | ) | 
|---|
|  | 246 | ); | 
|---|
|  | 247 | } | 
|---|
|  | 248 | { | 
|---|
|  | 249 | const double result = | 
|---|
|  | 250 | compound.parameter_derivative( | 
|---|
| [7c1091] | 251 | args, | 
|---|
| [7e5b94] | 252 | 3)[0]; | 
|---|
|  | 253 | CPPUNIT_ASSERT( | 
|---|
|  | 254 | Helpers::isEqual( | 
|---|
|  | 255 | 0., | 
|---|
|  | 256 | result, | 
|---|
|  | 257 | 1.e+6 | 
|---|
|  | 258 | ) | 
|---|
|  | 259 | ); | 
|---|
|  | 260 | } | 
|---|
| [154e88] | 261 | } | 
|---|