--- trunk/OOPSE/libmdtools/SimSetup.cpp 2003/07/15 03:08:00 604 +++ trunk/OOPSE/libmdtools/SimSetup.cpp 2004/05/12 16:38:45 1167 @@ -1,11 +1,17 @@ -#include +#include +#include #include -#include - +#include +#include +#include #include "SimSetup.hpp" +#include "ReadWrite.hpp" #include "parse_me.h" #include "Integrator.hpp" #include "simError.h" +#include "RigidBody.hpp" +//#include "ConjugateMinimizer.hpp" +#include "OOPSEMinimizer.hpp" #ifdef IS_MPI #include "mpiBASS.h" @@ -14,24 +20,56 @@ // some defines for ensemble and Forcefield cases -#define NVE_ENS 0 -#define NVT_ENS 1 -#define NPTi_ENS 2 -#define NPTf_ENS 3 -#define NPTim_ENS 4 -#define NPTfm_ENS 5 +#define NVE_ENS 0 +#define NVT_ENS 1 +#define NPTi_ENS 2 +#define NPTf_ENS 3 +#define NPTxyz_ENS 4 -#define FF_DUFF 0 -#define FF_LJ 1 +#define FF_DUFF 0 +#define FF_LJ 1 +#define FF_EAM 2 +#define FF_H2O 3 +using namespace std; +/** + * Check whether dividend is divisble by divisor or not + */ +bool isDivisible(double dividend, double divisor){ + double tolerance = 0.000001; + double quotient; + double diff; + int intQuotient; + + quotient = dividend / divisor; + + if (quotient < 0) + quotient = -quotient; + + intQuotient = int (quotient + tolerance); + + diff = fabs(fabs(dividend) - intQuotient * fabs(divisor)); + + if (diff <= tolerance) + return true; + else + return false; +} + SimSetup::SimSetup(){ + + initSuspend = false; + isInfoArray = 0; + nInfo = 1; + stamps = new MakeStamps(); globals = new Globals(); - + + #ifdef IS_MPI - strcpy( checkPointMsg, "SimSetup creation successful" ); + strcpy(checkPointMsg, "SimSetup creation successful"); MPIcheckPoint(); #endif // IS_MPI } @@ -41,1363 +79,1906 @@ SimSetup::~SimSetup(){ delete globals; } -void SimSetup::parseFile( char* fileName ){ +void SimSetup::setSimInfo(SimInfo* the_info, int theNinfo){ + info = the_info; + nInfo = theNinfo; + isInfoArray = 1; + initSuspend = true; +} + +void SimSetup::parseFile(char* fileName){ #ifdef IS_MPI - if( worldRank == 0 ){ + if (worldRank == 0){ #endif // is_mpi - + inFileName = fileName; - set_interface_stamps( stamps, globals ); - + set_interface_stamps(stamps, globals); + #ifdef IS_MPI mpiEventInit(); #endif - yacc_BASS( fileName ); + yacc_BASS(fileName); #ifdef IS_MPI throwMPIEvent(NULL); } - else receiveParse(); + else{ + receiveParse(); + } #endif } #ifdef IS_MPI void SimSetup::receiveParse(void){ - - set_interface_stamps( stamps, globals ); - mpiEventInit(); - MPIcheckPoint(); - mpiEventLoop(); - + set_interface_stamps(stamps, globals); + mpiEventInit(); + MPIcheckPoint(); + mpiEventLoop(); } #endif // is_mpi -void SimSetup::createSim( void ){ +void SimSetup::createSim(void){ - MakeStamps *the_stamps; - Globals* the_globals; - int i, j, k, globalAtomIndex; - - int ensembleCase; - int ffCase; - - ensembleCase = -1; - ffCase = -1; + // gather all of the information from the Bass file - // get the stamps and globals; - the_stamps = stamps; - the_globals = globals; + gatherInfo(); - // set the easy ones first - simnfo->target_temp = the_globals->getTargetTemp(); - simnfo->dt = the_globals->getDt(); - simnfo->run_time = the_globals->getRunTime(); + // creation of complex system objects - // get the ones we know are there, yet still may need some work. - n_components = the_globals->getNComponents(); - strcpy( force_field, the_globals->getForceField() ); + sysObjectsCreation(); - if( !strcasecmp( force_field, "DUFF" )) ffCase = FF_DUFF; - else if( !strcasecmp( force_field, "LJ" )) ffCase = FF_LJ; - else{ - sprintf( painCave.errMsg, - "SimSetup Error. Unrecognized force field -> %s\n", - force_field ); - painCave.isFatal = 1; - simError(); - } + // check on the post processing info - // get the ensemble: - strcpy( ensemble, the_globals->getEnsemble() ); + finalInfoCheck(); - if( !strcasecmp( ensemble, "NVE" )) ensembleCase = NVE_ENS; - else if( !strcasecmp( ensemble, "NVT" )) ensembleCase = NVT_ENS; - else if( !strcasecmp( ensemble, "NPTi" ) || !strcasecmp( ensemble, "NPT") ) - ensembleCase = NPTi_ENS; - else if( !strcasecmp( ensemble, "NPTf" )) ensembleCase = NPTf_ENS; - else if( !strcasecmp( ensemble, "NPTim" )) ensembleCase = NPTim_ENS; - else if( !strcasecmp( ensemble, "NPTfm" )) ensembleCase = NPTfm_ENS; - else{ - sprintf( painCave.errMsg, - "SimSetup Warning. Unrecognized Ensemble -> %s, " - "reverting to NVE for this simulation.\n", - ensemble ); - painCave.isFatal = 0; - simError(); - strcpy( ensemble, "NVE" ); - ensembleCase = NVE_ENS; + // initialize the system coordinates + + if ( !initSuspend ){ + initSystemCoords(); + + if( !(globals->getUseInitTime()) ) + info[0].currentTime = 0.0; } - strcpy( simnfo->ensemble, ensemble ); + // make the output filenames -// if( !strcasecmp( ensemble, "NPT" ) ) { -// the_extendedsystem = new ExtendedSystem( simnfo ); -// the_extendedsystem->setTargetTemp(the_globals->getTargetTemp()); -// if (the_globals->haveTargetPressure()) -// the_extendedsystem->setTargetPressure(the_globals->getTargetPressure()); -// else { -// sprintf( painCave.errMsg, -// "SimSetup error: If you use the constant pressure\n" -// " ensemble, you must set targetPressure.\n" -// " This was found in the BASS file.\n"); -// painCave.isFatal = 1; -// simError(); -// } + makeOutNames(); + +#ifdef IS_MPI + mpiSim->mpiRefresh(); +#endif -// if (the_globals->haveTauThermostat()) -// the_extendedsystem->setTauThermostat(the_globals->getTauThermostat()); -// else if (the_globals->haveQmass()) -// the_extendedsystem->setQmass(the_globals->getQmass()); -// else { -// sprintf( painCave.errMsg, -// "SimSetup error: If you use one of the constant temperature\n" -// " ensembles, you must set either tauThermostat or qMass.\n" -// " Neither of these was found in the BASS file.\n"); -// painCave.isFatal = 1; -// simError(); -// } + // initialize the Fortran -// if (the_globals->haveTauBarostat()) -// the_extendedsystem->setTauBarostat(the_globals->getTauBarostat()); -// else { -// sprintf( painCave.errMsg, -// "SimSetup error: If you use the constant pressure\n" -// " ensemble, you must set tauBarostat.\n" -// " This was found in the BASS file.\n"); -// painCave.isFatal = 1; -// simError(); -// } + initFortran(); -// } else if ( !strcasecmp( ensemble, "NVT") ) { -// the_extendedsystem = new ExtendedSystem( simnfo ); -// the_extendedsystem->setTargetTemp(the_globals->getTargetTemp()); + if (globals->haveMinimizer()) + // make minimizer + makeMinimizer(); + else + // make the integrator + makeIntegrator(); -// if (the_globals->haveTauThermostat()) -// the_extendedsystem->setTauThermostat(the_globals->getTauThermostat()); -// else if (the_globals->haveQmass()) -// the_extendedsystem->setQmass(the_globals->getQmass()); -// else { -// sprintf( painCave.errMsg, -// "SimSetup error: If you use one of the constant temperature\n" -// " ensembles, you must set either tauThermostat or qMass.\n" -// " Neither of these was found in the BASS file.\n"); -// painCave.isFatal = 1; -// simError(); -// } +} - strcpy( simnfo->mixingRule, the_globals->getMixingRule() ); - simnfo->usePBC = the_globals->getPBC(); - - int usesDipoles = 0; - switch( ffCase ){ - case FF_DUFF: - the_ff = new DUFF(); - usesDipoles = 1; - break; +void SimSetup::makeMolecules(void){ + int i, j, k; + int exI, exJ, exK, exL, slI, slJ; + int tempI, tempJ, tempK, tempL; + int molI; + int stampID, atomOffset, rbOffset; + molInit molInfo; + DirectionalAtom* dAtom; + RigidBody* myRB; + StuntDouble* mySD; + LinkedAssign* extras; + LinkedAssign* current_extra; + AtomStamp* currentAtom; + BondStamp* currentBond; + BendStamp* currentBend; + TorsionStamp* currentTorsion; + RigidBodyStamp* currentRigidBody; + CutoffGroupStamp* currentCutoffGroup; + CutoffGroup* myCutoffGroup; + int nCutoffGroups;// number of cutoff group of a molecule defined in mdl file + set cutoffAtomSet; //atoms belong to cutoffgroup defined at mdl file - case FF_LJ: - the_ff = new LJFF(); - break; + bond_pair* theBonds; + bend_set* theBends; + torsion_set* theTorsions; - default: - sprintf( painCave.errMsg, - "SimSetup Error. Unrecognized force field in case statement.\n"); - painCave.isFatal = 1; - simError(); - } + set skipList; -#ifdef IS_MPI - strcpy( checkPointMsg, "ForceField creation successful" ); - MPIcheckPoint(); -#endif // is_mpi + double phi, theta, psi; + char* molName; + char rbName[100]; - // get the components and calculate the tot_nMol and indvidual n_mol - the_components = the_globals->getComponents(); - components_nmol = new int[n_components]; - comp_stamps = new MoleculeStamp*[n_components]; + //init the forceField paramters - if( !the_globals->haveNMol() ){ - // we don't have the total number of molecules, so we assume it is - // given in each component + the_ff->readParams(); - tot_nmol = 0; - for( i=0; ihaveNMol() ){ - // we have a problem - sprintf( painCave.errMsg, - "SimSetup Error. No global NMol or component NMol" - " given. Cannot calculate the number of atoms.\n" ); - painCave.isFatal = 1; - simError(); - } + int nMembers, nNew, rb1, rb2; + + for (k = 0; k < nInfo; k++){ + the_ff->setSimInfo(&(info[k])); - tot_nmol += the_components[i]->getNMol(); - components_nmol[i] = the_components[i]->getNMol(); - } - } - else{ - sprintf( painCave.errMsg, - "SimSetup error.\n" - "\tSorry, the ability to specify total" - " nMols and then give molfractions in the components\n" - "\tis not currently supported." - " Please give nMol in the components.\n" ); - painCave.isFatal = 1; - simError(); - - - // tot_nmol = the_globals->getNMol(); - - // //we have the total number of molecules, now we check for molfractions - // for( i=0; ihaveMolFraction() ){ - - // if( !the_components[i]->haveNMol() ){ - // //we have a problem - // std::cerr << "SimSetup error. Neither molFraction nor " - // << " nMol was given in component - - } + atomOffset = 0; -#ifdef IS_MPI - strcpy( checkPointMsg, "Have the number of components" ); - MPIcheckPoint(); -#endif // is_mpi + for (i = 0; i < info[k].n_mol; i++){ + stampID = info[k].molecules[i].getStampID(); + molName = comp_stamps[stampID]->getID(); - // make an array of molecule stamps that match the components used. - // also extract the used stamps out into a separate linked list + molInfo.nAtoms = comp_stamps[stampID]->getNAtoms(); + molInfo.nBonds = comp_stamps[stampID]->getNBonds(); + molInfo.nBends = comp_stamps[stampID]->getNBends(); + molInfo.nTorsions = comp_stamps[stampID]->getNTorsions(); + molInfo.nRigidBodies = comp_stamps[stampID]->getNRigidBodies(); - simnfo->nComponents = n_components; - simnfo->componentsNmol = components_nmol; - simnfo->compStamps = comp_stamps; - simnfo->headStamp = new LinkedMolStamp(); - - char* id; - LinkedMolStamp* headStamp = simnfo->headStamp; - LinkedMolStamp* currentStamp = NULL; - for( i=0; igetNCutoffGroups(); + + molInfo.myAtoms = &(info[k].atoms[atomOffset]); - id = the_components[i]->getType(); - comp_stamps[i] = NULL; - - // check to make sure the component isn't already in the list + if (molInfo.nBonds > 0) + molInfo.myBonds = new (Bond *) [molInfo.nBonds]; + else + molInfo.myBonds = NULL; - comp_stamps[i] = headStamp->match( id ); - if( comp_stamps[i] == NULL ){ + if (molInfo.nBends > 0) + molInfo.myBends = new (Bend *) [molInfo.nBends]; + else + molInfo.myBends = NULL; + + if (molInfo.nTorsions > 0) + molInfo.myTorsions = new (Torsion *) [molInfo.nTorsions]; + else + molInfo.myTorsions = NULL; + + theBonds = new bond_pair[molInfo.nBonds]; + theBends = new bend_set[molInfo.nBends]; + theTorsions = new torsion_set[molInfo.nTorsions]; - // extract the component from the list; - - currentStamp = the_stamps->extractMolStamp( id ); - if( currentStamp == NULL ){ - sprintf( painCave.errMsg, - "SimSetup error: Component \"%s\" was not found in the " - "list of declared molecules\n", - id ); - painCave.isFatal = 1; - simError(); - } - - headStamp->add( currentStamp ); - comp_stamps[i] = headStamp->match( id ); - } - } + // make the Atoms -#ifdef IS_MPI - strcpy( checkPointMsg, "Component stamps successfully extracted\n" ); - MPIcheckPoint(); -#endif // is_mpi - + for (j = 0; j < molInfo.nAtoms; j++){ + currentAtom = comp_stamps[stampID]->getAtom(j); + if (currentAtom->haveOrientation()){ + dAtom = new DirectionalAtom((j + atomOffset), + info[k].getConfiguration()); + info[k].n_oriented++; + molInfo.myAtoms[j] = dAtom; + // Directional Atoms have standard unit vectors which are oriented + // in space using the three Euler angles. We assume the standard + // unit vector was originally along the z axis below. - // caclulate the number of atoms, bonds, bends and torsions + phi = currentAtom->getEulerPhi() * M_PI / 180.0; + theta = currentAtom->getEulerTheta() * M_PI / 180.0; + psi = currentAtom->getEulerPsi()* M_PI / 180.0; - tot_atoms = 0; - tot_bonds = 0; - tot_bends = 0; - tot_torsions = 0; - for( i=0; igetNAtoms(); - tot_bonds += components_nmol[i] * comp_stamps[i]->getNBonds(); - tot_bends += components_nmol[i] * comp_stamps[i]->getNBends(); - tot_torsions += components_nmol[i] * comp_stamps[i]->getNTorsions(); - } + dAtom->setUnitFrameFromEuler(phi, theta, psi); + + } + else{ - tot_SRI = tot_bonds + tot_bends + tot_torsions; + molInfo.myAtoms[j] = new Atom((j + atomOffset), info[k].getConfiguration()); - simnfo->n_atoms = tot_atoms; - simnfo->n_bonds = tot_bonds; - simnfo->n_bends = tot_bends; - simnfo->n_torsions = tot_torsions; - simnfo->n_SRI = tot_SRI; - simnfo->n_mol = tot_nmol; - - simnfo->molMembershipArray = new int[tot_atoms]; + } + molInfo.myAtoms[j]->setType(currentAtom->getType()); #ifdef IS_MPI - // divide the molecules among processors here. - - mpiSim = new mpiSimulation( simnfo ); - - globalIndex = mpiSim->divideLabor(); + molInfo.myAtoms[j]->setGlobalIndex(globalAtomIndex[j + atomOffset]); - // set up the local variables - - int localMol, allMol; - int local_atoms, local_bonds, local_bends, local_torsions, local_SRI; +#endif // is_mpi + } - int* mol2proc = mpiSim->getMolToProcMap(); - int* molCompType = mpiSim->getMolComponentType(); - - allMol = 0; - localMol = 0; - local_atoms = 0; - local_bonds = 0; - local_bends = 0; - local_torsions = 0; - globalAtomIndex = 0; + // make the bonds + for (j = 0; j < molInfo.nBonds; j++){ + currentBond = comp_stamps[stampID]->getBond(j); + theBonds[j].a = currentBond->getA() + atomOffset; + theBonds[j].b = currentBond->getB() + atomOffset; + tempI = theBonds[j].a; + tempJ = theBonds[j].b; - for( i=0; igetGlobalIndex() + 1; + exJ = info[k].atoms[tempJ]->getGlobalIndex() + 1; +#else + exI = tempI + 1; + exJ = tempJ + 1; +#endif - for( j=0; jgetNAtoms(); - local_bonds += comp_stamps[i]->getNBonds(); - local_bends += comp_stamps[i]->getNBends(); - local_torsions += comp_stamps[i]->getNTorsions(); - localMol++; - } - for (k = 0; k < comp_stamps[i]->getNAtoms(); k++) { - simnfo->molMembershipArray[globalAtomIndex] = allMol; - globalAtomIndex++; + info[k].excludes->addPair(exI, exJ); } - allMol++; - } - } - local_SRI = local_bonds + local_bends + local_torsions; - - simnfo->n_atoms = mpiSim->getMyNlocal(); - - if( local_atoms != simnfo->n_atoms ){ - sprintf( painCave.errMsg, - "SimSetup error: mpiSim's localAtom (%d) and SimSetup's" - " localAtom (%d) are not equal.\n", - simnfo->n_atoms, - local_atoms ); - painCave.isFatal = 1; - simError(); - } + //make the bends + for (j = 0; j < molInfo.nBends; j++){ + currentBend = comp_stamps[stampID]->getBend(j); + theBends[j].a = currentBend->getA() + atomOffset; + theBends[j].b = currentBend->getB() + atomOffset; + theBends[j].c = currentBend->getC() + atomOffset; - simnfo->n_bonds = local_bonds; - simnfo->n_bends = local_bends; - simnfo->n_torsions = local_torsions; - simnfo->n_SRI = local_SRI; - simnfo->n_mol = localMol; + if (currentBend->haveExtras()){ + extras = currentBend->getExtras(); + current_extra = extras; - strcpy( checkPointMsg, "Passed nlocal consistency check." ); - MPIcheckPoint(); - - -#endif // is_mpi - + while (current_extra != NULL){ + if (!strcmp(current_extra->getlhs(), "ghostVectorSource")){ + switch (current_extra->getType()){ + case 0: + theBends[j].ghost = current_extra->getInt() + atomOffset; + theBends[j].isGhost = 1; + break; - // create the atom and short range interaction arrays - - Atom::createArrays(simnfo->n_atoms); - the_atoms = new Atom*[simnfo->n_atoms]; - the_molecules = new Molecule[simnfo->n_mol]; - int molIndex; - - // initialize the molecule's stampID's + case 1: + theBends[j].ghost = (int) current_extra->getDouble() + + atomOffset; + theBends[j].isGhost = 1; + break; + default: + sprintf(painCave.errMsg, + "SimSetup Error: ghostVectorSource was neither a " + "double nor an int.\n" + "-->Bend[%d] in %s\n", + j, comp_stamps[stampID]->getID()); + painCave.isFatal = 1; + simError(); + } + } + else{ + sprintf(painCave.errMsg, + "SimSetup Error: unhandled bend assignment:\n" + " -->%s in Bend[%d] in %s\n", + current_extra->getlhs(), j, comp_stamps[stampID]->getID()); + painCave.isFatal = 1; + simError(); + } + + current_extra = current_extra->getNext(); + } + } + + if (theBends[j].isGhost) { + + tempI = theBends[j].a; + tempJ = theBends[j].b; + #ifdef IS_MPI - + exI = info[k].atoms[tempI]->getGlobalIndex() + 1; + exJ = info[k].atoms[tempJ]->getGlobalIndex() + 1; +#else + exI = tempI + 1; + exJ = tempJ + 1; +#endif + info[k].excludes->addPair(exI, exJ); - molIndex = 0; - for(i=0; igetTotNmol(); i++){ - - if(mol2proc[i] == worldRank ){ - the_molecules[molIndex].setStampID( molCompType[i] ); - the_molecules[molIndex].setMyIndex( molIndex ); - the_molecules[molIndex].setGlobalIndex( i ); - molIndex++; - } - } + } else { -#else // is_mpi - - molIndex = 0; - globalAtomIndex = 0; - for(i=0; igetNAtoms(); k++) { - simnfo->molMembershipArray[globalAtomIndex] = molIndex; - globalAtomIndex++; + tempI = theBends[j].a; + tempJ = theBends[j].b; + tempK = theBends[j].c; + +#ifdef IS_MPI + exI = info[k].atoms[tempI]->getGlobalIndex() + 1; + exJ = info[k].atoms[tempJ]->getGlobalIndex() + 1; + exK = info[k].atoms[tempK]->getGlobalIndex() + 1; +#else + exI = tempI + 1; + exJ = tempJ + 1; + exK = tempK + 1; +#endif + + info[k].excludes->addPair(exI, exK); + info[k].excludes->addPair(exI, exJ); + info[k].excludes->addPair(exJ, exK); + } } - molIndex++; - } - } - -#endif // is_mpi + for (j = 0; j < molInfo.nTorsions; j++){ + currentTorsion = comp_stamps[stampID]->getTorsion(j); + theTorsions[j].a = currentTorsion->getA() + atomOffset; + theTorsions[j].b = currentTorsion->getB() + atomOffset; + theTorsions[j].c = currentTorsion->getC() + atomOffset; + theTorsions[j].d = currentTorsion->getD() + atomOffset; + tempI = theTorsions[j].a; + tempJ = theTorsions[j].b; + tempK = theTorsions[j].c; + tempL = theTorsions[j].d; - if( simnfo->n_SRI ){ - - Exclude::createArray(simnfo->n_SRI); - the_excludes = new Exclude*[simnfo->n_SRI]; - for( int ex=0; exn_SRI; ex++) the_excludes[ex] = new Exclude(ex); - simnfo->globalExcludes = new int; - simnfo->n_exclude = simnfo->n_SRI; - } - else{ - - Exclude::createArray( 1 ); - the_excludes = new Exclude*; - the_excludes[0] = new Exclude(0); - the_excludes[0]->setPair( 0,0 ); - simnfo->globalExcludes = new int; - simnfo->globalExcludes[0] = 0; - simnfo->n_exclude = 0; - } +#ifdef IS_MPI + exI = info[k].atoms[tempI]->getGlobalIndex() + 1; + exJ = info[k].atoms[tempJ]->getGlobalIndex() + 1; + exK = info[k].atoms[tempK]->getGlobalIndex() + 1; + exL = info[k].atoms[tempL]->getGlobalIndex() + 1; +#else + exI = tempI + 1; + exJ = tempJ + 1; + exK = tempK + 1; + exL = tempL + 1; +#endif - // set the arrays into the SimInfo object + info[k].excludes->addPair(exI, exJ); + info[k].excludes->addPair(exI, exK); + info[k].excludes->addPair(exI, exL); + info[k].excludes->addPair(exJ, exK); + info[k].excludes->addPair(exJ, exL); + info[k].excludes->addPair(exK, exL); + } - simnfo->atoms = the_atoms; - simnfo->molecules = the_molecules; - simnfo->nGlobalExcludes = 0; - simnfo->excludes = the_excludes; + + molInfo.myRigidBodies.clear(); + + for (j = 0; j < molInfo.nRigidBodies; j++){ + currentRigidBody = comp_stamps[stampID]->getRigidBody(j); + nMembers = currentRigidBody->getNMembers(); - // get some of the tricky things that may still be in the globals + // Create the Rigid Body: - double boxVector[3]; - if( the_globals->haveBox() ){ - boxVector[0] = the_globals->getBox(); - boxVector[1] = the_globals->getBox(); - boxVector[2] = the_globals->getBox(); - - simnfo->setBox( boxVector ); - } - else if( the_globals->haveDensity() ){ + myRB = new RigidBody(); - double vol; - vol = (double)tot_nmol / the_globals->getDensity(); - boxVector[0] = pow( vol, ( 1.0 / 3.0 ) ); - boxVector[1] = boxVector[0]; - boxVector[2] = boxVector[0]; + sprintf(rbName,"%s_RB_%d", molName, j); + myRB->setType(rbName); + + for (rb1 = 0; rb1 < nMembers; rb1++) { - simnfo->setBox( boxVector ); - } - else{ - if( !the_globals->haveBoxX() ){ - sprintf( painCave.errMsg, - "SimSetup error, no periodic BoxX size given.\n" ); - painCave.isFatal = 1; - simError(); - } - boxVector[0] = the_globals->getBoxX(); + // molI is atom numbering inside this molecule + molI = currentRigidBody->getMember(rb1); - if( !the_globals->haveBoxY() ){ - sprintf( painCave.errMsg, - "SimSetup error, no periodic BoxY size given.\n" ); - painCave.isFatal = 1; - simError(); - } - boxVector[1] = the_globals->getBoxY(); + // tempI is atom numbering on local processor + tempI = molI + atomOffset; - if( !the_globals->haveBoxZ() ){ - sprintf( painCave.errMsg, - "SimSetup error, no periodic BoxZ size given.\n" ); - painCave.isFatal = 1; - simError(); - } - boxVector[2] = the_globals->getBoxZ(); + // currentAtom is the AtomStamp (which we need for + // rigid body reference positions) + currentAtom = comp_stamps[stampID]->getAtom(molI); - simnfo->setBox( boxVector ); - } + // When we add to the rigid body, add the atom itself and + // the stamp info: + myRB->addAtom(info[k].atoms[tempI], currentAtom); + + // Add this atom to the Skip List for the integrators #ifdef IS_MPI - strcpy( checkPointMsg, "Box size set up" ); - MPIcheckPoint(); -#endif // is_mpi + slI = info[k].atoms[tempI]->getGlobalIndex(); +#else + slI = tempI; +#endif + skipList.insert(slI); + + } + + for(rb1 = 0; rb1 < nMembers - 1; rb1++) { + for(rb2 = rb1+1; rb2 < nMembers; rb2++) { + + tempI = currentRigidBody->getMember(rb1); + tempJ = currentRigidBody->getMember(rb2); + + // Some explanation is required here. + // Fortran indexing starts at 1, while c indexing starts at 0 + // Also, in parallel computations, the GlobalIndex is + // used for the exclude list: + +#ifdef IS_MPI + exI = molInfo.myAtoms[tempI]->getGlobalIndex() + 1; + exJ = molInfo.myAtoms[tempJ]->getGlobalIndex() + 1; +#else + exI = molInfo.myAtoms[tempI]->getIndex() + 1; + exJ = molInfo.myAtoms[tempJ]->getIndex() + 1; +#endif + + info[k].excludes->addPair(exI, exJ); + + } + } + molInfo.myRigidBodies.push_back(myRB); + info[k].rigidBodies.push_back(myRB); + } + - // initialize the arrays + //create cutoff group for molecule - the_ff->setSimInfo( simnfo ); + cutoffAtomSet.clear(); + molInfo.myCutoffGroups.clear(); + + for (j = 0; j < nCutoffGroups; j++){ - makeMolecules(); - simnfo->identArray = new int[simnfo->n_atoms]; - for(i=0; in_atoms; i++){ - simnfo->identArray[i] = the_atoms[i]->getIdent(); - } - - if (the_globals->getUseRF() ) { - simnfo->useReactionField = 1; - - if( !the_globals->haveECR() ){ - sprintf( painCave.errMsg, - "SimSetup Warning: using default value of 1/2 the smallest " - "box length for the electrostaticCutoffRadius.\n" - "I hope you have a very fast processor!\n"); - painCave.isFatal = 0; - simError(); - double smallest; - smallest = simnfo->boxLx; - if (simnfo->boxLy <= smallest) smallest = simnfo->boxLy; - if (simnfo->boxLz <= smallest) smallest = simnfo->boxLz; - simnfo->ecr = 0.5 * smallest; - } else { - simnfo->ecr = the_globals->getECR(); - } + currentCutoffGroup = comp_stamps[stampID]->getCutoffGroup(j); + nMembers = currentCutoffGroup->getNMembers(); - if( !the_globals->haveEST() ){ - sprintf( painCave.errMsg, - "SimSetup Warning: using default value of 0.05 * the " - "electrostaticCutoffRadius for the electrostaticSkinThickness\n" - ); - painCave.isFatal = 0; - simError(); - simnfo->est = 0.05 * simnfo->ecr; - } else { - simnfo->est = the_globals->getEST(); - } - - if(!the_globals->haveDielectric() ){ - sprintf( painCave.errMsg, - "SimSetup Error: You are trying to use Reaction Field without" - "setting a dielectric constant!\n" - ); - painCave.isFatal = 1; - simError(); - } - simnfo->dielectric = the_globals->getDielectric(); - } else { - if (usesDipoles) { - - if( !the_globals->haveECR() ){ - sprintf( painCave.errMsg, - "SimSetup Warning: using default value of 1/2 the smallest " - "box length for the electrostaticCutoffRadius.\n" - "I hope you have a very fast processor!\n"); - painCave.isFatal = 0; - simError(); - double smallest; - smallest = simnfo->boxLx; - if (simnfo->boxLy <= smallest) smallest = simnfo->boxLy; - if (simnfo->boxLz <= smallest) smallest = simnfo->boxLz; - simnfo->ecr = 0.5 * smallest; - } else { - simnfo->ecr = the_globals->getECR(); + myCutoffGroup = new CutoffGroup(); + + for (int cg = 0; cg < nMembers; cg++) { + + // molI is atom numbering inside this molecule + molI = currentCutoffGroup->getMember(cg); + + // tempI is atom numbering on local processor + tempI = molI + atomOffset; + + myCutoffGroup->addAtom(info[k].atoms[tempI]); + + cutoffAtomSet.insert(tempI); + } + + molInfo.myCutoffGroups.push_back(myCutoffGroup); + }//end for (j = 0; j < molInfo.nCutoffGroups; j++) + + //creat a cutoff group for every atom in current molecule which does not belong to cutoffgroup defined at mdl file + + for(j = 0; j < molInfo.nAtoms; j++){ + + if(cutoffAtomSet.find(molInfo.myAtoms[j]->getIndex()) == cutoffAtomSet.end()){ + myCutoffGroup = new CutoffGroup(); + myCutoffGroup->addAtom(molInfo.myAtoms[j]); + molInfo.myCutoffGroups.push_back(myCutoffGroup); + } + } + + + + + // After this is all set up, scan through the atoms to + // see if they can be added to the integrableObjects: + + molInfo.myIntegrableObjects.clear(); - if( !the_globals->haveEST() ){ - sprintf( painCave.errMsg, - "SimSetup Warning: using default value of 5%% of the " - "electrostaticCutoffRadius for the " - "electrostaticSkinThickness\n" - ); - painCave.isFatal = 0; - simError(); - simnfo->est = 0.05 * simnfo->ecr; - } else { - simnfo->est = the_globals->getEST(); - } - } - } -#ifdef IS_MPI - strcpy( checkPointMsg, "electrostatic parameters check out" ); - MPIcheckPoint(); -#endif // is_mpi + for (j = 0; j < molInfo.nAtoms; j++){ - if( the_globals->haveInitialConfig() ){ - - InitializeFromFile* fileInit; -#ifdef IS_MPI // is_mpi - if( worldRank == 0 ){ -#endif //is_mpi - fileInit = new InitializeFromFile( the_globals->getInitialConfig() ); #ifdef IS_MPI - }else fileInit = new InitializeFromFile( NULL ); + slJ = molInfo.myAtoms[j]->getGlobalIndex(); +#else + slJ = j+atomOffset; #endif - fileInit->read_xyz( simnfo ); // default velocities on - delete fileInit; - } - else{ + // if they aren't on the skip list, then they can be integrated -#ifdef IS_MPI + if (skipList.find(slJ) == skipList.end()) { + mySD = (StuntDouble *) molInfo.myAtoms[j]; + info[k].integrableObjects.push_back(mySD); + molInfo.myIntegrableObjects.push_back(mySD); + } + } - // no init from bass - - sprintf( painCave.errMsg, - "Cannot intialize a parallel simulation without an initial configuration file.\n" ); - painCave.isFatal; - simError(); - -#else + // all rigid bodies are integrated: - initFromBass(); + for (j = 0; j < molInfo.nRigidBodies; j++) { + mySD = (StuntDouble *) molInfo.myRigidBodies[j]; + info[k].integrableObjects.push_back(mySD); + molInfo.myIntegrableObjects.push_back(mySD); + } + + + // send the arrays off to the forceField for init. + + the_ff->initializeAtoms(molInfo.nAtoms, molInfo.myAtoms); + the_ff->initializeBonds(molInfo.nBonds, molInfo.myBonds, theBonds); + the_ff->initializeBends(molInfo.nBends, molInfo.myBends, theBends); + the_ff->initializeTorsions(molInfo.nTorsions, molInfo.myTorsions, + theTorsions); + info[k].molecules[i].initialize(molInfo); -#endif - } + atomOffset += molInfo.nAtoms; + delete[] theBonds; + delete[] theBends; + delete[] theTorsions; + } + } + #ifdef IS_MPI - strcpy( checkPointMsg, "Successfully read in the initial configuration" ); + sprintf(checkPointMsg, "all molecules initialized succesfully"); MPIcheckPoint(); #endif // is_mpi +} - - - +void SimSetup::initFromBass(void){ + int i, j, k; + int n_cells; + double cellx, celly, cellz; + double temp1, temp2, temp3; + int n_per_extra; + int n_extra; + int have_extra, done; - -#ifdef IS_MPI - if( worldRank == 0 ){ -#endif // is_mpi - - if( the_globals->haveFinalConfig() ){ - strcpy( simnfo->finalName, the_globals->getFinalConfig() ); + double vel[3]; + vel[0] = 0.0; + vel[1] = 0.0; + vel[2] = 0.0; + + temp1 = (double) tot_nmol / 4.0; + temp2 = pow(temp1, (1.0 / 3.0)); + temp3 = ceil(temp2); + + have_extra = 0; + if (temp2 < temp3){ + // we have a non-complete lattice + have_extra = 1; + + n_cells = (int) temp3 - 1; + cellx = info[0].boxL[0] / temp3; + celly = info[0].boxL[1] / temp3; + cellz = info[0].boxL[2] / temp3; + n_extra = tot_nmol - (4 * n_cells * n_cells * n_cells); + temp1 = ((double) n_extra) / (pow(temp3, 3.0) - pow(n_cells, 3.0)); + n_per_extra = (int) ceil(temp1); + + if (n_per_extra > 4){ + sprintf(painCave.errMsg, + "SimSetup error. There has been an error in constructing" + " the non-complete lattice.\n"); + painCave.isFatal = 1; + simError(); } - else{ - strcpy( simnfo->finalName, inFileName ); - char* endTest; - int nameLength = strlen( simnfo->finalName ); - endTest = &(simnfo->finalName[nameLength - 5]); - if( !strcmp( endTest, ".bass" ) ){ - strcpy( endTest, ".eor" ); - } - else if( !strcmp( endTest, ".BASS" ) ){ - strcpy( endTest, ".eor" ); - } - else{ - endTest = &(simnfo->finalName[nameLength - 4]); - if( !strcmp( endTest, ".bss" ) ){ - strcpy( endTest, ".eor" ); - } - else if( !strcmp( endTest, ".mdl" ) ){ - strcpy( endTest, ".eor" ); - } - else{ - strcat( simnfo->finalName, ".eor" ); - } - } - } - - // make the sample and status out names - - strcpy( simnfo->sampleName, inFileName ); - char* endTest; - int nameLength = strlen( simnfo->sampleName ); - endTest = &(simnfo->sampleName[nameLength - 5]); - if( !strcmp( endTest, ".bass" ) ){ - strcpy( endTest, ".dump" ); - } - else if( !strcmp( endTest, ".BASS" ) ){ - strcpy( endTest, ".dump" ); - } - else{ - endTest = &(simnfo->sampleName[nameLength - 4]); - if( !strcmp( endTest, ".bss" ) ){ - strcpy( endTest, ".dump" ); - } - else if( !strcmp( endTest, ".mdl" ) ){ - strcpy( endTest, ".dump" ); - } - else{ - strcat( simnfo->sampleName, ".dump" ); - } - } - - strcpy( simnfo->statusName, inFileName ); - nameLength = strlen( simnfo->statusName ); - endTest = &(simnfo->statusName[nameLength - 5]); - if( !strcmp( endTest, ".bass" ) ){ - strcpy( endTest, ".stat" ); - } - else if( !strcmp( endTest, ".BASS" ) ){ - strcpy( endTest, ".stat" ); - } - else{ - endTest = &(simnfo->statusName[nameLength - 4]); - if( !strcmp( endTest, ".bss" ) ){ - strcpy( endTest, ".stat" ); - } - else if( !strcmp( endTest, ".mdl" ) ){ - strcpy( endTest, ".stat" ); - } - else{ - strcat( simnfo->statusName, ".stat" ); - } - } - -#ifdef IS_MPI } -#endif // is_mpi - - // set the status, sample, and themal kick times - - if( the_globals->haveSampleTime() ){ - simnfo->sampleTime = the_globals->getSampleTime(); - simnfo->statusTime = simnfo->sampleTime; - simnfo->thermalTime = simnfo->sampleTime; - } else{ - simnfo->sampleTime = the_globals->getRunTime(); - simnfo->statusTime = simnfo->sampleTime; - simnfo->thermalTime = simnfo->sampleTime; + n_cells = (int) temp3; + cellx = info[0].boxL[0] / temp3; + celly = info[0].boxL[1] / temp3; + cellz = info[0].boxL[2] / temp3; } - if( the_globals->haveStatusTime() ){ - simnfo->statusTime = the_globals->getStatusTime(); - } + current_mol = 0; + current_comp_mol = 0; + current_comp = 0; + current_atom_ndx = 0; - if( the_globals->haveThermalTime() ){ - simnfo->thermalTime = the_globals->getThermalTime(); - } + for (i = 0; i < n_cells ; i++){ + for (j = 0; j < n_cells; j++){ + for (k = 0; k < n_cells; k++){ + makeElement(i * cellx, j * celly, k * cellz); - // check for the temperature set flag + makeElement(i * cellx + 0.5 * cellx, j * celly + 0.5 * celly, k * cellz); - if( the_globals->haveTempSet() ) simnfo->setTemp = the_globals->getTempSet(); + makeElement(i * cellx, j * celly + 0.5 * celly, k * cellz + 0.5 * cellz); + makeElement(i * cellx + 0.5 * cellx, j * celly, k * cellz + 0.5 * cellz); + } + } + } - // make the integrator - - - NVT* myNVT = NULL; - NPTi* myNPTi = NULL; - NPTf* myNPTf = NULL; - NPTim* myNPTim = NULL; + if (have_extra){ + done = 0; - switch( ensembleCase ){ + int start_ndx; + for (i = 0; i < (n_cells + 1) && !done; i++){ + for (j = 0; j < (n_cells + 1) && !done; j++){ + if (i < n_cells){ + if (j < n_cells){ + start_ndx = n_cells; + } + else + start_ndx = 0; + } + else + start_ndx = 0; - case NVE_ENS: - new NVE( simnfo, the_ff ); - break; + for (k = start_ndx; k < (n_cells + 1) && !done; k++){ + makeElement(i * cellx, j * celly, k * cellz); + done = (current_mol >= tot_nmol); - case NVT_ENS: - myNVT = new NVT( simnfo, the_ff ); - myNVT->setTargetTemp(the_globals->getTargetTemp()); + if (!done && n_per_extra > 1){ + makeElement(i * cellx + 0.5 * cellx, j * celly + 0.5 * celly, + k * cellz); + done = (current_mol >= tot_nmol); + } - if (the_globals->haveTauThermostat()) - myNVT->setTauThermostat(the_globals->getTauThermostat()); + if (!done && n_per_extra > 2){ + makeElement(i * cellx, j * celly + 0.5 * celly, + k * cellz + 0.5 * cellz); + done = (current_mol >= tot_nmol); + } - else { - sprintf( painCave.errMsg, - "SimSetup error: If you use the NVT\n" - " ensemble, you must set tauThermostat.\n"); - painCave.isFatal = 1; - simError(); + if (!done && n_per_extra > 3){ + makeElement(i * cellx + 0.5 * cellx, j * celly, + k * cellz + 0.5 * cellz); + done = (current_mol >= tot_nmol); + } + } + } } - break; + } - case NPTi_ENS: - myNPTi = new NPTi( simnfo, the_ff ); - myNPTi->setTargetTemp( the_globals->getTargetTemp()); + for (i = 0; i < info[0].n_atoms; i++){ + info[0].atoms[i]->setVel(vel); + } +} - if (the_globals->haveTargetPressure()) - myNPTi->setTargetPressure(the_globals->getTargetPressure()); - else { - sprintf( painCave.errMsg, - "SimSetup error: If you use a constant pressure\n" - " ensemble, you must set targetPressure in the BASS file.\n"); - painCave.isFatal = 1; - simError(); - } - - if( the_globals->haveTauThermostat() ) - myNPTi->setTauThermostat( the_globals->getTauThermostat() ); - else{ - sprintf( painCave.errMsg, - "SimSetup error: If you use an NPT\n" - " ensemble, you must set tauThermostat.\n"); - painCave.isFatal = 1; - simError(); - } +void SimSetup::makeElement(double x, double y, double z){ + int k; + AtomStamp* current_atom; + DirectionalAtom* dAtom; + double rotMat[3][3]; + double pos[3]; - if( the_globals->haveTauBarostat() ) - myNPTi->setTauBarostat( the_globals->getTauBarostat() ); - else{ - sprintf( painCave.errMsg, - "SimSetup error: If you use an NPT\n" - " ensemble, you must set tauBarostat.\n"); + for (k = 0; k < comp_stamps[current_comp]->getNAtoms(); k++){ + current_atom = comp_stamps[current_comp]->getAtom(k); + if (!current_atom->havePosition()){ + sprintf(painCave.errMsg, + "SimSetup:initFromBass error.\n" + "\tComponent %s, atom %s does not have a position specified.\n" + "\tThe initialization routine is unable to give a start" + " position.\n", + comp_stamps[current_comp]->getID(), current_atom->getType()); painCave.isFatal = 1; simError(); } - break; - case NPTf_ENS: - myNPTf = new NPTf( simnfo, the_ff ); - myNPTf->setTargetTemp( the_globals->getTargetTemp()); + pos[0] = x + current_atom->getPosX(); + pos[1] = y + current_atom->getPosY(); + pos[2] = z + current_atom->getPosZ(); - if (the_globals->haveTargetPressure()) - myNPTf->setTargetPressure(the_globals->getTargetPressure()); - else { - sprintf( painCave.errMsg, - "SimSetup error: If you use a constant pressure\n" - " ensemble, you must set targetPressure in the BASS file.\n"); - painCave.isFatal = 1; - simError(); - } + info[0].atoms[current_atom_ndx]->setPos(pos); - if( the_globals->haveTauThermostat() ) - myNPTf->setTauThermostat( the_globals->getTauThermostat() ); - else{ - sprintf( painCave.errMsg, - "SimSetup error: If you use an NPT\n" - " ensemble, you must set tauThermostat.\n"); - painCave.isFatal = 1; - simError(); - } + if (info[0].atoms[current_atom_ndx]->isDirectional()){ + dAtom = (DirectionalAtom *) info[0].atoms[current_atom_ndx]; - if( the_globals->haveTauBarostat() ) - myNPTf->setTauBarostat( the_globals->getTauBarostat() ); - else{ - sprintf( painCave.errMsg, - "SimSetup error: If you use an NPT\n" - " ensemble, you must set tauBarostat.\n"); - painCave.isFatal = 1; - simError(); - } - break; - - case NPTim_ENS: - myNPTim = new NPTim( simnfo, the_ff ); - myNPTim->setTargetTemp( the_globals->getTargetTemp()); + rotMat[0][0] = 1.0; + rotMat[0][1] = 0.0; + rotMat[0][2] = 0.0; - if (the_globals->haveTargetPressure()) - myNPTim->setTargetPressure(the_globals->getTargetPressure()); - else { - sprintf( painCave.errMsg, - "SimSetup error: If you use a constant pressure\n" - " ensemble, you must set targetPressure in the BASS file.\n"); - painCave.isFatal = 1; - simError(); - } - - if( the_globals->haveTauThermostat() ) - myNPTim->setTauThermostat( the_globals->getTauThermostat() ); - else{ - sprintf( painCave.errMsg, - "SimSetup error: If you use an NPT\n" - " ensemble, you must set tauThermostat.\n"); - painCave.isFatal = 1; - simError(); - } + rotMat[1][0] = 0.0; + rotMat[1][1] = 1.0; + rotMat[1][2] = 0.0; - if( the_globals->haveTauBarostat() ) - myNPTim->setTauBarostat( the_globals->getTauBarostat() ); - else{ - sprintf( painCave.errMsg, - "SimSetup error: If you use an NPT\n" - " ensemble, you must set tauBarostat.\n"); - painCave.isFatal = 1; - simError(); + rotMat[2][0] = 0.0; + rotMat[2][1] = 0.0; + rotMat[2][2] = 1.0; + + dAtom->setA(rotMat); } - break; - + current_atom_ndx++; + } - default: - sprintf( painCave.errMsg, - "SimSetup Error. Unrecognized ensemble in case statement.\n"); - painCave.isFatal = 1; - simError(); + current_mol++; + current_comp_mol++; + + if (current_comp_mol >= components_nmol[current_comp]){ + current_comp_mol = 0; + current_comp++; } +} -#ifdef IS_MPI - mpiSim->mpiRefresh(); -#endif +void SimSetup::gatherInfo(void){ + int i; - // initialize the Fortran + ensembleCase = -1; + ffCase = -1; + // set the easy ones first - simnfo->refreshSim(); - - if( !strcmp( simnfo->mixingRule, "standard") ){ - the_ff->initForceField( LB_MIXING_RULE ); + for (i = 0; i < nInfo; i++){ + info[i].target_temp = globals->getTargetTemp(); + info[i].dt = globals->getDt(); + info[i].run_time = globals->getRunTime(); } - else if( !strcmp( simnfo->mixingRule, "explicit") ){ - the_ff->initForceField( EXPLICIT_MIXING_RULE ); + n_components = globals->getNComponents(); + + + // get the forceField + + strcpy(force_field, globals->getForceField()); + + if (!strcasecmp(force_field, "DUFF")){ + ffCase = FF_DUFF; } + else if (!strcasecmp(force_field, "LJ")){ + ffCase = FF_LJ; + } + else if (!strcasecmp(force_field, "EAM")){ + ffCase = FF_EAM; + } + else if (!strcasecmp(force_field, "WATER")){ + ffCase = FF_H2O; + } else{ - sprintf( painCave.errMsg, - "SimSetup Error: unknown mixing rule -> \"%s\"\n", - simnfo->mixingRule ); - painCave.isFatal = 1; - simError(); + sprintf(painCave.errMsg, "SimSetup Error. Unrecognized force field -> %s\n", + force_field); + painCave.isFatal = 1; + simError(); } + // get the ensemble -#ifdef IS_MPI - strcpy( checkPointMsg, - "Successfully intialized the mixingRule for Fortran." ); - MPIcheckPoint(); -#endif // is_mpi -} + strcpy(ensemble, globals->getEnsemble()); + if (!strcasecmp(ensemble, "NVE")){ + ensembleCase = NVE_ENS; + } + else if (!strcasecmp(ensemble, "NVT")){ + ensembleCase = NVT_ENS; + } + else if (!strcasecmp(ensemble, "NPTi") || !strcasecmp(ensemble, "NPT")){ + ensembleCase = NPTi_ENS; + } + else if (!strcasecmp(ensemble, "NPTf")){ + ensembleCase = NPTf_ENS; + } + else if (!strcasecmp(ensemble, "NPTxyz")){ + ensembleCase = NPTxyz_ENS; + } + else{ + sprintf(painCave.errMsg, + "SimSetup Warning. Unrecognized Ensemble -> %s \n" + "\treverting to NVE for this simulation.\n", + ensemble); + painCave.isFatal = 0; + simError(); + strcpy(ensemble, "NVE"); + ensembleCase = NVE_ENS; + } -void SimSetup::makeMolecules( void ){ + for (i = 0; i < nInfo; i++){ + strcpy(info[i].ensemble, ensemble); - int i, j, exI, exJ, tempEx, stampID, atomOffset, excludeOffset; - molInit info; - DirectionalAtom* dAtom; - LinkedAssign* extras; - LinkedAssign* current_extra; - AtomStamp* currentAtom; - BondStamp* currentBond; - BendStamp* currentBend; - TorsionStamp* currentTorsion; + // get the mixing rule - bond_pair* theBonds; - bend_set* theBends; - torsion_set* theTorsions; + strcpy(info[i].mixingRule, globals->getMixingRule()); + info[i].usePBC = globals->getPBC(); + } - - //init the forceField paramters + // get the components and calculate the tot_nMol and indvidual n_mol - the_ff->readParams(); + the_components = globals->getComponents(); + components_nmol = new int[n_components]; - - // init the atoms - double ux, uy, uz, u, uSqr; - - atomOffset = 0; - excludeOffset = 0; - for(i=0; in_mol; i++){ - - stampID = the_molecules[i].getStampID(); + if (!globals->haveNMol()){ + // we don't have the total number of molecules, so we assume it is + // given in each component - info.nAtoms = comp_stamps[stampID]->getNAtoms(); - info.nBonds = comp_stamps[stampID]->getNBonds(); - info.nBends = comp_stamps[stampID]->getNBends(); - info.nTorsions = comp_stamps[stampID]->getNTorsions(); - info.nExcludes = info.nBonds + info.nBends + info.nTorsions; + tot_nmol = 0; + for (i = 0; i < n_components; i++){ + if (!the_components[i]->haveNMol()){ + // we have a problem + sprintf(painCave.errMsg, + "SimSetup Error. No global NMol or component NMol given.\n" + "\tCannot calculate the number of atoms.\n"); + painCave.isFatal = 1; + simError(); + } - info.myAtoms = &the_atoms[atomOffset]; - info.myExcludes = &the_excludes[excludeOffset]; - info.myBonds = new Bond*[info.nBonds]; - info.myBends = new Bend*[info.nBends]; - info.myTorsions = new Torsion*[info.nTorsions]; + tot_nmol += the_components[i]->getNMol(); + components_nmol[i] = the_components[i]->getNMol(); + } + } + else{ + sprintf(painCave.errMsg, + "SimSetup error.\n" + "\tSorry, the ability to specify total" + " nMols and then give molfractions in the components\n" + "\tis not currently supported." + " Please give nMol in the components.\n"); + painCave.isFatal = 1; + simError(); + } - theBonds = new bond_pair[info.nBonds]; - theBends = new bend_set[info.nBends]; - theTorsions = new torsion_set[info.nTorsions]; - - // make the Atoms - - for(j=0; jgetAtom( j ); - if( currentAtom->haveOrientation() ){ - - dAtom = new DirectionalAtom(j + atomOffset); - simnfo->n_oriented++; - info.myAtoms[j] = dAtom; - - ux = currentAtom->getOrntX(); - uy = currentAtom->getOrntY(); - uz = currentAtom->getOrntZ(); - - uSqr = (ux * ux) + (uy * uy) + (uz * uz); - - u = sqrt( uSqr ); - ux = ux / u; - uy = uy / u; - uz = uz / u; - - dAtom->setSUx( ux ); - dAtom->setSUy( uy ); - dAtom->setSUz( uz ); - } - else{ - info.myAtoms[j] = new GeneralAtom(j + atomOffset); - } - info.myAtoms[j]->setType( currentAtom->getType() ); + //check whether sample time, status time, thermal time and reset time are divisble by dt + if (globals->haveSampleTime() && !isDivisible(globals->getSampleTime(), globals->getDt())){ + sprintf(painCave.errMsg, + "Sample time is not divisible by dt.\n" + "\tThis will result in samples that are not uniformly\n" + "\tdistributed in time. If this is a problem, change\n" + "\tyour sampleTime variable.\n"); + painCave.isFatal = 0; + simError(); + } + + if (globals->haveStatusTime() && !isDivisible(globals->getStatusTime(), globals->getDt())){ + sprintf(painCave.errMsg, + "Status time is not divisible by dt.\n" + "\tThis will result in status reports that are not uniformly\n" + "\tdistributed in time. If this is a problem, change \n" + "\tyour statusTime variable.\n"); + painCave.isFatal = 0; + simError(); + } + + if (globals->haveThermalTime() && !isDivisible(globals->getThermalTime(), globals->getDt())){ + sprintf(painCave.errMsg, + "Thermal time is not divisible by dt.\n" + "\tThis will result in thermalizations that are not uniformly\n" + "\tdistributed in time. If this is a problem, change \n" + "\tyour thermalTime variable.\n"); + painCave.isFatal = 0; + simError(); + } + + if (globals->haveResetTime() && !isDivisible(globals->getResetTime(), globals->getDt())){ + sprintf(painCave.errMsg, + "Reset time is not divisible by dt.\n" + "\tThis will result in integrator resets that are not uniformly\n" + "\tdistributed in time. If this is a problem, change\n" + "\tyour resetTime variable.\n"); + painCave.isFatal = 0; + simError(); + } + + // set the status, sample, and thermal kick times + + for (i = 0; i < nInfo; i++){ + if (globals->haveSampleTime()){ + info[i].sampleTime = globals->getSampleTime(); + info[i].statusTime = info[i].sampleTime; + } + else{ + info[i].sampleTime = globals->getRunTime(); + info[i].statusTime = info[i].sampleTime; + } + + if (globals->haveStatusTime()){ + info[i].statusTime = globals->getStatusTime(); + } + + if (globals->haveThermalTime()){ + info[i].thermalTime = globals->getThermalTime(); + } else { + info[i].thermalTime = globals->getRunTime(); + } + + info[i].resetIntegrator = 0; + if( globals->haveResetTime() ){ + info[i].resetTime = globals->getResetTime(); + info[i].resetIntegrator = 1; + } + + // check for the temperature set flag -#ifdef IS_MPI - - info.myAtoms[j]->setGlobalIndex( globalIndex[j+atomOffset] ); - -#endif // is_mpi - } + if (globals->haveTempSet()) + info[i].setTemp = globals->getTempSet(); + + // check for the extended State init + + info[i].useInitXSstate = globals->getUseInitXSstate(); + info[i].orthoTolerance = globals->getOrthoBoxTolerance(); - // make the bonds - for(j=0; jgetBond( j ); - theBonds[j].a = currentBond->getA() + atomOffset; - theBonds[j].b = currentBond->getB() + atomOffset; + } + + //setup seed for random number generator + int seedValue; - exI = theBonds[j].a; - exJ = theBonds[j].b; + if (globals->haveSeed()){ + seedValue = globals->getSeed(); - // exclude_I must always be the smaller of the pair - if( exI > exJ ){ - tempEx = exI; - exI = exJ; - exJ = tempEx; + if(seedValue / 1E9 == 0){ + sprintf(painCave.errMsg, + "Seed for sprng library should contain at least 9 digits\n" + "OOPSE will generate a seed for user\n"); + painCave.isFatal = 0; + simError(); + + //using seed generated by system instead of invalid seed set by user +#ifndef IS_MPI + seedValue = make_sprng_seed(); +#else + if (worldRank == 0){ + seedValue = make_sprng_seed(); } + MPI_Bcast(&seedValue, 1, MPI_INT, 0, MPI_COMM_WORLD); +#endif + } + }//end of if branch of globals->haveSeed() + else{ + +#ifndef IS_MPI + seedValue = make_sprng_seed(); +#else + if (worldRank == 0){ + seedValue = make_sprng_seed(); + } + MPI_Bcast(&seedValue, 1, MPI_INT, 0, MPI_COMM_WORLD); +#endif + }//end of globals->haveSeed() + + for (int i = 0; i < nInfo; i++){ + info[i].setSeed(seedValue); + } + #ifdef IS_MPI - tempEx = exI; - exI = the_atoms[tempEx]->getGlobalIndex() + 1; - tempEx = exJ; - exJ = the_atoms[tempEx]->getGlobalIndex() + 1; + strcpy(checkPointMsg, "Successfully gathered all information from Bass\n"); + MPIcheckPoint(); +#endif // is_mpi +} + + +void SimSetup::finalInfoCheck(void){ + int index; + int usesDipoles; + int usesCharges; + int i; + + for (i = 0; i < nInfo; i++){ + // check electrostatic parameters + + index = 0; + usesDipoles = 0; + while ((index < info[i].n_atoms) && !usesDipoles){ + usesDipoles = (info[i].atoms[index])->hasDipole(); + index++; + } + index = 0; + usesCharges = 0; + while ((index < info[i].n_atoms) && !usesCharges){ + usesCharges= (info[i].atoms[index])->hasCharge(); + index++; + } +#ifdef IS_MPI + int myUse = usesDipoles; + MPI_Allreduce(&myUse, &usesDipoles, 1, MPI_INT, MPI_LOR, MPI_COMM_WORLD); +#endif //is_mpi + + double theRcut, theRsw; + + if (globals->haveRcut()) { + theRcut = globals->getRcut(); + + if (globals->haveRsw()) + theRsw = globals->getRsw(); + else + theRsw = theRcut; - the_excludes[j+excludeOffset]->setPair( exI, exJ ); -#else // isn't MPI + info[i].setDefaultRcut(theRcut, theRsw); - the_excludes[j+excludeOffset]->setPair( (exI+1), (exJ+1) ); -#endif //is_mpi + } else { + + the_ff->calcRcut(); + theRcut = info[i].getRcut(); + + if (globals->haveRsw()) + theRsw = globals->getRsw(); + else + theRsw = theRcut; + + info[i].setDefaultRcut(theRcut, theRsw); } - excludeOffset += info.nBonds; - //make the bends - for(j=0; jgetUseRF()){ + info[i].useReactionField = 1; - currentBend = comp_stamps[stampID]->getBend( j ); - theBends[j].a = currentBend->getA() + atomOffset; - theBends[j].b = currentBend->getB() + atomOffset; - theBends[j].c = currentBend->getC() + atomOffset; - - if( currentBend->haveExtras() ){ - - extras = currentBend->getExtras(); - current_extra = extras; - - while( current_extra != NULL ){ - if( !strcmp( current_extra->getlhs(), "ghostVectorSource" )){ - - switch( current_extra->getType() ){ - - case 0: - theBends[j].ghost = - current_extra->getInt() + atomOffset; - theBends[j].isGhost = 1; - break; - - case 1: - theBends[j].ghost = - (int)current_extra->getDouble() + atomOffset; - theBends[j].isGhost = 1; - break; - - default: - sprintf( painCave.errMsg, - "SimSetup Error: ghostVectorSource was neither a " - "double nor an int.\n" - "-->Bend[%d] in %s\n", - j, comp_stamps[stampID]->getID() ); - painCave.isFatal = 1; - simError(); - } - } - - else{ - - sprintf( painCave.errMsg, - "SimSetup Error: unhandled bend assignment:\n" - " -->%s in Bend[%d] in %s\n", - current_extra->getlhs(), - j, comp_stamps[stampID]->getID() ); - painCave.isFatal = 1; - simError(); - } - - current_extra = current_extra->getNext(); - } + if (!globals->haveRcut()){ + sprintf(painCave.errMsg, + "SimSetup Warning: No value was set for the cutoffRadius.\n" + "\tOOPSE will use a default value of 15.0 angstroms" + "\tfor the cutoffRadius.\n"); + painCave.isFatal = 0; + simError(); + theRcut = 15.0; } - - if( !theBends[j].isGhost ){ - - exI = theBends[j].a; - exJ = theBends[j].c; + else{ + theRcut = globals->getRcut(); } + + if (!globals->haveRsw()){ + sprintf(painCave.errMsg, + "SimSetup Warning: No value was set for switchingRadius.\n" + "\tOOPSE will use a default value of\n" + "\t0.95 * cutoffRadius for the switchingRadius\n"); + painCave.isFatal = 0; + simError(); + theRsw = 0.95 * theRcut; + } else{ - - exI = theBends[j].a; - exJ = theBends[j].b; + theRsw = globals->getRsw(); } - - // exclude_I must always be the smaller of the pair - if( exI > exJ ){ - tempEx = exI; - exI = exJ; - exJ = tempEx; + + info[i].setDefaultRcut(theRcut, theRsw); + + if (!globals->haveDielectric()){ + sprintf(painCave.errMsg, + "SimSetup Error: No Dielectric constant was set.\n" + "\tYou are trying to use Reaction Field without" + "\tsetting a dielectric constant!\n"); + painCave.isFatal = 1; + simError(); } -#ifdef IS_MPI - tempEx = exI; - exI = the_atoms[tempEx]->getGlobalIndex() + 1; - tempEx = exJ; - exJ = the_atoms[tempEx]->getGlobalIndex() + 1; - - the_excludes[j+excludeOffset]->setPair( exI, exJ ); -#else // isn't MPI - the_excludes[j+excludeOffset]->setPair( (exI+1), (exJ+1) ); -#endif //is_mpi + info[i].dielectric = globals->getDielectric(); } - excludeOffset += info.nBends; + else{ + if (usesDipoles || usesCharges){ - for(j=0; jgetTorsion( j ); - theTorsions[j].a = currentTorsion->getA() + atomOffset; - theTorsions[j].b = currentTorsion->getB() + atomOffset; - theTorsions[j].c = currentTorsion->getC() + atomOffset; - theTorsions[j].d = currentTorsion->getD() + atomOffset; - - exI = theTorsions[j].a; - exJ = theTorsions[j].d; - - // exclude_I must always be the smaller of the pair - if( exI > exJ ){ - tempEx = exI; - exI = exJ; - exJ = tempEx; + if (!globals->haveRcut()){ + sprintf(painCave.errMsg, + "SimSetup Warning: No value was set for the cutoffRadius.\n" + "\tOOPSE will use a default value of 15.0 angstroms" + "\tfor the cutoffRadius.\n"); + painCave.isFatal = 0; + simError(); + theRcut = 15.0; } -#ifdef IS_MPI - tempEx = exI; - exI = the_atoms[tempEx]->getGlobalIndex() + 1; - tempEx = exJ; - exJ = the_atoms[tempEx]->getGlobalIndex() + 1; - - the_excludes[j+excludeOffset]->setPair( exI, exJ ); -#else // isn't MPI - the_excludes[j+excludeOffset]->setPair( (exI+1), (exJ+1) ); -#endif //is_mpi + else{ + theRcut = globals->getRcut(); + } + + if (!globals->haveRsw()){ + sprintf(painCave.errMsg, + "SimSetup Warning: No value was set for switchingRadius.\n" + "\tOOPSE will use a default value of\n" + "\t0.95 * cutoffRadius for the switchingRadius\n"); + painCave.isFatal = 0; + simError(); + theRsw = 0.95 * theRcut; + } + else{ + theRsw = globals->getRsw(); + } + + info[i].setDefaultRcut(theRcut, theRsw); + + } } - excludeOffset += info.nTorsions; + } +#ifdef IS_MPI + strcpy(checkPointMsg, "post processing checks out"); + MPIcheckPoint(); +#endif // is_mpi - - // send the arrays off to the forceField for init. + // clean up the forcefield + the_ff->cleanMe(); +} + +void SimSetup::initSystemCoords(void){ + int i; - the_ff->initializeAtoms( info.nAtoms, info.myAtoms ); - the_ff->initializeBonds( info.nBonds, info.myBonds, theBonds ); - the_ff->initializeBends( info.nBends, info.myBends, theBends ); - the_ff->initializeTorsions( info.nTorsions, info.myTorsions, theTorsions ); + char* inName; + (info[0].getConfiguration())->createArrays(info[0].n_atoms); - the_molecules[i].initialize( info ); + for (i = 0; i < info[0].n_atoms; i++) + info[0].atoms[i]->setCoords(); + if (globals->haveInitialConfig()){ + InitializeFromFile* fileInit; +#ifdef IS_MPI // is_mpi + if (worldRank == 0){ +#endif //is_mpi + inName = globals->getInitialConfig(); + fileInit = new InitializeFromFile(inName); +#ifdef IS_MPI + } + else + fileInit = new InitializeFromFile(NULL); +#endif + fileInit->readInit(info); // default velocities on - atomOffset += info.nAtoms; - delete[] theBonds; - delete[] theBends; - delete[] theTorsions; + delete fileInit; } + else{ + + // no init from bass + + sprintf(painCave.errMsg, + "Cannot intialize a simulation without an initial configuration file.\n"); + painCave.isFatal = 1;; + simError(); + + } #ifdef IS_MPI - sprintf( checkPointMsg, "all molecules initialized succesfully" ); + strcpy(checkPointMsg, "Successfully read in the initial configuration"); MPIcheckPoint(); #endif // is_mpi - - // clean up the forcefield - the_ff->calcRcut(); - the_ff->cleanMe(); - } -void SimSetup::initFromBass( void ){ - int i, j, k; - int n_cells; - double cellx, celly, cellz; - double temp1, temp2, temp3; - int n_per_extra; - int n_extra; - int have_extra, done; +void SimSetup::makeOutNames(void){ + int k; - temp1 = (double)tot_nmol / 4.0; - temp2 = pow( temp1, ( 1.0 / 3.0 ) ); - temp3 = ceil( temp2 ); - have_extra =0; - if( temp2 < temp3 ){ // we have a non-complete lattice - have_extra =1; - - n_cells = (int)temp3 - 1; - cellx = simnfo->boxLx / temp3; - celly = simnfo->boxLy / temp3; - cellz = simnfo->boxLz / temp3; - n_extra = tot_nmol - ( 4 * n_cells * n_cells * n_cells ); - temp1 = ((double)n_extra) / ( pow( temp3, 3.0 ) - pow( n_cells, 3.0 ) ); - n_per_extra = (int)ceil( temp1 ); + for (k = 0; k < nInfo; k++){ +#ifdef IS_MPI + if (worldRank == 0){ +#endif // is_mpi - if( n_per_extra > 4){ - sprintf( painCave.errMsg, - "SimSetup error. There has been an error in constructing" - " the non-complete lattice.\n" ); - painCave.isFatal = 1; - simError(); + if (globals->haveFinalConfig()){ + strcpy(info[k].finalName, globals->getFinalConfig()); + } + else{ + strcpy(info[k].finalName, inFileName); + char* endTest; + int nameLength = strlen(info[k].finalName); + endTest = &(info[k].finalName[nameLength - 5]); + if (!strcmp(endTest, ".bass")){ + strcpy(endTest, ".eor"); + } + else if (!strcmp(endTest, ".BASS")){ + strcpy(endTest, ".eor"); + } + else{ + endTest = &(info[k].finalName[nameLength - 4]); + if (!strcmp(endTest, ".bss")){ + strcpy(endTest, ".eor"); + } + else if (!strcmp(endTest, ".mdl")){ + strcpy(endTest, ".eor"); + } + else{ + strcat(info[k].finalName, ".eor"); + } + } + } + + // make the sample and status out names + + strcpy(info[k].sampleName, inFileName); + char* endTest; + int nameLength = strlen(info[k].sampleName); + endTest = &(info[k].sampleName[nameLength - 5]); + if (!strcmp(endTest, ".bass")){ + strcpy(endTest, ".dump"); + } + else if (!strcmp(endTest, ".BASS")){ + strcpy(endTest, ".dump"); + } + else{ + endTest = &(info[k].sampleName[nameLength - 4]); + if (!strcmp(endTest, ".bss")){ + strcpy(endTest, ".dump"); + } + else if (!strcmp(endTest, ".mdl")){ + strcpy(endTest, ".dump"); + } + else{ + strcat(info[k].sampleName, ".dump"); + } + } + + strcpy(info[k].statusName, inFileName); + nameLength = strlen(info[k].statusName); + endTest = &(info[k].statusName[nameLength - 5]); + if (!strcmp(endTest, ".bass")){ + strcpy(endTest, ".stat"); + } + else if (!strcmp(endTest, ".BASS")){ + strcpy(endTest, ".stat"); + } + else{ + endTest = &(info[k].statusName[nameLength - 4]); + if (!strcmp(endTest, ".bss")){ + strcpy(endTest, ".stat"); + } + else if (!strcmp(endTest, ".mdl")){ + strcpy(endTest, ".stat"); + } + else{ + strcat(info[k].statusName, ".stat"); + } + } + +#ifdef IS_MPI + } +#endif // is_mpi } - else{ - n_cells = (int)temp3; - cellx = simnfo->boxLx / temp3; - celly = simnfo->boxLy / temp3; - cellz = simnfo->boxLz / temp3; - } +} - current_mol = 0; - current_comp_mol = 0; - current_comp = 0; - current_atom_ndx = 0; - for( i=0; i < n_cells ; i++ ){ - for( j=0; j < n_cells; j++ ){ - for( k=0; k < n_cells; k++ ){ +void SimSetup::sysObjectsCreation(void){ + int i, k; - makeElement( i * cellx, - j * celly, - k * cellz ); + // create the forceField - makeElement( i * cellx + 0.5 * cellx, - j * celly + 0.5 * celly, - k * cellz ); + createFF(); - makeElement( i * cellx, - j * celly + 0.5 * celly, - k * cellz + 0.5 * cellz ); + // extract componentList - makeElement( i * cellx + 0.5 * cellx, - j * celly, - k * cellz + 0.5 * cellz ); - } - } - } + compList(); - if( have_extra ){ - done = 0; + // calc the number of atoms, bond, bends, and torsions - int start_ndx; - for( i=0; i < (n_cells+1) && !done; i++ ){ - for( j=0; j < (n_cells+1) && !done; j++ ){ + calcSysValues(); - if( i < n_cells ){ +#ifdef IS_MPI + // divide the molecules among the processors - if( j < n_cells ){ - start_ndx = n_cells; - } - else start_ndx = 0; - } - else start_ndx = 0; + mpiMolDivide(); +#endif //is_mpi - for( k=start_ndx; k < (n_cells+1) && !done; k++ ){ + // create the atom and SRI arrays. Also initialize Molecule Stamp ID's - makeElement( i * cellx, - j * celly, - k * cellz ); - done = ( current_mol >= tot_nmol ); + makeSysArrays(); - if( !done && n_per_extra > 1 ){ - makeElement( i * cellx + 0.5 * cellx, - j * celly + 0.5 * celly, - k * cellz ); - done = ( current_mol >= tot_nmol ); - } + // make and initialize the molecules (all but atomic coordinates) - if( !done && n_per_extra > 2){ - makeElement( i * cellx, - j * celly + 0.5 * celly, - k * cellz + 0.5 * cellz ); - done = ( current_mol >= tot_nmol ); - } + makeMolecules(); - if( !done && n_per_extra > 3){ - makeElement( i * cellx + 0.5 * cellx, - j * celly, - k * cellz + 0.5 * cellz ); - done = ( current_mol >= tot_nmol ); - } - } - } + for (k = 0; k < nInfo; k++){ + info[k].identArray = new int[info[k].n_atoms]; + for (i = 0; i < info[k].n_atoms; i++){ + info[k].identArray[i] = info[k].atoms[i]->getIdent(); } } +} - for( i=0; in_atoms; i++ ){ - simnfo->atoms[i]->set_vx( 0.0 ); - simnfo->atoms[i]->set_vy( 0.0 ); - simnfo->atoms[i]->set_vz( 0.0 ); - } -} +void SimSetup::createFF(void){ + switch (ffCase){ + case FF_DUFF: + the_ff = new DUFF(); + break; -void SimSetup::makeElement( double x, double y, double z ){ + case FF_LJ: + the_ff = new LJFF(); + break; - int k; - AtomStamp* current_atom; - DirectionalAtom* dAtom; - double rotMat[3][3]; + case FF_EAM: + the_ff = new EAM_FF(); + break; - for( k=0; kgetNAtoms(); k++ ){ + case FF_H2O: + the_ff = new WATER(); + break; - current_atom = comp_stamps[current_comp]->getAtom( k ); - if( !current_atom->havePosition() ){ - sprintf( painCave.errMsg, - "SimSetup:initFromBass error.\n" - "\tComponent %s, atom %s does not have a position specified.\n" - "\tThe initialization routine is unable to give a start" - " position.\n", - comp_stamps[current_comp]->getID(), - current_atom->getType() ); + default: + sprintf(painCave.errMsg, + "SimSetup Error. Unrecognized force field in case statement.\n"); painCave.isFatal = 1; simError(); - } + } - the_atoms[current_atom_ndx]->setX( x + current_atom->getPosX() ); - the_atoms[current_atom_ndx]->setY( y + current_atom->getPosY() ); - the_atoms[current_atom_ndx]->setZ( z + current_atom->getPosZ() ); +#ifdef IS_MPI + strcpy(checkPointMsg, "ForceField creation successful"); + MPIcheckPoint(); +#endif // is_mpi +} - if( the_atoms[current_atom_ndx]->isDirectional() ){ - dAtom = (DirectionalAtom *)the_atoms[current_atom_ndx]; +void SimSetup::compList(void){ + int i; + char* id; + LinkedMolStamp* headStamp = new LinkedMolStamp(); + LinkedMolStamp* currentStamp = NULL; + comp_stamps = new MoleculeStamp * [n_components]; + bool haveCutoffGroups; - rotMat[0][0] = 1.0; - rotMat[0][1] = 0.0; - rotMat[0][2] = 0.0; + haveCutoffGroups = false; + + // make an array of molecule stamps that match the components used. + // also extract the used stamps out into a separate linked list - rotMat[1][0] = 0.0; - rotMat[1][1] = 1.0; - rotMat[1][2] = 0.0; + for (i = 0; i < nInfo; i++){ + info[i].nComponents = n_components; + info[i].componentsNmol = components_nmol; + info[i].compStamps = comp_stamps; + info[i].headStamp = headStamp; + } - rotMat[2][0] = 0.0; - rotMat[2][1] = 0.0; - rotMat[2][2] = 1.0; - dAtom->setA( rotMat ); + for (i = 0; i < n_components; i++){ + id = the_components[i]->getType(); + comp_stamps[i] = NULL; + + // check to make sure the component isn't already in the list + + comp_stamps[i] = headStamp->match(id); + if (comp_stamps[i] == NULL){ + // extract the component from the list; + + currentStamp = stamps->extractMolStamp(id); + if (currentStamp == NULL){ + sprintf(painCave.errMsg, + "SimSetup error: Component \"%s\" was not found in the " + "list of declared molecules\n", + id); + painCave.isFatal = 1; + simError(); + } + + headStamp->add(currentStamp); + comp_stamps[i] = headStamp->match(id); } - current_atom_ndx++; + if(comp_stamps[i]->getNCutoffGroups() > 0) + haveCutoffGroups = true; + } + + for (i = 0; i < nInfo; i++) + info[i].haveCutoffGroups = haveCutoffGroups; + +#ifdef IS_MPI + strcpy(checkPointMsg, "Component stamps successfully extracted\n"); + MPIcheckPoint(); +#endif // is_mpi +} + +void SimSetup::calcSysValues(void){ + int i; + + int* molMembershipArray; + + tot_atoms = 0; + tot_bonds = 0; + tot_bends = 0; + tot_torsions = 0; + tot_rigid = 0; + for (i = 0; i < n_components; i++){ + tot_atoms += components_nmol[i] * comp_stamps[i]->getNAtoms(); + tot_bonds += components_nmol[i] * comp_stamps[i]->getNBonds(); + tot_bends += components_nmol[i] * comp_stamps[i]->getNBends(); + tot_torsions += components_nmol[i] * comp_stamps[i]->getNTorsions(); + tot_rigid += components_nmol[i] * comp_stamps[i]->getNRigidBodies(); + } + + tot_SRI = tot_bonds + tot_bends + tot_torsions; + molMembershipArray = new int[tot_atoms]; + + for (i = 0; i < nInfo; i++){ + info[i].n_atoms = tot_atoms; + info[i].n_bonds = tot_bonds; + info[i].n_bends = tot_bends; + info[i].n_torsions = tot_torsions; + info[i].n_SRI = tot_SRI; + info[i].n_mol = tot_nmol; + + info[i].molMembershipArray = molMembershipArray; + } +} + +#ifdef IS_MPI + +void SimSetup::mpiMolDivide(void){ + int i, j, k; + int localMol, allMol; + int local_atoms, local_bonds, local_bends, local_torsions, local_SRI; + int local_rigid; + vector globalMolIndex; + + mpiSim = new mpiSimulation(info); + + mpiSim->divideLabor(); + globalAtomIndex = mpiSim->getGlobalAtomIndex(); + //globalMolIndex = mpiSim->getGlobalMolIndex(); + + // set up the local variables + + mol2proc = mpiSim->getMolToProcMap(); + molCompType = mpiSim->getMolComponentType(); + + allMol = 0; + localMol = 0; + local_atoms = 0; + local_bonds = 0; + local_bends = 0; + local_torsions = 0; + local_rigid = 0; + globalAtomCounter = 0; + + for (i = 0; i < n_components; i++){ + for (j = 0; j < components_nmol[i]; j++){ + if (mol2proc[allMol] == worldRank){ + local_atoms += comp_stamps[i]->getNAtoms(); + local_bonds += comp_stamps[i]->getNBonds(); + local_bends += comp_stamps[i]->getNBends(); + local_torsions += comp_stamps[i]->getNTorsions(); + local_rigid += comp_stamps[i]->getNRigidBodies(); + localMol++; + } + for (k = 0; k < comp_stamps[i]->getNAtoms(); k++){ + info[0].molMembershipArray[globalAtomCounter] = allMol; + globalAtomCounter++; + } + + allMol++; + } + } + local_SRI = local_bonds + local_bends + local_torsions; + + info[0].n_atoms = mpiSim->getMyNlocal(); + + + if (local_atoms != info[0].n_atoms){ + sprintf(painCave.errMsg, + "SimSetup error: mpiSim's localAtom (%d) and SimSetup's\n" + "\tlocalAtom (%d) are not equal.\n", + info[0].n_atoms, local_atoms); + painCave.isFatal = 1; + simError(); + } + + info[0].n_bonds = local_bonds; + info[0].n_bends = local_bends; + info[0].n_torsions = local_torsions; + info[0].n_SRI = local_SRI; + info[0].n_mol = localMol; + + strcpy(checkPointMsg, "Passed nlocal consistency check."); + MPIcheckPoint(); +} + +#endif // is_mpi + + +void SimSetup::makeSysArrays(void){ + +#ifndef IS_MPI + int k, j; +#endif // is_mpi + int i, l; + + Atom** the_atoms; + Molecule* the_molecules; + + for (l = 0; l < nInfo; l++){ + // create the atom and short range interaction arrays + + the_atoms = new Atom * [info[l].n_atoms]; + the_molecules = new Molecule[info[l].n_mol]; + int molIndex; + + // initialize the molecule's stampID's + +#ifdef IS_MPI + + + molIndex = 0; + for (i = 0; i < mpiSim->getTotNmol(); i++){ + if (mol2proc[i] == worldRank){ + the_molecules[molIndex].setStampID(molCompType[i]); + the_molecules[molIndex].setMyIndex(molIndex); + the_molecules[molIndex].setGlobalIndex(i); + molIndex++; + } + } + +#else // is_mpi + + molIndex = 0; + globalAtomCounter = 0; + for (i = 0; i < n_components; i++){ + for (j = 0; j < components_nmol[i]; j++){ + the_molecules[molIndex].setStampID(i); + the_molecules[molIndex].setMyIndex(molIndex); + the_molecules[molIndex].setGlobalIndex(molIndex); + for (k = 0; k < comp_stamps[i]->getNAtoms(); k++){ + info[l].molMembershipArray[globalAtomCounter] = molIndex; + globalAtomCounter++; + } + molIndex++; + } + } + + +#endif // is_mpi + + info[l].globalExcludes = new int; + info[l].globalExcludes[0] = 0; + + // set the arrays into the SimInfo object + + info[l].atoms = the_atoms; + info[l].molecules = the_molecules; + info[l].nGlobalExcludes = 0; + + the_ff->setSimInfo(info); } +} - current_mol++; - current_comp_mol++; +void SimSetup::makeIntegrator(void){ + int k; - if( current_comp_mol >= components_nmol[current_comp] ){ + NVE* myNVE = NULL; + NVT* myNVT = NULL; + NPTi >* myNPTi = NULL; + NPTf >* myNPTf = NULL; + NPTxyz >* myNPTxyz = NULL; + + for (k = 0; k < nInfo; k++){ + switch (ensembleCase){ + case NVE_ENS: + if (globals->haveZconstraints()){ + setupZConstraint(info[k]); + myNVE = new ZConstraint >(&(info[k]), the_ff); + } + else{ + myNVE = new NVE(&(info[k]), the_ff); + } + + info->the_integrator = myNVE; + break; - current_comp_mol = 0; - current_comp++; + case NVT_ENS: + if (globals->haveZconstraints()){ + setupZConstraint(info[k]); + myNVT = new ZConstraint >(&(info[k]), the_ff); + } + else + myNVT = new NVT(&(info[k]), the_ff); + + myNVT->setTargetTemp(globals->getTargetTemp()); + + if (globals->haveTauThermostat()) + myNVT->setTauThermostat(globals->getTauThermostat()); + else{ + sprintf(painCave.errMsg, + "SimSetup error: If you use the NVT\n" + "\tensemble, you must set tauThermostat.\n"); + painCave.isFatal = 1; + simError(); + } + + info->the_integrator = myNVT; + break; + + case NPTi_ENS: + if (globals->haveZconstraints()){ + setupZConstraint(info[k]); + myNPTi = new ZConstraint > >(&(info[k]), the_ff); + } + else + myNPTi = new NPTi >(&(info[k]), the_ff); + + myNPTi->setTargetTemp(globals->getTargetTemp()); + + if (globals->haveTargetPressure()) + myNPTi->setTargetPressure(globals->getTargetPressure()); + else{ + sprintf(painCave.errMsg, + "SimSetup error: If you use a constant pressure\n" + "\tensemble, you must set targetPressure in the BASS file.\n"); + painCave.isFatal = 1; + simError(); + } + + if (globals->haveTauThermostat()) + myNPTi->setTauThermostat(globals->getTauThermostat()); + else{ + sprintf(painCave.errMsg, + "SimSetup error: If you use an NPT\n" + "\tensemble, you must set tauThermostat.\n"); + painCave.isFatal = 1; + simError(); + } + + if (globals->haveTauBarostat()) + myNPTi->setTauBarostat(globals->getTauBarostat()); + else{ + sprintf(painCave.errMsg, + "SimSetup error: If you use an NPT\n" + "\tensemble, you must set tauBarostat.\n"); + painCave.isFatal = 1; + simError(); + } + + info->the_integrator = myNPTi; + break; + + case NPTf_ENS: + if (globals->haveZconstraints()){ + setupZConstraint(info[k]); + myNPTf = new ZConstraint > >(&(info[k]), the_ff); + } + else + myNPTf = new NPTf >(&(info[k]), the_ff); + + myNPTf->setTargetTemp(globals->getTargetTemp()); + + if (globals->haveTargetPressure()) + myNPTf->setTargetPressure(globals->getTargetPressure()); + else{ + sprintf(painCave.errMsg, + "SimSetup error: If you use a constant pressure\n" + "\tensemble, you must set targetPressure in the BASS file.\n"); + painCave.isFatal = 1; + simError(); + } + + if (globals->haveTauThermostat()) + myNPTf->setTauThermostat(globals->getTauThermostat()); + + else{ + sprintf(painCave.errMsg, + "SimSetup error: If you use an NPT\n" + "\tensemble, you must set tauThermostat.\n"); + painCave.isFatal = 1; + simError(); + } + + if (globals->haveTauBarostat()) + myNPTf->setTauBarostat(globals->getTauBarostat()); + + else{ + sprintf(painCave.errMsg, + "SimSetup error: If you use an NPT\n" + "\tensemble, you must set tauBarostat.\n"); + painCave.isFatal = 1; + simError(); + } + + info->the_integrator = myNPTf; + break; + + case NPTxyz_ENS: + if (globals->haveZconstraints()){ + setupZConstraint(info[k]); + myNPTxyz = new ZConstraint > >(&(info[k]), the_ff); + } + else + myNPTxyz = new NPTxyz >(&(info[k]), the_ff); + + myNPTxyz->setTargetTemp(globals->getTargetTemp()); + + if (globals->haveTargetPressure()) + myNPTxyz->setTargetPressure(globals->getTargetPressure()); + else{ + sprintf(painCave.errMsg, + "SimSetup error: If you use a constant pressure\n" + "\tensemble, you must set targetPressure in the BASS file.\n"); + painCave.isFatal = 1; + simError(); + } + + if (globals->haveTauThermostat()) + myNPTxyz->setTauThermostat(globals->getTauThermostat()); + else{ + sprintf(painCave.errMsg, + "SimSetup error: If you use an NPT\n" + "\tensemble, you must set tauThermostat.\n"); + painCave.isFatal = 1; + simError(); + } + + if (globals->haveTauBarostat()) + myNPTxyz->setTauBarostat(globals->getTauBarostat()); + else{ + sprintf(painCave.errMsg, + "SimSetup error: If you use an NPT\n" + "\tensemble, you must set tauBarostat.\n"); + painCave.isFatal = 1; + simError(); + } + + info->the_integrator = myNPTxyz; + break; + + default: + sprintf(painCave.errMsg, + "SimSetup Error. Unrecognized ensemble in case statement.\n"); + painCave.isFatal = 1; + simError(); + } } } + +void SimSetup::initFortran(void){ + info[0].refreshSim(); + + if (!strcmp(info[0].mixingRule, "standard")){ + the_ff->initForceField(LB_MIXING_RULE); + } + else if (!strcmp(info[0].mixingRule, "explicit")){ + the_ff->initForceField(EXPLICIT_MIXING_RULE); + } + else{ + sprintf(painCave.errMsg, "SimSetup Error: unknown mixing rule -> \"%s\"\n", + info[0].mixingRule); + painCave.isFatal = 1; + simError(); + } + + +#ifdef IS_MPI + strcpy(checkPointMsg, "Successfully intialized the mixingRule for Fortran."); + MPIcheckPoint(); +#endif // is_mpi +} + +void SimSetup::setupZConstraint(SimInfo& theInfo){ + int nZConstraints; + ZconStamp** zconStamp; + + if (globals->haveZconstraintTime()){ + //add sample time of z-constraint into SimInfo's property list + DoubleData* zconsTimeProp = new DoubleData(); + zconsTimeProp->setID(ZCONSTIME_ID); + zconsTimeProp->setData(globals->getZconsTime()); + theInfo.addProperty(zconsTimeProp); + } + else{ + sprintf(painCave.errMsg, + "ZConstraint error: If you use a ZConstraint,\n" + "\tyou must set zconsTime.\n"); + painCave.isFatal = 1; + simError(); + } + + //push zconsTol into siminfo, if user does not specify + //value for zconsTol, a default value will be used + DoubleData* zconsTol = new DoubleData(); + zconsTol->setID(ZCONSTOL_ID); + if (globals->haveZconsTol()){ + zconsTol->setData(globals->getZconsTol()); + } + else{ + double defaultZConsTol = 0.01; + sprintf(painCave.errMsg, + "ZConstraint Warning: Tolerance for z-constraint method is not specified.\n" + "\tOOPSE will use a default value of %f.\n" + "\tTo set the tolerance, use the zconsTol variable.\n", + defaultZConsTol); + painCave.isFatal = 0; + simError(); + + zconsTol->setData(defaultZConsTol); + } + theInfo.addProperty(zconsTol); + + //set Force Subtraction Policy + StringData* zconsForcePolicy = new StringData(); + zconsForcePolicy->setID(ZCONSFORCEPOLICY_ID); + + if (globals->haveZconsForcePolicy()){ + zconsForcePolicy->setData(globals->getZconsForcePolicy()); + } + else{ + sprintf(painCave.errMsg, + "ZConstraint Warning: No force subtraction policy was set.\n" + "\tOOPSE will use PolicyByMass.\n" + "\tTo set the policy, use the zconsForcePolicy variable.\n"); + painCave.isFatal = 0; + simError(); + zconsForcePolicy->setData("BYMASS"); + } + + theInfo.addProperty(zconsForcePolicy); + + //set zcons gap + DoubleData* zconsGap = new DoubleData(); + zconsGap->setID(ZCONSGAP_ID); + + if (globals->haveZConsGap()){ + zconsGap->setData(globals->getZconsGap()); + theInfo.addProperty(zconsGap); + } + + //set zcons fixtime + DoubleData* zconsFixtime = new DoubleData(); + zconsFixtime->setID(ZCONSFIXTIME_ID); + + if (globals->haveZConsFixTime()){ + zconsFixtime->setData(globals->getZconsFixtime()); + theInfo.addProperty(zconsFixtime); + } + + //set zconsUsingSMD + IntData* zconsUsingSMD = new IntData(); + zconsUsingSMD->setID(ZCONSUSINGSMD_ID); + + if (globals->haveZConsUsingSMD()){ + zconsUsingSMD->setData(globals->getZconsUsingSMD()); + theInfo.addProperty(zconsUsingSMD); + } + + //Determine the name of ouput file and add it into SimInfo's property list + //Be careful, do not use inFileName, since it is a pointer which + //point to a string at master node, and slave nodes do not contain that string + + string zconsOutput(theInfo.finalName); + + zconsOutput = zconsOutput.substr(0, zconsOutput.rfind(".")) + ".fz"; + + StringData* zconsFilename = new StringData(); + zconsFilename->setID(ZCONSFILENAME_ID); + zconsFilename->setData(zconsOutput); + + theInfo.addProperty(zconsFilename); + + //setup index, pos and other parameters of z-constraint molecules + nZConstraints = globals->getNzConstraints(); + theInfo.nZconstraints = nZConstraints; + + zconStamp = globals->getZconStamp(); + ZConsParaItem tempParaItem; + + ZConsParaData* zconsParaData = new ZConsParaData(); + zconsParaData->setID(ZCONSPARADATA_ID); + + for (int i = 0; i < nZConstraints; i++){ + tempParaItem.havingZPos = zconStamp[i]->haveZpos(); + tempParaItem.zPos = zconStamp[i]->getZpos(); + tempParaItem.zconsIndex = zconStamp[i]->getMolIndex(); + tempParaItem.kRatio = zconStamp[i]->getKratio(); + tempParaItem.havingCantVel = zconStamp[i]->haveCantVel(); + tempParaItem.cantVel = zconStamp[i]->getCantVel(); + zconsParaData->addItem(tempParaItem); + } + + //check the uniqueness of index + if(!zconsParaData->isIndexUnique()){ + sprintf(painCave.errMsg, + "ZConstraint Error: molIndex is not unique!\n"); + painCave.isFatal = 1; + simError(); + } + + //sort the parameters by index of molecules + zconsParaData->sortByIndex(); + + //push data into siminfo, therefore, we can retrieve later + theInfo.addProperty(zconsParaData); +} + +void SimSetup::makeMinimizer(){ + + OOPSEMinimizer* myOOPSEMinimizer; + MinimizerParameterSet* param; + char minimizerName[100]; + + for (int i = 0; i < nInfo; i++){ + + //prepare parameter set for minimizer + param = new MinimizerParameterSet(); + param->setDefaultParameter(); + + if (globals->haveMinimizer()){ + param->setFTol(globals->getMinFTol()); + } + + if (globals->haveMinGTol()){ + param->setGTol(globals->getMinGTol()); + } + + if (globals->haveMinMaxIter()){ + param->setMaxIteration(globals->getMinMaxIter()); + } + + if (globals->haveMinWriteFrq()){ + param->setMaxIteration(globals->getMinMaxIter()); + } + + if (globals->haveMinWriteFrq()){ + param->setWriteFrq(globals->getMinWriteFrq()); + } + + if (globals->haveMinStepSize()){ + param->setStepSize(globals->getMinStepSize()); + } + + if (globals->haveMinLSMaxIter()){ + param->setLineSearchMaxIteration(globals->getMinLSMaxIter()); + } + + if (globals->haveMinLSTol()){ + param->setLineSearchTol(globals->getMinLSTol()); + } + + strcpy(minimizerName, globals->getMinimizer()); + + if (!strcasecmp(minimizerName, "CG")){ + myOOPSEMinimizer = new PRCGMinimizer(&(info[i]), the_ff, param); + } + else if (!strcasecmp(minimizerName, "SD")){ + //myOOPSEMinimizer = MinimizerFactory.creatMinimizer("", &(info[i]), the_ff, param); + myOOPSEMinimizer = new SDMinimizer(&(info[i]), the_ff, param); + } + else{ + sprintf(painCave.errMsg, + "SimSetup error: Unrecognized Minimizer, use Conjugate Gradient \n"); + painCave.isFatal = 0; + simError(); + + myOOPSEMinimizer = new PRCGMinimizer(&(info[i]), the_ff, param); + } + info[i].the_integrator = myOOPSEMinimizer; + + //store the minimizer into simInfo + info[i].the_minimizer = myOOPSEMinimizer; + info[i].has_minimizer = true; + } + +}