source: LinearAlgebra/src/unittests/RealSpaceMatrixUnitTest.cpp@ 68c923

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

Added serialization capability to RealSpaceMatrix.

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