1 |
/* |
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 |
|
26 |
/** |
27 |
* @file Molecule.cpp |
28 |
* @author tlin |
29 |
* @date 10/28/2004 |
30 |
* @version 1.0 |
31 |
*/ |
32 |
|
33 |
#include <algorithm> |
34 |
#include <set> |
35 |
|
36 |
#include "primitives/Molecule.hpp" |
37 |
#include "utils/MemoryUtils.hpp" |
38 |
#include "utils/simError.h" |
39 |
|
40 |
namespace oopse { |
41 |
Molecule::Molecule(int stampId, int globalIndex, const std::string& molName) |
42 |
: stampId_(stampId), globalIndex_(globalIndex), moleculeName_(molName) { |
43 |
|
44 |
} |
45 |
|
46 |
Molecule::~Molecule() { |
47 |
|
48 |
MemoryUtils::deleteVectorOfPointer(atoms_); |
49 |
MemoryUtils::deleteVectorOfPointer(bonds_); |
50 |
MemoryUtils::deleteVectorOfPointer(bends_); |
51 |
MemoryUtils::deleteVectorOfPointer(torsions_); |
52 |
MemoryUtils::deleteVectorOfPointer(rigidBodies_); |
53 |
MemoryUtils::deleteVectorOfPointer(cutoffGroups_); |
54 |
MemoryUtils::deleteVectorOfPointer(constraintPairs_); |
55 |
MemoryUtils::deleteVectorOfPointer(constraintElems_); |
56 |
//integrableObjects_ don't own the objects |
57 |
integrableObjects_.clear(); |
58 |
|
59 |
} |
60 |
|
61 |
void Molecule::addAtom(Atom* atom) { |
62 |
if (std::find(atoms_.begin(), atoms_.end(), atom) == atoms_.end()) { |
63 |
atoms_.push_back(atom); |
64 |
} |
65 |
} |
66 |
|
67 |
void Molecule::addBond(Bond* bond) { |
68 |
if (std::find(bonds_.begin(), bonds_.end(), bond) == bonds_.end()) { |
69 |
bonds_.push_back(bond); |
70 |
} |
71 |
} |
72 |
|
73 |
void Molecule::addBend(Bend* bend) { |
74 |
if (std::find(bends_.begin(), bends_.end(), bend) == bends_.end()) { |
75 |
bends_.push_back(bend); |
76 |
} |
77 |
} |
78 |
|
79 |
void Molecule::addTorsion(Torsion* torsion) { |
80 |
if (std::find(torsions_.begin(), torsions_.end(), torsion) == torsions_.end()) { |
81 |
torsions_.push_back(torsion); |
82 |
} |
83 |
} |
84 |
|
85 |
void Molecule::addRigidBody(RigidBody *rb) { |
86 |
if (std::find(rigidBodies_.begin(), rigidBodies_.end(), rb) == rigidBodies_.end()) { |
87 |
rigidBodies_.push_back(rb); |
88 |
} |
89 |
} |
90 |
|
91 |
void Molecule::addCutoffGroup(CutoffGroup* cp) { |
92 |
if (std::find(cutoffGroups_.begin(), cutoffGroups_.end(), cp) == cutoffGroups_.end()) { |
93 |
cutoffGroups_.push_back(cp); |
94 |
} |
95 |
|
96 |
} |
97 |
|
98 |
void Molecule::addConstraintPair(ConstraintPair* cp) { |
99 |
if (std::find(constraintPairs_.begin(), constraintPairs_.end(), cp) == constraintPairs_.end()) { |
100 |
constraintPairs_.push_back(cp); |
101 |
} |
102 |
|
103 |
} |
104 |
|
105 |
void Molecule::addConstraintElem(ConstraintElem* cp) { |
106 |
if (std::find(constraintElems_.begin(), constraintElems_.end(), cp) == constraintElems_.end()) { |
107 |
constraintElems_.push_back(cp); |
108 |
} |
109 |
|
110 |
} |
111 |
|
112 |
void Molecule::complete() { |
113 |
|
114 |
std::set<Atom*> rigidAtoms; |
115 |
RigidBody* rb; |
116 |
std::vector<RigidBody*>::iterator rbIter; |
117 |
|
118 |
|
119 |
for (rb = beginRigidBody(rbIter); rb != NULL; rb = nextRigidBody(rbIter)) { |
120 |
rigidAtoms.insert(rb->getBeginAtomIter(), rb->getEndAtomIter()); |
121 |
} |
122 |
|
123 |
Atom* atom; |
124 |
AtomIterator ai; |
125 |
for (atom = beginAtom(ai); atom != NULL; atom = nextAtom(ai)) { |
126 |
|
127 |
if (rigidAtoms.find(*ai) == rigidAtoms.end()) { |
128 |
//if an atom does not belong to a rigid body, it is an integrable object |
129 |
integrableObjects_.push_back(*ai); |
130 |
} |
131 |
} |
132 |
|
133 |
//find all free atoms (which do not belong to rigid bodies) |
134 |
//performs the "difference" operation from set theory, the output range contains a copy of every |
135 |
//element that is contained in [allAtoms.begin(), allAtoms.end()) and not contained in |
136 |
//[rigidAtoms.begin(), rigidAtoms.end()). |
137 |
//std::set_difference(allAtoms.begin(), allAtoms.end(), rigidAtoms.begin(), rigidAtoms.end(), |
138 |
// std::back_inserter(integrableObjects_)); |
139 |
|
140 |
//if (integrableObjects_.size() != allAtoms.size() - rigidAtoms.size()) { |
141 |
// //Some atoms in rigidAtoms are not in allAtoms, something must be wrong |
142 |
// sprintf(painCave.errMsg, "Atoms in rigidbody are not in the atom list of the same molecule"); |
143 |
// |
144 |
// painCave.isFatal = 1; |
145 |
// simError(); |
146 |
//} |
147 |
|
148 |
integrableObjects_.insert(integrableObjects_.end(), rigidBodies_.begin(), rigidBodies_.end()); |
149 |
} |
150 |
|
151 |
double Molecule::getMass() { |
152 |
StuntDouble* sd; |
153 |
std::vector<StuntDouble*>::iterator i; |
154 |
double mass = 0.0; |
155 |
|
156 |
for (sd = beginIntegrableObject(i); sd != NULL; sd = nextIntegrableObject(i)){ |
157 |
mass += sd->getMass(); |
158 |
} |
159 |
|
160 |
return mass; |
161 |
|
162 |
} |
163 |
|
164 |
Vector3d Molecule::getCom() { |
165 |
StuntDouble* sd; |
166 |
std::vector<StuntDouble*>::iterator i; |
167 |
Vector3d com; |
168 |
double totalMass = 0; |
169 |
double mass; |
170 |
|
171 |
for (sd = beginIntegrableObject(i); sd != NULL; sd = nextIntegrableObject(i)){ |
172 |
mass = sd->getMass(); |
173 |
totalMass += mass; |
174 |
com += sd->getPos() * mass; |
175 |
} |
176 |
|
177 |
com /= totalMass; |
178 |
|
179 |
return com; |
180 |
} |
181 |
|
182 |
void Molecule::moveCom(const Vector3d& delta) { |
183 |
StuntDouble* sd; |
184 |
std::vector<StuntDouble*>::iterator i; |
185 |
|
186 |
for (sd = beginIntegrableObject(i); sd != NULL; sd = nextIntegrableObject(i)){ |
187 |
sd->setPos(sd->getPos() + delta); |
188 |
} |
189 |
|
190 |
} |
191 |
|
192 |
Vector3d Molecule::getComVel() { |
193 |
StuntDouble* sd; |
194 |
std::vector<StuntDouble*>::iterator i; |
195 |
Vector3d velCom; |
196 |
double totalMass = 0; |
197 |
double mass; |
198 |
|
199 |
for (sd = beginIntegrableObject(i); sd != NULL; sd = nextIntegrableObject(i)){ |
200 |
mass = sd->getMass(); |
201 |
totalMass += mass; |
202 |
velCom += sd->getVel() * mass; |
203 |
} |
204 |
|
205 |
velCom /= totalMass; |
206 |
|
207 |
return velCom; |
208 |
} |
209 |
|
210 |
double Molecule::getPotential() { |
211 |
|
212 |
Bond* bond; |
213 |
Bend* bend; |
214 |
Torsion* torsion; |
215 |
Molecule::BondIterator bondIter;; |
216 |
Molecule::BendIterator bendIter; |
217 |
Molecule::TorsionIterator torsionIter; |
218 |
|
219 |
double potential = 0.0; |
220 |
|
221 |
for (bond = beginBond(bondIter); bond != NULL; bond = nextBond(bondIter)) { |
222 |
potential += bond->getPotential(); |
223 |
} |
224 |
|
225 |
for (bend = beginBend(bendIter); bend != NULL; bend = nextBend(bendIter)) { |
226 |
potential += bend->getPotential(); |
227 |
} |
228 |
|
229 |
for (torsion = beginTorsion(torsionIter); torsion != NULL; torsion = nextTorsion(torsionIter)) { |
230 |
potential += torsion->getPotential(); |
231 |
} |
232 |
|
233 |
return potential; |
234 |
|
235 |
} |
236 |
|
237 |
std::ostream& operator <<(std::ostream& o, Molecule& mol) { |
238 |
o << std::endl; |
239 |
o << "Molecule " << mol.getGlobalIndex() << "has: " << std::endl; |
240 |
o << mol.getNAtoms() << " atoms" << std::endl; |
241 |
o << mol.getNBonds() << " bonds" << std::endl; |
242 |
o << mol.getNBends() << " bends" << std::endl; |
243 |
o << mol.getNTorsions() << " torsions" << std::endl; |
244 |
o << mol.getNRigidBodies() << " rigid bodies" << std::endl; |
245 |
o << mol.getNIntegrableObjects() << "integrable objects" << std::endl; |
246 |
o << mol.getNCutoffGroups() << "cutoff groups" << std::endl; |
247 |
o << mol.getNConstraintPairs() << "constraint pairs" << std::endl; |
248 |
return o; |
249 |
} |
250 |
|
251 |
}//end namespace oopse |