ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-3.0/src/primitives/Molecule.cpp
Revision: 1782
Committed: Wed Nov 24 20:55:03 2004 UTC (19 years, 7 months ago) by tim
File size: 7901 byte(s)
Log Message:
types and primitives get built

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*> allAtoms;
100 allAtoms.insert(atoms_.begin(), atoms_.end());
101
102 std::set<Atom*> rigidAtoms;
103 RigidBody* rb;
104 std::vector<RigidBody*>::iterator rbIter;
105
106
107 for (rb = beginRigidBody(rbIter); rb != NULL; rb = nextRigidBody(rbIter)) {
108 rigidAtoms.insert(rb->getBeginAtomIter(), rb->getEndAtomIter());
109 }
110
111 //find all free atoms (which do not belong to rigid bodies)
112 //performs the "difference" operation from set theory, the output range contains a copy of every
113 //element that is contained in [allAtoms.begin(), allAtoms.end()) and not contained in
114 //[rigidAtoms.begin(), rigidAtoms.end()).
115 std::set_difference(allAtoms.begin(), allAtoms.end(), rigidAtoms.begin(), rigidAtoms.end(),
116 std::back_inserter(integrableObjects_));
117
118 if (integrableObjects_.size() != allAtoms.size() - rigidAtoms.size()) {
119 //Some atoms in rigidAtoms are not in allAtoms, something must be wrong
120 sprintf(painCave.errMsg, "Atoms in rigidbody are not in the atom list of the same molecule");
121
122 painCave.isFatal = 1;
123 simError();
124 }
125
126 integrableObjects_.insert(integrableObjects_.end(), rigidBodies_.begin(), rigidBodies_.end());
127 }
128
129 Atom* Molecule::beginAtom(std::vector<Atom*>::iterator& i) {
130 i = atoms_.begin();
131 return (i == atoms_.end()) ? NULL : *i;
132 }
133
134 Atom* Molecule::nextAtom(std::vector<Atom*>::iterator& i) {
135 ++i;
136 return (i == atoms_.end()) ? NULL : *i;
137 }
138
139 Bond* Molecule::beginBond(std::vector<Bond*>::iterator& i) {
140 i = bonds_.begin();
141 return (i == bonds_.end()) ? NULL : *i;
142 }
143
144 Bond* Molecule::nextBond(std::vector<Bond*>::iterator& i) {
145 ++i;
146 return (i == bonds_.end()) ? NULL : *i;
147
148 }
149
150
151 Bend* Molecule::beginBend(std::vector<Bend*>::iterator& i) {
152 i = bends_.begin();
153 return (i == bends_.end()) ? NULL : *i;
154 }
155
156 Bend* Molecule::nextBend(std::vector<Bend*>::iterator& i) {
157 ++i;
158 return (i == bends_.end()) ? NULL : *i;
159 }
160
161 Torsion* Molecule::beginTorsion(std::vector<Torsion*>::iterator& i) {
162 i = torsions_.begin();
163 return (i == torsions_.end()) ? NULL : *i;
164 }
165
166 Torsion* Molecule::nextTorsion(std::vector<Torsion*>::iterator& i) {
167 ++i;
168 return (i == torsions_.end()) ? NULL : *i;
169 }
170
171 RigidBody* Molecule::beginRigidBody(std::vector<RigidBody*>::iterator& i) {
172 i = rigidBodies_.begin();
173 return (i == rigidBodies_.end()) ? NULL : *i;
174 }
175
176 RigidBody* Molecule::nextRigidBody(std::vector<RigidBody*>::iterator& i) {
177 ++i;
178 return (i == rigidBodies_.end()) ? NULL : *i;
179 }
180
181 StuntDouble* Molecule::beginIntegrableObject(std::vector<StuntDouble*>::iterator& i) {
182 i = integrableObjects_.begin();
183 return (i == integrableObjects_.end()) ? NULL : *i;
184 }
185
186 StuntDouble* Molecule::nextIntegrableObject(std::vector<StuntDouble*>::iterator& i) {
187 ++i;
188 return (i == integrableObjects_.end()) ? NULL : *i;
189 }
190
191 CutoffGroup* Molecule::beginCutoffGroup(std::vector<CutoffGroup*>::iterator& i) {
192 i = cutoffGroups_.begin();
193 return (i == cutoffGroups_.end()) ? NULL : *i;
194 }
195
196 CutoffGroup* Molecule::nextCutoffGroup(std::vector<CutoffGroup*>::iterator& i) {
197 ++i;
198 return (i == cutoffGroups_.end()) ? NULL : *i;
199 }
200
201
202 Vector3d Molecule::getCom() {
203 StuntDouble* sd;
204 std::vector<StuntDouble*>::iterator i;
205 Vector3d com;
206 double totalMass = 0;
207 double mass;
208
209 for (sd = beginIntegrableObject(i); sd != NULL; sd = nextIntegrableObject(i)){
210 mass = sd->getMass();
211 totalMass += mass;
212 com += sd->getPos() * mass;
213 }
214
215 com /= totalMass;
216
217 return com;
218 }
219
220 void Molecule::moveCom(const Vector3d& delta) {
221 StuntDouble* sd;
222 std::vector<StuntDouble*>::iterator i;
223
224 for (sd = beginIntegrableObject(i); sd != NULL; sd = nextIntegrableObject(i)){
225 sd->setPos(sd->getPos() + delta);
226 }
227
228 }
229
230 Vector3d Molecule::getComVel() {
231 StuntDouble* sd;
232 std::vector<StuntDouble*>::iterator i;
233 Vector3d velCom;
234 double totalMass = 0;
235 double mass;
236
237 for (sd = beginIntegrableObject(i); sd != NULL; sd = nextIntegrableObject(i)){
238 mass = sd->getMass();
239 totalMass += mass;
240 velCom += sd->getVel() * mass;
241 }
242
243 velCom /= totalMass;
244
245 return velCom;
246 }
247
248 std::ostream& operator <<(std::ostream& o, Molecule& mol) {
249 o << std::endl;
250 o << "Molecule " << mol.getGlobalIndex() << "has: " << std::endl;
251 o << mol.getNAtoms() << " atoms" << std::endl;
252 o << mol.getNBonds() << " bonds" << std::endl;
253 o << mol.getNBends() << " bends" << std::endl;
254 o << mol.getNTorsions() << " torsions" << std::endl;
255 o << mol.getNRigidBodies() << " rigid bodies" << std::endl;
256 o << mol.getNIntegrableObjects() << "integrable objects" << std::endl;
257 o << mol.getNCutoffGroups() << "cutoff groups" << std::endl;
258
259 return o;
260 }
261
262 }//end namespace oopse