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