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 | * SubspaceFactorizerUnittest.cpp
|
---|
10 | *
|
---|
11 | * Created on: Nov 13, 2010
|
---|
12 | * Author: heber
|
---|
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 |
|
---|
26 | #include <gsl/gsl_vector.h>
|
---|
27 | #include <boost/foreach.hpp>
|
---|
28 | #include <boost/shared_ptr.hpp>
|
---|
29 |
|
---|
30 | #include "Helpers/Assert.hpp"
|
---|
31 | #include "Helpers/Log.hpp"
|
---|
32 | #include "Helpers/toString.hpp"
|
---|
33 | #include "Helpers/Verbose.hpp"
|
---|
34 | #include "LinearAlgebra/VectorContent.hpp"
|
---|
35 | #include "LinearAlgebra/MatrixContent.hpp"
|
---|
36 |
|
---|
37 | #include "SubspaceFactorizerUnittest.hpp"
|
---|
38 |
|
---|
39 | #ifdef HAVE_TESTRUNNER
|
---|
40 | #include "UnitTestMain.hpp"
|
---|
41 | #endif /*HAVE_TESTRUNNER*/
|
---|
42 |
|
---|
43 | // Registers the fixture into the 'registry'
|
---|
44 | CPPUNIT_TEST_SUITE_REGISTRATION( SubspaceFactorizerUnittest );
|
---|
45 |
|
---|
46 | void SubspaceFactorizerUnittest::setUp(){
|
---|
47 | fourbyfour = new MatrixContent(4,4);
|
---|
48 | fourbyfour->setZero();
|
---|
49 | for (int i=0; i<4 ; i++) {
|
---|
50 | fourbyfour->set(i,i, 2.);
|
---|
51 | if (i < (4-1)) {
|
---|
52 | fourbyfour->set(i+1,i, 1.);
|
---|
53 | fourbyfour->set(i,i+1, 1.);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | transformation = new MatrixContent**[3];
|
---|
57 |
|
---|
58 | // 1d subspace
|
---|
59 | transformation[0] = new MatrixContent*[4];
|
---|
60 | for(size_t i=0; i<4;++i) {
|
---|
61 | transformation[0][i] = new MatrixContent(4,4);
|
---|
62 | transformation[0][i]->setZero();
|
---|
63 | for (size_t j=0; j<1; ++j)
|
---|
64 | transformation[0][i]->set(i+j,i+j, 1.);
|
---|
65 | // std::cout << i << "th transformation matrix, " << 1 << "d subspace is "
|
---|
66 | // << *transformation[0][i] << std::endl;
|
---|
67 | }
|
---|
68 |
|
---|
69 | // 2d subspace
|
---|
70 | transformation[1] = new MatrixContent*[3];
|
---|
71 | for(size_t i=0; i<3;++i) {
|
---|
72 | transformation[1][i] = new MatrixContent(4,4);
|
---|
73 | transformation[1][i]->setZero();
|
---|
74 | for (size_t j=0; j<2; ++j)
|
---|
75 | transformation[1][i]->set(i+j,i+j, 1.);
|
---|
76 | // std::cout << i << "th transformation matrix, " << 2 << "d subspace is "
|
---|
77 | // << *transformation[1][i] << std::endl;
|
---|
78 | }
|
---|
79 |
|
---|
80 | // 3d subspace
|
---|
81 | transformation[2] = new MatrixContent*[2];
|
---|
82 | for(size_t i=0; i<2;++i) {
|
---|
83 | transformation[2][i] = new MatrixContent(4,4);
|
---|
84 | transformation[2][i]->setZero();
|
---|
85 | for (size_t j=0; j<3; ++j)
|
---|
86 | transformation[2][i]->set(i+j,i+j, 1.);
|
---|
87 | // std::cout << i << "th transformation matrix, " << 3 << "d subspace is "
|
---|
88 | // << *transformation[2][i] << std::endl;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | void SubspaceFactorizerUnittest::tearDown(){
|
---|
92 | // delete test matrix
|
---|
93 | delete fourbyfour;
|
---|
94 |
|
---|
95 | // delete all transformations
|
---|
96 | for(size_t i=0; i<3;++i)
|
---|
97 | delete transformation[0][i];
|
---|
98 | delete[] transformation[0];
|
---|
99 | for(size_t i=0; i<3;++i)
|
---|
100 | delete transformation[1][i];
|
---|
101 | delete[] transformation[1];
|
---|
102 | for(size_t i=0; i<2;++i)
|
---|
103 | delete transformation[2][i];
|
---|
104 | delete[] transformation[2];
|
---|
105 | delete[] transformation;
|
---|
106 | }
|
---|
107 |
|
---|
108 | void SubspaceFactorizerUnittest::BlockTest()
|
---|
109 | {
|
---|
110 | MatrixContent temp((*fourbyfour)&(*transformation[0][0]));
|
---|
111 | std::cout << "Our matrix is " << *fourbyfour << "." << std::endl;
|
---|
112 |
|
---|
113 | std::cout << "Hadamard product of " << *fourbyfour << " with " << *transformation[0][0] << " is: " << std::endl;
|
---|
114 | std::cout << temp << std::endl;
|
---|
115 |
|
---|
116 | gsl_vector *eigenvalues = temp.transformToEigenbasis();
|
---|
117 | VectorContent *eigenvaluesView = new VectorViewContent(gsl_vector_subvector(eigenvalues, 0, eigenvalues->size));
|
---|
118 | std::cout << "The resulting eigenbasis is " << temp
|
---|
119 | << "\n\t with eigenvalues " << *eigenvaluesView << std::endl;
|
---|
120 | delete eigenvaluesView;
|
---|
121 | gsl_vector_free(eigenvalues);
|
---|
122 |
|
---|
123 |
|
---|
124 | CPPUNIT_ASSERT_EQUAL(0,0);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /** For given set of row and column indices, we extract the small block matrix.
|
---|
128 | * @param bigmatrix big matrix to extract from
|
---|
129 | * @param Eigenvectors eigenvectors of the subspaces to obtain matrix in
|
---|
130 | * @param columnindexset index set to pick out of all indices
|
---|
131 | * @return small matrix with dimension equal to the number of indices for row and column.
|
---|
132 | */
|
---|
133 | MatrixContent * getSubspaceMatrix(
|
---|
134 | MatrixContent &bigmatrix,
|
---|
135 | VectorArray &Eigenvectors,
|
---|
136 | const IndexSet &indexset)
|
---|
137 | {
|
---|
138 | // check whether subsystem is big enough for both index sets
|
---|
139 | ASSERT(indexset.size() <= bigmatrix.getRows(),
|
---|
140 | "embedSubspaceMatrix() - bigmatrix has less rows "+toString(bigmatrix.getRows())
|
---|
141 | +" than needed by index set "
|
---|
142 | +toString(indexset.size())+"!");
|
---|
143 | ASSERT(indexset.size() <= bigmatrix.getColumns(),
|
---|
144 | "embedSubspaceMatrix() - bigmatrix has less columns "+toString(bigmatrix.getColumns())
|
---|
145 | +" than needed by index set "
|
---|
146 | +toString(indexset.size())+"!");
|
---|
147 |
|
---|
148 | // construct small matrix
|
---|
149 | MatrixContent *subsystem = new MatrixContent(indexset.size(), indexset.size());
|
---|
150 | size_t localrow = 0; // local row indices for the subsystem
|
---|
151 | size_t localcolumn = 0;
|
---|
152 | BOOST_FOREACH( size_t rowindex, indexset) {
|
---|
153 | localcolumn = 0;
|
---|
154 | BOOST_FOREACH( size_t columnindex, indexset) {
|
---|
155 | ASSERT((rowindex < bigmatrix.getRows()) && (columnindex < bigmatrix.getColumns()),
|
---|
156 | "current index pair ("
|
---|
157 | +toString(rowindex)+","+toString(columnindex)
|
---|
158 | +") is out of bounds of bigmatrix ("
|
---|
159 | +toString(bigmatrix.getRows())+","+toString(bigmatrix.getColumns())
|
---|
160 | +")");
|
---|
161 | subsystem->at(localrow,localcolumn) = (*Eigenvectors[rowindex]) * (bigmatrix * (*Eigenvectors[columnindex]));
|
---|
162 | localcolumn++;
|
---|
163 | }
|
---|
164 | localrow++;
|
---|
165 | }
|
---|
166 | return subsystem;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /** For a given set of row and columns indices, we embed a small block matrix into a bigger space.
|
---|
170 | *
|
---|
171 | * @param eigenvectors current eigenvectors
|
---|
172 | * @param rowindexset row index set
|
---|
173 | * @param columnindexset column index set
|
---|
174 | * @return bigmatrix with eigenvectors contained
|
---|
175 | */
|
---|
176 | MatrixContent * embedSubspaceMatrix(
|
---|
177 | VectorArray &Eigenvectors,
|
---|
178 | MatrixContent &subsystem,
|
---|
179 | const IndexSet &columnindexset)
|
---|
180 | {
|
---|
181 | // check whether bigmatrix is at least as big as subsystem
|
---|
182 | ASSERT(Eigenvectors.size() > 0,
|
---|
183 | "embedSubspaceMatrix() - no Eigenvectors given!");
|
---|
184 | ASSERT(subsystem.getRows() <= Eigenvectors[0]->getDimension(),
|
---|
185 | "embedSubspaceMatrix() - subsystem has more rows "
|
---|
186 | +toString(subsystem.getRows())+" than eigenvectors "
|
---|
187 | +toString(Eigenvectors[0]->getDimension())+"!");
|
---|
188 | ASSERT(subsystem.getColumns() <= Eigenvectors.size(),
|
---|
189 | "embedSubspaceMatrix() - subsystem has more columns "
|
---|
190 | +toString(subsystem.getColumns())+" than eigenvectors "
|
---|
191 | +toString(Eigenvectors.size())+"!");
|
---|
192 | // check whether subsystem is big enough for both index sets
|
---|
193 | ASSERT(subsystem.getColumns() == subsystem.getRows(),
|
---|
194 | "embedSubspaceMatrix() - subsystem is not square "
|
---|
195 | +toString(subsystem.getRows())+" than needed by index set "
|
---|
196 | +toString(subsystem.getColumns())+"!");
|
---|
197 | ASSERT(columnindexset.size() == subsystem.getColumns(),
|
---|
198 | "embedSubspaceMatrix() - subsystem has not the same number of columns "
|
---|
199 | +toString(subsystem.getColumns())+" compared to the index set "
|
---|
200 | +toString(columnindexset.size())+"!");
|
---|
201 |
|
---|
202 | // construct intermediate matrix
|
---|
203 | MatrixContent *intermediatematrix = new MatrixContent(Eigenvectors[0]->getDimension(), columnindexset.size());
|
---|
204 | size_t localcolumn = 0;
|
---|
205 | BOOST_FOREACH(size_t columnindex, columnindexset) {
|
---|
206 | // create two vectors from each row and copy assign them
|
---|
207 | boost::shared_ptr<VectorContent> srceigenvector(Eigenvectors[columnindex]);
|
---|
208 | boost::shared_ptr<VectorContent> desteigenvector(intermediatematrix->getColumnVector(localcolumn));
|
---|
209 | *desteigenvector = *srceigenvector;
|
---|
210 | localcolumn++;
|
---|
211 | }
|
---|
212 | // matrix product with eigenbasis subsystem matrix
|
---|
213 | *intermediatematrix *= subsystem;
|
---|
214 |
|
---|
215 | // and place at right columns into bigmatrix
|
---|
216 | MatrixContent *bigmatrix = new MatrixContent(Eigenvectors[0]->getDimension(), Eigenvectors.size());
|
---|
217 | bigmatrix->setZero();
|
---|
218 | localcolumn = 0;
|
---|
219 | BOOST_FOREACH(size_t columnindex, columnindexset) {
|
---|
220 | // create two vectors from each row and copy assign them
|
---|
221 | boost::shared_ptr<VectorContent> srceigenvector(intermediatematrix->getColumnVector(localcolumn));
|
---|
222 | boost::shared_ptr<VectorContent> desteigenvector(bigmatrix->getColumnVector(columnindex));
|
---|
223 | *desteigenvector = *srceigenvector;
|
---|
224 | localcolumn++;
|
---|
225 | }
|
---|
226 |
|
---|
227 | return bigmatrix;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /** Prints the scalar product of each possible pair that is not orthonormal.
|
---|
231 | * We use class logger for printing.
|
---|
232 | * @param AllIndices set of all possible indices of the eigenvectors
|
---|
233 | * @param CurrentEigenvectors array of eigenvectors
|
---|
234 | * @return true - all are orthonormal to each other,
|
---|
235 | * false - some are not orthogonal or not normalized.
|
---|
236 | */
|
---|
237 | bool checkOrthogonality(const IndexSet &AllIndices, const VectorArray &CurrentEigenvectors)
|
---|
238 | {
|
---|
239 | size_t nonnormalized = 0;
|
---|
240 | size_t nonorthogonal = 0;
|
---|
241 | // check orthogonality
|
---|
242 | BOOST_FOREACH( size_t firstindex, AllIndices) {
|
---|
243 | BOOST_FOREACH( size_t secondindex, AllIndices) {
|
---|
244 | const double scp = (*CurrentEigenvectors[firstindex])*(*CurrentEigenvectors[secondindex]);
|
---|
245 | if (firstindex == secondindex) {
|
---|
246 | if (fabs(scp - 1.) > MYEPSILON) {
|
---|
247 | nonnormalized++;
|
---|
248 | Log() << Verbose(1) << "Vector " << firstindex << " is not normalized, off by "
|
---|
249 | << fabs(1.-(*CurrentEigenvectors[firstindex])*(*CurrentEigenvectors[secondindex])) << std::endl;
|
---|
250 | }
|
---|
251 | } else {
|
---|
252 | if (fabs(scp) > MYEPSILON) {
|
---|
253 | nonorthogonal++;
|
---|
254 | Log() << Verbose(1) << "Scalar product between " << firstindex << " and " << secondindex
|
---|
255 | << " is " << (*CurrentEigenvectors[firstindex])*(*CurrentEigenvectors[secondindex]) << std::endl;
|
---|
256 | }
|
---|
257 | }
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | if ((nonnormalized == 0) && (nonorthogonal == 0)) {
|
---|
262 | Log() << Verbose(1) << "All vectors are orthonormal to each other." << std::endl;
|
---|
263 | return true;
|
---|
264 | }
|
---|
265 | if ((nonnormalized == 0) && (nonorthogonal != 0))
|
---|
266 | Log() << Verbose(1) << "All vectors are normalized." << std::endl;
|
---|
267 | if ((nonnormalized != 0) && (nonorthogonal == 0))
|
---|
268 | Log() << Verbose(1) << "All vectors are orthogonal to each other." << std::endl;
|
---|
269 | return false;
|
---|
270 | }
|
---|
271 |
|
---|
272 | /** Calculate the sum of the scalar product of each possible pair.
|
---|
273 | * @param AllIndices set of all possible indices of the eigenvectors
|
---|
274 | * @param CurrentEigenvectors array of eigenvectors
|
---|
275 | * @return sum of scalar products between all possible pairs
|
---|
276 | */
|
---|
277 | double calculateOrthogonalityThreshold(const IndexSet &AllIndices, const VectorArray &CurrentEigenvectors)
|
---|
278 | {
|
---|
279 | double threshold = 0.;
|
---|
280 | // check orthogonality
|
---|
281 | BOOST_FOREACH( size_t firstindex, AllIndices) {
|
---|
282 | BOOST_FOREACH( size_t secondindex, AllIndices) {
|
---|
283 | const double scp = (*CurrentEigenvectors[firstindex])*(*CurrentEigenvectors[secondindex]);
|
---|
284 | if (firstindex == secondindex) {
|
---|
285 | threshold += fabs(scp - 1.);
|
---|
286 | } else {
|
---|
287 | threshold += fabs(scp);
|
---|
288 | }
|
---|
289 | }
|
---|
290 | }
|
---|
291 | return threshold;
|
---|
292 | }
|
---|
293 |
|
---|
294 | /** Operator for output to std::ostream operator of an IndexSet.
|
---|
295 | * @param ost output stream
|
---|
296 | * @param indexset index set to output
|
---|
297 | * @return ost output stream
|
---|
298 | */
|
---|
299 | std::ostream & operator<<(std::ostream &ost, const IndexSet &indexset)
|
---|
300 | {
|
---|
301 | ost << "{ ";
|
---|
302 | for (IndexSet::const_iterator iter = indexset.begin();
|
---|
303 | iter != indexset.end();
|
---|
304 | ++iter)
|
---|
305 | ost << *iter << " ";
|
---|
306 | ost << "}";
|
---|
307 | return ost;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /** Assign eigenvectors of subspace to full eigenvectors.
|
---|
311 | * We use parallelity as relation measure.
|
---|
312 | * @param eigenvalue eigenvalue to assign along with
|
---|
313 | * @param CurrentEigenvector eigenvector to assign, is taken over within
|
---|
314 | * boost::shared_ptr
|
---|
315 | * @param CurrentEigenvectors full eigenvectors
|
---|
316 | * @param CorrespondenceList list to make sure that each subspace eigenvector
|
---|
317 | * is assigned to a unique full eigenvector
|
---|
318 | * @param ParallelEigenvectorList list of "similar" subspace eigenvectors per
|
---|
319 | * full eigenvector, allocated
|
---|
320 | */
|
---|
321 | void AssignSubspaceEigenvectors(
|
---|
322 | double eigenvalue,
|
---|
323 | VectorContent *CurrentEigenvector,
|
---|
324 | VectorArray &CurrentEigenvectors,
|
---|
325 | IndexSet &CorrespondenceList,
|
---|
326 | VectorValueList *&ParallelEigenvectorList)
|
---|
327 | {
|
---|
328 | Log() << Verbose(1) << "Current Eigenvector is " << *CurrentEigenvector << std::endl;
|
---|
329 |
|
---|
330 | // (for now settle with the one we are most parallel to)
|
---|
331 | size_t mostparallel_index = 4;
|
---|
332 | double mostparallel_scalarproduct = 0.;
|
---|
333 | BOOST_FOREACH( size_t indexiter, CorrespondenceList) {
|
---|
334 | Log() << Verbose(2) << "Comparing to old " << indexiter << "th eigenvector " << *(CurrentEigenvectors[indexiter]) << std::endl;
|
---|
335 | const double scalarproduct = (*(CurrentEigenvectors[indexiter])) * (*CurrentEigenvector);
|
---|
336 | Log() << Verbose(2) << "SKP is " << scalarproduct << std::endl;
|
---|
337 | if (fabs(scalarproduct) > mostparallel_scalarproduct) {
|
---|
338 | mostparallel_scalarproduct = fabs(scalarproduct);
|
---|
339 | mostparallel_index = indexiter;
|
---|
340 | }
|
---|
341 | }
|
---|
342 | if (mostparallel_index != 4) {
|
---|
343 | // put into std::list for later use
|
---|
344 | // invert if pointing in negative direction
|
---|
345 | if ((*(CurrentEigenvectors[mostparallel_index])) * (*CurrentEigenvector) < 0) {
|
---|
346 | *CurrentEigenvector *= -1.;
|
---|
347 | Log() << Verbose(1) << "Pushing (inverted) " << *CurrentEigenvector << " into parallel list [" << mostparallel_index << "]" << std::endl;
|
---|
348 | } else {
|
---|
349 | Log() << Verbose(1) << "Pushing " << *CurrentEigenvector << " into parallel list [" << mostparallel_index << "]" << std::endl;
|
---|
350 | }
|
---|
351 | ParallelEigenvectorList[mostparallel_index].push_back(make_pair(boost::shared_ptr<VectorContent>(CurrentEigenvector), eigenvalue));
|
---|
352 | CorrespondenceList.erase(mostparallel_index);
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | void SubspaceFactorizerUnittest::EigenvectorTest()
|
---|
357 | {
|
---|
358 | VectorArray CurrentEigenvectors;
|
---|
359 | ValueArray CurrentEigenvalues;
|
---|
360 | VectorValueList *ParallelEigenvectorList = new VectorValueList[4];
|
---|
361 | IndexSet AllIndices;
|
---|
362 |
|
---|
363 | // create the total index set
|
---|
364 | for (size_t i=0;i<4;++i)
|
---|
365 | AllIndices.insert(i);
|
---|
366 |
|
---|
367 | // create all consecutive index subsets for dim 1 to 3
|
---|
368 | IndexMap Dimension_to_Indexset;
|
---|
369 | for (size_t dim = 0; dim<3;++dim) {
|
---|
370 | for (size_t i=0;i<4-dim;++i) {
|
---|
371 | IndexSet *indexset = new IndexSet;
|
---|
372 | for (size_t j=0;j<=dim;++j) {
|
---|
373 | //std::cout << "Putting " << i+j << " into " << i << "th map " << dim << std::endl;
|
---|
374 | CPPUNIT_ASSERT_MESSAGE("index "+toString(i+j)+" already present in "+toString(dim)+"-dim "+toString(i)+"th indexset.", indexset->count(i+j) == 0);
|
---|
375 | indexset->insert(i+j);
|
---|
376 | }
|
---|
377 | Dimension_to_Indexset.insert( make_pair(dim, boost::shared_ptr<IndexSet>(indexset)) );
|
---|
378 | // no need to free indexset, is stored in shared_ptr and
|
---|
379 | // will get released when Dimension_to_Indexset is destroyed
|
---|
380 | }
|
---|
381 | }
|
---|
382 |
|
---|
383 | // set to first guess, i.e. the unit vectors of R^4
|
---|
384 | BOOST_FOREACH( size_t index, AllIndices) {
|
---|
385 | boost::shared_ptr<VectorContent> EV(new VectorContent(4));
|
---|
386 | EV->setZero();
|
---|
387 | EV->at(index) = 1.;
|
---|
388 | CurrentEigenvectors.push_back(EV);
|
---|
389 | CurrentEigenvalues.push_back(0.);
|
---|
390 | }
|
---|
391 |
|
---|
392 | size_t run=1; // counting iterations
|
---|
393 | double threshold = 1.; // containing threshold value
|
---|
394 | while ((threshold > 1e-6) && (run < 200)) {
|
---|
395 | // for every dimension
|
---|
396 | for (size_t dim = 0; dim<3;++dim) {
|
---|
397 | // for every index set of this dimension
|
---|
398 | Log() << Verbose(0) << std::endl << std::endl;
|
---|
399 | Log() << Verbose(0) << "Current dimension is " << dim << std::endl;
|
---|
400 | std::pair<IndexMap::const_iterator,IndexMap::const_iterator> Bounds = Dimension_to_Indexset.equal_range(dim);
|
---|
401 | for (IndexMap::const_iterator IndexsetIter = Bounds.first;
|
---|
402 | IndexsetIter != Bounds.second;
|
---|
403 | ++IndexsetIter) {
|
---|
404 | // show the index set
|
---|
405 | Log() << Verbose(0) << std::endl;
|
---|
406 | Log() << Verbose(1) << "Current index set is " << *(IndexsetIter->second) << std::endl;
|
---|
407 |
|
---|
408 | // create transformation matrices from these
|
---|
409 | MatrixContent *subsystem = getSubspaceMatrix(*fourbyfour, CurrentEigenvectors, *(IndexsetIter->second));
|
---|
410 | Log() << Verbose(2) << "Subsystem matrix is " << *subsystem << std::endl;
|
---|
411 |
|
---|
412 | // solve _small_ systems for eigenvalues
|
---|
413 | VectorContent *Eigenvalues = new VectorContent(subsystem->transformToEigenbasis());
|
---|
414 | Log() << Verbose(2) << "Eigenvector matrix is " << *subsystem << std::endl;
|
---|
415 | Log() << Verbose(2) << "Eigenvalues are " << *Eigenvalues << std::endl;
|
---|
416 |
|
---|
417 | // blow up eigenvectors to 4dim column vector again
|
---|
418 | MatrixContent *Eigenvectors = embedSubspaceMatrix(CurrentEigenvectors, *subsystem, *(IndexsetIter->second));
|
---|
419 | Log() << Verbose(1) << "4x4 Eigenvector matrix is " << *Eigenvectors << std::endl;
|
---|
420 |
|
---|
421 | // we don't need the subsystem anymore
|
---|
422 | delete subsystem;
|
---|
423 |
|
---|
424 | // go through all eigenvectors in this subspace
|
---|
425 | IndexSet CorrespondenceList((*IndexsetIter->second)); // assure one-to-one and onto assignment
|
---|
426 | size_t localindex = 0;
|
---|
427 | BOOST_FOREACH( size_t iter, (*IndexsetIter->second)) {
|
---|
428 | // recognize eigenvectors parallel to existing ones
|
---|
429 | AssignSubspaceEigenvectors(
|
---|
430 | Eigenvalues->at(localindex),
|
---|
431 | new VectorContent(Eigenvectors->getColumnVector(iter)),
|
---|
432 | CurrentEigenvectors,
|
---|
433 | CorrespondenceList,
|
---|
434 | ParallelEigenvectorList);
|
---|
435 | localindex++;
|
---|
436 | }
|
---|
437 |
|
---|
438 | // free eigenvectors
|
---|
439 | delete Eigenvectors;
|
---|
440 | delete Eigenvalues;
|
---|
441 | }
|
---|
442 | }
|
---|
443 |
|
---|
444 | // print list of similar eigenvectors
|
---|
445 | BOOST_FOREACH( size_t index, AllIndices) {
|
---|
446 | Log() << Verbose(2) << "Similar to " << index << "th current eigenvector " << *(CurrentEigenvectors[index]) << " are:" << std::endl;
|
---|
447 | BOOST_FOREACH( VectorValueList::value_type &iter, ParallelEigenvectorList[index] ) {
|
---|
448 | Log() << Verbose(2) << *(iter.first) << std::endl;
|
---|
449 | }
|
---|
450 | Log() << Verbose(2) << std::endl;
|
---|
451 | }
|
---|
452 |
|
---|
453 | // create new CurrentEigenvectors from averaging parallel ones.
|
---|
454 | BOOST_FOREACH(size_t index, AllIndices) {
|
---|
455 | CurrentEigenvectors[index]->setZero();
|
---|
456 | CurrentEigenvalues[index] = 0.;
|
---|
457 | BOOST_FOREACH( VectorValueList::value_type &iter, ParallelEigenvectorList[index] ) {
|
---|
458 | *CurrentEigenvectors[index] += (*iter.first) * (iter.second);
|
---|
459 | CurrentEigenvalues[index] += (iter.second);
|
---|
460 | }
|
---|
461 | *CurrentEigenvectors[index] *= 1./CurrentEigenvalues[index];
|
---|
462 | CurrentEigenvalues[index] /= (double)ParallelEigenvectorList[index].size();
|
---|
463 | ParallelEigenvectorList[index].clear();
|
---|
464 | }
|
---|
465 |
|
---|
466 | // check orthonormality
|
---|
467 | threshold = calculateOrthogonalityThreshold(AllIndices, CurrentEigenvectors);
|
---|
468 | bool dontOrthonormalization = checkOrthogonality(AllIndices, CurrentEigenvectors);
|
---|
469 |
|
---|
470 | // orthonormalize
|
---|
471 | if (!dontOrthonormalization) {
|
---|
472 | Log() << Verbose(0) << "Orthonormalizing ... " << std::endl;
|
---|
473 | for (IndexSet::const_iterator firstindex = AllIndices.begin();
|
---|
474 | firstindex != AllIndices.end();
|
---|
475 | ++firstindex) {
|
---|
476 | for (IndexSet::const_iterator secondindex = firstindex;
|
---|
477 | secondindex != AllIndices.end();
|
---|
478 | ++secondindex) {
|
---|
479 | if (*firstindex == *secondindex) {
|
---|
480 | (*CurrentEigenvectors[*secondindex]) *= 1./(*CurrentEigenvectors[*secondindex]).Norm();
|
---|
481 | } else {
|
---|
482 | (*CurrentEigenvectors[*secondindex]) -=
|
---|
483 | ((*CurrentEigenvectors[*firstindex])*(*CurrentEigenvectors[*secondindex]))
|
---|
484 | *(*CurrentEigenvectors[*firstindex]);
|
---|
485 | }
|
---|
486 | }
|
---|
487 | }
|
---|
488 | }
|
---|
489 |
|
---|
490 | // check orthonormality again
|
---|
491 | checkOrthogonality(AllIndices, CurrentEigenvectors);
|
---|
492 |
|
---|
493 | // show new ones
|
---|
494 | Log() << Verbose(0) << "Resulting new eigenvectors and -values, run " << run << " are:" << std::endl;
|
---|
495 | BOOST_FOREACH( size_t index, AllIndices) {
|
---|
496 | Log() << Verbose(0) << *CurrentEigenvectors[index] << " with " << CurrentEigenvalues[index] << std::endl;
|
---|
497 | }
|
---|
498 | run++;
|
---|
499 | }
|
---|
500 |
|
---|
501 |
|
---|
502 | delete[] ParallelEigenvectorList;
|
---|
503 |
|
---|
504 | CPPUNIT_ASSERT_EQUAL(0,0);
|
---|
505 | }
|
---|
506 |
|
---|