1 |
tim |
1692 |
/* |
2 |
|
|
* Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project |
3 |
|
|
* |
4 |
|
|
* Contact: oopse@oopse.org |
5 |
|
|
* |
6 |
|
|
* This program is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU Lesser General Public License |
8 |
|
|
* as published by the Free Software Foundation; either version 2.1 |
9 |
|
|
* of the License, or (at your option) any later version. |
10 |
|
|
* All we ask is that proper credit is given for our work, which includes |
11 |
|
|
* - but is not limited to - adding the above copyright notice to the beginning |
12 |
|
|
* of your source code files, and to any copyright notice that you may distribute |
13 |
|
|
* with programs based on this work. |
14 |
|
|
* |
15 |
|
|
* This program 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 Lesser General Public License for more details. |
19 |
|
|
* |
20 |
|
|
* You should have received a copy of the GNU Lesser General Public License |
21 |
|
|
* along with this program; if not, write to the Free Software |
22 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 |
|
|
* |
24 |
|
|
*/ |
25 |
gezelter |
1490 |
|
26 |
tim |
1692 |
/** |
27 |
|
|
* @file Molecule.cpp |
28 |
|
|
* @author tlin |
29 |
|
|
* @date 10/28/2004 |
30 |
|
|
* @version 1.0 |
31 |
|
|
*/ |
32 |
gezelter |
1490 |
|
33 |
tim |
1692 |
#include <algorithm> |
34 |
tim |
1695 |
#include <set> |
35 |
gezelter |
1490 |
|
36 |
tim |
1695 |
#include "primitives/Molecule.hpp" |
37 |
|
|
#include "utils/MemoryUtils.hpp" |
38 |
tim |
1782 |
#include "utils/simError.h" |
39 |
tim |
1695 |
|
40 |
tim |
1692 |
namespace oopse { |
41 |
tim |
1733 |
Molecule::Molecule(int stampId, int globalIndex, const std::string& molName) |
42 |
|
|
: stampId_(stampId), globalIndex_(globalIndex), moleculeName_(molName) { |
43 |
gezelter |
1490 |
|
44 |
tim |
1733 |
} |
45 |
|
|
|
46 |
tim |
1692 |
Molecule::~Molecule() { |
47 |
gezelter |
1490 |
|
48 |
tim |
1695 |
MemoryUtils::deleteVectorOfPointer(atoms_); |
49 |
|
|
MemoryUtils::deleteVectorOfPointer(bonds_); |
50 |
|
|
MemoryUtils::deleteVectorOfPointer(bends_); |
51 |
|
|
MemoryUtils::deleteVectorOfPointer(torsions_); |
52 |
|
|
MemoryUtils::deleteVectorOfPointer(rigidBodies_); |
53 |
|
|
MemoryUtils::deleteVectorOfPointer(cutoffGroups_); |
54 |
gezelter |
1490 |
|
55 |
tim |
1695 |
//integrableObjects_ don't own the objects |
56 |
tim |
1692 |
integrableObjects_.clear(); |
57 |
|
|
|
58 |
gezelter |
1490 |
} |
59 |
|
|
|
60 |
tim |
1692 |
void Molecule::addAtom(Atom* atom) { |
61 |
tim |
1695 |
if (std::find(atoms_.begin(), atoms_.end(), atom) == atoms_.end()) { |
62 |
tim |
1692 |
atoms_.push_back(atom); |
63 |
|
|
} |
64 |
|
|
} |
65 |
gezelter |
1490 |
|
66 |
tim |
1692 |
void Molecule::addBond(Bond* bond) { |
67 |
tim |
1695 |
if (std::find(bonds_.begin(), bonds_.end(), bond) == bonds_.end()) { |
68 |
tim |
1692 |
bonds_.push_back(bond); |
69 |
|
|
} |
70 |
|
|
} |
71 |
gezelter |
1490 |
|
72 |
tim |
1692 |
void Molecule::addBend(Bend* bend) { |
73 |
tim |
1695 |
if (std::find(bends_.begin(), bends_.end(), bend) == bends_.end()) { |
74 |
tim |
1692 |
bends_.push_back(bend); |
75 |
|
|
} |
76 |
|
|
} |
77 |
gezelter |
1490 |
|
78 |
tim |
1692 |
void Molecule::addTorsion(Torsion* torsion) { |
79 |
tim |
1695 |
if (std::find(torsions_.begin(), torsions_.end(), torsion) == torsions_.end()) { |
80 |
tim |
1692 |
torsions_.push_back(torsion); |
81 |
|
|
} |
82 |
|
|
} |
83 |
gezelter |
1490 |
|
84 |
tim |
1692 |
void Molecule::addRigidBody(RigidBody *rb) { |
85 |
tim |
1695 |
if (std::find(rigidBodies_.begin(), rigidBodies_.end(), rb) == rigidBodies_.end()) { |
86 |
tim |
1692 |
rigidBodies_.push_back(rb); |
87 |
|
|
} |
88 |
gezelter |
1490 |
} |
89 |
|
|
|
90 |
tim |
1692 |
void Molecule::addCutoffGroup(CutoffGroup* cp) { |
91 |
tim |
1695 |
if (std::find(cutoffGroups_.begin(), cutoffGroups_.end(), cp) == cutoffGroups_.end()) { |
92 |
tim |
1692 |
cutoffGroups_.push_back(cp); |
93 |
|
|
} |
94 |
gezelter |
1490 |
|
95 |
tim |
1692 |
} |
96 |
gezelter |
1490 |
|
97 |
tim |
1692 |
void Molecule::complete() { |
98 |
|
|
|
99 |
|
|
std::set<Atom*> rigidAtoms; |
100 |
|
|
RigidBody* rb; |
101 |
tim |
1695 |
std::vector<RigidBody*>::iterator rbIter; |
102 |
gezelter |
1490 |
|
103 |
tim |
1692 |
|
104 |
|
|
for (rb = beginRigidBody(rbIter); rb != NULL; rb = nextRigidBody(rbIter)) { |
105 |
tim |
1695 |
rigidAtoms.insert(rb->getBeginAtomIter(), rb->getEndAtomIter()); |
106 |
tim |
1692 |
} |
107 |
gezelter |
1490 |
|
108 |
tim |
1843 |
Atom* atom; |
109 |
|
|
AtomIterator ai; |
110 |
|
|
for (atom = beginAtom(ai); atom != NULL; atom = nextAtom(ai)) { |
111 |
tim |
1844 |
|
112 |
|
|
if (rigidAtoms.find(*ai) == rigidAtoms.end()) { |
113 |
|
|
//if an atom does not belong to a rigid body, it is an integrable object |
114 |
tim |
1843 |
integrableObjects_.push_back(*ai); |
115 |
|
|
} |
116 |
|
|
} |
117 |
|
|
|
118 |
tim |
1692 |
//find all free atoms (which do not belong to rigid bodies) |
119 |
|
|
//performs the "difference" operation from set theory, the output range contains a copy of every |
120 |
|
|
//element that is contained in [allAtoms.begin(), allAtoms.end()) and not contained in |
121 |
|
|
//[rigidAtoms.begin(), rigidAtoms.end()). |
122 |
tim |
1843 |
//std::set_difference(allAtoms.begin(), allAtoms.end(), rigidAtoms.begin(), rigidAtoms.end(), |
123 |
|
|
// std::back_inserter(integrableObjects_)); |
124 |
gezelter |
1490 |
|
125 |
tim |
1843 |
//if (integrableObjects_.size() != allAtoms.size() - rigidAtoms.size()) { |
126 |
|
|
// //Some atoms in rigidAtoms are not in allAtoms, something must be wrong |
127 |
|
|
// sprintf(painCave.errMsg, "Atoms in rigidbody are not in the atom list of the same molecule"); |
128 |
|
|
// |
129 |
|
|
// painCave.isFatal = 1; |
130 |
|
|
// simError(); |
131 |
|
|
//} |
132 |
tim |
1733 |
|
133 |
tim |
1692 |
integrableObjects_.insert(integrableObjects_.end(), rigidBodies_.begin(), rigidBodies_.end()); |
134 |
gezelter |
1490 |
} |
135 |
|
|
|
136 |
tim |
1692 |
Atom* Molecule::beginAtom(std::vector<Atom*>::iterator& i) { |
137 |
|
|
i = atoms_.begin(); |
138 |
|
|
return (i == atoms_.end()) ? NULL : *i; |
139 |
|
|
} |
140 |
gezelter |
1490 |
|
141 |
tim |
1692 |
Atom* Molecule::nextAtom(std::vector<Atom*>::iterator& i) { |
142 |
|
|
++i; |
143 |
|
|
return (i == atoms_.end()) ? NULL : *i; |
144 |
|
|
} |
145 |
gezelter |
1490 |
|
146 |
tim |
1692 |
Bond* Molecule::beginBond(std::vector<Bond*>::iterator& i) { |
147 |
|
|
i = bonds_.begin(); |
148 |
|
|
return (i == bonds_.end()) ? NULL : *i; |
149 |
|
|
} |
150 |
gezelter |
1490 |
|
151 |
tim |
1692 |
Bond* Molecule::nextBond(std::vector<Bond*>::iterator& i) { |
152 |
|
|
++i; |
153 |
|
|
return (i == bonds_.end()) ? NULL : *i; |
154 |
gezelter |
1490 |
|
155 |
tim |
1692 |
} |
156 |
gezelter |
1490 |
|
157 |
|
|
|
158 |
tim |
1692 |
Bend* Molecule::beginBend(std::vector<Bend*>::iterator& i) { |
159 |
|
|
i = bends_.begin(); |
160 |
|
|
return (i == bends_.end()) ? NULL : *i; |
161 |
gezelter |
1490 |
} |
162 |
|
|
|
163 |
tim |
1692 |
Bend* Molecule::nextBend(std::vector<Bend*>::iterator& i) { |
164 |
|
|
++i; |
165 |
|
|
return (i == bends_.end()) ? NULL : *i; |
166 |
|
|
} |
167 |
gezelter |
1490 |
|
168 |
tim |
1692 |
Torsion* Molecule::beginTorsion(std::vector<Torsion*>::iterator& i) { |
169 |
|
|
i = torsions_.begin(); |
170 |
|
|
return (i == torsions_.end()) ? NULL : *i; |
171 |
|
|
} |
172 |
gezelter |
1490 |
|
173 |
tim |
1692 |
Torsion* Molecule::nextTorsion(std::vector<Torsion*>::iterator& i) { |
174 |
|
|
++i; |
175 |
|
|
return (i == torsions_.end()) ? NULL : *i; |
176 |
|
|
} |
177 |
gezelter |
1490 |
|
178 |
tim |
1692 |
RigidBody* Molecule::beginRigidBody(std::vector<RigidBody*>::iterator& i) { |
179 |
|
|
i = rigidBodies_.begin(); |
180 |
|
|
return (i == rigidBodies_.end()) ? NULL : *i; |
181 |
|
|
} |
182 |
gezelter |
1490 |
|
183 |
tim |
1692 |
RigidBody* Molecule::nextRigidBody(std::vector<RigidBody*>::iterator& i) { |
184 |
|
|
++i; |
185 |
|
|
return (i == rigidBodies_.end()) ? NULL : *i; |
186 |
|
|
} |
187 |
gezelter |
1490 |
|
188 |
tim |
1692 |
StuntDouble* Molecule::beginIntegrableObject(std::vector<StuntDouble*>::iterator& i) { |
189 |
|
|
i = integrableObjects_.begin(); |
190 |
|
|
return (i == integrableObjects_.end()) ? NULL : *i; |
191 |
gezelter |
1490 |
} |
192 |
|
|
|
193 |
tim |
1692 |
StuntDouble* Molecule::nextIntegrableObject(std::vector<StuntDouble*>::iterator& i) { |
194 |
|
|
++i; |
195 |
|
|
return (i == integrableObjects_.end()) ? NULL : *i; |
196 |
|
|
} |
197 |
gezelter |
1490 |
|
198 |
tim |
1692 |
CutoffGroup* Molecule::beginCutoffGroup(std::vector<CutoffGroup*>::iterator& i) { |
199 |
|
|
i = cutoffGroups_.begin(); |
200 |
|
|
return (i == cutoffGroups_.end()) ? NULL : *i; |
201 |
gezelter |
1490 |
} |
202 |
|
|
|
203 |
tim |
1692 |
CutoffGroup* Molecule::nextCutoffGroup(std::vector<CutoffGroup*>::iterator& i) { |
204 |
|
|
++i; |
205 |
|
|
return (i == cutoffGroups_.end()) ? NULL : *i; |
206 |
|
|
} |
207 |
gezelter |
1490 |
|
208 |
tim |
1843 |
double Molecule::getMass() { |
209 |
|
|
StuntDouble* sd; |
210 |
|
|
std::vector<StuntDouble*>::iterator i; |
211 |
|
|
double mass = 0.0; |
212 |
gezelter |
1490 |
|
213 |
tim |
1843 |
for (sd = beginIntegrableObject(i); sd != NULL; sd = nextIntegrableObject(i)){ |
214 |
|
|
mass += sd->getMass(); |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
return mass; |
218 |
|
|
|
219 |
|
|
} |
220 |
|
|
|
221 |
tim |
1692 |
Vector3d Molecule::getCom() { |
222 |
|
|
StuntDouble* sd; |
223 |
|
|
std::vector<StuntDouble*>::iterator i; |
224 |
|
|
Vector3d com; |
225 |
|
|
double totalMass = 0; |
226 |
|
|
double mass; |
227 |
|
|
|
228 |
|
|
for (sd = beginIntegrableObject(i); sd != NULL; sd = nextIntegrableObject(i)){ |
229 |
|
|
mass = sd->getMass(); |
230 |
|
|
totalMass += mass; |
231 |
|
|
com += sd->getPos() * mass; |
232 |
|
|
} |
233 |
gezelter |
1490 |
|
234 |
tim |
1692 |
com /= totalMass; |
235 |
gezelter |
1490 |
|
236 |
tim |
1692 |
return com; |
237 |
|
|
} |
238 |
gezelter |
1490 |
|
239 |
tim |
1695 |
void Molecule::moveCom(const Vector3d& delta) { |
240 |
tim |
1692 |
StuntDouble* sd; |
241 |
|
|
std::vector<StuntDouble*>::iterator i; |
242 |
|
|
|
243 |
|
|
for (sd = beginIntegrableObject(i); sd != NULL; sd = nextIntegrableObject(i)){ |
244 |
tim |
1695 |
sd->setPos(sd->getPos() + delta); |
245 |
gezelter |
1490 |
} |
246 |
|
|
|
247 |
|
|
} |
248 |
|
|
|
249 |
tim |
1692 |
Vector3d Molecule::getComVel() { |
250 |
|
|
StuntDouble* sd; |
251 |
|
|
std::vector<StuntDouble*>::iterator i; |
252 |
|
|
Vector3d velCom; |
253 |
|
|
double totalMass = 0; |
254 |
|
|
double mass; |
255 |
|
|
|
256 |
|
|
for (sd = beginIntegrableObject(i); sd != NULL; sd = nextIntegrableObject(i)){ |
257 |
|
|
mass = sd->getMass(); |
258 |
|
|
totalMass += mass; |
259 |
|
|
velCom += sd->getVel() * mass; |
260 |
gezelter |
1490 |
} |
261 |
|
|
|
262 |
tim |
1692 |
velCom /= totalMass; |
263 |
gezelter |
1490 |
|
264 |
tim |
1692 |
return velCom; |
265 |
gezelter |
1490 |
} |
266 |
|
|
|
267 |
tim |
1843 |
double Molecule::getPotential() { |
268 |
|
|
|
269 |
|
|
Bond* bond; |
270 |
|
|
Bend* bend; |
271 |
|
|
Torsion* torsion; |
272 |
|
|
Molecule::BondIterator bondIter;; |
273 |
|
|
Molecule::BendIterator bendIter; |
274 |
|
|
Molecule::TorsionIterator torsionIter; |
275 |
|
|
|
276 |
|
|
double potential = 0.0; |
277 |
|
|
|
278 |
|
|
for (bond = beginBond(bondIter); bond != NULL; bond = nextBond(bondIter)) { |
279 |
|
|
potential += bond->getPotential(); |
280 |
|
|
} |
281 |
|
|
|
282 |
|
|
for (bend = beginBend(bendIter); bend != NULL; bend = nextBend(bendIter)) { |
283 |
|
|
potential += bend->getPotential(); |
284 |
|
|
} |
285 |
|
|
|
286 |
|
|
for (torsion = beginTorsion(torsionIter); torsion != NULL; torsion = nextTorsion(torsionIter)) { |
287 |
|
|
potential += torsion->getPotential(); |
288 |
|
|
} |
289 |
|
|
|
290 |
|
|
return potential; |
291 |
|
|
|
292 |
|
|
} |
293 |
|
|
|
294 |
tim |
1695 |
std::ostream& operator <<(std::ostream& o, Molecule& mol) { |
295 |
tim |
1692 |
o << std::endl; |
296 |
|
|
o << "Molecule " << mol.getGlobalIndex() << "has: " << std::endl; |
297 |
|
|
o << mol.getNAtoms() << " atoms" << std::endl; |
298 |
|
|
o << mol.getNBonds() << " bonds" << std::endl; |
299 |
|
|
o << mol.getNBends() << " bends" << std::endl; |
300 |
|
|
o << mol.getNTorsions() << " torsions" << std::endl; |
301 |
|
|
o << mol.getNRigidBodies() << " rigid bodies" << std::endl; |
302 |
|
|
o << mol.getNIntegrableObjects() << "integrable objects" << std::endl; |
303 |
|
|
o << mol.getNCutoffGroups() << "cutoff groups" << std::endl; |
304 |
gezelter |
1490 |
|
305 |
tim |
1692 |
return o; |
306 |
|
|
} |
307 |
gezelter |
1490 |
|
308 |
tim |
1692 |
}//end namespace oopse |