ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/brains/MoleculeCreator.cpp
Revision: 2346
Committed: Wed Oct 5 19:34:01 2005 UTC (18 years, 9 months ago) by tim
File size: 15341 byte(s)
Log Message:
fix a bug in creating cutoffGroup. When
cutoffGroup is turned off, there is a mismatch between group and
center of mass array

File Contents

# User Rev Content
1 gezelter 2204 /*
2 gezelter 1930 * 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 gezelter 2204 /**
43     * @file MoleculeCreator.cpp
44     * @author tlin
45     * @date 11/04/2004
46     * @time 13:44am
47     * @version 1.0
48     */
49 gezelter 1930
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 tim 2346
63 gezelter 2204 Molecule* MoleculeCreator::createMolecule(ForceField* ff, MoleculeStamp *molStamp,
64     int stampId, int globalIndex, LocalIndexManager* localIndexMan) {
65 gezelter 1930
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 gezelter 2204 currentAtomStamp = molStamp->getAtom(i);
74     atom = createAtom(ff, mol, currentAtomStamp, localIndexMan);
75     mol->addAtom(atom);
76 gezelter 1930 }
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 gezelter 2204 currentRigidBodyStamp = molStamp->getRigidBody(i);
85     rb = createRigidBody(molStamp, mol, currentRigidBodyStamp, localIndexMan);
86     mol->addRigidBody(rb);
87 gezelter 1930 }
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 gezelter 2204 currentBondStamp = molStamp->getBond(i);
96     bond = createBond(ff, mol, currentBondStamp);
97     mol->addBond(bond);
98 gezelter 1930 }
99    
100     //create bends
101     Bend* bend;
102     BendStamp* currentBendStamp;
103     int nBends = molStamp->getNBends();
104     for (int i = 0; i < nBends; ++i) {
105 gezelter 2204 currentBendStamp = molStamp->getBend(i);
106     bend = createBend(ff, mol, currentBendStamp);
107     mol->addBend(bend);
108 gezelter 1930 }
109    
110     //create torsions
111     Torsion* torsion;
112     TorsionStamp* currentTorsionStamp;
113     int nTorsions = molStamp->getNTorsions();
114     for (int i = 0; i < nTorsions; ++i) {
115 gezelter 2204 currentTorsionStamp = molStamp->getTorsion(i);
116     torsion = createTorsion(ff, mol, currentTorsionStamp);
117     mol->addTorsion(torsion);
118 gezelter 1930 }
119    
120     //create cutoffGroups
121     CutoffGroup* cutoffGroup;
122     CutoffGroupStamp* currentCutoffGroupStamp;
123     int nCutoffGroups = molStamp->getNCutoffGroups();
124     for (int i = 0; i < nCutoffGroups; ++i) {
125 gezelter 2204 currentCutoffGroupStamp = molStamp->getCutoffGroup(i);
126     cutoffGroup = createCutoffGroup(mol, currentCutoffGroupStamp);
127     mol->addCutoffGroup(cutoffGroup);
128 gezelter 1930 }
129    
130     //every free atom is a cutoff group
131 tim 2346 std::vector<Atom*> freeAtoms;
132     std::vector<Atom*>::iterator ai;
133     std::vector<Atom*>::iterator fai;
134 gezelter 1930
135     //add all atoms into allAtoms set
136 tim 2346 for(atom = mol->beginAtom(fai); atom != NULL; atom = mol->nextAtom(fai)) {
137     freeAtoms.push_back(atom);
138 gezelter 1930 }
139    
140     Molecule::CutoffGroupIterator ci;
141     CutoffGroup* cg;
142    
143     for (cg = mol->beginCutoffGroup(ci); cg != NULL; cg = mol->nextCutoffGroup(ci)) {
144    
145 gezelter 2204 for(atom = cg->beginAtom(ai); atom != NULL; atom = cg->nextAtom(ai)) {
146 tim 2346 //erase the atoms belong to cutoff groups from freeAtoms vector
147     freeAtoms.erase(std::remove(freeAtoms.begin(), freeAtoms.end(), atom), freeAtoms.end());
148 gezelter 2204 }
149 gezelter 1930
150     }
151    
152     //loop over the free atoms and then create one cutoff group for every single free atom
153 tim 2346
154 gezelter 1930 for (fai = freeAtoms.begin(); fai != freeAtoms.end(); ++fai) {
155 gezelter 2204 cutoffGroup = createCutoffGroup(mol, *fai);
156     mol->addCutoffGroup(cutoffGroup);
157 gezelter 1930 }
158     //create constraints
159     createConstraintPair(mol);
160     createConstraintElem(mol);
161    
162     //the construction of this molecule is finished
163     mol->complete();
164    
165     return mol;
166 gezelter 2204 }
167 gezelter 1930
168    
169 gezelter 2204 Atom* MoleculeCreator::createAtom(ForceField* ff, Molecule* mol, AtomStamp* stamp,
170     LocalIndexManager* localIndexMan) {
171 gezelter 1930 AtomType * atomType;
172     Atom* atom;
173    
174     atomType = ff->getAtomType(stamp->getType());
175    
176     if (atomType == NULL) {
177 gezelter 2204 sprintf(painCave.errMsg, "Can not find Matching Atom Type for[%s]",
178     stamp->getType());
179 gezelter 1930
180 gezelter 2204 painCave.isFatal = 1;
181     simError();
182 gezelter 1930 }
183    
184     //below code still have some kind of hard-coding smell
185     if (atomType->isDirectional()){
186    
187 gezelter 2204 DirectionalAtomType* dAtomType = dynamic_cast<DirectionalAtomType*>(atomType);
188 gezelter 1930
189 gezelter 2204 if (dAtomType == NULL) {
190     sprintf(painCave.errMsg, "Can not cast AtomType to DirectionalAtomType");
191 gezelter 1930
192 gezelter 2204 painCave.isFatal = 1;
193     simError();
194     }
195 gezelter 1930
196 gezelter 2204 DirectionalAtom* dAtom;
197     dAtom = new DirectionalAtom(dAtomType);
198     atom = dAtom;
199 gezelter 1930 }
200     else{
201 gezelter 2204 atom = new Atom(atomType);
202 gezelter 1930 }
203    
204     atom->setLocalIndex(localIndexMan->getNextAtomIndex());
205    
206     return atom;
207 gezelter 2204 }
208 gezelter 1930
209 gezelter 2204 RigidBody* MoleculeCreator::createRigidBody(MoleculeStamp *molStamp, Molecule* mol,
210     RigidBodyStamp* rbStamp,
211     LocalIndexManager* localIndexMan) {
212 gezelter 1930 Atom* atom;
213     int nAtoms;
214     Vector3d refCoor;
215     AtomStamp* atomStamp;
216    
217     RigidBody* rb = new RigidBody();
218     nAtoms = rbStamp->getNMembers();
219     for (int i = 0; i < nAtoms; ++i) {
220 gezelter 2204 //rbStamp->getMember(i) return the local index of current atom inside the molecule.
221     //It is not the same as local index of atom which is the index of atom at DataStorage class
222     atom = mol->getAtomAt(rbStamp->getMember(i));
223     atomStamp= molStamp->getAtom(rbStamp->getMember(i));
224     rb->addAtom(atom, atomStamp);
225 gezelter 1930 }
226    
227     //after all of the atoms are added, we need to calculate the reference coordinates
228     rb->calcRefCoords();
229    
230     //set the local index of this rigid body, global index will be set later
231     rb->setLocalIndex(localIndexMan->getNextRigidBodyIndex());
232    
233     //the rule for naming rigidbody MoleculeName_RB_Integer
234     //The first part is the name of the molecule
235     //The second part is alway fixed as "RB"
236     //The third part is the index of the rigidbody defined in meta-data file
237     //For example, Butane_RB_0 is a valid rigid body name of butane molecule
238     /**@todo replace itoa by lexi_cast */
239 gezelter 2087 std::string s = OOPSE_itoa(mol->getNRigidBodies(), 10);
240     rb->setType(mol->getType() + "_RB_" + s.c_str());
241 tim 1976
242 gezelter 1930 return rb;
243 gezelter 2204 }
244 gezelter 1930
245 gezelter 2204 Bond* MoleculeCreator::createBond(ForceField* ff, Molecule* mol, BondStamp* stamp) {
246 gezelter 1930 BondType* bondType;
247     Atom* atomA;
248     Atom* atomB;
249    
250     atomA = mol->getAtomAt(stamp->getA());
251     atomB = mol->getAtomAt(stamp->getB());
252    
253     assert( atomA && atomB);
254    
255     bondType = ff->getBondType(atomA->getType(), atomB->getType());
256    
257     if (bondType == NULL) {
258 gezelter 2204 sprintf(painCave.errMsg, "Can not find Matching Bond Type for[%s, %s]",
259     atomA->getType().c_str(),
260     atomB->getType().c_str());
261 gezelter 1930
262 gezelter 2204 painCave.isFatal = 1;
263     simError();
264 gezelter 1930 }
265     return new Bond(atomA, atomB, bondType);
266 gezelter 2204 }
267 gezelter 1930
268 gezelter 2204 Bend* MoleculeCreator::createBend(ForceField* ff, Molecule* mol, BendStamp* stamp) {
269 gezelter 1930 bool isGhostBend = false;
270     int ghostIndex;
271    
272    
273     //
274     if (stamp->haveExtras()){
275 gezelter 2204 LinkedAssign* extras = stamp->getExtras();
276     LinkedAssign* currentExtra = extras;
277 gezelter 1930
278 gezelter 2204 while (currentExtra != NULL){
279     if (!strcmp(currentExtra->getlhs(), "ghostVectorSource")){
280     switch (currentExtra->getType()){
281     case 0:
282     ghostIndex = currentExtra->getInt();
283     isGhostBend = true;
284     break;
285 gezelter 1930
286 gezelter 2204 default:
287     sprintf(painCave.errMsg,
288     "SimSetup Error: ghostVectorSource must be an int.\n");
289     painCave.isFatal = 1;
290     simError();
291     }
292     } else{
293     sprintf(painCave.errMsg,
294     "SimSetup Error: unhandled bend assignment:\n");
295     painCave.isFatal = 1;
296     simError();
297     }
298     currentExtra = currentExtra->getNext();
299     }
300 gezelter 1930
301     }
302    
303     if (isGhostBend) {
304    
305 gezelter 2204 int indexA = stamp->getA();
306     int indexB= stamp->getB();
307 gezelter 1930
308 gezelter 2204 assert(indexA != indexB);
309 gezelter 1930
310 gezelter 2204 int normalIndex;
311     if (indexA == ghostIndex) {
312     normalIndex = indexB;
313     } else if (indexB == ghostIndex) {
314     normalIndex = indexA;
315     }
316 gezelter 1930
317 gezelter 2204 Atom* normalAtom = mol->getAtomAt(normalIndex) ;
318     DirectionalAtom* ghostAtom = dynamic_cast<DirectionalAtom*>(mol->getAtomAt(ghostIndex));
319     if (ghostAtom == NULL) {
320     sprintf(painCave.errMsg, "Can not cast Atom to DirectionalAtom");
321     painCave.isFatal = 1;
322     simError();
323     }
324 gezelter 1930
325 gezelter 2204 BendType* bendType = ff->getBendType(normalAtom->getType(), ghostAtom->getType(), "GHOST");
326 gezelter 1930
327 gezelter 2204 if (bendType == NULL) {
328     sprintf(painCave.errMsg, "Can not find Matching Bend Type for[%s, %s, %s]",
329     normalAtom->getType().c_str(),
330     ghostAtom->getType().c_str(),
331     "GHOST");
332 gezelter 1930
333 gezelter 2204 painCave.isFatal = 1;
334     simError();
335     }
336 gezelter 1930
337 gezelter 2204 return new GhostBend(normalAtom, ghostAtom, bendType);
338 gezelter 1930
339     } else {
340    
341 gezelter 2204 Atom* atomA = mol->getAtomAt(stamp->getA());
342     Atom* atomB = mol->getAtomAt(stamp->getB());
343     Atom* atomC = mol->getAtomAt(stamp->getC());
344 gezelter 1930
345 gezelter 2204 assert( atomA && atomB && atomC);
346 gezelter 1930
347 gezelter 2204 BendType* bendType = ff->getBendType(atomA->getType(), atomB->getType(), atomC->getType());
348 gezelter 1930
349 gezelter 2204 if (bendType == NULL) {
350     sprintf(painCave.errMsg, "Can not find Matching Bend Type for[%s, %s, %s]",
351     atomA->getType().c_str(),
352     atomB->getType().c_str(),
353     atomC->getType().c_str());
354 gezelter 1930
355 gezelter 2204 painCave.isFatal = 1;
356     simError();
357     }
358 gezelter 1930
359 gezelter 2204 return new Bend(atomA, atomB, atomC, bendType);
360 gezelter 1930 }
361 gezelter 2204 }
362 gezelter 1930
363 gezelter 2204 Torsion* MoleculeCreator::createTorsion(ForceField* ff, Molecule* mol, TorsionStamp* stamp) {
364 gezelter 1930
365 tim 1957 Atom* atomA = mol->getAtomAt(stamp->getA());
366     Atom* atomB = mol->getAtomAt(stamp->getB());
367     Atom* atomC = mol->getAtomAt(stamp->getC());
368     Torsion* torsion;
369 gezelter 1930
370 tim 1957 if (stamp->getD() != -1) {
371 gezelter 2204 Atom* atomD = mol->getAtomAt(stamp->getD());
372 gezelter 1930
373 gezelter 2204 assert(atomA && atomB && atomC && atomD);
374 tim 1957
375 gezelter 2204 TorsionType* torsionType = ff->getTorsionType(atomA->getType(), atomB->getType(),
376     atomC->getType(), atomD->getType());
377 gezelter 1930
378 gezelter 2204 if (torsionType == NULL) {
379     sprintf(painCave.errMsg, "Can not find Matching Torsion Type for[%s, %s, %s, %s]",
380     atomA->getType().c_str(),
381     atomB->getType().c_str(),
382     atomC->getType().c_str(),
383     atomD->getType().c_str());
384 tim 1957
385 gezelter 2204 painCave.isFatal = 1;
386     simError();
387     }
388 tim 1957
389 gezelter 2204 torsion = new Torsion(atomA, atomB, atomC, atomD, torsionType);
390 gezelter 1930 }
391 tim 1957 else {
392    
393 gezelter 2204 DirectionalAtom* dAtom = dynamic_cast<DirectionalAtom*>(atomC);
394     if (dAtom == NULL) {
395     sprintf(painCave.errMsg, "Can not cast Atom to DirectionalAtom");
396     painCave.isFatal = 1;
397     simError();
398     }
399 tim 1957
400 gezelter 2204 TorsionType* torsionType = ff->getTorsionType(atomA->getType(), atomB->getType(),
401     atomC->getType(), "GHOST");
402 tim 1957
403 gezelter 2204 if (torsionType == NULL) {
404     sprintf(painCave.errMsg, "Can not find Matching Torsion Type for[%s, %s, %s, %s]",
405     atomA->getType().c_str(),
406     atomB->getType().c_str(),
407     atomC->getType().c_str(),
408     "GHOST");
409 tim 1957
410 gezelter 2204 painCave.isFatal = 1;
411     simError();
412     }
413 tim 1957
414 gezelter 2204 torsion = new GhostTorsion(atomA, atomB, dAtom, torsionType);
415 tim 1957 }
416    
417     return torsion;
418 gezelter 2204 }
419 gezelter 1930
420 gezelter 2204 CutoffGroup* MoleculeCreator::createCutoffGroup(Molecule* mol, CutoffGroupStamp* stamp) {
421 gezelter 1930 int nAtoms;
422     CutoffGroup* cg;
423     Atom* atom;
424     cg = new CutoffGroup();
425    
426     nAtoms = stamp->getNMembers();
427     for (int i =0; i < nAtoms; ++i) {
428 gezelter 2204 atom = mol->getAtomAt(stamp->getMember(i));
429     assert(atom);
430     cg->addAtom(atom);
431 gezelter 1930 }
432    
433     return cg;
434 gezelter 2204 }
435 gezelter 1930
436 gezelter 2204 CutoffGroup* MoleculeCreator::createCutoffGroup(Molecule * mol, Atom* atom) {
437 gezelter 1930 CutoffGroup* cg;
438     cg = new CutoffGroup();
439     cg->addAtom(atom);
440     return cg;
441 gezelter 2204 }
442 gezelter 1930
443 gezelter 2204 void MoleculeCreator::createConstraintPair(Molecule* mol) {
444 gezelter 1930
445     //add bond constraints
446     Molecule::BondIterator bi;
447     Bond* bond;
448     for (bond = mol->beginBond(bi); bond != NULL; bond = mol->nextBond(bi)) {
449    
450 gezelter 2204 BondType* bt = bond->getBondType();
451 gezelter 1930
452 gezelter 2204 //class Parent1 {};
453     //class Child1 : public Parent {};
454     //class Child2 : public Parent {};
455     //Child1* ch1 = new Child1();
456     //Child2* ch2 = dynamic_cast<Child2*>(ch1);
457     //the dynamic_cast is succeed in above line. A compiler bug?
458 gezelter 1930
459 gezelter 2204 if (typeid(FixedBondType) == typeid(*bt)) {
460     FixedBondType* fbt = dynamic_cast<FixedBondType*>(bt);
461 gezelter 1930
462 gezelter 2204 ConstraintElem* consElemA = new ConstraintElem(bond->getAtomA());
463     ConstraintElem* consElemB = new ConstraintElem(bond->getAtomB());
464     ConstraintPair* consPair = new ConstraintPair(consElemA, consElemB, fbt->getEquilibriumBondLength());
465     mol->addConstraintPair(consPair);
466     }
467 gezelter 1930 }
468    
469     //rigidbody -- rigidbody constraint is not support yet
470 gezelter 2204 }
471 gezelter 1930
472 gezelter 2204 void MoleculeCreator::createConstraintElem(Molecule* mol) {
473 gezelter 1930
474     ConstraintPair* consPair;
475     Molecule::ConstraintPairIterator cpi;
476     std::set<StuntDouble*> sdSet;
477     for (consPair = mol->beginConstraintPair(cpi); consPair != NULL; consPair = mol->nextConstraintPair(cpi)) {
478    
479 gezelter 2204 StuntDouble* sdA = consPair->getConsElem1()->getStuntDouble();
480     if (sdSet.find(sdA) == sdSet.end()){
481     sdSet.insert(sdA);
482     mol->addConstraintElem(new ConstraintElem(sdA));
483     }
484 gezelter 1930
485 gezelter 2204 StuntDouble* sdB = consPair->getConsElem2()->getStuntDouble();
486     if (sdSet.find(sdB) == sdSet.end()){
487     sdSet.insert(sdB);
488     mol->addConstraintElem(new ConstraintElem(sdB));
489     }
490 gezelter 1930
491     }
492    
493 gezelter 2204 }
494 gezelter 1930
495     }

Properties

Name Value
svn:executable *