ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/SimInfo.cpp
(Generate patch)

Comparing trunk/OOPSE/libmdtools/SimInfo.cpp (file contents):
Revision 569 by mmeineke, Tue Jul 1 21:33:45 2003 UTC vs.
Revision 617 by gezelter, Tue Jul 15 19:56:08 2003 UTC

# Line 2 | Line 2
2   #include <cstring>
3   #include <cmath>
4  
5 + #include <iostream>
6 + using namespace std;
7  
8   #include "SimInfo.hpp"
9   #define __C
# Line 14 | Line 16 | SimInfo* currentInfo;
16   #include "mpiSimulation.hpp"
17   #endif
18  
19 + inline double roundMe( double x ){
20 +  return ( x >= 0 ) ? floor( x + 0.5 ) : ceil( x - 0.5 );
21 + }
22 +          
23 +
24   SimInfo* currentInfo;
25  
26   SimInfo::SimInfo(){
# Line 40 | Line 47 | void SimInfo::setBox(double newBox[3]) {
47   }
48  
49   void SimInfo::setBox(double newBox[3]) {
50 +  
51 +  int i, j;
52 +  double tempMat[3][3];
53  
54 <  double smallestBoxL, maxCutoff;
55 <  int status;
46 <  int i;
54 >  for(i=0; i<3; i++)
55 >    for (j=0; j<3; j++) tempMat[i][j] = 0.0;;
56  
57 <  for(i=0; i<9; i++) Hmat[i] = 0.0;;
57 >  tempMat[0][0] = newBox[0];
58 >  tempMat[1][1] = newBox[1];
59 >  tempMat[2][2] = newBox[2];
60  
61 <  Hmat[0] = newBox[0];
51 <  Hmat[4] = newBox[1];
52 <  Hmat[8] = newBox[2];
61 >  setBoxM( tempMat );
62  
63 <  calcHmatI();
55 <  calcBoxL();
63 > }
64  
65 <  setFortranBoxSize(Hmat, HmatI, &orthoRhombic);
65 > void SimInfo::setBoxM( double theBox[3][3] ){
66 >  
67 >  int i, j, status;
68 >  double smallestBoxL, maxCutoff;
69 >  double FortranHmat[9]; // to preserve compatibility with Fortran the
70 >                         // ordering in the array is as follows:
71 >                         // [ 0 3 6 ]
72 >                         // [ 1 4 7 ]
73 >                         // [ 2 5 8 ]
74 >  double FortranHmatInv[9]; // the inverted Hmat (for Fortran);
75  
59  smallestBoxL = boxLx;
60  if (boxLy < smallestBoxL) smallestBoxL = boxLy;
61  if (boxLz < smallestBoxL) smallestBoxL = boxLz;
76  
77 <  maxCutoff = smallestBoxL / 2.0;
77 >  for(i=0; i < 3; i++)
78 >    for (j=0; j < 3; j++) Hmat[i][j] = theBox[i][j];
79 >  
80 >  //  cerr
81 >  // << "setting Hmat ->\n"
82 >  // << "[ " << Hmat[0][0] << ", " << Hmat[0][1] << ", " << Hmat[0][2] << " ]\n"
83 >  // << "[ " << Hmat[1][0] << ", " << Hmat[1][1] << ", " << Hmat[1][2] << " ]\n"
84 >  // << "[ " << Hmat[2][0] << ", " << Hmat[2][1] << ", " << Hmat[2][2] << " ]\n";
85  
86 <  if (rList > maxCutoff) {
87 <    sprintf( painCave.errMsg,
67 <             "New Box size is forcing neighborlist radius down to %lf\n",
68 <             maxCutoff );
69 <    painCave.isFatal = 0;
70 <    simError();
86 >  calcBoxL();
87 >  calcHmatInv();
88  
89 <    rList = maxCutoff;
90 <
91 <    sprintf( painCave.errMsg,
92 <             "New Box size is forcing cutoff radius down to %lf\n",
76 <             maxCutoff - 1.0 );
77 <    painCave.isFatal = 0;
78 <    simError();
79 <
80 <    rCut = rList - 1.0;
81 <
82 <    // list radius changed so we have to refresh the simulation structure.
83 <    refreshSim();
84 <  }
85 <
86 <  if (rCut > maxCutoff) {
87 <    sprintf( painCave.errMsg,
88 <             "New Box size is forcing cutoff radius down to %lf\n",
89 <             maxCutoff );
90 <    painCave.isFatal = 0;
91 <    simError();
92 <
93 <    status = 0;
94 <    LJ_new_rcut(&rCut, &status);
95 <    if (status != 0) {
96 <      sprintf( painCave.errMsg,
97 <               "Error in recomputing LJ shifts based on new rcut\n");
98 <      painCave.isFatal = 1;
99 <      simError();
89 >  for(i=0; i < 3; i++) {
90 >    for (j=0; j < 3; j++) {
91 >      FortranHmat[3*j + i] = Hmat[i][j];
92 >      FortranHmatInv[3*j + i] = HmatInv[i][j];
93      }
94    }
102 }
95  
96 < void SimInfo::setBoxM( double theBox[9] ){
105 <  
106 <  int i, status;
107 <  double smallestBoxL, maxCutoff;
108 <
109 <  for(i=0; i<9; i++) Hmat[i] = theBox[i];
110 <  calcHmatI();
111 <  calcBoxL();
112 <
113 <  setFortranBoxSize(Hmat, HmatI, &orthoRhombic);
96 >  setFortranBoxSize(FortranHmat, FortranHmatInv, &orthoRhombic);
97  
98    smallestBoxL = boxLx;
99    if (boxLy < smallestBoxL) smallestBoxL = boxLy;
# Line 158 | Line 141 | void SimInfo::getBox(double theBox[9]) {
141   }
142  
143  
144 < void SimInfo::getBox(double theBox[9]) {
144 > void SimInfo::getBoxM (double theBox[3][3]) {
145  
146 <  int i;
147 <  for(i=0; i<9; i++) theBox[i] = Hmat[i];
146 >  int i, j;
147 >  for(i=0; i<3; i++)
148 >    for (j=0; j<3; j++) theBox[i][j] = Hmat[i][j];
149   }
166
150  
168 void SimInfo::calcHmatI( void ) {
151  
152 <  double C[3][3];
153 <  double detHmat;
154 <  int i, j, k;
173 <  double smallDiag;
174 <  double tol;
175 <  double sanity[3][3];
152 > void SimInfo::scaleBox(double scale) {
153 >  double theBox[3][3];
154 >  int i, j;
155  
156 <  // calculate the adjunct of Hmat;
156 >  // cerr << "Scaling box by " << scale << "\n";
157  
158 <  C[0][0] =  ( Hmat[4]*Hmat[8]) - (Hmat[7]*Hmat[5]);
159 <  C[1][0] = -( Hmat[1]*Hmat[8]) + (Hmat[7]*Hmat[2]);
181 <  C[2][0] =  ( Hmat[1]*Hmat[5]) - (Hmat[4]*Hmat[2]);
158 >  for(i=0; i<3; i++)
159 >    for (j=0; j<3; j++) theBox[i][j] = Hmat[i][j]*scale;
160  
161 <  C[0][1] = -( Hmat[3]*Hmat[8]) + (Hmat[6]*Hmat[5]);
184 <  C[1][1] =  ( Hmat[0]*Hmat[8]) - (Hmat[6]*Hmat[2]);
185 <  C[2][1] = -( Hmat[0]*Hmat[5]) + (Hmat[3]*Hmat[2]);
161 >  setBoxM(theBox);
162  
163 <  C[0][2] =  ( Hmat[3]*Hmat[7]) - (Hmat[6]*Hmat[4]);
188 <  C[1][2] = -( Hmat[0]*Hmat[7]) + (Hmat[6]*Hmat[1]);
189 <  C[2][2] =  ( Hmat[0]*Hmat[4]) - (Hmat[3]*Hmat[1]);
163 > }
164  
165 <  // calcutlate the determinant of Hmat
165 > void SimInfo::calcHmatInv( void ) {
166    
167 <  detHmat = 0.0;
168 <  for(i=0; i<3; i++) detHmat += Hmat[i] * C[i][0];
167 >  int i,j;
168 >  double smallDiag;
169 >  double tol;
170 >  double sanity[3][3];
171  
172 <  
197 <  // H^-1 = C^T / det(H)
198 <  
199 <  i=0;
200 <  for(j=0; j<3; j++){
201 <    for(k=0; k<3; k++){
172 >  invertMat3( Hmat, HmatInv );
173  
174 <      HmatI[i] = C[j][k] / detHmat;
204 <      i++;
205 <    }
206 <  }
174 >  // Check the inverse to make sure it is sane:
175  
176 <  // sanity check
209 <
210 <  for(i=0; i<3; i++){
211 <    for(j=0; j<3; j++){
212 <      
213 <      sanity[i][j] = 0.0;
214 <      for(k=0; k<3; k++){
215 <        sanity[i][j] += Hmat[3*k+i] * HmatI[3*j+k];
216 <      }
217 <    }
218 <  }
219 <
220 <  cerr << "sanity => \n"
221 <       << sanity[0][0] << "\t" << sanity[0][1] << "\t" << sanity [0][2] << "\n"
222 <       << sanity[1][0] << "\t" << sanity[1][1] << "\t" << sanity [1][2] << "\n"
223 <       << sanity[2][0] << "\t" << sanity[2][1] << "\t" << sanity [2][2]
224 <       << "\n";
176 >  matMul3( Hmat, HmatInv, sanity );
177      
226
178    // check to see if Hmat is orthorhombic
179    
180 <  smallDiag = Hmat[0];
181 <  if(smallDiag > Hmat[4]) smallDiag = Hmat[4];
182 <  if(smallDiag > Hmat[8]) smallDiag = Hmat[8];
180 >  smallDiag = Hmat[0][0];
181 >  if(smallDiag > Hmat[1][1]) smallDiag = Hmat[1][1];
182 >  if(smallDiag > Hmat[2][2]) smallDiag = Hmat[2][2];
183    tol = smallDiag * 1E-6;
184  
185    orthoRhombic = 1;
186 <  for(i=0; (i<9) && orthoRhombic; i++){
187 <    
188 <    if( (i%4) ){ // ignore the diagonals (0, 4, and 8)
189 <      orthoRhombic = (Hmat[i] <= tol);
186 >  
187 >  for (i = 0; i < 3; i++ ) {
188 >    for (j = 0 ; j < 3; j++) {
189 >      if (i != j) {
190 >        if (orthoRhombic) {
191 >          if (Hmat[i][j] >= tol) orthoRhombic = 0;
192 >        }        
193 >      }
194      }
195    }
241    
196   }
197  
198 + double SimInfo::matDet3(double a[3][3]) {
199 +  int i, j, k;
200 +  double determinant;
201 +
202 +  determinant = 0.0;
203 +
204 +  for(i = 0; i < 3; i++) {
205 +    j = (i+1)%3;
206 +    k = (i+2)%3;
207 +
208 +    determinant += a[0][i] * (a[1][j]*a[2][k] - a[1][k]*a[2][j]);
209 +  }
210 +
211 +  return determinant;
212 + }
213 +
214 + void SimInfo::invertMat3(double a[3][3], double b[3][3]) {
215 +  
216 +  int  i, j, k, l, m, n;
217 +  double determinant;
218 +
219 +  determinant = matDet3( a );
220 +
221 +  if (determinant == 0.0) {
222 +    sprintf( painCave.errMsg,
223 +             "Can't invert a matrix with a zero determinant!\n");
224 +    painCave.isFatal = 1;
225 +    simError();
226 +  }
227 +
228 +  for (i=0; i < 3; i++) {
229 +    j = (i+1)%3;
230 +    k = (i+2)%3;
231 +    for(l = 0; l < 3; l++) {
232 +      m = (l+1)%3;
233 +      n = (l+2)%3;
234 +      
235 +      b[l][i] = (a[j][m]*a[k][n] - a[j][n]*a[k][m]) / determinant;
236 +    }
237 +  }
238 + }
239 +
240 + void SimInfo::matMul3(double a[3][3], double b[3][3], double c[3][3]) {
241 +  double r00, r01, r02, r10, r11, r12, r20, r21, r22;
242 +
243 +  r00 = a[0][0]*b[0][0] + a[0][1]*b[1][0] + a[0][2]*b[2][0];
244 +  r01 = a[0][0]*b[0][1] + a[0][1]*b[1][1] + a[0][2]*b[2][1];
245 +  r02 = a[0][0]*b[0][2] + a[0][1]*b[1][2] + a[0][2]*b[2][2];
246 +  
247 +  r10 = a[1][0]*b[0][0] + a[1][1]*b[1][0] + a[1][2]*b[2][0];
248 +  r11 = a[1][0]*b[0][1] + a[1][1]*b[1][1] + a[1][2]*b[2][1];
249 +  r12 = a[1][0]*b[0][2] + a[1][1]*b[1][2] + a[1][2]*b[2][2];
250 +  
251 +  r20 = a[2][0]*b[0][0] + a[2][1]*b[1][0] + a[2][2]*b[2][0];
252 +  r21 = a[2][0]*b[0][1] + a[2][1]*b[1][1] + a[2][2]*b[2][1];
253 +  r22 = a[2][0]*b[0][2] + a[2][1]*b[1][2] + a[2][2]*b[2][2];
254 +  
255 +  c[0][0] = r00; c[0][1] = r01; c[0][2] = r02;
256 +  c[1][0] = r10; c[1][1] = r11; c[1][2] = r12;
257 +  c[2][0] = r20; c[2][1] = r21; c[2][2] = r22;
258 + }
259 +
260 + void SimInfo::matVecMul3(double m[3][3], double inVec[3], double outVec[3]) {
261 +  double a0, a1, a2;
262 +
263 +  a0 = inVec[0];  a1 = inVec[1];  a2 = inVec[2];
264 +
265 +  outVec[0] = m[0][0]*a0 + m[0][1]*a1 + m[0][2]*a2;
266 +  outVec[1] = m[1][0]*a0 + m[1][1]*a1 + m[1][2]*a2;
267 +  outVec[2] = m[2][0]*a0 + m[2][1]*a1 + m[2][2]*a2;
268 + }
269 +
270 + void SimInfo::transposeMat3(double in[3][3], double out[3][3]) {
271 +  double temp[3][3];
272 +  int i, j;
273 +
274 +  for (i = 0; i < 3; i++) {
275 +    for (j = 0; j < 3; j++) {
276 +      temp[j][i] = in[i][j];
277 +    }
278 +  }
279 +  for (i = 0; i < 3; i++) {
280 +    for (j = 0; j < 3; j++) {
281 +      out[i][j] = temp[i][j];
282 +    }
283 +  }
284 + }
285 +  
286 + void SimInfo::printMat3(double A[3][3] ){
287 +
288 +  std::cerr
289 +            << "[ " << A[0][0] << ", " << A[0][1] << ", " << A[0][2] << " ]\n"
290 +            << "[ " << A[1][0] << ", " << A[1][1] << ", " << A[1][2] << " ]\n"
291 +            << "[ " << A[2][0] << ", " << A[2][1] << ", " << A[2][2] << " ]\n";
292 + }
293 +
294 + void SimInfo::printMat9(double A[9] ){
295 +
296 +  std::cerr
297 +            << "[ " << A[0] << ", " << A[1] << ", " << A[2] << " ]\n"
298 +            << "[ " << A[3] << ", " << A[4] << ", " << A[5] << " ]\n"
299 +            << "[ " << A[6] << ", " << A[7] << ", " << A[8] << " ]\n";
300 + }
301 +
302   void SimInfo::calcBoxL( void ){
303  
304    double dx, dy, dz, dsq;
305    int i;
306  
307 <  // boxVol = h1 (dot) h2 (cross) h3
307 >  // boxVol = Determinant of Hmat
308  
309 <  boxVol = Hmat[0] * ( (Hmat[4]*Hmat[8]) - (Hmat[7]*Hmat[5]) )
252 <         + Hmat[1] * ( (Hmat[5]*Hmat[6]) - (Hmat[8]*Hmat[3]) )
253 <         + Hmat[2] * ( (Hmat[3]*Hmat[7]) - (Hmat[6]*Hmat[4]) );
309 >  boxVol = matDet3( Hmat );
310  
255
311    // boxLx
312    
313 <  dx = Hmat[0]; dy = Hmat[1]; dz = Hmat[2];
313 >  dx = Hmat[0][0]; dy = Hmat[1][0]; dz = Hmat[2][0];
314    dsq = dx*dx + dy*dy + dz*dz;
315    boxLx = sqrt( dsq );
316  
317    // boxLy
318    
319 <  dx = Hmat[3]; dy = Hmat[4]; dz = Hmat[5];
319 >  dx = Hmat[0][1]; dy = Hmat[1][1]; dz = Hmat[2][1];
320    dsq = dx*dx + dy*dy + dz*dz;
321    boxLy = sqrt( dsq );
322  
323    // boxLz
324    
325 <  dx = Hmat[6]; dy = Hmat[7]; dz = Hmat[8];
325 >  dx = Hmat[0][2]; dy = Hmat[1][2]; dz = Hmat[2][2];
326    dsq = dx*dx + dy*dy + dz*dz;
327    boxLz = sqrt( dsq );
328    
# Line 281 | Line 336 | void SimInfo::wrapVector( double thePos[3] ){
336  
337    if( !orthoRhombic ){
338      // calc the scaled coordinates.
339 +  
340 +
341 +    matVecMul3(HmatInv, thePos, scaled);
342      
343      for(i=0; i<3; i++)
344 <      scaled[i] =
287 <        thePos[0]*HmatI[i] + thePos[1]*HmatI[i+3] + thePos[3]*HmatI[i+6];
344 >      scaled[i] -= roundMe(scaled[i]);
345      
289    // wrap the scaled coordinates
290    
291    for(i=0; i<3; i++)
292      scaled[i] -= round(scaled[i]);
293    
346      // calc the wrapped real coordinates from the wrapped scaled coordinates
347      
348 <    for(i=0; i<3; i++)
349 <      thePos[i] =
298 <        scaled[0]*Hmat[i] + scaled[1]*Hmat[i+3] + scaled[3]*Hmat[i+6];
348 >    matVecMul3(Hmat, scaled, thePos);
349 >
350    }
351    else{
352      // calc the scaled coordinates.
353      
354      for(i=0; i<3; i++)
355 <      scaled[i] = thePos[i]*HmatI[i*4];
355 >      scaled[i] = thePos[i]*HmatInv[i][i];
356      
357      // wrap the scaled coordinates
358      
359      for(i=0; i<3; i++)
360 <      scaled[i] -= round(scaled[i]);
360 >      scaled[i] -= roundMe(scaled[i]);
361      
362      // calc the wrapped real coordinates from the wrapped scaled coordinates
363      
364      for(i=0; i<3; i++)
365 <      thePos[i] = scaled[i]*Hmat[i*4];
365 >      thePos[i] = scaled[i]*Hmat[i][i];
366    }
367      
317    
368   }
369  
370  
# Line 360 | Line 410 | void SimInfo::refreshSim(){
410    fInfo.rt = 0.0;
411    fInfo.dielect = 0.0;
412  
363  fInfo.box[0] = box_x;
364  fInfo.box[1] = box_y;
365  fInfo.box[2] = box_z;
366
413    fInfo.rlist = rList;
414    fInfo.rcut = rCut;
415  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines