ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-4/src/primitives/Molecule.cpp
Revision: 1844
Committed: Fri Dec 3 22:36:06 2004 UTC (19 years, 7 months ago) by tim
File size: 9092 byte(s)
Log Message:
NVE is running

File Contents

# Content
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
55 //integrableObjects_ don't own the objects
56 integrableObjects_.clear();
57
58 }
59
60 void Molecule::addAtom(Atom* atom) {
61 if (std::find(atoms_.begin(), atoms_.end(), atom) == atoms_.end()) {
62 atoms_.push_back(atom);
63 }
64 }
65
66 void Molecule::addBond(Bond* bond) {
67 if (std::find(bonds_.begin(), bonds_.end(), bond) == bonds_.end()) {
68 bonds_.push_back(bond);
69 }
70 }
71
72 void Molecule::addBend(Bend* bend) {
73 if (std::find(bends_.begin(), bends_.end(), bend) == bends_.end()) {
74 bends_.push_back(bend);
75 }
76 }
77
78 void Molecule::addTorsion(Torsion* torsion) {
79 if (std::find(torsions_.begin(), torsions_.end(), torsion) == torsions_.end()) {
80 torsions_.push_back(torsion);
81 }
82 }
83
84 void Molecule::addRigidBody(RigidBody *rb) {
85 if (std::find(rigidBodies_.begin(), rigidBodies_.end(), rb) == rigidBodies_.end()) {
86 rigidBodies_.push_back(rb);
87 }
88 }
89
90 void Molecule::addCutoffGroup(CutoffGroup* cp) {
91 if (std::find(cutoffGroups_.begin(), cutoffGroups_.end(), cp) == cutoffGroups_.end()) {
92 cutoffGroups_.push_back(cp);
93 }
94
95 }
96
97 void Molecule::complete() {
98
99 std::set<Atom*> rigidAtoms;
100 RigidBody* rb;
101 std::vector<RigidBody*>::iterator rbIter;
102
103
104 for (rb = beginRigidBody(rbIter); rb != NULL; rb = nextRigidBody(rbIter)) {
105 rigidAtoms.insert(rb->getBeginAtomIter(), rb->getEndAtomIter());
106 }
107
108 Atom* atom;
109 AtomIterator ai;
110 for (atom = beginAtom(ai); atom != NULL; atom = nextAtom(ai)) {
111
112 if (rigidAtoms.find(*ai) == rigidAtoms.end()) {
113 //if an atom does not belong to a rigid body, it is an integrable object
114 integrableObjects_.push_back(*ai);
115 }
116 }
117
118 //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 //std::set_difference(allAtoms.begin(), allAtoms.end(), rigidAtoms.begin(), rigidAtoms.end(),
123 // std::back_inserter(integrableObjects_));
124
125 //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
133 integrableObjects_.insert(integrableObjects_.end(), rigidBodies_.begin(), rigidBodies_.end());
134 }
135
136 Atom* Molecule::beginAtom(std::vector<Atom*>::iterator& i) {
137 i = atoms_.begin();
138 return (i == atoms_.end()) ? NULL : *i;
139 }
140
141 Atom* Molecule::nextAtom(std::vector<Atom*>::iterator& i) {
142 ++i;
143 return (i == atoms_.end()) ? NULL : *i;
144 }
145
146 Bond* Molecule::beginBond(std::vector<Bond*>::iterator& i) {
147 i = bonds_.begin();
148 return (i == bonds_.end()) ? NULL : *i;
149 }
150
151 Bond* Molecule::nextBond(std::vector<Bond*>::iterator& i) {
152 ++i;
153 return (i == bonds_.end()) ? NULL : *i;
154
155 }
156
157
158 Bend* Molecule::beginBend(std::vector<Bend*>::iterator& i) {
159 i = bends_.begin();
160 return (i == bends_.end()) ? NULL : *i;
161 }
162
163 Bend* Molecule::nextBend(std::vector<Bend*>::iterator& i) {
164 ++i;
165 return (i == bends_.end()) ? NULL : *i;
166 }
167
168 Torsion* Molecule::beginTorsion(std::vector<Torsion*>::iterator& i) {
169 i = torsions_.begin();
170 return (i == torsions_.end()) ? NULL : *i;
171 }
172
173 Torsion* Molecule::nextTorsion(std::vector<Torsion*>::iterator& i) {
174 ++i;
175 return (i == torsions_.end()) ? NULL : *i;
176 }
177
178 RigidBody* Molecule::beginRigidBody(std::vector<RigidBody*>::iterator& i) {
179 i = rigidBodies_.begin();
180 return (i == rigidBodies_.end()) ? NULL : *i;
181 }
182
183 RigidBody* Molecule::nextRigidBody(std::vector<RigidBody*>::iterator& i) {
184 ++i;
185 return (i == rigidBodies_.end()) ? NULL : *i;
186 }
187
188 StuntDouble* Molecule::beginIntegrableObject(std::vector<StuntDouble*>::iterator& i) {
189 i = integrableObjects_.begin();
190 return (i == integrableObjects_.end()) ? NULL : *i;
191 }
192
193 StuntDouble* Molecule::nextIntegrableObject(std::vector<StuntDouble*>::iterator& i) {
194 ++i;
195 return (i == integrableObjects_.end()) ? NULL : *i;
196 }
197
198 CutoffGroup* Molecule::beginCutoffGroup(std::vector<CutoffGroup*>::iterator& i) {
199 i = cutoffGroups_.begin();
200 return (i == cutoffGroups_.end()) ? NULL : *i;
201 }
202
203 CutoffGroup* Molecule::nextCutoffGroup(std::vector<CutoffGroup*>::iterator& i) {
204 ++i;
205 return (i == cutoffGroups_.end()) ? NULL : *i;
206 }
207
208 double Molecule::getMass() {
209 StuntDouble* sd;
210 std::vector<StuntDouble*>::iterator i;
211 double mass = 0.0;
212
213 for (sd = beginIntegrableObject(i); sd != NULL; sd = nextIntegrableObject(i)){
214 mass += sd->getMass();
215 }
216
217 return mass;
218
219 }
220
221 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
234 com /= totalMass;
235
236 return com;
237 }
238
239 void Molecule::moveCom(const Vector3d& delta) {
240 StuntDouble* sd;
241 std::vector<StuntDouble*>::iterator i;
242
243 for (sd = beginIntegrableObject(i); sd != NULL; sd = nextIntegrableObject(i)){
244 sd->setPos(sd->getPos() + delta);
245 }
246
247 }
248
249 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 }
261
262 velCom /= totalMass;
263
264 return velCom;
265 }
266
267 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 std::ostream& operator <<(std::ostream& o, Molecule& mol) {
295 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
305 return o;
306 }
307
308 }//end namespace oopse