source: LinearAlgebra/src/unittests/RealSpaceMatrixUnitTest.cpp@ f221e3

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 f221e3 was 50c35d, checked in by Frederik Heber <heber@…>, 13 years ago

Added Line::getRotationMatrix() and RealSpaceMatrix::setRotation(Vector,double).

  • setRotation() now allows to set a rotation matrix about an arbitrary axis.
  • Line::getRotationMatrix() just forwards to RealSpaceMatrix::setRotation() with its direction.
  • added (simple) unit test function for both.
  • Property mode set to 100644
File size: 12.2 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 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * RealSpaceMatrixUnitTest.cpp
10 *
11 * Created on: Jul 7, 2010
12 * Author: crueger
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include <cppunit/CompilerOutputter.h>
21#include <cppunit/extensions/TestFactoryRegistry.h>
22#include <cppunit/ui/text/TestRunner.h>
23
24#include <cmath>
25#include <limits>
26
27#include "RealSpaceMatrixUnitTest.hpp"
28
29// include headers that implement a archive in simple text format
30#include <boost/archive/text_oarchive.hpp>
31#include <boost/archive/text_iarchive.hpp>
32
33#include "CodePatterns/Assert.hpp"
34
35#include "defs.hpp"
36#include "Exceptions.hpp"
37#include "MatrixContent.hpp"
38#include "RealSpaceMatrix.hpp"
39#include "Vector.hpp"
40
41#ifdef HAVE_TESTRUNNER
42#include "UnitTestMain.hpp"
43#endif /*HAVE_TESTRUNNER*/
44
45// Registers the fixture into the 'registry'
46CPPUNIT_TEST_SUITE_REGISTRATION( RealSpaceMatrixTest );
47
48void RealSpaceMatrixTest::setUp(){
49 // failing asserts should be thrown
50 ASSERT_DO(Assert::Throw);
51
52 zero = new RealSpaceMatrix();
53 for(int i =NDIM;i--;) {
54 for(int j =NDIM;j--;) {
55 zero->at(i,j)=0.;
56 }
57 }
58 one = new RealSpaceMatrix();
59 for(int i =NDIM;i--;){
60 one->at(i,i)=1.;
61 }
62 full=new RealSpaceMatrix();
63 for(int i=NDIM;i--;){
64 for(int j=NDIM;j--;){
65 full->at(i,j)=1.;
66 }
67 }
68 diagonal = new RealSpaceMatrix();
69 for(int i=NDIM;i--;){
70 diagonal->at(i,i)=i+1.;
71 }
72 perm1 = new RealSpaceMatrix();
73 perm1->column(0) = unitVec[0];
74 perm1->column(1) = unitVec[2];
75 perm1->column(2) = unitVec[1];
76
77
78 perm2 = new RealSpaceMatrix();
79 perm2->column(0) = unitVec[1];
80 perm2->column(1) = unitVec[0];
81 perm2->column(2) = unitVec[2];
82
83 perm3 = new RealSpaceMatrix();
84 perm3->column(0) = unitVec[1];
85 perm3->column(1) = unitVec[2];
86 perm3->column(2) = unitVec[0];
87
88 perm4 = new RealSpaceMatrix();
89 perm4->column(0) = unitVec[2];
90 perm4->column(1) = unitVec[1];
91 perm4->column(2) = unitVec[0];
92
93 perm5 = new RealSpaceMatrix();
94 perm5->column(0) = unitVec[2];
95 perm5->column(1) = unitVec[0];
96 perm5->column(2) = unitVec[1];
97
98}
99void RealSpaceMatrixTest::tearDown(){
100 delete zero;
101 delete one;
102 delete full;
103 delete diagonal;
104 delete perm1;
105 delete perm2;
106 delete perm3;
107 delete perm4;
108 delete perm5;
109}
110
111void RealSpaceMatrixTest::AccessTest(){
112 RealSpaceMatrix mat;
113 for(int i=NDIM;i--;){
114 for(int j=NDIM;j--;){
115 CPPUNIT_ASSERT_EQUAL(mat.at(i,j),0.);
116 }
117 }
118 int k=1;
119 for(int i=NDIM;i--;){
120 for(int j=NDIM;j--;){
121 mat.at(i,j)=k++;
122 }
123 }
124 k=1;
125 for(int i=NDIM;i--;){
126 for(int j=NDIM;j--;){
127 CPPUNIT_ASSERT_EQUAL(mat.at(i,j),(double)k);
128 ++k;
129 }
130 }
131}
132
133void RealSpaceMatrixTest::VectorTest(){
134 RealSpaceMatrix mat;
135 for(int i=NDIM;i--;){
136 CPPUNIT_ASSERT_EQUAL(mat.row(i),zeroVec);
137 CPPUNIT_ASSERT_EQUAL(mat.column(i),zeroVec);
138 }
139 CPPUNIT_ASSERT_EQUAL(mat.diagonal(),zeroVec);
140
141 mat.setIdentity();
142 CPPUNIT_ASSERT_EQUAL(mat.row(0),unitVec[0]);
143 CPPUNIT_ASSERT_EQUAL(mat.row(1),unitVec[1]);
144 CPPUNIT_ASSERT_EQUAL(mat.row(2),unitVec[2]);
145 CPPUNIT_ASSERT_EQUAL(mat.column(0),unitVec[0]);
146 CPPUNIT_ASSERT_EQUAL(mat.column(1),unitVec[1]);
147 CPPUNIT_ASSERT_EQUAL(mat.column(2),unitVec[2]);
148
149 Vector t1=Vector(1.,1.,1.);
150 Vector t2=Vector(2.,2.,2.);
151 Vector t3=Vector(3.,3.,3.);
152 Vector t4=Vector(1.,2.,3.);
153
154 mat.row(0)=t1;
155 mat.row(1)=t2;
156 mat.row(2)=t3;
157 CPPUNIT_ASSERT_EQUAL(mat.row(0),t1);
158 CPPUNIT_ASSERT_EQUAL(mat.row(1),t2);
159 CPPUNIT_ASSERT_EQUAL(mat.row(2),t3);
160 CPPUNIT_ASSERT_EQUAL(mat.column(0),t4);
161 CPPUNIT_ASSERT_EQUAL(mat.column(1),t4);
162 CPPUNIT_ASSERT_EQUAL(mat.column(2),t4);
163 CPPUNIT_ASSERT_EQUAL(mat.diagonal(),t4);
164 for(int i=NDIM;i--;){
165 for(int j=NDIM;j--;){
166 CPPUNIT_ASSERT_EQUAL(mat.at(i,j),i+1.);
167 }
168 }
169
170 mat.column(0)=t1;
171 mat.column(1)=t2;
172 mat.column(2)=t3;
173 CPPUNIT_ASSERT_EQUAL(mat.column(0),t1);
174 CPPUNIT_ASSERT_EQUAL(mat.column(1),t2);
175 CPPUNIT_ASSERT_EQUAL(mat.column(2),t3);
176 CPPUNIT_ASSERT_EQUAL(mat.row(0),t4);
177 CPPUNIT_ASSERT_EQUAL(mat.row(1),t4);
178 CPPUNIT_ASSERT_EQUAL(mat.row(2),t4);
179 CPPUNIT_ASSERT_EQUAL(mat.diagonal(),t4);
180 for(int i=NDIM;i--;){
181 for(int j=NDIM;j--;){
182 CPPUNIT_ASSERT_EQUAL(mat.at(i,j),j+1.);
183 }
184 }
185}
186
187void RealSpaceMatrixTest::TransposeTest(){
188 RealSpaceMatrix res;
189
190 // transpose of unit is unit
191 res.setIdentity();
192 res.transpose();
193 CPPUNIT_ASSERT_EQUAL(res,*one);
194
195 // transpose of transpose is same matrix
196 res.setZero();
197 res.set(2,2, 1.);
198 CPPUNIT_ASSERT_EQUAL(res.transpose().transpose(),res);
199}
200
201void RealSpaceMatrixTest::OperationTest(){
202 RealSpaceMatrix res;
203
204 res =(*zero) *(*zero);
205 //std::cout << *zero << " times " << *zero << " is " << res << std::endl;
206 CPPUNIT_ASSERT_EQUAL(res,*zero);
207 res =(*zero) *(*one);
208 CPPUNIT_ASSERT_EQUAL(res,*zero);
209 res =(*zero) *(*full);
210 CPPUNIT_ASSERT_EQUAL(res,*zero);
211 res =(*zero) *(*diagonal);
212 CPPUNIT_ASSERT_EQUAL(res,*zero);
213 res =(*zero) *(*perm1);
214 CPPUNIT_ASSERT_EQUAL(res,*zero);
215 res =(*zero) *(*perm2);
216 CPPUNIT_ASSERT_EQUAL(res,*zero);
217 res =(*zero) *(*perm3);
218 CPPUNIT_ASSERT_EQUAL(res,*zero);
219 res =(*zero) *(*perm4);
220 CPPUNIT_ASSERT_EQUAL(res,*zero);
221 res =(*zero) *(*perm5);
222 CPPUNIT_ASSERT_EQUAL(res,*zero);
223
224 res =(*one)*(*one);
225 CPPUNIT_ASSERT_EQUAL(res,*one);
226 res =(*one)*(*full);
227 CPPUNIT_ASSERT_EQUAL(res,*full);
228 res =(*one)*(*diagonal);
229 CPPUNIT_ASSERT_EQUAL(res,*diagonal);
230 res =(*one)*(*perm1);
231 CPPUNIT_ASSERT_EQUAL(res,*perm1);
232 res =(*one)*(*perm2);
233 CPPUNIT_ASSERT_EQUAL(res,*perm2);
234 res =(*one)*(*perm3);
235 CPPUNIT_ASSERT_EQUAL(res,*perm3);
236 res =(*one)*(*perm4);
237 CPPUNIT_ASSERT_EQUAL(res,*perm4);
238 res =(*one)*(*perm5);
239 CPPUNIT_ASSERT_EQUAL(res,*perm5);
240
241 res = (*full)*(*perm1);
242 CPPUNIT_ASSERT_EQUAL(res,*full);
243 res = (*full)*(*perm2);
244 CPPUNIT_ASSERT_EQUAL(res,*full);
245 res = (*full)*(*perm3);
246 CPPUNIT_ASSERT_EQUAL(res,*full);
247 res = (*full)*(*perm4);
248 CPPUNIT_ASSERT_EQUAL(res,*full);
249 res = (*full)*(*perm5);
250 CPPUNIT_ASSERT_EQUAL(res,*full);
251
252 res = (*diagonal)*(*perm1);
253 CPPUNIT_ASSERT_EQUAL(res.column(0),unitVec[0]);
254 CPPUNIT_ASSERT_EQUAL(res.column(1),3*unitVec[2]);
255 CPPUNIT_ASSERT_EQUAL(res.column(2),2*unitVec[1]);
256 res = (*diagonal)*(*perm2);
257 CPPUNIT_ASSERT_EQUAL(res.column(0),2*unitVec[1]);
258 CPPUNIT_ASSERT_EQUAL(res.column(1),unitVec[0]);
259 CPPUNIT_ASSERT_EQUAL(res.column(2),3*unitVec[2]);
260 res = (*diagonal)*(*perm3);
261 CPPUNIT_ASSERT_EQUAL(res.column(0),2*unitVec[1]);
262 CPPUNIT_ASSERT_EQUAL(res.column(1),3*unitVec[2]);
263 CPPUNIT_ASSERT_EQUAL(res.column(2),unitVec[0]);
264 res = (*diagonal)*(*perm4);
265 CPPUNIT_ASSERT_EQUAL(res.column(0),3*unitVec[2]);
266 CPPUNIT_ASSERT_EQUAL(res.column(1),2*unitVec[1]);
267 CPPUNIT_ASSERT_EQUAL(res.column(2),unitVec[0]);
268 res = (*diagonal)*(*perm5);
269 CPPUNIT_ASSERT_EQUAL(res.column(0),3*unitVec[2]);
270 CPPUNIT_ASSERT_EQUAL(res.column(1),unitVec[0]);
271 CPPUNIT_ASSERT_EQUAL(res.column(2),2*unitVec[1]);
272}
273
274void RealSpaceMatrixTest::RotationTest(){
275 RealSpaceMatrix res;
276 RealSpaceMatrix inverse;
277 Vector fixture;
278
279 // zero rotation angles yields unity matrix
280 res.setRotation(0,0,0);
281 CPPUNIT_ASSERT_EQUAL(*one, res);
282
283 // arbitrary rotation matrix has det = 1
284 res.setRotation(M_PI/3.,1.,M_PI/7.);
285 CPPUNIT_ASSERT(fabs(res.determinant() - 1.) <= LINALG_MYEPSILON());
286
287 // inverse is rotation matrix with negative angles
288 res.setRotation(M_PI/3.,0.,0.);
289 inverse.setRotation(-M_PI/3.,0.,0.);
290 CPPUNIT_ASSERT_EQUAL(*one, res * inverse);
291
292 // ... or transposed
293 res.setRotation(M_PI/3.,0.,0.);
294 CPPUNIT_ASSERT_EQUAL(inverse, res.transposed());
295
296 // check arbitrary axis
297 res.setRotation(unitVec[0], M_PI/3.);
298 inverse.setRotation(M_PI/3.,0.,0.);
299 CPPUNIT_ASSERT_EQUAL( res, inverse);
300 res.setRotation(unitVec[1], M_PI/3.);
301 inverse.setRotation(0., M_PI/3.,0.);
302 CPPUNIT_ASSERT_EQUAL( res, inverse);
303 res.setRotation(unitVec[2], M_PI/3.);
304 inverse.setRotation(0.,0.,M_PI/3.);
305 CPPUNIT_ASSERT_EQUAL( res, inverse);
306
307 // check axis not normalized throws
308#ifndef NDEBUG
309 CPPUNIT_ASSERT_THROW( res.setRotation(unitVec[0]+unitVec[1], M_PI/3.),Assert::AssertionFailure);
310#endif
311
312 // check arbitrary axis and det=1
313 fixture = (unitVec[0]+unitVec[1]).getNormalized();
314 res.setRotation(fixture, M_PI/3.);
315 CPPUNIT_ASSERT(fabs(res.determinant() - 1.) <= LINALG_MYEPSILON());
316 fixture = (unitVec[1]+unitVec[2]).getNormalized();
317 res.setRotation(fixture, M_PI/3.);
318 CPPUNIT_ASSERT(fabs(res.determinant() - 1.) <= LINALG_MYEPSILON());
319 fixture = (unitVec[1]+unitVec[2]).getNormalized();
320 res.setRotation(fixture, M_PI/3.);
321 CPPUNIT_ASSERT(fabs(res.determinant() - 1.) <= LINALG_MYEPSILON());
322}
323
324void RealSpaceMatrixTest::InvertTest(){
325 CPPUNIT_ASSERT_THROW(zero->invert(),NotInvertibleException);
326 CPPUNIT_ASSERT_THROW(full->invert(),NotInvertibleException);
327
328 RealSpaceMatrix res;
329 res = (*one)*one->invert();
330 CPPUNIT_ASSERT_EQUAL(res,*one);
331 res = (*diagonal)*diagonal->invert();
332 CPPUNIT_ASSERT_EQUAL(res,*one);
333 res = (*perm1)*perm1->invert();
334 CPPUNIT_ASSERT_EQUAL(res,*one);
335 res = (*perm2)*perm2->invert();
336 CPPUNIT_ASSERT_EQUAL(res,*one);
337 res = (*perm3)*perm3->invert();
338 CPPUNIT_ASSERT_EQUAL(res,*one);
339 res = (*perm4)*perm4->invert();
340 CPPUNIT_ASSERT_EQUAL(res,*one);
341 res = (*perm5)*perm5->invert();
342 CPPUNIT_ASSERT_EQUAL(res,*one);
343}
344
345
346void RealSpaceMatrixTest::DeterminantTest(){
347 CPPUNIT_ASSERT_EQUAL(zero->determinant(),0.);
348 CPPUNIT_ASSERT_EQUAL(one->determinant(),1.);
349 CPPUNIT_ASSERT_EQUAL(diagonal->determinant(),6.);
350 CPPUNIT_ASSERT_EQUAL(full->determinant(),0.);
351 CPPUNIT_ASSERT_EQUAL(perm1->determinant(),-1.);
352 CPPUNIT_ASSERT_EQUAL(perm2->determinant(),-1.);
353 CPPUNIT_ASSERT_EQUAL(perm3->determinant(),1.);
354 CPPUNIT_ASSERT_EQUAL(perm4->determinant(),-1.);
355 CPPUNIT_ASSERT_EQUAL(perm5->determinant(),1.);
356}
357
358void RealSpaceMatrixTest::VecMultTest(){
359 CPPUNIT_ASSERT_EQUAL((*zero)*unitVec[0],zeroVec);
360 CPPUNIT_ASSERT_EQUAL((*zero)*unitVec[1],zeroVec);
361 CPPUNIT_ASSERT_EQUAL((*zero)*unitVec[2],zeroVec);
362 CPPUNIT_ASSERT_EQUAL((*zero)*zeroVec,zeroVec);
363
364 CPPUNIT_ASSERT_EQUAL((*one)*unitVec[0],unitVec[0]);
365 CPPUNIT_ASSERT_EQUAL((*one)*unitVec[1],unitVec[1]);
366 CPPUNIT_ASSERT_EQUAL((*one)*unitVec[2],unitVec[2]);
367 CPPUNIT_ASSERT_EQUAL((*one)*zeroVec,zeroVec);
368
369 CPPUNIT_ASSERT_EQUAL((*diagonal)*unitVec[0],unitVec[0]);
370 CPPUNIT_ASSERT_EQUAL((*diagonal)*unitVec[1],2*unitVec[1]);
371 CPPUNIT_ASSERT_EQUAL((*diagonal)*unitVec[2],3*unitVec[2]);
372 CPPUNIT_ASSERT_EQUAL((*diagonal)*zeroVec,zeroVec);
373
374 CPPUNIT_ASSERT_EQUAL((*perm1)*unitVec[0],unitVec[0]);
375 CPPUNIT_ASSERT_EQUAL((*perm1)*unitVec[1],unitVec[2]);
376 CPPUNIT_ASSERT_EQUAL((*perm1)*unitVec[2],unitVec[1]);
377 CPPUNIT_ASSERT_EQUAL((*perm1)*zeroVec,zeroVec);
378
379 CPPUNIT_ASSERT_EQUAL((*perm2)*unitVec[0],unitVec[1]);
380 CPPUNIT_ASSERT_EQUAL((*perm2)*unitVec[1],unitVec[0]);
381 CPPUNIT_ASSERT_EQUAL((*perm2)*unitVec[2],unitVec[2]);
382 CPPUNIT_ASSERT_EQUAL((*perm2)*zeroVec,zeroVec);
383
384 CPPUNIT_ASSERT_EQUAL((*perm3)*unitVec[0],unitVec[1]);
385 CPPUNIT_ASSERT_EQUAL((*perm3)*unitVec[1],unitVec[2]);
386 CPPUNIT_ASSERT_EQUAL((*perm3)*unitVec[2],unitVec[0]);
387 CPPUNIT_ASSERT_EQUAL((*perm3)*zeroVec,zeroVec);
388
389 CPPUNIT_ASSERT_EQUAL((*perm4)*unitVec[0],unitVec[2]);
390 CPPUNIT_ASSERT_EQUAL((*perm4)*unitVec[1],unitVec[1]);
391 CPPUNIT_ASSERT_EQUAL((*perm4)*unitVec[2],unitVec[0]);
392 CPPUNIT_ASSERT_EQUAL((*perm4)*zeroVec,zeroVec);
393
394 CPPUNIT_ASSERT_EQUAL((*perm5)*unitVec[0],unitVec[2]);
395 CPPUNIT_ASSERT_EQUAL((*perm5)*unitVec[1],unitVec[0]);
396 CPPUNIT_ASSERT_EQUAL((*perm5)*unitVec[2],unitVec[1]);
397 CPPUNIT_ASSERT_EQUAL((*perm5)*zeroVec,zeroVec);
398
399 Vector t = Vector(1.,2.,3.);
400 CPPUNIT_ASSERT_EQUAL((*perm1)*t,Vector(1,3,2));
401 CPPUNIT_ASSERT_EQUAL((*perm2)*t,Vector(2,1,3));
402 CPPUNIT_ASSERT_EQUAL((*perm3)*t,Vector(3,1,2));
403 CPPUNIT_ASSERT_EQUAL((*perm4)*t,Vector(3,2,1));
404 CPPUNIT_ASSERT_EQUAL((*perm5)*t,Vector(2,3,1));
405}
406
407void RealSpaceMatrixTest::SerializationTest()
408{
409 // write element to stream
410 std::stringstream stream;
411 boost::archive::text_oarchive oa(stream);
412 oa << diagonal;
413
414 //std::cout << "Contents of archive is " << stream.str() << std::endl;
415
416 // create and open an archive for input
417 boost::archive::text_iarchive ia(stream);
418 // read class state from archive
419 RealSpaceMatrix *newm;
420
421 ia >> newm;
422
423 CPPUNIT_ASSERT (*diagonal == *newm);
424}
Note: See TracBrowser for help on using the repository browser.