[9c5296] | 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 | * Subspace.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Nov 22, 2010
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
| 20 | #include "Helpers/MemDebug.hpp"
|
---|
| 21 |
|
---|
| 22 | #include <boost/shared_ptr.hpp>
|
---|
| 23 | #include <boost/foreach.hpp>
|
---|
| 24 |
|
---|
| 25 | #include "Helpers/Assert.hpp"
|
---|
| 26 | #include "Helpers/Log.hpp"
|
---|
| 27 | #include "Helpers/Verbose.hpp"
|
---|
| 28 |
|
---|
| 29 | #include "Eigenspace.hpp"
|
---|
| 30 | #include "Subspace.hpp"
|
---|
| 31 |
|
---|
| 32 | /** Constructor for class Subspace.
|
---|
| 33 | *
|
---|
| 34 | * @param _s indices that make up this subspace
|
---|
| 35 | * @param _FullSpace reference to the full eigenspace
|
---|
| 36 | */
|
---|
| 37 | Subspace::Subspace(indexset & _s, Eigenspace &_FullSpace) :
|
---|
| 38 | Eigenspace(_s),
|
---|
| 39 | ProjectToSubspace(_s.size(), _FullSpace.getIndices().size()),
|
---|
| 40 | ProjectFromSubspace(_FullSpace.getIndices().size(), _s.size()),
|
---|
| 41 | FullSpace(_FullSpace)
|
---|
| 42 | {}
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | /** Destructor for class Subspace.
|
---|
| 46 | *
|
---|
| 47 | */
|
---|
| 48 | Subspace::~Subspace()
|
---|
| 49 | {}
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | /** Diagonalizes the subspace matrix.
|
---|
| 53 | *
|
---|
| 54 | * @param fullmatrix full matrix to project into subspace and solve
|
---|
| 55 | */
|
---|
| 56 | void Subspace::calculateEigenSubspace()
|
---|
| 57 | {
|
---|
| 58 | getSubspacematrixFromBigmatrix(FullSpace.getEigenspaceMatrix());
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | /** Projects a given full matrix down into the subspace of this class.
|
---|
| 63 | *
|
---|
| 64 | * @param bigmatrix full matrix to project into subspace
|
---|
| 65 | */
|
---|
| 66 | void Subspace::getSubspacematrixFromBigmatrix(const MatrixContent & bigmatrix)
|
---|
| 67 | {
|
---|
| 68 | // check whether subsystem is big enough for both index sets
|
---|
| 69 | ASSERT(Indices.size() <= bigmatrix.getRows(),
|
---|
| 70 | "embedEigenspaceMatrix() - bigmatrix has less rows "+toString(bigmatrix.getRows())
|
---|
| 71 | +" than needed by index set "
|
---|
| 72 | +toString(Indices.size())+"!");
|
---|
| 73 | ASSERT(Indices.size() <= bigmatrix.getColumns(),
|
---|
| 74 | "embedEigenspaceMatrix() - bigmatrix has less columns "+toString(bigmatrix.getColumns())
|
---|
| 75 | +" than needed by index set "
|
---|
| 76 | +toString(Indices.size())+"!");
|
---|
| 77 |
|
---|
| 78 | // construct small matrix
|
---|
| 79 | MatrixContent *subsystem = new MatrixContent(Indices.size(), Indices.size());
|
---|
| 80 | size_t localrow = 0; // local row indices for the subsystem
|
---|
| 81 | size_t localcolumn = 0;
|
---|
| 82 | BOOST_FOREACH( size_t rowindex, Indices) {
|
---|
| 83 | localcolumn = 0;
|
---|
| 84 | BOOST_FOREACH( size_t columnindex, Indices) {
|
---|
| 85 | ASSERT((rowindex < bigmatrix.getRows()) && (columnindex < bigmatrix.getColumns()),
|
---|
| 86 | "current index pair ("
|
---|
| 87 | +toString(rowindex)+","+toString(columnindex)
|
---|
| 88 | +") is out of bounds of bigmatrix ("
|
---|
| 89 | +toString(bigmatrix.getRows())+","+toString(bigmatrix.getColumns())
|
---|
| 90 | +")");
|
---|
| 91 | subsystem->at(localrow,localcolumn) = (*Eigenvectors[rowindex]) * (bigmatrix * (*Eigenvectors[columnindex]));
|
---|
| 92 | localcolumn++;
|
---|
| 93 | }
|
---|
| 94 | localrow++;
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | void Subspace::correctEigenvectorsFromSubIndices()
|
---|
| 100 | {
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | /** Creates the inverse of LocalToGlobal.
|
---|
| 105 | * Mapping is one-to-one and onto, hence it's simply turning around the map.
|
---|
| 106 | */
|
---|
| 107 | void Subspace::invertLocalToGlobalMapping()
|
---|
| 108 | {
|
---|
| 109 | GlobalToLocal.clear();
|
---|
| 110 | for (mapping::const_iterator iter = LocalToGlobal.begin();
|
---|
| 111 | iter != LocalToGlobal.end();
|
---|
| 112 | ++iter) {
|
---|
| 113 | GlobalToLocal.insert( make_pair(iter->second, iter->first) );
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 |
|
---|
| 118 | /** Project calculated Eigenvectors into full space.
|
---|
| 119 | *
|
---|
| 120 | * @return set of projected eigenvectors.
|
---|
| 121 | */
|
---|
| 122 | Eigenspace::eigenvectorset Subspace::getEigenvectorsInFullSpace()
|
---|
| 123 | {
|
---|
| 124 | // check whether bigmatrix is at least as big as EigenspaceMatrix
|
---|
| 125 | ASSERT(Eigenvectors.size() > 0,
|
---|
| 126 | "embedEigenspaceMatrix() - no Eigenvectors given!");
|
---|
| 127 | ASSERT(EigenspaceMatrix.getRows() <= Eigenvectors[0]->getDimension(),
|
---|
| 128 | "embedEigenspaceMatrix() - EigenspaceMatrix has more rows "
|
---|
| 129 | +toString(EigenspaceMatrix.getRows())+" than eigenvectors "
|
---|
| 130 | +toString(Eigenvectors[0]->getDimension())+"!");
|
---|
| 131 | ASSERT(EigenspaceMatrix.getColumns() <= Eigenvectors.size(),
|
---|
| 132 | "embedEigenspaceMatrix() - EigenspaceMatrix has more columns "
|
---|
| 133 | +toString(EigenspaceMatrix.getColumns())+" than eigenvectors "
|
---|
| 134 | +toString(Eigenvectors.size())+"!");
|
---|
| 135 | // check whether EigenspaceMatrix is big enough for both index sets
|
---|
| 136 | ASSERT(EigenspaceMatrix.getColumns() == EigenspaceMatrix.getRows(),
|
---|
| 137 | "embedEigenspaceMatrix() - EigenspaceMatrix is not square "
|
---|
| 138 | +toString(EigenspaceMatrix.getRows())+" than needed by index set "
|
---|
| 139 | +toString(EigenspaceMatrix.getColumns())+"!");
|
---|
| 140 | ASSERT(Indices.size() == EigenspaceMatrix.getColumns(),
|
---|
| 141 | "embedEigenspaceMatrix() - EigenspaceMatrix has not the same number of columns "
|
---|
| 142 | +toString(EigenspaceMatrix.getColumns())+" compared to the index set "
|
---|
| 143 | +toString(Indices.size())+"!");
|
---|
| 144 |
|
---|
| 145 | // construct intermediate matrix
|
---|
| 146 | MatrixContent *intermediatematrix = new MatrixContent(Eigenvectors[0]->getDimension(), Indices.size());
|
---|
| 147 | size_t localcolumn = 0;
|
---|
| 148 | BOOST_FOREACH(size_t columnindex, Indices) {
|
---|
| 149 | // create two vectors from each row and copy assign them
|
---|
| 150 | boost::shared_ptr<VectorContent> srceigenvector(Eigenvectors[columnindex]);
|
---|
| 151 | boost::shared_ptr<VectorContent> desteigenvector(intermediatematrix->getColumnVector(localcolumn));
|
---|
| 152 | *desteigenvector = *srceigenvector;
|
---|
| 153 | localcolumn++;
|
---|
| 154 | }
|
---|
| 155 | // matrix product with eigenbasis EigenspaceMatrix matrix
|
---|
| 156 | *intermediatematrix *= EigenspaceMatrix;
|
---|
| 157 |
|
---|
| 158 | // and place at right columns into bigmatrix
|
---|
| 159 | MatrixContent *bigmatrix = new MatrixContent(Eigenvectors[0]->getDimension(), Eigenvectors.size());
|
---|
| 160 | bigmatrix->setZero();
|
---|
| 161 | localcolumn = 0;
|
---|
| 162 | BOOST_FOREACH(size_t columnindex, Indices) {
|
---|
| 163 | // create two vectors from each row and copy assign them
|
---|
| 164 | boost::shared_ptr<VectorContent> srceigenvector(intermediatematrix->getColumnVector(localcolumn));
|
---|
| 165 | boost::shared_ptr<VectorContent> desteigenvector(bigmatrix->getColumnVector(columnindex));
|
---|
| 166 | *desteigenvector = *srceigenvector;
|
---|
| 167 | localcolumn++;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | return Eigenvectors;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 |
|
---|
| 174 | /** Remove a subset of indices from the SubIndices.
|
---|
| 175 | *
|
---|
| 176 | * @param _s subset to remove
|
---|
| 177 | * @return true - removed, false - not found
|
---|
| 178 | */
|
---|
| 179 | bool Subspace::removeSubset(boost::shared_ptr<Subspace> &_s)
|
---|
| 180 | {
|
---|
| 181 | if (SubIndices.count(_s) != 0) {
|
---|
| 182 | SubIndices.erase(_s);
|
---|
| 183 | return true;
|
---|
| 184 | } else {
|
---|
| 185 | return false;
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | /** Add a subspace set to SubIndices.
|
---|
| 190 | *
|
---|
| 191 | * @param _s subset to add
|
---|
| 192 | * @return true - not present before, false - has already been present
|
---|
| 193 | */
|
---|
| 194 | bool Subspace::addSubset(boost::shared_ptr<Subspace> & _s)
|
---|
| 195 | {
|
---|
| 196 | if (SubIndices.count(_s) != 0)
|
---|
| 197 | return false;
|
---|
| 198 | else {
|
---|
| 199 | SubIndices.insert(_s);
|
---|
| 200 | return true;
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 |
|
---|
| 205 | /** Simply a getter for Eigenvectors, as they are stored as subspace eigenvectors.
|
---|
| 206 | *
|
---|
| 207 | * @return set of eigenvectors in subspace
|
---|
| 208 | */
|
---|
| 209 | Eigenspace::eigenvectorset Subspace::getEigenvectorsInSubspace()
|
---|
| 210 | {
|
---|
| 211 | return Eigenvectors;
|
---|
| 212 | }
|
---|
| 213 |
|
---|