ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/src/brains/MoleculeCreator.cpp
Revision: 1976
Committed: Fri Feb 4 22:44:15 2005 UTC (19 years, 5 months ago) by tim
File size: 17595 byte(s)
Log Message:
adding SelectionManager into SimInfo

File Contents

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

Properties

Name Value
svn:executable *