Changeset e828c0


Ignore:
Timestamp:
Dec 4, 2010, 11:56:28 PM (14 years ago)
Author:
Frederik Heber <heber@…>
Branches:
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
Children:
4fbca9c, f03705
Parents:
1fb318
git-author:
Frederik Heber <heber@…> (12/04/10 18:10:56)
git-committer:
Frederik Heber <heber@…> (12/04/10 23:56:28)
Message:

SubspaceFactorizer is now hierarchical.

  • higher order subspace matrices are only corrections to lower order ones.
  • i.e. eigenvectors obtained from there have all lower ones projected and substracted.
Location:
src
Files:
1 added
9 edited

Legend:

Unmodified
Added
Removed
  • src/LinearAlgebra/Eigenspace.cpp

    r1fb318 re828c0  
    8585    boost::shared_ptr<VectorContent> ev(EigenvectorMatrix.getColumnVector(iter));
    8686    Eigenvectors.push_back( ev );
    87     Log() << Verbose(1) << iter << " th Eigenvector is " << *ev << std::endl;
     87    DoLog(1) && (Log() << Verbose(1) << iter << " th Eigenvector is " << *ev << std::endl);
    8888  }
    8989}
  • src/LinearAlgebra/MatrixContent.cpp

    r1fb318 re828c0  
    416416bool MatrixContent::SwapRowColumn(size_t i, size_t j)
    417417{
    418   assert (rows == columns && "The matrix must be square for swapping row against column to be possible.");
     418  ASSERT (rows == columns,
     419      "MatrixContent::SwapRowColumn() - The matrix must be square for swapping row against column to be possible.");
    419420  return (gsl_matrix_swap_rowcol (content, i, j) == GSL_SUCCESS);
    420421};
     
    533534}
    534535
     536
     537/** Sorts the eigenpairs in ascending order of the eigenvalues.
     538 * We assume that MatrixContent::transformToEigenbasis() has just been called.
     539 * @param eigenvalues vector of eigenvalue from
     540 *        MatrixContent::transformToEigenbasis()
     541 */
     542void MatrixContent::sortEigenbasis(gsl_vector *eigenvalues)
     543{
     544  gsl_eigen_symmv_sort (eigenvalues, content,
     545                          GSL_EIGEN_SORT_ABS_ASC);
     546}
     547
    535548/* ============================ Properties ============================== */
    536549/** Checks whether matrix' elements are strictly null.
     
    585598{
    586599  int signum = 0;
    587   assert (rows == columns && "Determinant can only be calculated for square matrices.");
     600  ASSERT(rows == columns,
     601      "MatrixContent::Determinant() - determinant can only be calculated for square matrices.");
    588602  gsl_permutation *p = gsl_permutation_alloc(rows);
    589603  gsl_linalg_LU_decomp(content, p, &signum);
     
    647661std::ostream & operator<<(std::ostream &ost, const MatrixContent &mat)
    648662{
    649   ost << "[";
     663  ost << "\\begin{pmatrix}";
    650664  for (size_t i=0;i<mat.rows;i++) {
    651665    for (size_t j=0;j<mat.columns;j++) {
    652       ost << mat.at(i,j);
     666      ost << mat.at(i,j) << " ";
    653667      if (j != mat.columns-1)
    654         ost << " ";
     668        ost << "& ";
    655669    }
    656670    if (i != mat.rows-1)
    657       ost << "; ";
     671      ost << "\\\\ ";
    658672  }
    659   ost << "]";
     673  ost << "\\end{pmatrix}";
    660674  return ost;
    661675}
  • src/LinearAlgebra/MatrixContent.hpp

    r1fb318 re828c0  
    8888  MatrixContent &transpose();
    8989  gsl_vector* transformToEigenbasis();
     90  void sortEigenbasis(gsl_vector *eigenvalues);
    9091
    9192  // Properties
  • src/LinearAlgebra/Subspace.cpp

    r1fb318 re828c0  
    4949
    5050  // create eigenspace matrices
    51   projectFullSpaceMatrixToSubspace();
     51  EigenspaceMatrix = projectFullspaceMatrixToSubspace(FullSpace.getEigenspaceMatrix());
    5252}
    5353
     
    6262/** Diagonalizes the subspace matrix.
    6363 *
    64  * @param fullmatrix full matrix to project into subspace and solve
    6564 */
    6665void Subspace::calculateEigenSubspace()
     
    6968  createProjectionMatrices();
    7069  // remove subsubspace directions
    71   correctEigenvectorsFromSubIndices();
     70  //correctProjectionMatricesFromSubIndices();
    7271  // solve
    73   projectFullSpaceMatrixToSubspace();
     72  EigenspaceMatrix = projectFullspaceMatrixToSubspace(FullSpace.getEigenspaceMatrix());
    7473  EigenvectorMatrix = EigenspaceMatrix;
    7574  VectorContent *EigenvalueVector = new VectorContent(EigenvectorMatrix.transformToEigenbasis());
     
    7978  }
    8079  delete EigenvalueVector;
    81   Log() << Verbose(2) << "Eigenvector matrix is " << EigenvectorMatrix << std::endl;
    82   Log() << Verbose(2) << "Eigenvalues are " << Eigenvalues << std::endl;
     80
    8381  // create mapping
    8482  createLocalMapping();
     83
     84  // swap the eigenvectors/-values to their correct sequence
     85  sortEigenvectors();
     86
     87  // scale eigenvectors by their eigenvalues for the subsequent correction
     88  scaleEigenvectorsbyEigenvalue();
     89
     90  // let only remain corrections to lower orders on this order
     91  correctEigenvectorsFromSubIndices();
     92
     93  if (!EigenvectorMatrix.IsNull()) {
     94    // get correct eigenvalues
     95    getNormofEigenvectorAsEigenvalue();
     96
     97    DoLog(2) && (Log() << Verbose(2) << "Eigenvector matrix is " << EigenvectorMatrix << std::endl);
     98    DoLog(2) && (Log() << Verbose(2) << "Eigenvalues are " << Eigenvalues << std::endl);
     99
     100  }
    85101}
    86102
     
    123139
    124140
     141/** Sort the eigenvectors in the order of Subspace::Indices.
     142 *
     143 */
     144void Subspace::sortEigenvectors()
     145{
     146  DoLog(1) && (Log() << Verbose(1) << "Sorting Eigenvectors ..." << std::endl);
     147  MatrixContent tempMatrix = EigenvectorMatrix;
     148  eigenvalueset tempValues = Eigenvalues;
     149  size_t localindex = 0;
     150  BOOST_FOREACH( size_t iter, Indices) {
     151    Log() << Verbose(1) << GlobalToLocal[iter] << "th eigenvector is associated to "
     152        << iter << " and goes to column " << localindex << "." << std::endl;
     153    boost::shared_ptr<VectorContent> columnvector(tempMatrix.getColumnVector(GlobalToLocal[iter]));
     154    *Eigenvectors[localindex] = *columnvector;
     155    Eigenvalues[localindex] = tempValues[GlobalToLocal[iter]];
     156    ++localindex;
     157  }
     158}
     159
     160
     161/** We remove the projections from lower subspaces from our Eigenspacematrix.
     162 * This is intended to diminish parallel changing of eigenspaces.
     163 */
    125164void Subspace::correctEigenvectorsFromSubIndices()
    126165{
     166  DoLog(1) && (Log() << Verbose(1) << "Correcting EigenvectorMatrix ... " << std::endl);
     167  BOOST_FOREACH( boost::shared_ptr<Subspace> iter, SubIndices ) {
     168    const MatrixContent tempMatrix =
     169        projectFullspaceMatrixToSubspace(
     170            iter->projectSubspaceMatrixToFullspace(iter->getEigenvectorMatrix())
     171            );
     172    DoLog(1) && (Log() << Verbose(1) << "Subtracting matrix from " << *iter << ":" << tempMatrix << std::endl);
     173    EigenvectorMatrix -= tempMatrix;
     174  }
     175  DoLog(1) && (Log() << Verbose(1) << "Corrected EigenvectorMatrix is " << EigenvectorMatrix << std::endl);
     176
     177  // check for null vectors
     178  const size_t max = EigenvectorMatrix.getColumns();
     179  for(size_t i = 0; i< max; ++i) {
     180    if (Eigenvectors[i]->IsZero()) {
     181      Eigenvalues[i] = 0.;
     182      Eigenvectors[i]->setZero();
     183    }
     184  }
     185}
     186
     187
     188/** Scale the local eigenvectors each by their eigenvalue.
     189 *
     190 */
     191void Subspace::scaleEigenvectorsbyEigenvalue()
     192{
     193  size_t localindex = 0;
     194  BOOST_FOREACH( boost::shared_ptr<VectorContent> ev, Eigenvectors) {
     195    *ev *= Eigenvalues[localindex];
     196    ++localindex;
     197  }
     198}
     199
     200
     201/** Get Norm of each eigenvector to serve als eigenvalue.
     202 *
     203 */
     204void Subspace::getNormofEigenvectorAsEigenvalue()
     205{
     206  size_t localindex = 0;
     207  BOOST_FOREACH( boost::shared_ptr<VectorContent> ev, Eigenvectors) {
     208    Eigenvalues[localindex] = ev->Norm();
     209    ++localindex;
     210  }
     211}
     212
     213
     214/** We remove the projections from lower subspaces from our Eigenspacematrix.
     215 * This is intended to diminish parallel changing of eigenspaces.
     216 */
     217void Subspace::correctProjectionMatricesFromSubIndices()
     218{
     219  MatrixContent subtractMatrix(ProjectToSubspace.getRows(), ProjectToSubspace.getColumns());
     220  DoLog(1) && (Log() << Verbose(1) << "Correcting ProjectToSubspace ... " << std::endl);
     221  BOOST_FOREACH( boost::shared_ptr<Subspace> iter, SubIndices ) {
     222    const MatrixContent tempMatrix = iter->getEigenvectorMatrix();
     223    const MatrixContent tempMatrix2 = iter->projectSubspaceMatrixToFullspace(tempMatrix);
     224    const MatrixContent tempMatrix3 = (tempMatrix2 * ProjectToSubspace);
     225    DoLog(1) && (Log() << Verbose(1) << "Subtracting matrix from " << *iter << ":" << tempMatrix3 << std::endl);
     226    subtractMatrix += tempMatrix3;
     227  }
     228  ProjectToSubspace -= subtractMatrix;
     229  DoLog(1) && (Log() << Verbose(1) << "Corrected ProjectToSubspace is " << ProjectToSubspace << std::endl);
     230
     231  ProjectFromSubspace = ((const MatrixContent)ProjectToSubspace).transpose();
     232  DoLog(1) && (Log() << Verbose(1) << "Corrected ProjectFromSubspace is " << ProjectFromSubspace << std::endl);
    127233}
    128234
     
    198304
    199305
     306/** Getter for Subspace::SubIndices.
     307 *
     308 * @return const reference to Subspace::SubIndices.
     309 */
     310const Subspace::subset & Subspace::getSubIndices() const
     311{
     312  return SubIndices;
     313}
     314
    200315/** Remove a subset of indices from the SubIndices.
    201316 *
     
    236351{
    237352  // check whether bigmatrix is at least as big as EigenspaceMatrix
    238   ASSERT(EigenspaceMatrix.getRows() <= Eigenvectors[0]->getDimension(),
     353  ASSERT(EigenspaceMatrix.getRows() <= FullSpace.getEigenspaceMatrix().getRows(),
    239354      "embedEigenspaceMatrix() - EigenspaceMatrix has more rows "
    240       +toString(EigenspaceMatrix.getRows())+" than eigenvectors "
     355      +toString(EigenspaceMatrix.getRows())+" than FullSpace::EigenspaceMatrix "
    241356      +toString(Eigenvectors[0]->getDimension())+"!");
    242   ASSERT(EigenspaceMatrix.getColumns() <= Eigenvectors.size(),
     357  ASSERT(EigenspaceMatrix.getColumns() <= FullSpace.getEigenspaceMatrix().getColumns(),
    243358      "embedEigenspaceMatrix() - EigenspaceMatrix has more columns "
    244       +toString(EigenspaceMatrix.getColumns())+" than eigenvectors "
     359      +toString(EigenspaceMatrix.getColumns())+" than FullSpace::EigenspaceMatrix "
    245360      +toString(Eigenvectors.size())+"!");
    246361  // check whether EigenspaceMatrix is big enough for both index sets
     
    255370
    256371  // construct intermediate matrix
    257   MatrixContent *intermediatematrix = new MatrixContent(Eigenvectors[0]->getDimension(), Indices.size());
    258   size_t localcolumn = 0;
    259   BOOST_FOREACH(size_t columnindex, Indices) {
    260     // create two vectors from each row and copy assign them
    261     boost::shared_ptr<VectorContent> srceigenvector(Eigenvectors[columnindex]);
    262     boost::shared_ptr<VectorContent> desteigenvector(intermediatematrix->getColumnVector(localcolumn));
    263     *desteigenvector = *srceigenvector;
    264     localcolumn++;
    265   }
    266   // matrix product with eigenbasis EigenspaceMatrix matrix
    267   *intermediatematrix *= EigenspaceMatrix;
    268 
    269   // and place at right columns into bigmatrix
    270   MatrixContent *bigmatrix = new MatrixContent(Eigenvectors[0]->getDimension(), Eigenvectors.size());
    271   bigmatrix->setZero();
    272   localcolumn = 0;
    273   BOOST_FOREACH(size_t columnindex, Indices) {
    274     // create two vectors from each row and copy assign them
    275     boost::shared_ptr<VectorContent> srceigenvector(intermediatematrix->getColumnVector(localcolumn));
    276     boost::shared_ptr<VectorContent> desteigenvector(bigmatrix->getColumnVector(columnindex));
    277     *desteigenvector = *srceigenvector;
    278     localcolumn++;
    279   }
     372  MatrixContent *bigmatrix = new MatrixContent(
     373      FullSpace.getEigenspaceMatrix().getRows(),
     374      FullSpace.getEigenspaceMatrix().getColumns());
     375  *bigmatrix = ProjectToSubspace * EigenspaceMatrix * ProjectFromSubspace;
     376
     377//  MatrixContent *intermediatematrix = new MatrixContent(FullSpace.getEigenspaceMatrix().getRows(), Indices.size());
     378//  size_t localcolumn = 0;
     379//  BOOST_FOREACH(size_t columnindex, Indices) {
     380//    // create two vectors from each row and copy assign them
     381//    boost::shared_ptr<VectorContent> srceigenvector(Eigenvectors[columnindex]);
     382//    boost::shared_ptr<VectorContent> desteigenvector(intermediatematrix->getColumnVector(localcolumn));
     383//    *desteigenvector = *srceigenvector;
     384//    localcolumn++;
     385//  }
     386//  // matrix product with eigenbasis EigenspaceMatrix matrix
     387//  *intermediatematrix *= EigenspaceMatrix;
     388//
     389//  // and place at right columns into bigmatrix
     390//  MatrixContent *bigmatrix = new MatrixContent(FullSpace.getEigenspaceMatrix().getRows(), Eigenvectors.size());
     391//  bigmatrix->setZero();
     392//  localcolumn = 0;
     393//  BOOST_FOREACH(size_t columnindex, Indices) {
     394//    // create two vectors from each row and copy assign them
     395//    boost::shared_ptr<VectorContent> srceigenvector(intermediatematrix->getColumnVector(localcolumn));
     396//    boost::shared_ptr<VectorContent> desteigenvector(bigmatrix->getColumnVector(columnindex));
     397//    *desteigenvector = *srceigenvector;
     398//    localcolumn++;
     399//  }
    280400
    281401  return *bigmatrix;
     
    302422  BOOST_FOREACH(size_t iter, Indices) {
    303423    *ColumnVectors[localindex] = FullSpace.getEigenvector(iter);
    304     localindex++;
    305   }
    306   Log() << Verbose(2) << "ProjectionToSubspace matrix is " << ProjectToSubspace << std::endl;
     424    ++localindex;
     425  }
     426  DoLog(2) && (Log() << Verbose(2) << "ProjectionToSubspace matrix is " << ProjectToSubspace << std::endl);
    307427
    308428  // then ProjectFromSubspace is just transposed
    309429  ProjectFromSubspace = ((const MatrixContent)ProjectToSubspace).transpose();
    310   Log() << Verbose(2) << "ProjectFromSubspace matrix is " << ProjectFromSubspace << std::endl;
    311 }
    312 
    313 
    314 /** Creates the subspace matrix by projecting down the FullSpace::EigenspaceMatrix.
     430  DoLog(2) && (Log() << Verbose(2) << "ProjectFromSubspace matrix is " << ProjectFromSubspace << std::endl);
     431}
     432
     433
     434/** Creates the subspace matrix by projecting down the given \a _fullmatrix.
    315435 *  We just perform \f$M = P^t \cdot A \cdot P\f$, when P are the projection matrix,
    316436 *  A the full matrix and M the subspace matrix.
    317  */
    318 void Subspace::projectFullSpaceMatrixToSubspace()
    319 {
     437 *
     438 *  @param _fullmatrix full matrix A to project into subspace
     439 *  @return subspace matrix M
     440 */
     441const MatrixContent Subspace::projectFullspaceMatrixToSubspace(const MatrixContent &_fullmatrix) const
     442{
     443  ASSERT((FullSpace.getIndices().size() == _fullmatrix.getRows())
     444      && (FullSpace.getIndices().size() == _fullmatrix.getColumns()),
     445      "Subspace::projectSubspaceMatrixToFullspace() - dimensions of this subspace "
     446      +toString(Indices.size())+" and the given matrix ("
     447      +toString(_fullmatrix.getRows())+" x "+toString(_fullmatrix.getColumns())+") differ!");
    320448  // construct small matrix
    321   EigenspaceMatrix = ProjectFromSubspace * FullSpace.getEigenspaceMatrix() * ProjectToSubspace;
    322   Log() << Verbose(2) << "EigenspaceMatrix matrix is " << EigenspaceMatrix << std::endl;
    323 }
    324 
    325 
    326 /** We associate local Eigenvectors with ones from FullSpace by a paralellity
     449  MatrixContent tempMatrix = ProjectFromSubspace * _fullmatrix * ProjectToSubspace;
     450  return tempMatrix;
     451  DoLog(2) && (Log() << Verbose(2) << "returned subspace matrix is " << tempMatrix << std::endl);
     452}
     453
     454
     455/** Creates a full space matrix which is the projection of given \a _subspacematrix
     456 * from the subspace.
     457 *
     458 * @param _subspacematrix subspace matrix
     459 * @return full matrix of the Subspace::EigenspaceMatrix projected into
     460 *         Subspace::FullSpace.
     461 */
     462const MatrixContent Subspace::projectSubspaceMatrixToFullspace(const MatrixContent &_subspacematrix) const
     463{
     464  ASSERT((Indices.size() == _subspacematrix.getRows()) && (Indices.size() == _subspacematrix.getColumns()),
     465      "Subspace::projectSubspaceMatrixToFullspace() - dimensions of this subspace "
     466      +toString(Indices.size())+" and the given matrix ("
     467      +toString(_subspacematrix.getRows())+" x "+toString(_subspacematrix.getColumns())+") differ!");
     468  return (ProjectToSubspace * _subspacematrix * ProjectFromSubspace);
     469}
     470
     471
     472/** We associate local Eigenvectors with ones from FullSpace by a parallelity
    327473 * criterion.
    328474 */
     
    335481  for (size_t localindex = 0; localindex< Indices.size(); ++localindex) {
    336482    boost::shared_ptr<VectorContent> &CurrentEigenvector = Eigenvectors[localindex];
    337     Log() << Verbose(1) << "Current Eigenvector is " << *CurrentEigenvector << std::endl;
     483    VectorContent tempCurrentEigenvector = ProjectToSubspace * (*CurrentEigenvector);
     484    Log() << Verbose(1) << "Current Eigenvector is " << *CurrentEigenvector
     485        << " --(projected)-> " << tempCurrentEigenvector << std::endl;
     486    if (Eigenvalues[localindex] == 0) {
     487      DoLog(2) && (Log() << Verbose(2) << "Is null, skipping." << std::endl);
     488      continue;
     489    }
    338490
    339491    // (for now settle with the one we are most parallel to)
     
    341493    double mostparallel_scalarproduct = 0.;
    342494    BOOST_FOREACH( size_t indexiter, CorrespondenceList) {
    343       Log() << Verbose(2) << "Comparing to old " << indexiter << "th eigenvector " << FullSpace.getEigenvector(indexiter) << std::endl;
    344       const double scalarproduct = (FullSpace.getEigenvector(indexiter)) * ( ProjectToSubspace * (*CurrentEigenvector));
    345       Log() << Verbose(2) << "SKP is " << scalarproduct << std::endl;
     495      DoLog(2) && (Log() << Verbose(2) << "Comparing to old " << indexiter << "th eigenvector " << FullSpace.getEigenvector(indexiter) << std::endl);
     496      const double scalarproduct = (FullSpace.getEigenvector(indexiter)) * ( tempCurrentEigenvector );
     497      DoLog(2) && (Log() << Verbose(2) << "SKP is " << scalarproduct << std::endl);
    346498      if (fabs(scalarproduct) > fabs(mostparallel_scalarproduct)) {
    347499        mostparallel_scalarproduct = scalarproduct;
     
    355507      if (mostparallel_scalarproduct < 0) {
    356508        *CurrentEigenvector *= -1.;
    357         Log() << Verbose(1) << "Associating (inverted) " << *CurrentEigenvector << " with " << mostparallel_index << "th's fullspace eigenvector." << std::endl;
     509        DoLog(1) && (Log() << Verbose(1) << "Associating (inverted) " << *CurrentEigenvector << " with " << mostparallel_index << "th's fullspace eigenvector." << std::endl);
    358510      } else {
    359         Log() << Verbose(1) << "Associating " << *CurrentEigenvector << " with " << mostparallel_index << "th's fullspace eigenvector." << std::endl;
     511        DoLog(1) && (Log() << Verbose(1) << "Associating " << *CurrentEigenvector << " with " << mostparallel_index << "th's fullspace eigenvector." << std::endl);
    360512      }
    361513      ASSERT( LocalToGlobal.count(localindex) == 0,
  • src/LinearAlgebra/Subspace.hpp

    r1fb318 re828c0  
    4545  bool removeSubset(boost::shared_ptr<Subspace> &_s);
    4646
     47  // solving
    4748  void calculateEigenSubspace();
    48   void correctEigenvectorsFromSubIndices();
     49
     50  // accessing
    4951  const MatrixContent & getEigenvectorMatrixInFullSpace();
    5052  const eigenvectorset & getEigenvectorsInFullSpace();
    5153  const VectorContent getEigenvectorParallelToFullOne(size_t i);
    5254  const double getEigenvalueOfEigenvectorParallelToFullOne(size_t i);
    53   void createLocalMapping();
     55  const subset & getSubIndices() const;
    5456
    5557private:
    5658
     59  void createLocalMapping();
    5760  void invertLocalToGlobalMapping();
    5861  void getSubspacematrixFromBigmatrix(const MatrixContent & bigmatrix);
     62  void sortEigenvectors();
     63  void correctEigenvectorsFromSubIndices();
     64  void correctProjectionMatricesFromSubIndices();
     65  void scaleEigenvectorsbyEigenvalue();
     66  void getNormofEigenvectorAsEigenvalue();
    5967  void createProjectionMatrices();
    60   void projectFullSpaceMatrixToSubspace();
     68  const MatrixContent projectFullspaceMatrixToSubspace(const MatrixContent &_fullmatrix) const;
     69  const MatrixContent projectSubspaceMatrixToFullspace(const MatrixContent &_subspacematrix) const;
    6170
    6271  mapping LocalToGlobal;
  • src/LinearAlgebra/VectorContent.cpp

    r1fb318 re828c0  
    347347ostream& operator<<(ostream& ost, const VectorContent& m)
    348348{
    349   ost << "(";
     349  ost << "[";
    350350  for (size_t i=0;i<m.dimension;i++) {
    351351    ost << m.at(i);
    352352    if (i != m.dimension-1)
    353       ost << ",";
     353      ost << " ";
    354354  }
    355   ost << ")";
     355  ost << "]";
    356356  return ost;
    357357};
  • src/Makefile.am

    r1fb318 re828c0  
    272272
    273273noinst_LIBRARIES = libmenu.a
    274 bin_PROGRAMS = molecuilder molecuildergui joiner analyzer
     274bin_PROGRAMS = molecuilder molecuildergui joiner analyzer SubspaceFactorizer
    275275EXTRA_PROGRAMS = unity
    276276
     
    280280
    281281molecuilder_DATA = elements.db valence.db orbitals.db Hbonddistance.db Hbondangle.db
     282
     283SubspaceFactorizer_CXXFLAGS = $(BOOST_CPPFLAGS)
     284SubspaceFactorizer_SOURCES = SubspaceFactorizer.cpp
     285SubspaceFactorizer_LDADD = \
     286        LinearAlgebra/libMolecuilderLinearAlgebra-@MOLECUILDER_API_VERSION@.la \
     287        Exceptions/libMolecuilderExceptions-@MOLECUILDER_API_VERSION@.la \
     288        Helpers/libMolecuilderHelpers-@MOLECUILDER_API_VERSION@.la \
     289        $(GSLLIB) \
     290        $(BOOST_LIB)
    282291
    283292molecuilder_CXXFLAGS = $(BOOST_CPPFLAGS)
  • src/unittests/SubspaceFactorizerUnittest.cpp

    r1fb318 re828c0  
    2727#include <boost/foreach.hpp>
    2828#include <boost/shared_ptr.hpp>
     29#include <boost/timer.hpp>
    2930
    3031#include "Helpers/Assert.hpp"
     
    4950  matrix = new MatrixContent(matrixdimension,matrixdimension);
    5051  matrix->setZero();
    51   for (int i=0; i<matrixdimension ; i++) {
    52     matrix->set(i,i, 2.);
    53     if (i < (matrixdimension-1)) {
    54       matrix->set(i+1,i, 1.);
    55       matrix->set(i,i+1, 1.);
     52  for (size_t i=0; i<matrixdimension ; i++) {
     53    for (size_t j=0; j<= i; ++j) {
     54      //const double value = 10. * rand() / (double)RAND_MAX;
     55      //const double value = i==j ? 2. : 1.;
     56      if (i==j)
     57        matrix->set(i,i, 2.);
     58      else if (j+1 == i) {
     59        matrix->set(i,j, 1.);
     60        matrix->set(j,i, 1.);
     61      } else {
     62        matrix->set(i,j, 0.);
     63        matrix->set(j,i, 0.);
     64      }
    5665    }
    5766  }
     
    209218        if (fabs(scp - 1.) > MYEPSILON) {
    210219          nonnormalized++;
    211           Log() << Verbose(1) << "Vector " << firstindex << " is not normalized, off by "
     220          Log() << Verbose(2) << "Vector " << firstindex << " is not normalized, off by "
    212221              << fabs(1.-(*CurrentEigenvectors[firstindex])*(*CurrentEigenvectors[secondindex])) << std::endl;
    213222        }
     
    215224        if (fabs(scp) > MYEPSILON) {
    216225          nonorthogonal++;
    217           Log() << Verbose(1) << "Scalar product between " << firstindex << " and " << secondindex
     226          Log() << Verbose(2) << "Scalar product between " << firstindex << " and " << secondindex
    218227              << " is " << (*CurrentEigenvectors[firstindex])*(*CurrentEigenvectors[secondindex]) << std::endl;
    219228        }
     
    223232
    224233  if ((nonnormalized == 0) && (nonorthogonal == 0)) {
    225     Log() << Verbose(1) << "All vectors are orthonormal to each other." << std::endl;
     234    DoLog(1) && (DoLog(1) && (Log() << Verbose(1) << "All vectors are orthonormal to each other." << std::endl));
    226235    return true;
    227236  }
    228237  if ((nonnormalized == 0) && (nonorthogonal != 0))
    229     Log() << Verbose(1) << "All vectors are normalized." << std::endl;
     238    DoLog(1) && (DoLog(1) && (Log() << Verbose(1) << "All vectors are normalized." << std::endl));
    230239  if ((nonnormalized != 0) && (nonorthogonal == 0))
    231     Log() << Verbose(1) << "All vectors are orthogonal to each other." << std::endl;
     240    DoLog(1) && (DoLog(1) && (Log() << Verbose(1) << "All vectors are orthogonal to each other." << std::endl));
    232241  return false;
    233242}
     
    289298    VectorValueList *&ParallelEigenvectorList)
    290299{
    291   Log() << Verbose(1) << "Current Eigenvector is " << *CurrentEigenvector << std::endl;
     300  DoLog(1) && (DoLog(1) && (Log() << Verbose(1) << "Current Eigenvector is " << *CurrentEigenvector << std::endl));
    292301
    293302  // (for now settle with the one we are most parallel to)
     
    295304  double mostparallel_scalarproduct = 0.;
    296305  BOOST_FOREACH( size_t indexiter, CorrespondenceList) {
    297     Log() << Verbose(2) << "Comparing to old " << indexiter << "th eigenvector " << *(CurrentEigenvectors[indexiter]) << std::endl;
     306    DoLog(2) && (DoLog(2) && (Log() << Verbose(2) << "Comparing to old " << indexiter << "th eigenvector " << *(CurrentEigenvectors[indexiter]) << std::endl));
    298307    const double scalarproduct = (*(CurrentEigenvectors[indexiter])) * (*CurrentEigenvector);
    299     Log() << Verbose(2) << "SKP is " << scalarproduct << std::endl;
     308    DoLog(2) && (DoLog(2) && (Log() << Verbose(2) << "SKP is " << scalarproduct << std::endl));
    300309    if (fabs(scalarproduct) > mostparallel_scalarproduct) {
    301310      mostparallel_scalarproduct = fabs(scalarproduct);
     
    308317    if ((*(CurrentEigenvectors[mostparallel_index])) * (*CurrentEigenvector) < 0) {
    309318      *CurrentEigenvector *= -1.;
    310       Log() << Verbose(1) << "Pushing (inverted) " << *CurrentEigenvector << " into parallel list [" << mostparallel_index << "]" << std::endl;
     319      DoLog(1) && (Log() << Verbose(1) << "Pushing (inverted) " << *CurrentEigenvector << " into parallel list [" << mostparallel_index << "]" << std::endl);
    311320    } else {
    312       Log() << Verbose(1) << "Pushing " << *CurrentEigenvector << " into parallel list [" << mostparallel_index << "]" << std::endl;
     321      DoLog(1) && (Log() << Verbose(1) << "Pushing " << *CurrentEigenvector << " into parallel list [" << mostparallel_index << "]" << std::endl);
    313322    }
    314323    ParallelEigenvectorList[mostparallel_index].push_back(make_pair(boost::shared_ptr<VectorContent>(CurrentEigenvector), eigenvalue));
     
    360369    for (size_t dim = 0; dim<subspacelimit;++dim) {
    361370      // for every index set of this dimension
    362       Log() << Verbose(0) << std::endl << std::endl;
    363       Log() << Verbose(0) << "Current dimension is " << dim << std::endl;
     371      DoLog(0) && (Log() << Verbose(0) << std::endl << std::endl);
     372      DoLog(0) && (Log() << Verbose(0) << "Current dimension is " << dim << std::endl);
    364373      std::pair<IndexMap::const_iterator,IndexMap::const_iterator> Bounds = Dimension_to_Indexset.equal_range(dim);
    365374      for (IndexMap::const_iterator IndexsetIter = Bounds.first;
     
    367376          ++IndexsetIter) {
    368377        // show the index set
    369         Log() << Verbose(0) << std::endl;
    370         Log() << Verbose(1) << "Current index set is " << *(IndexsetIter->second) << std::endl;
     378        DoLog(0) && (Log() << Verbose(0) << std::endl);
     379        DoLog(1) && (Log() << Verbose(1) << "Current index set is " << *(IndexsetIter->second) << std::endl);
    371380
    372381        // create transformation matrices from these
    373382        MatrixContent *subsystem = getSubspaceMatrix(*matrix, CurrentEigenvectors, *(IndexsetIter->second));
    374         Log() << Verbose(2) << "Subsystem matrix is " << *subsystem << std::endl;
     383        DoLog(2) && (Log() << Verbose(2) << "Subsystem matrix is " << *subsystem << std::endl);
    375384
    376385        // solve _small_ systems for eigenvalues
    377386        VectorContent *Eigenvalues = new VectorContent(subsystem->transformToEigenbasis());
    378         Log() << Verbose(2) << "Eigenvector matrix is " << *subsystem << std::endl;
    379         Log() << Verbose(2) << "Eigenvalues are " << *Eigenvalues << std::endl;
     387        DoLog(2) && (Log() << Verbose(2) << "Eigenvector matrix is " << *subsystem << std::endl);
     388        DoLog(2) && (Log() << Verbose(2) << "Eigenvalues are " << *Eigenvalues << std::endl);
    380389
    381390        // blow up eigenvectors to matrixdimensiondim column vector again
    382391        MatrixContent *Eigenvectors = embedSubspaceMatrix(CurrentEigenvectors, *subsystem, *(IndexsetIter->second));
    383         Log() << Verbose(1) << matrixdimension << "x" << matrixdimension << " Eigenvector matrix is " << *Eigenvectors << std::endl;
     392        DoLog(1) && (Log() << Verbose(1) << matrixdimension << "x" << matrixdimension << " Eigenvector matrix is " << *Eigenvectors << std::endl);
    384393
    385394        // we don't need the subsystem anymore
     
    408417    // print list of similar eigenvectors
    409418    BOOST_FOREACH( size_t index, AllIndices) {
    410       Log() << Verbose(2) << "Similar to " << index << "th current eigenvector " << *(CurrentEigenvectors[index]) << " are:" << std::endl;
     419      DoLog(2) && (Log() << Verbose(2) << "Similar to " << index << "th current eigenvector " << *(CurrentEigenvectors[index]) << " are:" << std::endl);
    411420      BOOST_FOREACH( VectorValueList::value_type &iter, ParallelEigenvectorList[index] ) {
    412         Log() << Verbose(2) << *(iter.first) << std::endl;
    413       }
    414       Log() << Verbose(2) << std::endl;
     421        DoLog(2) && (Log() << Verbose(2) << *(iter.first) << std::endl);
     422      }
     423      DoLog(2) && (Log() << Verbose(2) << std::endl);
    415424    }
    416425
     
    434443    // orthonormalize
    435444    if (!dontOrthonormalization) {
    436       Log() << Verbose(0) << "Orthonormalizing ... " << std::endl;
     445      DoLog(0) && (Log() << Verbose(0) << "Orthonormalizing ... " << std::endl);
    437446      for (IndexSet::const_iterator firstindex = AllIndices.begin();
    438447          firstindex != AllIndices.end();
     
    456465
    457466    // show new ones
    458     Log() << Verbose(0) << "Resulting new eigenvectors and -values, run " << run << " are:" << std::endl;
     467    DoLog(0) && (Log() << Verbose(0) << "Resulting new eigenvectors and -values, run " << run << " are:" << std::endl);
    459468    BOOST_FOREACH( size_t index, AllIndices) {
    460       Log() << Verbose(0) << *CurrentEigenvectors[index] << " with " << CurrentEigenvalues[index] << std::endl;
     469      DoLog(0) && (Log() << Verbose(0) << *CurrentEigenvectors[index] << " with " << CurrentEigenvalues[index] << std::endl);
    461470    }
    462471    run++;
    463472  }
    464473
    465 
    466474  delete[] ParallelEigenvectorList;
    467475
    468476  CPPUNIT_ASSERT_EQUAL(0,0);
     477}
     478
     479
     480/** Iterative function to generate all power sets of indices of size \a maxelements.
     481 *
     482 * @param SetofSets Container for all sets
     483 * @param CurrentSet pointer to current set in this container
     484 * @param Indices Source set of indices
     485 * @param maxelements number of elements of each set in final SetofSets
     486 * @return true - generation continued, false - current set already had
     487 *         \a maxelements elements
     488 */
     489bool generatePowerSet(
     490    SetofIndexSets &SetofSets,
     491    SetofIndexSets::iterator &CurrentSet,
     492    IndexSet &Indices,
     493    const size_t maxelements)
     494{
     495  if (CurrentSet->size() < maxelements) {
     496    // allocate the needed sets
     497    const size_t size = Indices.size() - CurrentSet->size();
     498    std::vector<std::set<size_t> > SetExpanded;
     499    SetExpanded.reserve(size);
     500
     501    // copy the current set into each
     502    for (size_t i=0;i<size;++i)
     503      SetExpanded.push_back(*CurrentSet);
     504
     505    // expand each set by one index
     506    size_t localindex=0;
     507    BOOST_FOREACH(size_t iter, Indices) {
     508      if (CurrentSet->count(iter) == 0) {
     509        SetExpanded[localindex].insert(iter);
     510        ++localindex;
     511      }
     512    }
     513
     514    // insert set at position of CurrentSet
     515    for (size_t i=0;i<size;++i) {
     516      //DoLog(1) && (Log() << Verbose(1) << "Inserting set #" << i << ": " << SetExpanded[i] << std::endl);
     517      SetofSets.insert(CurrentSet, SetExpanded[i]);
     518    }
     519    SetExpanded.clear();
     520
     521    // and remove the current set
     522    //SetofSets.erase(CurrentSet);
     523    //CurrentSet = SetofSets.begin();
     524
     525    // set iterator to a valid position again
     526    ++CurrentSet;
     527    return true;
     528  } else {
     529    return false;
     530  }
     531}
     532
     533void SubspaceFactorizerUnittest::generatePowerSetTest()
     534{
     535  IndexSet AllIndices;
     536  for (size_t i=0;i<4;++i)
     537    AllIndices.insert(i);
     538
     539  SetofIndexSets SetofSets;
     540  // note that starting off empty set is unstable
     541  IndexSet singleset;
     542  BOOST_FOREACH(size_t iter, AllIndices) {
     543    singleset.insert(iter);
     544    SetofSets.insert(singleset);
     545    singleset.clear();
     546  }
     547  SetofIndexSets::iterator CurrentSet = SetofSets.begin();
     548  while (CurrentSet != SetofSets.end()) {
     549    //DoLog(0) && (Log() << Verbose(0) << "Current set is " << *CurrentSet << std::endl);
     550    if (!generatePowerSet(SetofSets, CurrentSet, AllIndices, 2)) {
     551      // go to next set
     552      ++CurrentSet;
     553    }
     554  }
     555
     556  SetofIndexSets ComparisonSet;
     557  // now follows a very stupid construction
     558  // because we can't use const arrays of some type meaningfully ...
     559  { IndexSet tempSet; tempSet.insert(0); ComparisonSet.insert(tempSet); }
     560  { IndexSet tempSet; tempSet.insert(1); ComparisonSet.insert(tempSet); }
     561  { IndexSet tempSet; tempSet.insert(2); ComparisonSet.insert(tempSet); }
     562  { IndexSet tempSet; tempSet.insert(3); ComparisonSet.insert(tempSet); }
     563
     564  { IndexSet tempSet; tempSet.insert(0); tempSet.insert(1); ComparisonSet.insert(tempSet); }
     565  { IndexSet tempSet; tempSet.insert(0); tempSet.insert(2); ComparisonSet.insert(tempSet); }
     566  { IndexSet tempSet; tempSet.insert(0); tempSet.insert(3); ComparisonSet.insert(tempSet); }
     567
     568  { IndexSet tempSet; tempSet.insert(1); tempSet.insert(2); ComparisonSet.insert(tempSet); }
     569  { IndexSet tempSet; tempSet.insert(1); tempSet.insert(3); ComparisonSet.insert(tempSet); }
     570
     571  { IndexSet tempSet; tempSet.insert(2); tempSet.insert(3); ComparisonSet.insert(tempSet); }
     572
     573  CPPUNIT_ASSERT_EQUAL(SetofSets, ComparisonSet);
     574}
     575
     576bool cmd(double a, double b)
     577{
     578  return a > b;
    469579}
    470580
     
    474584  Eigenspace::eigenvalueset CurrentEigenvalues;
    475585
    476   Log() << Verbose(0) << std::endl << std::endl;
     586  setVerbosity(0);
     587
     588  boost::timer Time_generatingfullspace;
     589  DoLog(0) && (Log() << Verbose(0) << std::endl << std::endl);
    477590  // create the total index set
    478591  IndexSet AllIndices;
     
    480593    AllIndices.insert(i);
    481594  Eigenspace FullSpace(AllIndices, *matrix);
    482   Log() << Verbose(1) << "Generated full space: " << FullSpace << std::endl;
     595  DoLog(1) && (Log() << Verbose(1) << "Generated full space: " << FullSpace << std::endl);
     596  DoLog(0) && (Log() << Verbose(0) << "Full space generation took " << Time_generatingfullspace.elapsed() << " seconds." << std::endl);
    483597
    484598  // generate first set of eigenvectors
     
    492606  }
    493607
    494   Log() << Verbose(1) << "Generating sub spaces ..." << std::endl;
    495   // create all consecutive index subsets for dim 1 to 3
     608  boost::timer Time_generatingsubsets;
     609  DoLog(0) && (Log() << Verbose(0) << "Generating sub sets ..." << std::endl);
     610  SetofIndexSets SetofSets;
     611  // note that starting off empty set is unstable
     612  IndexSet singleset;
     613  BOOST_FOREACH(size_t iter, AllIndices) {
     614    singleset.insert(iter);
     615    SetofSets.insert(singleset);
     616    singleset.clear();
     617  }
     618  SetofIndexSets::iterator CurrentSet = SetofSets.begin();
     619  while (CurrentSet != SetofSets.end()) {
     620    //DoLog(2) && (Log() << Verbose(2) << "Current set is " << *CurrentSet << std::endl);
     621    if (!generatePowerSet(SetofSets, CurrentSet, AllIndices, subspacelimit)) {
     622      // go to next set
     623      ++CurrentSet;
     624    }
     625  }
     626  DoLog(0) && (Log() << Verbose(0) << "Sub set generation took " << Time_generatingsubsets.elapsed() << " seconds." << std::endl);
     627
     628  // create a subspace to each set and and to respective level
     629  boost::timer Time_generatingsubspaces;
     630  DoLog(0) && (Log() << Verbose(0) << "Generating sub spaces ..." << std::endl);
    496631  SubspaceMap Dimension_to_Indexset;
    497   for (size_t dim = 0; dim<3;++dim) {
    498     for (size_t i=0;i<matrixdimension;++i) {
    499       IndexSet *indexset = new IndexSet;
    500       for (size_t j=0;j<dim+1;++j) {
    501         const int value = (i+j) % matrixdimension;
    502         //std::cout << "Putting " << value << " into " << i << "th map " << dim << std::endl;
    503         CPPUNIT_ASSERT_MESSAGE("index "+toString(value)+" already present in "+toString(dim)+"-dim "+toString(i)+"th indexset.", indexset->count(value) == 0);
    504         indexset->insert(value);
    505       }
    506       boost::shared_ptr<Subspace> subspace(new Subspace(*indexset, FullSpace));
    507       Log() << Verbose(1) << "Current subspace is " << *subspace << std::endl;
    508       Dimension_to_Indexset.insert( make_pair(dim, boost::shared_ptr<Subspace>(subspace)) );
    509       delete(indexset);
    510 
    511       // for through next lower subspace list and add to subspaces if contained.
    512       if (dim != 0) {
    513         Log() << Verbose(1) << "Going through subspace list in dim " << dim-1 << "." << std::endl;
     632  BOOST_FOREACH(std::set<size_t> iter, SetofSets) {
     633    boost::shared_ptr<Subspace> subspace(new Subspace(iter, FullSpace));
     634    DoLog(1) && (Log() << Verbose(1) << "Current subspace is " << *subspace << std::endl);
     635    Dimension_to_Indexset.insert( make_pair(iter.size(), boost::shared_ptr<Subspace>(subspace)) );
     636  }
     637
     638  for (size_t dim = 1; dim<=subspacelimit;++dim) {
     639    BOOST_FOREACH( SubspaceMap::value_type subspace, Dimension_to_Indexset.equal_range(dim)) {
     640      if (dim != 0) { // from level 1 and onward
    514641        BOOST_FOREACH( SubspaceMap::value_type entry, Dimension_to_Indexset.equal_range(dim-1)) {
    515           if (subspace->contains(*entry.second)) {
    516             Log() << Verbose(2) << "Adding " << *(entry.second) << " to list of contained subspaces." << std::endl;
    517             subspace->addSubset(entry.second);
     642          if (subspace.second->contains(*entry.second)) {
     643            // if contained then add ...
     644            subspace.second->addSubset(entry.second);
     645            // ... and also its containees as they are all automatically contained as well
     646            BOOST_FOREACH(boost::shared_ptr<Subspace> iter, entry.second->SubIndices) {
     647              subspace.second->addSubset(iter);
     648            }
    518649          }
    519650        }
    520       } else {
    521         Log() << Verbose(2) << "Subspace with dimension of 1 cannot contain other subspaces." << std::endl;
    522       }
    523 
    524      }
    525   }
    526 
     651      }
     652    }
     653  }
     654  DoLog(0) && (Log() << Verbose(0) << "Sub space generation took " << Time_generatingsubspaces.elapsed() << " seconds." << std::endl);
     655
     656  // create a file handle for the eigenvalues
     657  std::ofstream outputvalues("eigenvalues.dat", std::ios_base::trunc);
     658  ASSERT(outputvalues.good(),
     659      "SubspaceFactorizerUnittest::EigenvectorTest() - failed to open eigenvalue file!");
     660  outputvalues << "# iteration ";
     661  BOOST_FOREACH(size_t iter, AllIndices) {
     662    outputvalues << "\teigenvalue" << iter;
     663  }
     664  outputvalues << std::endl;
     665
     666  DoLog(0) && (Log() << Verbose(0) << "Solving ..." << std::endl);
     667  boost::timer Time_solving;
    527668  size_t run=1; // counting iterations
    528669  double threshold = 1.;  // containing threshold value
    529   while ((threshold > 1e-10) && (run < 10)) {
     670  while ((threshold > MYEPSILON) && (run < 20)) {
    530671    // for every dimension
    531     for (size_t dim = 0; dim< subspacelimit;++dim) {
     672    for (size_t dim = 1; dim <= subspacelimit;++dim) {
    532673      // for every index set of this dimension
    533       Log() << Verbose(0) << std::endl << std::endl;
    534       Log() << Verbose(0) << "Current dimension is " << dim << std::endl;
     674      DoLog(1) && (Log() << Verbose(1) << std::endl << std::endl);
     675      DoLog(1) && (Log() << Verbose(1) << "Current dimension is " << dim << std::endl);
    535676      std::pair<SubspaceMap::const_iterator,SubspaceMap::const_iterator> Bounds = Dimension_to_Indexset.equal_range(dim);
    536677      for (SubspaceMap::const_iterator IndexsetIter = Bounds.first;
     
    539680        Subspace& subspace = *(IndexsetIter->second);
    540681        // show the index set
    541         Log() << Verbose(0) << std::endl;
    542         Log() << Verbose(1) << "Current subspace is " << subspace << std::endl;
     682        DoLog(2) && (Log() << Verbose(2) << std::endl);
     683        DoLog(2) && (Log() << Verbose(2) << "Current subspace is " << subspace << std::endl);
    543684
    544685        // solve
     
    550691
    551692    // print list of similar eigenvectors
     693    DoLog(2) && (Log() << Verbose(2) << std::endl);
    552694    BOOST_FOREACH( size_t index, AllIndices) {
    553       Log() << Verbose(2) << "Similar to " << index << "th current eigenvector " << *(CurrentEigenvectors[index]) << " are:" << std::endl;
     695      DoLog(2) && (Log() << Verbose(2) << "Similar to " << index << "th current eigenvector " << *(CurrentEigenvectors[index]) << " are:" << std::endl);
    554696      BOOST_FOREACH( SubspaceMap::value_type iter, Dimension_to_Indexset) {
    555697        const VectorContent & CurrentEV = (iter.second)->getEigenvectorParallelToFullOne(index);
    556698        if (!CurrentEV.IsZero())
    557           Log() << Verbose(2) << CurrentEV << std::endl;
    558       }
    559       Log() << Verbose(2) << std::endl;
     699          Log() << Verbose(2)
     700          << "dim" << iter.first
     701          << ", subspace{" << (iter.second)->getIndices()
     702          << "}: "<<CurrentEV << std::endl;
     703      }
     704      DoLog(2) && (Log() << Verbose(2) << std::endl);
    560705    }
    561706
     
    567712      BOOST_FOREACH( SubspaceMap::value_type iter, Dimension_to_Indexset) {
    568713        const VectorContent CurrentEV = (iter.second)->getEigenvectorParallelToFullOne(index);
    569         *CurrentEigenvectors[index] += CurrentEV * (iter.second)->getEigenvalueOfEigenvectorParallelToFullOne(index);
     714        *CurrentEigenvectors[index] += CurrentEV; // * (iter.second)->getEigenvalueOfEigenvectorParallelToFullOne(index);
    570715        CurrentEigenvalues[index] += (iter.second)->getEigenvalueOfEigenvectorParallelToFullOne(index);
    571716        if (!CurrentEV.IsZero())
     
    573718      }
    574719      *CurrentEigenvectors[index] *= 1./CurrentEigenvalues[index];
    575       CurrentEigenvalues[index] /= (double)count;
     720      //CurrentEigenvalues[index] /= (double)count;
    576721    }
    577722
     
    582727    // orthonormalize
    583728    if (!dontOrthonormalization) {
    584       Log() << Verbose(0) << "Orthonormalizing ... " << std::endl;
     729      DoLog(1) && (Log() << Verbose(1) << "Orthonormalizing ... " << std::endl);
    585730      for (IndexSet::const_iterator firstindex = AllIndices.begin();
    586731          firstindex != AllIndices.end();
     
    607752
    608753    // show new ones
    609     Log() << Verbose(0) << "Resulting new eigenvectors and -values, run " << run << " are:" << std::endl;
     754    DoLog(1) && (Log() << Verbose(1) << "Resulting new eigenvectors and -values, run " << run << " are:" << std::endl);
     755    outputvalues << run;
    610756    BOOST_FOREACH( size_t index, AllIndices) {
    611       Log() << Verbose(0) << *CurrentEigenvectors[index] << " with " << CurrentEigenvalues[index] << std::endl;
    612     }
     757      DoLog(1) && (Log() << Verbose(1) << *CurrentEigenvectors[index] << " with " << CurrentEigenvalues[index] << std::endl);
     758      outputvalues << "\t" << CurrentEigenvalues[index];
     759    }
     760    outputvalues << std::endl;
     761
     762    // and next iteration
     763    DoLog(0) && (Log() << Verbose(0) << "\titeration #" << run << std::endl);
    613764    run++;
    614765  }
    615 
     766  DoLog(0) && (Log() << Verbose(0) << "Solving took " << Time_solving.elapsed() << " seconds." << std::endl);
     767  // show final ones
     768  DoLog(0) && (Log() << Verbose(0) << "Resulting new eigenvectors and -values, run " << run << " are:" << std::endl);
     769  outputvalues << run;
     770  BOOST_FOREACH( size_t index, AllIndices) {
     771    DoLog(0) && (Log() << Verbose(0) << *CurrentEigenvectors[index] << " with " << CurrentEigenvalues[index] << std::endl);
     772    outputvalues << "\t" << CurrentEigenvalues[index];
     773  }
     774  outputvalues << std::endl;
     775  outputvalues.close();
     776
     777  setVerbosity(2);
     778
     779  DoLog(0) && (Log() << Verbose(0) << "Solving full space ..." << std::endl);
     780  boost::timer Time_comparison;
     781  MatrixContent tempFullspaceMatrix = FullSpace.getEigenspaceMatrix();
     782  gsl_vector *eigenvalues = tempFullspaceMatrix.transformToEigenbasis();
     783  tempFullspaceMatrix.sortEigenbasis(eigenvalues);
     784  DoLog(0) && (Log() << Verbose(0) << "full space solution took " << Time_comparison.elapsed() << " seconds." << std::endl);
     785
     786  // compare all
     787  sort(CurrentEigenvalues.begin(),CurrentEigenvalues.end()); //, cmd);
     788  for (size_t i=0;i<eigenvalues->size; ++i) {
     789    CPPUNIT_ASSERT_MESSAGE(toString(i)+"ths eigenvalue differs:"
     790        +toString(CurrentEigenvalues[i])+" != "+toString(gsl_vector_get(eigenvalues,i)),
     791        fabs((CurrentEigenvalues[i] - gsl_vector_get(eigenvalues,i))/CurrentEigenvalues[i]) < 1e-3);
     792  }
    616793
    617794  CPPUNIT_ASSERT_EQUAL(0,0);
  • src/unittests/SubspaceFactorizerUnittest.hpp

    r1fb318 re828c0  
    1515class Subspace;
    1616
     17typedef std::set<std::set<size_t> > SetofIndexSets;
    1718typedef std::set<size_t> IndexSet;
    1819typedef std::multimap< size_t, boost::shared_ptr<Subspace> > SubspaceMap;
     
    2728  CPPUNIT_TEST_SUITE( SubspaceFactorizerUnittest) ;
    2829  CPPUNIT_TEST ( BlockTest );
    29   CPPUNIT_TEST ( EigenvectorTest );
     30  //CPPUNIT_TEST ( EigenvectorTest );
     31  CPPUNIT_TEST ( generatePowerSetTest );
    3032  CPPUNIT_TEST ( SubspaceTest );
    3133  CPPUNIT_TEST_SUITE_END();
     
    3739  void BlockTest();
    3840  void EigenvectorTest();
     41  void generatePowerSetTest();
    3942  void SubspaceTest();
    4043
    41   enum { matrixdimension = 3 };
    42   enum { subspacelimit = 2 };
     44  enum { matrixdimension = 4 };
     45  enum { subspacelimit = 3 };
    4346private:
    4447
Note: See TracChangeset for help on using the changeset viewer.