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

Comparing branches/new_design/OOPSE-3.0/src/integrators/NPTf.cpp (file contents):
Revision 1701 by tim, Wed Nov 3 16:08:43 2004 UTC vs.
Revision 1883 by tim, Mon Dec 13 22:30:27 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   // Basic non-isotropic thermostating and barostating via the Melchionna
12   // modification of the Hoover algorithm:
# Line 25 | Line 18 | template<typename T> NPTf<T>::NPTf ( SimInfo *theInfo,
18   //
19   //    Hoover, W. G., 1986, Phys. Rev. A, 34, 2499.
20  
21 < 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;
21 > void NPTf::evolveEtaA() {
22  
23 <  for(i = 0; i < 3; i++){
36 <    for (j = 0; j < 3; j++){
23 >  int i, j;
24  
25 <      eta[i][j] = 0.0;
26 <      oldEta[i][j] = 0.0;
25 >    for(i = 0; i < 3; i ++){
26 >        for(j = 0; j < 3; j++){
27 >            if( i == j) {
28 >                eta(i, j) += dt2 *  instaVol * (press(i, j) - targetPressure/OOPSEConstant::pressureConvert) / (NkBT*tb2);
29 >            } else {
30 >                eta(i, j) += dt2 * instaVol * press(i, j) / (NkBT*tb2);
31 >            }
32 >        }
33      }
34 <  }
35 <
36 <
37 <  if( theInfo->useInitXSstate ){
38 <    // retrieve eta array from simInfo if it exists
46 <    data = info->getPropertyByName(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 <      }
34 >  
35 >    for(i = 0; i < 3; i++) {
36 >        for (j = 0; j < 3; j++) {
37 >        oldEta(i, j) = eta(i, j);
38 >        }
39      }
40 <  }
61 <
40 >  
41   }
42  
43 < template<typename T> NPTf<T>::~NPTf() {
43 > void NPTf::evolveEtaB() {
44  
45 <  // empty for now
46 < }
45 >    int i;
46 >    int j;
47  
48 < template<typename T> void NPTf<T>::resetIntegrator() {
48 >    for(i = 0; i < 3; i++) {
49 >        for (j = 0; j < 3; j++) {
50 >            prevEta(i, j) = eta(i, j);
51 >        }
52 >    }
53  
54 <  int i, j;
55 <
56 <  for(i = 0; i < 3; i++)
57 <    for (j = 0; j < 3; j++)
58 <      eta[i][j] = 0.0;
59 <
60 <  T::resetIntegrator();
61 < }
62 <
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);
54 >    for(i = 0; i < 3; i ++){
55 >        for(j = 0; j < 3; j++){
56 >            if( i == j) {
57 >                eta(i, j) = oldEta(i, j) + dt2 *  instaVol *
58 >                (press(i, j) - targetPressure/OOPSEConstant::pressureConvert) / (NkBT*tb2);
59 >            } else {
60 >                eta(i, j) = oldEta(i, j) + dt2 * instaVol * press(i, j) / (NkBT*tb2);
61 >            }
62 >        }
63      }
64 <  }
64 >
65    
94  for(i = 0; i < 3; i++)
95    for (j = 0; j < 3; j++)
96      oldEta[i][j] = eta[i][j];
66   }
67  
68 < template<typename T> void NPTf<T>::evolveEtaB() {
68 > void NPTf::calcVelScale(){
69  
70 <  int i,j;
70 >  for (int i = 0; i < 3; i++ ) {
71 >    for (int j = 0; j < 3; j++ ) {
72 >      vScale(i, j) = eta(i, j);
73  
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
74        if (i == j) {
75 <        vScale[i][j] += chi;
75 >        vScale(i, j) += chi;
76        }
77      }
78    }
79   }
80  
81 < template<typename T> void NPTf<T>::getVelScaleA(double sc[3], double vel[3]) {
82 <
135 <  matVecMul3( vScale, vel, sc );
81 > void NPTf::getVelScaleA(Vector3d& sc, const Vector3d& vel){
82 >    sc = vScale * vel;
83   }
84  
85 < template<typename T> void NPTf<T>::getVelScaleB(double sc[3], int index ){
86 <  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 );
85 > void NPTf::getVelScaleB(Vector3d& sc, int index ) {
86 >  sc = vScale * oldVel[index];
87   }
88  
89 < 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];
89 > void NPTf::getPosScale(const Vector3d& pos, const Vector3d& COM, int index, Vector3d& sc) {
90  
91 <  for(j=0; j<3; j++)
92 <    rj[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j];
93 <
156 <  matVecMul3( eta, rj, sc );
91 >    /**@todo */
92 >    Vector3d rj = (oldPos[index] + pos)/2.0 -COM;
93 >    sc = eta * rj;
94   }
95  
96 < template<typename T> void NPTf<T>::scaleSimBox( void ){
96 > void NPTf::scaleSimBox(){
97  
98 <  int i,j,k;
99 <  double scaleMat[3][3];
98 >  int i;
99 >  int j;
100 >  int k;
101 >  Mat3x3d scaleMat;
102    double eta2ij;
103    double bigScale, smallScale, offDiagMax;
104 <  double hm[3][3], hmnew[3][3];
104 >  Mat3x3d hm;
105 >  Mat3x3d hmnew;
106  
107  
108  
# Line 183 | Line 123 | template<typename T> void NPTf<T>::scaleSimBox( void )
123  
124        eta2ij = 0.0;
125        for(k=0; k<3; k++){
126 <        eta2ij += eta[i][k] * eta[k][j];
126 >        eta2ij += eta(i, k) * eta(k, j);
127        }
128  
129 <      scaleMat[i][j] = 0.0;
129 >      scaleMat(i, j) = 0.0;
130        // identity matrix (see above):
131 <      if (i == j) scaleMat[i][j] = 1.0;
131 >      if (i == j) scaleMat(i, j) = 1.0;
132        // Taylor expansion for the exponential truncated at second order:
133 <      scaleMat[i][j] += dt*eta[i][j]  + 0.5*dt*dt*eta2ij;
133 >      scaleMat(i, j) += dt*eta(i, j)  + 0.5*dt*dt*eta2ij;
134        
135  
136        if (i != j)
137 <        if (fabs(scaleMat[i][j]) > offDiagMax)
138 <          offDiagMax = fabs(scaleMat[i][j]);
137 >        if (fabs(scaleMat(i, j)) > offDiagMax)
138 >          offDiagMax = fabs(scaleMat(i, j));
139      }
140  
141 <    if (scaleMat[i][i] > bigScale) bigScale = scaleMat[i][i];
142 <    if (scaleMat[i][i] < smallScale) smallScale = scaleMat[i][i];
141 >    if (scaleMat(i, i) > bigScale) bigScale = scaleMat(i, i);
142 >    if (scaleMat(i, i) < smallScale) smallScale = scaleMat(i, i);
143    }
144  
145    if ((bigScale > 1.01) || (smallScale < 0.99)) {
# Line 212 | Line 152 | template<typename T> void NPTf<T>::scaleSimBox( void )
152               "      eta = [%lf\t%lf\t%lf]\n"
153               "            [%lf\t%lf\t%lf]\n"
154               "            [%lf\t%lf\t%lf]\n",
155 <             scaleMat[0][0],scaleMat[0][1],scaleMat[0][2],
156 <             scaleMat[1][0],scaleMat[1][1],scaleMat[1][2],
157 <             scaleMat[2][0],scaleMat[2][1],scaleMat[2][2],
158 <             eta[0][0],eta[0][1],eta[0][2],
159 <             eta[1][0],eta[1][1],eta[1][2],
160 <             eta[2][0],eta[2][1],eta[2][2]);
155 >             scaleMat(0, 0),scaleMat(0, 1),scaleMat(0, 2),
156 >             scaleMat(1, 0),scaleMat(1, 1),scaleMat(1, 2),
157 >             scaleMat(2, 0),scaleMat(2, 1),scaleMat(2, 2),
158 >             eta(0, 0),eta(0, 1),eta(0, 2),
159 >             eta(1, 0),eta(1, 1),eta(1, 2),
160 >             eta(2, 0),eta(2, 1),eta(2, 2));
161      painCave.isFatal = 1;
162      simError();
163    } else if (offDiagMax > 0.01) {
# Line 230 | Line 170 | template<typename T> void NPTf<T>::scaleSimBox( void )
170               "      eta = [%lf\t%lf\t%lf]\n"
171               "            [%lf\t%lf\t%lf]\n"
172               "            [%lf\t%lf\t%lf]\n",
173 <             scaleMat[0][0],scaleMat[0][1],scaleMat[0][2],
174 <             scaleMat[1][0],scaleMat[1][1],scaleMat[1][2],
175 <             scaleMat[2][0],scaleMat[2][1],scaleMat[2][2],
176 <             eta[0][0],eta[0][1],eta[0][2],
177 <             eta[1][0],eta[1][1],eta[1][2],
178 <             eta[2][0],eta[2][1],eta[2][2]);
173 >             scaleMat(0, 0),scaleMat(0, 1),scaleMat(0, 2),
174 >             scaleMat(1, 0),scaleMat(1, 1),scaleMat(1, 2),
175 >             scaleMat(2, 0),scaleMat(2, 1),scaleMat(2, 2),
176 >             eta(0, 0),eta(0, 1),eta(0, 2),
177 >             eta(1, 0),eta(1, 1),eta(1, 2),
178 >             eta(2, 0),eta(2, 1),eta(2, 2));
179      painCave.isFatal = 1;
180      simError();
181    } else {
182 <    info->getBoxM(hm);
183 <    matMul3(hm, scaleMat, hmnew);
184 <    info->setBoxM(hmnew);
182 >
183 >        Mat3x3d hmat = currentSnapshot_->getHmat();
184 >        hmat = hmat *scaleMat;
185 >        currentSnapshot_->setHmat(hmat);
186 >        
187    }
188   }
189  
190 < template<typename T> bool NPTf<T>::etaConverged() {
191 <  int i;
192 <  double diffEta, sumEta;
190 > bool NPTf::etaConverged() {
191 >    int i;
192 >    double diffEta, sumEta;
193  
194 <  sumEta = 0;
195 <  for(i = 0; i < 3; i++)
196 <    sumEta += pow(prevEta[i][i] - eta[i][i], 2);
194 >    sumEta = 0;
195 >    for(i = 0; i < 3; i++) {
196 >        sumEta += pow(prevEta(i, i) - eta(i, i), 2);
197 >    }
198 >    
199 >    diffEta = sqrt( sumEta / 3.0 );
200  
201 <  diffEta = sqrt( sumEta / 3.0 );
257 <
258 <  return ( diffEta <= etaTolerance );
201 >    return ( diffEta <= etaTolerance );
202   }
203  
204 < template<typename T> double NPTf<T>::getConservedQuantity(void){
204 > double NPTf::calcConservedQuantity(){
205  
206 <  double conservedQuantity;
207 <  double totalEnergy;
208 <  double thermostat_kinetic;
209 <  double thermostat_potential;
210 <  double barostat_kinetic;
211 <  double barostat_potential;
212 <  double trEta;
213 <  double a[3][3], b[3][3];
206 >    chi= currentSnapshot_->getChi();
207 >    integralOfChidt = currentSnapshot_->getIntegralOfChiDt();
208 >    loadEta();
209 >    
210 >    // We need NkBT a lot, so just set it here: This is the RAW number
211 >    // of integrableObjects, so no subtraction or addition of constraints or
212 >    // orientational degrees of freedom:
213 >    NkBT = info_->getNGlobalIntegrableObjects()*OOPSEConstant::kB *targetTemp;
214  
215 <  totalEnergy = tStats->getTotalE();
215 >    // fkBT is used because the thermostat operates on more degrees of freedom
216 >    // than the barostat (when there are particles with orientational degrees
217 >    // of freedom).  
218 >    fkBT = info_->getNdf()*OOPSEConstant::kB *targetTemp;    
219 >    
220 >    double conservedQuantity;
221 >    double totalEnergy;
222 >    double thermostat_kinetic;
223 >    double thermostat_potential;
224 >    double barostat_kinetic;
225 >    double barostat_potential;
226 >    double trEta;
227  
228 <  thermostat_kinetic = fkBT * tt2 * chi * chi /
275 <    (2.0 * eConvert);
228 >    totalEnergy = thermo.getTotalE();
229  
230 <  thermostat_potential = fkBT* integralOfChidt / eConvert;
230 >    thermostat_kinetic = fkBT * tt2 * chi * chi /(2.0 * OOPSEConstant::energyConvert);
231  
232 <  transposeMat3(eta, a);
280 <  matMul3(a, eta, b);
281 <  trEta = matTrace3(b);
232 >    thermostat_potential = fkBT* integralOfChidt / OOPSEConstant::energyConvert;
233  
234 <  barostat_kinetic = NkBT * tb2 * trEta /
235 <    (2.0 * eConvert);
234 >    SquareMatrix<double, 3> tmp = eta.transpose() * eta;
235 >    trEta = tmp.trace();
236 >    
237 >    barostat_kinetic = NkBT * tb2 * trEta /(2.0 * OOPSEConstant::energyConvert);
238  
239 <  barostat_potential = (targetPressure * tStats->getVolume() / p_convert) /
287 <    eConvert;
239 >    barostat_potential = (targetPressure * thermo.getVolume() / OOPSEConstant::pressureConvert) /OOPSEConstant::energyConvert;
240  
241 <  conservedQuantity = totalEnergy + thermostat_kinetic + thermostat_potential +
242 <    barostat_kinetic + barostat_potential;
241 >    conservedQuantity = totalEnergy + thermostat_kinetic + thermostat_potential +
242 >        barostat_kinetic + barostat_potential;
243  
244 <  return conservedQuantity;
244 >    return conservedQuantity;
245  
246   }
247  
248 < template<typename T> string NPTf<T>::getAdditionalParameters(void){
249 <  string parameters;
298 <  const int BUFFERSIZE = 2000; // size of the read buffer
299 <  char buffer[BUFFERSIZE];
248 > void NPTf::loadEta() {
249 >    eta= currentSnapshot_->getEta();
250  
251 <  sprintf(buffer,"\t%G\t%G;", chi, integralOfChidt);
252 <  parameters += buffer;
251 >    //if (!eta.isDiagonal()) {
252 >    //    sprintf( painCave.errMsg,
253 >    //             "NPTf error: the diagonal elements of eta matrix are not the same or etaMat is not a diagonal matrix");
254 >    //    painCave.isFatal = 1;
255 >    //    simError();
256 >    //}
257 > }
258  
259 <  for(int i = 0; i < 3; i++){
260 <    sprintf(buffer,"\t%G\t%G\t%G;", eta[i][0], eta[i][1], eta[i][2]);
261 <    parameters += buffer;
307 <  }
259 > void NPTf::saveEta() {
260 >    currentSnapshot_->setEta(eta);
261 > }
262  
309  return parameters;
310
263   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines