--- trunk/OOPSE/libmdtools/SimInfo.cpp 2003/07/09 22:14:06 586 +++ trunk/OOPSE/libmdtools/SimInfo.cpp 2003/07/10 17:10:56 588 @@ -48,38 +48,52 @@ void SimInfo::setBox(double newBox[3]) { void SimInfo::setBox(double newBox[3]) { - int i; - double tempMat[9]; + int i, j; + double tempMat[3][3]; - for(i=0; i<9; i++) tempMat[i] = 0.0;; + for(i=0; i<3; i++) + for (j=0; j<3; j++) tempMat[i][j] = 0.0;; - tempMat[0] = newBox[0]; - tempMat[4] = newBox[1]; - tempMat[8] = newBox[2]; + tempMat[0][0] = newBox[0]; + tempMat[1][1] = newBox[1]; + tempMat[2][2] = newBox[2]; setBoxM( tempMat ); } -void SimInfo::setBoxM( double theBox[9] ){ +void SimInfo::setBoxM( double theBox[3][3] ){ - int i, status; + 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); - for(i=0; i<9; i++) Hmat[i] = theBox[i]; + for(i=0; i < 3; i++) + for (j=0; j < 3; j++) Hmat[i][j] = theBox[i][j]; + cerr << "setting Hmat ->\n" - << "[ " << Hmat[0] << ", " << Hmat[3] << ", " << Hmat[6] << " ]\n" - << "[ " << Hmat[1] << ", " << Hmat[4] << ", " << Hmat[7] << " ]\n" - << "[ " << Hmat[2] << ", " << Hmat[5] << ", " << Hmat[8] << " ]\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"; - calcHmatI(); calcBoxL(); + calcHmatInv(); + 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]; + } + } - - setFortranBoxSize(Hmat, HmatI, &orthoRhombic); + setFortranBoxSize(FortranHmat, FortranHmatI, &orthoRhombic); smallestBoxL = boxLx; if (boxLy < smallestBoxL) smallestBoxL = boxLy; @@ -127,128 +141,161 @@ void SimInfo::getBoxM (double theBox[9]) { } -void SimInfo::getBoxM (double theBox[9]) { +void SimInfo::getBoxM (double theBox[3][3]) { - int i; - for(i=0; i<9; i++) theBox[i] = Hmat[i]; + int i, j; + for(i=0; i<3; i++) + for (j=0; j<3; j++) theBox[i][j] = Hmat[i][j]; } void SimInfo::scaleBox(double scale) { - double theBox[9]; - int i; + double theBox[3][3]; + int i, j; cerr << "Scaling box by " << scale << "\n"; - for(i=0; i<9; i++) theBox[i] = Hmat[i]*scale; + for(i=0; i<3; i++) + for (j=0; j<3; j++) theBox[i][j] = Hmat[i][j]*scale; setBoxM(theBox); } -void SimInfo::calcHmatI( void ) { +void SimInfo::calcHmatInv( void ) { - double C[3][3]; - double detHmat; - int i, j, k; double smallDiag; double tol; double sanity[3][3]; - // calculate the adjunct of Hmat; + invertMat3( Hmat, HmatInv ); - 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]); + // Check the inverse to make sure it is sane: - 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]); + matMul3( Hmat, HmatInv, sanity ); - 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++; - } - } - - // sanity check - - for(i=0; i<3; i++){ - for(j=0; j<3; j++){ - - sanity[i][j] = 0.0; - for(k=0; k<3; k++){ - sanity[i][j] += Hmat[3*k+i] * HmatI[3*j+k]; - } - } - } - 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"; - // check to see if Hmat is orthorhombic - smallDiag = Hmat[0]; - if(smallDiag > Hmat[4]) smallDiag = Hmat[4]; - if(smallDiag > Hmat[8]) smallDiag = Hmat[8]; + 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; orthoRhombic = 1; - for(i=0; (i<9) && orthoRhombic; i++){ - - if( (i%4) ){ // ignore the diagonals (0, 4, and 8) - orthoRhombic = (Hmat[i] <= tol); + + for (i = 0; i < 3; i++ ) { + for (j = 0 ; j < 3; j++) { + if (i != j) { + if (orthoRhombic) { + if (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; + } + } +} + +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 = 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 ); // 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 ); // 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 ); @@ -262,27 +309,23 @@ 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]; - - // 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[0]*Hmat[i] + scaled[1]*Hmat[i+3] + scaled[2]*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 @@ -292,10 +335,9 @@ void SimInfo::wrapVector( double thePos[3] ){ // 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]; } - }