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, 9 months ago) by tim
File size: 7901 byte(s)
Log Message:
types and primitives get built

File Contents

# User Rev Content
1 tim 1692 /*
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 gezelter 1490
26 tim 1692 /**
27     * @file Molecule.cpp
28     * @author tlin
29     * @date 10/28/2004
30     * @version 1.0
31     */
32 gezelter 1490
33 tim 1692 #include <algorithm>
34 tim 1695 #include <set>
35 gezelter 1490
36 tim 1695 #include "primitives/Molecule.hpp"
37     #include "utils/MemoryUtils.hpp"
38 tim 1782 #include "utils/simError.h"
39 tim 1695
40 tim 1692 namespace oopse {
41 tim 1733 Molecule::Molecule(int stampId, int globalIndex, const std::string& molName)
42     : stampId_(stampId), globalIndex_(globalIndex), moleculeName_(molName) {
43 gezelter 1490
44 tim 1733 }
45    
46 tim 1692 Molecule::~Molecule() {
47 gezelter 1490
48 tim 1695 MemoryUtils::deleteVectorOfPointer(atoms_);
49     MemoryUtils::deleteVectorOfPointer(bonds_);
50     MemoryUtils::deleteVectorOfPointer(bends_);
51     MemoryUtils::deleteVectorOfPointer(torsions_);
52     MemoryUtils::deleteVectorOfPointer(rigidBodies_);
53     MemoryUtils::deleteVectorOfPointer(cutoffGroups_);
54 gezelter 1490
55 tim 1695 //integrableObjects_ don't own the objects
56 tim 1692 integrableObjects_.clear();
57    
58 gezelter 1490 }
59    
60 tim 1692 void Molecule::addAtom(Atom* atom) {
61 tim 1695 if (std::find(atoms_.begin(), atoms_.end(), atom) == atoms_.end()) {
62 tim 1692 atoms_.push_back(atom);
63     }
64     }
65 gezelter 1490
66 tim 1692 void Molecule::addBond(Bond* bond) {
67 tim 1695 if (std::find(bonds_.begin(), bonds_.end(), bond) == bonds_.end()) {
68 tim 1692 bonds_.push_back(bond);
69     }
70     }
71 gezelter 1490
72 tim 1692 void Molecule::addBend(Bend* bend) {
73 tim 1695 if (std::find(bends_.begin(), bends_.end(), bend) == bends_.end()) {
74 tim 1692 bends_.push_back(bend);
75     }
76     }
77 gezelter 1490
78 tim 1692 void Molecule::addTorsion(Torsion* torsion) {
79 tim 1695 if (std::find(torsions_.begin(), torsions_.end(), torsion) == torsions_.end()) {
80 tim 1692 torsions_.push_back(torsion);
81     }
82     }
83 gezelter 1490
84 tim 1692 void Molecule::addRigidBody(RigidBody *rb) {
85 tim 1695 if (std::find(rigidBodies_.begin(), rigidBodies_.end(), rb) == rigidBodies_.end()) {
86 tim 1692 rigidBodies_.push_back(rb);
87     }
88 gezelter 1490 }
89    
90 tim 1692 void Molecule::addCutoffGroup(CutoffGroup* cp) {
91 tim 1695 if (std::find(cutoffGroups_.begin(), cutoffGroups_.end(), cp) == cutoffGroups_.end()) {
92 tim 1692 cutoffGroups_.push_back(cp);
93     }
94 gezelter 1490
95 tim 1692 }
96 gezelter 1490
97 tim 1692 void Molecule::complete() {
98    
99     std::set<Atom*> allAtoms;
100     allAtoms.insert(atoms_.begin(), atoms_.end());
101 gezelter 1490
102 tim 1692 std::set<Atom*> rigidAtoms;
103     RigidBody* rb;
104 tim 1695 std::vector<RigidBody*>::iterator rbIter;
105 gezelter 1490
106 tim 1692
107     for (rb = beginRigidBody(rbIter); rb != NULL; rb = nextRigidBody(rbIter)) {
108 tim 1695 rigidAtoms.insert(rb->getBeginAtomIter(), rb->getEndAtomIter());
109 tim 1692 }
110 gezelter 1490
111 tim 1692 //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 tim 1782 std::back_inserter(integrableObjects_));
117 gezelter 1490
118 tim 1692 if (integrableObjects_.size() != allAtoms.size() - rigidAtoms.size()) {
119     //Some atoms in rigidAtoms are not in allAtoms, something must be wrong
120 tim 1733 sprintf(painCave.errMsg, "Atoms in rigidbody are not in the atom list of the same molecule");
121    
122     painCave.isFatal = 1;
123     simError();
124 tim 1692 }
125 gezelter 1490
126 tim 1692 integrableObjects_.insert(integrableObjects_.end(), rigidBodies_.begin(), rigidBodies_.end());
127 gezelter 1490 }
128    
129 tim 1692 Atom* Molecule::beginAtom(std::vector<Atom*>::iterator& i) {
130     i = atoms_.begin();
131     return (i == atoms_.end()) ? NULL : *i;
132     }
133 gezelter 1490
134 tim 1692 Atom* Molecule::nextAtom(std::vector<Atom*>::iterator& i) {
135     ++i;
136     return (i == atoms_.end()) ? NULL : *i;
137     }
138 gezelter 1490
139 tim 1692 Bond* Molecule::beginBond(std::vector<Bond*>::iterator& i) {
140     i = bonds_.begin();
141     return (i == bonds_.end()) ? NULL : *i;
142     }
143 gezelter 1490
144 tim 1692 Bond* Molecule::nextBond(std::vector<Bond*>::iterator& i) {
145     ++i;
146     return (i == bonds_.end()) ? NULL : *i;
147 gezelter 1490
148 tim 1692 }
149 gezelter 1490
150    
151 tim 1692 Bend* Molecule::beginBend(std::vector<Bend*>::iterator& i) {
152     i = bends_.begin();
153     return (i == bends_.end()) ? NULL : *i;
154 gezelter 1490 }
155    
156 tim 1692 Bend* Molecule::nextBend(std::vector<Bend*>::iterator& i) {
157     ++i;
158     return (i == bends_.end()) ? NULL : *i;
159     }
160 gezelter 1490
161 tim 1692 Torsion* Molecule::beginTorsion(std::vector<Torsion*>::iterator& i) {
162     i = torsions_.begin();
163     return (i == torsions_.end()) ? NULL : *i;
164     }
165 gezelter 1490
166 tim 1692 Torsion* Molecule::nextTorsion(std::vector<Torsion*>::iterator& i) {
167     ++i;
168     return (i == torsions_.end()) ? NULL : *i;
169     }
170 gezelter 1490
171 tim 1692 RigidBody* Molecule::beginRigidBody(std::vector<RigidBody*>::iterator& i) {
172     i = rigidBodies_.begin();
173     return (i == rigidBodies_.end()) ? NULL : *i;
174     }
175 gezelter 1490
176 tim 1692 RigidBody* Molecule::nextRigidBody(std::vector<RigidBody*>::iterator& i) {
177     ++i;
178     return (i == rigidBodies_.end()) ? NULL : *i;
179     }
180 gezelter 1490
181 tim 1692 StuntDouble* Molecule::beginIntegrableObject(std::vector<StuntDouble*>::iterator& i) {
182     i = integrableObjects_.begin();
183     return (i == integrableObjects_.end()) ? NULL : *i;
184 gezelter 1490 }
185    
186 tim 1692 StuntDouble* Molecule::nextIntegrableObject(std::vector<StuntDouble*>::iterator& i) {
187     ++i;
188     return (i == integrableObjects_.end()) ? NULL : *i;
189     }
190 gezelter 1490
191 tim 1692 CutoffGroup* Molecule::beginCutoffGroup(std::vector<CutoffGroup*>::iterator& i) {
192     i = cutoffGroups_.begin();
193     return (i == cutoffGroups_.end()) ? NULL : *i;
194 gezelter 1490 }
195    
196 tim 1692 CutoffGroup* Molecule::nextCutoffGroup(std::vector<CutoffGroup*>::iterator& i) {
197     ++i;
198     return (i == cutoffGroups_.end()) ? NULL : *i;
199     }
200 gezelter 1490
201    
202 tim 1692 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 gezelter 1490
215 tim 1692 com /= totalMass;
216 gezelter 1490
217 tim 1692 return com;
218     }
219 gezelter 1490
220 tim 1695 void Molecule::moveCom(const Vector3d& delta) {
221 tim 1692 StuntDouble* sd;
222     std::vector<StuntDouble*>::iterator i;
223    
224     for (sd = beginIntegrableObject(i); sd != NULL; sd = nextIntegrableObject(i)){
225 tim 1695 sd->setPos(sd->getPos() + delta);
226 gezelter 1490 }
227    
228     }
229    
230 tim 1692 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 gezelter 1490 }
242    
243 tim 1692 velCom /= totalMass;
244 gezelter 1490
245 tim 1692 return velCom;
246 gezelter 1490 }
247    
248 tim 1695 std::ostream& operator <<(std::ostream& o, Molecule& mol) {
249 tim 1692 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 gezelter 1490
259 tim 1692 return o;
260     }
261 gezelter 1490
262 tim 1692 }//end namespace oopse