[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 |
|
---|
[9eb71b3] | 36 | //#include "CodePatterns/MemDebug.hpp"
|
---|
[8f6e2a] | 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) {
|
---|
[d00441] | 113 | #ifndef NDEBUG
|
---|
| 114 | std::pair<atomIdSet::iterator, bool> inserter =
|
---|
| 115 | #endif
|
---|
| 116 | atoms.insert(id);
|
---|
[8f6e2a] | 117 | ASSERT(inserter.second,
|
---|
| 118 | "Cluster::insert() - atomic id "+toString(id)+" is already present.");
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /** Remove atom by its \a id from the cluster.
|
---|
| 123 | *
|
---|
| 124 | * @param id atom to remove
|
---|
| 125 | */
|
---|
| 126 | void Cluster::erase(const atomId_t id)
|
---|
| 127 | {
|
---|
| 128 | atomIdSet::iterator iter = atoms.find(id);
|
---|
| 129 | ASSERT(iter != atoms.end(),
|
---|
| 130 | "Cluster::erase() - atomic id "+toString(id)+" unknown in this Cluster.");
|
---|
| 131 | if (iter != atoms.end())
|
---|
| 132 | atoms.erase(iter);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /** Checks whether a given atom is within the shape \a s.
|
---|
| 136 | *
|
---|
| 137 | * @param id atomic id to check
|
---|
| 138 | * @return true - is in Shape, false - is not contained (or does not exist)
|
---|
| 139 | */
|
---|
| 140 | bool Cluster::IsInShape(const atomId_t id) const
|
---|
| 141 | {
|
---|
| 142 | const atom * const _atom = getAtomById(id);
|
---|
| 143 | if (_atom != NULL)
|
---|
| 144 | return s.isInside(_atom->getPosition());
|
---|
| 145 | else
|
---|
| 146 | return false;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /** Helper function for looking up atomic reference by its id.
|
---|
| 150 | *
|
---|
| 151 | * @param id id to look up
|
---|
| 152 | * @return reference to atom with this id
|
---|
| 153 | */
|
---|
| 154 | atom * const Cluster::getAtomById(const atomId_t id) const
|
---|
| 155 | {
|
---|
| 156 | atom * const _atom = World::getInstance().getAtom(AtomById(id));
|
---|
| 157 | ASSERT(_atom != NULL,
|
---|
| 158 | "Cluster::getAtomById() - id "+toString(id)+" is unknown to World.");
|
---|
| 159 | return _atom;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | bool isNullAtom(const atom* _atom) {
|
---|
| 163 | return _atom == NULL;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | /** Getter for the underlying true atoms refs.
|
---|
| 167 | *
|
---|
| 168 | * @return AtomVector filled with looked-up atom references
|
---|
| 169 | */
|
---|
| 170 | Cluster::AtomVector Cluster::getAtomRefs() const
|
---|
| 171 | {
|
---|
| 172 | AtomVector atomVector;
|
---|
| 173 | atomVector.reserve(atoms.size());
|
---|
| 174 | BOOST_FOREACH(atomId_t _id, atoms) {
|
---|
| 175 | atom * const _atom = World::getInstance().getAtom(AtomById(_id));
|
---|
| 176 | if (_atom != NULL)
|
---|
| 177 | atomVector.push_back( _atom );
|
---|
| 178 | else
|
---|
| 179 | ASSERT( false, "Cluster::getAtomRefs() - unknown id "+toString(_id)+".");
|
---|
| 180 | }
|
---|
| 181 | return atomVector;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | /** Clone function for this instance.
|
---|
| 185 | *
|
---|
| 186 | * @param copyMethod functor that knows how to copy atoms
|
---|
| 187 | * @param offset Vector to translate new cluster relative to old one
|
---|
| 188 | * @return another instance with newly allocated atoms
|
---|
| 189 | */
|
---|
| 190 | ClusterInterface::Cluster_impl Cluster::clone(
|
---|
| 191 | CopyAtomsInterface& copyMethod,
|
---|
| 192 | const Vector &offset) const
|
---|
| 193 | {
|
---|
| 194 | LOG(2, "INFO: Clone this cluster with " << atoms.size() << " atoms.");
|
---|
| 195 | /// get another cluster instance
|
---|
| 196 | Cluster * clonedInstance = new Cluster(::translate(getShape(), offset));
|
---|
| 197 |
|
---|
| 198 | /// copy and move atoms
|
---|
| 199 | copyMethod(getAtomRefs());
|
---|
| 200 | AtomVector CopiedAtoms = copyMethod.getCopiedAtoms();
|
---|
| 201 | BOOST_FOREACH( atom *_atom, CopiedAtoms) {
|
---|
| 202 | _atom->setPosition( _atom->getPosition() + offset );
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /// fill copied atoms into new instance
|
---|
| 206 | // dont use a set here, makes life hard with STL algos
|
---|
| 207 | std::vector<atomId_t> Copies(CopiedAtoms.size(), (size_t)-1);
|
---|
| 208 | std::transform(CopiedAtoms.begin(), CopiedAtoms.end(), Copies.begin(),
|
---|
| 209 | boost::bind(&atom::getId, _1) );
|
---|
| 210 | clonedInstance->atoms.insert(Copies.begin(), Copies.end());
|
---|
| 211 |
|
---|
| 212 | return ClusterInterface::Cluster_impl(clonedInstance);
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | /** Translate atoms inside Cluster and Shape.
|
---|
| 216 | *
|
---|
| 217 | * @param offset offset to translate by
|
---|
| 218 | */
|
---|
| 219 | void Cluster::translate(const Vector &offset)
|
---|
| 220 | {
|
---|
| 221 | // move atoms
|
---|
| 222 | AtomVector atomVector = getAtomRefs();
|
---|
| 223 | BOOST_FOREACH(atom *_atom, atomVector) {
|
---|
| 224 | _atom->setPosition(_atom->getPosition()+offset);
|
---|
| 225 | }
|
---|
| 226 | // translate shape
|
---|
| 227 | s = ::translate(s, offset);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | /** Transform atoms inside Cluster and Shape.
|
---|
| 231 | *
|
---|
| 232 | * @param M transformation matrix
|
---|
| 233 | */
|
---|
| 234 | void Cluster::transform(const RealSpaceMatrix &M)
|
---|
| 235 | {
|
---|
| 236 | // transform atoms
|
---|
| 237 | AtomVector atomVector = getAtomRefs();
|
---|
| 238 | BOOST_FOREACH(atom *_atom, atomVector) {
|
---|
| 239 | _atom->setPosition( M * _atom->getPosition() );
|
---|
| 240 | }
|
---|
| 241 | // translate shape
|
---|
| 242 | s = ::transform(s, M);
|
---|
| 243 | }
|
---|