1 |
#include "brains/SimInfo.hpp" |
2 |
#include "brains/Thermo.hpp" |
3 |
#include "integrators/NPTxyz.hpp" |
4 |
#include "primitives/Molecule.hpp" |
5 |
#include "utils/OOPSEConstant.hpp" |
6 |
#include "utils/simError.h" |
7 |
|
8 |
// Basic non-isotropic thermostating and barostating via the Melchionna |
9 |
// modification of the Hoover algorithm: |
10 |
// |
11 |
// Melchionna, S., Ciccotti, G., and Holian, B. L., 1993, |
12 |
// Molec. Phys., 78, 533. |
13 |
// |
14 |
// and |
15 |
// |
16 |
// Hoover, W. G., 1986, Phys. Rev. A, 34, 2499. |
17 |
|
18 |
namespace oopse { |
19 |
/* |
20 |
NPTxyz::NPTxyz (SimInfo* info): NPT(info) { |
21 |
GenericData* data; |
22 |
DoubleVectorGenericData * etaValue; |
23 |
int i,j; |
24 |
|
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; |
30 |
} |
31 |
} |
32 |
|
33 |
|
34 |
if( theInfo->useInitXSstate ){ |
35 |
|
36 |
// retrieve eta array from simInfo if it exists |
37 |
data = info->getPropertyByName(ETAVALUE_ID); |
38 |
if(data){ |
39 |
etaValue = dynamic_cast<DoubleVectorGenericData*>(data); |
40 |
|
41 |
if(etaValue){ |
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); |
47 |
} |
48 |
} |
49 |
} |
50 |
} |
51 |
} |
52 |
} |
53 |
|
54 |
|
55 |
|
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 |
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 |
} |
69 |
|
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 |
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); |
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/OOPSEConstant::pressureConvert) / (NkBT*tb2); |
91 |
} else { |
92 |
eta(i, j) = 0.0; |
93 |
} |
94 |
} |
95 |
} |
96 |
} |
97 |
|
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); |
104 |
|
105 |
if (i == j) { |
106 |
vScale(i, j) += chi; |
107 |
} |
108 |
} |
109 |
} |
110 |
} |
111 |
|
112 |
|
113 |
void NPTxyz::getVelScaleA(Vector3d& sc, const Vector3d& vel) { |
114 |
sc = vScale * vel; |
115 |
} |
116 |
|
117 |
void NPTxyz::getVelScaleB(Vector3d& sc, int index ) { |
118 |
sc = vScale * oldVel[index]; |
119 |
} |
120 |
|
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 |
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 |
*/ |
142 |
|
143 |
double NPTxyz::calcConservedQuantity(){ |
144 |
|
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 |
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 |
|
175 |
void NPTxyz::scaleSimBox(){ |
176 |
|
177 |
int i,j,k; |
178 |
Mat3x3d scaleMat; |
179 |
double eta2ij, scaleFactor; |
180 |
double bigScale, smallScale, offDiagMax; |
181 |
Mat3x3d hm; |
182 |
Mat3x3d hmnew; |
183 |
|
184 |
|
185 |
|
186 |
// Scale the box after all the positions have been moved: |
187 |
|
188 |
// Use a taylor expansion for eta products: Hmat = Hmat . exp(dt * etaMat) |
189 |
// Hmat = Hmat . ( Ident + dt * etaMat + dt^2 * etaMat*etaMat / 2) |
190 |
|
191 |
bigScale = 1.0; |
192 |
smallScale = 1.0; |
193 |
offDiagMax = 0.0; |
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; |
199 |
} |
200 |
} |
201 |
|
202 |
for(i=0;i<3;i++){ |
203 |
|
204 |
// calculate the scaleFactors |
205 |
|
206 |
scaleFactor = exp(dt*eta(i, i)); |
207 |
|
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); |
212 |
} |
213 |
|
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" |
217 |
" Check your tauBarostat, as it is probably too small!\n\n" |
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)); |
224 |
painCave.isFatal = 1; |
225 |
simError(); |
226 |
} else { |
227 |
|
228 |
Mat3x3d hmat = currentSnapshot_->getHmat(); |
229 |
hmat = hmat *scaleMat; |
230 |
currentSnapshot_->setHmat(hmat); |
231 |
} |
232 |
} |
233 |
|
234 |
} |