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