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