ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/src/brains/MoleculeCreator.cpp
Revision: 2204
Committed: Fri Apr 15 22:04:00 2005 UTC (19 years, 3 months ago) by gezelter
File size: 16073 byte(s)
Log Message:
xemacs has been drafted to perform our indentation services

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    
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     std::set<Atom*> allAtoms;
132 gezelter 2204 Molecule::AtomIterator ai;
133 gezelter 1930
134     //add all atoms into allAtoms set
135     for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) {
136 gezelter 2204 allAtoms.insert(atom);
137 gezelter 1930 }
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 gezelter 2204 for(atom = cg->beginAtom(ai); atom != NULL; atom = cg->nextAtom(ai)) {
147     cutoffAtoms.insert(atom);
148     }
149 gezelter 1930
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 gezelter 2204 std::back_inserter(freeAtoms));
159 gezelter 1930
160     if (freeAtoms.size() != allAtoms.size() - cutoffAtoms.size()) {
161 gezelter 2204 //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 gezelter 1930
164 gezelter 2204 painCave.isFatal = 1;
165     simError();
166 gezelter 1930 }
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 gezelter 2204 cutoffGroup = createCutoffGroup(mol, *fai);
173     mol->addCutoffGroup(cutoffGroup);
174 gezelter 1930 }
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 gezelter 2204 }
184 gezelter 1930
185    
186 gezelter 2204 Atom* MoleculeCreator::createAtom(ForceField* ff, Molecule* mol, AtomStamp* stamp,
187     LocalIndexManager* localIndexMan) {
188 gezelter 1930 AtomType * atomType;
189     Atom* atom;
190    
191     atomType = ff->getAtomType(stamp->getType());
192    
193     if (atomType == NULL) {
194 gezelter 2204 sprintf(painCave.errMsg, "Can not find Matching Atom Type for[%s]",
195     stamp->getType());
196 gezelter 1930
197 gezelter 2204 painCave.isFatal = 1;
198     simError();
199 gezelter 1930 }
200    
201     //below code still have some kind of hard-coding smell
202     if (atomType->isDirectional()){
203    
204 gezelter 2204 DirectionalAtomType* dAtomType = dynamic_cast<DirectionalAtomType*>(atomType);
205 gezelter 1930
206 gezelter 2204 if (dAtomType == NULL) {
207     sprintf(painCave.errMsg, "Can not cast AtomType to DirectionalAtomType");
208 gezelter 1930
209 gezelter 2204 painCave.isFatal = 1;
210     simError();
211     }
212 gezelter 1930
213 gezelter 2204 DirectionalAtom* dAtom;
214     dAtom = new DirectionalAtom(dAtomType);
215     atom = dAtom;
216 gezelter 1930 }
217     else{
218 gezelter 2204 atom = new Atom(atomType);
219 gezelter 1930 }
220    
221     atom->setLocalIndex(localIndexMan->getNextAtomIndex());
222    
223     return atom;
224 gezelter 2204 }
225 gezelter 1930
226 gezelter 2204 RigidBody* MoleculeCreator::createRigidBody(MoleculeStamp *molStamp, Molecule* mol,
227     RigidBodyStamp* rbStamp,
228     LocalIndexManager* localIndexMan) {
229 gezelter 1930 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 gezelter 2204 //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 gezelter 1930 }
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 gezelter 2204 }
261 gezelter 1930
262 gezelter 2204 Bond* MoleculeCreator::createBond(ForceField* ff, Molecule* mol, BondStamp* stamp) {
263 gezelter 1930 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 gezelter 2204 sprintf(painCave.errMsg, "Can not find Matching Bond Type for[%s, %s]",
276     atomA->getType().c_str(),
277     atomB->getType().c_str());
278 gezelter 1930
279 gezelter 2204 painCave.isFatal = 1;
280     simError();
281 gezelter 1930 }
282     return new Bond(atomA, atomB, bondType);
283 gezelter 2204 }
284 gezelter 1930
285 gezelter 2204 Bend* MoleculeCreator::createBend(ForceField* ff, Molecule* mol, BendStamp* stamp) {
286 gezelter 1930 bool isGhostBend = false;
287     int ghostIndex;
288    
289    
290     //
291     if (stamp->haveExtras()){
292 gezelter 2204 LinkedAssign* extras = stamp->getExtras();
293     LinkedAssign* currentExtra = extras;
294 gezelter 1930
295 gezelter 2204 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 gezelter 1930
303 gezelter 2204 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 gezelter 1930
318     }
319    
320     if (isGhostBend) {
321    
322 gezelter 2204 int indexA = stamp->getA();
323     int indexB= stamp->getB();
324 gezelter 1930
325 gezelter 2204 assert(indexA != indexB);
326 gezelter 1930
327 gezelter 2204 int normalIndex;
328     if (indexA == ghostIndex) {
329     normalIndex = indexB;
330     } else if (indexB == ghostIndex) {
331     normalIndex = indexA;
332     }
333 gezelter 1930
334 gezelter 2204 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 gezelter 1930
342 gezelter 2204 BendType* bendType = ff->getBendType(normalAtom->getType(), ghostAtom->getType(), "GHOST");
343 gezelter 1930
344 gezelter 2204 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 gezelter 1930
350 gezelter 2204 painCave.isFatal = 1;
351     simError();
352     }
353 gezelter 1930
354 gezelter 2204 return new GhostBend(normalAtom, ghostAtom, bendType);
355 gezelter 1930
356     } else {
357    
358 gezelter 2204 Atom* atomA = mol->getAtomAt(stamp->getA());
359     Atom* atomB = mol->getAtomAt(stamp->getB());
360     Atom* atomC = mol->getAtomAt(stamp->getC());
361 gezelter 1930
362 gezelter 2204 assert( atomA && atomB && atomC);
363 gezelter 1930
364 gezelter 2204 BendType* bendType = ff->getBendType(atomA->getType(), atomB->getType(), atomC->getType());
365 gezelter 1930
366 gezelter 2204 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 gezelter 1930
372 gezelter 2204 painCave.isFatal = 1;
373     simError();
374     }
375 gezelter 1930
376 gezelter 2204 return new Bend(atomA, atomB, atomC, bendType);
377 gezelter 1930 }
378 gezelter 2204 }
379 gezelter 1930
380 gezelter 2204 Torsion* MoleculeCreator::createTorsion(ForceField* ff, Molecule* mol, TorsionStamp* stamp) {
381 gezelter 1930
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 gezelter 2204 Atom* atomD = mol->getAtomAt(stamp->getD());
389 gezelter 1930
390 gezelter 2204 assert(atomA && atomB && atomC && atomD);
391 tim 1957
392 gezelter 2204 TorsionType* torsionType = ff->getTorsionType(atomA->getType(), atomB->getType(),
393     atomC->getType(), atomD->getType());
394 gezelter 1930
395 gezelter 2204 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 tim 1957
402 gezelter 2204 painCave.isFatal = 1;
403     simError();
404     }
405 tim 1957
406 gezelter 2204 torsion = new Torsion(atomA, atomB, atomC, atomD, torsionType);
407 gezelter 1930 }
408 tim 1957 else {
409    
410 gezelter 2204 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 tim 1957
417 gezelter 2204 TorsionType* torsionType = ff->getTorsionType(atomA->getType(), atomB->getType(),
418     atomC->getType(), "GHOST");
419 tim 1957
420 gezelter 2204 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 tim 1957
427 gezelter 2204 painCave.isFatal = 1;
428     simError();
429     }
430 tim 1957
431 gezelter 2204 torsion = new GhostTorsion(atomA, atomB, dAtom, torsionType);
432 tim 1957 }
433    
434     return torsion;
435 gezelter 2204 }
436 gezelter 1930
437 gezelter 2204 CutoffGroup* MoleculeCreator::createCutoffGroup(Molecule* mol, CutoffGroupStamp* stamp) {
438 gezelter 1930 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 gezelter 2204 atom = mol->getAtomAt(stamp->getMember(i));
446     assert(atom);
447     cg->addAtom(atom);
448 gezelter 1930 }
449    
450     return cg;
451 gezelter 2204 }
452 gezelter 1930
453 gezelter 2204 CutoffGroup* MoleculeCreator::createCutoffGroup(Molecule * mol, Atom* atom) {
454 gezelter 1930 CutoffGroup* cg;
455     cg = new CutoffGroup();
456     cg->addAtom(atom);
457     return cg;
458 gezelter 2204 }
459 gezelter 1930
460 gezelter 2204 void MoleculeCreator::createConstraintPair(Molecule* mol) {
461 gezelter 1930
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 gezelter 2204 BondType* bt = bond->getBondType();
468 gezelter 1930
469 gezelter 2204 //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 gezelter 1930
476 gezelter 2204 if (typeid(FixedBondType) == typeid(*bt)) {
477     FixedBondType* fbt = dynamic_cast<FixedBondType*>(bt);
478 gezelter 1930
479 gezelter 2204 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 gezelter 1930 }
485    
486     //rigidbody -- rigidbody constraint is not support yet
487 gezelter 2204 }
488 gezelter 1930
489 gezelter 2204 void MoleculeCreator::createConstraintElem(Molecule* mol) {
490 gezelter 1930
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 gezelter 2204 StuntDouble* sdA = consPair->getConsElem1()->getStuntDouble();
497     if (sdSet.find(sdA) == sdSet.end()){
498     sdSet.insert(sdA);
499     mol->addConstraintElem(new ConstraintElem(sdA));
500     }
501 gezelter 1930
502 gezelter 2204 StuntDouble* sdB = consPair->getConsElem2()->getStuntDouble();
503     if (sdSet.find(sdB) == sdSet.end()){
504     sdSet.insert(sdB);
505     mol->addConstraintElem(new ConstraintElem(sdB));
506     }
507 gezelter 1930
508     }
509    
510 gezelter 2204 }
511 gezelter 1930
512     }

Properties

Name Value
svn:executable *