ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/src/brains/MoleculeCreator.cpp
Revision: 2087
Committed: Tue Mar 8 21:06:49 2005 UTC (19 years, 4 months ago) by gezelter
File size: 17498 byte(s)
Log Message:
Using OOPSE_itoa and checking a NULL left over from the LinkedList days

File Contents

# User Rev Content
1 gezelter 1930 /*
2     * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3     *
4     * The University of Notre Dame grants you ("Licensee") a
5     * non-exclusive, royalty free, license to use, modify and
6     * redistribute this software in source and binary code form, provided
7     * that the following conditions are met:
8     *
9     * 1. Acknowledgement of the program authors must be made in any
10     * publication of scientific results based in part on use of the
11     * program. An acceptable form of acknowledgement is citation of
12     * the article in which the program was described (Matthew
13     * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14     * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15     * Parallel Simulation Engine for Molecular Dynamics,"
16     * J. Comput. Chem. 26, pp. 252-271 (2005))
17     *
18     * 2. Redistributions of source code must retain the above copyright
19     * notice, this list of conditions and the following disclaimer.
20     *
21     * 3. Redistributions in binary form must reproduce the above copyright
22     * notice, this list of conditions and the following disclaimer in the
23     * documentation and/or other materials provided with the
24     * distribution.
25     *
26     * This software is provided "AS IS," without a warranty of any
27     * kind. All express or implied conditions, representations and
28     * warranties, including any implied warranty of merchantability,
29     * fitness for a particular purpose or non-infringement, are hereby
30     * excluded. The University of Notre Dame and its licensors shall not
31     * be liable for any damages suffered by licensee as a result of
32     * using, modifying or distributing the software or its
33     * derivatives. In no event will the University of Notre Dame or its
34     * licensors be liable for any lost revenue, profit or data, or for
35     * direct, indirect, special, consequential, incidental or punitive
36     * damages, however caused and regardless of the theory of liability,
37     * arising out of the use of or inability to use software, even if the
38     * University of Notre Dame has been advised of the possibility of
39     * such damages.
40     */
41    
42     /**
43     * @file MoleculeCreator.cpp
44     * @author tlin
45     * @date 11/04/2004
46     * @time 13:44am
47     * @version 1.0
48     */
49    
50     #include <cassert>
51     #include <set>
52    
53     #include "brains/MoleculeCreator.hpp"
54     #include "primitives/GhostBend.hpp"
55 tim 1957 #include "primitives/GhostTorsion.hpp"
56 gezelter 1930 #include "types/DirectionalAtomType.hpp"
57     #include "types/FixedBondType.hpp"
58     #include "utils/simError.h"
59     #include "utils/StringUtils.hpp"
60    
61     namespace oopse {
62    
63     Molecule* MoleculeCreator::createMolecule(ForceField* ff, MoleculeStamp *molStamp,
64     int stampId, int globalIndex, LocalIndexManager* localIndexMan) {
65    
66     Molecule* mol = new Molecule(stampId, globalIndex, molStamp->getID());
67    
68     //create atoms
69     Atom* atom;
70     AtomStamp* currentAtomStamp;
71     int nAtom = molStamp->getNAtoms();
72     for (int i = 0; i < nAtom; ++i) {
73     currentAtomStamp = molStamp->getAtom(i);
74     atom = createAtom(ff, mol, currentAtomStamp, localIndexMan);
75     mol->addAtom(atom);
76     }
77    
78     //create rigidbodies
79     RigidBody* rb;
80     RigidBodyStamp * currentRigidBodyStamp;
81     int nRigidbodies = molStamp->getNRigidBodies();
82    
83     for (int i = 0; i < nRigidbodies; ++i) {
84     currentRigidBodyStamp = molStamp->getRigidBody(i);
85     rb = createRigidBody(molStamp, mol, currentRigidBodyStamp, localIndexMan);
86     mol->addRigidBody(rb);
87     }
88    
89     //create bonds
90     Bond* bond;
91     BondStamp* currentBondStamp;
92     int nBonds = molStamp->getNBonds();
93    
94     for (int i = 0; i < nBonds; ++i) {
95     currentBondStamp = molStamp->getBond(i);
96     bond = createBond(ff, mol, currentBondStamp);
97     mol->addBond(bond);
98     }
99    
100     //create bends
101     Bend* bend;
102     BendStamp* currentBendStamp;
103     int nBends = molStamp->getNBends();
104     for (int i = 0; i < nBends; ++i) {
105     currentBendStamp = molStamp->getBend(i);
106     bend = createBend(ff, mol, currentBendStamp);
107     mol->addBend(bend);
108     }
109    
110     //create torsions
111     Torsion* torsion;
112     TorsionStamp* currentTorsionStamp;
113     int nTorsions = molStamp->getNTorsions();
114     for (int i = 0; i < nTorsions; ++i) {
115     currentTorsionStamp = molStamp->getTorsion(i);
116     torsion = createTorsion(ff, mol, currentTorsionStamp);
117     mol->addTorsion(torsion);
118     }
119    
120     //create cutoffGroups
121     CutoffGroup* cutoffGroup;
122     CutoffGroupStamp* currentCutoffGroupStamp;
123     int nCutoffGroups = molStamp->getNCutoffGroups();
124     for (int i = 0; i < nCutoffGroups; ++i) {
125     currentCutoffGroupStamp = molStamp->getCutoffGroup(i);
126     cutoffGroup = createCutoffGroup(mol, currentCutoffGroupStamp);
127     mol->addCutoffGroup(cutoffGroup);
128     }
129    
130     //every free atom is a cutoff group
131     std::set<Atom*> allAtoms;
132     Molecule::AtomIterator ai;
133    
134     //add all atoms into allAtoms set
135     for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
136     allAtoms.insert(atom);
137     }
138    
139     Molecule::CutoffGroupIterator ci;
140     CutoffGroup* cg;
141     std::set<Atom*> cutoffAtoms;
142    
143     //add all of the atoms belong to cutoff groups into cutoffAtoms set
144     for (cg = mol->beginCutoffGroup(ci); cg != NULL; cg = mol->nextCutoffGroup(ci)) {
145    
146     for(atom = cg->beginAtom(ai); atom != NULL; atom = cg->nextAtom(ai)) {
147     cutoffAtoms.insert(atom);
148     }
149    
150     }
151    
152     //find all free atoms (which do not belong to cutoff groups)
153     //performs the "difference" operation from set theory, the output range contains a copy of every
154     //element that is contained in [allAtoms.begin(), allAtoms.end()) and not contained in
155     //[cutoffAtoms.begin(), cutoffAtoms.end()).
156     std::vector<Atom*> freeAtoms;
157     std::set_difference(allAtoms.begin(), allAtoms.end(), cutoffAtoms.begin(), cutoffAtoms.end(),
158     std::back_inserter(freeAtoms));
159    
160     if (freeAtoms.size() != allAtoms.size() - cutoffAtoms.size()) {
161     //Some atoms in rigidAtoms are not in allAtoms, something must be wrong
162     sprintf(painCave.errMsg, "Atoms in cutoff groups are not in the atom list of the same molecule");
163    
164     painCave.isFatal = 1;
165     simError();
166     }
167    
168     //loop over the free atoms and then create one cutoff group for every single free atom
169     std::vector<Atom*>::iterator fai;
170    
171     for (fai = freeAtoms.begin(); fai != freeAtoms.end(); ++fai) {
172     cutoffGroup = createCutoffGroup(mol, *fai);
173     mol->addCutoffGroup(cutoffGroup);
174     }
175     //create constraints
176     createConstraintPair(mol);
177     createConstraintElem(mol);
178    
179     //the construction of this molecule is finished
180     mol->complete();
181    
182     return mol;
183     }
184    
185    
186     Atom* MoleculeCreator::createAtom(ForceField* ff, Molecule* mol, AtomStamp* stamp,
187     LocalIndexManager* localIndexMan) {
188     AtomType * atomType;
189     Atom* atom;
190    
191     atomType = ff->getAtomType(stamp->getType());
192    
193     if (atomType == NULL) {
194     sprintf(painCave.errMsg, "Can not find Matching Atom Type for[%s]",
195     stamp->getType());
196    
197     painCave.isFatal = 1;
198     simError();
199     }
200    
201     //below code still have some kind of hard-coding smell
202     if (atomType->isDirectional()){
203    
204     DirectionalAtomType* dAtomType = dynamic_cast<DirectionalAtomType*>(atomType);
205    
206     if (dAtomType == NULL) {
207     sprintf(painCave.errMsg, "Can not cast AtomType to DirectionalAtomType");
208    
209     painCave.isFatal = 1;
210     simError();
211     }
212    
213     DirectionalAtom* dAtom;
214     dAtom = new DirectionalAtom(dAtomType);
215     atom = dAtom;
216     }
217     else{
218     atom = new Atom(atomType);
219     }
220    
221     atom->setLocalIndex(localIndexMan->getNextAtomIndex());
222    
223     return atom;
224     }
225    
226     RigidBody* MoleculeCreator::createRigidBody(MoleculeStamp *molStamp, Molecule* mol,
227     RigidBodyStamp* rbStamp,
228     LocalIndexManager* localIndexMan) {
229     Atom* atom;
230     int nAtoms;
231     Vector3d refCoor;
232     AtomStamp* atomStamp;
233    
234     RigidBody* rb = new RigidBody();
235     nAtoms = rbStamp->getNMembers();
236     for (int i = 0; i < nAtoms; ++i) {
237     //rbStamp->getMember(i) return the local index of current atom inside the molecule.
238     //It is not the same as local index of atom which is the index of atom at DataStorage class
239     atom = mol->getAtomAt(rbStamp->getMember(i));
240     atomStamp= molStamp->getAtom(rbStamp->getMember(i));
241     rb->addAtom(atom, atomStamp);
242     }
243    
244     //after all of the atoms are added, we need to calculate the reference coordinates
245     rb->calcRefCoords();
246    
247     //set the local index of this rigid body, global index will be set later
248     rb->setLocalIndex(localIndexMan->getNextRigidBodyIndex());
249    
250     //the rule for naming rigidbody MoleculeName_RB_Integer
251     //The first part is the name of the molecule
252     //The second part is alway fixed as "RB"
253     //The third part is the index of the rigidbody defined in meta-data file
254     //For example, Butane_RB_0 is a valid rigid body name of butane molecule
255     /**@todo replace itoa by lexi_cast */
256 gezelter 2087 std::string s = OOPSE_itoa(mol->getNRigidBodies(), 10);
257     rb->setType(mol->getType() + "_RB_" + s.c_str());
258 tim 1976
259 gezelter 1930 return rb;
260     }
261    
262     Bond* MoleculeCreator::createBond(ForceField* ff, Molecule* mol, BondStamp* stamp) {
263     BondType* bondType;
264     Atom* atomA;
265     Atom* atomB;
266    
267     atomA = mol->getAtomAt(stamp->getA());
268     atomB = mol->getAtomAt(stamp->getB());
269    
270     assert( atomA && atomB);
271    
272     bondType = ff->getBondType(atomA->getType(), atomB->getType());
273    
274     if (bondType == NULL) {
275     sprintf(painCave.errMsg, "Can not find Matching Bond Type for[%s, %s]",
276     atomA->getType().c_str(),
277     atomB->getType().c_str());
278    
279     painCave.isFatal = 1;
280     simError();
281     }
282     return new Bond(atomA, atomB, bondType);
283     }
284    
285     Bend* MoleculeCreator::createBend(ForceField* ff, Molecule* mol, BendStamp* stamp) {
286     bool isGhostBend = false;
287     int ghostIndex;
288    
289    
290     //
291     if (stamp->haveExtras()){
292     LinkedAssign* extras = stamp->getExtras();
293     LinkedAssign* currentExtra = extras;
294    
295     while (currentExtra != NULL){
296     if (!strcmp(currentExtra->getlhs(), "ghostVectorSource")){
297     switch (currentExtra->getType()){
298     case 0:
299     ghostIndex = currentExtra->getInt();
300     isGhostBend = true;
301     break;
302    
303     default:
304     sprintf(painCave.errMsg,
305     "SimSetup Error: ghostVectorSource must be an int.\n");
306     painCave.isFatal = 1;
307     simError();
308     }
309     } else{
310     sprintf(painCave.errMsg,
311     "SimSetup Error: unhandled bend assignment:\n");
312     painCave.isFatal = 1;
313     simError();
314     }
315     currentExtra = currentExtra->getNext();
316     }
317    
318     }
319    
320     if (isGhostBend) {
321    
322     int indexA = stamp->getA();
323     int indexB= stamp->getB();
324    
325     assert(indexA != indexB);
326    
327     int normalIndex;
328     if (indexA == ghostIndex) {
329     normalIndex = indexB;
330     } else if (indexB == ghostIndex) {
331     normalIndex = indexA;
332     }
333    
334     Atom* normalAtom = mol->getAtomAt(normalIndex) ;
335     DirectionalAtom* ghostAtom = dynamic_cast<DirectionalAtom*>(mol->getAtomAt(ghostIndex));
336     if (ghostAtom == NULL) {
337     sprintf(painCave.errMsg, "Can not cast Atom to DirectionalAtom");
338     painCave.isFatal = 1;
339     simError();
340     }
341    
342     BendType* bendType = ff->getBendType(normalAtom->getType(), ghostAtom->getType(), "GHOST");
343    
344     if (bendType == NULL) {
345     sprintf(painCave.errMsg, "Can not find Matching Bend Type for[%s, %s, %s]",
346     normalAtom->getType().c_str(),
347     ghostAtom->getType().c_str(),
348     "GHOST");
349    
350     painCave.isFatal = 1;
351     simError();
352     }
353    
354     return new GhostBend(normalAtom, ghostAtom, bendType);
355    
356     } else {
357    
358     Atom* atomA = mol->getAtomAt(stamp->getA());
359     Atom* atomB = mol->getAtomAt(stamp->getB());
360     Atom* atomC = mol->getAtomAt(stamp->getC());
361    
362     assert( atomA && atomB && atomC);
363    
364     BendType* bendType = ff->getBendType(atomA->getType(), atomB->getType(), atomC->getType());
365    
366     if (bendType == NULL) {
367     sprintf(painCave.errMsg, "Can not find Matching Bend Type for[%s, %s, %s]",
368     atomA->getType().c_str(),
369     atomB->getType().c_str(),
370     atomC->getType().c_str());
371    
372     painCave.isFatal = 1;
373     simError();
374     }
375    
376     return new Bend(atomA, atomB, atomC, bendType);
377     }
378     }
379    
380     Torsion* MoleculeCreator::createTorsion(ForceField* ff, Molecule* mol, TorsionStamp* stamp) {
381    
382 tim 1957 Atom* atomA = mol->getAtomAt(stamp->getA());
383     Atom* atomB = mol->getAtomAt(stamp->getB());
384     Atom* atomC = mol->getAtomAt(stamp->getC());
385     Torsion* torsion;
386 gezelter 1930
387 tim 1957 if (stamp->getD() != -1) {
388     Atom* atomD = mol->getAtomAt(stamp->getD());
389 gezelter 1930
390 tim 1957 assert(atomA && atomB && atomC && atomD);
391    
392     TorsionType* torsionType = ff->getTorsionType(atomA->getType(), atomB->getType(),
393     atomC->getType(), atomD->getType());
394 gezelter 1930
395 tim 1957 if (torsionType == NULL) {
396     sprintf(painCave.errMsg, "Can not find Matching Torsion Type for[%s, %s, %s, %s]",
397     atomA->getType().c_str(),
398     atomB->getType().c_str(),
399     atomC->getType().c_str(),
400     atomD->getType().c_str());
401    
402     painCave.isFatal = 1;
403     simError();
404     }
405    
406     torsion = new Torsion(atomA, atomB, atomC, atomD, torsionType);
407 gezelter 1930 }
408 tim 1957 else {
409    
410     DirectionalAtom* dAtom = dynamic_cast<DirectionalAtom*>(atomC);
411     if (dAtom == NULL) {
412     sprintf(painCave.errMsg, "Can not cast Atom to DirectionalAtom");
413     painCave.isFatal = 1;
414     simError();
415     }
416    
417     TorsionType* torsionType = ff->getTorsionType(atomA->getType(), atomB->getType(),
418     atomC->getType(), "GHOST");
419    
420     if (torsionType == NULL) {
421     sprintf(painCave.errMsg, "Can not find Matching Torsion Type for[%s, %s, %s, %s]",
422     atomA->getType().c_str(),
423     atomB->getType().c_str(),
424     atomC->getType().c_str(),
425     "GHOST");
426    
427     painCave.isFatal = 1;
428     simError();
429     }
430    
431     torsion = new GhostTorsion(atomA, atomB, dAtom, torsionType);
432     }
433    
434     return torsion;
435 gezelter 1930 }
436    
437     CutoffGroup* MoleculeCreator::createCutoffGroup(Molecule* mol, CutoffGroupStamp* stamp) {
438     int nAtoms;
439     CutoffGroup* cg;
440     Atom* atom;
441     cg = new CutoffGroup();
442    
443     nAtoms = stamp->getNMembers();
444     for (int i =0; i < nAtoms; ++i) {
445     atom = mol->getAtomAt(stamp->getMember(i));
446     assert(atom);
447     cg->addAtom(atom);
448     }
449    
450     return cg;
451     }
452    
453     CutoffGroup* MoleculeCreator::createCutoffGroup(Molecule * mol, Atom* atom) {
454     CutoffGroup* cg;
455     cg = new CutoffGroup();
456     cg->addAtom(atom);
457     return cg;
458     }
459    
460     void MoleculeCreator::createConstraintPair(Molecule* mol) {
461    
462     //add bond constraints
463     Molecule::BondIterator bi;
464     Bond* bond;
465     for (bond = mol->beginBond(bi); bond != NULL; bond = mol->nextBond(bi)) {
466    
467     BondType* bt = bond->getBondType();
468    
469     //class Parent1 {};
470     //class Child1 : public Parent {};
471     //class Child2 : public Parent {};
472     //Child1* ch1 = new Child1();
473     //Child2* ch2 = dynamic_cast<Child2*>(ch1);
474     //the dynamic_cast is succeed in above line. A compiler bug?
475    
476     if (typeid(FixedBondType) == typeid(*bt)) {
477     FixedBondType* fbt = dynamic_cast<FixedBondType*>(bt);
478    
479     ConstraintElem* consElemA = new ConstraintElem(bond->getAtomA());
480     ConstraintElem* consElemB = new ConstraintElem(bond->getAtomB());
481     ConstraintPair* consPair = new ConstraintPair(consElemA, consElemB, fbt->getEquilibriumBondLength());
482     mol->addConstraintPair(consPair);
483     }
484     }
485    
486     //rigidbody -- rigidbody constraint is not support yet
487     }
488    
489     void MoleculeCreator::createConstraintElem(Molecule* mol) {
490    
491     ConstraintPair* consPair;
492     Molecule::ConstraintPairIterator cpi;
493     std::set<StuntDouble*> sdSet;
494     for (consPair = mol->beginConstraintPair(cpi); consPair != NULL; consPair = mol->nextConstraintPair(cpi)) {
495    
496     StuntDouble* sdA = consPair->getConsElem1()->getStuntDouble();
497     if (sdSet.find(sdA) == sdSet.end()){
498     sdSet.insert(sdA);
499     mol->addConstraintElem(new ConstraintElem(sdA));
500     }
501    
502     StuntDouble* sdB = consPair->getConsElem2()->getStuntDouble();
503     if (sdSet.find(sdB) == sdSet.end()){
504     sdSet.insert(sdB);
505     mol->addConstraintElem(new ConstraintElem(sdB));
506     }
507    
508     }
509    
510     }
511    
512     }

Properties

Name Value
svn:executable *