--- trunk/OOPSE/libmdtools/SimInfo.cpp 2003/07/10 22:15:53 590 +++ trunk/OOPSE/libmdtools/SimInfo.cpp 2004/04/15 16:18:26 1113 @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #include using namespace std; @@ -12,6 +12,8 @@ using namespace std; #include "fortranWrappers.hpp" +#include "MatVec3.h" + #ifdef IS_MPI #include "mpiSimulation.hpp" #endif @@ -20,32 +22,70 @@ 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; + ecr = 0.0; + est = 0.0; + haveRcut = 0; + haveEcr = 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; + excludes = Exclude::Instance(); + + myConfiguration = new SimState(); + + has_minimizer = false; + the_minimizer =NULL; + wrapMeSimInfo( this ); } + +SimInfo::~SimInfo(){ + + delete myConfiguration; + + map::iterator i; + + for(i = properties.begin(); i != properties.end(); i++) + delete (*i).second; + +} + void SimInfo::setBox(double newBox[3]) { int i, j; @@ -64,8 +104,7 @@ void SimInfo::setBoxM( double theBox[3][3] ){ void SimInfo::setBoxM( double theBox[3][3] ){ - int i, j, status; - double smallestBoxL, maxCutoff; + int i, j; double FortranHmat[9]; // to preserve compatibility with Fortran the // ordering in the array is as follows: // [ 0 3 6 ] @@ -73,16 +112,11 @@ void SimInfo::setBoxM( double theBox[3][3] ){ // [ 2 5 8 ] double FortranHmatInv[9]; // the inverted Hmat (for Fortran); + if( !boxIsInit ) boxIsInit = 1; for(i=0; i < 3; i++) for (j=0; j < 3; j++) Hmat[i][j] = theBox[i][j]; - cerr - << "setting Hmat ->\n" - << "[ " << Hmat[0][0] << ", " << Hmat[0][1] << ", " << Hmat[0][2] << " ]\n" - << "[ " << Hmat[1][0] << ", " << Hmat[1][1] << ", " << Hmat[1][2] << " ]\n" - << "[ " << Hmat[2][0] << ", " << Hmat[2][1] << ", " << Hmat[2][2] << " ]\n"; - calcBoxL(); calcHmatInv(); @@ -95,49 +129,6 @@ void SimInfo::setBoxM( double theBox[3][3] ){ setFortranBoxSize(FortranHmat, FortranHmatInv, &orthoRhombic); - smallestBoxL = boxLx; - if (boxLy < smallestBoxL) smallestBoxL = boxLy; - if (boxLz < smallestBoxL) smallestBoxL = boxLz; - - maxCutoff = smallestBoxL / 2.0; - - if (rList > maxCutoff) { - sprintf( painCave.errMsg, - "New Box size is forcing neighborlist radius down to %lf\n", - maxCutoff ); - painCave.isFatal = 0; - simError(); - - rList = maxCutoff; - - sprintf( painCave.errMsg, - "New Box size is forcing cutoff radius down to %lf\n", - maxCutoff - 1.0 ); - painCave.isFatal = 0; - simError(); - - rCut = rList - 1.0; - - // list radius changed so we have to refresh the simulation structure. - refreshSim(); - } - - if (rCut > maxCutoff) { - sprintf( painCave.errMsg, - "New Box size is forcing cutoff radius down to %lf\n", - maxCutoff ); - painCave.isFatal = 0; - simError(); - - 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(); - } - } } @@ -153,7 +144,7 @@ void SimInfo::scaleBox(double scale) { double theBox[3][3]; int i, j; - cerr << "Scaling box by " << scale << "\n"; + // cerr << "Scaling box by " << scale << "\n"; for(i=0; i<3; i++) for (j=0; j<3; j++) theBox[i][j] = Hmat[i][j]*scale; @@ -164,6 +155,7 @@ void SimInfo::calcHmatInv( void ) { void SimInfo::calcHmatInv( void ) { + int oldOrtho; int i,j; double smallDiag; double tol; @@ -171,106 +163,56 @@ void SimInfo::calcHmatInv( void ) { invertMat3( Hmat, HmatInv ); - // Check the inverse to make sure it is sane: - - matMul3( Hmat, HmatInv, sanity ); - // check to see if Hmat is orthorhombic - smallDiag = Hmat[0][0]; - if(smallDiag > Hmat[1][1]) smallDiag = Hmat[1][1]; - if(smallDiag > Hmat[2][2]) smallDiag = Hmat[2][2]; - tol = smallDiag * 1E-6; + oldOrtho = orthoRhombic; + 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; + orthoRhombic = 1; for (i = 0; i < 3; i++ ) { for (j = 0 ; j < 3; j++) { if (i != j) { if (orthoRhombic) { - if (Hmat[i][j] >= tol) orthoRhombic = 0; + if ( fabs(Hmat[i][j]) >= tol) orthoRhombic = 0; } } } } -} -double SimInfo::matDet3(double a[3][3]) { - int i, j, k; - double determinant; - - determinant = 0.0; - - for(i = 0; i < 3; i++) { - j = (i+1)%3; - k = (i+2)%3; - - determinant += a[0][i] * (a[1][j]*a[2][k] - a[1][k]*a[2][j]); - } - - return determinant; -} - -void SimInfo::invertMat3(double a[3][3], double b[3][3]) { - - int i, j, k, l, m, n; - double determinant; - - determinant = matDet3( a ); - - if (determinant == 0.0) { - sprintf( painCave.errMsg, - "Can't invert a matrix with a zero determinant!\n"); - painCave.isFatal = 1; - simError(); - } - - for (i=0; i < 3; i++) { - j = (i+1)%3; - k = (i+2)%3; - for(l = 0; l < 3; l++) { - m = (l+1)%3; - n = (l+2)%3; - - b[l][i] = (a[j][m]*a[k][n] - a[j][n]*a[k][m]) / determinant; + 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::matMul3(double a[3][3], double b[3][3], double c[3][3]) { - double r00, r01, r02, r10, r11, r12, r20, r21, r22; - - r00 = a[0][0]*b[0][0] + a[0][1]*b[1][0] + a[0][2]*b[2][0]; - r01 = a[0][0]*b[0][1] + a[0][1]*b[1][1] + a[0][2]*b[2][1]; - r02 = a[0][0]*b[0][2] + a[0][1]*b[1][2] + a[0][2]*b[2][2]; - - r10 = a[1][0]*b[0][0] + a[1][1]*b[1][0] + a[1][2]*b[2][0]; - r11 = a[1][0]*b[0][1] + a[1][1]*b[1][1] + a[1][2]*b[2][1]; - r12 = a[1][0]*b[0][2] + a[1][1]*b[1][2] + a[1][2]*b[2][2]; - - r20 = a[2][0]*b[0][0] + a[2][1]*b[1][0] + a[2][2]*b[2][0]; - r21 = a[2][0]*b[0][1] + a[2][1]*b[1][1] + a[2][2]*b[2][1]; - r22 = a[2][0]*b[0][2] + a[2][1]*b[1][2] + a[2][2]*b[2][2]; - - c[0][0] = r00; c[0][1] = r01; c[0][2] = r02; - c[1][0] = r10; c[1][1] = r11; c[1][2] = r12; - c[2][0] = r20; c[2][1] = r21; c[2][2] = r22; -} - -void SimInfo::matVecMul3(double m[3][3], double inVec[3], double outVec[3]) { - double a0, a1, a2; - - a0 = inVec[0]; a1 = inVec[1]; a2 = inVec[2]; - - outVec[0] = m[0][0]*a0 + m[0][1]*a1 + m[0][2]*a2; - outVec[1] = m[1][0]*a0 + m[1][1]*a1 + m[1][2]*a2; - outVec[2] = m[2][0]*a0 + m[2][1]*a1 + m[2][2]*a2; -} - void SimInfo::calcBoxL( void ){ double dx, dy, dz, dsq; - int i; // boxVol = Determinant of Hmat @@ -280,26 +222,67 @@ void SimInfo::calcBoxL( void ){ 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[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[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(); + } +double SimInfo::calcMaxCutOff(){ + + double ri[3], rj[3], rk[3]; + double rij[3], rjk[3], rki[3]; + double minDist; + + ri[0] = Hmat[0][0]; + ri[1] = Hmat[1][0]; + ri[2] = Hmat[2][0]; + + rj[0] = Hmat[0][1]; + rj[1] = Hmat[1][1]; + rj[2] = Hmat[2][1]; + + 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, j, k; + int i; double scaled[3]; if( !orthoRhombic ){ @@ -337,27 +320,46 @@ int SimInfo::getNDF(){ 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()) + 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()) + ndfRaw_local += 3; + } + #ifdef IS_MPI MPI_Allreduce(&ndfRaw_local,&ndfRaw,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD); #else @@ -366,24 +368,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.rlist = rList; - fInfo.rcut = rCut; - - if( useDipole ){ - fInfo.rrf = ecr; - fInfo.rt = ecr - est; + if( useDipoles ){ if( useReactionField )fInfo.dielect = dielectric; } @@ -392,14 +421,16 @@ 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(); @@ -429,6 +460,134 @@ void SimInfo::refreshSim(){ this->ndf = this->getNDF(); this->ndfRaw = this->getNDFraw(); + this->ndfTrans = this->getNDFtranslational(); +} +void SimInfo::setDefaultRcut( double theRcut ){ + + haveRcut = 1; + rCut = theRcut; + + ( rCut > ecr )? rList = rCut + 1.0: rList = ecr + 1.0; + + notifyFortranCutOffs( &rCut, &rList, &ecr, &est ); } +void SimInfo::setDefaultEcr( double theEcr ){ + + haveEcr = 1; + ecr = theEcr; + + ( rCut > ecr )? rList = rCut + 1.0: rList = ecr + 1.0; + + notifyFortranCutOffs( &rCut, &rList, &ecr, &est ); +} + +void SimInfo::setDefaultEcr( double theEcr, double theEst ){ + + est = theEst; + setDefaultEcr( theEcr ); +} + + +void SimInfo::checkCutOffs( void ){ + + if( boxIsInit ){ + + //we need to check cutOffs against the box + + if( rCut > maxCutoff ){ + sprintf( painCave.errMsg, + "LJrcut is too large for the current periodic box.\n" + "\tCurrent Value of LJrcut = %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, %G" + "\t[ %G %G %G ]\n" + "\t[ %G %G %G ]\n" + "\t[ %G %G %G ]\n", + rCut, currentTime, maxCutoff, + 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(); + } + + if( haveEcr ){ + if( ecr > maxCutoff ){ + sprintf( painCave.errMsg, + "electrostaticCutoffRadius is too large for the current\n" + "\tperiodic box.\n\n" + "\tCurrent Value of ECR = %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", + ecr, 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; +} + +vector SimInfo::getProperties(){ + + vector result; + map::iterator i; + + for(i = properties.begin(); i != properties.end(); i++) + result.push_back((*i).second); + + return result; +}