| 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. Redistributions of source code must retain the above copyright | 
| 10 | *    notice, this list of conditions and the following disclaimer. | 
| 11 | * | 
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | 
| 13 | *    notice, this list of conditions and the following disclaimer in the | 
| 14 | *    documentation and/or other materials provided with the | 
| 15 | *    distribution. | 
| 16 | * | 
| 17 | * This software is provided "AS IS," without a warranty of any | 
| 18 | * kind. All express or implied conditions, representations and | 
| 19 | * warranties, including any implied warranty of merchantability, | 
| 20 | * fitness for a particular purpose or non-infringement, are hereby | 
| 21 | * excluded.  The University of Notre Dame and its licensors shall not | 
| 22 | * be liable for any damages suffered by licensee as a result of | 
| 23 | * using, modifying or distributing the software or its | 
| 24 | * derivatives. In no event will the University of Notre Dame or its | 
| 25 | * licensors be liable for any lost revenue, profit or data, or for | 
| 26 | * direct, indirect, special, consequential, incidental or punitive | 
| 27 | * damages, however caused and regardless of the theory of liability, | 
| 28 | * arising out of the use of or inability to use software, even if the | 
| 29 | * University of Notre Dame has been advised of the possibility of | 
| 30 | * such damages. | 
| 31 | * | 
| 32 | * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your | 
| 33 | * research, please cite the appropriate papers when you publish your | 
| 34 | * work.  Good starting points are: | 
| 35 | * | 
| 36 | * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). | 
| 37 | * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). | 
| 38 | * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). | 
| 39 | * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 | * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 41 | */ | 
| 42 |  | 
| 43 | /** | 
| 44 | * @file MoleculeCreator.cpp | 
| 45 | * @author tlin | 
| 46 | * @date 11/04/2004 | 
| 47 | * @version 1.0 | 
| 48 | */ | 
| 49 |  | 
| 50 | #include <cassert> | 
| 51 | #include <typeinfo> | 
| 52 | #include <set> | 
| 53 |  | 
| 54 | #include "brains/MoleculeCreator.hpp" | 
| 55 | #include "primitives/GhostBend.hpp" | 
| 56 | #include "primitives/GhostTorsion.hpp" | 
| 57 | #include "types/AtomType.hpp" | 
| 58 | #include "types/FixedBondType.hpp" | 
| 59 | #include "types/BondTypeParser.hpp" | 
| 60 | #include "types/BendTypeParser.hpp" | 
| 61 | #include "types/TorsionTypeParser.hpp" | 
| 62 | #include "types/InversionTypeParser.hpp" | 
| 63 | #include "utils/simError.h" | 
| 64 | #include "utils/StringUtils.hpp" | 
| 65 |  | 
| 66 | namespace OpenMD { | 
| 67 |  | 
| 68 | Molecule* MoleculeCreator::createMolecule(ForceField* ff, | 
| 69 | MoleculeStamp *molStamp, | 
| 70 | int stampId, int globalIndex, | 
| 71 | LocalIndexManager* localIndexMan) { | 
| 72 | Molecule* mol = new Molecule(stampId, globalIndex, molStamp->getName(), | 
| 73 | molStamp->getRegion() ); | 
| 74 |  | 
| 75 | //create atoms | 
| 76 | Atom* atom; | 
| 77 | AtomStamp* currentAtomStamp; | 
| 78 | int nAtom = molStamp->getNAtoms(); | 
| 79 | for (int i = 0; i < nAtom; ++i) { | 
| 80 | currentAtomStamp = molStamp->getAtomStamp(i); | 
| 81 | atom = createAtom(ff, mol, currentAtomStamp, localIndexMan); | 
| 82 | mol->addAtom(atom); | 
| 83 | } | 
| 84 |  | 
| 85 | //create rigidbodies | 
| 86 | RigidBody* rb; | 
| 87 | RigidBodyStamp * currentRigidBodyStamp; | 
| 88 | int nRigidbodies = molStamp->getNRigidBodies(); | 
| 89 |  | 
| 90 | for (int i = 0; i < nRigidbodies; ++i) { | 
| 91 | currentRigidBodyStamp = molStamp->getRigidBodyStamp(i); | 
| 92 | rb = createRigidBody(molStamp, mol, currentRigidBodyStamp, | 
| 93 | localIndexMan); | 
| 94 | mol->addRigidBody(rb); | 
| 95 | } | 
| 96 |  | 
| 97 | //create bonds | 
| 98 | Bond* bond; | 
| 99 | BondStamp* currentBondStamp; | 
| 100 | int nBonds = molStamp->getNBonds(); | 
| 101 |  | 
| 102 | for (int i = 0; i < nBonds; ++i) { | 
| 103 | currentBondStamp = molStamp->getBondStamp(i); | 
| 104 | bond = createBond(ff, mol, currentBondStamp, localIndexMan); | 
| 105 | mol->addBond(bond); | 
| 106 | } | 
| 107 |  | 
| 108 | //create bends | 
| 109 | Bend* bend; | 
| 110 | BendStamp* currentBendStamp; | 
| 111 | int nBends = molStamp->getNBends(); | 
| 112 | for (int i = 0; i < nBends; ++i) { | 
| 113 | currentBendStamp = molStamp->getBendStamp(i); | 
| 114 | bend = createBend(ff, mol, currentBendStamp, localIndexMan); | 
| 115 | mol->addBend(bend); | 
| 116 | } | 
| 117 |  | 
| 118 | //create torsions | 
| 119 | Torsion* torsion; | 
| 120 | TorsionStamp* currentTorsionStamp; | 
| 121 | int nTorsions = molStamp->getNTorsions(); | 
| 122 | for (int i = 0; i < nTorsions; ++i) { | 
| 123 | currentTorsionStamp = molStamp->getTorsionStamp(i); | 
| 124 | torsion = createTorsion(ff, mol, currentTorsionStamp, localIndexMan); | 
| 125 | mol->addTorsion(torsion); | 
| 126 | } | 
| 127 |  | 
| 128 | //create inversions | 
| 129 | Inversion* inversion; | 
| 130 | InversionStamp* currentInversionStamp; | 
| 131 | int nInversions = molStamp->getNInversions(); | 
| 132 | for (int i = 0; i < nInversions; ++i) { | 
| 133 | currentInversionStamp = molStamp->getInversionStamp(i); | 
| 134 | inversion = createInversion(ff, mol, currentInversionStamp, | 
| 135 | localIndexMan); | 
| 136 | if (inversion != NULL ) { | 
| 137 | mol->addInversion(inversion); | 
| 138 | } | 
| 139 | } | 
| 140 |  | 
| 141 | //create cutoffGroups | 
| 142 | CutoffGroup* cutoffGroup; | 
| 143 | CutoffGroupStamp* currentCutoffGroupStamp; | 
| 144 | int nCutoffGroups = molStamp->getNCutoffGroups(); | 
| 145 | for (int i = 0; i < nCutoffGroups; ++i) { | 
| 146 | currentCutoffGroupStamp = molStamp->getCutoffGroupStamp(i); | 
| 147 | cutoffGroup = createCutoffGroup(mol, currentCutoffGroupStamp, | 
| 148 | localIndexMan); | 
| 149 | mol->addCutoffGroup(cutoffGroup); | 
| 150 | } | 
| 151 |  | 
| 152 | //every free atom is a cutoff group | 
| 153 | std::vector<Atom*> freeAtoms; | 
| 154 | std::vector<Atom*>::iterator ai; | 
| 155 | std::vector<Atom*>::iterator fai; | 
| 156 |  | 
| 157 | //add all atoms into allAtoms set | 
| 158 | for(atom = mol->beginAtom(fai); atom != NULL; atom = mol->nextAtom(fai)) { | 
| 159 | freeAtoms.push_back(atom); | 
| 160 | } | 
| 161 |  | 
| 162 | Molecule::CutoffGroupIterator ci; | 
| 163 | CutoffGroup* cg; | 
| 164 |  | 
| 165 | for (cg = mol->beginCutoffGroup(ci); cg != NULL; | 
| 166 | cg = mol->nextCutoffGroup(ci)) { | 
| 167 |  | 
| 168 | for(atom = cg->beginAtom(ai); atom != NULL; atom = cg->nextAtom(ai)) { | 
| 169 | //erase the atoms belong to cutoff groups from freeAtoms vector | 
| 170 | freeAtoms.erase(std::remove(freeAtoms.begin(), freeAtoms.end(), atom), | 
| 171 | freeAtoms.end()); | 
| 172 | } | 
| 173 | } | 
| 174 |  | 
| 175 | // loop over the free atoms and then create one cutoff group for | 
| 176 | // every single free atom | 
| 177 |  | 
| 178 | for (fai = freeAtoms.begin(); fai != freeAtoms.end(); ++fai) { | 
| 179 | cutoffGroup = createCutoffGroup(mol, *fai, localIndexMan); | 
| 180 | mol->addCutoffGroup(cutoffGroup); | 
| 181 | } | 
| 182 |  | 
| 183 | //create bonded constraintPairs: | 
| 184 | createConstraintPair(mol); | 
| 185 |  | 
| 186 | //create non-bonded constraintPairs | 
| 187 | for (int i = 0; i < molStamp->getNConstraints(); ++i) { | 
| 188 | ConstraintStamp* cStamp = molStamp->getConstraintStamp(i); | 
| 189 | Atom* atomA; | 
| 190 | Atom* atomB; | 
| 191 |  | 
| 192 | atomA = mol->getAtomAt(cStamp->getA()); | 
| 193 | atomB = mol->getAtomAt(cStamp->getB()); | 
| 194 | assert( atomA && atomB ); | 
| 195 |  | 
| 196 | RealType distance; | 
| 197 | bool printConstraintForce = false; | 
| 198 |  | 
| 199 | if (!cStamp->haveConstrainedDistance()) { | 
| 200 | sprintf(painCave.errMsg, | 
| 201 | "Constraint Error: A non-bond constraint was specified\n" | 
| 202 | "\twithout providing a value for the constrainedDistance.\n"); | 
| 203 | painCave.isFatal = 1; | 
| 204 | simError(); | 
| 205 | } else { | 
| 206 | distance = cStamp->getConstrainedDistance(); | 
| 207 | } | 
| 208 |  | 
| 209 | if (cStamp->havePrintConstraintForce()) { | 
| 210 | printConstraintForce = cStamp->getPrintConstraintForce(); | 
| 211 | } | 
| 212 |  | 
| 213 | ConstraintElem* consElemA = new ConstraintElem(atomA); | 
| 214 | ConstraintElem* consElemB = new ConstraintElem(atomB); | 
| 215 | ConstraintPair* cPair = new ConstraintPair(consElemA, consElemB, distance, | 
| 216 | printConstraintForce); | 
| 217 | mol->addConstraintPair(cPair); | 
| 218 | } | 
| 219 |  | 
| 220 | // now create the constraint elements: | 
| 221 |  | 
| 222 | createConstraintElem(mol); | 
| 223 |  | 
| 224 | // Does this molecule stamp define a total constrained charge value? | 
| 225 | // If so, let the created molecule know about it. | 
| 226 |  | 
| 227 | if (molStamp->haveConstrainTotalCharge() ) { | 
| 228 | mol->setConstrainTotalCharge( molStamp->getConstrainTotalCharge() ); | 
| 229 | } | 
| 230 |  | 
| 231 | //the construction of this molecule is finished | 
| 232 | mol->complete(); | 
| 233 |  | 
| 234 | return mol; | 
| 235 | } | 
| 236 |  | 
| 237 |  | 
| 238 | Atom* MoleculeCreator::createAtom(ForceField* ff, Molecule* mol, | 
| 239 | AtomStamp* stamp, | 
| 240 | LocalIndexManager* localIndexMan) { | 
| 241 | AtomType * atomType; | 
| 242 | Atom* atom; | 
| 243 |  | 
| 244 | atomType =  ff->getAtomType(stamp->getType()); | 
| 245 |  | 
| 246 | if (atomType == NULL) { | 
| 247 | sprintf(painCave.errMsg, "Can not find Matching Atom Type for[%s]", | 
| 248 | stamp->getType().c_str()); | 
| 249 |  | 
| 250 | painCave.isFatal = 1; | 
| 251 | simError(); | 
| 252 | } | 
| 253 |  | 
| 254 | //below code still have some kind of hard-coding smell | 
| 255 | if (atomType->isDirectional()){ | 
| 256 |  | 
| 257 | DirectionalAtom* dAtom; | 
| 258 | dAtom = new DirectionalAtom(atomType); | 
| 259 | atom = dAtom; | 
| 260 | } | 
| 261 | else{ | 
| 262 | atom = new Atom(atomType); | 
| 263 | } | 
| 264 |  | 
| 265 | atom->setLocalIndex(localIndexMan->getNextAtomIndex()); | 
| 266 |  | 
| 267 | return atom; | 
| 268 | } | 
| 269 |  | 
| 270 | RigidBody* MoleculeCreator::createRigidBody(MoleculeStamp *molStamp, | 
| 271 | Molecule* mol, | 
| 272 | RigidBodyStamp* rbStamp, | 
| 273 | LocalIndexManager* localIndexMan){ | 
| 274 | Atom* atom; | 
| 275 | int nAtoms; | 
| 276 | Vector3d refCoor; | 
| 277 | AtomStamp* atomStamp; | 
| 278 |  | 
| 279 | RigidBody* rb = new RigidBody(); | 
| 280 | nAtoms = rbStamp->getNMembers(); | 
| 281 | for (int i = 0; i < nAtoms; ++i) { | 
| 282 | //rbStamp->getMember(i) return the local index of current atom | 
| 283 | //inside the molecule.  It is not the same as local index of | 
| 284 | //atom which is the index of atom at DataStorage class | 
| 285 | atom = mol->getAtomAt(rbStamp->getMemberAt(i)); | 
| 286 | atomStamp= molStamp->getAtomStamp(rbStamp->getMemberAt(i)); | 
| 287 | rb->addAtom(atom, atomStamp); | 
| 288 | } | 
| 289 |  | 
| 290 | //after all of the atoms are added, we need to calculate the | 
| 291 | //reference coordinates | 
| 292 | rb->calcRefCoords(); | 
| 293 |  | 
| 294 | //set the local index of this rigid body, global index will be set later | 
| 295 | rb->setLocalIndex(localIndexMan->getNextRigidBodyIndex()); | 
| 296 |  | 
| 297 | // The rule for naming a rigidbody is: MoleculeName_RB_Integer | 
| 298 | // The first part is the name of the molecule | 
| 299 | // The second part is always fixed as "RB" | 
| 300 | // The third part is the index of the rigidbody defined in meta-data file | 
| 301 | // For example, Butane_RB_0 is a valid rigid body name of butane molecule | 
| 302 |  | 
| 303 | std::string s = OpenMD_itoa(mol->getNRigidBodies(), 10); | 
| 304 | rb->setType(mol->getType() + "_RB_" + s.c_str()); | 
| 305 | return rb; | 
| 306 | } | 
| 307 |  | 
| 308 | Bond* MoleculeCreator::createBond(ForceField* ff, Molecule* mol, | 
| 309 | BondStamp* stamp, | 
| 310 | LocalIndexManager* localIndexMan) { | 
| 311 | BondTypeParser btParser; | 
| 312 | BondType* bondType = NULL; | 
| 313 | Atom* atomA; | 
| 314 | Atom* atomB; | 
| 315 |  | 
| 316 | atomA = mol->getAtomAt(stamp->getA()); | 
| 317 | atomB = mol->getAtomAt(stamp->getB()); | 
| 318 |  | 
| 319 | assert( atomA && atomB); | 
| 320 |  | 
| 321 | if (stamp->hasOverride()) { | 
| 322 |  | 
| 323 | try { | 
| 324 | bondType = btParser.parseTypeAndPars(stamp->getOverrideType(), | 
| 325 | stamp->getOverridePars() ); | 
| 326 | } | 
| 327 | catch( OpenMDException e) { | 
| 328 | sprintf(painCave.errMsg, "MoleculeCreator Error: %s " | 
| 329 | "for molecule %s\n", | 
| 330 | e.what(), mol->getType().c_str() ); | 
| 331 | painCave.isFatal = 1; | 
| 332 | simError(); | 
| 333 | } | 
| 334 |  | 
| 335 | } else { | 
| 336 | bondType = ff->getBondType(atomA->getType(), atomB->getType()); | 
| 337 |  | 
| 338 | if (bondType == NULL) { | 
| 339 | sprintf(painCave.errMsg, "Can not find Matching Bond Type for[%s, %s]", | 
| 340 | atomA->getType().c_str(), | 
| 341 | atomB->getType().c_str()); | 
| 342 |  | 
| 343 | painCave.isFatal = 1; | 
| 344 | simError(); | 
| 345 | } | 
| 346 | } | 
| 347 |  | 
| 348 | Bond* bond = new Bond(atomA, atomB, bondType); | 
| 349 |  | 
| 350 | //set the local index of this bond, the global index will be set later | 
| 351 | bond->setLocalIndex(localIndexMan->getNextBondIndex()); | 
| 352 |  | 
| 353 | // The rule for naming a bond is: MoleculeName_Bond_Integer | 
| 354 | // The first part is the name of the molecule | 
| 355 | // The second part is always fixed as "Bond" | 
| 356 | // The third part is the index of the bond defined in meta-data file | 
| 357 | // For example, Butane_bond_0 is a valid Bond name in a butane molecule | 
| 358 |  | 
| 359 | std::string s = OpenMD_itoa(mol->getNBonds(), 10); | 
| 360 | bond->setName(mol->getType() + "_Bond_" + s.c_str()); | 
| 361 | return bond; | 
| 362 | } | 
| 363 |  | 
| 364 | Bend* MoleculeCreator::createBend(ForceField* ff, Molecule* mol, | 
| 365 | BendStamp* stamp, | 
| 366 | LocalIndexManager* localIndexMan) { | 
| 367 | BendTypeParser btParser; | 
| 368 | BendType* bendType = NULL; | 
| 369 | Bend* bend = NULL; | 
| 370 |  | 
| 371 | std::vector<int> bendAtoms = stamp->getMembers(); | 
| 372 | if (bendAtoms.size() == 3) { | 
| 373 | Atom* atomA = mol->getAtomAt(bendAtoms[0]); | 
| 374 | Atom* atomB = mol->getAtomAt(bendAtoms[1]); | 
| 375 | Atom* atomC = mol->getAtomAt(bendAtoms[2]); | 
| 376 |  | 
| 377 | assert( atomA && atomB && atomC ); | 
| 378 |  | 
| 379 | if (stamp->hasOverride()) { | 
| 380 |  | 
| 381 | try { | 
| 382 | bendType = btParser.parseTypeAndPars(stamp->getOverrideType(), | 
| 383 | stamp->getOverridePars() ); | 
| 384 | } | 
| 385 | catch( OpenMDException e) { | 
| 386 | sprintf(painCave.errMsg, "MoleculeCreator Error: %s " | 
| 387 | "for molecule %s\n", | 
| 388 | e.what(), mol->getType().c_str() ); | 
| 389 | painCave.isFatal = 1; | 
| 390 | simError(); | 
| 391 | } | 
| 392 | } else { | 
| 393 |  | 
| 394 | bendType = ff->getBendType(atomA->getType().c_str(), | 
| 395 | atomB->getType().c_str(), | 
| 396 | atomC->getType().c_str()); | 
| 397 |  | 
| 398 | if (bendType == NULL) { | 
| 399 | sprintf(painCave.errMsg, | 
| 400 | "Can not find Matching Bend Type for[%s, %s, %s]", | 
| 401 | atomA->getType().c_str(), | 
| 402 | atomB->getType().c_str(), | 
| 403 | atomC->getType().c_str()); | 
| 404 |  | 
| 405 | painCave.isFatal = 1; | 
| 406 | simError(); | 
| 407 | } | 
| 408 | } | 
| 409 |  | 
| 410 | bend = new Bend(atomA, atomB, atomC, bendType); | 
| 411 |  | 
| 412 | } else if ( bendAtoms.size() == 2 && stamp->haveGhostVectorSource()) { | 
| 413 | int ghostIndex = stamp->getGhostVectorSource(); | 
| 414 | int normalIndex = ghostIndex != bendAtoms[0] ? | 
| 415 | bendAtoms[0] : bendAtoms[1]; | 
| 416 | Atom* normalAtom = mol->getAtomAt(normalIndex) ; | 
| 417 | DirectionalAtom* ghostAtom = dynamic_cast<DirectionalAtom*>(mol->getAtomAt(ghostIndex)); | 
| 418 | if (ghostAtom == NULL) { | 
| 419 | sprintf(painCave.errMsg, "Can not cast Atom to DirectionalAtom"); | 
| 420 | painCave.isFatal = 1; | 
| 421 | simError(); | 
| 422 | } | 
| 423 |  | 
| 424 | if (stamp->hasOverride()) { | 
| 425 |  | 
| 426 | try { | 
| 427 | bendType = btParser.parseTypeAndPars(stamp->getOverrideType(), | 
| 428 | stamp->getOverridePars() ); | 
| 429 | } | 
| 430 | catch( OpenMDException e) { | 
| 431 | sprintf(painCave.errMsg, "MoleculeCreator Error: %s " | 
| 432 | "for molecule %s\n", | 
| 433 | e.what(), mol->getType().c_str() ); | 
| 434 | painCave.isFatal = 1; | 
| 435 | simError(); | 
| 436 | } | 
| 437 | } else { | 
| 438 |  | 
| 439 | bendType = ff->getBendType(normalAtom->getType(), ghostAtom->getType(), | 
| 440 | "GHOST"); | 
| 441 |  | 
| 442 | if (bendType == NULL) { | 
| 443 | sprintf(painCave.errMsg, | 
| 444 | "Can not find Matching Bend Type for[%s, %s, %s]", | 
| 445 | normalAtom->getType().c_str(), | 
| 446 | ghostAtom->getType().c_str(), | 
| 447 | "GHOST"); | 
| 448 |  | 
| 449 | painCave.isFatal = 1; | 
| 450 | simError(); | 
| 451 | } | 
| 452 | } | 
| 453 |  | 
| 454 | bend = new GhostBend(normalAtom, ghostAtom, bendType); | 
| 455 |  | 
| 456 | } | 
| 457 |  | 
| 458 | //set the local index of this bend, the global index will be set later | 
| 459 | bend->setLocalIndex(localIndexMan->getNextBendIndex()); | 
| 460 |  | 
| 461 | // The rule for naming a bend is: MoleculeName_Bend_Integer | 
| 462 | // The first part is the name of the molecule | 
| 463 | // The second part is always fixed as "Bend" | 
| 464 | // The third part is the index of the bend defined in meta-data file | 
| 465 | // For example, Butane_Bend_0 is a valid Bend name in a butane molecule | 
| 466 |  | 
| 467 | std::string s = OpenMD_itoa(mol->getNBends(), 10); | 
| 468 | bend->setName(mol->getType() + "_Bend_" + s.c_str()); | 
| 469 | return bend; | 
| 470 | } | 
| 471 |  | 
| 472 | Torsion* MoleculeCreator::createTorsion(ForceField* ff, Molecule* mol, | 
| 473 | TorsionStamp* stamp, | 
| 474 | LocalIndexManager* localIndexMan) { | 
| 475 |  | 
| 476 | TorsionTypeParser ttParser; | 
| 477 | TorsionType* torsionType = NULL; | 
| 478 | Torsion* torsion = NULL; | 
| 479 |  | 
| 480 | std::vector<int> torsionAtoms = stamp->getMembers(); | 
| 481 | if (torsionAtoms.size() < 3) { | 
| 482 | return torsion; | 
| 483 | } | 
| 484 |  | 
| 485 | Atom* atomA = mol->getAtomAt(torsionAtoms[0]); | 
| 486 | Atom* atomB = mol->getAtomAt(torsionAtoms[1]); | 
| 487 | Atom* atomC = mol->getAtomAt(torsionAtoms[2]); | 
| 488 |  | 
| 489 | if (torsionAtoms.size() == 4) { | 
| 490 | Atom* atomD = mol->getAtomAt(torsionAtoms[3]); | 
| 491 |  | 
| 492 | assert(atomA && atomB && atomC && atomD ); | 
| 493 |  | 
| 494 | if (stamp->hasOverride()) { | 
| 495 |  | 
| 496 | try { | 
| 497 | torsionType = ttParser.parseTypeAndPars(stamp->getOverrideType(), | 
| 498 | stamp->getOverridePars() ); | 
| 499 | } | 
| 500 | catch( OpenMDException e) { | 
| 501 | sprintf(painCave.errMsg, "MoleculeCreator Error: %s " | 
| 502 | "for molecule %s\n", | 
| 503 | e.what(), mol->getType().c_str() ); | 
| 504 | painCave.isFatal = 1; | 
| 505 | simError(); | 
| 506 | } | 
| 507 | } else { | 
| 508 |  | 
| 509 |  | 
| 510 | torsionType = ff->getTorsionType(atomA->getType(), | 
| 511 | atomB->getType(), | 
| 512 | atomC->getType(), | 
| 513 | atomD->getType()); | 
| 514 | if (torsionType == NULL) { | 
| 515 | sprintf(painCave.errMsg, | 
| 516 | "Can not find Matching Torsion Type for[%s, %s, %s, %s]", | 
| 517 | atomA->getType().c_str(), | 
| 518 | atomB->getType().c_str(), | 
| 519 | atomC->getType().c_str(), | 
| 520 | atomD->getType().c_str()); | 
| 521 |  | 
| 522 | painCave.isFatal = 1; | 
| 523 | simError(); | 
| 524 | } | 
| 525 | } | 
| 526 |  | 
| 527 | torsion = new Torsion(atomA, atomB, atomC, atomD, torsionType); | 
| 528 | } else { | 
| 529 |  | 
| 530 | DirectionalAtom* dAtom = dynamic_cast<DirectionalAtom*>(mol->getAtomAt(stamp->getGhostVectorSource())); | 
| 531 | if (dAtom == NULL) { | 
| 532 | sprintf(painCave.errMsg, "Can not cast Atom to DirectionalAtom"); | 
| 533 | painCave.isFatal = 1; | 
| 534 | simError(); | 
| 535 | } | 
| 536 |  | 
| 537 | if (stamp->hasOverride()) { | 
| 538 |  | 
| 539 | try { | 
| 540 | torsionType = ttParser.parseTypeAndPars(stamp->getOverrideType(), | 
| 541 | stamp->getOverridePars() ); | 
| 542 | } | 
| 543 | catch( OpenMDException e) { | 
| 544 | sprintf(painCave.errMsg, "MoleculeCreator Error: %s " | 
| 545 | "for molecule %s\n", | 
| 546 | e.what(), mol->getType().c_str() ); | 
| 547 | painCave.isFatal = 1; | 
| 548 | simError(); | 
| 549 | } | 
| 550 | } else { | 
| 551 | torsionType = ff->getTorsionType(atomA->getType(), atomB->getType(), | 
| 552 | atomC->getType(), "GHOST"); | 
| 553 |  | 
| 554 | if (torsionType == NULL) { | 
| 555 | sprintf(painCave.errMsg, | 
| 556 | "Can not find Matching Torsion Type for[%s, %s, %s, %s]", | 
| 557 | atomA->getType().c_str(), | 
| 558 | atomB->getType().c_str(), | 
| 559 | atomC->getType().c_str(), | 
| 560 | "GHOST"); | 
| 561 |  | 
| 562 | painCave.isFatal = 1; | 
| 563 | simError(); | 
| 564 | } | 
| 565 | } | 
| 566 |  | 
| 567 | torsion = new GhostTorsion(atomA, atomB, dAtom, torsionType); | 
| 568 | } | 
| 569 |  | 
| 570 | //set the local index of this torsion, the global index will be set later | 
| 571 | torsion->setLocalIndex(localIndexMan->getNextTorsionIndex()); | 
| 572 |  | 
| 573 | // The rule for naming a torsion is: MoleculeName_Torsion_Integer | 
| 574 | // The first part is the name of the molecule | 
| 575 | // The second part is always fixed as "Torsion" | 
| 576 | // The third part is the index of the torsion defined in meta-data file | 
| 577 | // For example, Butane_Torsion_0 is a valid Torsion name in a | 
| 578 | // butane molecule | 
| 579 |  | 
| 580 | std::string s = OpenMD_itoa(mol->getNTorsions(), 10); | 
| 581 | torsion->setName(mol->getType() + "_Torsion_" + s.c_str()); | 
| 582 | return torsion; | 
| 583 | } | 
| 584 |  | 
| 585 | Inversion* MoleculeCreator::createInversion(ForceField* ff, Molecule* mol, | 
| 586 | InversionStamp* stamp, | 
| 587 | LocalIndexManager* localIndexMan) { | 
| 588 |  | 
| 589 | InversionTypeParser itParser; | 
| 590 | InversionType* inversionType = NULL; | 
| 591 | Inversion* inversion = NULL; | 
| 592 |  | 
| 593 | int center = stamp->getCenter(); | 
| 594 | std::vector<int> satellites = stamp->getSatellites(); | 
| 595 | if (satellites.size() != 3) { | 
| 596 | return inversion; | 
| 597 | } | 
| 598 |  | 
| 599 | Atom* atomA = mol->getAtomAt(center); | 
| 600 | Atom* atomB = mol->getAtomAt(satellites[0]); | 
| 601 | Atom* atomC = mol->getAtomAt(satellites[1]); | 
| 602 | Atom* atomD = mol->getAtomAt(satellites[2]); | 
| 603 |  | 
| 604 | assert(atomA && atomB && atomC && atomD); | 
| 605 |  | 
| 606 | if (stamp->hasOverride()) { | 
| 607 |  | 
| 608 | try { | 
| 609 | inversionType = itParser.parseTypeAndPars(stamp->getOverrideType(), | 
| 610 | stamp->getOverridePars() ); | 
| 611 | } | 
| 612 | catch( OpenMDException e) { | 
| 613 | sprintf(painCave.errMsg, "MoleculeCreator Error: %s " | 
| 614 | "for molecule %s\n", | 
| 615 | e.what(), mol->getType().c_str() ); | 
| 616 | painCave.isFatal = 1; | 
| 617 | simError(); | 
| 618 | } | 
| 619 | } else { | 
| 620 |  | 
| 621 | inversionType = ff->getInversionType(atomA->getType(), | 
| 622 | atomB->getType(), | 
| 623 | atomC->getType(), | 
| 624 | atomD->getType()); | 
| 625 |  | 
| 626 | if (inversionType == NULL) { | 
| 627 | sprintf(painCave.errMsg, | 
| 628 | "No Matching Inversion Type for[%s, %s, %s, %s]\n" | 
| 629 | "\t(May not be a problem: not all inversions are parametrized)\n", | 
| 630 | atomA->getType().c_str(), | 
| 631 | atomB->getType().c_str(), | 
| 632 | atomC->getType().c_str(), | 
| 633 | atomD->getType().c_str()); | 
| 634 |  | 
| 635 | painCave.isFatal = 0; | 
| 636 | painCave.severity = OPENMD_INFO; | 
| 637 | simError(); | 
| 638 | } | 
| 639 | } | 
| 640 | if (inversionType != NULL) { | 
| 641 |  | 
| 642 | inversion = new Inversion(atomA, atomB, atomC, atomD, inversionType); | 
| 643 |  | 
| 644 | // set the local index of this inversion, the global index will | 
| 645 | // be set later | 
| 646 | inversion->setLocalIndex(localIndexMan->getNextInversionIndex()); | 
| 647 |  | 
| 648 | // The rule for naming an inversion is: MoleculeName_Inversion_Integer | 
| 649 | // The first part is the name of the molecule | 
| 650 | // The second part is always fixed as "Inversion" | 
| 651 | // The third part is the index of the inversion defined in meta-data file | 
| 652 | // For example, Benzene_Inversion_0 is a valid Inversion name in a | 
| 653 | // Benzene molecule | 
| 654 |  | 
| 655 | std::string s = OpenMD_itoa(mol->getNInversions(), 10); | 
| 656 | inversion->setName(mol->getType() + "_Inversion_" + s.c_str()); | 
| 657 | return inversion; | 
| 658 | } else { | 
| 659 | return NULL; | 
| 660 | } | 
| 661 | } | 
| 662 |  | 
| 663 |  | 
| 664 | CutoffGroup* MoleculeCreator::createCutoffGroup(Molecule* mol, | 
| 665 | CutoffGroupStamp* stamp, | 
| 666 | LocalIndexManager* localIndexMan) { | 
| 667 | int nAtoms; | 
| 668 | CutoffGroup* cg; | 
| 669 | Atom* atom; | 
| 670 | cg = new CutoffGroup(); | 
| 671 |  | 
| 672 | nAtoms = stamp->getNMembers(); | 
| 673 | for (int i =0; i < nAtoms; ++i) { | 
| 674 | atom = mol->getAtomAt(stamp->getMemberAt(i)); | 
| 675 | assert(atom); | 
| 676 | cg->addAtom(atom); | 
| 677 | } | 
| 678 |  | 
| 679 | //set the local index of this cutoffGroup, global index will be set later | 
| 680 | cg->setLocalIndex(localIndexMan->getNextCutoffGroupIndex()); | 
| 681 |  | 
| 682 | return cg; | 
| 683 | } | 
| 684 |  | 
| 685 | CutoffGroup* MoleculeCreator::createCutoffGroup(Molecule * mol, Atom* atom, | 
| 686 | LocalIndexManager* localIndexMan) { | 
| 687 | CutoffGroup* cg; | 
| 688 | cg  = new CutoffGroup(); | 
| 689 | cg->addAtom(atom); | 
| 690 |  | 
| 691 | //set the local index of this cutoffGroup, global index will be set later | 
| 692 | cg->setLocalIndex(localIndexMan->getNextCutoffGroupIndex()); | 
| 693 |  | 
| 694 | return cg; | 
| 695 | } | 
| 696 |  | 
| 697 | void MoleculeCreator::createConstraintPair(Molecule* mol) { | 
| 698 |  | 
| 699 | //add bond constraints | 
| 700 | Molecule::BondIterator bi; | 
| 701 | Bond* bond; | 
| 702 | ConstraintPair* cPair; | 
| 703 |  | 
| 704 | for (bond = mol->beginBond(bi); bond != NULL; bond = mol->nextBond(bi)) { | 
| 705 |  | 
| 706 | BondType* bt = bond->getBondType(); | 
| 707 |  | 
| 708 | if (typeid(FixedBondType) == typeid(*bt)) { | 
| 709 | FixedBondType* fbt = dynamic_cast<FixedBondType*>(bt); | 
| 710 |  | 
| 711 | ConstraintElem* consElemA = new ConstraintElem(bond->getAtomA()); | 
| 712 | ConstraintElem* consElemB = new ConstraintElem(bond->getAtomB()); | 
| 713 | cPair = new ConstraintPair(consElemA, consElemB, | 
| 714 | fbt->getEquilibriumBondLength(), false); | 
| 715 | mol->addConstraintPair(cPair); | 
| 716 | } | 
| 717 | } | 
| 718 |  | 
| 719 | //rigidbody -- rigidbody constraint is not support yet | 
| 720 | } | 
| 721 |  | 
| 722 | void MoleculeCreator::createConstraintElem(Molecule* mol) { | 
| 723 |  | 
| 724 | ConstraintPair* consPair; | 
| 725 | Molecule::ConstraintPairIterator cpi; | 
| 726 | std::set<StuntDouble*> sdSet; | 
| 727 | for (consPair = mol->beginConstraintPair(cpi); consPair != NULL; | 
| 728 | consPair = mol->nextConstraintPair(cpi)) { | 
| 729 |  | 
| 730 | StuntDouble* sdA = consPair->getConsElem1()->getStuntDouble(); | 
| 731 | if (sdSet.find(sdA) == sdSet.end()){ | 
| 732 | sdSet.insert(sdA); | 
| 733 | mol->addConstraintElem(new ConstraintElem(sdA)); | 
| 734 | } | 
| 735 |  | 
| 736 | StuntDouble* sdB = consPair->getConsElem2()->getStuntDouble(); | 
| 737 | if (sdSet.find(sdB) == sdSet.end()){ | 
| 738 | sdSet.insert(sdB); | 
| 739 | mol->addConstraintElem(new ConstraintElem(sdB)); | 
| 740 | } | 
| 741 | } | 
| 742 | } | 
| 743 | } |