ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/brains/MoleculeCreator.cpp
Revision: 1957
Committed: Tue Jan 25 17:45:23 2005 UTC (19 years, 5 months ago) by tim
File size: 17465 byte(s)
Log Message:
(1) complete section parser's error message
(2) add GhostTorsion
(3) accumulate inertial tensor from the directional atoms before calculate rigidbody's inertial tensor

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     rb->setType(mol->getType() + "_RB_" + toString(mol->getNRigidBodies()));
257    
258     return rb;
259     }
260    
261     Bond* MoleculeCreator::createBond(ForceField* ff, Molecule* mol, BondStamp* stamp) {
262     BondType* bondType;
263     Atom* atomA;
264     Atom* atomB;
265    
266     atomA = mol->getAtomAt(stamp->getA());
267     atomB = mol->getAtomAt(stamp->getB());
268    
269     assert( atomA && atomB);
270    
271     bondType = ff->getBondType(atomA->getType(), atomB->getType());
272    
273     if (bondType == NULL) {
274     sprintf(painCave.errMsg, "Can not find Matching Bond Type for[%s, %s]",
275     atomA->getType().c_str(),
276     atomB->getType().c_str());
277    
278     painCave.isFatal = 1;
279     simError();
280     }
281     return new Bond(atomA, atomB, bondType);
282     }
283    
284     Bend* MoleculeCreator::createBend(ForceField* ff, Molecule* mol, BendStamp* stamp) {
285     bool isGhostBend = false;
286     int ghostIndex;
287    
288    
289     //
290     if (stamp->haveExtras()){
291     LinkedAssign* extras = stamp->getExtras();
292     LinkedAssign* currentExtra = extras;
293    
294     while (currentExtra != NULL){
295     if (!strcmp(currentExtra->getlhs(), "ghostVectorSource")){
296     switch (currentExtra->getType()){
297     case 0:
298     ghostIndex = currentExtra->getInt();
299     isGhostBend = true;
300     break;
301    
302     default:
303     sprintf(painCave.errMsg,
304     "SimSetup Error: ghostVectorSource must be an int.\n");
305     painCave.isFatal = 1;
306     simError();
307     }
308     } else{
309     sprintf(painCave.errMsg,
310     "SimSetup Error: unhandled bend assignment:\n");
311     painCave.isFatal = 1;
312     simError();
313     }
314     currentExtra = currentExtra->getNext();
315     }
316    
317     }
318    
319     if (isGhostBend) {
320    
321     int indexA = stamp->getA();
322     int indexB= stamp->getB();
323    
324     assert(indexA != indexB);
325    
326     int normalIndex;
327     if (indexA == ghostIndex) {
328     normalIndex = indexB;
329     } else if (indexB == ghostIndex) {
330     normalIndex = indexA;
331     }
332    
333     Atom* normalAtom = mol->getAtomAt(normalIndex) ;
334     DirectionalAtom* ghostAtom = dynamic_cast<DirectionalAtom*>(mol->getAtomAt(ghostIndex));
335     if (ghostAtom == NULL) {
336     sprintf(painCave.errMsg, "Can not cast Atom to DirectionalAtom");
337     painCave.isFatal = 1;
338     simError();
339     }
340    
341     BendType* bendType = ff->getBendType(normalAtom->getType(), ghostAtom->getType(), "GHOST");
342    
343     if (bendType == NULL) {
344     sprintf(painCave.errMsg, "Can not find Matching Bend Type for[%s, %s, %s]",
345     normalAtom->getType().c_str(),
346     ghostAtom->getType().c_str(),
347     "GHOST");
348    
349     painCave.isFatal = 1;
350     simError();
351     }
352    
353     return new GhostBend(normalAtom, ghostAtom, bendType);
354    
355     } else {
356    
357     Atom* atomA = mol->getAtomAt(stamp->getA());
358     Atom* atomB = mol->getAtomAt(stamp->getB());
359     Atom* atomC = mol->getAtomAt(stamp->getC());
360    
361     assert( atomA && atomB && atomC);
362    
363     BendType* bendType = ff->getBendType(atomA->getType(), atomB->getType(), atomC->getType());
364    
365     if (bendType == NULL) {
366     sprintf(painCave.errMsg, "Can not find Matching Bend Type for[%s, %s, %s]",
367     atomA->getType().c_str(),
368     atomB->getType().c_str(),
369     atomC->getType().c_str());
370    
371     painCave.isFatal = 1;
372     simError();
373     }
374    
375     return new Bend(atomA, atomB, atomC, bendType);
376     }
377     }
378    
379     Torsion* MoleculeCreator::createTorsion(ForceField* ff, Molecule* mol, TorsionStamp* stamp) {
380    
381 tim 1957 Atom* atomA = mol->getAtomAt(stamp->getA());
382     Atom* atomB = mol->getAtomAt(stamp->getB());
383     Atom* atomC = mol->getAtomAt(stamp->getC());
384     Torsion* torsion;
385 gezelter 1930
386 tim 1957 if (stamp->getD() != -1) {
387     Atom* atomD = mol->getAtomAt(stamp->getD());
388 gezelter 1930
389 tim 1957 assert(atomA && atomB && atomC && atomD);
390    
391     TorsionType* torsionType = ff->getTorsionType(atomA->getType(), atomB->getType(),
392     atomC->getType(), atomD->getType());
393 gezelter 1930
394 tim 1957 if (torsionType == NULL) {
395     sprintf(painCave.errMsg, "Can not find Matching Torsion Type for[%s, %s, %s, %s]",
396     atomA->getType().c_str(),
397     atomB->getType().c_str(),
398     atomC->getType().c_str(),
399     atomD->getType().c_str());
400    
401     painCave.isFatal = 1;
402     simError();
403     }
404    
405     torsion = new Torsion(atomA, atomB, atomC, atomD, torsionType);
406 gezelter 1930 }
407 tim 1957 else {
408    
409     DirectionalAtom* dAtom = dynamic_cast<DirectionalAtom*>(atomC);
410     if (dAtom == NULL) {
411     sprintf(painCave.errMsg, "Can not cast Atom to DirectionalAtom");
412     painCave.isFatal = 1;
413     simError();
414     }
415    
416     TorsionType* torsionType = ff->getTorsionType(atomA->getType(), atomB->getType(),
417     atomC->getType(), "GHOST");
418    
419     if (torsionType == NULL) {
420     sprintf(painCave.errMsg, "Can not find Matching Torsion Type for[%s, %s, %s, %s]",
421     atomA->getType().c_str(),
422     atomB->getType().c_str(),
423     atomC->getType().c_str(),
424     "GHOST");
425    
426     painCave.isFatal = 1;
427     simError();
428     }
429    
430     torsion = new GhostTorsion(atomA, atomB, dAtom, torsionType);
431     }
432    
433     return torsion;
434 gezelter 1930 }
435    
436     CutoffGroup* MoleculeCreator::createCutoffGroup(Molecule* mol, CutoffGroupStamp* stamp) {
437     int nAtoms;
438     CutoffGroup* cg;
439     Atom* atom;
440     cg = new CutoffGroup();
441    
442     nAtoms = stamp->getNMembers();
443     for (int i =0; i < nAtoms; ++i) {
444     atom = mol->getAtomAt(stamp->getMember(i));
445     assert(atom);
446     cg->addAtom(atom);
447     }
448    
449     return cg;
450     }
451    
452     CutoffGroup* MoleculeCreator::createCutoffGroup(Molecule * mol, Atom* atom) {
453     CutoffGroup* cg;
454     cg = new CutoffGroup();
455     cg->addAtom(atom);
456     return cg;
457     }
458    
459     void MoleculeCreator::createConstraintPair(Molecule* mol) {
460    
461     //add bond constraints
462     Molecule::BondIterator bi;
463     Bond* bond;
464     for (bond = mol->beginBond(bi); bond != NULL; bond = mol->nextBond(bi)) {
465    
466     BondType* bt = bond->getBondType();
467    
468     //class Parent1 {};
469     //class Child1 : public Parent {};
470     //class Child2 : public Parent {};
471     //Child1* ch1 = new Child1();
472     //Child2* ch2 = dynamic_cast<Child2*>(ch1);
473     //the dynamic_cast is succeed in above line. A compiler bug?
474    
475     if (typeid(FixedBondType) == typeid(*bt)) {
476     FixedBondType* fbt = dynamic_cast<FixedBondType*>(bt);
477    
478     ConstraintElem* consElemA = new ConstraintElem(bond->getAtomA());
479     ConstraintElem* consElemB = new ConstraintElem(bond->getAtomB());
480     ConstraintPair* consPair = new ConstraintPair(consElemA, consElemB, fbt->getEquilibriumBondLength());
481     mol->addConstraintPair(consPair);
482     }
483     }
484    
485     //rigidbody -- rigidbody constraint is not support yet
486     }
487    
488     void MoleculeCreator::createConstraintElem(Molecule* mol) {
489    
490     ConstraintPair* consPair;
491     Molecule::ConstraintPairIterator cpi;
492     std::set<StuntDouble*> sdSet;
493     for (consPair = mol->beginConstraintPair(cpi); consPair != NULL; consPair = mol->nextConstraintPair(cpi)) {
494    
495     StuntDouble* sdA = consPair->getConsElem1()->getStuntDouble();
496     if (sdSet.find(sdA) == sdSet.end()){
497     sdSet.insert(sdA);
498     mol->addConstraintElem(new ConstraintElem(sdA));
499     }
500    
501     StuntDouble* sdB = consPair->getConsElem2()->getStuntDouble();
502     if (sdSet.find(sdB) == sdSet.end()){
503     sdSet.insert(sdB);
504     mol->addConstraintElem(new ConstraintElem(sdB));
505     }
506    
507     }
508    
509     }
510    
511     }

Properties

Name Value
svn:executable *