| [8f6e2a] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2012 University of Bonn. All rights reserved. | 
|---|
| [94d5ac6] | 5 | * | 
|---|
|  | 6 | * | 
|---|
|  | 7 | *   This file is part of MoleCuilder. | 
|---|
|  | 8 | * | 
|---|
|  | 9 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
|  | 10 | *    it under the terms of the GNU General Public License as published by | 
|---|
|  | 11 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
|  | 12 | *    (at your option) any later version. | 
|---|
|  | 13 | * | 
|---|
|  | 14 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
|  | 15 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 16 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 17 | *    GNU General Public License for more details. | 
|---|
|  | 18 | * | 
|---|
|  | 19 | *    You should have received a copy of the GNU General Public License | 
|---|
|  | 20 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| [8f6e2a] | 21 | */ | 
|---|
|  | 22 |  | 
|---|
|  | 23 | /* | 
|---|
|  | 24 | * Cluster.cpp | 
|---|
|  | 25 | * | 
|---|
|  | 26 | *  Created on: Jan 16, 2012 | 
|---|
|  | 27 | *      Author: heber | 
|---|
|  | 28 | */ | 
|---|
|  | 29 |  | 
|---|
|  | 30 |  | 
|---|
|  | 31 | // include config.h | 
|---|
|  | 32 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 33 | #include <config.h> | 
|---|
|  | 34 | #endif | 
|---|
|  | 35 |  | 
|---|
|  | 36 | #include "CodePatterns/MemDebug.hpp" | 
|---|
|  | 37 |  | 
|---|
|  | 38 | #include <algorithm> | 
|---|
|  | 39 | #include <boost/bind.hpp> | 
|---|
|  | 40 | #include <boost/foreach.hpp> | 
|---|
|  | 41 |  | 
|---|
|  | 42 | #include "Cluster.hpp" | 
|---|
|  | 43 |  | 
|---|
|  | 44 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 45 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 46 |  | 
|---|
|  | 47 | #include "Atom/atom.hpp" | 
|---|
|  | 48 | #include "Descriptors/AtomIdDescriptor.hpp" | 
|---|
|  | 49 | #include "LinearAlgebra/RealSpaceMatrix.hpp" | 
|---|
|  | 50 | #include "LinearAlgebra/Vector.hpp" | 
|---|
|  | 51 | #include "Shapes/ShapeOps.hpp" | 
|---|
|  | 52 | #include "World.hpp" | 
|---|
|  | 53 |  | 
|---|
|  | 54 | /** Constructor for class Cluster. | 
|---|
|  | 55 | * | 
|---|
|  | 56 | * @param _s Shape of this Cluster | 
|---|
|  | 57 | */ | 
|---|
|  | 58 | Cluster::Cluster(const Shape & _s) : | 
|---|
|  | 59 | s(_s) | 
|---|
|  | 60 | {} | 
|---|
|  | 61 |  | 
|---|
|  | 62 | /** Copy Constructor for class Cluster. | 
|---|
|  | 63 | * | 
|---|
|  | 64 | * Here, we do not check whether we atomds reside in the Shape or not, as | 
|---|
|  | 65 | * this should have been validated in the instance to copy \a _cluster. | 
|---|
|  | 66 | * | 
|---|
|  | 67 | * @param _cluster instance to copy | 
|---|
|  | 68 | */ | 
|---|
|  | 69 | Cluster::Cluster(const Cluster & _cluster) : | 
|---|
|  | 70 | atoms(_cluster.atoms), | 
|---|
|  | 71 | s(_cluster.s) | 
|---|
|  | 72 | {} | 
|---|
|  | 73 |  | 
|---|
|  | 74 | /** Constructor for class Cluster. | 
|---|
|  | 75 | * | 
|---|
|  | 76 | * @param _atoms list of atoms to place in this cluster | 
|---|
|  | 77 | * @param _s Shape of this Cluster | 
|---|
|  | 78 | */ | 
|---|
|  | 79 | Cluster::Cluster(const atomIdSet & _atoms, const Shape & _s) : | 
|---|
|  | 80 | s(_s) | 
|---|
|  | 81 | { | 
|---|
|  | 82 | // make sure only those atoms are in Cluster that are also inside its Shape | 
|---|
|  | 83 | std::vector<atomId_t> tempIds(_atoms.size(), (size_t)-1); | 
|---|
|  | 84 | std::vector<atomId_t>::iterator iter = | 
|---|
|  | 85 | std::remove_copy_if( _atoms.begin(), _atoms.end(), tempIds.begin(), | 
|---|
|  | 86 | !boost::bind(&Cluster::IsInShape, this, _1) ); | 
|---|
|  | 87 | tempIds.erase( iter, tempIds.end() ); | 
|---|
|  | 88 | ASSERT( tempIds.size() == _atoms.size(), | 
|---|
|  | 89 | "Cluster::Cluster() - at least one atom is not inside the Shape."); | 
|---|
|  | 90 | atoms.insert( tempIds.begin(), tempIds.end() ); | 
|---|
|  | 91 | LOG(1, "INFO: New cluster has " << atoms.size() << " atoms."); | 
|---|
|  | 92 | } | 
|---|
|  | 93 |  | 
|---|
|  | 94 |  | 
|---|
|  | 95 | /** Destructor for class Cluster. | 
|---|
|  | 96 | * | 
|---|
|  | 97 | */ | 
|---|
|  | 98 | Cluster::~Cluster() | 
|---|
|  | 99 | {} | 
|---|
|  | 100 |  | 
|---|
|  | 101 | /** Inserts an atomic by its \a id into the Cluster. | 
|---|
|  | 102 | * | 
|---|
|  | 103 | * We check whether the atom is inside the given Shape \a s. | 
|---|
|  | 104 | * | 
|---|
|  | 105 | * @param id id to insert | 
|---|
|  | 106 | */ | 
|---|
|  | 107 | void Cluster::insert(const atomId_t id) | 
|---|
|  | 108 | { | 
|---|
|  | 109 | const bool status = IsInShape(id); | 
|---|
|  | 110 | ASSERT(status, | 
|---|
|  | 111 | "Cluster::insert() - atomic id "+toString(id)+" is not contained in Shape."); | 
|---|
|  | 112 | if (status) { | 
|---|
|  | 113 | std::pair<atomIdSet::iterator, bool> inserter = atoms.insert(id); | 
|---|
|  | 114 | ASSERT(inserter.second, | 
|---|
|  | 115 | "Cluster::insert() - atomic id "+toString(id)+" is already present."); | 
|---|
|  | 116 | } | 
|---|
|  | 117 | } | 
|---|
|  | 118 |  | 
|---|
|  | 119 | /** Remove atom by its \a id from the cluster. | 
|---|
|  | 120 | * | 
|---|
|  | 121 | * @param id atom to remove | 
|---|
|  | 122 | */ | 
|---|
|  | 123 | void Cluster::erase(const atomId_t id) | 
|---|
|  | 124 | { | 
|---|
|  | 125 | atomIdSet::iterator iter = atoms.find(id); | 
|---|
|  | 126 | ASSERT(iter != atoms.end(), | 
|---|
|  | 127 | "Cluster::erase() - atomic id "+toString(id)+" unknown in this Cluster."); | 
|---|
|  | 128 | if (iter != atoms.end()) | 
|---|
|  | 129 | atoms.erase(iter); | 
|---|
|  | 130 | } | 
|---|
|  | 131 |  | 
|---|
|  | 132 | /** Checks whether a given atom is within the shape \a s. | 
|---|
|  | 133 | * | 
|---|
|  | 134 | * @param id atomic id to check | 
|---|
|  | 135 | * @return true - is in Shape, false - is not contained (or does not exist) | 
|---|
|  | 136 | */ | 
|---|
|  | 137 | bool Cluster::IsInShape(const atomId_t id) const | 
|---|
|  | 138 | { | 
|---|
|  | 139 | const atom * const _atom = getAtomById(id); | 
|---|
|  | 140 | if (_atom != NULL) | 
|---|
|  | 141 | return s.isInside(_atom->getPosition()); | 
|---|
|  | 142 | else | 
|---|
|  | 143 | return false; | 
|---|
|  | 144 | } | 
|---|
|  | 145 |  | 
|---|
|  | 146 | /** Helper function for looking up atomic reference by its id. | 
|---|
|  | 147 | * | 
|---|
|  | 148 | * @param id id to look up | 
|---|
|  | 149 | * @return reference to atom with this id | 
|---|
|  | 150 | */ | 
|---|
|  | 151 | atom * const Cluster::getAtomById(const atomId_t id) const | 
|---|
|  | 152 | { | 
|---|
|  | 153 | atom * const _atom = World::getInstance().getAtom(AtomById(id)); | 
|---|
|  | 154 | ASSERT(_atom != NULL, | 
|---|
|  | 155 | "Cluster::getAtomById() - id "+toString(id)+" is unknown to World."); | 
|---|
|  | 156 | return _atom; | 
|---|
|  | 157 | } | 
|---|
|  | 158 |  | 
|---|
|  | 159 | bool isNullAtom(const atom* _atom) { | 
|---|
|  | 160 | return _atom == NULL; | 
|---|
|  | 161 | } | 
|---|
|  | 162 |  | 
|---|
|  | 163 | /** Getter for the underlying true atoms refs. | 
|---|
|  | 164 | * | 
|---|
|  | 165 | * @return AtomVector filled with looked-up atom references | 
|---|
|  | 166 | */ | 
|---|
|  | 167 | Cluster::AtomVector Cluster::getAtomRefs() const | 
|---|
|  | 168 | { | 
|---|
|  | 169 | AtomVector atomVector; | 
|---|
|  | 170 | atomVector.reserve(atoms.size()); | 
|---|
|  | 171 | BOOST_FOREACH(atomId_t _id, atoms) { | 
|---|
|  | 172 | atom * const _atom = World::getInstance().getAtom(AtomById(_id)); | 
|---|
|  | 173 | if (_atom != NULL) | 
|---|
|  | 174 | atomVector.push_back( _atom ); | 
|---|
|  | 175 | else | 
|---|
|  | 176 | ASSERT( false, "Cluster::getAtomRefs() - unknown id "+toString(_id)+"."); | 
|---|
|  | 177 | } | 
|---|
|  | 178 | return atomVector; | 
|---|
|  | 179 | } | 
|---|
|  | 180 |  | 
|---|
|  | 181 | /** Clone function for this instance. | 
|---|
|  | 182 | * | 
|---|
|  | 183 | * @param copyMethod functor that knows how to copy atoms | 
|---|
|  | 184 | * @param offset Vector to translate new cluster relative to old one | 
|---|
|  | 185 | * @return another instance with newly allocated atoms | 
|---|
|  | 186 | */ | 
|---|
|  | 187 | ClusterInterface::Cluster_impl Cluster::clone( | 
|---|
|  | 188 | CopyAtomsInterface& copyMethod, | 
|---|
|  | 189 | const Vector &offset) const | 
|---|
|  | 190 | { | 
|---|
|  | 191 | LOG(2, "INFO: Clone this cluster with " << atoms.size() << " atoms."); | 
|---|
|  | 192 | /// get another cluster instance | 
|---|
|  | 193 | Cluster * clonedInstance = new Cluster(::translate(getShape(), offset)); | 
|---|
|  | 194 |  | 
|---|
|  | 195 | /// copy and move atoms | 
|---|
|  | 196 | copyMethod(getAtomRefs()); | 
|---|
|  | 197 | AtomVector CopiedAtoms = copyMethod.getCopiedAtoms(); | 
|---|
|  | 198 | BOOST_FOREACH( atom *_atom, CopiedAtoms) { | 
|---|
|  | 199 | _atom->setPosition( _atom->getPosition() + offset ); | 
|---|
|  | 200 | } | 
|---|
|  | 201 |  | 
|---|
|  | 202 | /// fill copied atoms into new instance | 
|---|
|  | 203 | // dont use a set here, makes life hard with STL algos | 
|---|
|  | 204 | std::vector<atomId_t> Copies(CopiedAtoms.size(), (size_t)-1); | 
|---|
|  | 205 | std::transform(CopiedAtoms.begin(), CopiedAtoms.end(), Copies.begin(), | 
|---|
|  | 206 | boost::bind(&atom::getId, _1) ); | 
|---|
|  | 207 | clonedInstance->atoms.insert(Copies.begin(), Copies.end()); | 
|---|
|  | 208 |  | 
|---|
|  | 209 | return ClusterInterface::Cluster_impl(clonedInstance); | 
|---|
|  | 210 | } | 
|---|
|  | 211 |  | 
|---|
|  | 212 | /** Translate atoms inside Cluster and Shape. | 
|---|
|  | 213 | * | 
|---|
|  | 214 | * @param offset offset to translate by | 
|---|
|  | 215 | */ | 
|---|
|  | 216 | void Cluster::translate(const Vector &offset) | 
|---|
|  | 217 | { | 
|---|
|  | 218 | // move atoms | 
|---|
|  | 219 | AtomVector atomVector = getAtomRefs(); | 
|---|
|  | 220 | BOOST_FOREACH(atom *_atom, atomVector) { | 
|---|
|  | 221 | _atom->setPosition(_atom->getPosition()+offset); | 
|---|
|  | 222 | } | 
|---|
|  | 223 | // translate shape | 
|---|
|  | 224 | s = ::translate(s, offset); | 
|---|
|  | 225 | } | 
|---|
|  | 226 |  | 
|---|
|  | 227 | /** Transform atoms inside Cluster and Shape. | 
|---|
|  | 228 | * | 
|---|
|  | 229 | * @param M transformation matrix | 
|---|
|  | 230 | */ | 
|---|
|  | 231 | void Cluster::transform(const RealSpaceMatrix &M) | 
|---|
|  | 232 | { | 
|---|
|  | 233 | // transform atoms | 
|---|
|  | 234 | AtomVector atomVector = getAtomRefs(); | 
|---|
|  | 235 | BOOST_FOREACH(atom *_atom, atomVector) { | 
|---|
|  | 236 | _atom->setPosition( M * _atom->getPosition() ); | 
|---|
|  | 237 | } | 
|---|
|  | 238 | // translate shape | 
|---|
|  | 239 | s = ::transform(s, M); | 
|---|
|  | 240 | } | 
|---|