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, 9 months ago) by tim
File size: 9003 byte(s)
Log Message:
more missing function get implemented

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*> rigidAtoms;
100     RigidBody* rb;
101 tim 1695 std::vector<RigidBody*>::iterator rbIter;
102 gezelter 1490
103 tim 1692
104     for (rb = beginRigidBody(rbIter); rb != NULL; rb = nextRigidBody(rbIter)) {
105 tim 1695 rigidAtoms.insert(rb->getBeginAtomIter(), rb->getEndAtomIter());
106 tim 1692 }
107 gezelter 1490
108 tim 1843 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 tim 1692 //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 tim 1843 //std::set_difference(allAtoms.begin(), allAtoms.end(), rigidAtoms.begin(), rigidAtoms.end(),
121     // std::back_inserter(integrableObjects_));
122 gezelter 1490
123 tim 1843 //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 tim 1733
131 tim 1692 integrableObjects_.insert(integrableObjects_.end(), rigidBodies_.begin(), rigidBodies_.end());
132 gezelter 1490 }
133    
134 tim 1692 Atom* Molecule::beginAtom(std::vector<Atom*>::iterator& i) {
135     i = atoms_.begin();
136     return (i == atoms_.end()) ? NULL : *i;
137     }
138 gezelter 1490
139 tim 1692 Atom* Molecule::nextAtom(std::vector<Atom*>::iterator& i) {
140     ++i;
141     return (i == atoms_.end()) ? NULL : *i;
142     }
143 gezelter 1490
144 tim 1692 Bond* Molecule::beginBond(std::vector<Bond*>::iterator& i) {
145     i = bonds_.begin();
146     return (i == bonds_.end()) ? NULL : *i;
147     }
148 gezelter 1490
149 tim 1692 Bond* Molecule::nextBond(std::vector<Bond*>::iterator& i) {
150     ++i;
151     return (i == bonds_.end()) ? NULL : *i;
152 gezelter 1490
153 tim 1692 }
154 gezelter 1490
155    
156 tim 1692 Bend* Molecule::beginBend(std::vector<Bend*>::iterator& i) {
157     i = bends_.begin();
158     return (i == bends_.end()) ? NULL : *i;
159 gezelter 1490 }
160    
161 tim 1692 Bend* Molecule::nextBend(std::vector<Bend*>::iterator& i) {
162     ++i;
163     return (i == bends_.end()) ? NULL : *i;
164     }
165 gezelter 1490
166 tim 1692 Torsion* Molecule::beginTorsion(std::vector<Torsion*>::iterator& i) {
167     i = torsions_.begin();
168     return (i == torsions_.end()) ? NULL : *i;
169     }
170 gezelter 1490
171 tim 1692 Torsion* Molecule::nextTorsion(std::vector<Torsion*>::iterator& i) {
172     ++i;
173     return (i == torsions_.end()) ? NULL : *i;
174     }
175 gezelter 1490
176 tim 1692 RigidBody* Molecule::beginRigidBody(std::vector<RigidBody*>::iterator& i) {
177     i = rigidBodies_.begin();
178     return (i == rigidBodies_.end()) ? NULL : *i;
179     }
180 gezelter 1490
181 tim 1692 RigidBody* Molecule::nextRigidBody(std::vector<RigidBody*>::iterator& i) {
182     ++i;
183     return (i == rigidBodies_.end()) ? NULL : *i;
184     }
185 gezelter 1490
186 tim 1692 StuntDouble* Molecule::beginIntegrableObject(std::vector<StuntDouble*>::iterator& i) {
187     i = integrableObjects_.begin();
188     return (i == integrableObjects_.end()) ? NULL : *i;
189 gezelter 1490 }
190    
191 tim 1692 StuntDouble* Molecule::nextIntegrableObject(std::vector<StuntDouble*>::iterator& i) {
192     ++i;
193     return (i == integrableObjects_.end()) ? NULL : *i;
194     }
195 gezelter 1490
196 tim 1692 CutoffGroup* Molecule::beginCutoffGroup(std::vector<CutoffGroup*>::iterator& i) {
197     i = cutoffGroups_.begin();
198     return (i == cutoffGroups_.end()) ? NULL : *i;
199 gezelter 1490 }
200    
201 tim 1692 CutoffGroup* Molecule::nextCutoffGroup(std::vector<CutoffGroup*>::iterator& i) {
202     ++i;
203     return (i == cutoffGroups_.end()) ? NULL : *i;
204     }
205 gezelter 1490
206 tim 1843 double Molecule::getMass() {
207     StuntDouble* sd;
208     std::vector<StuntDouble*>::iterator i;
209     double mass = 0.0;
210 gezelter 1490
211 tim 1843 for (sd = beginIntegrableObject(i); sd != NULL; sd = nextIntegrableObject(i)){
212     mass += sd->getMass();
213     }
214    
215     return mass;
216    
217     }
218    
219 tim 1692 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 gezelter 1490
232 tim 1692 com /= totalMass;
233 gezelter 1490
234 tim 1692 return com;
235     }
236 gezelter 1490
237 tim 1695 void Molecule::moveCom(const Vector3d& delta) {
238 tim 1692 StuntDouble* sd;
239     std::vector<StuntDouble*>::iterator i;
240    
241     for (sd = beginIntegrableObject(i); sd != NULL; sd = nextIntegrableObject(i)){
242 tim 1695 sd->setPos(sd->getPos() + delta);
243 gezelter 1490 }
244    
245     }
246    
247 tim 1692 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 gezelter 1490 }
259    
260 tim 1692 velCom /= totalMass;
261 gezelter 1490
262 tim 1692 return velCom;
263 gezelter 1490 }
264    
265 tim 1843 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 tim 1695 std::ostream& operator <<(std::ostream& o, Molecule& mol) {
293 tim 1692 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 gezelter 1490
303 tim 1692 return o;
304     }
305 gezelter 1490
306 tim 1692 }//end namespace oopse