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:
1fb318
Parents:
7d059d
git-author:
Frederik Heber <heber@…> (11/22/10 18:00:17)
git-committer:
Frederik Heber <heber@…> (12/04/10 23:56:28)
Message:

SubspaceFactorizerUnittest::SubspaceTest() - fully working compared to EigenvectorTest().

  • we also have an iteration and the results are exactly the same for a 3x3 matrix at run 2 and 9.
  • classes Eigenspace and Subspace are thus for now fully working.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/LinearAlgebra/Subspace.cpp

    r7d059d r286af5f  
    2525#include "Helpers/Assert.hpp"
    2626#include "Helpers/Log.hpp"
     27#include "Helpers/toString.hpp"
    2728#include "Helpers/Verbose.hpp"
    2829
    2930#include "Eigenspace.hpp"
    3031#include "Subspace.hpp"
     32
    3133
    3234/** Constructor for class Subspace.
     
    3941  ProjectToSubspace(_FullSpace.getIndices().size(), _s.size()),
    4042  ProjectFromSubspace(_s.size(), _FullSpace.getIndices().size()),
    41   FullSpace(_FullSpace)
    42 {
     43  FullSpace(_FullSpace),
     44  ZeroVector(_FullSpace.getIndices().size())
     45{
     46  // TODO: away with both of this when calculateEigenSubspace() works
    4347  // create projection matrices
    4448  createProjectionMatrices();
     
    6266void Subspace::calculateEigenSubspace()
    6367{
    64   getSubspacematrixFromBigmatrix(FullSpace.getEigenspaceMatrix());
     68  // project down
     69  createProjectionMatrices();
     70  // remove subsubspace directions
     71  correctEigenvectorsFromSubIndices();
     72  // solve
     73  projectFullSpaceMatrixToSubspace();
     74  EigenvectorMatrix = EigenspaceMatrix;
     75  VectorContent *EigenvalueVector = new VectorContent(EigenvectorMatrix.transformToEigenbasis());
     76  Eigenvalues.clear();
     77  for(size_t i = 0; i< EigenvalueVector->getDimension(); ++i) {
     78    Eigenvalues.push_back( EigenvalueVector->at(i) );
     79  }
     80  delete EigenvalueVector;
     81  Log() << Verbose(2) << "Eigenvector matrix is " << EigenvectorMatrix << std::endl;
     82  Log() << Verbose(2) << "Eigenvalues are " << Eigenvalues << std::endl;
     83  // create mapping
     84  createLocalMapping();
    6585}
    6686
     
    126146 * @return set of projected eigenvectors.
    127147 */
    128 Eigenspace::eigenvectorset Subspace::getEigenvectorsInFullSpace()
     148const Eigenspace::eigenvectorset & Subspace::getEigenvectorsInFullSpace()
    129149{
    130150  // check whether bigmatrix is at least as big as EigenspaceMatrix
     
    213233 * @return set of eigenvectors in subspace
    214234 */
    215 Eigenspace::eigenvectorset Subspace::getEigenvectorsInSubspace()
    216 {
    217   return Eigenvectors;
     235const MatrixContent & Subspace::getEigenvectorMatrixInFullSpace()
     236{
     237  // check whether bigmatrix is at least as big as EigenspaceMatrix
     238  ASSERT(EigenspaceMatrix.getRows() <= Eigenvectors[0]->getDimension(),
     239      "embedEigenspaceMatrix() - EigenspaceMatrix has more rows "
     240      +toString(EigenspaceMatrix.getRows())+" than eigenvectors "
     241      +toString(Eigenvectors[0]->getDimension())+"!");
     242  ASSERT(EigenspaceMatrix.getColumns() <= Eigenvectors.size(),
     243      "embedEigenspaceMatrix() - EigenspaceMatrix has more columns "
     244      +toString(EigenspaceMatrix.getColumns())+" than eigenvectors "
     245      +toString(Eigenvectors.size())+"!");
     246  // check whether EigenspaceMatrix is big enough for both index sets
     247  ASSERT(EigenspaceMatrix.getColumns() == EigenspaceMatrix.getRows(),
     248      "embedEigenspaceMatrix() - EigenspaceMatrix is not square "
     249      +toString(EigenspaceMatrix.getRows())+" than needed by index set "
     250      +toString(EigenspaceMatrix.getColumns())+"!");
     251  ASSERT(Indices.size() == EigenspaceMatrix.getColumns(),
     252      "embedEigenspaceMatrix() - EigenspaceMatrix has not the same number of columns "
     253      +toString(EigenspaceMatrix.getColumns())+" compared to the index set "
     254      +toString(Indices.size())+"!");
     255
     256  // 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  }
     280
     281  return *bigmatrix;
    218282}
    219283
     
    258322  Log() << Verbose(2) << "EigenspaceMatrix matrix is " << EigenspaceMatrix << std::endl;
    259323}
     324
     325
     326/** We associate local Eigenvectors with ones from FullSpace by a paralellity
     327 * criterion.
     328 */
     329void Subspace::createLocalMapping()
     330{
     331  // first LocalToGlobal
     332  LocalToGlobal.clear();
     333  IndexSet CorrespondenceList(Indices); // is to ensure one-to-one mapping
     334
     335  for (size_t localindex = 0; localindex< Indices.size(); ++localindex) {
     336    boost::shared_ptr<VectorContent> &CurrentEigenvector = Eigenvectors[localindex];
     337    Log() << Verbose(1) << "Current Eigenvector is " << *CurrentEigenvector << std::endl;
     338
     339    // (for now settle with the one we are most parallel to)
     340    size_t mostparallel_index = FullSpace.getIndices().size();
     341    double mostparallel_scalarproduct = 0.;
     342    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;
     346      if (fabs(scalarproduct) > fabs(mostparallel_scalarproduct)) {
     347        mostparallel_scalarproduct = scalarproduct;
     348        mostparallel_index = indexiter;
     349      }
     350    }
     351    // and make the assignment if we found one
     352    if (mostparallel_index != FullSpace.getIndices().size()) {
     353      // put into std::list for later use
     354      // invert if pointing in negative direction
     355      if (mostparallel_scalarproduct < 0) {
     356        *CurrentEigenvector *= -1.;
     357        Log() << Verbose(1) << "Associating (inverted) " << *CurrentEigenvector << " with " << mostparallel_index << "th's fullspace eigenvector." << std::endl;
     358      } else {
     359        Log() << Verbose(1) << "Associating " << *CurrentEigenvector << " with " << mostparallel_index << "th's fullspace eigenvector." << std::endl;
     360      }
     361      ASSERT( LocalToGlobal.count(localindex) == 0,
     362          "Subspace::createLocalMapping() - localindex "+toString(localindex)+" already assigned to "
     363          +toString(LocalToGlobal[localindex])+" !=? "+toString(mostparallel_index)+".");
     364      LocalToGlobal.insert( make_pair(localindex, mostparallel_index) );
     365      CorrespondenceList.erase(mostparallel_index);
     366    }
     367  }
     368
     369  // then invert mapping
     370  GlobalToLocal.clear();
     371  BOOST_FOREACH(mapping::value_type iter, LocalToGlobal) {
     372    ASSERT(GlobalToLocal.count(iter.second) == 0,
     373        "Subspace::createLocalMapping() - LocalToGlobal is not bijective, key "
     374        +toString(iter.second)+" present more than once!");
     375    GlobalToLocal.insert( make_pair(iter.second, iter.first) );
     376  }
     377}
     378
     379
     380/** Get the local eigenvector that is most parallel to the \a i'th full one.
     381 * We just the internal mapping Subspace::GlobalToLocal.
     382 * @param i index of global eigenvector
     383 * @return local eigenvector, most parallel
     384 */
     385const VectorContent Subspace::getEigenvectorParallelToFullOne(size_t i)
     386{
     387  if (GlobalToLocal.count(i) == 0) {
     388    return ZeroVector;
     389  } else {
     390    return ProjectToSubspace*(*Eigenvectors[GlobalToLocal[i]]);
     391  }
     392}
     393
     394
     395/** Get the local eigenvalue of the eigenvector that is most parallel to the
     396 * \a i'th full one.
     397 * We just the internal mapping Subspace::GlobalToLocal.
     398 * @param i index of global eigenvector
     399 * @return eigenvalue of local eigenvector, most parallel
     400 */
     401const double Subspace::getEigenvalueOfEigenvectorParallelToFullOne(size_t i)
     402{
     403  if (GlobalToLocal.count(i) == 0) {
     404    return 0.;
     405  } else {
     406    return Eigenvalues[GlobalToLocal[i]];
     407  }
     408}
Note: See TracChangeset for help on using the changeset viewer.