source: LinearAlgebra/src/unittests/VectorUnitTest.cpp@ 94d5ac6

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 94d5ac6 was 94d5ac6, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: As we use GSL internally, we are as of now required to use GPL v2 license.

  • GNU Scientific Library is used at every place in the code, especially the sub-package LinearAlgebra is based on it which in turn is used really everywhere in the remainder of MoleCuilder. Hence, we have to use the GPL license for the whole of MoleCuilder. In effect, GPL's COPYING was present all along and stated the terms of the GPL v2 license.
  • Hence, I added the default GPL v2 disclaimer to every source file and removed the note about a (actually missing) LICENSE file.
  • also, I added a help-redistribute action which again gives the disclaimer of the GPL v2.
  • also, I changed in the disclaimer that is printed at every program start in builder_init.cpp.
  • TEST: Added check on GPL statement present in every module to test CodeChecks project-disclaimer.
  • Property mode set to 100644
File size: 9.4 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. 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 * VectorUnitTest.cpp
25 *
26 * Created on: Aug 17, 2009
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35using namespace std;
36
37#include <cppunit/CompilerOutputter.h>
38#include <cppunit/extensions/TestFactoryRegistry.h>
39#include <cppunit/ui/text/TestRunner.h>
40
41#include <limits>
42
43#include "CodePatterns/Log.hpp"
44#include "defs.hpp"
45#include "Plane.hpp"
46#include "RealSpaceMatrix.hpp"
47#include "Vector.hpp"
48#include "vector_ops.hpp"
49
50#include "VectorUnitTest.hpp"
51
52#ifdef HAVE_TESTRUNNER
53#include "UnitTestMain.hpp"
54#endif /*HAVE_TESTRUNNER*/
55
56#include <iostream>
57
58/********************************************** Test classes **************************************/
59
60// Registers the fixture into the 'registry'
61CPPUNIT_TEST_SUITE_REGISTRATION( VectorTest );
62
63
64void VectorTest::setUp()
65{
66 zero = Vector(0.,0.,0.);
67 unit = Vector(1.,0.,0.);
68 otherunit = Vector(0.,1.,0.);
69 notunit = Vector(0.,1.,1.);
70 two = Vector(2.,1.,0.);
71 three = Vector(1,2,3);
72};
73
74
75void VectorTest::tearDown()
76{
77 logger::purgeInstance();
78 errorLogger::purgeInstance();
79};
80
81/** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne().
82 */
83void VectorTest::AssignmentTest()
84{
85 // test with zero
86 zero.at(0) = 0;
87 zero.at(1) = 0;
88 zero.at(2) = 0;
89 double zero_array[3] = {0., 0., 0.};
90
91 CPPUNIT_ASSERT_EQUAL( zero, Vector(0,0,0));
92 CPPUNIT_ASSERT_EQUAL( zero, Vector(0.,0.,0.));
93 CPPUNIT_ASSERT_EQUAL( zero, Vector(zero_array[0], zero_array[1], zero_array[2]));
94 CPPUNIT_ASSERT_EQUAL( zero, Vector(zero_array));
95
96 // test with unit
97 unit.at(0) = 1;
98 unit.at(1) = 0;
99 unit.at(2) = 0;
100 double unit_array[3] = {1., 0., 0.};
101
102 CPPUNIT_ASSERT_EQUAL( unit, Vector(1,0,0));
103 CPPUNIT_ASSERT_EQUAL( unit, Vector(1.,0.,0.));
104 CPPUNIT_ASSERT_EQUAL( unit, Vector(unit_array[0], unit_array[1], unit_array[2]));
105 CPPUNIT_ASSERT_EQUAL( unit, Vector(unit_array));
106
107 // test with two
108 two.at(0) = 2;
109 two.at(1) = 1;
110 two.at(2) = 0;
111 double two_array[3] = {2., 1., 0.};
112
113 CPPUNIT_ASSERT_EQUAL( two, Vector(2,1,0));
114 CPPUNIT_ASSERT_EQUAL( two, Vector(2.,1.,0.));
115 CPPUNIT_ASSERT_EQUAL( two, Vector(two_array[0], two_array[1], two_array[2]));
116 CPPUNIT_ASSERT_EQUAL( two, Vector(two_array));
117
118 // test with three
119 three.at(0) = 1;
120 three.at(1) = 2;
121 three.at(2) = 3;
122 double three_array[3] = {1., 2., 3.};
123
124 CPPUNIT_ASSERT_EQUAL( three, Vector(1,2,3));
125 CPPUNIT_ASSERT_EQUAL( three, Vector(1.,2.,3.));
126 CPPUNIT_ASSERT_EQUAL( three, Vector(three_array[0], three_array[1], three_array[2]));
127 CPPUNIT_ASSERT_EQUAL( three, Vector(three_array));
128}
129
130/** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne().
131 */
132void VectorTest::UnityTest()
133{
134 // unity and zero tests
135 CPPUNIT_ASSERT_EQUAL( true, zero.IsZero() );
136 CPPUNIT_ASSERT_EQUAL( false, zero.IsOne() );
137 CPPUNIT_ASSERT_EQUAL( false, unit.IsZero() );
138 CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
139 CPPUNIT_ASSERT_EQUAL( false, notunit.IsOne() );
140 CPPUNIT_ASSERT_EQUAL( true, otherunit.IsOne() );
141 CPPUNIT_ASSERT_EQUAL( false, otherunit.IsZero() );
142};
143
144/** UnitTest for Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale()/
145 */
146void VectorTest::SimpleAlgebraTest()
147{
148 double factor;
149 // copy vector
150 fixture = Vector(2.,3.,4.);
151 CPPUNIT_ASSERT_EQUAL( Vector(2.,3.,4.), fixture );
152 // summation and scaling
153 fixture = zero + unit;
154 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
155 fixture = zero - unit;
156 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
157 CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
158 fixture = zero + zero;
159 CPPUNIT_ASSERT_EQUAL( true, fixture.IsZero() );
160 fixture = notunit - otherunit;
161 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
162 fixture = unit - otherunit;
163 CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
164 fixture = notunit - unit - otherunit;
165 CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
166 fixture = 0.98 * unit;
167 CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
168 fixture = 1. * unit;
169 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
170 factor = 0.98;
171 fixture = factor * unit;
172 CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
173 factor = 1.;
174 fixture = factor * unit;
175 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
176};
177
178
179/** UnitTest for operator versions of Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale().
180 */
181void VectorTest::OperatorAlgebraTest()
182{
183 // summation and scaling
184 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
185 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
186 CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
187 CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
188 CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
189 CPPUNIT_ASSERT_EQUAL( true, (notunit-otherunit).IsOne() );
190 CPPUNIT_ASSERT_EQUAL( false, (unit+otherunit).IsOne() );
191 CPPUNIT_ASSERT_EQUAL( false, (notunit-unit-otherunit).IsZero() );
192 CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
193 CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
194
195 CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) );
196 CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,1.), (notunit-otherunit) );
197 CPPUNIT_ASSERT_EQUAL( Vector(-1, 0., 1.), (notunit-unit-otherunit) );
198};
199
200/** UnitTest for scalar products.
201 */
202void VectorTest::EuclidianScalarProductTest()
203{
204 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(zero) );
205 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(unit) );
206 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(otherunit) );
207 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(notunit) );
208 CPPUNIT_ASSERT_EQUAL( 1., unit.ScalarProduct(unit) );
209 CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(unit) );
210 CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(unit) );
211 CPPUNIT_ASSERT_EQUAL( 1., otherunit.ScalarProduct(notunit) );
212 CPPUNIT_ASSERT_EQUAL( 2., two.ScalarProduct(unit) );
213 CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(otherunit) );
214 CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(notunit) );
215}
216
217/** UnitTest for norms.
218 */
219void VectorTest::EuclidianNormTest()
220{
221 CPPUNIT_ASSERT_EQUAL( 0., zero.Norm() );
222 CPPUNIT_ASSERT_EQUAL( 0., zero.NormSquared() );
223 CPPUNIT_ASSERT_EQUAL( 1., unit.Norm() );
224 CPPUNIT_ASSERT_EQUAL( 1., unit.NormSquared() );
225 CPPUNIT_ASSERT_EQUAL( 1., otherunit.Norm() );
226 CPPUNIT_ASSERT_EQUAL( 1., otherunit.NormSquared() );
227 CPPUNIT_ASSERT_EQUAL( 2., notunit.NormSquared() );
228 CPPUNIT_ASSERT_EQUAL( sqrt(2.), notunit.Norm() );
229}
230
231/** UnitTest for distances.
232 */
233void VectorTest::EuclidianDistancesTest()
234{
235 CPPUNIT_ASSERT_EQUAL( 1., zero.distance(unit) );
236 CPPUNIT_ASSERT_EQUAL( sqrt(2.), otherunit.distance(unit) );
237 CPPUNIT_ASSERT_EQUAL( sqrt(2.), zero.distance(notunit) );
238 CPPUNIT_ASSERT_EQUAL( 1., otherunit.distance(notunit) );
239 CPPUNIT_ASSERT_EQUAL( sqrt(5.), two.distance(notunit) );
240
241 // check operator
242 CPPUNIT_ASSERT( !(zero < zero) );
243 CPPUNIT_ASSERT( !(unit < otherunit) );
244 CPPUNIT_ASSERT( zero < unit );
245 CPPUNIT_ASSERT( unit < two );
246}
247
248/** UnitTest for angles.
249 */
250void VectorTest::EuclidianAnglesTest()
251{
252 CPPUNIT_ASSERT_EQUAL( M_PI, zero.Angle(unit) );
253 CPPUNIT_ASSERT_EQUAL( 0., unit.Angle(unit) );
254 CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - otherunit.Angle(unit)) <= LINALG_MYEPSILON() );
255 CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - unit.Angle(notunit)) <= LINALG_MYEPSILON() );
256 CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/4. - otherunit.Angle(notunit)) <= LINALG_MYEPSILON() );
257};
258
259/** UnitTest for projections.
260 */
261void VectorTest::ProjectionTest()
262{
263 CPPUNIT_ASSERT_EQUAL( zero, zero.Projection(unit) );
264 CPPUNIT_ASSERT_EQUAL( zero, otherunit.Projection(unit) );
265 CPPUNIT_ASSERT_EQUAL( Vector(0.4,0.2,0.), otherunit.Projection(two) );
266 CPPUNIT_ASSERT_EQUAL( Vector(0.,1.,0.), two.Projection(otherunit) );
267};
268
269/**
270 * Unittest for operation with normals
271 */
272void VectorTest::NormalsTest(){
273 Vector testVector;
274 // the zero Vector should produce an error
275 CPPUNIT_ASSERT(!testVector.GetOneNormalVector(zero));
276
277 // first one-component system
278 CPPUNIT_ASSERT(testVector.GetOneNormalVector(unit));
279 CPPUNIT_ASSERT(testVector.ScalarProduct(unit) <= LINALG_MYEPSILON());
280
281 // second one-component system
282 CPPUNIT_ASSERT(testVector.GetOneNormalVector(otherunit));
283 CPPUNIT_ASSERT(testVector.ScalarProduct(otherunit) <= LINALG_MYEPSILON());
284
285 // first two-component system
286 CPPUNIT_ASSERT(testVector.GetOneNormalVector(notunit));
287 CPPUNIT_ASSERT(testVector.ScalarProduct(notunit) <= LINALG_MYEPSILON());
288
289 // second two-component system
290 CPPUNIT_ASSERT(testVector.GetOneNormalVector(two));
291 CPPUNIT_ASSERT(testVector.ScalarProduct(two) <= LINALG_MYEPSILON());
292
293 // three component system
294 CPPUNIT_ASSERT(testVector.GetOneNormalVector(three));
295 CPPUNIT_ASSERT(testVector.ScalarProduct(three) <= LINALG_MYEPSILON());
296}
Note: See TracBrowser for help on using the repository browser.