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

Comparing branches/new_design/OOPSE-2.0/src/integrators/NPTf.cpp (file contents):
Revision 1683, Thu Oct 28 22:34:02 2004 UTC vs.
Revision 1837 by tim, Thu Dec 2 22:15:31 2004 UTC

# Line 1 | Line 1
1 #include <math.h>
2
3 #include "math/MatVec3.h"
4 #include "primitives/Atom.hpp"
5 #include "primitives/SRI.hpp"
6 #include "primitives/AbstractClasses.hpp"
1   #include "brains/SimInfo.hpp"
8 #include "UseTheForce/ForceFields.hpp"
2   #include "brains/Thermo.hpp"
3 < #include "io/ReadWrite.hpp"
4 < #include "integrators/Integrator.hpp"
3 > #include "integrators/IntegratorCreator.hpp"
4 > #include "integrators/NPTf.hpp"
5 > #include "primitives/Molecule.hpp"
6 > #include "utils/OOPSEConstant.hpp"
7   #include "utils/simError.h"
8  
9 < #ifdef IS_MPI
15 < #include "brains/mpiSimulation.hpp"
16 < #endif
9 > namespace oopse {
10  
11 + static IntegratorBuilder<NPTf>* NPTfCreator = new IntegratorBuilder<NPTf>("NPTf");
12 +
13   // Basic non-isotropic thermostating and barostating via the Melchionna
14   // modification of the Hoover algorithm:
15   //
# Line 25 | Line 20 | template<typename T> NPTf<T>::NPTf ( SimInfo *theInfo,
20   //
21   //    Hoover, W. G., 1986, Phys. Rev. A, 34, 2499.
22  
23 < template<typename T> NPTf<T>::NPTf ( SimInfo *theInfo, ForceFields* the_ff):
29 <  T( theInfo, the_ff )
30 < {
31 <  GenericData* data;
32 <  DoubleVectorGenericData * etaValue;
33 <  int i,j;
23 > void NPTf::evolveEtaA() {
24  
25 <  for(i = 0; i < 3; i++){
36 <    for (j = 0; j < 3; j++){
25 >  int i, j;
26  
27 <      eta[i][j] = 0.0;
28 <      oldEta[i][j] = 0.0;
27 >    for(i = 0; i < 3; i ++){
28 >        for(j = 0; j < 3; j++){
29 >            if( i == j) {
30 >                eta(i, j) += dt2 *  instaVol * (press(i, j) - targetPressure/OOPSEConstant::pressureConvert) / (NkBT*tb2);
31 >            } else {
32 >                eta(i, j) += dt2 * instaVol * press(i, j) / (NkBT*tb2);
33 >            }
34 >        }
35      }
36 <  }
37 <
38 <
39 <  if( theInfo->useInitXSstate ){
40 <    // retrieve eta array from simInfo if it exists
46 <    data = info->getProperty(ETAVALUE_ID);
47 <    if(data){
48 <      etaValue = dynamic_cast<DoubleVectorGenericData*>(data);
49 <      
50 <      if(etaValue){
51 <        
52 <        for(i = 0; i < 3; i++){
53 <          for (j = 0; j < 3; j++){
54 <            eta[i][j] = (*etaValue)[3*i+j];
55 <            oldEta[i][j] = eta[i][j];
56 <          }
57 <        }
58 <      }
36 >  
37 >    for(i = 0; i < 3; i++) {
38 >        for (j = 0; j < 3; j++) {
39 >        oldEta(i, j) = eta(i, j);
40 >        }
41      }
42 <  }
61 <
42 >  
43   }
44  
45 < template<typename T> NPTf<T>::~NPTf() {
45 > void NPTf::evolveEtaB() {
46  
47 <  // empty for now
48 < }
47 >    int i;
48 >    int j;
49  
50 < template<typename T> void NPTf<T>::resetIntegrator() {
50 >    for(i = 0; i < 3; i++) {
51 >        for (j = 0; j < 3; j++) {
52 >            prevEta(i, j) = eta(i, j);
53 >        }
54 >    }
55  
56 <  int i, j;
57 <
58 <  for(i = 0; i < 3; i++)
59 <    for (j = 0; j < 3; j++)
60 <      eta[i][j] = 0.0;
61 <
62 <  T::resetIntegrator();
63 < }
64 <
80 < template<typename T> void NPTf<T>::evolveEtaA() {
81 <
82 <  int i, j;
83 <
84 <  for(i = 0; i < 3; i ++){
85 <    for(j = 0; j < 3; j++){
86 <      if( i == j)
87 <        eta[i][j] += dt2 *  instaVol *
88 <          (press[i][j] - targetPressure/p_convert) / (NkBT*tb2);
89 <      else
90 <        eta[i][j] += dt2 * instaVol * press[i][j] / (NkBT*tb2);
56 >    for(i = 0; i < 3; i ++){
57 >        for(j = 0; j < 3; j++){
58 >            if( i == j) {
59 >                eta(i, j) = oldEta(i, j) + dt2 *  instaVol *
60 >                (press(i, j) - targetPressure/OOPSEConstant::pressureConvert) / (NkBT*tb2);
61 >            } else {
62 >                eta(i, j) = oldEta(i, j) + dt2 * instaVol * press(i, j) / (NkBT*tb2);
63 >            }
64 >        }
65      }
66 <  }
66 >
67    
94  for(i = 0; i < 3; i++)
95    for (j = 0; j < 3; j++)
96      oldEta[i][j] = eta[i][j];
68   }
69  
70 < template<typename T> void NPTf<T>::evolveEtaB() {
70 > void NPTf::calcVelScale(){
71  
72 <  int i,j;
72 >  for (int i = 0; i < 3; i++ ) {
73 >    for (int j = 0; j < 3; j++ ) {
74 >      vScale(i, j) = eta(i, j);
75  
103  for(i = 0; i < 3; i++)
104    for (j = 0; j < 3; j++)
105      prevEta[i][j] = eta[i][j];
106
107  for(i = 0; i < 3; i ++){
108    for(j = 0; j < 3; j++){
109      if( i == j) {
110        eta[i][j] = oldEta[i][j] + dt2 *  instaVol *
111          (press[i][j] - targetPressure/p_convert) / (NkBT*tb2);
112      } else {
113        eta[i][j] = oldEta[i][j] + dt2 * instaVol * press[i][j] / (NkBT*tb2);
114      }
115    }
116  }
117 }
118
119 template<typename T> void NPTf<T>::calcVelScale(void){
120  int i,j;
121
122  for (i = 0; i < 3; i++ ) {
123    for (j = 0; j < 3; j++ ) {
124      vScale[i][j] = eta[i][j];
125
76        if (i == j) {
77 <        vScale[i][j] += chi;
77 >        vScale(i, j) += chi;
78        }
79      }
80    }
81   }
82  
83 < template<typename T> void NPTf<T>::getVelScaleA(double sc[3], double vel[3]) {
84 <
135 <  matVecMul3( vScale, vel, sc );
83 > void NPTf::getVelScaleA(Vector3d& sc, const Vector3d& vel){
84 >    sc = vScale * vel;
85   }
86  
87 < template<typename T> void NPTf<T>::getVelScaleB(double sc[3], int index ){
88 <  int j;
140 <  double myVel[3];
141 <
142 <  for (j = 0; j < 3; j++)
143 <    myVel[j] = oldVel[3*index + j];
144 <  
145 <  matVecMul3( vScale, myVel, sc );
87 > void NPTf::getVelScaleB(Vector3d& sc, int index ) {
88 >  sc = vScale * oldVel[index];
89   }
90  
91 < template<typename T> void NPTf<T>::getPosScale(double pos[3], double COM[3],
149 <                                               int index, double sc[3]){
150 <  int j;
151 <  double rj[3];
91 > void NPTf::getPosScale(const Vector3d& pos, const Vector3d& COM, int index, Vector3d& sc) {
92  
93 <  for(j=0; j<3; j++)
94 <    rj[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j];
95 <
156 <  matVecMul3( eta, rj, sc );
93 >    /**@todo */
94 >    Vector3d rj = (oldPos[index] + pos)/2.0 -COM;
95 >    sc = eta * rj;
96   }
97  
98 < template<typename T> void NPTf<T>::scaleSimBox( void ){
98 > void NPTf::scaleSimBox(){
99  
100 <  int i,j,k;
101 <  double scaleMat[3][3];
100 >  int i;
101 >  int j;
102 >  int k;
103 >  Mat3x3d scaleMat;
104    double eta2ij;
105    double bigScale, smallScale, offDiagMax;
106 <  double hm[3][3], hmnew[3][3];
106 >  Mat3x3d hm;
107 >  Mat3x3d hmnew;
108  
109  
110  
# Line 183 | Line 125 | template<typename T> void NPTf<T>::scaleSimBox( void )
125  
126        eta2ij = 0.0;
127        for(k=0; k<3; k++){
128 <        eta2ij += eta[i][k] * eta[k][j];
128 >        eta2ij += eta(i, k) * eta(k, j);
129        }
130  
131 <      scaleMat[i][j] = 0.0;
131 >      scaleMat(i, j) = 0.0;
132        // identity matrix (see above):
133 <      if (i == j) scaleMat[i][j] = 1.0;
133 >      if (i == j) scaleMat(i, j) = 1.0;
134        // Taylor expansion for the exponential truncated at second order:
135 <      scaleMat[i][j] += dt*eta[i][j]  + 0.5*dt*dt*eta2ij;
135 >      scaleMat(i, j) += dt*eta(i, j)  + 0.5*dt*dt*eta2ij;
136        
137  
138        if (i != j)
139 <        if (fabs(scaleMat[i][j]) > offDiagMax)
140 <          offDiagMax = fabs(scaleMat[i][j]);
139 >        if (fabs(scaleMat(i, j)) > offDiagMax)
140 >          offDiagMax = fabs(scaleMat(i, j));
141      }
142  
143 <    if (scaleMat[i][i] > bigScale) bigScale = scaleMat[i][i];
144 <    if (scaleMat[i][i] < smallScale) smallScale = scaleMat[i][i];
143 >    if (scaleMat(i, i) > bigScale) bigScale = scaleMat(i, i);
144 >    if (scaleMat(i, i) < smallScale) smallScale = scaleMat(i, i);
145    }
146  
147    if ((bigScale > 1.01) || (smallScale < 0.99)) {
# Line 212 | Line 154 | template<typename T> void NPTf<T>::scaleSimBox( void )
154               "      eta = [%lf\t%lf\t%lf]\n"
155               "            [%lf\t%lf\t%lf]\n"
156               "            [%lf\t%lf\t%lf]\n",
157 <             scaleMat[0][0],scaleMat[0][1],scaleMat[0][2],
158 <             scaleMat[1][0],scaleMat[1][1],scaleMat[1][2],
159 <             scaleMat[2][0],scaleMat[2][1],scaleMat[2][2],
160 <             eta[0][0],eta[0][1],eta[0][2],
161 <             eta[1][0],eta[1][1],eta[1][2],
162 <             eta[2][0],eta[2][1],eta[2][2]);
157 >             scaleMat(0, 0),scaleMat(0, 1),scaleMat(0, 2),
158 >             scaleMat(1, 0),scaleMat(1, 1),scaleMat(1, 2),
159 >             scaleMat(2, 0),scaleMat(2, 1),scaleMat(2, 2),
160 >             eta(0, 0),eta(0, 1),eta(0, 2),
161 >             eta(1, 0),eta(1, 1),eta(1, 2),
162 >             eta(2, 0),eta(2, 1),eta(2, 2));
163      painCave.isFatal = 1;
164      simError();
165    } else if (offDiagMax > 0.01) {
# Line 230 | Line 172 | template<typename T> void NPTf<T>::scaleSimBox( void )
172               "      eta = [%lf\t%lf\t%lf]\n"
173               "            [%lf\t%lf\t%lf]\n"
174               "            [%lf\t%lf\t%lf]\n",
175 <             scaleMat[0][0],scaleMat[0][1],scaleMat[0][2],
176 <             scaleMat[1][0],scaleMat[1][1],scaleMat[1][2],
177 <             scaleMat[2][0],scaleMat[2][1],scaleMat[2][2],
178 <             eta[0][0],eta[0][1],eta[0][2],
179 <             eta[1][0],eta[1][1],eta[1][2],
180 <             eta[2][0],eta[2][1],eta[2][2]);
175 >             scaleMat(0, 0),scaleMat(0, 1),scaleMat(0, 2),
176 >             scaleMat(1, 0),scaleMat(1, 1),scaleMat(1, 2),
177 >             scaleMat(2, 0),scaleMat(2, 1),scaleMat(2, 2),
178 >             eta(0, 0),eta(0, 1),eta(0, 2),
179 >             eta(1, 0),eta(1, 1),eta(1, 2),
180 >             eta(2, 0),eta(2, 1),eta(2, 2));
181      painCave.isFatal = 1;
182      simError();
183    } else {
184 <    info->getBoxM(hm);
185 <    matMul3(hm, scaleMat, hmnew);
186 <    info->setBoxM(hmnew);
184 >
185 >        Mat3x3d hmat = currentSnapshot_->getHmat();
186 >        hmat = hmat *scaleMat;
187 >        currentSnapshot_->setHmat(hmat);
188 >        
189    }
190   }
191  
192 < template<typename T> bool NPTf<T>::etaConverged() {
193 <  int i;
194 <  double diffEta, sumEta;
192 > bool NPTf::etaConverged() {
193 >    int i;
194 >    double diffEta, sumEta;
195  
196 <  sumEta = 0;
197 <  for(i = 0; i < 3; i++)
198 <    sumEta += pow(prevEta[i][i] - eta[i][i], 2);
196 >    sumEta = 0;
197 >    for(i = 0; i < 3; i++) {
198 >        sumEta += pow(prevEta(i, i) - eta(i, i), 2);
199 >    }
200 >    
201 >    diffEta = sqrt( sumEta / 3.0 );
202  
203 <  diffEta = sqrt( sumEta / 3.0 );
257 <
258 <  return ( diffEta <= etaTolerance );
203 >    return ( diffEta <= etaTolerance );
204   }
205  
206 < template<typename T> double NPTf<T>::getConservedQuantity(void){
206 > double NPTf::calcConservedQuantity(){
207  
208 <  double conservedQuantity;
209 <  double totalEnergy;
210 <  double thermostat_kinetic;
211 <  double thermostat_potential;
212 <  double barostat_kinetic;
213 <  double barostat_potential;
214 <  double trEta;
270 <  double a[3][3], b[3][3];
208 >    double conservedQuantity;
209 >    double totalEnergy;
210 >    double thermostat_kinetic;
211 >    double thermostat_potential;
212 >    double barostat_kinetic;
213 >    double barostat_potential;
214 >    double trEta;
215  
216 <  totalEnergy = tStats->getTotalE();
216 >    totalEnergy = thermo.getTotalE();
217  
218 <  thermostat_kinetic = fkBT * tt2 * chi * chi /
275 <    (2.0 * eConvert);
218 >    thermostat_kinetic = fkBT * tt2 * chi * chi /(2.0 * OOPSEConstant::energyConvert);
219  
220 <  thermostat_potential = fkBT* integralOfChidt / eConvert;
220 >    thermostat_potential = fkBT* integralOfChidt / OOPSEConstant::energyConvert;
221  
222 <  transposeMat3(eta, a);
223 <  matMul3(a, eta, b);
224 <  trEta = matTrace3(b);
222 >    SquareMatrix<double, 3> tmp = eta.transpose() * eta;
223 >    trEta = tmp.trace();
224 >    
225 >    barostat_kinetic = NkBT * tb2 * trEta /(2.0 * OOPSEConstant::energyConvert);
226  
227 <  barostat_kinetic = NkBT * tb2 * trEta /
284 <    (2.0 * eConvert);
227 >    barostat_potential = (targetPressure * thermo.getVolume() / OOPSEConstant::pressureConvert) /OOPSEConstant::energyConvert;
228  
229 <  barostat_potential = (targetPressure * tStats->getVolume() / p_convert) /
230 <    eConvert;
229 >    conservedQuantity = totalEnergy + thermostat_kinetic + thermostat_potential +
230 >        barostat_kinetic + barostat_potential;
231  
232 <  conservedQuantity = totalEnergy + thermostat_kinetic + thermostat_potential +
290 <    barostat_kinetic + barostat_potential;
232 >    return conservedQuantity;
233  
292  return conservedQuantity;
293
234   }
235  
236 < template<typename T> string NPTf<T>::getAdditionalParameters(void){
237 <  string parameters;
298 <  const int BUFFERSIZE = 2000; // size of the read buffer
299 <  char buffer[BUFFERSIZE];
236 > void NPTf::loadEta() {
237 >    eta= currentSnapshot_->getEta();
238  
239 <  sprintf(buffer,"\t%G\t%G;", chi, integralOfChidt);
240 <  parameters += buffer;
239 >    if (!eta.isDiagonal()) {
240 >        sprintf( painCave.errMsg,
241 >                 "NPTi error: the diagonal elements of are eta matrix is not same or etaMat is not a diagonal matrix");
242 >        painCave.isFatal = 1;
243 >        simError();
244 >    }
245 > }
246  
247 <  for(int i = 0; i < 3; i++){
248 <    sprintf(buffer,"\t%G\t%G\t%G;", eta[i][0], eta[i][1], eta[i][2]);
249 <    parameters += buffer;
307 <  }
247 > void NPTf::saveEta() {
248 >    currentSnapshot_->setEta(eta);
249 > }
250  
309  return parameters;
310
251   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines