| [7d92f1] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| [5aaa43] | 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| [7d92f1] | 6 |  * 
 | 
|---|
 | 7 |  *
 | 
|---|
 | 8 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 9 |  *
 | 
|---|
 | 10 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 11 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 12 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 13 |  *    (at your option) any later version.
 | 
|---|
 | 14 |  *
 | 
|---|
 | 15 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 16 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 17 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 18 |  *    GNU General Public License for more details.
 | 
|---|
 | 19 |  *
 | 
|---|
 | 20 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 21 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
 | 22 |  */
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 | /*
 | 
|---|
 | 25 |  * IndexedVectors.cpp
 | 
|---|
 | 26 |  *
 | 
|---|
 | 27 |  *  Created on: 29.07.2012
 | 
|---|
 | 28 |  *      Author: heber
 | 
|---|
 | 29 |  */
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | // include config.h
 | 
|---|
 | 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 33 | #include <config.h>
 | 
|---|
 | 34 | #endif
 | 
|---|
 | 35 | 
 | 
|---|
| [9eb71b3] | 36 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [7d92f1] | 37 | 
 | 
|---|
| [fbf143] | 38 | #include "Fragmentation/Summation/SetValues/IndexedVectors.hpp"
 | 
|---|
| [7d92f1] | 39 | 
 | 
|---|
 | 40 | #include <iostream>
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 43 | 
 | 
|---|
 | 44 | // static member variables
 | 
|---|
 | 45 | const size_t IndexedVectors::FixedSize = 3;
 | 
|---|
 | 46 | const IndexedVectors::vector_t IndexedVectors::nullvector( FixedSize, 0.);
 | 
|---|
 | 47 | 
 | 
|---|
 | 48 | IndexedVectors::IndexedVectors(const indices_t &_indices, const vectors_t &_vectors)
 | 
|---|
 | 49 | {
 | 
|---|
 | 50 |   ASSERT(_indices.size() == _vectors.size(),
 | 
|---|
| [0c8989] | 51 |       "IndexedVectors::IndexedVectors() - vector of indices and vectors don't match in size: "
 | 
|---|
 | 52 |       +toString(_indices.size())+" != "+toString(_vectors.size()));
 | 
|---|
| [7d92f1] | 53 |   indices_t::const_iterator indexiter = _indices.begin();
 | 
|---|
 | 54 |   vectors_t::const_iterator vectoriter = _vectors.begin();
 | 
|---|
 | 55 |   for (; vectoriter != _vectors.end(); ++indexiter, ++vectoriter) {
 | 
|---|
 | 56 |     ASSERT( vectoriter->size() == FixedSize,
 | 
|---|
 | 57 |         "IndexedVectors::IndexedVectors() - vector to instance "
 | 
|---|
 | 58 |         +toString(*indexiter)+" has size "
 | 
|---|
 | 59 |         +toString(vectoriter->size())+" different to FixedSize "
 | 
|---|
 | 60 |         +toString(FixedSize)+".");
 | 
|---|
| [a67a04] | 61 |     if (*indexiter != (size_t)DropIndex) { // skip all force vectors associated to -1
 | 
|---|
| [7d92f1] | 62 | #ifndef NDEBUG
 | 
|---|
| [a67a04] | 63 |       std::pair<indexedvectors_t::iterator, bool> inserter =
 | 
|---|
| [7d92f1] | 64 | #endif
 | 
|---|
| [a67a04] | 65 |       vectors.insert( std::make_pair( *indexiter, *vectoriter) );
 | 
|---|
 | 66 |       ASSERT( inserter.second,
 | 
|---|
 | 67 |           "IndexedVectors::IndexedVectors() - index "
 | 
|---|
 | 68 |           +toString(inserter.first->first)+" already present with vector "
 | 
|---|
 | 69 |           +toString(inserter.first->second)+".");
 | 
|---|
 | 70 |     }
 | 
|---|
| [7d92f1] | 71 |   }
 | 
|---|
 | 72 | }
 | 
|---|
 | 73 | 
 | 
|---|
 | 74 | IndexedVectors& IndexedVectors::operator=(const IndexedVectors &other)
 | 
|---|
 | 75 | {
 | 
|---|
 | 76 |   // check for self-assignment
 | 
|---|
 | 77 |   if (this != &other) {
 | 
|---|
 | 78 |     vectors.clear();
 | 
|---|
 | 79 |     vectors = other.vectors;
 | 
|---|
 | 80 |   }
 | 
|---|
 | 81 |   return *this;
 | 
|---|
 | 82 | }
 | 
|---|
 | 83 | 
 | 
|---|
 | 84 | void IndexedVectors::superposeOtherIndexedVectors(const IndexedVectors &other, const double prefactor)
 | 
|---|
 | 85 | {
 | 
|---|
 | 86 |   for (indexedvectors_t::const_iterator otheriter = other.vectors.begin();
 | 
|---|
 | 87 |       otheriter != other.vectors.end(); ++otheriter) {
 | 
|---|
 | 88 |     indexedvectors_t::iterator iter = vectors.find(otheriter->first);
 | 
|---|
 | 89 |     if (iter == vectors.end()) {
 | 
|---|
 | 90 |       // index does not exist
 | 
|---|
 | 91 |       std::pair<indexedvectors_t::iterator, bool> inserter =
 | 
|---|
 | 92 |           vectors.insert( std::make_pair( otheriter->first, nullvector) );
 | 
|---|
 | 93 |       ASSERT( inserter.second,
 | 
|---|
 | 94 |           "IndexedVectors::superposeOtherIndexedVectors() - index is present though unfound before?");
 | 
|---|
 | 95 |       iter = inserter.first;
 | 
|---|
 | 96 |     }
 | 
|---|
 | 97 |     // now simply vector to index from other instance add with prefactor
 | 
|---|
 | 98 |     vector_t::iterator vectoriter = iter->second.begin();
 | 
|---|
 | 99 |     vector_t::const_iterator othervectoriter = otheriter->second.begin();
 | 
|---|
 | 100 |     for (;vectoriter != iter->second.end(); ++vectoriter, ++othervectoriter) {
 | 
|---|
 | 101 |       *vectoriter += prefactor * (*othervectoriter);
 | 
|---|
 | 102 |     }
 | 
|---|
 | 103 |   }
 | 
|---|
 | 104 | }
 | 
|---|
 | 105 | 
 | 
|---|
| [955051] | 106 | bool IndexedVectors::operator==(const IndexedVectors& other) const
 | 
|---|
 | 107 | {
 | 
|---|
 | 108 |   bool status = vectors == other.vectors;
 | 
|---|
 | 109 |   return status;
 | 
|---|
 | 110 | }
 | 
|---|
 | 111 | 
 | 
|---|
| [7d92f1] | 112 | std::ostream & operator<<(std::ostream &ost, const IndexedVectors &other)
 | 
|---|
 | 113 | {
 | 
|---|
 | 114 |   for (IndexedVectors::indexedvectors_t::const_iterator iter = other.vectors.begin();
 | 
|---|
 | 115 |       iter != other.vectors.end(); ++iter)
 | 
|---|
 | 116 |     ost << "(" << iter->first << "," << iter->second << ") ";
 | 
|---|
 | 117 |   return ost;
 | 
|---|
 | 118 | }
 | 
|---|
| [beb16e] | 119 | 
 | 
|---|
 | 120 | template<> IndexedVectors ZeroInstance<IndexedVectors>()
 | 
|---|
 | 121 | {
 | 
|---|
 | 122 |   IndexedVectors returnvalue;
 | 
|---|
 | 123 |   return returnvalue;
 | 
|---|
 | 124 | }
 | 
|---|