--- trunk/OOPSE/libmdtools/SimInfo.cpp 2003/07/01 21:33:45 569 +++ trunk/OOPSE/libmdtools/SimInfo.cpp 2003/08/15 19:24:13 699 @@ -2,6 +2,8 @@ #include #include +#include +using namespace std; #include "SimInfo.hpp" #define __C @@ -14,20 +16,40 @@ #include "mpiSimulation.hpp" #endif +inline double roundMe( double x ){ + return ( x >= 0 ) ? floor( x + 0.5 ) : ceil( x - 0.5 ); +} + + 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; + origRcut = -1.0; + ecr = 0.0; + origEcr = -1.0; + est = 0.0; + oldEcr = 0.0; + oldRcut = 0.0; + haveOrigRcut = 0; + haveOrigEcr = 0; + boxIsInit = 0; + + + usePBC = 0; useLJ = 0; useSticky = 0; @@ -36,241 +58,261 @@ SimInfo::SimInfo(){ useGB = 0; useEAM = 0; + myConfiguration = new SimState(); + 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, HmatI, &orthoRhombic); + 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, status; + double smallestBoxL, maxCutoff; + 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, HmatI, &orthoRhombic); - - 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 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 the inverse to make sure it is sane: - if (rCut > maxCutoff) { - sprintf( painCave.errMsg, - "New Box size is forcing cutoff radius down to %lf\n", - maxCutoff ); - painCave.isFatal = 0; - simError(); + 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; - 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 (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; +double SimInfo::matDet3(double a[3][3]) { int i, j, k; - double smallDiag; - double tol; - double sanity[3][3]; + double determinant; - // calculate the adjunct of Hmat; + determinant = 0.0; - 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]); + for(i = 0; i < 3; i++) { + j = (i+1)%3; + k = (i+2)%3; - 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]); + determinant += a[0][i] * (a[1][j]*a[2][k] - a[1][k]*a[2][j]); + } - 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]); + return determinant; +} - // calcutlate the determinant of Hmat +void SimInfo::invertMat3(double a[3][3], double b[3][3]) { - detHmat = 0.0; - for(i=0; i<3; i++) detHmat += Hmat[i] * C[i][0]; + int i, j, k, l, m, n; + double determinant; - - // H^-1 = C^T / det(H) - - i=0; - for(j=0; j<3; j++){ - for(k=0; k<3; k++){ + determinant = matDet3( a ); - HmatI[i] = C[j][k] / detHmat; - i++; - } + if (determinant == 0.0) { + sprintf( painCave.errMsg, + "Can't invert a matrix with a zero determinant!\n"); + painCave.isFatal = 1; + simError(); } - // sanity check - - for(i=0; i<3; i++){ - for(j=0; j<3; j++){ + 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; - sanity[i][j] = 0.0; - for(k=0; k<3; k++){ - sanity[i][j] += Hmat[3*k+i] * HmatI[3*j+k]; - } + b[l][i] = (a[j][m]*a[k][n] - a[j][n]*a[k][m]) / determinant; } } +} - cerr << "sanity => \n" - << sanity[0][0] << "\t" << sanity[0][1] << "\t" << sanity [0][2] << "\n" - << sanity[1][0] << "\t" << sanity[1][1] << "\t" << sanity [1][2] << "\n" - << sanity[2][0] << "\t" << sanity[2][1] << "\t" << sanity [2][2] - << "\n"; - +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; - // check to see if Hmat is orthorhombic + 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]; - smallDiag = Hmat[0]; - if(smallDiag > Hmat[4]) smallDiag = Hmat[4]; - if(smallDiag > Hmat[8]) smallDiag = Hmat[8]; - tol = smallDiag * 1E-6; + 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; +} - orthoRhombic = 1; - for(i=0; (i<9) && orthoRhombic; i++){ - - if( (i%4) ){ // ignore the diagonals (0, 4, and 8) - orthoRhombic = (Hmat[i] <= tol); +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::transposeMat3(double in[3][3], double out[3][3]) { + double temp[3][3]; + int i, j; + + for (i = 0; i < 3; i++) { + for (j = 0; j < 3; j++) { + temp[j][i] = in[i][j]; } } - + for (i = 0; i < 3; i++) { + for (j = 0; j < 3; j++) { + out[i][j] = temp[i][j]; + } + } } + +void SimInfo::printMat3(double A[3][3] ){ + std::cerr + << "[ " << A[0][0] << ", " << A[0][1] << ", " << A[0][2] << " ]\n" + << "[ " << A[1][0] << ", " << A[1][1] << ", " << A[1][2] << " ]\n" + << "[ " << A[2][0] << ", " << A[2][1] << ", " << A[2][2] << " ]\n"; +} + +void SimInfo::printMat9(double A[9] ){ + + std::cerr + << "[ " << A[0] << ", " << A[1] << ", " << A[2] << " ]\n" + << "[ " << A[3] << ", " << A[4] << ", " << A[5] << " ]\n" + << "[ " << A[6] << ", " << A[7] << ", " << A[8] << " ]\n"; +} + 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]; + checkCutOffs(); + } @@ -281,40 +323,35 @@ void SimInfo::wrapVector( double thePos[3] ){ if( !orthoRhombic ){ // calc the scaled coordinates. + + + matVecMul3(HmatInv, thePos, scaled); for(i=0; i<3; i++) - scaled[i] = - thePos[0]*HmatI[i] + thePos[1]*HmatI[i+3] + thePos[3]*HmatI[i+6]; + scaled[i] -= roundMe(scaled[i]); - // wrap the scaled coordinates - - for(i=0; i<3; i++) - scaled[i] -= round(scaled[i]); - // calc the wrapped real coordinates from the wrapped scaled coordinates - for(i=0; i<3; i++) - thePos[i] = - scaled[0]*Hmat[i] + scaled[1]*Hmat[i+3] + scaled[3]*Hmat[i+6]; + matVecMul3(Hmat, scaled, thePos); + } else{ // calc the scaled coordinates. for(i=0; i<3; i++) - scaled[i] = thePos[i]*HmatI[i*4]; + scaled[i] = thePos[i]*HmatInv[i][i]; // wrap the scaled coordinates for(i=0; i<3; i++) - scaled[i] -= round(scaled[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*4]; + thePos[i] = scaled[i]*Hmat[i][i]; } - } @@ -329,7 +366,7 @@ int SimInfo::getNDF(){ ndf = ndf_local; #endif - ndf = ndf - 3; + ndf = ndf - 3 - nZconstraints; return ndf; } @@ -355,21 +392,10 @@ void SimInfo::refreshSim(){ 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( useReactionField )fInfo.dielect = dielectric; } @@ -415,6 +441,162 @@ void SimInfo::refreshSim(){ this->ndf = this->getNDF(); this->ndfRaw = this->getNDFraw(); + +} + + +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; + } + + ecr = theEcr; + checkCutOffs(); +} +void SimInfo::setEcr( double theEcr, double theEst ){ + + est = theEst; + setEcr( theEcr ); } + +void SimInfo::checkCutOffs( void ){ + + int cutChanged = 0; + + + + if( boxIsInit ){ + + //we need to check cutOffs against the box + + 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(); + } + } + + 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)) { + sprintf( painCave.errMsg, + "New Box size is setting the long range cutoff radius " + "to %lf\n", + maxCutoff ); + painCave.isFatal = 0; + simError(); + rCut = maxCutoff; + } + + if( ecr > maxCutoff){ + sprintf( painCave.errMsg, + "New Box size is setting the electrostaticCutoffRadius " + "to %lf\n", + maxCutoff ); + painCave.isFatal = 0; + simError(); + ecr = maxCutoff; + } + + + } + + + 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 ){ + + notifyFortranCutOffs( &rCut, &rList, &ecr, &est ); + } + + oldEcr = ecr; + oldRcut = rCut; +} + +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; +} + +