--- trunk/OOPSE/libmdtools/SimInfo.cpp 2003/06/30 22:04:01 568 +++ trunk/OOPSE/libmdtools/SimInfo.cpp 2004/05/12 14:30:12 1163 @@ -1,7 +1,9 @@ -#include -#include -#include +#include +#include +#include +#include +using namespace std; #include "SimInfo.hpp" #define __C @@ -10,273 +12,365 @@ #include "fortranWrappers.hpp" +#include "MatVec3.h" + #ifdef IS_MPI #include "mpiSimulation.hpp" #endif +inline double roundMe( double x ){ + return ( x >= 0 ) ? floor( x + 0.5 ) : ceil( x - 0.5 ); +} + +inline double min( double a, double b ){ + return (a < b ) ? a : b; +} + SimInfo* currentInfo; SimInfo::SimInfo(){ - excludes = NULL; + n_constraints = 0; + nZconstraints = 0; n_oriented = 0; n_dipoles = 0; ndf = 0; ndfRaw = 0; + nZconstraints = 0; the_integrator = NULL; setTemp = 0; thermalTime = 0.0; + currentTime = 0.0; rCut = 0.0; + rSw = 0.0; + haveRcut = 0; + haveRsw = 0; + boxIsInit = 0; + + resetTime = 1e99; + + orthoRhombic = 0; + orthoTolerance = 1E-6; + useInitXSstate = true; + usePBC = 0; useLJ = 0; useSticky = 0; - useDipole = 0; + useCharges = 0; + useDipoles = 0; useReactionField = 0; useGB = 0; useEAM = 0; + + haveCutoffGroups = false; + excludes = Exclude::Instance(); + + myConfiguration = new SimState(); + + has_minimizer = false; + the_minimizer =NULL; + + ngroup = 0; + wrapMeSimInfo( this ); } -void SimInfo::setBox(double newBox[3]) { - double smallestBoxL, maxCutoff; - int status; - int i; +SimInfo::~SimInfo(){ - for(i=0; i<9; i++) Hmat[i] = 0.0;; + delete myConfiguration; - Hmat[0] = newBox[0]; - Hmat[4] = newBox[1]; - Hmat[8] = newBox[2]; + map::iterator i; + + for(i = properties.begin(); i != properties.end(); i++) + delete (*i).second; + +} - calcHmatI(); - calcBoxL(); +void SimInfo::setBox(double newBox[3]) { + + int i, j; + double tempMat[3][3]; - setFortranBoxSize(Hmat); + for(i=0; i<3; i++) + for (j=0; j<3; j++) tempMat[i][j] = 0.0;; - smallestBoxL = boxLx; - if (boxLy < smallestBoxL) smallestBoxL = boxLy; - if (boxLz < smallestBoxL) smallestBoxL = boxLz; + tempMat[0][0] = newBox[0]; + tempMat[1][1] = newBox[1]; + tempMat[2][2] = newBox[2]; - maxCutoff = smallestBoxL / 2.0; + setBoxM( tempMat ); - if (rList > maxCutoff) { - sprintf( painCave.errMsg, - "New Box size is forcing neighborlist radius down to %lf\n", - maxCutoff ); - painCave.isFatal = 0; - simError(); +} - rList = maxCutoff; +void SimInfo::setBoxM( double theBox[3][3] ){ + + int i, j; + double FortranHmat[9]; // to preserve compatibility with Fortran the + // ordering in the array is as follows: + // [ 0 3 6 ] + // [ 1 4 7 ] + // [ 2 5 8 ] + double FortranHmatInv[9]; // the inverted Hmat (for Fortran); - sprintf( painCave.errMsg, - "New Box size is forcing cutoff radius down to %lf\n", - maxCutoff - 1.0 ); - painCave.isFatal = 0; - simError(); + if( !boxIsInit ) boxIsInit = 1; - rCut = rList - 1.0; + for(i=0; i < 3; i++) + for (j=0; j < 3; j++) Hmat[i][j] = theBox[i][j]; + + calcBoxL(); + calcHmatInv(); - // list radius changed so we have to refresh the simulation structure. - refreshSim(); + for(i=0; i < 3; i++) { + for (j=0; j < 3; j++) { + FortranHmat[3*j + i] = Hmat[i][j]; + FortranHmatInv[3*j + i] = HmatInv[i][j]; + } } - if (rCut > maxCutoff) { - sprintf( painCave.errMsg, - "New Box size is forcing cutoff radius down to %lf\n", - maxCutoff ); - painCave.isFatal = 0; - simError(); + setFortranBoxSize(FortranHmat, FortranHmatInv, &orthoRhombic); + +} + - status = 0; - LJ_new_rcut(&rCut, &status); - if (status != 0) { - sprintf( painCave.errMsg, - "Error in recomputing LJ shifts based on new rcut\n"); - painCave.isFatal = 1; - simError(); - } - } +void SimInfo::getBoxM (double theBox[3][3]) { + + int i, j; + for(i=0; i<3; i++) + for (j=0; j<3; j++) theBox[i][j] = Hmat[i][j]; } -void SimInfo::setBoxM( double theBox[9] ){ - - int i, status; - double smallestBoxL, maxCutoff; - for(i=0; i<9; i++) Hmat[i] = theBox[i]; - calcHmatI(); - calcBoxL(); +void SimInfo::scaleBox(double scale) { + double theBox[3][3]; + int i, j; - setFortranBoxSize(Hmat); - - smallestBoxL = boxLx; - if (boxLy < smallestBoxL) smallestBoxL = boxLy; - if (boxLz < smallestBoxL) smallestBoxL = boxLz; + // cerr << "Scaling box by " << scale << "\n"; - maxCutoff = smallestBoxL / 2.0; + for(i=0; i<3; i++) + for (j=0; j<3; j++) theBox[i][j] = Hmat[i][j]*scale; - if (rList > maxCutoff) { - sprintf( painCave.errMsg, - "New Box size is forcing neighborlist radius down to %lf\n", - maxCutoff ); - painCave.isFatal = 0; - simError(); + setBoxM(theBox); - rList = maxCutoff; +} - sprintf( painCave.errMsg, - "New Box size is forcing cutoff radius down to %lf\n", - maxCutoff - 1.0 ); - painCave.isFatal = 0; - simError(); +void SimInfo::calcHmatInv( void ) { + + int oldOrtho; + int i,j; + double smallDiag; + double tol; + double sanity[3][3]; - rCut = rList - 1.0; + invertMat3( Hmat, HmatInv ); - // list radius changed so we have to refresh the simulation structure. - refreshSim(); - } + // check to see if Hmat is orthorhombic + + oldOrtho = orthoRhombic; - if (rCut > maxCutoff) { - sprintf( painCave.errMsg, - "New Box size is forcing cutoff radius down to %lf\n", - maxCutoff ); - painCave.isFatal = 0; - simError(); + smallDiag = fabs(Hmat[0][0]); + if(smallDiag > fabs(Hmat[1][1])) smallDiag = fabs(Hmat[1][1]); + if(smallDiag > fabs(Hmat[2][2])) smallDiag = fabs(Hmat[2][2]); + tol = smallDiag * orthoTolerance; - status = 0; - LJ_new_rcut(&rCut, &status); - if (status != 0) { - sprintf( painCave.errMsg, - "Error in recomputing LJ shifts based on new rcut\n"); - painCave.isFatal = 1; - simError(); + orthoRhombic = 1; + + for (i = 0; i < 3; i++ ) { + for (j = 0 ; j < 3; j++) { + if (i != j) { + if (orthoRhombic) { + if ( fabs(Hmat[i][j]) >= tol) orthoRhombic = 0; + } + } } } -} - -void SimInfo::getBox(double theBox[9]) { - - int i; - for(i=0; i<9; i++) theBox[i] = Hmat[i]; -} - - -void SimInfo::calcHmatI( void ) { - - double C[3][3]; - double detHmat; - int i, j, k; - - // calculate the adjunct of Hmat; - - C[0][0] = ( Hmat[4]*Hmat[8]) - (Hmat[7]*Hmat[5]); - C[1][0] = -( Hmat[1]*Hmat[8]) + (Hmat[7]*Hmat[2]); - C[2][0] = ( Hmat[1]*Hmat[5]) - (Hmat[4]*Hmat[2]); - - C[0][1] = -( Hmat[3]*Hmat[8]) + (Hmat[6]*Hmat[5]); - C[1][1] = ( Hmat[0]*Hmat[8]) - (Hmat[6]*Hmat[2]); - C[2][1] = -( Hmat[0]*Hmat[5]) + (Hmat[3]*Hmat[2]); - - C[0][2] = ( Hmat[3]*Hmat[7]) - (Hmat[6]*Hmat[4]); - C[1][2] = -( Hmat[0]*Hmat[7]) + (Hmat[6]*Hmat[1]); - C[2][2] = ( Hmat[0]*Hmat[4]) - (Hmat[3]*Hmat[1]); - - // calcutlate the determinant of Hmat - - detHmat = 0.0; - for(i=0; i<3; i++) detHmat += Hmat[i] * C[i][0]; - - - // H^-1 = C^T / det(H) - - i=0; - for(j=0; j<3; j++){ - for(k=0; k<3; k++){ - - HmatI[i] = C[j][k] / detHmat; - i++; + if( oldOrtho != orthoRhombic ){ + + if( orthoRhombic ){ + sprintf( painCave.errMsg, + "OOPSE is switching from the default Non-Orthorhombic\n" + "\tto the faster Orthorhombic periodic boundary computations.\n" + "\tThis is usually a good thing, but if you wan't the\n" + "\tNon-Orthorhombic computations, make the orthoBoxTolerance\n" + "\tvariable ( currently set to %G ) smaller.\n", + orthoTolerance); + simError(); } + else { + sprintf( painCave.errMsg, + "OOPSE is switching from the faster Orthorhombic to the more\n" + "\tflexible Non-Orthorhombic periodic boundary computations.\n" + "\tThis is usually because the box has deformed under\n" + "\tNPTf integration. If you wan't to live on the edge with\n" + "\tthe Orthorhombic computations, make the orthoBoxTolerance\n" + "\tvariable ( currently set to %G ) larger.\n", + orthoTolerance); + simError(); + } } } void SimInfo::calcBoxL( void ){ double dx, dy, dz, dsq; - int i; - // boxVol = h1 (dot) h2 (cross) h3 + // boxVol = Determinant of Hmat - boxVol = Hmat[0] * ( (Hmat[4]*Hmat[8]) - (Hmat[7]*Hmat[5]) ) - + Hmat[1] * ( (Hmat[5]*Hmat[6]) - (Hmat[8]*Hmat[3]) ) - + Hmat[2] * ( (Hmat[3]*Hmat[7]) - (Hmat[6]*Hmat[4]) ); + boxVol = matDet3( Hmat ); - // boxLx - dx = Hmat[0]; dy = Hmat[1]; dz = Hmat[2]; + dx = Hmat[0][0]; dy = Hmat[1][0]; dz = Hmat[2][0]; dsq = dx*dx + dy*dy + dz*dz; - boxLx = sqrt( dsq ); + boxL[0] = sqrt( dsq ); + //maxCutoff = 0.5 * boxL[0]; // boxLy - dx = Hmat[3]; dy = Hmat[4]; dz = Hmat[5]; + dx = Hmat[0][1]; dy = Hmat[1][1]; dz = Hmat[2][1]; dsq = dx*dx + dy*dy + dz*dz; - boxLy = sqrt( dsq ); + boxL[1] = sqrt( dsq ); + //if( (0.5 * boxL[1]) < maxCutoff ) maxCutoff = 0.5 * boxL[1]; + // boxLz - dx = Hmat[6]; dy = Hmat[7]; dz = Hmat[8]; + dx = Hmat[0][2]; dy = Hmat[1][2]; dz = Hmat[2][2]; dsq = dx*dx + dy*dy + dz*dz; - boxLz = sqrt( dsq ); + boxL[2] = sqrt( dsq ); + //if( (0.5 * boxL[2]) < maxCutoff ) maxCutoff = 0.5 * boxL[2]; + + //calculate the max cutoff + maxCutoff = calcMaxCutOff(); + checkCutOffs(); + } -void SimInfo::wrapVector( double thePos[3] ){ +double SimInfo::calcMaxCutOff(){ - int i, j, k; - double scaled[3]; + double ri[3], rj[3], rk[3]; + double rij[3], rjk[3], rki[3]; + double minDist; - // calc the scaled coordinates. - - for(i=0; i<3; i++) - scaled[i] = thePos[0]*Hmat[i] + thePos[1]*Hat[i+3] + thePos[3]*Hmat[i+6]; + ri[0] = Hmat[0][0]; + ri[1] = Hmat[1][0]; + ri[2] = Hmat[2][0]; - // wrap the scaled coordinates + rj[0] = Hmat[0][1]; + rj[1] = Hmat[1][1]; + rj[2] = Hmat[2][1]; - for(i=0; i<3; i++) - scaled[i] -= (copysign(1,scaled[i]) * (int)(fabs(scaled[i]) + 0.5)); + rk[0] = Hmat[0][2]; + rk[1] = Hmat[1][2]; + rk[2] = Hmat[2][2]; + + crossProduct3(ri, rj, rij); + distXY = dotProduct3(rk,rij) / norm3(rij); + + crossProduct3(rj,rk, rjk); + distYZ = dotProduct3(ri,rjk) / norm3(rjk); + + crossProduct3(rk,ri, rki); + distZX = dotProduct3(rj,rki) / norm3(rki); + + minDist = min(min(distXY, distYZ), distZX); + return minDist/2; + +} + +void SimInfo::wrapVector( double thePos[3] ){ + + int i; + double scaled[3]; + + if( !orthoRhombic ){ + // calc the scaled coordinates. + matVecMul3(HmatInv, thePos, scaled); + + for(i=0; i<3; i++) + scaled[i] -= roundMe(scaled[i]); + + // calc the wrapped real coordinates from the wrapped scaled coordinates + + matVecMul3(Hmat, scaled, thePos); + + } + else{ + // calc the scaled coordinates. + + for(i=0; i<3; i++) + scaled[i] = thePos[i]*HmatInv[i][i]; + + // wrap the scaled coordinates + + for(i=0; i<3; i++) + scaled[i] -= roundMe(scaled[i]); + + // calc the wrapped real coordinates from the wrapped scaled coordinates + + for(i=0; i<3; i++) + thePos[i] = scaled[i]*Hmat[i][i]; + } + } int SimInfo::getNDF(){ - int ndf_local, ndf; + int ndf_local; + + ndf_local = 0; - ndf_local = 3 * n_atoms + 3 * n_oriented - n_constraints; + for(int i = 0; i < integrableObjects.size(); i++){ + ndf_local += 3; + if (integrableObjects[i]->isDirectional()) { + if (integrableObjects[i]->isLinear()) + ndf_local += 2; + else + ndf_local += 3; + } + } + // n_constraints is local, so subtract them on each processor: + + ndf_local -= n_constraints; + #ifdef IS_MPI MPI_Allreduce(&ndf_local,&ndf,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD); #else ndf = ndf_local; #endif - ndf = ndf - 3; + // nZconstraints is global, as are the 3 COM translations for the + // entire system: + ndf = ndf - 3 - nZconstraints; + return ndf; } int SimInfo::getNDFraw() { - int ndfRaw_local, ndfRaw; + int ndfRaw_local; // Raw degrees of freedom that we have to set - ndfRaw_local = 3 * n_atoms + 3 * n_oriented; - + ndfRaw_local = 0; + + for(int i = 0; i < integrableObjects.size(); i++){ + ndfRaw_local += 3; + if (integrableObjects[i]->isDirectional()) { + if (integrableObjects[i]->isLinear()) + ndfRaw_local += 2; + else + ndfRaw_local += 3; + } + } + #ifdef IS_MPI MPI_Allreduce(&ndfRaw_local,&ndfRaw,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD); #else @@ -285,28 +379,51 @@ int SimInfo::getNDFraw() { return ndfRaw; } - + +int SimInfo::getNDFtranslational() { + int ndfTrans_local; + + ndfTrans_local = 3 * integrableObjects.size() - n_constraints; + + +#ifdef IS_MPI + MPI_Allreduce(&ndfTrans_local,&ndfTrans,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD); +#else + ndfTrans = ndfTrans_local; +#endif + + ndfTrans = ndfTrans - 3 - nZconstraints; + + return ndfTrans; +} + +int SimInfo::getTotIntegrableObjects() { + int nObjs_local; + int nObjs; + + nObjs_local = integrableObjects.size(); + + +#ifdef IS_MPI + MPI_Allreduce(&nObjs_local,&nObjs,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD); +#else + nObjs = nObjs_local; +#endif + + + return nObjs; +} + void SimInfo::refreshSim(){ simtype fInfo; int isError; int n_global; int* excl; - - fInfo.rrf = 0.0; - fInfo.rt = 0.0; + fInfo.dielect = 0.0; - fInfo.box[0] = box_x; - fInfo.box[1] = box_y; - fInfo.box[2] = box_z; - - fInfo.rlist = rList; - fInfo.rcut = rCut; - - if( useDipole ){ - fInfo.rrf = ecr; - fInfo.rt = ecr - est; + if( useDipoles ){ if( useReactionField )fInfo.dielect = dielectric; } @@ -315,43 +432,206 @@ void SimInfo::refreshSim(){ fInfo.SIM_uses_LJ = useLJ; fInfo.SIM_uses_sticky = useSticky; //fInfo.SIM_uses_sticky = 0; - fInfo.SIM_uses_dipoles = useDipole; + fInfo.SIM_uses_charges = useCharges; + fInfo.SIM_uses_dipoles = useDipoles; //fInfo.SIM_uses_dipoles = 0; - //fInfo.SIM_uses_RF = useReactionField; - fInfo.SIM_uses_RF = 0; + fInfo.SIM_uses_RF = useReactionField; + //fInfo.SIM_uses_RF = 0; fInfo.SIM_uses_GB = useGB; fInfo.SIM_uses_EAM = useEAM; - excl = Exclude::getArray(); - + n_exclude = excludes->getSize(); + excl = excludes->getFortranArray(); + #ifdef IS_MPI n_global = mpiSim->getTotAtoms(); #else n_global = n_atoms; #endif - + isError = 0; - + + getFortranGroupArray(this, mfact, ngroup, groupList, groupStart); + //it may not be a good idea to pass the address of first element in vector + //since c++ standard does not require vector to be stored continously in meomory + //Most of the compilers will organize the memory of vector continously setFsimulation( &fInfo, &n_global, &n_atoms, identArray, &n_exclude, excl, - &nGlobalExcludes, globalExcludes, molMembershipArray, - &isError ); - + &nGlobalExcludes, globalExcludes, molMembershipArray, + &mfact[0], &ngroup, &groupList[0], &groupStart[0], &isError); + if( isError ){ - + sprintf( painCave.errMsg, - "There was an error setting the simulation information in fortran.\n" ); + "There was an error setting the simulation information in fortran.\n" ); painCave.isFatal = 1; simError(); } - + #ifdef IS_MPI sprintf( checkPointMsg, "succesfully sent the simulation information to fortran.\n"); MPIcheckPoint(); #endif // is_mpi - + this->ndf = this->getNDF(); this->ndfRaw = this->getNDFraw(); + this->ndfTrans = this->getNDFtranslational(); +} +void SimInfo::setDefaultRcut( double theRcut ){ + + haveRcut = 1; + rCut = theRcut; + rList = rCut + 1.0; + + notifyFortranCutOffs( &rCut, &rSw, &rList ); } +void SimInfo::setDefaultRcut( double theRcut, double theRsw ){ + + rSw = theRsw; + setDefaultRcut( theRcut ); +} + + +void SimInfo::checkCutOffs( void ){ + + if( boxIsInit ){ + + //we need to check cutOffs against the box + + if( rCut > maxCutoff ){ + sprintf( painCave.errMsg, + "cutoffRadius is too large for the current periodic box.\n" + "\tCurrent Value of cutoffRadius = %G at time %G\n " + "\tThis is larger than half of at least one of the\n" + "\tperiodic box vectors. Right now, the Box matrix is:\n" + "\n" + "\t[ %G %G %G ]\n" + "\t[ %G %G %G ]\n" + "\t[ %G %G %G ]\n", + rCut, currentTime, + Hmat[0][0], Hmat[0][1], Hmat[0][2], + Hmat[1][0], Hmat[1][1], Hmat[1][2], + Hmat[2][0], Hmat[2][1], Hmat[2][2]); + painCave.isFatal = 1; + simError(); + } + } else { + // initialize this stuff before using it, OK? + sprintf( painCave.errMsg, + "Trying to check cutoffs without a box.\n" + "\tOOPSE should have better programmers than that.\n" ); + painCave.isFatal = 1; + simError(); + } + +} + +void SimInfo::addProperty(GenericData* prop){ + + map::iterator result; + result = properties.find(prop->getID()); + + //we can't simply use properties[prop->getID()] = prop, + //it will cause memory leak if we already contain a propery which has the same name of prop + + if(result != properties.end()){ + + delete (*result).second; + (*result).second = prop; + + } + else{ + + properties[prop->getID()] = prop; + + } + +} + +GenericData* SimInfo::getProperty(const string& propName){ + + map::iterator result; + + //string lowerCaseName = (); + + result = properties.find(propName); + + if(result != properties.end()) + return (*result).second; + else + return NULL; +} + + +void getFortranGroupArray(SimInfo* info, vector& mfact, int& ngroup, + vector& groupList, vector& groupStart){ + Molecule* myMols; + Atom** myAtoms; + int numAtom; + int curIndex; + double mtot; + int numMol; + int numCutoffGroups; + CutoffGroup* myCutoffGroup; + vector::iterator iterCutoff; + Atom* cutoffAtom; + vector::iterator iterAtom; + int atomIndex; + double totalMass; + + mfact.clear(); + groupList.clear(); + groupStart.clear(); + + //Be careful, fortran array begin at 1 + curIndex = 1; + + myMols = info->molecules; + numMol = info->n_mol; + for(int i = 0; i < numMol; i++){ + numAtom = myMols[i].getNAtoms(); + myAtoms = myMols[i].getMyAtoms(); + + + for(int j = 0; j < numAtom; j++){ + + +#ifdef IS_MPI + atomIndex = myAtoms[j]->getGlobalIndex(); +#else + atomIndex = myAtoms[j]->getIndex(); +#endif + + if(myMols[i].belongToCutoffGroup(atomIndex)) + continue; + else{ + // this is a fraction of the cutoff group's mass, not the mass itself! + mfact.push_back(1.0); + groupList.push_back(myAtoms[j]->getIndex() + 1); + groupStart.push_back(curIndex++); + } + } + + numCutoffGroups = myMols[i].getNCutoffGroups(); + for(myCutoffGroup =myMols[i].beginCutoffGroup(iterCutoff); myCutoffGroup != NULL; + myCutoffGroup =myMols[i].nextCutoffGroup(iterCutoff)){ + + totalMass = myCutoffGroup->getMass(); + + for(cutoffAtom = myCutoffGroup->beginAtom(iterAtom); cutoffAtom != NULL; + cutoffAtom = myCutoffGroup->nextAtom(iterAtom)){ + mfact.push_back(cutoffAtom->getMass()/totalMass); + groupList.push_back(cutoffAtom->getIndex() + 1); + } + + groupStart.push_back(curIndex); + curIndex += myCutoffGroup->getNumAtom(); + + }//end for(myCutoffGroup =myMols[i].beginCutoffGroup(iterCutoff)) + + }//end for(int i = 0; i < numMol; i++) + + ngroup = groupStart.size(); +}