[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 |
|
---|
| 94 | // create graph
|
---|
| 95 | {
|
---|
| 96 | // add nodes
|
---|
| 97 | nodes +=
|
---|
| 98 | std::make_pair(FragmentNode(0,1),1),
|
---|
| 99 | std::make_pair(FragmentNode(1,1),1);
|
---|
| 100 |
|
---|
| 101 | // add edges
|
---|
| 102 | edges +=
|
---|
| 103 | std::make_pair(FragmentEdge(0,1),1);
|
---|
| 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 +=
|
---|
| 127 | -78.5211,
|
---|
| 128 | -78.8049,
|
---|
| 129 | -78.935,
|
---|
| 130 | -78.9822,
|
---|
| 131 | -78.9867,
|
---|
| 132 | -78.971,
|
---|
| 133 | -78.9475,
|
---|
| 134 | -78.9225,
|
---|
| 135 | -78.899,
|
---|
| 136 | -78.8784;
|
---|
| 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) {
|
---|
| 159 | argument_t arg(argument_t::indices_t(0,1), argument_t::types_t(0,1), input[index]);
|
---|
[7e5b94] | 160 | const double result = compound( FunctionModel::arguments_t(1,arg) )[0];
|
---|
[154e88] | 161 | CPPUNIT_ASSERT(
|
---|
| 162 | Helpers::isEqual(
|
---|
| 163 | output[index],
|
---|
[7e5b94] | 164 | result,
|
---|
[154e88] | 165 | 1.e-4/std::numeric_limits<double>::epsilon() // only compare four digits
|
---|
| 166 | )
|
---|
| 167 | );
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | /** UnitTest for derivative()
|
---|
| 172 | */
|
---|
| 173 | //void CompoundPotentialTest::derivativeTest()
|
---|
| 174 | //{
|
---|
| 175 | // CompoundPotential compound(*graph);
|
---|
| 176 | // CompoundPotential::parameters_t params;
|
---|
[7e5b94] | 177 | // params += d,a,c,D;
|
---|
[154e88] | 178 | // compound.setParameters(params);
|
---|
| 179 | // argument_t arg(argument_t::indices_t(0,1), argument_t::types_t(0,1), c);
|
---|
[7e5b94] | 180 | // const double result = compound.derivative(FunctionModel::arguments_t(1,arg))[0]
|
---|
[154e88] | 181 | // CPPUNIT_ASSERT(
|
---|
| 182 | // Helpers::isEqual(
|
---|
| 183 | // 0.,
|
---|
[7e5b94] | 184 | // result,
|
---|
[154e88] | 185 | // 1.e+6
|
---|
| 186 | // )
|
---|
| 187 | // );
|
---|
| 188 | //}
|
---|
| 189 |
|
---|
| 190 |
|
---|
| 191 | /** UnitTest for parameter_derivative()
|
---|
| 192 | */
|
---|
| 193 | void CompoundPotentialTest::parameter_derivativeTest()
|
---|
| 194 | {
|
---|
| 195 | CompoundPotential compound(*graph);
|
---|
| 196 | CompoundPotential::parameters_t params;
|
---|
[7e5b94] | 197 | params += d,a,c,D;
|
---|
[154e88] | 198 | compound.setParameters(params);
|
---|
| 199 | argument_t arg(argument_t::indices_t(0,1), argument_t::types_t(0,1), c);
|
---|
[7e5b94] | 200 | {
|
---|
| 201 | const double result =
|
---|
| 202 | compound.parameter_derivative(
|
---|
| 203 | FunctionModel::arguments_t(1,arg),
|
---|
| 204 | 0)[0];
|
---|
| 205 | CPPUNIT_ASSERT(
|
---|
| 206 | Helpers::isEqual(
|
---|
| 207 | 1.,
|
---|
| 208 | result,
|
---|
| 209 | 1.e+6
|
---|
| 210 | )
|
---|
| 211 | );
|
---|
| 212 | }
|
---|
| 213 | {
|
---|
| 214 | const double result =
|
---|
| 215 | compound.parameter_derivative(
|
---|
| 216 | FunctionModel::arguments_t(1,arg),
|
---|
| 217 | 1)[0];
|
---|
| 218 | CPPUNIT_ASSERT(
|
---|
| 219 | Helpers::isEqual(
|
---|
| 220 | 0.,
|
---|
| 221 | result,
|
---|
| 222 | 1.e+6
|
---|
| 223 | )
|
---|
| 224 | );
|
---|
| 225 | }
|
---|
| 226 | {
|
---|
| 227 | const double result =
|
---|
| 228 | compound.parameter_derivative(
|
---|
| 229 | FunctionModel::arguments_t(1,arg),
|
---|
| 230 | 2)[0];
|
---|
| 231 | CPPUNIT_ASSERT(
|
---|
| 232 | Helpers::isEqual(
|
---|
| 233 | 0.,
|
---|
| 234 | result,
|
---|
| 235 | 1.e+6
|
---|
| 236 | )
|
---|
| 237 | );
|
---|
| 238 | }
|
---|
| 239 | {
|
---|
| 240 | const double result =
|
---|
| 241 | compound.parameter_derivative(
|
---|
| 242 | FunctionModel::arguments_t(1,arg),
|
---|
| 243 | 3)[0];
|
---|
| 244 | CPPUNIT_ASSERT(
|
---|
| 245 | Helpers::isEqual(
|
---|
| 246 | 0.,
|
---|
| 247 | result,
|
---|
| 248 | 1.e+6
|
---|
| 249 | )
|
---|
| 250 | );
|
---|
| 251 | }
|
---|
[154e88] | 252 | }
|
---|