ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-2.0/src/integrators/NPTf.cpp
Revision: 1883
Committed: Mon Dec 13 22:30:27 2004 UTC (19 years, 6 months ago) by tim
File size: 7354 byte(s)
Log Message:
MPI version is built

File Contents

# Content
1 #include "brains/SimInfo.hpp"
2 #include "brains/Thermo.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 namespace oopse {
10
11 // Basic non-isotropic thermostating and barostating via the Melchionna
12 // modification of the Hoover algorithm:
13 //
14 // Melchionna, S., Ciccotti, G., and Holian, B. L., 1993,
15 // Molec. Phys., 78, 533.
16 //
17 // and
18 //
19 // Hoover, W. G., 1986, Phys. Rev. A, 34, 2499.
20
21 void NPTf::evolveEtaA() {
22
23 int i, j;
24
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 for(i = 0; i < 3; i++) {
36 for (j = 0; j < 3; j++) {
37 oldEta(i, j) = eta(i, j);
38 }
39 }
40
41 }
42
43 void NPTf::evolveEtaB() {
44
45 int i;
46 int j;
47
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 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
65
66 }
67
68 void NPTf::calcVelScale(){
69
70 for (int i = 0; i < 3; i++ ) {
71 for (int j = 0; j < 3; j++ ) {
72 vScale(i, j) = eta(i, j);
73
74 if (i == j) {
75 vScale(i, j) += chi;
76 }
77 }
78 }
79 }
80
81 void NPTf::getVelScaleA(Vector3d& sc, const Vector3d& vel){
82 sc = vScale * vel;
83 }
84
85 void NPTf::getVelScaleB(Vector3d& sc, int index ) {
86 sc = vScale * oldVel[index];
87 }
88
89 void NPTf::getPosScale(const Vector3d& pos, const Vector3d& COM, int index, Vector3d& sc) {
90
91 /**@todo */
92 Vector3d rj = (oldPos[index] + pos)/2.0 -COM;
93 sc = eta * rj;
94 }
95
96 void NPTf::scaleSimBox(){
97
98 int i;
99 int j;
100 int k;
101 Mat3x3d scaleMat;
102 double eta2ij;
103 double bigScale, smallScale, offDiagMax;
104 Mat3x3d hm;
105 Mat3x3d hmnew;
106
107
108
109 // Scale the box after all the positions have been moved:
110
111 // Use a taylor expansion for eta products: Hmat = Hmat . exp(dt * etaMat)
112 // Hmat = Hmat . ( Ident + dt * etaMat + dt^2 * etaMat*etaMat / 2)
113
114 bigScale = 1.0;
115 smallScale = 1.0;
116 offDiagMax = 0.0;
117
118 for(i=0; i<3; i++){
119 for(j=0; j<3; j++){
120
121 // Calculate the matrix Product of the eta array (we only need
122 // the ij element right now):
123
124 eta2ij = 0.0;
125 for(k=0; k<3; k++){
126 eta2ij += eta(i, k) * eta(k, j);
127 }
128
129 scaleMat(i, j) = 0.0;
130 // identity matrix (see above):
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;
134
135
136 if (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);
143 }
144
145 if ((bigScale > 1.01) || (smallScale < 0.99)) {
146 sprintf( painCave.errMsg,
147 "NPTf error: Attempting a Box scaling of more than 1 percent.\n"
148 " Check your tauBarostat, as it is probably too small!\n\n"
149 " scaleMat = [%lf\t%lf\t%lf]\n"
150 " [%lf\t%lf\t%lf]\n"
151 " [%lf\t%lf\t%lf]\n"
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));
161 painCave.isFatal = 1;
162 simError();
163 } else if (offDiagMax > 0.01) {
164 sprintf( painCave.errMsg,
165 "NPTf error: Attempting an off-diagonal Box scaling of more than 1 percent.\n"
166 " Check your tauBarostat, as it is probably too small!\n\n"
167 " scaleMat = [%lf\t%lf\t%lf]\n"
168 " [%lf\t%lf\t%lf]\n"
169 " [%lf\t%lf\t%lf]\n"
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));
179 painCave.isFatal = 1;
180 simError();
181 } else {
182
183 Mat3x3d hmat = currentSnapshot_->getHmat();
184 hmat = hmat *scaleMat;
185 currentSnapshot_->setHmat(hmat);
186
187 }
188 }
189
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);
197 }
198
199 diffEta = sqrt( sumEta / 3.0 );
200
201 return ( diffEta <= etaTolerance );
202 }
203
204 double NPTf::calcConservedQuantity(){
205
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 // 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 totalEnergy = thermo.getTotalE();
229
230 thermostat_kinetic = fkBT * tt2 * chi * chi /(2.0 * OOPSEConstant::energyConvert);
231
232 thermostat_potential = fkBT* integralOfChidt / OOPSEConstant::energyConvert;
233
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 * thermo.getVolume() / OOPSEConstant::pressureConvert) /OOPSEConstant::energyConvert;
240
241 conservedQuantity = totalEnergy + thermostat_kinetic + thermostat_potential +
242 barostat_kinetic + barostat_potential;
243
244 return conservedQuantity;
245
246 }
247
248 void NPTf::loadEta() {
249 eta= currentSnapshot_->getEta();
250
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 void NPTf::saveEta() {
260 currentSnapshot_->setEta(eta);
261 }
262
263 }