--- trunk/OOPSE/libmdtools/SimInfo.cpp 2003/08/11 18:29:46 674 +++ trunk/OOPSE/libmdtools/SimInfo.cpp 2003/11/06 19:11:38 853 @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #include using namespace std; @@ -26,6 +26,7 @@ SimInfo::SimInfo(){ SimInfo::SimInfo(){ excludes = NULL; n_constraints = 0; + nZconstraints = 0; n_oriented = 0; n_dipoles = 0; ndf = 0; @@ -36,7 +37,9 @@ SimInfo::SimInfo(){ thermalTime = 0.0; currentTime = 0.0; rCut = 0.0; + origRcut = -1.0; ecr = 0.0; + origEcr = -1.0; est = 0.0; oldEcr = 0.0; oldRcut = 0.0; @@ -45,6 +48,7 @@ SimInfo::SimInfo(){ haveOrigEcr = 0; boxIsInit = 0; + resetTime = 1e99; usePBC = 0; @@ -90,8 +94,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 ] @@ -143,6 +146,7 @@ void SimInfo::calcHmatInv( void ) { void SimInfo::calcHmatInv( void ) { + int oldOrtho; int i,j; double smallDiag; double tol; @@ -150,15 +154,13 @@ 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]; + 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 * 1E-6; orthoRhombic = 1; @@ -167,10 +169,26 @@ void SimInfo::calcHmatInv( void ) { 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; } } + } + } + + if( oldOrtho != orthoRhombic ){ + + if( orthoRhombic ){ + sprintf( painCave.errMsg, + "Hmat is switching from Non-Orthorhombic to OrthoRhombic\n" + " If this is a bad thing change the ortho tolerance in SimInfo.\n" ); + simError(); } + else { + sprintf( painCave.errMsg, + "Hmat is switching from Orthorhombic to Non-OrthoRhombic\n" + " If this is a bad thing change the ortho tolerance in SimInfo.\n" ); + simError(); + } } } @@ -278,10 +296,26 @@ void SimInfo::printMat9(double A[9] ){ << "[ " << A[6] << ", " << A[7] << ", " << A[8] << " ]\n"; } + +void SimInfo::crossProduct3(double a[3],double b[3], double out[3]){ + + out[0] = a[1] * b[2] - a[2] * b[1]; + out[1] = a[2] * b[0] - a[0] * b[2] ; + out[2] = a[0] * b[1] - a[1] * b[0]; + +} + +double SimInfo::dotProduct3(double a[3], double b[3]){ + return a[0]*b[0] + a[1]*b[1]+ a[2]*b[2]; +} + +double SimInfo::length3(double a[3]){ + return sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]); +} + void SimInfo::calcBoxL( void ){ double dx, dy, dz, dsq; - int i; // boxVol = Determinant of Hmat @@ -292,30 +326,66 @@ void SimInfo::calcBoxL( void ){ dx = Hmat[0][0]; dy = Hmat[1][0]; dz = Hmat[2][0]; dsq = dx*dx + dy*dy + dz*dz; boxL[0] = sqrt( dsq ); - maxCutoff = 0.5 * boxL[0]; + //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; boxL[1] = sqrt( dsq ); - if( (0.5 * boxL[1]) < maxCutoff ) maxCutoff = 0.5 * boxL[1]; + //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; boxL[2] = sqrt( dsq ); - if( (0.5 * boxL[2]) < maxCutoff ) maxCutoff = 0.5 * boxL[2]; + //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) / length3(rij); + + crossProduct3(rj,rk, rjk); + distYZ = dotProduct3(ri,rjk) / length3(rjk); + + crossProduct3(rk,ri, rki); + distZX = dotProduct3(rj,rki) / length3(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 ){ @@ -353,7 +423,7 @@ int SimInfo::getNDF(){ int SimInfo::getNDF(){ - int ndf_local, ndf; + int ndf_local; ndf_local = 3 * n_atoms + 3 * n_oriented - n_constraints; @@ -369,7 +439,7 @@ int SimInfo::getNDFraw() { } 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; @@ -382,7 +452,23 @@ int SimInfo::getNDFraw() { return ndfRaw; } - + +int SimInfo::getNDFtranslational() { + int ndfTrans_local; + + ndfTrans_local = 3 * n_atoms - 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; +} + void SimInfo::refreshSim(){ simtype fInfo; @@ -438,115 +524,138 @@ void SimInfo::refreshSim(){ this->ndf = this->getNDF(); this->ndfRaw = this->getNDFraw(); - + this->ndfTrans = this->getNDFtranslational(); } void SimInfo::setRcut( double theRcut ){ - if( !haveOrigRcut ){ - haveOrigRcut = 1; - origRcut = theRcut; - } - rCut = theRcut; checkCutOffs(); } -void SimInfo::setEcr( double theEcr ){ - - if( !haveOrigEcr ){ - haveOrigEcr = 1; - origEcr = theEcr; - } +void SimInfo::setDefaultRcut( double theRcut ){ + haveOrigRcut = 1; + origRcut = theRcut; + rCut = theRcut; + + ( rCut > ecr )? rList = rCut + 1.0: rList = ecr + 1.0; + + notifyFortranCutOffs( &rCut, &rList, &ecr, &est ); +} + +void SimInfo::setEcr( double theEcr ){ + ecr = theEcr; checkCutOffs(); } +void SimInfo::setDefaultEcr( double theEcr ){ + + haveOrigEcr = 1; + origEcr = theEcr; + + ( rCut > ecr )? rList = rCut + 1.0: rList = ecr + 1.0; + + ecr = theEcr; + + notifyFortranCutOffs( &rCut, &rList, &ecr, &est ); +} + void SimInfo::setEcr( double theEcr, double theEst ){ est = theEst; setEcr( theEcr ); } +void SimInfo::setDefaultEcr( double theEcr, double theEst ){ -void SimInfo::checkCutOffs( void ){ + est = theEst; + setDefaultEcr( theEcr ); +} - int cutChanged = 0; +void SimInfo::checkCutOffs( void ){ - + int cutChanged = 0; + if( boxIsInit ){ //we need to check cutOffs against the box - + + //detect the change of rCut if(( maxCutoff > rCut )&&(usePBC)){ if( rCut < origRcut ){ - rCut = origRcut; - if (rCut > maxCutoff) rCut = maxCutoff; - - sprintf( painCave.errMsg, - "New Box size is setting the long range cutoff radius " - "to %lf\n", - rCut ); - painCave.isFatal = 0; - simError(); + rCut = origRcut; + + if (rCut > maxCutoff) + rCut = maxCutoff; + + sprintf( painCave.errMsg, + "New Box size is setting the long range cutoff radius " + "to %lf at time %lf\n", + rCut, currentTime ); + painCave.isFatal = 0; + simError(); } } - - if( maxCutoff > ecr ){ - if( ecr < origEcr ){ - rCut = origEcr; - if (ecr > maxCutoff) ecr = maxCutoff; - - sprintf( painCave.errMsg, - "New Box size is setting the electrostaticCutoffRadius " - "to %lf\n", - ecr ); - painCave.isFatal = 0; - simError(); - } - } - - - if ((rCut > maxCutoff)&&(usePBC)) { + else if ((rCut > maxCutoff)&&(usePBC)) { sprintf( painCave.errMsg, "New Box size is setting the long range cutoff radius " - "to %lf\n", - maxCutoff ); + "to %lf at time %lf\n", + maxCutoff, currentTime ); painCave.isFatal = 0; simError(); rCut = maxCutoff; } - if( ecr > maxCutoff){ + + //detect the change of ecr + if( maxCutoff > ecr ){ + if( ecr < origEcr ){ + ecr = origEcr; + if (ecr > maxCutoff) ecr = maxCutoff; + + sprintf( painCave.errMsg, + "New Box size is setting the electrostaticCutoffRadius " + "to %lf at time %lf\n", + ecr, currentTime ); + painCave.isFatal = 0; + simError(); + } + } + else if( ecr > maxCutoff){ sprintf( painCave.errMsg, "New Box size is setting the electrostaticCutoffRadius " - "to %lf\n", - maxCutoff ); + "to %lf at time %lf\n", + maxCutoff, currentTime ); painCave.isFatal = 0; simError(); ecr = maxCutoff; } + if( (oldEcr != ecr) || ( oldRcut != rCut ) ) cutChanged = 1; - } - - - if( (oldEcr != ecr) || ( oldRcut != rCut ) ) cutChanged = 1; - - // rlist is the 1.0 plus max( rcut, ecr ) - - ( rCut > ecr )? rList = rCut + 1.0: rList = ecr + 1.0; - - if( cutChanged ){ + // rlist is the 1.0 plus max( rcut, ecr ) - notifyFortranCutOffs( &rCut, &rList, &ecr, &est ); + ( rCut > ecr )? rList = rCut + 1.0: rList = ecr + 1.0; + + if( cutChanged ){ + notifyFortranCutOffs( &rCut, &rList, &ecr, &est ); + } + + oldEcr = ecr; + oldRcut = rCut; + + } else { + // initialize this stuff before using it, OK? + sprintf( painCave.errMsg, + "Trying to check cutoffs without a box. Be smarter.\n" ); + painCave.isFatal = 1; + simError(); } - - oldEcr = ecr; - oldRcut = rCut; + } void SimInfo::addProperty(GenericData* prop){ @@ -596,4 +705,9 @@ vector SimInfo::getProperties(){ return result; } +double SimInfo::matTrace3(double m[3][3]){ + double trace; + trace = m[0][0] + m[1][1] + m[2][2]; + return trace; +}