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"
|
---|
52 | #include "Potentials/Specifics/PairPotential_Morse.hpp"
|
---|
53 |
|
---|
54 | using namespace boost::assign;
|
---|
55 |
|
---|
56 | #ifdef HAVE_TESTRUNNER
|
---|
57 | #include "UnitTestMain.hpp"
|
---|
58 | #endif /*HAVE_TESTRUNNER*/
|
---|
59 |
|
---|
60 | /********************************************** Test classes **************************************/
|
---|
61 |
|
---|
62 | // Registers the fixture into the 'registry'
|
---|
63 | CPPUNIT_TEST_SUITE_REGISTRATION( CompoundPotentialTest );
|
---|
64 |
|
---|
65 |
|
---|
66 | void CompoundPotentialTest::setUp()
|
---|
67 | {
|
---|
68 | // failing asserts should be thrown
|
---|
69 | ASSERT_DO(Assert::Throw);
|
---|
70 |
|
---|
71 | // register MorsePotential with registry
|
---|
72 | {
|
---|
73 | PairPotential_Morse::ParticleTypes_t types =
|
---|
74 | boost::assign::list_of<PairPotential_Morse::ParticleType_t>
|
---|
75 | (0)(1)
|
---|
76 | ;
|
---|
77 | EmpiricalPotential *morse =
|
---|
78 | PotentialFactory::getInstance().createInstance(
|
---|
79 | std::string("morse"), types);
|
---|
80 | PotentialRegistry::getInstance().registerInstance(morse);
|
---|
81 | morse = NULL;
|
---|
82 | }
|
---|
83 |
|
---|
84 | // create graph
|
---|
85 | {
|
---|
86 | // add nodes
|
---|
87 | nodes +=
|
---|
88 | std::make_pair(FragmentNode(0,1),1),
|
---|
89 | std::make_pair(FragmentNode(1,1),1);
|
---|
90 |
|
---|
91 | // add edges
|
---|
92 | edges +=
|
---|
93 | std::make_pair(FragmentEdge(0,1),1);
|
---|
94 |
|
---|
95 | // construct graph
|
---|
96 | graph = new HomologyGraph(nodes, edges);
|
---|
97 | }
|
---|
98 |
|
---|
99 | // data is taken from gnuplot via set table "morse.dat" with
|
---|
100 | // g(x)=D*(1- exp(-a*(x-c)))**2+d
|
---|
101 | a = 0.897888;
|
---|
102 | c = 2.92953;
|
---|
103 | d = -78.9883;
|
---|
104 | D = 0.196289;
|
---|
105 | input +=
|
---|
106 | 1.89012,
|
---|
107 | 2.17632,
|
---|
108 | 2.46253,
|
---|
109 | 2.74873,
|
---|
110 | 3.03493,
|
---|
111 | 3.32114,
|
---|
112 | 3.60734,
|
---|
113 | 3.89354,
|
---|
114 | 4.17974,
|
---|
115 | 4.46595;
|
---|
116 | output +=
|
---|
117 | -78.5211,
|
---|
118 | -78.8049,
|
---|
119 | -78.935,
|
---|
120 | -78.9822,
|
---|
121 | -78.9867,
|
---|
122 | -78.971,
|
---|
123 | -78.9475,
|
---|
124 | -78.9225,
|
---|
125 | -78.899,
|
---|
126 | -78.8784;
|
---|
127 |
|
---|
128 | CPPUNIT_ASSERT_EQUAL( input.size(), output.size() );
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | void CompoundPotentialTest::tearDown()
|
---|
133 | {
|
---|
134 | delete graph;
|
---|
135 | PotentialFactory::purgeInstance();
|
---|
136 | PotentialRegistry::getInstance().cleanup();
|
---|
137 | PotentialRegistry::purgeInstance();
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** UnitTest for operator()
|
---|
141 | */
|
---|
142 | void CompoundPotentialTest::operatorTest()
|
---|
143 | {
|
---|
144 | CompoundPotential compound(*graph);
|
---|
145 | CompoundPotential::parameters_t params;
|
---|
146 | params += a,c,D,d;
|
---|
147 | compound.setParameters(params);
|
---|
148 | for (size_t index = 0; index < input.size(); ++index) {
|
---|
149 | argument_t arg(argument_t::indices_t(0,1), argument_t::types_t(0,1), input[index]);
|
---|
150 | CPPUNIT_ASSERT(
|
---|
151 | Helpers::isEqual(
|
---|
152 | output[index],
|
---|
153 | compound( FunctionModel::arguments_t(1,arg) )[0],
|
---|
154 | 1.e-4/std::numeric_limits<double>::epsilon() // only compare four digits
|
---|
155 | )
|
---|
156 | );
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | /** UnitTest for derivative()
|
---|
161 | */
|
---|
162 | //void CompoundPotentialTest::derivativeTest()
|
---|
163 | //{
|
---|
164 | // CompoundPotential compound(*graph);
|
---|
165 | // CompoundPotential::parameters_t params;
|
---|
166 | // params += a,c,D,d;
|
---|
167 | // compound.setParameters(params);
|
---|
168 | // argument_t arg(argument_t::indices_t(0,1), argument_t::types_t(0,1), c);
|
---|
169 | // CPPUNIT_ASSERT(
|
---|
170 | // Helpers::isEqual(
|
---|
171 | // 0.,
|
---|
172 | // compound.derivative(
|
---|
173 | // FunctionModel::arguments_t(1,arg)
|
---|
174 | // )[0],
|
---|
175 | // 1.e+6
|
---|
176 | // )
|
---|
177 | // );
|
---|
178 | //}
|
---|
179 |
|
---|
180 |
|
---|
181 | /** UnitTest for parameter_derivative()
|
---|
182 | */
|
---|
183 | void CompoundPotentialTest::parameter_derivativeTest()
|
---|
184 | {
|
---|
185 | CompoundPotential compound(*graph);
|
---|
186 | CompoundPotential::parameters_t params;
|
---|
187 | params += a,c,D,d;
|
---|
188 | compound.setParameters(params);
|
---|
189 | argument_t arg(argument_t::indices_t(0,1), argument_t::types_t(0,1), c);
|
---|
190 | CPPUNIT_ASSERT(
|
---|
191 | Helpers::isEqual(
|
---|
192 | 0.,
|
---|
193 | compound.parameter_derivative(
|
---|
194 | FunctionModel::arguments_t(1,arg),
|
---|
195 | 0
|
---|
196 | )[0],
|
---|
197 | 1.e+6
|
---|
198 | )
|
---|
199 | );
|
---|
200 | CPPUNIT_ASSERT(
|
---|
201 | Helpers::isEqual(
|
---|
202 | 0.,
|
---|
203 | compound.parameter_derivative(
|
---|
204 | FunctionModel::arguments_t(1,arg),
|
---|
205 | 1
|
---|
206 | )[0],
|
---|
207 | 1.e+6
|
---|
208 | )
|
---|
209 | );
|
---|
210 | CPPUNIT_ASSERT(
|
---|
211 | Helpers::isEqual(
|
---|
212 | 0.,
|
---|
213 | compound.parameter_derivative(
|
---|
214 | FunctionModel::arguments_t(1,arg),
|
---|
215 | 2
|
---|
216 | )[0],
|
---|
217 | 1.e+6
|
---|
218 | )
|
---|
219 | );
|
---|
220 | CPPUNIT_ASSERT(
|
---|
221 | Helpers::isEqual(
|
---|
222 | 1.,
|
---|
223 | compound.parameter_derivative(
|
---|
224 | FunctionModel::arguments_t(1,arg),
|
---|
225 | 3
|
---|
226 | )[0],
|
---|
227 | 1.e+6
|
---|
228 | )
|
---|
229 | );
|
---|
230 | }
|
---|