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 586 by mmeineke, Wed Jul 9 22:14:06 2003 UTC vs.
Revision 626 by mmeineke, Wed Jul 16 21:30:56 2003 UTC

# Line 34 | Line 34 | SimInfo::SimInfo(){
34    setTemp = 0;
35    thermalTime = 0.0;
36    rCut = 0.0;
37 +  ecr = 0.0;
38 +  est = 0.0;
39 +  oldEcr = 0.0;
40 +  oldRcut = 0.0;
41  
42 +  haveOrigRcut = 0;
43 +  haveOrigEcr = 0;
44 +  boxIsInit = 0;
45 +  
46 +  
47 +
48    usePBC = 0;
49    useLJ = 0;
50    useSticky = 0;
# Line 48 | Line 58 | void SimInfo::setBox(double newBox[3]) {
58  
59   void SimInfo::setBox(double newBox[3]) {
60    
61 <  int i;
62 <  double tempMat[9];
61 >  int i, j;
62 >  double tempMat[3][3];
63  
64 <  for(i=0; i<9; i++) tempMat[i] = 0.0;;
64 >  for(i=0; i<3; i++)
65 >    for (j=0; j<3; j++) tempMat[i][j] = 0.0;;
66  
67 <  tempMat[0] = newBox[0];
68 <  tempMat[4] = newBox[1];
69 <  tempMat[8] = newBox[2];
67 >  tempMat[0][0] = newBox[0];
68 >  tempMat[1][1] = newBox[1];
69 >  tempMat[2][2] = newBox[2];
70  
71    setBoxM( tempMat );
72  
73   }
74  
75 < void SimInfo::setBoxM( double theBox[9] ){
75 > void SimInfo::setBoxM( double theBox[3][3] ){
76    
77 <  int i, status;
77 >  int i, j, status;
78    double smallestBoxL, maxCutoff;
79 +  double FortranHmat[9]; // to preserve compatibility with Fortran the
80 +                         // ordering in the array is as follows:
81 +                         // [ 0 3 6 ]
82 +                         // [ 1 4 7 ]
83 +                         // [ 2 5 8 ]
84 +  double FortranHmatInv[9]; // the inverted Hmat (for Fortran);
85  
86 <  for(i=0; i<9; i++) Hmat[i] = theBox[i];
86 >  
87 >  if( !boxIsInit ) boxIsInit = 1;
88  
89 <  cerr
90 <    << "setting Hmat ->\n"
91 <    << "[ " << Hmat[0] << ", " << Hmat[3] << ", " << Hmat[6] << " ]\n"
74 <    << "[ " << Hmat[1] << ", " << Hmat[4] << ", " << Hmat[7] << " ]\n"
75 <    << "[ " << Hmat[2] << ", " << Hmat[5] << ", " << Hmat[8] << " ]\n";
76 <
77 <  calcHmatI();
89 >  for(i=0; i < 3; i++)
90 >    for (j=0; j < 3; j++) Hmat[i][j] = theBox[i][j];
91 >  
92    calcBoxL();
93 +  calcHmatInv();
94  
95 <
96 <
97 <  setFortranBoxSize(Hmat, HmatI, &orthoRhombic);
98 <
84 <  smallestBoxL = boxLx;
85 <  if (boxLy < smallestBoxL) smallestBoxL = boxLy;
86 <  if (boxLz < smallestBoxL) smallestBoxL = boxLz;
87 <
88 <  maxCutoff = smallestBoxL / 2.0;
89 <
90 <  if (rList > maxCutoff) {
91 <    sprintf( painCave.errMsg,
92 <             "New Box size is forcing neighborlist radius down to %lf\n",
93 <             maxCutoff );
94 <    painCave.isFatal = 0;
95 <    simError();
96 <
97 <    rList = maxCutoff;
98 <
99 <    sprintf( painCave.errMsg,
100 <             "New Box size is forcing cutoff radius down to %lf\n",
101 <             maxCutoff - 1.0 );
102 <    painCave.isFatal = 0;
103 <    simError();
104 <
105 <    rCut = rList - 1.0;
106 <
107 <    // list radius changed so we have to refresh the simulation structure.
108 <    refreshSim();
109 <  }
110 <
111 <  if (rCut > maxCutoff) {
112 <    sprintf( painCave.errMsg,
113 <             "New Box size is forcing cutoff radius down to %lf\n",
114 <             maxCutoff );
115 <    painCave.isFatal = 0;
116 <    simError();
117 <
118 <    status = 0;
119 <    LJ_new_rcut(&rCut, &status);
120 <    if (status != 0) {
121 <      sprintf( painCave.errMsg,
122 <               "Error in recomputing LJ shifts based on new rcut\n");
123 <      painCave.isFatal = 1;
124 <      simError();
95 >  for(i=0; i < 3; i++) {
96 >    for (j=0; j < 3; j++) {
97 >      FortranHmat[3*j + i] = Hmat[i][j];
98 >      FortranHmatInv[3*j + i] = HmatInv[i][j];
99      }
100    }
101 +
102 +  setFortranBoxSize(FortranHmat, FortranHmatInv, &orthoRhombic);
103 +
104   }
105  
106  
107 < void SimInfo::getBoxM (double theBox[9]) {
107 > void SimInfo::getBoxM (double theBox[3][3]) {
108  
109 <  int i;
110 <  for(i=0; i<9; i++) theBox[i] = Hmat[i];
109 >  int i, j;
110 >  for(i=0; i<3; i++)
111 >    for (j=0; j<3; j++) theBox[i][j] = Hmat[i][j];
112   }
113  
114  
115   void SimInfo::scaleBox(double scale) {
116 <  double theBox[9];
117 <  int i;
116 >  double theBox[3][3];
117 >  int i, j;
118  
119 <  cerr << "Scaling box by " << scale << "\n";
119 >  // cerr << "Scaling box by " << scale << "\n";
120  
121 <  for(i=0; i<9; i++) theBox[i] = Hmat[i]*scale;
121 >  for(i=0; i<3; i++)
122 >    for (j=0; j<3; j++) theBox[i][j] = Hmat[i][j]*scale;
123  
124    setBoxM(theBox);
125  
126   }
127  
128 < void SimInfo::calcHmatI( void ) {
129 <
130 <  double C[3][3];
152 <  double detHmat;
153 <  int i, j, k;
128 > void SimInfo::calcHmatInv( void ) {
129 >  
130 >  int i,j;
131    double smallDiag;
132    double tol;
133    double sanity[3][3];
134  
135 <  // calculate the adjunct of Hmat;
135 >  invertMat3( Hmat, HmatInv );
136  
137 <  C[0][0] =  ( Hmat[4]*Hmat[8]) - (Hmat[7]*Hmat[5]);
161 <  C[1][0] = -( Hmat[1]*Hmat[8]) + (Hmat[7]*Hmat[2]);
162 <  C[2][0] =  ( Hmat[1]*Hmat[5]) - (Hmat[4]*Hmat[2]);
137 >  // Check the inverse to make sure it is sane:
138  
139 <  C[0][1] = -( Hmat[3]*Hmat[8]) + (Hmat[6]*Hmat[5]);
140 <  C[1][1] =  ( Hmat[0]*Hmat[8]) - (Hmat[6]*Hmat[2]);
141 <  C[2][1] = -( Hmat[0]*Hmat[5]) + (Hmat[3]*Hmat[2]);
167 <
168 <  C[0][2] =  ( Hmat[3]*Hmat[7]) - (Hmat[6]*Hmat[4]);
169 <  C[1][2] = -( Hmat[0]*Hmat[7]) + (Hmat[6]*Hmat[1]);
170 <  C[2][2] =  ( Hmat[0]*Hmat[4]) - (Hmat[3]*Hmat[1]);
171 <
172 <  // calcutlate the determinant of Hmat
139 >  matMul3( Hmat, HmatInv, sanity );
140 >    
141 >  // check to see if Hmat is orthorhombic
142    
143 <  detHmat = 0.0;
144 <  for(i=0; i<3; i++) detHmat += Hmat[i] * C[i][0];
143 >  smallDiag = Hmat[0][0];
144 >  if(smallDiag > Hmat[1][1]) smallDiag = Hmat[1][1];
145 >  if(smallDiag > Hmat[2][2]) smallDiag = Hmat[2][2];
146 >  tol = smallDiag * 1E-6;
147  
148 +  orthoRhombic = 1;
149    
150 <  // H^-1 = C^T / det(H)
151 <  
152 <  i=0;
153 <  for(j=0; j<3; j++){
154 <    for(k=0; k<3; k++){
155 <
156 <      HmatI[i] = C[j][k] / detHmat;
185 <      i++;
150 >  for (i = 0; i < 3; i++ ) {
151 >    for (j = 0 ; j < 3; j++) {
152 >      if (i != j) {
153 >        if (orthoRhombic) {
154 >          if (Hmat[i][j] >= tol) orthoRhombic = 0;
155 >        }        
156 >      }
157      }
158    }
159 + }
160  
161 <  // sanity check
161 > double SimInfo::matDet3(double a[3][3]) {
162 >  int i, j, k;
163 >  double determinant;
164  
165 <  for(i=0; i<3; i++){
166 <    for(j=0; j<3; j++){
165 >  determinant = 0.0;
166 >
167 >  for(i = 0; i < 3; i++) {
168 >    j = (i+1)%3;
169 >    k = (i+2)%3;
170 >
171 >    determinant += a[0][i] * (a[1][j]*a[2][k] - a[1][k]*a[2][j]);
172 >  }
173 >
174 >  return determinant;
175 > }
176 >
177 > void SimInfo::invertMat3(double a[3][3], double b[3][3]) {
178 >  
179 >  int  i, j, k, l, m, n;
180 >  double determinant;
181 >
182 >  determinant = matDet3( a );
183 >
184 >  if (determinant == 0.0) {
185 >    sprintf( painCave.errMsg,
186 >             "Can't invert a matrix with a zero determinant!\n");
187 >    painCave.isFatal = 1;
188 >    simError();
189 >  }
190 >
191 >  for (i=0; i < 3; i++) {
192 >    j = (i+1)%3;
193 >    k = (i+2)%3;
194 >    for(l = 0; l < 3; l++) {
195 >      m = (l+1)%3;
196 >      n = (l+2)%3;
197        
198 <      sanity[i][j] = 0.0;
195 <      for(k=0; k<3; k++){
196 <        sanity[i][j] += Hmat[3*k+i] * HmatI[3*j+k];
197 <      }
198 >      b[l][i] = (a[j][m]*a[k][n] - a[j][n]*a[k][m]) / determinant;
199      }
200    }
201 + }
202  
203 <  cerr << "sanity => \n"
204 <       << sanity[0][0] << "\t" << sanity[0][1] << "\t" << sanity [0][2] << "\n"
203 <       << sanity[1][0] << "\t" << sanity[1][1] << "\t" << sanity [1][2] << "\n"
204 <       << sanity[2][0] << "\t" << sanity[2][1] << "\t" << sanity [2][2]
205 <       << "\n";
206 <    
203 > void SimInfo::matMul3(double a[3][3], double b[3][3], double c[3][3]) {
204 >  double r00, r01, r02, r10, r11, r12, r20, r21, r22;
205  
206 <  // check to see if Hmat is orthorhombic
206 >  r00 = a[0][0]*b[0][0] + a[0][1]*b[1][0] + a[0][2]*b[2][0];
207 >  r01 = a[0][0]*b[0][1] + a[0][1]*b[1][1] + a[0][2]*b[2][1];
208 >  r02 = a[0][0]*b[0][2] + a[0][1]*b[1][2] + a[0][2]*b[2][2];
209    
210 <  smallDiag = Hmat[0];
211 <  if(smallDiag > Hmat[4]) smallDiag = Hmat[4];
212 <  if(smallDiag > Hmat[8]) smallDiag = Hmat[8];
213 <  tol = smallDiag * 1E-6;
210 >  r10 = a[1][0]*b[0][0] + a[1][1]*b[1][0] + a[1][2]*b[2][0];
211 >  r11 = a[1][0]*b[0][1] + a[1][1]*b[1][1] + a[1][2]*b[2][1];
212 >  r12 = a[1][0]*b[0][2] + a[1][1]*b[1][2] + a[1][2]*b[2][2];
213 >  
214 >  r20 = a[2][0]*b[0][0] + a[2][1]*b[1][0] + a[2][2]*b[2][0];
215 >  r21 = a[2][0]*b[0][1] + a[2][1]*b[1][1] + a[2][2]*b[2][1];
216 >  r22 = a[2][0]*b[0][2] + a[2][1]*b[1][2] + a[2][2]*b[2][2];
217 >  
218 >  c[0][0] = r00; c[0][1] = r01; c[0][2] = r02;
219 >  c[1][0] = r10; c[1][1] = r11; c[1][2] = r12;
220 >  c[2][0] = r20; c[2][1] = r21; c[2][2] = r22;
221 > }
222  
223 <  orthoRhombic = 1;
224 <  for(i=0; (i<9) && orthoRhombic; i++){
225 <    
226 <    if( (i%4) ){ // ignore the diagonals (0, 4, and 8)
227 <      orthoRhombic = (Hmat[i] <= tol);
223 > void SimInfo::matVecMul3(double m[3][3], double inVec[3], double outVec[3]) {
224 >  double a0, a1, a2;
225 >
226 >  a0 = inVec[0];  a1 = inVec[1];  a2 = inVec[2];
227 >
228 >  outVec[0] = m[0][0]*a0 + m[0][1]*a1 + m[0][2]*a2;
229 >  outVec[1] = m[1][0]*a0 + m[1][1]*a1 + m[1][2]*a2;
230 >  outVec[2] = m[2][0]*a0 + m[2][1]*a1 + m[2][2]*a2;
231 > }
232 >
233 > void SimInfo::transposeMat3(double in[3][3], double out[3][3]) {
234 >  double temp[3][3];
235 >  int i, j;
236 >
237 >  for (i = 0; i < 3; i++) {
238 >    for (j = 0; j < 3; j++) {
239 >      temp[j][i] = in[i][j];
240      }
241    }
242 <    
242 >  for (i = 0; i < 3; i++) {
243 >    for (j = 0; j < 3; j++) {
244 >      out[i][j] = temp[i][j];
245 >    }
246 >  }
247   }
248 +  
249 + void SimInfo::printMat3(double A[3][3] ){
250  
251 +  std::cerr
252 +            << "[ " << A[0][0] << ", " << A[0][1] << ", " << A[0][2] << " ]\n"
253 +            << "[ " << A[1][0] << ", " << A[1][1] << ", " << A[1][2] << " ]\n"
254 +            << "[ " << A[2][0] << ", " << A[2][1] << ", " << A[2][2] << " ]\n";
255 + }
256 +
257 + void SimInfo::printMat9(double A[9] ){
258 +
259 +  std::cerr
260 +            << "[ " << A[0] << ", " << A[1] << ", " << A[2] << " ]\n"
261 +            << "[ " << A[3] << ", " << A[4] << ", " << A[5] << " ]\n"
262 +            << "[ " << A[6] << ", " << A[7] << ", " << A[8] << " ]\n";
263 + }
264 +
265   void SimInfo::calcBoxL( void ){
266  
267    double dx, dy, dz, dsq;
268    int i;
269  
270 <  // boxVol = h1 (dot) h2 (cross) h3
270 >  // boxVol = Determinant of Hmat
271  
272 <  boxVol = Hmat[0] * ( (Hmat[4]*Hmat[8]) - (Hmat[7]*Hmat[5]) )
233 <         + Hmat[1] * ( (Hmat[5]*Hmat[6]) - (Hmat[8]*Hmat[3]) )
234 <         + Hmat[2] * ( (Hmat[3]*Hmat[7]) - (Hmat[6]*Hmat[4]) );
272 >  boxVol = matDet3( Hmat );
273  
236
274    // boxLx
275    
276 <  dx = Hmat[0]; dy = Hmat[1]; dz = Hmat[2];
276 >  dx = Hmat[0][0]; dy = Hmat[1][0]; dz = Hmat[2][0];
277    dsq = dx*dx + dy*dy + dz*dz;
278 <  boxLx = sqrt( dsq );
278 >  boxL[0] = sqrt( dsq );
279 >  maxCutoff = 0.5 * boxL[0];
280  
281    // boxLy
282    
283 <  dx = Hmat[3]; dy = Hmat[4]; dz = Hmat[5];
283 >  dx = Hmat[0][1]; dy = Hmat[1][1]; dz = Hmat[2][1];
284    dsq = dx*dx + dy*dy + dz*dz;
285 <  boxLy = sqrt( dsq );
285 >  boxL[1] = sqrt( dsq );
286 >  if( (0.5 * boxL[1]) < maxCutoff ) maxCutoff = 0.5 * boxL[1];
287  
288    // boxLz
289    
290 <  dx = Hmat[6]; dy = Hmat[7]; dz = Hmat[8];
290 >  dx = Hmat[0][2]; dy = Hmat[1][2]; dz = Hmat[2][2];
291    dsq = dx*dx + dy*dy + dz*dz;
292 <  boxLz = sqrt( dsq );
293 <  
292 >  boxL[2] = sqrt( dsq );
293 >  if( (0.5 * boxL[2]) < maxCutoff ) maxCutoff = 0.5 * boxL[2];
294 >
295   }
296  
297  
# Line 262 | Line 302 | void SimInfo::wrapVector( double thePos[3] ){
302  
303    if( !orthoRhombic ){
304      // calc the scaled coordinates.
305 +  
306 +
307 +    matVecMul3(HmatInv, thePos, scaled);
308      
309      for(i=0; i<3; i++)
267      scaled[i] =
268        thePos[0]*HmatI[i] + thePos[1]*HmatI[i+3] + thePos[3]*HmatI[i+6];
269    
270    // wrap the scaled coordinates
271    
272    for(i=0; i<3; i++)
310        scaled[i] -= roundMe(scaled[i]);
311      
312      // calc the wrapped real coordinates from the wrapped scaled coordinates
313      
314 <    for(i=0; i<3; i++)
315 <      thePos[i] =
279 <        scaled[0]*Hmat[i] + scaled[1]*Hmat[i+3] + scaled[2]*Hmat[i+6];
314 >    matVecMul3(Hmat, scaled, thePos);
315 >
316    }
317    else{
318      // calc the scaled coordinates.
319      
320      for(i=0; i<3; i++)
321 <      scaled[i] = thePos[i]*HmatI[i*4];
321 >      scaled[i] = thePos[i]*HmatInv[i][i];
322      
323      // wrap the scaled coordinates
324      
# Line 292 | Line 328 | void SimInfo::wrapVector( double thePos[3] ){
328      // calc the wrapped real coordinates from the wrapped scaled coordinates
329      
330      for(i=0; i<3; i++)
331 <      thePos[i] = scaled[i]*Hmat[i*4];
331 >      thePos[i] = scaled[i]*Hmat[i][i];
332    }
333      
298    
334   }
335  
336  
# Line 336 | Line 371 | void SimInfo::refreshSim(){
371    int isError;
372    int n_global;
373    int* excl;
374 <  
340 <  fInfo.rrf = 0.0;
341 <  fInfo.rt = 0.0;
374 >
375    fInfo.dielect = 0.0;
376  
344  fInfo.rlist = rList;
345  fInfo.rcut = rCut;
346
377    if( useDipole ){
348    fInfo.rrf = ecr;
349    fInfo.rt = ecr - est;
378      if( useReactionField )fInfo.dielect = dielectric;
379    }
380  
# Line 395 | Line 423 | void SimInfo::refreshSim(){
423  
424   }
425  
426 +
427 + void SimInfo::setRcut( double theRcut ){
428 +
429 +  if( !haveOrigRcut ){
430 +    haveOrigRcut = 1;
431 +    origRcut = theRcut;
432 +  }
433 +
434 +  rCut = theRcut;
435 +  checkCutOffs();
436 + }
437 +
438 + void SimInfo::setEcr( double theEcr ){
439 +
440 +  if( !haveOrigEcr ){
441 +    haveOrigEcr = 1;
442 +    origEcr = theEcr;
443 +  }
444 +
445 +  ecr = theEcr;
446 +  checkCutOffs();
447 + }
448 +
449 + void SimInfo::setEcr( double theEcr, double theEst ){
450 +
451 +  est = theEst;
452 +  setEcr( theEcr );
453 + }
454 +
455 +
456 + void SimInfo::checkCutOffs( void ){
457 +
458 +  int cutChanged = 0;
459 +
460 +  if( boxIsInit ){
461 +    
462 +    //we need to check cutOffs against the box
463 +    
464 +    if( maxCutoff > rCut ){
465 +      if( rCut < origRcut ){
466 +        rCut = origRcut;
467 +        if (rCut > maxCutoff) rCut = maxCutoff;
468 +        
469 +        sprintf( painCave.errMsg,
470 +                 "New Box size is setting the long range cutoff radius "
471 +                 "to %lf\n",
472 +                 rCut );
473 +        painCave.isFatal = 0;
474 +        simError();
475 +      }
476 +    }
477 +
478 +    if( maxCutoff > ecr ){
479 +      if( ecr < origEcr ){
480 +        rCut = origEcr;
481 +        if (ecr > maxCutoff) ecr = maxCutoff;
482 +        
483 +        sprintf( painCave.errMsg,
484 +                 "New Box size is setting the electrostaticCutoffRadius "
485 +                 "to %lf\n",
486 +                 ecr );
487 +        painCave.isFatal = 0;
488 +        simError();
489 +      }
490 +    }
491 +
492 +
493 +    if (rCut > maxCutoff) {
494 +      sprintf( painCave.errMsg,
495 +               "New Box size is setting the long range cutoff radius "
496 +               "to %lf\n",
497 +               maxCutoff );
498 +      painCave.isFatal = 0;
499 +      simError();
500 +      rCut = maxCutoff;
501 +    }
502 +
503 +    if( ecr > maxCutoff){
504 +      sprintf( painCave.errMsg,
505 +               "New Box size is setting the electrostaticCutoffRadius "
506 +               "to %lf\n",
507 +               maxCutoff  );
508 +      painCave.isFatal = 0;
509 +      simError();      
510 +      ecr = maxCutoff;
511 +    }
512 +
513 +    
514 +  }
515 +  
516 +
517 +  if( (oldEcr != ecr) || ( oldRcut != rCut ) ) cutChanged = 1;
518 +
519 +  // rlist is the 1.0 plus max( rcut, ecr )
520 +  
521 +  ( rCut > ecr )? rList = rCut + 1.0: rList = ecr + 1.0;
522 +
523 +  if( cutChanged ){
524 +    
525 +    notifyFortranCutOffs( &rCut, &rList, &ecr, &est );
526 +  }
527 +
528 +  oldEcr = ecr;
529 +  oldRcut = rCut;
530 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines