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

Comparing trunk/OOPSE-2.0/src/integrators/NPTf.cpp (file contents):
Revision 1490 by gezelter, Fri Sep 24 04:16:43 2004 UTC vs.
Revision 2204 by gezelter, Fri Apr 15 22:04:00 2005 UTC

# Line 1 | Line 1
1 < #include <math.h>
1 > /*
2 > * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3 > *
4 > * The University of Notre Dame grants you ("Licensee") a
5 > * non-exclusive, royalty free, license to use, modify and
6 > * redistribute this software in source and binary code form, provided
7 > * that the following conditions are met:
8 > *
9 > * 1. Acknowledgement of the program authors must be made in any
10 > *    publication of scientific results based in part on use of the
11 > *    program.  An acceptable form of acknowledgement is citation of
12 > *    the article in which the program was described (Matthew
13 > *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14 > *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15 > *    Parallel Simulation Engine for Molecular Dynamics,"
16 > *    J. Comput. Chem. 26, pp. 252-271 (2005))
17 > *
18 > * 2. Redistributions of source code must retain the above copyright
19 > *    notice, this list of conditions and the following disclaimer.
20 > *
21 > * 3. Redistributions in binary form must reproduce the above copyright
22 > *    notice, this list of conditions and the following disclaimer in the
23 > *    documentation and/or other materials provided with the
24 > *    distribution.
25 > *
26 > * This software is provided "AS IS," without a warranty of any
27 > * kind. All express or implied conditions, representations and
28 > * warranties, including any implied warranty of merchantability,
29 > * fitness for a particular purpose or non-infringement, are hereby
30 > * excluded.  The University of Notre Dame and its licensors shall not
31 > * be liable for any damages suffered by licensee as a result of
32 > * using, modifying or distributing the software or its
33 > * derivatives. In no event will the University of Notre Dame or its
34 > * licensors be liable for any lost revenue, profit or data, or for
35 > * direct, indirect, special, consequential, incidental or punitive
36 > * damages, however caused and regardless of the theory of liability,
37 > * arising out of the use of or inability to use software, even if the
38 > * University of Notre Dame has been advised of the possibility of
39 > * such damages.
40 > */
41 >
42 > #include "brains/SimInfo.hpp"
43 > #include "brains/Thermo.hpp"
44 > #include "integrators/IntegratorCreator.hpp"
45 > #include "integrators/NPTf.hpp"
46 > #include "primitives/Molecule.hpp"
47 > #include "utils/OOPSEConstant.hpp"
48 > #include "utils/simError.h"
49  
50 < #include "MatVec3.h"
4 < #include "Atom.hpp"
5 < #include "SRI.hpp"
6 < #include "AbstractClasses.hpp"
7 < #include "SimInfo.hpp"
8 < #include "ForceFields.hpp"
9 < #include "Thermo.hpp"
10 < #include "ReadWrite.hpp"
11 < #include "Integrator.hpp"
12 < #include "simError.h"
50 > namespace oopse {
51  
52 < #ifdef IS_MPI
53 < #include "mpiSimulation.hpp"
54 < #endif
52 >  // Basic non-isotropic thermostating and barostating via the Melchionna
53 >  // modification of the Hoover algorithm:
54 >  //
55 >  //    Melchionna, S., Ciccotti, G., and Holian, B. L., 1993,
56 >  //       Molec. Phys., 78, 533.
57 >  //
58 >  //           and
59 >  //
60 >  //    Hoover, W. G., 1986, Phys. Rev. A, 34, 2499.
61  
62 < // Basic non-isotropic thermostating and barostating via the Melchionna
19 < // modification of the Hoover algorithm:
20 < //
21 < //    Melchionna, S., Ciccotti, G., and Holian, B. L., 1993,
22 < //       Molec. Phys., 78, 533.
23 < //
24 < //           and
25 < //
26 < //    Hoover, W. G., 1986, Phys. Rev. A, 34, 2499.
62 >  void NPTf::evolveEtaA() {
63  
64 < template<typename T> NPTf<T>::NPTf ( SimInfo *theInfo, ForceFields* the_ff):
29 <  T( theInfo, the_ff )
30 < {
31 <  GenericData* data;
32 <  DoubleArrayData * etaValue;
33 <  vector<double> etaArray;
34 <  int i,j;
64 >    int i, j;
65  
66 <  for(i = 0; i < 3; i++){
67 <    for (j = 0; j < 3; j++){
68 <
69 <      eta[i][j] = 0.0;
70 <      oldEta[i][j] = 0.0;
71 <    }
42 <  }
43 <
44 <
45 <  if( theInfo->useInitXSstate ){
46 <    // retrieve eta array from simInfo if it exists
47 <    data = info->getProperty(ETAVALUE_ID);
48 <    if(data){
49 <      etaValue = dynamic_cast<DoubleArrayData*>(data);
50 <      
51 <      if(etaValue){
52 <        etaArray = etaValue->getData();
53 <        
54 <        for(i = 0; i < 3; i++){
55 <          for (j = 0; j < 3; j++){
56 <            eta[i][j] = etaArray[3*i+j];
57 <            oldEta[i][j] = eta[i][j];
58 <          }
66 >    for(i = 0; i < 3; i ++){
67 >      for(j = 0; j < 3; j++){
68 >        if( i == j) {
69 >          eta(i, j) += dt2 *  instaVol * (press(i, j) - targetPressure/OOPSEConstant::pressureConvert) / (NkBT*tb2);
70 >        } else {
71 >          eta(i, j) += dt2 * instaVol * press(i, j) / (NkBT*tb2);
72          }
73        }
74      }
75 +  
76 +    for(i = 0; i < 3; i++) {
77 +      for (j = 0; j < 3; j++) {
78 +        oldEta(i, j) = eta(i, j);
79 +      }
80 +    }
81 +  
82    }
83  
84 < }
84 >  void NPTf::evolveEtaB() {
85  
86 < template<typename T> NPTf<T>::~NPTf() {
86 >    int i;
87 >    int j;
88  
89 <  // empty for now
90 < }
91 <
92 < template<typename T> void NPTf<T>::resetIntegrator() {
72 <
73 <  int i, j;
74 <
75 <  for(i = 0; i < 3; i++)
76 <    for (j = 0; j < 3; j++)
77 <      eta[i][j] = 0.0;
78 <
79 <  T::resetIntegrator();
80 < }
81 <
82 < template<typename T> void NPTf<T>::evolveEtaA() {
83 <
84 <  int 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] += dt2 *  instaVol *
90 <          (press[i][j] - targetPressure/p_convert) / (NkBT*tb2);
91 <      else
92 <        eta[i][j] += dt2 * instaVol * press[i][j] / (NkBT*tb2);
89 >    for(i = 0; i < 3; i++) {
90 >      for (j = 0; j < 3; j++) {
91 >        prevEta(i, j) = eta(i, j);
92 >      }
93      }
94  }
95  
96  for(i = 0; i < 3; i++)
97    for (j = 0; j < 3; j++)
98      oldEta[i][j] = eta[i][j];
99 }
94  
95 < template<typename T> void NPTf<T>::evolveEtaB() {
96 <
97 <  int i,j;
98 <
99 <  for(i = 0; i < 3; i++)
100 <    for (j = 0; j < 3; j++)
101 <      prevEta[i][j] = eta[i][j];
102 <
109 <  for(i = 0; i < 3; i ++){
110 <    for(j = 0; j < 3; j++){
111 <      if( i == j) {
112 <        eta[i][j] = oldEta[i][j] + dt2 *  instaVol *
113 <          (press[i][j] - targetPressure/p_convert) / (NkBT*tb2);
114 <      } else {
115 <        eta[i][j] = oldEta[i][j] + dt2 * instaVol * press[i][j] / (NkBT*tb2);
95 >    for(i = 0; i < 3; i ++){
96 >      for(j = 0; j < 3; j++){
97 >        if( i == j) {
98 >          eta(i, j) = oldEta(i, j) + dt2 *  instaVol *
99 >            (press(i, j) - targetPressure/OOPSEConstant::pressureConvert) / (NkBT*tb2);
100 >        } else {
101 >          eta(i, j) = oldEta(i, j) + dt2 * instaVol * press(i, j) / (NkBT*tb2);
102 >        }
103        }
104      }
105 +
106 +  
107    }
119 }
108  
109 < template<typename T> void NPTf<T>::calcVelScale(void){
122 <  int i,j;
109 >  void NPTf::calcVelScale(){
110  
111 <  for (i = 0; i < 3; i++ ) {
112 <    for (j = 0; j < 3; j++ ) {
113 <      vScale[i][j] = eta[i][j];
111 >    for (int i = 0; i < 3; i++ ) {
112 >      for (int j = 0; j < 3; j++ ) {
113 >        vScale(i, j) = eta(i, j);
114  
115 <      if (i == j) {
116 <        vScale[i][j] += chi;
115 >        if (i == j) {
116 >          vScale(i, j) += chi;
117 >        }
118        }
119      }
120    }
133 }
121  
122 < template<typename T> void NPTf<T>::getVelScaleA(double sc[3], double vel[3]) {
123 <
124 <  matVecMul3( vScale, vel, sc );
138 < }
122 >  void NPTf::getVelScaleA(Vector3d& sc, const Vector3d& vel){
123 >    sc = vScale * vel;
124 >  }
125  
126 < template<typename T> void NPTf<T>::getVelScaleB(double sc[3], int index ){
127 <  int j;
128 <  double myVel[3];
126 >  void NPTf::getVelScaleB(Vector3d& sc, int index ) {
127 >    sc = vScale * oldVel[index];
128 >  }
129  
130 <  for (j = 0; j < 3; j++)
145 <    myVel[j] = oldVel[3*index + j];
146 <  
147 <  matVecMul3( vScale, myVel, sc );
148 < }
130 >  void NPTf::getPosScale(const Vector3d& pos, const Vector3d& COM, int index, Vector3d& sc) {
131  
132 < template<typename T> void NPTf<T>::getPosScale(double pos[3], double COM[3],
133 <                                               int index, double sc[3]){
134 <  int j;
135 <  double rj[3];
132 >    /**@todo */
133 >    Vector3d rj = (oldPos[index] + pos)/2.0 -COM;
134 >    sc = eta * rj;
135 >  }
136  
137 <  for(j=0; j<3; j++)
156 <    rj[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j];
137 >  void NPTf::scaleSimBox(){
138  
139 <  matVecMul3( eta, rj, sc );
140 < }
139 >    int i;
140 >    int j;
141 >    int k;
142 >    Mat3x3d scaleMat;
143 >    double eta2ij;
144 >    double bigScale, smallScale, offDiagMax;
145 >    Mat3x3d hm;
146 >    Mat3x3d hmnew;
147  
161 template<typename T> void NPTf<T>::scaleSimBox( void ){
148  
163  int i,j,k;
164  double scaleMat[3][3];
165  double eta2ij;
166  double bigScale, smallScale, offDiagMax;
167  double hm[3][3], hmnew[3][3];
149  
150 +    // Scale the box after all the positions have been moved:
151  
152 +    // Use a taylor expansion for eta products:  Hmat = Hmat . exp(dt * etaMat)
153 +    //  Hmat = Hmat . ( Ident + dt * etaMat  + dt^2 * etaMat*etaMat / 2)
154  
155 <  // Scale the box after all the positions have been moved:
155 >    bigScale = 1.0;
156 >    smallScale = 1.0;
157 >    offDiagMax = 0.0;
158  
159 <  // Use a taylor expansion for eta products:  Hmat = Hmat . exp(dt * etaMat)
160 <  //  Hmat = Hmat . ( Ident + dt * etaMat  + dt^2 * etaMat*etaMat / 2)
159 >    for(i=0; i<3; i++){
160 >      for(j=0; j<3; j++){
161  
162 <  bigScale = 1.0;
163 <  smallScale = 1.0;
178 <  offDiagMax = 0.0;
162 >        // Calculate the matrix Product of the eta array (we only need
163 >        // the ij element right now):
164  
165 <  for(i=0; i<3; i++){
166 <    for(j=0; j<3; j++){
165 >        eta2ij = 0.0;
166 >        for(k=0; k<3; k++){
167 >          eta2ij += eta(i, k) * eta(k, j);
168 >        }
169  
170 <      // Calculate the matrix Product of the eta array (we only need
171 <      // the ij element right now):
170 >        scaleMat(i, j) = 0.0;
171 >        // identity matrix (see above):
172 >        if (i == j) scaleMat(i, j) = 1.0;
173 >        // Taylor expansion for the exponential truncated at second order:
174 >        scaleMat(i, j) += dt*eta(i, j)  + 0.5*dt*dt*eta2ij;
175 >      
176  
177 <      eta2ij = 0.0;
178 <      for(k=0; k<3; k++){
179 <        eta2ij += eta[i][k] * eta[k][j];
177 >        if (i != j)
178 >          if (fabs(scaleMat(i, j)) > offDiagMax)
179 >            offDiagMax = fabs(scaleMat(i, j));
180        }
181  
182 <      scaleMat[i][j] = 0.0;
183 <      // identity matrix (see above):
193 <      if (i == j) scaleMat[i][j] = 1.0;
194 <      // Taylor expansion for the exponential truncated at second order:
195 <      scaleMat[i][j] += dt*eta[i][j]  + 0.5*dt*dt*eta2ij;
196 <      
197 <
198 <      if (i != j)
199 <        if (fabs(scaleMat[i][j]) > offDiagMax)
200 <          offDiagMax = fabs(scaleMat[i][j]);
182 >      if (scaleMat(i, i) > bigScale) bigScale = scaleMat(i, i);
183 >      if (scaleMat(i, i) < smallScale) smallScale = scaleMat(i, i);
184      }
185  
186 <    if (scaleMat[i][i] > bigScale) bigScale = scaleMat[i][i];
187 <    if (scaleMat[i][i] < smallScale) smallScale = scaleMat[i][i];
188 <  }
186 >    if ((bigScale > 1.01) || (smallScale < 0.99)) {
187 >      sprintf( painCave.errMsg,
188 >               "NPTf error: Attempting a Box scaling of more than 1 percent.\n"
189 >               " Check your tauBarostat, as it is probably too small!\n\n"
190 >               " scaleMat = [%lf\t%lf\t%lf]\n"
191 >               "            [%lf\t%lf\t%lf]\n"
192 >               "            [%lf\t%lf\t%lf]\n"
193 >               "      eta = [%lf\t%lf\t%lf]\n"
194 >               "            [%lf\t%lf\t%lf]\n"
195 >               "            [%lf\t%lf\t%lf]\n",
196 >               scaleMat(0, 0),scaleMat(0, 1),scaleMat(0, 2),
197 >               scaleMat(1, 0),scaleMat(1, 1),scaleMat(1, 2),
198 >               scaleMat(2, 0),scaleMat(2, 1),scaleMat(2, 2),
199 >               eta(0, 0),eta(0, 1),eta(0, 2),
200 >               eta(1, 0),eta(1, 1),eta(1, 2),
201 >               eta(2, 0),eta(2, 1),eta(2, 2));
202 >      painCave.isFatal = 1;
203 >      simError();
204 >    } else if (offDiagMax > 0.01) {
205 >      sprintf( painCave.errMsg,
206 >               "NPTf error: Attempting an off-diagonal Box scaling of more than 1 percent.\n"
207 >               " Check your tauBarostat, as it is probably too small!\n\n"
208 >               " scaleMat = [%lf\t%lf\t%lf]\n"
209 >               "            [%lf\t%lf\t%lf]\n"
210 >               "            [%lf\t%lf\t%lf]\n"
211 >               "      eta = [%lf\t%lf\t%lf]\n"
212 >               "            [%lf\t%lf\t%lf]\n"
213 >               "            [%lf\t%lf\t%lf]\n",
214 >               scaleMat(0, 0),scaleMat(0, 1),scaleMat(0, 2),
215 >               scaleMat(1, 0),scaleMat(1, 1),scaleMat(1, 2),
216 >               scaleMat(2, 0),scaleMat(2, 1),scaleMat(2, 2),
217 >               eta(0, 0),eta(0, 1),eta(0, 2),
218 >               eta(1, 0),eta(1, 1),eta(1, 2),
219 >               eta(2, 0),eta(2, 1),eta(2, 2));
220 >      painCave.isFatal = 1;
221 >      simError();
222 >    } else {
223  
224 <  if ((bigScale > 1.01) || (smallScale < 0.99)) {
225 <    sprintf( painCave.errMsg,
226 <             "NPTf error: Attempting a Box scaling of more than 1 percent.\n"
227 <             " Check your tauBarostat, as it is probably too small!\n\n"
228 <             " scaleMat = [%lf\t%lf\t%lf]\n"
212 <             "            [%lf\t%lf\t%lf]\n"
213 <             "            [%lf\t%lf\t%lf]\n"
214 <             "      eta = [%lf\t%lf\t%lf]\n"
215 <             "            [%lf\t%lf\t%lf]\n"
216 <             "            [%lf\t%lf\t%lf]\n",
217 <             scaleMat[0][0],scaleMat[0][1],scaleMat[0][2],
218 <             scaleMat[1][0],scaleMat[1][1],scaleMat[1][2],
219 <             scaleMat[2][0],scaleMat[2][1],scaleMat[2][2],
220 <             eta[0][0],eta[0][1],eta[0][2],
221 <             eta[1][0],eta[1][1],eta[1][2],
222 <             eta[2][0],eta[2][1],eta[2][2]);
223 <    painCave.isFatal = 1;
224 <    simError();
225 <  } else if (offDiagMax > 0.01) {
226 <    sprintf( painCave.errMsg,
227 <             "NPTf error: Attempting an off-diagonal Box scaling of more than 1 percent.\n"
228 <             " Check your tauBarostat, as it is probably too small!\n\n"
229 <             " scaleMat = [%lf\t%lf\t%lf]\n"
230 <             "            [%lf\t%lf\t%lf]\n"
231 <             "            [%lf\t%lf\t%lf]\n"
232 <             "      eta = [%lf\t%lf\t%lf]\n"
233 <             "            [%lf\t%lf\t%lf]\n"
234 <             "            [%lf\t%lf\t%lf]\n",
235 <             scaleMat[0][0],scaleMat[0][1],scaleMat[0][2],
236 <             scaleMat[1][0],scaleMat[1][1],scaleMat[1][2],
237 <             scaleMat[2][0],scaleMat[2][1],scaleMat[2][2],
238 <             eta[0][0],eta[0][1],eta[0][2],
239 <             eta[1][0],eta[1][1],eta[1][2],
240 <             eta[2][0],eta[2][1],eta[2][2]);
241 <    painCave.isFatal = 1;
242 <    simError();
243 <  } else {
244 <    info->getBoxM(hm);
245 <    matMul3(hm, scaleMat, hmnew);
246 <    info->setBoxM(hmnew);
224 >      Mat3x3d hmat = currentSnapshot_->getHmat();
225 >      hmat = hmat *scaleMat;
226 >      currentSnapshot_->setHmat(hmat);
227 >        
228 >    }
229    }
248 }
230  
231 < template<typename T> bool NPTf<T>::etaConverged() {
232 <  int i;
233 <  double diffEta, sumEta;
231 >  bool NPTf::etaConverged() {
232 >    int i;
233 >    double diffEta, sumEta;
234  
235 <  sumEta = 0;
236 <  for(i = 0; i < 3; i++)
237 <    sumEta += pow(prevEta[i][i] - eta[i][i], 2);
235 >    sumEta = 0;
236 >    for(i = 0; i < 3; i++) {
237 >      sumEta += pow(prevEta(i, i) - eta(i, i), 2);
238 >    }
239 >    
240 >    diffEta = sqrt( sumEta / 3.0 );
241  
242 <  diffEta = sqrt( sumEta / 3.0 );
242 >    return ( diffEta <= etaTolerance );
243 >  }
244  
245 <  return ( diffEta <= etaTolerance );
261 < }
245 >  double NPTf::calcConservedQuantity(){
246  
247 < template<typename T> double NPTf<T>::getConservedQuantity(void){
247 >    chi= currentSnapshot_->getChi();
248 >    integralOfChidt = currentSnapshot_->getIntegralOfChiDt();
249 >    loadEta();
250 >    
251 >    // We need NkBT a lot, so just set it here: This is the RAW number
252 >    // of integrableObjects, so no subtraction or addition of constraints or
253 >    // orientational degrees of freedom:
254 >    NkBT = info_->getNGlobalIntegrableObjects()*OOPSEConstant::kB *targetTemp;
255  
256 <  double conservedQuantity;
257 <  double totalEnergy;
258 <  double thermostat_kinetic;
259 <  double thermostat_potential;
260 <  double barostat_kinetic;
261 <  double barostat_potential;
262 <  double trEta;
263 <  double a[3][3], b[3][3];
256 >    // fkBT is used because the thermostat operates on more degrees of freedom
257 >    // than the barostat (when there are particles with orientational degrees
258 >    // of freedom).  
259 >    fkBT = info_->getNdf()*OOPSEConstant::kB *targetTemp;    
260 >    
261 >    double conservedQuantity;
262 >    double totalEnergy;
263 >    double thermostat_kinetic;
264 >    double thermostat_potential;
265 >    double barostat_kinetic;
266 >    double barostat_potential;
267 >    double trEta;
268  
269 <  totalEnergy = tStats->getTotalE();
269 >    totalEnergy = thermo.getTotalE();
270  
271 <  thermostat_kinetic = fkBT * tt2 * chi * chi /
277 <    (2.0 * eConvert);
271 >    thermostat_kinetic = fkBT * tt2 * chi * chi /(2.0 * OOPSEConstant::energyConvert);
272  
273 <  thermostat_potential = fkBT* integralOfChidt / eConvert;
273 >    thermostat_potential = fkBT* integralOfChidt / OOPSEConstant::energyConvert;
274  
275 <  transposeMat3(eta, a);
276 <  matMul3(a, eta, b);
277 <  trEta = matTrace3(b);
275 >    SquareMatrix<double, 3> tmp = eta.transpose() * eta;
276 >    trEta = tmp.trace();
277 >    
278 >    barostat_kinetic = NkBT * tb2 * trEta /(2.0 * OOPSEConstant::energyConvert);
279  
280 <  barostat_kinetic = NkBT * tb2 * trEta /
286 <    (2.0 * eConvert);
280 >    barostat_potential = (targetPressure * thermo.getVolume() / OOPSEConstant::pressureConvert) /OOPSEConstant::energyConvert;
281  
282 <  barostat_potential = (targetPressure * tStats->getVolume() / p_convert) /
283 <    eConvert;
282 >    conservedQuantity = totalEnergy + thermostat_kinetic + thermostat_potential +
283 >      barostat_kinetic + barostat_potential;
284  
285 <  conservedQuantity = totalEnergy + thermostat_kinetic + thermostat_potential +
292 <    barostat_kinetic + barostat_potential;
285 >    return conservedQuantity;
286  
287 <  return conservedQuantity;
287 >  }
288  
289 < }
289 >  void NPTf::loadEta() {
290 >    eta= currentSnapshot_->getEta();
291  
292 < template<typename T> string NPTf<T>::getAdditionalParameters(void){
293 <  string parameters;
294 <  const int BUFFERSIZE = 2000; // size of the read buffer
295 <  char buffer[BUFFERSIZE];
292 >    //if (!eta.isDiagonal()) {
293 >    //    sprintf( painCave.errMsg,
294 >    //             "NPTf error: the diagonal elements of eta matrix are not the same or etaMat is not a diagonal matrix");
295 >    //    painCave.isFatal = 1;
296 >    //    simError();
297 >    //}
298 >  }
299  
300 <  sprintf(buffer,"\t%G\t%G;", chi, integralOfChidt);
301 <  parameters += buffer;
305 <
306 <  for(int i = 0; i < 3; i++){
307 <    sprintf(buffer,"\t%G\t%G\t%G;", eta[i][0], eta[i][1], eta[i][2]);
308 <    parameters += buffer;
300 >  void NPTf::saveEta() {
301 >    currentSnapshot_->setEta(eta);
302    }
303  
311  return parameters;
312
304   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines