source: LinearAlgebra/src/unittests/RealSpaceMatrixUnitTest.cpp@ 7a0340

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

Renamed MatrixUnitTest to more appropriate RealSpaceMatrixUnitTest.

  • Property mode set to 100644
File size: 10.5 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 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 "defs.hpp"
30#include "Exceptions.hpp"
31#include "RealSpaceMatrix.hpp"
32#include "Vector.hpp"
33
34#ifdef HAVE_TESTRUNNER
35#include "UnitTestMain.hpp"
36#endif /*HAVE_TESTRUNNER*/
37
38// Registers the fixture into the 'registry'
39CPPUNIT_TEST_SUITE_REGISTRATION( RealSpaceMatrixTest );
40
41void RealSpaceMatrixTest::setUp(){
42 zero = new RealSpaceMatrix();
43 for(int i =NDIM;i--;) {
44 for(int j =NDIM;j--;) {
45 zero->at(i,j)=0.;
46 }
47 }
48 one = new RealSpaceMatrix();
49 for(int i =NDIM;i--;){
50 one->at(i,i)=1.;
51 }
52 full=new RealSpaceMatrix();
53 for(int i=NDIM;i--;){
54 for(int j=NDIM;j--;){
55 full->at(i,j)=1.;
56 }
57 }
58 diagonal = new RealSpaceMatrix();
59 for(int i=NDIM;i--;){
60 diagonal->at(i,i)=i+1.;
61 }
62 perm1 = new RealSpaceMatrix();
63 perm1->column(0) = unitVec[0];
64 perm1->column(1) = unitVec[2];
65 perm1->column(2) = unitVec[1];
66
67
68 perm2 = new RealSpaceMatrix();
69 perm2->column(0) = unitVec[1];
70 perm2->column(1) = unitVec[0];
71 perm2->column(2) = unitVec[2];
72
73 perm3 = new RealSpaceMatrix();
74 perm3->column(0) = unitVec[1];
75 perm3->column(1) = unitVec[2];
76 perm3->column(2) = unitVec[0];
77
78 perm4 = new RealSpaceMatrix();
79 perm4->column(0) = unitVec[2];
80 perm4->column(1) = unitVec[1];
81 perm4->column(2) = unitVec[0];
82
83 perm5 = new RealSpaceMatrix();
84 perm5->column(0) = unitVec[2];
85 perm5->column(1) = unitVec[0];
86 perm5->column(2) = unitVec[1];
87
88}
89void RealSpaceMatrixTest::tearDown(){
90 delete zero;
91 delete one;
92 delete full;
93 delete diagonal;
94 delete perm1;
95 delete perm2;
96 delete perm3;
97 delete perm4;
98 delete perm5;
99}
100
101void RealSpaceMatrixTest::AccessTest(){
102 RealSpaceMatrix mat;
103 for(int i=NDIM;i--;){
104 for(int j=NDIM;j--;){
105 CPPUNIT_ASSERT_EQUAL(mat.at(i,j),0.);
106 }
107 }
108 int k=1;
109 for(int i=NDIM;i--;){
110 for(int j=NDIM;j--;){
111 mat.at(i,j)=k++;
112 }
113 }
114 k=1;
115 for(int i=NDIM;i--;){
116 for(int j=NDIM;j--;){
117 CPPUNIT_ASSERT_EQUAL(mat.at(i,j),(double)k);
118 ++k;
119 }
120 }
121}
122
123void RealSpaceMatrixTest::VectorTest(){
124 RealSpaceMatrix mat;
125 for(int i=NDIM;i--;){
126 CPPUNIT_ASSERT_EQUAL(mat.row(i),zeroVec);
127 CPPUNIT_ASSERT_EQUAL(mat.column(i),zeroVec);
128 }
129 CPPUNIT_ASSERT_EQUAL(mat.diagonal(),zeroVec);
130
131 mat.setIdentity();
132 CPPUNIT_ASSERT_EQUAL(mat.row(0),unitVec[0]);
133 CPPUNIT_ASSERT_EQUAL(mat.row(1),unitVec[1]);
134 CPPUNIT_ASSERT_EQUAL(mat.row(2),unitVec[2]);
135 CPPUNIT_ASSERT_EQUAL(mat.column(0),unitVec[0]);
136 CPPUNIT_ASSERT_EQUAL(mat.column(1),unitVec[1]);
137 CPPUNIT_ASSERT_EQUAL(mat.column(2),unitVec[2]);
138
139 Vector t1=Vector(1.,1.,1.);
140 Vector t2=Vector(2.,2.,2.);
141 Vector t3=Vector(3.,3.,3.);
142 Vector t4=Vector(1.,2.,3.);
143
144 mat.row(0)=t1;
145 mat.row(1)=t2;
146 mat.row(2)=t3;
147 CPPUNIT_ASSERT_EQUAL(mat.row(0),t1);
148 CPPUNIT_ASSERT_EQUAL(mat.row(1),t2);
149 CPPUNIT_ASSERT_EQUAL(mat.row(2),t3);
150 CPPUNIT_ASSERT_EQUAL(mat.column(0),t4);
151 CPPUNIT_ASSERT_EQUAL(mat.column(1),t4);
152 CPPUNIT_ASSERT_EQUAL(mat.column(2),t4);
153 CPPUNIT_ASSERT_EQUAL(mat.diagonal(),t4);
154 for(int i=NDIM;i--;){
155 for(int j=NDIM;j--;){
156 CPPUNIT_ASSERT_EQUAL(mat.at(i,j),i+1.);
157 }
158 }
159
160 mat.column(0)=t1;
161 mat.column(1)=t2;
162 mat.column(2)=t3;
163 CPPUNIT_ASSERT_EQUAL(mat.column(0),t1);
164 CPPUNIT_ASSERT_EQUAL(mat.column(1),t2);
165 CPPUNIT_ASSERT_EQUAL(mat.column(2),t3);
166 CPPUNIT_ASSERT_EQUAL(mat.row(0),t4);
167 CPPUNIT_ASSERT_EQUAL(mat.row(1),t4);
168 CPPUNIT_ASSERT_EQUAL(mat.row(2),t4);
169 CPPUNIT_ASSERT_EQUAL(mat.diagonal(),t4);
170 for(int i=NDIM;i--;){
171 for(int j=NDIM;j--;){
172 CPPUNIT_ASSERT_EQUAL(mat.at(i,j),j+1.);
173 }
174 }
175}
176
177void RealSpaceMatrixTest::TransposeTest(){
178 RealSpaceMatrix res;
179
180 // transpose of unit is unit
181 res.setIdentity();
182 res.transpose();
183 CPPUNIT_ASSERT_EQUAL(res,*one);
184
185 // transpose of transpose is same matrix
186 res.setZero();
187 res.set(2,2, 1.);
188 CPPUNIT_ASSERT_EQUAL(res.transpose().transpose(),res);
189}
190
191void RealSpaceMatrixTest::OperationTest(){
192 RealSpaceMatrix res;
193
194 res =(*zero) *(*zero);
195 //std::cout << *zero << " times " << *zero << " is " << res << std::endl;
196 CPPUNIT_ASSERT_EQUAL(res,*zero);
197 res =(*zero) *(*one);
198 CPPUNIT_ASSERT_EQUAL(res,*zero);
199 res =(*zero) *(*full);
200 CPPUNIT_ASSERT_EQUAL(res,*zero);
201 res =(*zero) *(*diagonal);
202 CPPUNIT_ASSERT_EQUAL(res,*zero);
203 res =(*zero) *(*perm1);
204 CPPUNIT_ASSERT_EQUAL(res,*zero);
205 res =(*zero) *(*perm2);
206 CPPUNIT_ASSERT_EQUAL(res,*zero);
207 res =(*zero) *(*perm3);
208 CPPUNIT_ASSERT_EQUAL(res,*zero);
209 res =(*zero) *(*perm4);
210 CPPUNIT_ASSERT_EQUAL(res,*zero);
211 res =(*zero) *(*perm5);
212 CPPUNIT_ASSERT_EQUAL(res,*zero);
213
214 res =(*one)*(*one);
215 CPPUNIT_ASSERT_EQUAL(res,*one);
216 res =(*one)*(*full);
217 CPPUNIT_ASSERT_EQUAL(res,*full);
218 res =(*one)*(*diagonal);
219 CPPUNIT_ASSERT_EQUAL(res,*diagonal);
220 res =(*one)*(*perm1);
221 CPPUNIT_ASSERT_EQUAL(res,*perm1);
222 res =(*one)*(*perm2);
223 CPPUNIT_ASSERT_EQUAL(res,*perm2);
224 res =(*one)*(*perm3);
225 CPPUNIT_ASSERT_EQUAL(res,*perm3);
226 res =(*one)*(*perm4);
227 CPPUNIT_ASSERT_EQUAL(res,*perm4);
228 res =(*one)*(*perm5);
229 CPPUNIT_ASSERT_EQUAL(res,*perm5);
230
231 res = (*full)*(*perm1);
232 CPPUNIT_ASSERT_EQUAL(res,*full);
233 res = (*full)*(*perm2);
234 CPPUNIT_ASSERT_EQUAL(res,*full);
235 res = (*full)*(*perm3);
236 CPPUNIT_ASSERT_EQUAL(res,*full);
237 res = (*full)*(*perm4);
238 CPPUNIT_ASSERT_EQUAL(res,*full);
239 res = (*full)*(*perm5);
240 CPPUNIT_ASSERT_EQUAL(res,*full);
241
242 res = (*diagonal)*(*perm1);
243 CPPUNIT_ASSERT_EQUAL(res.column(0),unitVec[0]);
244 CPPUNIT_ASSERT_EQUAL(res.column(1),3*unitVec[2]);
245 CPPUNIT_ASSERT_EQUAL(res.column(2),2*unitVec[1]);
246 res = (*diagonal)*(*perm2);
247 CPPUNIT_ASSERT_EQUAL(res.column(0),2*unitVec[1]);
248 CPPUNIT_ASSERT_EQUAL(res.column(1),unitVec[0]);
249 CPPUNIT_ASSERT_EQUAL(res.column(2),3*unitVec[2]);
250 res = (*diagonal)*(*perm3);
251 CPPUNIT_ASSERT_EQUAL(res.column(0),2*unitVec[1]);
252 CPPUNIT_ASSERT_EQUAL(res.column(1),3*unitVec[2]);
253 CPPUNIT_ASSERT_EQUAL(res.column(2),unitVec[0]);
254 res = (*diagonal)*(*perm4);
255 CPPUNIT_ASSERT_EQUAL(res.column(0),3*unitVec[2]);
256 CPPUNIT_ASSERT_EQUAL(res.column(1),2*unitVec[1]);
257 CPPUNIT_ASSERT_EQUAL(res.column(2),unitVec[0]);
258 res = (*diagonal)*(*perm5);
259 CPPUNIT_ASSERT_EQUAL(res.column(0),3*unitVec[2]);
260 CPPUNIT_ASSERT_EQUAL(res.column(1),unitVec[0]);
261 CPPUNIT_ASSERT_EQUAL(res.column(2),2*unitVec[1]);
262}
263
264void RealSpaceMatrixTest::RotationTest(){
265 RealSpaceMatrix res;
266 RealSpaceMatrix inverse;
267
268 // zero rotation angles yields unity matrix
269 res.setRotation(0,0,0);
270 CPPUNIT_ASSERT_EQUAL(*one, res);
271
272 // arbitrary rotation matrix has det = 1
273 res.setRotation(M_PI/3.,1.,M_PI/7.);
274 CPPUNIT_ASSERT(fabs(res.determinant() - 1.) <= LINALG_MYEPSILON());
275
276 // inverse is rotation matrix with negative angles
277 res.setRotation(M_PI/3.,0.,0.);
278 inverse.setRotation(-M_PI/3.,0.,0.);
279 CPPUNIT_ASSERT_EQUAL(*one, res * inverse);
280
281 // ... or transposed
282 res.setRotation(M_PI/3.,0.,0.);
283 CPPUNIT_ASSERT_EQUAL(inverse, ((const RealSpaceMatrix) res).transpose());
284}
285
286void RealSpaceMatrixTest::InvertTest(){
287 CPPUNIT_ASSERT_THROW(zero->invert(),NotInvertibleException);
288 CPPUNIT_ASSERT_THROW(full->invert(),NotInvertibleException);
289
290 RealSpaceMatrix res;
291 res = (*one)*one->invert();
292 CPPUNIT_ASSERT_EQUAL(res,*one);
293 res = (*diagonal)*diagonal->invert();
294 CPPUNIT_ASSERT_EQUAL(res,*one);
295 res = (*perm1)*perm1->invert();
296 CPPUNIT_ASSERT_EQUAL(res,*one);
297 res = (*perm2)*perm2->invert();
298 CPPUNIT_ASSERT_EQUAL(res,*one);
299 res = (*perm3)*perm3->invert();
300 CPPUNIT_ASSERT_EQUAL(res,*one);
301 res = (*perm4)*perm4->invert();
302 CPPUNIT_ASSERT_EQUAL(res,*one);
303 res = (*perm5)*perm5->invert();
304 CPPUNIT_ASSERT_EQUAL(res,*one);
305}
306
307
308void RealSpaceMatrixTest::DeterminantTest(){
309 CPPUNIT_ASSERT_EQUAL(zero->determinant(),0.);
310 CPPUNIT_ASSERT_EQUAL(one->determinant(),1.);
311 CPPUNIT_ASSERT_EQUAL(diagonal->determinant(),6.);
312 CPPUNIT_ASSERT_EQUAL(full->determinant(),0.);
313 CPPUNIT_ASSERT_EQUAL(perm1->determinant(),-1.);
314 CPPUNIT_ASSERT_EQUAL(perm2->determinant(),-1.);
315 CPPUNIT_ASSERT_EQUAL(perm3->determinant(),1.);
316 CPPUNIT_ASSERT_EQUAL(perm4->determinant(),-1.);
317 CPPUNIT_ASSERT_EQUAL(perm5->determinant(),1.);
318}
319
320void RealSpaceMatrixTest::VecMultTest(){
321 CPPUNIT_ASSERT_EQUAL((*zero)*unitVec[0],zeroVec);
322 CPPUNIT_ASSERT_EQUAL((*zero)*unitVec[1],zeroVec);
323 CPPUNIT_ASSERT_EQUAL((*zero)*unitVec[2],zeroVec);
324 CPPUNIT_ASSERT_EQUAL((*zero)*zeroVec,zeroVec);
325
326 CPPUNIT_ASSERT_EQUAL((*one)*unitVec[0],unitVec[0]);
327 CPPUNIT_ASSERT_EQUAL((*one)*unitVec[1],unitVec[1]);
328 CPPUNIT_ASSERT_EQUAL((*one)*unitVec[2],unitVec[2]);
329 CPPUNIT_ASSERT_EQUAL((*one)*zeroVec,zeroVec);
330
331 CPPUNIT_ASSERT_EQUAL((*diagonal)*unitVec[0],unitVec[0]);
332 CPPUNIT_ASSERT_EQUAL((*diagonal)*unitVec[1],2*unitVec[1]);
333 CPPUNIT_ASSERT_EQUAL((*diagonal)*unitVec[2],3*unitVec[2]);
334 CPPUNIT_ASSERT_EQUAL((*diagonal)*zeroVec,zeroVec);
335
336 CPPUNIT_ASSERT_EQUAL((*perm1)*unitVec[0],unitVec[0]);
337 CPPUNIT_ASSERT_EQUAL((*perm1)*unitVec[1],unitVec[2]);
338 CPPUNIT_ASSERT_EQUAL((*perm1)*unitVec[2],unitVec[1]);
339 CPPUNIT_ASSERT_EQUAL((*perm1)*zeroVec,zeroVec);
340
341 CPPUNIT_ASSERT_EQUAL((*perm2)*unitVec[0],unitVec[1]);
342 CPPUNIT_ASSERT_EQUAL((*perm2)*unitVec[1],unitVec[0]);
343 CPPUNIT_ASSERT_EQUAL((*perm2)*unitVec[2],unitVec[2]);
344 CPPUNIT_ASSERT_EQUAL((*perm2)*zeroVec,zeroVec);
345
346 CPPUNIT_ASSERT_EQUAL((*perm3)*unitVec[0],unitVec[1]);
347 CPPUNIT_ASSERT_EQUAL((*perm3)*unitVec[1],unitVec[2]);
348 CPPUNIT_ASSERT_EQUAL((*perm3)*unitVec[2],unitVec[0]);
349 CPPUNIT_ASSERT_EQUAL((*perm3)*zeroVec,zeroVec);
350
351 CPPUNIT_ASSERT_EQUAL((*perm4)*unitVec[0],unitVec[2]);
352 CPPUNIT_ASSERT_EQUAL((*perm4)*unitVec[1],unitVec[1]);
353 CPPUNIT_ASSERT_EQUAL((*perm4)*unitVec[2],unitVec[0]);
354 CPPUNIT_ASSERT_EQUAL((*perm4)*zeroVec,zeroVec);
355
356 CPPUNIT_ASSERT_EQUAL((*perm5)*unitVec[0],unitVec[2]);
357 CPPUNIT_ASSERT_EQUAL((*perm5)*unitVec[1],unitVec[0]);
358 CPPUNIT_ASSERT_EQUAL((*perm5)*unitVec[2],unitVec[1]);
359 CPPUNIT_ASSERT_EQUAL((*perm5)*zeroVec,zeroVec);
360
361 Vector t = Vector(1.,2.,3.);
362 CPPUNIT_ASSERT_EQUAL((*perm1)*t,Vector(1,3,2));
363 CPPUNIT_ASSERT_EQUAL((*perm2)*t,Vector(2,1,3));
364 CPPUNIT_ASSERT_EQUAL((*perm3)*t,Vector(3,1,2));
365 CPPUNIT_ASSERT_EQUAL((*perm4)*t,Vector(3,2,1));
366 CPPUNIT_ASSERT_EQUAL((*perm5)*t,Vector(2,3,1));
367}
Note: See TracBrowser for help on using the repository browser.