--- trunk/src/brains/MoleculeCreator.cpp 2009/11/25 20:02:06 1390 +++ trunk/src/brains/MoleculeCreator.cpp 2014/12/02 22:11:04 2046 @@ -35,15 +35,15 @@ * * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). - * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). - * [4] Vardeman & Gezelter, in progress (2009). + * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). + * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). + * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). */ /** * @file MoleculeCreator.cpp * @author tlin * @date 11/04/2004 - * @time 13:44am * @version 1.0 */ @@ -54,8 +54,12 @@ #include "brains/MoleculeCreator.hpp" #include "primitives/GhostBend.hpp" #include "primitives/GhostTorsion.hpp" -#include "types/DirectionalAtomType.hpp" +#include "types/AtomType.hpp" #include "types/FixedBondType.hpp" +#include "types/BondTypeParser.hpp" +#include "types/BendTypeParser.hpp" +#include "types/TorsionTypeParser.hpp" +#include "types/InversionTypeParser.hpp" #include "utils/simError.h" #include "utils/StringUtils.hpp" @@ -65,8 +69,9 @@ namespace OpenMD { MoleculeStamp *molStamp, int stampId, int globalIndex, LocalIndexManager* localIndexMan) { - Molecule* mol = new Molecule(stampId, globalIndex, molStamp->getName()); - + Molecule* mol = new Molecule(stampId, globalIndex, molStamp->getName(), + molStamp->getRegion() ); + //create atoms Atom* atom; AtomStamp* currentAtomStamp; @@ -95,8 +100,8 @@ namespace OpenMD { int nBonds = molStamp->getNBonds(); for (int i = 0; i < nBonds; ++i) { - currentBondStamp = molStamp->getBondStamp(i); - bond = createBond(ff, mol, currentBondStamp); + currentBondStamp = molStamp->getBondStamp(i); + bond = createBond(ff, mol, currentBondStamp, localIndexMan); mol->addBond(bond); } @@ -106,7 +111,7 @@ namespace OpenMD { int nBends = molStamp->getNBends(); for (int i = 0; i < nBends; ++i) { currentBendStamp = molStamp->getBendStamp(i); - bend = createBend(ff, mol, currentBendStamp); + bend = createBend(ff, mol, currentBendStamp, localIndexMan); mol->addBend(bend); } @@ -116,7 +121,7 @@ namespace OpenMD { int nTorsions = molStamp->getNTorsions(); for (int i = 0; i < nTorsions; ++i) { currentTorsionStamp = molStamp->getTorsionStamp(i); - torsion = createTorsion(ff, mol, currentTorsionStamp); + torsion = createTorsion(ff, mol, currentTorsionStamp, localIndexMan); mol->addTorsion(torsion); } @@ -126,7 +131,8 @@ namespace OpenMD { int nInversions = molStamp->getNInversions(); for (int i = 0; i < nInversions; ++i) { currentInversionStamp = molStamp->getInversionStamp(i); - inversion = createInversion(ff, mol, currentInversionStamp); + inversion = createInversion(ff, mol, currentInversionStamp, + localIndexMan); if (inversion != NULL ) { mol->addInversion(inversion); } @@ -138,7 +144,8 @@ namespace OpenMD { int nCutoffGroups = molStamp->getNCutoffGroups(); for (int i = 0; i < nCutoffGroups; ++i) { currentCutoffGroupStamp = molStamp->getCutoffGroupStamp(i); - cutoffGroup = createCutoffGroup(mol, currentCutoffGroupStamp); + cutoffGroup = createCutoffGroup(mol, currentCutoffGroupStamp, + localIndexMan); mol->addCutoffGroup(cutoffGroup); } @@ -169,13 +176,58 @@ namespace OpenMD { // every single free atom for (fai = freeAtoms.begin(); fai != freeAtoms.end(); ++fai) { - cutoffGroup = createCutoffGroup(mol, *fai); + cutoffGroup = createCutoffGroup(mol, *fai, localIndexMan); mol->addCutoffGroup(cutoffGroup); } - //create constraints + + //create bonded constraintPairs: createConstraintPair(mol); + + //create non-bonded constraintPairs + for (int i = 0; i < molStamp->getNConstraints(); ++i) { + ConstraintStamp* cStamp = molStamp->getConstraintStamp(i); + Atom* atomA; + Atom* atomB; + + atomA = mol->getAtomAt(cStamp->getA()); + atomB = mol->getAtomAt(cStamp->getB()); + assert( atomA && atomB ); + + RealType distance; + bool printConstraintForce = false; + + if (!cStamp->haveConstrainedDistance()) { + sprintf(painCave.errMsg, + "Constraint Error: A non-bond constraint was specified\n" + "\twithout providing a value for the constrainedDistance.\n"); + painCave.isFatal = 1; + simError(); + } else { + distance = cStamp->getConstrainedDistance(); + } + + if (cStamp->havePrintConstraintForce()) { + printConstraintForce = cStamp->getPrintConstraintForce(); + } + + ConstraintElem* consElemA = new ConstraintElem(atomA); + ConstraintElem* consElemB = new ConstraintElem(atomB); + ConstraintPair* cPair = new ConstraintPair(consElemA, consElemB, distance, + printConstraintForce); + mol->addConstraintPair(cPair); + } + + // now create the constraint elements: + createConstraintElem(mol); + // Does this molecule stamp define a total constrained charge value? + // If so, let the created molecule know about it. + + if (molStamp->haveConstrainTotalCharge() ) { + mol->setConstrainTotalCharge( molStamp->getConstrainTotalCharge() ); + } + //the construction of this molecule is finished mol->complete(); @@ -198,21 +250,12 @@ namespace OpenMD { painCave.isFatal = 1; simError(); } - + //below code still have some kind of hard-coding smell if (atomType->isDirectional()){ - - DirectionalAtomType* dAtomType = dynamic_cast(atomType); - - if (dAtomType == NULL) { - sprintf(painCave.errMsg, "Can not cast AtomType to DirectionalAtomType"); - painCave.isFatal = 1; - simError(); - } - DirectionalAtom* dAtom; - dAtom = new DirectionalAtom(dAtomType); + dAtom = new DirectionalAtom(atomType); atom = dAtom; } else{ @@ -227,7 +270,7 @@ namespace OpenMD { RigidBody* MoleculeCreator::createRigidBody(MoleculeStamp *molStamp, Molecule* mol, RigidBodyStamp* rbStamp, - LocalIndexManager* localIndexMan) { + LocalIndexManager* localIndexMan){ Atom* atom; int nAtoms; Vector3d refCoor; @@ -251,21 +294,22 @@ namespace OpenMD { //set the local index of this rigid body, global index will be set later rb->setLocalIndex(localIndexMan->getNextRigidBodyIndex()); - //the rule for naming rigidbody MoleculeName_RB_Integer - //The first part is the name of the molecule - //The second part is alway fixed as "RB" - //The third part is the index of the rigidbody defined in meta-data file - //For example, Butane_RB_0 is a valid rigid body name of butane molecule - /**@todo replace itoa by lexi_cast */ + // The rule for naming a rigidbody is: MoleculeName_RB_Integer + // The first part is the name of the molecule + // The second part is always fixed as "RB" + // The third part is the index of the rigidbody defined in meta-data file + // For example, Butane_RB_0 is a valid rigid body name of butane molecule + std::string s = OpenMD_itoa(mol->getNRigidBodies(), 10); rb->setType(mol->getType() + "_RB_" + s.c_str()); - return rb; } Bond* MoleculeCreator::createBond(ForceField* ff, Molecule* mol, - BondStamp* stamp) { - BondType* bondType; + BondStamp* stamp, + LocalIndexManager* localIndexMan) { + BondTypeParser btParser; + BondType* bondType = NULL; Atom* atomA; Atom* atomB; @@ -273,49 +317,102 @@ namespace OpenMD { atomB = mol->getAtomAt(stamp->getB()); assert( atomA && atomB); - - bondType = ff->getBondType(atomA->getType(), atomB->getType()); - if (bondType == NULL) { - sprintf(painCave.errMsg, "Can not find Matching Bond Type for[%s, %s]", - atomA->getType().c_str(), - atomB->getType().c_str()); - - painCave.isFatal = 1; - simError(); + if (stamp->hasOverride()) { + + try { + bondType = btParser.parseTypeAndPars(stamp->getOverrideType(), + stamp->getOverridePars() ); + } + catch( OpenMDException e) { + sprintf(painCave.errMsg, "MoleculeCreator Error: %s " + "for molecule %s\n", + e.what(), mol->getType().c_str() ); + painCave.isFatal = 1; + simError(); + } + + } else { + bondType = ff->getBondType(atomA->getType(), atomB->getType()); + + if (bondType == NULL) { + sprintf(painCave.errMsg, "Can not find Matching Bond Type for[%s, %s]", + atomA->getType().c_str(), + atomB->getType().c_str()); + + painCave.isFatal = 1; + simError(); + } } - return new Bond(atomA, atomB, bondType); + + Bond* bond = new Bond(atomA, atomB, bondType); + + //set the local index of this bond, the global index will be set later + bond->setLocalIndex(localIndexMan->getNextBondIndex()); + + // The rule for naming a bond is: MoleculeName_Bond_Integer + // The first part is the name of the molecule + // The second part is always fixed as "Bond" + // The third part is the index of the bond defined in meta-data file + // For example, Butane_bond_0 is a valid Bond name in a butane molecule + + std::string s = OpenMD_itoa(mol->getNBonds(), 10); + bond->setName(mol->getType() + "_Bond_" + s.c_str()); + return bond; } Bend* MoleculeCreator::createBend(ForceField* ff, Molecule* mol, - BendStamp* stamp) { - Bend* bend = NULL; - std::vector bendAtoms = stamp->getMembers(); + BendStamp* stamp, + LocalIndexManager* localIndexMan) { + BendTypeParser btParser; + BendType* bendType = NULL; + Bend* bend = NULL; + + std::vector bendAtoms = stamp->getMembers(); if (bendAtoms.size() == 3) { Atom* atomA = mol->getAtomAt(bendAtoms[0]); Atom* atomB = mol->getAtomAt(bendAtoms[1]); Atom* atomC = mol->getAtomAt(bendAtoms[2]); - assert( atomA && atomB && atomC); - - BendType* bendType = ff->getBendType(atomA->getType().c_str(), - atomB->getType().c_str(), - atomC->getType().c_str()); - - if (bendType == NULL) { - sprintf(painCave.errMsg, "Can not find Matching Bend Type for[%s, %s, %s]", - atomA->getType().c_str(), - atomB->getType().c_str(), - atomC->getType().c_str()); + assert( atomA && atomB && atomC ); + + if (stamp->hasOverride()) { - painCave.isFatal = 1; - simError(); + try { + bendType = btParser.parseTypeAndPars(stamp->getOverrideType(), + stamp->getOverridePars() ); + } + catch( OpenMDException e) { + sprintf(painCave.errMsg, "MoleculeCreator Error: %s " + "for molecule %s\n", + e.what(), mol->getType().c_str() ); + painCave.isFatal = 1; + simError(); + } + } else { + + bendType = ff->getBendType(atomA->getType().c_str(), + atomB->getType().c_str(), + atomC->getType().c_str()); + + if (bendType == NULL) { + sprintf(painCave.errMsg, + "Can not find Matching Bend Type for[%s, %s, %s]", + atomA->getType().c_str(), + atomB->getType().c_str(), + atomC->getType().c_str()); + + painCave.isFatal = 1; + simError(); + } } bend = new Bend(atomA, atomB, atomC, bendType); + } else if ( bendAtoms.size() == 2 && stamp->haveGhostVectorSource()) { int ghostIndex = stamp->getGhostVectorSource(); - int normalIndex = ghostIndex != bendAtoms[0] ? bendAtoms[0] : bendAtoms[1]; + int normalIndex = ghostIndex != bendAtoms[0] ? + bendAtoms[0] : bendAtoms[1]; Atom* normalAtom = mol->getAtomAt(normalIndex) ; DirectionalAtom* ghostAtom = dynamic_cast(mol->getAtomAt(ghostIndex)); if (ghostAtom == NULL) { @@ -323,30 +420,63 @@ namespace OpenMD { painCave.isFatal = 1; simError(); } - - BendType* bendType = ff->getBendType(normalAtom->getType(), ghostAtom->getType(), "GHOST"); - if (bendType == NULL) { - sprintf(painCave.errMsg, "Can not find Matching Bend Type for[%s, %s, %s]", - normalAtom->getType().c_str(), - ghostAtom->getType().c_str(), - "GHOST"); - - painCave.isFatal = 1; - simError(); + if (stamp->hasOverride()) { + + try { + bendType = btParser.parseTypeAndPars(stamp->getOverrideType(), + stamp->getOverridePars() ); + } + catch( OpenMDException e) { + sprintf(painCave.errMsg, "MoleculeCreator Error: %s " + "for molecule %s\n", + e.what(), mol->getType().c_str() ); + painCave.isFatal = 1; + simError(); + } + } else { + + bendType = ff->getBendType(normalAtom->getType(), ghostAtom->getType(), + "GHOST"); + + if (bendType == NULL) { + sprintf(painCave.errMsg, + "Can not find Matching Bend Type for[%s, %s, %s]", + normalAtom->getType().c_str(), + ghostAtom->getType().c_str(), + "GHOST"); + + painCave.isFatal = 1; + simError(); + } } bend = new GhostBend(normalAtom, ghostAtom, bendType); } + //set the local index of this bend, the global index will be set later + bend->setLocalIndex(localIndexMan->getNextBendIndex()); + + // The rule for naming a bend is: MoleculeName_Bend_Integer + // The first part is the name of the molecule + // The second part is always fixed as "Bend" + // The third part is the index of the bend defined in meta-data file + // For example, Butane_Bend_0 is a valid Bend name in a butane molecule + + std::string s = OpenMD_itoa(mol->getNBends(), 10); + bend->setName(mol->getType() + "_Bend_" + s.c_str()); return bend; } Torsion* MoleculeCreator::createTorsion(ForceField* ff, Molecule* mol, - TorsionStamp* stamp) { + TorsionStamp* stamp, + LocalIndexManager* localIndexMan) { + TorsionTypeParser ttParser; + TorsionType* torsionType = NULL; Torsion* torsion = NULL; + std::vector torsionAtoms = stamp->getMembers(); if (torsionAtoms.size() < 3) { return torsion; @@ -359,26 +489,43 @@ namespace OpenMD { if (torsionAtoms.size() == 4) { Atom* atomD = mol->getAtomAt(torsionAtoms[3]); - assert(atomA && atomB && atomC && atomD); + assert(atomA && atomB && atomC && atomD ); + + if (stamp->hasOverride()) { - TorsionType* torsionType = ff->getTorsionType(atomA->getType(), - atomB->getType(), - atomC->getType(), - atomD->getType()); - if (torsionType == NULL) { - sprintf(painCave.errMsg, "Can not find Matching Torsion Type for[%s, %s, %s, %s]", - atomA->getType().c_str(), - atomB->getType().c_str(), - atomC->getType().c_str(), - atomD->getType().c_str()); + try { + torsionType = ttParser.parseTypeAndPars(stamp->getOverrideType(), + stamp->getOverridePars() ); + } + catch( OpenMDException e) { + sprintf(painCave.errMsg, "MoleculeCreator Error: %s " + "for molecule %s\n", + e.what(), mol->getType().c_str() ); + painCave.isFatal = 1; + simError(); + } + } else { + - painCave.isFatal = 1; - simError(); + torsionType = ff->getTorsionType(atomA->getType(), + atomB->getType(), + atomC->getType(), + atomD->getType()); + if (torsionType == NULL) { + sprintf(painCave.errMsg, + "Can not find Matching Torsion Type for[%s, %s, %s, %s]", + atomA->getType().c_str(), + atomB->getType().c_str(), + atomC->getType().c_str(), + atomD->getType().c_str()); + + painCave.isFatal = 1; + simError(); + } } torsion = new Torsion(atomA, atomB, atomC, atomD, torsionType); - } - else { + } else { DirectionalAtom* dAtom = dynamic_cast(mol->getAtomAt(stamp->getGhostVectorSource())); if (dAtom == NULL) { @@ -386,31 +533,63 @@ namespace OpenMD { painCave.isFatal = 1; simError(); } - - TorsionType* torsionType = ff->getTorsionType(atomA->getType(), atomB->getType(), - atomC->getType(), "GHOST"); - - if (torsionType == NULL) { - sprintf(painCave.errMsg, "Can not find Matching Torsion Type for[%s, %s, %s, %s]", - atomA->getType().c_str(), - atomB->getType().c_str(), - atomC->getType().c_str(), - "GHOST"); + + if (stamp->hasOverride()) { - painCave.isFatal = 1; - simError(); - } + try { + torsionType = ttParser.parseTypeAndPars(stamp->getOverrideType(), + stamp->getOverridePars() ); + } + catch( OpenMDException e) { + sprintf(painCave.errMsg, "MoleculeCreator Error: %s " + "for molecule %s\n", + e.what(), mol->getType().c_str() ); + painCave.isFatal = 1; + simError(); + } + } else { + torsionType = ff->getTorsionType(atomA->getType(), atomB->getType(), + atomC->getType(), "GHOST"); + if (torsionType == NULL) { + sprintf(painCave.errMsg, + "Can not find Matching Torsion Type for[%s, %s, %s, %s]", + atomA->getType().c_str(), + atomB->getType().c_str(), + atomC->getType().c_str(), + "GHOST"); + + painCave.isFatal = 1; + simError(); + } + } + torsion = new GhostTorsion(atomA, atomB, dAtom, torsionType); } + + //set the local index of this torsion, the global index will be set later + torsion->setLocalIndex(localIndexMan->getNextTorsionIndex()); + // The rule for naming a torsion is: MoleculeName_Torsion_Integer + // The first part is the name of the molecule + // The second part is always fixed as "Torsion" + // The third part is the index of the torsion defined in meta-data file + // For example, Butane_Torsion_0 is a valid Torsion name in a + // butane molecule + + std::string s = OpenMD_itoa(mol->getNTorsions(), 10); + torsion->setName(mol->getType() + "_Torsion_" + s.c_str()); return torsion; } Inversion* MoleculeCreator::createInversion(ForceField* ff, Molecule* mol, - InversionStamp* stamp) { - + InversionStamp* stamp, + LocalIndexManager* localIndexMan) { + + InversionTypeParser itParser; + InversionType* inversionType = NULL; Inversion* inversion = NULL; + int center = stamp->getCenter(); std::vector satellites = stamp->getSatellites(); if (satellites.size() != 3) { @@ -423,33 +602,68 @@ namespace OpenMD { Atom* atomD = mol->getAtomAt(satellites[2]); assert(atomA && atomB && atomC && atomD); - - InversionType* inversionType = ff->getInversionType(atomA->getType(), - atomB->getType(), - atomC->getType(), - atomD->getType()); - if (inversionType == NULL) { - sprintf(painCave.errMsg, "No Matching Inversion Type for[%s, %s, %s, %s]\n" - "\t(May not be a problem: not all inversions are parametrized)\n", - atomA->getType().c_str(), - atomB->getType().c_str(), - atomC->getType().c_str(), - atomD->getType().c_str()); + if (stamp->hasOverride()) { - painCave.isFatal = 0; - painCave.severity = OPENMD_INFO; - simError(); - return NULL; + try { + inversionType = itParser.parseTypeAndPars(stamp->getOverrideType(), + stamp->getOverridePars() ); + } + catch( OpenMDException e) { + sprintf(painCave.errMsg, "MoleculeCreator Error: %s " + "for molecule %s\n", + e.what(), mol->getType().c_str() ); + painCave.isFatal = 1; + simError(); + } } else { + inversionType = ff->getInversionType(atomA->getType(), + atomB->getType(), + atomC->getType(), + atomD->getType()); + + if (inversionType == NULL) { + sprintf(painCave.errMsg, + "No Matching Inversion Type for[%s, %s, %s, %s]\n" + "\t(May not be a problem: not all inversions are parametrized)\n", + atomA->getType().c_str(), + atomB->getType().c_str(), + atomC->getType().c_str(), + atomD->getType().c_str()); + + painCave.isFatal = 0; + painCave.severity = OPENMD_INFO; + simError(); + } + } + if (inversionType != NULL) { + inversion = new Inversion(atomA, atomB, atomC, atomD, inversionType); + + // set the local index of this inversion, the global index will + // be set later + inversion->setLocalIndex(localIndexMan->getNextInversionIndex()); + + // The rule for naming an inversion is: MoleculeName_Inversion_Integer + // The first part is the name of the molecule + // The second part is always fixed as "Inversion" + // The third part is the index of the inversion defined in meta-data file + // For example, Benzene_Inversion_0 is a valid Inversion name in a + // Benzene molecule + + std::string s = OpenMD_itoa(mol->getNInversions(), 10); + inversion->setName(mol->getType() + "_Inversion_" + s.c_str()); return inversion; + } else { + return NULL; } } - CutoffGroup* MoleculeCreator::createCutoffGroup(Molecule* mol, CutoffGroupStamp* stamp) { + CutoffGroup* MoleculeCreator::createCutoffGroup(Molecule* mol, + CutoffGroupStamp* stamp, + LocalIndexManager* localIndexMan) { int nAtoms; CutoffGroup* cg; Atom* atom; @@ -461,14 +675,22 @@ namespace OpenMD { assert(atom); cg->addAtom(atom); } - + + //set the local index of this cutoffGroup, global index will be set later + cg->setLocalIndex(localIndexMan->getNextCutoffGroupIndex()); + return cg; } - - CutoffGroup* MoleculeCreator::createCutoffGroup(Molecule * mol, Atom* atom) { + + CutoffGroup* MoleculeCreator::createCutoffGroup(Molecule * mol, Atom* atom, + LocalIndexManager* localIndexMan) { CutoffGroup* cg; cg = new CutoffGroup(); cg->addAtom(atom); + + //set the local index of this cutoffGroup, global index will be set later + cg->setLocalIndex(localIndexMan->getNextCutoffGroupIndex()); + return cg; } @@ -477,24 +699,20 @@ namespace OpenMD { //add bond constraints Molecule::BondIterator bi; Bond* bond; + ConstraintPair* cPair; + for (bond = mol->beginBond(bi); bond != NULL; bond = mol->nextBond(bi)) { BondType* bt = bond->getBondType(); - //class Parent1 {}; - //class Child1 : public Parent {}; - //class Child2 : public Parent {}; - //Child1* ch1 = new Child1(); - //Child2* ch2 = dynamic_cast(ch1); - //the dynamic_cast is succeed in above line. A compiler bug? - if (typeid(FixedBondType) == typeid(*bt)) { FixedBondType* fbt = dynamic_cast(bt); ConstraintElem* consElemA = new ConstraintElem(bond->getAtomA()); ConstraintElem* consElemB = new ConstraintElem(bond->getAtomB()); - ConstraintPair* consPair = new ConstraintPair(consElemA, consElemB, fbt->getEquilibriumBondLength()); - mol->addConstraintPair(consPair); + cPair = new ConstraintPair(consElemA, consElemB, + fbt->getEquilibriumBondLength(), false); + mol->addConstraintPair(cPair); } } @@ -506,22 +724,20 @@ namespace OpenMD { ConstraintPair* consPair; Molecule::ConstraintPairIterator cpi; std::set sdSet; - for (consPair = mol->beginConstraintPair(cpi); consPair != NULL; consPair = mol->nextConstraintPair(cpi)) { + for (consPair = mol->beginConstraintPair(cpi); consPair != NULL; + consPair = mol->nextConstraintPair(cpi)) { StuntDouble* sdA = consPair->getConsElem1()->getStuntDouble(); if (sdSet.find(sdA) == sdSet.end()){ sdSet.insert(sdA); mol->addConstraintElem(new ConstraintElem(sdA)); } - + StuntDouble* sdB = consPair->getConsElem2()->getStuntDouble(); if (sdSet.find(sdB) == sdSet.end()){ sdSet.insert(sdB); mol->addConstraintElem(new ConstraintElem(sdB)); - } - + } } - } - }