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 619 by mmeineke, Tue Jul 15 22:22:41 2003 UTC vs.
Revision 872 by chrisfen, Fri Nov 21 19:31:05 2003 UTC

# Line 1 | Line 1
1 < #include <cstdlib>
2 < #include <cstring>
3 < #include <cmath>
1 > #include <stdlib.h>
2 > #include <string.h>
3 > #include <math.h>
4  
5   #include <iostream>
6   using namespace std;
# Line 20 | Line 20 | inline double roundMe( double x ){
20    return ( x >= 0 ) ? floor( x + 0.5 ) : ceil( x - 0.5 );
21   }
22            
23 + inline double min( double a, double b ){
24 +  return (a < b ) ? a : b;
25 + }
26  
27   SimInfo* currentInfo;
28  
29   SimInfo::SimInfo(){
30    excludes = NULL;
31    n_constraints = 0;
32 +  nZconstraints = 0;
33    n_oriented = 0;
34    n_dipoles = 0;
35    ndf = 0;
36    ndfRaw = 0;
37 +  nZconstraints = 0;
38    the_integrator = NULL;
39    setTemp = 0;
40    thermalTime = 0.0;
41 +  currentTime = 0.0;
42    rCut = 0.0;
43    ecr = 0.0;
44    est = 0.0;
45  
46 +  haveRcut = 0;
47 +  haveEcr = 0;
48 +  boxIsInit = 0;
49 +  
50 +  resetTime = 1e99;
51 +
52 +  orthoTolerance = 1E-6;
53 +  useInitXSstate = true;
54 +
55    usePBC = 0;
56    useLJ = 0;
57    useSticky = 0;
# Line 45 | Line 60 | SimInfo::SimInfo(){
60    useGB = 0;
61    useEAM = 0;
62  
63 +  myConfiguration = new SimState();
64 +
65    wrapMeSimInfo( this );
66   }
67  
68 +
69 + SimInfo::~SimInfo(){
70 +
71 +  delete myConfiguration;
72 +
73 +  map<string, GenericData*>::iterator i;
74 +  
75 +  for(i = properties.begin(); i != properties.end(); i++)
76 +    delete (*i).second;
77 +    
78 + }
79 +
80   void SimInfo::setBox(double newBox[3]) {
81    
82    int i, j;
# Line 66 | Line 95 | void SimInfo::setBoxM( double theBox[3][3] ){
95  
96   void SimInfo::setBoxM( double theBox[3][3] ){
97    
98 <  int i, j, status;
70 <  double smallestBoxL, maxCutoff;
98 >  int i, j;
99    double FortranHmat[9]; // to preserve compatibility with Fortran the
100                           // ordering in the array is as follows:
101                           // [ 0 3 6 ]
# Line 75 | Line 103 | void SimInfo::setBoxM( double theBox[3][3] ){
103                           // [ 2 5 8 ]
104    double FortranHmatInv[9]; // the inverted Hmat (for Fortran);
105  
106 +  if( !boxIsInit ) boxIsInit = 1;
107  
108    for(i=0; i < 3; i++)
109      for (j=0; j < 3; j++) Hmat[i][j] = theBox[i][j];
110    
82  //  cerr
83  // << "setting Hmat ->\n"
84  // << "[ " << Hmat[0][0] << ", " << Hmat[0][1] << ", " << Hmat[0][2] << " ]\n"
85  // << "[ " << Hmat[1][0] << ", " << Hmat[1][1] << ", " << Hmat[1][2] << " ]\n"
86  // << "[ " << Hmat[2][0] << ", " << Hmat[2][1] << ", " << Hmat[2][2] << " ]\n";
87
111    calcBoxL();
112    calcHmatInv();
113  
# Line 97 | Line 120 | void SimInfo::setBoxM( double theBox[3][3] ){
120  
121    setFortranBoxSize(FortranHmat, FortranHmatInv, &orthoRhombic);
122  
100  smallestBoxL = boxLx;
101  if (boxLy < smallestBoxL) smallestBoxL = boxLy;
102  if (boxLz < smallestBoxL) smallestBoxL = boxLz;
103
104  maxCutoff = smallestBoxL / 2.0;
105
106  if (rList > maxCutoff) {
107    sprintf( painCave.errMsg,
108             "New Box size is forcing neighborlist radius down to %lf\n",
109             maxCutoff );
110    painCave.isFatal = 0;
111    simError();
112
113    rList = maxCutoff;
114
115    sprintf( painCave.errMsg,
116             "New Box size is forcing cutoff radius down to %lf\n",
117             maxCutoff - 1.0 );
118    painCave.isFatal = 0;
119    simError();
120
121    rCut = rList - 1.0;
122
123    // list radius changed so we have to refresh the simulation structure.
124    refreshSim();
125  }
126
127  if( ecr > maxCutoff ){
128
129    sprintf( painCave.errMsg,
130             "New Box size is forcing electrostatic cutoff radius "
131             "down to %lf\n",
132             maxCutoff );
133    painCave.isFatal = 0;
134    simError();
135
136    ecr = maxCutoff;
137    est = 0.05 * ecr;
138
139    refreshSim();
140  }
141    
123   }
124  
125  
# Line 165 | Line 146 | void SimInfo::calcHmatInv( void ) {
146  
147   void SimInfo::calcHmatInv( void ) {
148    
149 +  int oldOrtho;
150    int i,j;
151    double smallDiag;
152    double tol;
# Line 172 | Line 154 | void SimInfo::calcHmatInv( void ) {
154  
155    invertMat3( Hmat, HmatInv );
156  
175  // Check the inverse to make sure it is sane:
176
177  matMul3( Hmat, HmatInv, sanity );
178    
157    // check to see if Hmat is orthorhombic
158    
159 <  smallDiag = Hmat[0][0];
182 <  if(smallDiag > Hmat[1][1]) smallDiag = Hmat[1][1];
183 <  if(smallDiag > Hmat[2][2]) smallDiag = Hmat[2][2];
184 <  tol = smallDiag * 1E-6;
159 >  oldOrtho = orthoRhombic;
160  
161 +  smallDiag = fabs(Hmat[0][0]);
162 +  if(smallDiag > fabs(Hmat[1][1])) smallDiag = fabs(Hmat[1][1]);
163 +  if(smallDiag > fabs(Hmat[2][2])) smallDiag = fabs(Hmat[2][2]);
164 +  tol = smallDiag * orthoTolerance;
165 +
166    orthoRhombic = 1;
167    
168    for (i = 0; i < 3; i++ ) {
169      for (j = 0 ; j < 3; j++) {
170        if (i != j) {
171          if (orthoRhombic) {
172 <          if (Hmat[i][j] >= tol) orthoRhombic = 0;
172 >          if ( fabs(Hmat[i][j]) >= tol) orthoRhombic = 0;
173          }        
174        }
175      }
176    }
177 +
178 +  if( oldOrtho != orthoRhombic ){
179 +    
180 +    if( orthoRhombic ){
181 +      sprintf( painCave.errMsg,
182 +               "Hmat is switching from Non-Orthorhombic to OrthoRhombic\n"
183 +               "       If this is a bad thing, change the orthoBoxTolerance( currently %G ).\n",
184 +               orthoTolerance);
185 +      simError();
186 +    }
187 +    else {
188 +      sprintf( painCave.errMsg,
189 +               "Hmat is switching from Orthorhombic to Non-OrthoRhombic\n"
190 +               "       If this is a bad thing, change the orthoBoxTolerance( currently %G ).\n",
191 +               orthoTolerance);
192 +      simError();
193 +    }
194 +  }
195   }
196  
197   double SimInfo::matDet3(double a[3][3]) {
# Line 300 | Line 298 | void SimInfo::calcBoxL( void ){
298              << "[ " << A[6] << ", " << A[7] << ", " << A[8] << " ]\n";
299   }
300  
301 +
302 + void SimInfo::crossProduct3(double a[3],double b[3], double out[3]){
303 +
304 +      out[0] = a[1] * b[2] - a[2] * b[1];
305 +      out[1] = a[2] * b[0] - a[0] * b[2] ;
306 +      out[2] = a[0] * b[1] - a[1] * b[0];
307 +      
308 + }
309 +
310 + double SimInfo::dotProduct3(double a[3], double b[3]){
311 +  return a[0]*b[0] + a[1]*b[1]+ a[2]*b[2];
312 + }
313 +
314 + double SimInfo::length3(double a[3]){
315 +  return sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
316 + }
317 +
318   void SimInfo::calcBoxL( void ){
319  
320    double dx, dy, dz, dsq;
306  int i;
321  
322    // boxVol = Determinant of Hmat
323  
# Line 313 | Line 327 | void SimInfo::calcBoxL( void ){
327    
328    dx = Hmat[0][0]; dy = Hmat[1][0]; dz = Hmat[2][0];
329    dsq = dx*dx + dy*dy + dz*dz;
330 <  boxLx = sqrt( dsq );
330 >  boxL[0] = sqrt( dsq );
331 >  //maxCutoff = 0.5 * boxL[0];
332  
333    // boxLy
334    
335    dx = Hmat[0][1]; dy = Hmat[1][1]; dz = Hmat[2][1];
336    dsq = dx*dx + dy*dy + dz*dz;
337 <  boxLy = sqrt( dsq );
337 >  boxL[1] = sqrt( dsq );
338 >  //if( (0.5 * boxL[1]) < maxCutoff ) maxCutoff = 0.5 * boxL[1];
339  
340 +
341    // boxLz
342    
343    dx = Hmat[0][2]; dy = Hmat[1][2]; dz = Hmat[2][2];
344    dsq = dx*dx + dy*dy + dz*dz;
345 <  boxLz = sqrt( dsq );
345 >  boxL[2] = sqrt( dsq );
346 >  //if( (0.5 * boxL[2]) < maxCutoff ) maxCutoff = 0.5 * boxL[2];
347 >
348 >  //calculate the max cutoff
349 >  maxCutoff =  calcMaxCutOff();
350    
351 +  checkCutOffs();
352 +
353   }
354  
355  
356 + double SimInfo::calcMaxCutOff(){
357 +
358 +  double ri[3], rj[3], rk[3];
359 +  double rij[3], rjk[3], rki[3];
360 +  double minDist;
361 +
362 +  ri[0] = Hmat[0][0];
363 +  ri[1] = Hmat[1][0];
364 +  ri[2] = Hmat[2][0];
365 +
366 +  rj[0] = Hmat[0][1];
367 +  rj[1] = Hmat[1][1];
368 +  rj[2] = Hmat[2][1];
369 +
370 +  rk[0] = Hmat[0][2];
371 +  rk[1] = Hmat[1][2];
372 +  rk[2] = Hmat[2][2];
373 +  
374 +  crossProduct3(ri,rj, rij);
375 +  distXY = dotProduct3(rk,rij) / length3(rij);
376 +
377 +  crossProduct3(rj,rk, rjk);
378 +  distYZ = dotProduct3(ri,rjk) / length3(rjk);
379 +
380 +  crossProduct3(rk,ri, rki);
381 +  distZX = dotProduct3(rj,rki) / length3(rki);
382 +
383 +  minDist = min(min(distXY, distYZ), distZX);
384 +  return minDist/2;
385 +  
386 + }
387 +
388   void SimInfo::wrapVector( double thePos[3] ){
389  
390 <  int i, j, k;
390 >  int i;
391    double scaled[3];
392  
393    if( !orthoRhombic ){
# Line 370 | Line 425 | int SimInfo::getNDF(){
425  
426  
427   int SimInfo::getNDF(){
428 <  int ndf_local, ndf;
428 >  int ndf_local;
429    
430    ndf_local = 3 * n_atoms + 3 * n_oriented - n_constraints;
431  
# Line 380 | Line 435 | int SimInfo::getNDF(){
435    ndf = ndf_local;
436   #endif
437  
438 <  ndf = ndf - 3;
438 >  ndf = ndf - 3 - nZconstraints;
439  
440    return ndf;
441   }
442  
443   int SimInfo::getNDFraw() {
444 <  int ndfRaw_local, ndfRaw;
444 >  int ndfRaw_local;
445  
446    // Raw degrees of freedom that we have to set
447    ndfRaw_local = 3 * n_atoms + 3 * n_oriented;
# Line 399 | Line 454 | int SimInfo::getNDFraw() {
454  
455    return ndfRaw;
456   }
457 <
457 >
458 > int SimInfo::getNDFtranslational() {
459 >  int ndfTrans_local;
460 >
461 >  ndfTrans_local = 3 * n_atoms - n_constraints;
462 >
463 > #ifdef IS_MPI
464 >  MPI_Allreduce(&ndfTrans_local,&ndfTrans,1,MPI_INT,MPI_SUM, MPI_COMM_WORLD);
465 > #else
466 >  ndfTrans = ndfTrans_local;
467 > #endif
468 >
469 >  ndfTrans = ndfTrans - 3 - nZconstraints;
470 >
471 >  return ndfTrans;
472 > }
473 >
474   void SimInfo::refreshSim(){
475  
476    simtype fInfo;
477    int isError;
478    int n_global;
479    int* excl;
480 <  
410 <  fInfo.rrf = 0.0;
411 <  fInfo.rt = 0.0;
480 >
481    fInfo.dielect = 0.0;
482  
414  fInfo.rlist = rList;
415  fInfo.rcut = rCut;
416
483    if( useDipole ){
418    fInfo.rrf = ecr;
419    fInfo.rt = ecr - est;
484      if( useReactionField )fInfo.dielect = dielectric;
485    }
486  
# Line 462 | Line 526 | void SimInfo::refreshSim(){
526  
527    this->ndf = this->getNDF();
528    this->ndfRaw = this->getNDFraw();
529 +  this->ndfTrans = this->getNDFtranslational();
530 + }
531  
532 + void SimInfo::setDefaultRcut( double theRcut ){
533 +
534 +  haveRcut = 1;
535 +  rCut = theRcut;
536 +
537 +  ( rCut > ecr )? rList = rCut + 1.0: rList = ecr + 1.0;
538 +
539 +  notifyFortranCutOffs( &rCut, &rList, &ecr, &est );
540   }
541  
542 + void SimInfo::setDefaultEcr( double theEcr ){
543 +
544 +  haveEcr = 1;
545 +  ecr = theEcr;
546 +  
547 +  ( rCut > ecr )? rList = rCut + 1.0: rList = ecr + 1.0;
548 +
549 +  notifyFortranCutOffs( &rCut, &rList, &ecr, &est );
550 + }
551 +
552 + void SimInfo::setDefaultEcr( double theEcr, double theEst ){
553 +
554 +  est = theEst;
555 +  setDefaultEcr( theEcr );
556 + }
557 +
558 +
559 + void SimInfo::checkCutOffs( void ){
560 +  
561 +  if( boxIsInit ){
562 +    
563 +    //we need to check cutOffs against the box
564 +    
565 +    if( rCut > maxCutoff ){
566 +      sprintf( painCave.errMsg,
567 +               "Box size is too small for the long range cutoff radius, "
568 +               "%lf, at time %lf\n",
569 +               rCut, currentTime );
570 +      painCave.isFatal = 1;
571 +      simError();
572 +    }
573 +    
574 +    if( haveEcr ){
575 +      if( ecr > maxCutoff ){
576 +        sprintf( painCave.errMsg,
577 +                 "Box size is too small for the electrostatic cutoff radius, "
578 +                 "%lf, at time %lf\n",
579 +                 ecr, currentTime );
580 +        painCave.isFatal = 1;
581 +        simError();
582 +      }
583 +    }
584 +  } else {
585 +    // initialize this stuff before using it, OK?
586 +    sprintf( painCave.errMsg,
587 +             "Trying to check cutoffs without a box. Be smarter.\n" );
588 +    painCave.isFatal = 1;
589 +    simError();      
590 +  }
591 +  
592 + }
593 +
594 + void SimInfo::addProperty(GenericData* prop){
595 +
596 +  map<string, GenericData*>::iterator result;
597 +  result = properties.find(prop->getID());
598 +  
599 +  //we can't simply use  properties[prop->getID()] = prop,
600 +  //it will cause memory leak if we already contain a propery which has the same name of prop
601 +  
602 +  if(result != properties.end()){
603 +    
604 +    delete (*result).second;
605 +    (*result).second = prop;
606 +      
607 +  }
608 +  else{
609 +
610 +    properties[prop->getID()] = prop;
611 +
612 +  }
613 +    
614 + }
615 +
616 + GenericData* SimInfo::getProperty(const string& propName){
617 +
618 +  map<string, GenericData*>::iterator result;
619 +  
620 +  //string lowerCaseName = ();
621 +  
622 +  result = properties.find(propName);
623 +  
624 +  if(result != properties.end())
625 +    return (*result).second;  
626 +  else  
627 +    return NULL;  
628 + }
629 +
630 + vector<GenericData*> SimInfo::getProperties(){
631 +
632 +  vector<GenericData*> result;
633 +  map<string, GenericData*>::iterator i;
634 +  
635 +  for(i = properties.begin(); i != properties.end(); i++)
636 +    result.push_back((*i).second);
637 +    
638 +  return result;
639 + }
640 +
641 + double SimInfo::matTrace3(double m[3][3]){
642 +  double trace;
643 +  trace = m[0][0] + m[1][1] + m[2][2];
644 +
645 +  return trace;
646 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines