ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-4/src/brains/MoleculeCreator.cpp
Revision: 1813
Committed: Wed Dec 1 17:38:32 2004 UTC (19 years, 9 months ago) by tim
File size: 13528 byte(s)
Log Message:
refactory AtomType

File Contents

# User Rev Content
1 tim 1719 /*
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 MoleculeCreator.cpp
28     * @author tlin
29     * @date 11/04/2004
30     * @time 13:44am
31     * @version 1.0
32     */
33    
34     #include <cassert>
35    
36 tim 1805 #include "brains/MoleculeCreator.hpp"
37     #include "primitives/GhostBend.hpp"
38     #include "utils/simError.h"
39     #include "utils/StringUtils.hpp"
40    
41 tim 1719 namespace oopse {
42    
43 tim 1805 Molecule* MoleculeCreator::createMolecule(ForceField* ff, MoleculeStamp *molStamp,
44     int stampId, int globalIndex, LocalIndexManager* localIndexMan) {
45    
46 tim 1733 Molecule* mol = new Molecule(stampId, globalIndex, molStamp->getID());
47 tim 1719
48     //create atoms
49     Atom* atom;
50     AtomStamp* currentAtomStamp;
51     int nAtom = molStamp->getNAtoms();
52     for (int i = 0; i < nAtom; ++i) {
53     currentAtomStamp = molStamp->getAtom(i);
54 tim 1805 atom = createAtom(ff, mol, currentAtomStamp, localIndexMan);
55 tim 1719 mol->addAtom(atom);
56     }
57    
58     //create rigidbodies
59     RigidBody* rb;
60     RigidBodyStamp * currentRigidBodyStamp;
61     int nRigidbodies = molStamp->getNRigidBodies();
62    
63     for (int i = 0; i < nRigidbodies; ++i) {
64     currentRigidBodyStamp = molStamp->getRigidBody(i);
65 tim 1805 rb = createRigidBody(molStamp, mol, currentRigidBodyStamp, localIndexMan);
66 tim 1719 mol->addRigidBody(rb);
67     }
68    
69     //create bonds
70     Bond* bond;
71     BondStamp* currentBondStamp;
72     int nBonds = molStamp->getNBends();
73    
74     for (int i = 0; i < nBonds; ++i) {
75 tim 1805 currentBondStamp = molStamp->getBond(i);
76 tim 1719 bond = createBond(ff, mol, currentBondStamp);
77     mol->addBond(bond);
78     }
79    
80     //create bends
81     Bend* bend;
82     BendStamp* currentBendStamp;
83     int nBends = molStamp->getNBends();
84     for (int i = 0; i < nBends; ++i) {
85     currentBendStamp = molStamp->getBend(i);
86     bend = createBend(ff, mol, currentBendStamp);
87     mol->addBend(bend);
88     }
89    
90     //create torsions
91     Torsion* torsion;
92     TorsionStamp* currentTorsionStamp;
93     int nTorsions = molStamp->getNTorsions();
94     for (int i = 0; i < nTorsions; ++i) {
95     currentTorsionStamp = molStamp->getTorsion(i);
96     torsion = createTorsion(ff, mol, currentTorsionStamp);
97     mol->addTorsion(torsion);
98     }
99    
100     //create cutoffGroups
101     CutoffGroup* cutoffGroup;
102     CutoffGroupStamp* currentCutoffGroupStamp;
103     int nCutoffGroups = molStamp->getNCutoffGroups();
104     for (int i = 0; i < nCutoffGroups; ++i) {
105     currentCutoffGroupStamp = molStamp->getCutoffGroup(i);
106 tim 1805 cutoffGroup = createCutoffGroup(mol, currentCutoffGroupStamp);
107 tim 1719 mol->addCutoffGroup(cutoffGroup);
108     }
109    
110 tim 1734 //every free atom is a cutoff group
111     std::set<Atom*> allAtoms;
112 tim 1805 Molecule::AtomIterator ai;
113 tim 1734
114     //add all atoms into allAtoms set
115     for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
116     allAtoms.insert(atom);
117     }
118    
119 tim 1805 Molecule::CutoffGroupIterator ci;
120 tim 1734 CutoffGroup* cg;
121     std::set<Atom*> cutoffAtoms;
122    
123     //add all of the atoms belong to cutoff groups into cutoffAtoms set
124     for (cg = mol->beginCutoffGroup(ci); cg != NULL; cg = mol->nextCutoffGroup(ci)) {
125    
126     for(atom = cg->beginAtom(ai); atom != NULL; atom = cg->nextAtom(ai)) {
127     cutoffAtoms.insert(atom);
128     }
129    
130     }
131    
132     //find all free atoms (which do not belong to cutoff groups)
133     //performs the "difference" operation from set theory, the output range contains a copy of every
134     //element that is contained in [allAtoms.begin(), allAtoms.end()) and not contained in
135     //[cutoffAtoms.begin(), cutoffAtoms.end()).
136     std::vector<Atom*> freeAtoms;
137     std::set_difference(allAtoms.begin(), allAtoms.end(), cutoffAtoms.begin(), cutoffAtoms.end(),
138 tim 1805 std::back_inserter(freeAtoms));
139 tim 1734
140     if (freeAtoms.size() != allAtoms.size() - cutoffAtoms.size()) {
141     //Some atoms in rigidAtoms are not in allAtoms, something must be wrong
142     sprintf(painCave.errMsg, "Atoms in cutoff groups are not in the atom list of the same molecule");
143    
144     painCave.isFatal = 1;
145     simError();
146     }
147    
148     //loop over the free atoms and then create one cutoff group for every single free atom
149     std::vector<Atom*>::iterator fai;
150    
151     for (fai = freeAtoms.begin(); fai != freeAtoms.end(); ++fai) {
152     createCutoffGroup(mol, *fai);
153     mol->addCutoffGroup(cutoffGroup);
154     }
155 tim 1719 //create constraints
156    
157     //the construction of this molecule is finished
158     mol->complete();
159    
160     return mol;
161     }
162    
163    
164     Atom* MoleculeCreator::createAtom(ForceField* ff, Molecule* mol, AtomStamp* stamp,
165     LocalIndexManager* localIndexMan) {
166     AtomType * atomType;
167     Atom* atom;
168    
169     atomType = ff->getAtomType(stamp->getType());
170    
171 tim 1804 if (atomType == NULL) {
172 tim 1733 sprintf(painCave.errMsg, "Can not find Matching Atom Type for[%s]",
173     stamp->getType());
174    
175     painCave.isFatal = 1;
176     simError();
177     }
178    
179 tim 1719 //below code still have some kind of hard-coding smell
180 tim 1813 if (atomType->isDirectional()){
181    
182 tim 1804 DirectionalAtomType* dAtomType = dynamic_cast<DirectionalAtomType*>(atomType);
183 tim 1813
184 tim 1804 if (dAtomType == NULL) {
185     sprintf(painCave.errMsg, "Can not cast AtomType to DirectionalAtomType");
186    
187     painCave.isFatal = 1;
188     simError();
189     }
190 tim 1813
191     DirectionalAtom* dAtom;
192 tim 1804 dAtom = new DirectionalAtom(dAtomType);
193 tim 1719 atom = dAtom;
194     }
195     else{
196     atom = new Atom(atomType);
197     }
198    
199     atom->setLocalIndex(localIndexMan->getNextAtomIndex());
200 tim 1805
201     return atom;
202 tim 1719 }
203    
204     RigidBody* MoleculeCreator::createRigidBody(MoleculeStamp *molStamp, Molecule* mol,
205     RigidBodyStamp* rbStamp,
206     LocalIndexManager* localIndexMan) {
207     Atom* atom;
208     int nAtoms;
209     Vector3d refCoor;
210     AtomStamp* atomStamp;
211    
212     RigidBody* rb = new RigidBody();
213 tim 1805 nAtoms = rbStamp->getNMembers();
214 tim 1719 for (int i = 0; i < nAtoms; ++i) {
215     //rbStamp->getMember(i) return the local index of current atom inside the molecule.
216     //It is not the same as local index of atom which is the index of atom at DataStorage class
217     atom = mol->getAtomAt(rbStamp->getMember(i));
218 tim 1805 atomStamp= molStamp->getAtom(rbStamp->getMember(i));
219 tim 1719 rb->addAtom(atom, atomStamp);
220     }
221 tim 1733
222     //after all of the atoms are added, we need to calculate the reference coordinates
223 tim 1719 rb->calcRefCoords();
224 tim 1733
225     //set the local index of this rigid body, global index will be set later
226 tim 1719 rb->setLocalIndex(localIndexMan->getNextRigidBodyIndex());
227 tim 1733
228     //the rule for naming rigidbody MoleculeName_RB_Integer
229     //The first part is the name of the molecule
230     //The second part is alway fixed as "RB"
231     //The third part is the index of the rigidbody defined in meta-data file
232     //For example, Butane_RB_0 is a valid rigid body name of butane molecule
233 tim 1805 /**@todo replace itoa by lexi_cast */
234     rb->setType(mol->getType() + "_RB_" + toString(mol->getNRigidBodies()));
235 tim 1719
236     return rb;
237     }
238    
239     Bond* MoleculeCreator::createBond(ForceField* ff, Molecule* mol, BondStamp* stamp) {
240     BondType* bondType;
241     Atom* atomA;
242     Atom* atomB;
243    
244     atomA = mol->getAtomAt(stamp->getA());
245     atomB = mol->getAtomAt(stamp->getB());
246    
247     assert( atomA && atomB);
248    
249 tim 1805 bondType = ff->getBondType(atomA->getType(), atomB->getType());
250 tim 1719
251 tim 1733 if (bondType == NULL) {
252     sprintf(painCave.errMsg, "Can not find Matching Bond Type for[%s, %s]",
253     atomA->getType().c_str(),
254     atomB->getType().c_str());
255    
256     painCave.isFatal = 1;
257     simError();
258     }
259 tim 1719 return new Bond(atomA, atomB, bondType);
260     }
261    
262     Bend* MoleculeCreator::createBend(ForceField* ff, Molecule* mol, BendStamp* stamp) {
263 tim 1774 bool isGhostBend = false;
264     int ghostIndex;
265 tim 1719
266    
267 tim 1774 //
268     if (stamp->haveExtras()){
269 tim 1805 LinkedAssign* extras = stamp->getExtras();
270 tim 1774 LinkedAssign* currentExtra = extras;
271 tim 1719
272 tim 1774 while (currentExtra != NULL){
273     if (!strcmp(currentExtra->getlhs(), "ghostVectorSource")){
274     switch (currentExtra->getType()){
275     case 0:
276     ghostIndex = currentExtra->getInt();
277     isGhostBend = true;
278     break;
279 tim 1719
280 tim 1774 default:
281     sprintf(painCave.errMsg,
282     "SimSetup Error: ghostVectorSource must be an int.\n");
283     painCave.isFatal = 1;
284     simError();
285     }
286     } else{
287     sprintf(painCave.errMsg,
288     "SimSetup Error: unhandled bend assignment:\n");
289     painCave.isFatal = 1;
290     simError();
291     }
292     currentExtra = currentExtra->getNext();
293     }
294    
295 tim 1733 }
296    
297 tim 1774 if (isGhostBend) {
298 tim 1733
299 tim 1774 int indexA = stamp->getA();
300     int indexB= stamp->getB();
301    
302     assert(indexA != indexB);
303    
304     int normalIndex;
305     if (indexA == ghostIndex) {
306     normalIndex = indexB;
307     } else if (indexB == ghostIndex) {
308     normalIndex = indexA;
309     }
310    
311 tim 1805 Atom* normalAtom = mol->getAtomAt(normalIndex) ;
312     DirectionalAtom* ghostAtom = dynamic_cast<DirectionalAtom*>(mol->getAtomAt(ghostIndex));
313     if (ghostAtom == NULL) {
314     sprintf(painCave.errMsg, "Can not cast Atom to DirectionalAtom");
315     painCave.isFatal = 1;
316     simError();
317     }
318    
319 tim 1774 BendType* bendType = ff->getBendType(normalAtom->getType(), ghostAtom->getType(), "GHOST");
320    
321     if (bendType == NULL) {
322     sprintf(painCave.errMsg, "Can not find Matching Bend Type for[%s, %s, %s]",
323     normalAtom->getType().c_str(),
324     ghostAtom->getType().c_str(),
325     "GHOST");
326    
327     painCave.isFatal = 1;
328     simError();
329     }
330 tim 1805
331 tim 1774 return new GhostBend(normalAtom, ghostAtom, bendType);
332    
333     } else {
334    
335     Atom* atomA = mol->getAtomAt(stamp->getA());
336     Atom* atomB = mol->getAtomAt(stamp->getB());
337     Atom* atomC = mol->getAtomAt(stamp->getC());
338    
339     assert( atomA && atomB && atomC);
340    
341     BendType* bendType = ff->getBendType(atomA->getType(), atomB->getType(), atomC->getType());
342    
343     if (bendType == NULL) {
344     sprintf(painCave.errMsg, "Can not find Matching Bend Type for[%s, %s, %s]",
345     atomA->getType().c_str(),
346     atomB->getType().c_str(),
347     atomC->getType().c_str());
348    
349     painCave.isFatal = 1;
350     simError();
351     }
352    
353     return new Bend(atomA, atomB, atomC, bendType);
354     }
355 tim 1719 }
356    
357     Torsion* MoleculeCreator::createTorsion(ForceField* ff, Molecule* mol, TorsionStamp* stamp) {
358     TorsionType* torsionType;
359     Atom* atomA;
360     Atom* atomB;
361     Atom* atomC;
362     Atom* atomD;
363    
364     atomA = mol->getAtomAt(stamp->getA());
365     atomB = mol->getAtomAt(stamp->getB());
366     atomC = mol->getAtomAt(stamp->getC());
367     atomD = mol->getAtomAt(stamp->getD());
368    
369     assert(atomA && atomB && atomC && atomD);
370    
371 tim 1805 torsionType = ff->getTorsionType(atomA->getType(), atomB->getType(),
372 tim 1719 atomC->getType(), atomD->getType());
373    
374 tim 1733 if (torsionType == NULL) {
375     sprintf(painCave.errMsg, "Can not find Matching Torsion Type for[%s, %s, %s, %s]",
376     atomA->getType().c_str(),
377     atomB->getType().c_str(),
378     atomC->getType().c_str(),
379     atomD->getType().c_str());
380    
381     painCave.isFatal = 1;
382     simError();
383     }
384    
385 tim 1805 return new Torsion(atomA, atomB, atomC, atomD, torsionType);
386 tim 1719 }
387    
388     CutoffGroup* MoleculeCreator::createCutoffGroup(Molecule* mol, CutoffGroupStamp* stamp) {
389     int nAtoms;
390     CutoffGroup* cg;
391     Atom* atom;
392     cg = new CutoffGroup();
393    
394     nAtoms = stamp->getNMembers();
395     for (int i =0; i < nAtoms; ++i) {
396     atom = mol->getAtomAt(stamp->getMember(i));
397     assert(atom);
398     cg->addAtom(atom);
399     }
400    
401     return cg;
402     }
403    
404 tim 1734 CutoffGroup* MoleculeCreator::createCutoffGroup(Molecule * mol, Atom* atom) {
405     CutoffGroup* cg;
406     cg = new CutoffGroup();
407 tim 1805 cg->addAtom(atom);
408 tim 1734 return cg;
409     }
410 tim 1719 //Constraint* MoleculeCreator::createConstraint() {
411    
412     //}
413    
414     }

Properties

Name Value
svn:executable *