ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-2.0/src/integrators/NPTxyz.cpp
(Generate patch)

Comparing branches/new_design/OOPSE-2.0/src/integrators/NPTxyz.cpp (file contents):
Revision 1701 by tim, Wed Nov 3 16:08:43 2004 UTC vs.
Revision 1822 by tim, Thu Dec 2 02:08:29 2004 UTC

# Line 1 | Line 1
1 #include <math.h>
2 #include "math/MatVec3.h"
3 #include "primitives/Atom.hpp"
4 #include "primitives/SRI.hpp"
5 #include "primitives/AbstractClasses.hpp"
1   #include "brains/SimInfo.hpp"
7 #include "UseTheForce/ForceFields.hpp"
2   #include "brains/Thermo.hpp"
3 < #include "io/ReadWrite.hpp"
4 < #include "integrators/Integrator.hpp"
3 > #include "integrators/NPTxyz.hpp"
4 > #include "primitives/Molecule.hpp"
5 > #include "utils/OOPSEConstant.hpp"
6   #include "utils/simError.h"
7  
13 #ifdef IS_MPI
14 #include "brains/mpiSimulation.hpp"
15 #endif
16
8   // Basic non-isotropic thermostating and barostating via the Melchionna
9   // modification of the Hoover algorithm:
10   //
# Line 24 | Line 15 | template<typename T> NPTxyz<T>::NPTxyz ( SimInfo *theI
15   //
16   //    Hoover, W. G., 1986, Phys. Rev. A, 34, 2499.
17  
18 < template<typename T> NPTxyz<T>::NPTxyz ( SimInfo *theInfo, ForceFields* the_ff):
19 <  T( theInfo, the_ff )
20 < {
18 > namespace oopse {
19 > /*
20 > NPTxyz::NPTxyz (SimInfo* info): NPT(info) {
21    GenericData* data;
22    DoubleVectorGenericData * etaValue;
23    int i,j;
# Line 34 | Line 25 | template<typename T> NPTxyz<T>::NPTxyz ( SimInfo *theI
25    for(i = 0; i < 3; i++){
26      for (j = 0; j < 3; j++){
27  
28 <      eta[i][j] = 0.0;
29 <      oldEta[i][j] = 0.0;
28 >      eta(i, j) = 0.0;
29 >      oldEta(i, j) = 0.0;
30      }
31    }
32  
# Line 51 | Line 42 | template<typename T> NPTxyz<T>::NPTxyz ( SimInfo *theI
42          
43          for(i = 0; i < 3; i++){
44            for (j = 0; j < 3; j++){
45 <            eta[i][j] = (*etaValue)[3*i+j];
46 <            oldEta[i][j] = eta[i][j];
45 >            eta(i, j) = (*etaValue)[3*i+j];
46 >            oldEta(i, j) = eta(i, j);
47            }
48          }
49        }
# Line 60 | Line 51 | template<typename T> NPTxyz<T>::~NPTxyz() {
51    }
52   }
53  
63 template<typename T> NPTxyz<T>::~NPTxyz() {
54  
65  // empty for now
66 }
55  
56 < template<typename T> void NPTxyz<T>::resetIntegrator() {
56 > void NPTxyz::evolveEtaA() {
57  
58    int i, j;
59  
60 <  for(i = 0; i < 3; i++)
61 <    for (j = 0; j < 3; j++)
62 <      eta[i][j] = 0.0;
63 <
64 <  T::resetIntegrator();
65 < }
66 <
67 < template<typename T> void NPTxyz<T>::evolveEtaA() {
80 <
81 <  int i, j;
82 <
83 <  for(i = 0; i < 3; i ++){
84 <    for(j = 0; j < 3; j++){
85 <      if( i == j)
86 <        eta[i][j] += dt2 *  instaVol *
87 <          (press[i][j] - targetPressure/p_convert) / (NkBT*tb2);
88 <      else
89 <        eta[i][j] = 0.0;
60 >    for(i = 0; i < 3; i ++){
61 >        for(j = 0; j < 3; j++){
62 >            if( i == j) {
63 >                eta(i, j) += dt2 *  instaVol *(press(i, j) - targetPressure/OOPSEConstant::pressureConvert) / (NkBT*tb2);
64 >            } else {
65 >                eta(i, j) = 0.0;
66 >            }
67 >        }
68      }
91  }
69  
70 <  for(i = 0; i < 3; i++)
71 <    for (j = 0; j < 3; j++)
72 <      oldEta[i][j] = eta[i][j];
70 >    for(i = 0; i < 3; i++) {
71 >        for (j = 0; j < 3; j++) {
72 >            oldEta(i, j) = eta(i, j);
73 >        }
74 >    }
75 >    
76   }
77  
78 < template<typename T> void NPTxyz<T>::evolveEtaB() {
78 > void NPTxyz::evolveEtaB() {
79  
80    int i,j;
81  
82    for(i = 0; i < 3; i++)
83      for (j = 0; j < 3; j++)
84 <      prevEta[i][j] = eta[i][j];
84 >      prevEta(i, j) = eta(i, j);
85  
86    for(i = 0; i < 3; i ++){
87      for(j = 0; j < 3; j++){
88        if( i == j) {
89 <        eta[i][j] = oldEta[i][j] + dt2 *  instaVol *
90 <          (press[i][j] - targetPressure/p_convert) / (NkBT*tb2);
89 >        eta(i, j) = oldEta(i, j) + dt2 *  instaVol *
90 >          (press(i, j) - targetPressure/OOPSEConstant::pressureConvert) / (NkBT*tb2);
91        } else {
92 <        eta[i][j] = 0.0;
92 >        eta(i, j) = 0.0;
93        }
94      }
95    }
96   }
97  
98 < template<typename T> void NPTxyz<T>::calcVelScale(void) {
98 > void NPTxyz::calcVelScale(void) {
99    int i,j;
100  
101    for (i = 0; i < 3; i++ ) {
102      for (j = 0; j < 3; j++ ) {
103 <      vScale[i][j] = eta[i][j];
103 >      vScale(i, j) = eta(i, j);
104  
105        if (i == j) {
106 <        vScale[i][j] += chi;
106 >        vScale(i, j) += chi;
107        }
108      }
109    }
110   }
111  
112 < template<typename T> void NPTxyz<T>::getVelScaleA(double sc[3], double vel[3]) {
113 <  matVecMul3( vScale, vel, sc );
112 >
113 > void NPTxyz::getVelScaleA(Vector3d& sc, const Vector3d& vel) {
114 >    sc = vScale * vel;
115   }
116  
117 < template<typename T> void NPTxyz<T>::getVelScaleB(double sc[3], int index ){
118 <  int j;
119 <  double myVel[3];
117 > void NPTxyz::getVelScaleB(Vector3d& sc, int index ) {
118 >  sc = vScale * oldVel[index];
119 > }
120  
121 <  for (j = 0; j < 3; j++)
122 <    myVel[j] = oldVel[3*index + j];
121 > void NPTxyz::getPosScale(const Vector3d& pos, const Vector3d& COM,
122 >                           int index, Vector3d& sc) {
123 >                          
124 >    Vector3d rj = (oldPos[index] + pos[j])/2.0 -COM;
125 >    sc = eta * rj;
126 > }
127  
128 <  matVecMul3( vScale, myVel, sc );
128 > bool NPTxyz::etaConverged() {
129 >  int i;
130 >  double diffEta, sumEta;
131 >
132 >  sumEta = 0;
133 >  for(i = 0; i < 3; i++)
134 >    sumEta += pow(prevEta(i, i) - eta(i, i), 2);
135 >
136 >  diffEta = sqrt( sumEta / 3.0 );
137 >
138 >  return ( diffEta <= etaTolerance );
139   }
140  
141 < template<typename T> void NPTxyz<T>::getPosScale(double pos[3], double COM[3],
142 <                                               int index, double sc[3]){
143 <  int j;
149 <  double rj[3];
141 > */
142 >    
143 > double NPTxyz::calcConservedQuantity(){
144  
145 <  for(j=0; j<3; j++)
146 <    rj[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j];
145 >  double conservedQuantity;
146 >  double totalEnergy;
147 >  double thermostat_kinetic;
148 >  double thermostat_potential;
149 >  double barostat_kinetic;
150 >  double barostat_potential;
151 >  double trEta;
152  
153 <  matVecMul3( eta, rj, sc );
153 >  totalEnergy = thermo.getTotalE();
154 >
155 >  thermostat_kinetic = fkBT * tt2 * chi * chi /(2.0 * OOPSEConstant::energyConvert);
156 >
157 >  thermostat_potential = fkBT* integralOfChidt / OOPSEConstant::energyConvert;
158 >
159 >    SquareMatrix<double, 3> tmp = eta.transpose() * eta;
160 >    trEta = tmp.trace();
161 >
162 >  barostat_kinetic = NkBT * tb2 * trEta /(2.0 * OOPSEConstant::energyConvert);
163 >
164 >  barostat_potential = (targetPressure * thermo.getVolume() / OOPSEConstant::pressureConvert) /OOPSEConstant::energyConvert;
165 >
166 >  conservedQuantity = totalEnergy + thermostat_kinetic + thermostat_potential +
167 >    barostat_kinetic + barostat_potential;
168 >
169 >
170 >  return conservedQuantity;
171 >
172   }
173  
174 < template<typename T> void NPTxyz<T>::scaleSimBox( void ){
174 >    
175 > void NPTxyz::scaleSimBox(){
176  
177    int i,j,k;
178 <  double scaleMat[3][3];
178 >  Mat3x3d scaleMat;
179    double eta2ij, scaleFactor;
180    double bigScale, smallScale, offDiagMax;
181 <  double hm[3][3], hmnew[3][3];
181 >  Mat3x3d hm;
182 >  Mat3x3d hmnew;
183  
184  
185  
# Line 175 | Line 194 | template<typename T> void NPTxyz<T>::scaleSimBox( void
194  
195    for(i=0; i<3; i++){
196      for(j=0; j<3; j++){
197 <      scaleMat[i][j] = 0.0;
198 <      if(i==j) scaleMat[i][j] = 1.0;
197 >      scaleMat(i, j) = 0.0;
198 >      if(i==j) scaleMat(i, j) = 1.0;
199      }
200    }
201  
# Line 184 | Line 203 | template<typename T> void NPTxyz<T>::scaleSimBox( void
203  
204      // calculate the scaleFactors
205  
206 <    scaleFactor = exp(dt*eta[i][i]);
206 >    scaleFactor = exp(dt*eta(i, i));
207  
208 <    scaleMat[i][i] = scaleFactor;
208 >    scaleMat(i, i) = scaleFactor;
209  
210 <    if (scaleMat[i][i] > bigScale) bigScale = scaleMat[i][i];
211 <    if (scaleMat[i][i] < smallScale) smallScale = scaleMat[i][i];
210 >    if (scaleMat(i, i) > bigScale) bigScale = scaleMat(i, i);
211 >    if (scaleMat(i, i) < smallScale) smallScale = scaleMat(i, i);
212    }
213  
195 //   for(i=0; i<3; i++){
196 //     for(j=0; j<3; j++){
197
198 //       // Calculate the matrix Product of the eta array (we only need
199 //       // the ij element right now):
200
201 //       eta2ij = 0.0;
202 //       for(k=0; k<3; k++){
203 //         eta2ij += eta[i][k] * eta[k][j];
204 //       }
205
206 //       scaleMat[i][j] = 0.0;
207 //       // identity matrix (see above):
208 //       if (i == j) scaleMat[i][j] = 1.0;
209 //       // Taylor expansion for the exponential truncated at second order:
210 //       scaleMat[i][j] += dt*eta[i][j]  + 0.5*dt*dt*eta2ij;
211
212 //       if (i != j)
213 //         if (fabs(scaleMat[i][j]) > offDiagMax)
214 //           offDiagMax = fabs(scaleMat[i][j]);
215 //     }
216
217 //     if (scaleMat[i][i] > bigScale) bigScale = scaleMat[i][i];
218 //     if (scaleMat[i][i] < smallScale) smallScale = scaleMat[i][i];
219 //   }
220
214    if ((bigScale > 1.1) || (smallScale < 0.9)) {
215      sprintf( painCave.errMsg,
216               "NPTxyz error: Attempting a Box scaling of more than 10 percent.\n"
# Line 225 | Line 218 | template<typename T> void NPTxyz<T>::scaleSimBox( void
218               " scaleMat = [%lf\t%lf\t%lf]\n"
219               "            [%lf\t%lf\t%lf]\n"
220               "            [%lf\t%lf\t%lf]\n",
221 <             scaleMat[0][0],scaleMat[0][1],scaleMat[0][2],
222 <             scaleMat[1][0],scaleMat[1][1],scaleMat[1][2],
223 <             scaleMat[2][0],scaleMat[2][1],scaleMat[2][2]);
221 >             scaleMat(0, 0),scaleMat(0, 1),scaleMat(0, 2),
222 >             scaleMat(1, 0),scaleMat(1, 1),scaleMat(1, 2),
223 >             scaleMat(2, 0),scaleMat(2, 1),scaleMat(2, 2));
224      painCave.isFatal = 1;
225      simError();
226    } else {
227 <    info->getBoxM(hm);
228 <    matMul3(hm, scaleMat, hmnew);
229 <    info->setBoxM(hmnew);
227 >
228 >        Mat3x3d hmat = currentSnapshot_->getHmat();
229 >        hmat = hmat *scaleMat;
230 >        currentSnapshot_->setHmat(hmat);
231    }
232   }
233  
240 template<typename T> bool NPTxyz<T>::etaConverged() {
241  int i;
242  double diffEta, sumEta;
243
244  sumEta = 0;
245  for(i = 0; i < 3; i++)
246    sumEta += pow(prevEta[i][i] - eta[i][i], 2);
247
248  diffEta = sqrt( sumEta / 3.0 );
249
250  return ( diffEta <= etaTolerance );
234   }
252
253 template<typename T> double NPTxyz<T>::getConservedQuantity(void){
254
255  double conservedQuantity;
256  double totalEnergy;
257  double thermostat_kinetic;
258  double thermostat_potential;
259  double barostat_kinetic;
260  double barostat_potential;
261  double trEta;
262  double a[3][3], b[3][3];
263
264  totalEnergy = tStats->getTotalE();
265
266  thermostat_kinetic = fkBT * tt2 * chi * chi /
267    (2.0 * eConvert);
268
269  thermostat_potential = fkBT* integralOfChidt / eConvert;
270
271  transposeMat3(eta, a);
272  matMul3(a, eta, b);
273  trEta = matTrace3(b);
274
275  barostat_kinetic = NkBT * tb2 * trEta /
276    (2.0 * eConvert);
277
278  barostat_potential = (targetPressure * tStats->getVolume() / p_convert) /
279    eConvert;
280
281  conservedQuantity = totalEnergy + thermostat_kinetic + thermostat_potential +
282    barostat_kinetic + barostat_potential;
283
284 //   cout.width(8);
285 //   cout.precision(8);
286
287 //   cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic <<
288 //       "\t" << thermostat_potential << "\t" << barostat_kinetic <<
289 //       "\t" << barostat_potential << "\t" << conservedQuantity << endl;
290
291  return conservedQuantity;
292
293 }
294
295 template<typename T> string NPTxyz<T>::getAdditionalParameters(void){
296  string parameters;
297  const int BUFFERSIZE = 2000; // size of the read buffer
298  char buffer[BUFFERSIZE];
299
300  sprintf(buffer,"\t%G\t%G;", chi, integralOfChidt);
301  parameters += buffer;
302
303  for(int i = 0; i < 3; i++){
304    sprintf(buffer,"\t%G\t%G\t%G;", eta[i][0], eta[i][1], eta[i][2]);
305    parameters += buffer;
306  }
307
308  return parameters;
309
310 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines