ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-2.0/src/primitives/Molecule.cpp
Revision: 1843
Committed: Fri Dec 3 21:30:30 2004 UTC (19 years, 7 months ago) by tim
File size: 9003 byte(s)
Log Message:
more missing function get implemented

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