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: 1738
Committed: Sat Nov 13 05:08:12 2004 UTC (19 years, 8 months ago) by tim
File size: 7879 byte(s)
Log Message:
refactory, refactory, refactory

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