# | Line 1 | Line 1 | |
---|---|---|
1 | + | #include <cmath> |
2 | #include "Atom.hpp" | |
3 | #include "SRI.hpp" | |
4 | #include "AbstractClasses.hpp" | |
# | Line 8 | Line 9 | |
9 | #include "Integrator.hpp" | |
10 | #include "simError.h" | |
11 | ||
12 | + | #ifdef IS_MPI |
13 | + | #include "mpiSimulation.hpp" |
14 | + | #endif |
15 | ||
16 | // Basic isotropic thermostating and barostating via the Melchionna | |
17 | // modification of the Hoover algorithm: | |
# | Line 19 | Line 23 | |
23 | // | |
24 | // Hoover, W. G., 1986, Phys. Rev. A, 34, 2499. | |
25 | ||
26 | < | NPTi::NPTi ( SimInfo *theInfo, ForceFields* the_ff): |
27 | < | Integrator( theInfo, the_ff ) |
26 | > | template<typename T> NPTi<T>::NPTi ( SimInfo *theInfo, ForceFields* the_ff): |
27 | > | T( theInfo, the_ff ) |
28 | { | |
25 | – | chi = 0.0; |
29 | eta = 0.0; | |
30 | < | have_tau_thermostat = 0; |
28 | < | have_tau_barostat = 0; |
29 | < | have_target_temp = 0; |
30 | < | have_target_pressure = 0; |
30 | > | oldEta = 0.0; |
31 | } | |
32 | ||
33 | < | void NPTi::moveA() { |
34 | < | |
35 | < | int i,j,k; |
36 | < | int atomIndex, aMatIndex; |
37 | < | DirectionalAtom* dAtom; |
38 | < | double Tb[3]; |
39 | < | double ji[3]; |
40 | < | double rj[3]; |
41 | < | double instaTemp, instaPress, instaVol; |
42 | < | double tt2, tb2; |
43 | < | double angle; |
33 | > | template<typename T> NPTi<T>::~NPTi() { |
34 | > | //nothing for now |
35 | > | } |
36 | ||
37 | < | tt2 = tauThermostat * tauThermostat; |
38 | < | tb2 = tauBarostat * tauBarostat; |
37 | > | template<typename T> void NPTi<T>::resetIntegrator() { |
38 | > | eta = 0.0; |
39 | > | T::resetIntegrator(); |
40 | > | } |
41 | ||
42 | < | instaTemp = tStats->getTemperature(); |
43 | < | instaPress = tStats->getPressure(); |
44 | < | instaVol = tStats->getVolume(); |
45 | < | |
46 | < | // first evolve chi a half step |
42 | > | template<typename T> void NPTi<T>::evolveEtaA() { |
43 | > | eta += dt2 * ( instaVol * (instaPress - targetPressure) / |
44 | > | (p_convert*NkBT*tb2)); |
45 | > | oldEta = eta; |
46 | > | } |
47 | > | |
48 | > | template<typename T> void NPTi<T>::evolveEtaB() { |
49 | ||
50 | < | chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; |
51 | < | eta += dt2 * ( instaVol * (instaPress - targetPressure) / (NkBT*tb2)); |
50 | > | prevEta = eta; |
51 | > | eta = oldEta + dt2 * ( instaVol * (instaPress - targetPressure) / |
52 | > | (p_convert*NkBT*tb2)); |
53 | > | } |
54 | ||
55 | < | for( i=0; i<nAtoms; i++ ){ |
56 | < | atomIndex = i * 3; |
59 | < | aMatIndex = i * 9; |
60 | < | |
61 | < | // velocity half step |
62 | < | for( j=atomIndex; j<(atomIndex+3); j++ ) |
63 | < | vel[j] += dt2 * ((frc[j]/atoms[i]->getMass())*eConvert |
64 | < | - vel[j]*(chi+eta)); |
55 | > | template<typename T> void NPTi<T>::getVelScaleA(double sc[3], double vel[3]) { |
56 | > | int i; |
57 | ||
58 | < | // position whole step |
58 | > | for(i=0; i<3; i++) sc[i] = vel[i] * ( chi + eta ); |
59 | > | } |
60 | ||
61 | < | rj[0] = pos[atomIndex]; |
62 | < | rj[1] = pos[atomIndex+1]; |
70 | < | rj[2] = pos[atomIndex+2]; |
71 | < | |
72 | < | info->wrapVector(rj); |
61 | > | template<typename T> void NPTi<T>::getVelScaleB(double sc[3], int index ){ |
62 | > | int i; |
63 | ||
64 | < | pos[atomIndex] += dt * (vel[atomIndex] + eta*rj[0]); |
65 | < | pos[atomIndex+1] += dt * (vel[atomIndex+1] + eta*rj[1]); |
76 | < | pos[atomIndex+2] += dt * (vel[atomIndex+2] + eta*rj[2]); |
77 | < | |
78 | < | if( atoms[i]->isDirectional() ){ |
64 | > | for(i=0; i<3; i++) sc[i] = oldVel[index*3 + i] * ( chi + eta ); |
65 | > | } |
66 | ||
80 | – | dAtom = (DirectionalAtom *)atoms[i]; |
81 | – | |
82 | – | // get and convert the torque to body frame |
83 | – | |
84 | – | Tb[0] = dAtom->getTx(); |
85 | – | Tb[1] = dAtom->getTy(); |
86 | – | Tb[2] = dAtom->getTz(); |
87 | – | |
88 | – | dAtom->lab2Body( Tb ); |
89 | – | |
90 | – | // get the angular momentum, and propagate a half step |
67 | ||
68 | < | ji[0] = dAtom->getJx(); |
69 | < | ji[1] = dAtom->getJy(); |
70 | < | ji[2] = dAtom->getJz(); |
95 | < | |
96 | < | ji[0] += dt2 * (Tb[0] * eConvert - ji[0]*chi); |
97 | < | ji[1] += dt2 * (Tb[1] * eConvert - ji[1]*chi); |
98 | < | ji[2] += dt2 * (Tb[2] * eConvert - ji[2]*chi); |
99 | < | |
100 | < | // use the angular velocities to propagate the rotation matrix a |
101 | < | // full time step |
102 | < | |
103 | < | // rotate about the x-axis |
104 | < | angle = dt2 * ji[0] / dAtom->getIxx(); |
105 | < | this->rotate( 1, 2, angle, ji, &Amat[aMatIndex] ); |
106 | < | |
107 | < | // rotate about the y-axis |
108 | < | angle = dt2 * ji[1] / dAtom->getIyy(); |
109 | < | this->rotate( 2, 0, angle, ji, &Amat[aMatIndex] ); |
110 | < | |
111 | < | // rotate about the z-axis |
112 | < | angle = dt * ji[2] / dAtom->getIzz(); |
113 | < | this->rotate( 0, 1, angle, ji, &Amat[aMatIndex] ); |
114 | < | |
115 | < | // rotate about the y-axis |
116 | < | angle = dt2 * ji[1] / dAtom->getIyy(); |
117 | < | this->rotate( 2, 0, angle, ji, &Amat[aMatIndex] ); |
118 | < | |
119 | < | // rotate about the x-axis |
120 | < | angle = dt2 * ji[0] / dAtom->getIxx(); |
121 | < | this->rotate( 1, 2, angle, ji, &Amat[aMatIndex] ); |
122 | < | |
123 | < | dAtom->setJx( ji[0] ); |
124 | < | dAtom->setJy( ji[1] ); |
125 | < | dAtom->setJz( ji[2] ); |
126 | < | } |
127 | < | |
128 | < | } |
129 | < | // Scale the box after all the positions have been moved: |
68 | > | template<typename T> void NPTi<T>::getPosScale(double pos[3], double COM[3], |
69 | > | int index, double sc[3]){ |
70 | > | int j; |
71 | ||
72 | < | info->scaleBox(exp(dt*eta)); |
72 | > | for(j=0; j<3; j++) |
73 | > | sc[j] = ( oldPos[index*3+j] + pos[j]) / 2.0 - COM[j]; |
74 | ||
75 | + | for(j=0; j<3; j++) |
76 | + | sc[j] *= eta; |
77 | } | |
78 | ||
79 | < | void NPTi::moveB( void ){ |
136 | < | int i,j,k; |
137 | < | int atomIndex; |
138 | < | DirectionalAtom* dAtom; |
139 | < | double Tb[3]; |
140 | < | double ji[3]; |
141 | < | double instaTemp, instaPress, instaVol; |
142 | < | double tt2, tb2; |
143 | < | |
144 | < | tt2 = tauThermostat * tauThermostat; |
145 | < | tb2 = tauBarostat * tauBarostat; |
79 | > | template<typename T> void NPTi<T>::scaleSimBox( void ){ |
80 | ||
81 | < | instaTemp = tStats->getTemperature(); |
148 | < | instaPress = tStats->getPressure(); |
149 | < | instaVol = tStats->getVolume(); |
81 | > | double scaleFactor; |
82 | ||
83 | < | chi += dt2 * ( instaTemp / targetTemp - 1.0) / tt2; |
152 | < | eta += dt2 * ( instaVol * (instaPress - targetPressure) / (NkBT*tb2)); |
153 | < | |
154 | < | for( i=0; i<nAtoms; i++ ){ |
155 | < | atomIndex = i * 3; |
156 | < | |
157 | < | // velocity half step |
158 | < | for( j=atomIndex; j<(atomIndex+3); j++ ) |
159 | < | for( j=atomIndex; j<(atomIndex+3); j++ ) |
160 | < | vel[j] += dt2 * ((frc[j]/atoms[i]->getMass())*eConvert |
161 | < | - vel[j]*(chi+eta)); |
162 | < | |
163 | < | if( atoms[i]->isDirectional() ){ |
164 | < | |
165 | < | dAtom = (DirectionalAtom *)atoms[i]; |
166 | < | |
167 | < | // get and convert the torque to body frame |
168 | < | |
169 | < | Tb[0] = dAtom->getTx(); |
170 | < | Tb[1] = dAtom->getTy(); |
171 | < | Tb[2] = dAtom->getTz(); |
172 | < | |
173 | < | dAtom->lab2Body( Tb ); |
174 | < | |
175 | < | // get the angular momentum, and complete the angular momentum |
176 | < | // half step |
177 | < | |
178 | < | ji[0] = dAtom->getJx(); |
179 | < | ji[1] = dAtom->getJy(); |
180 | < | ji[2] = dAtom->getJz(); |
181 | < | |
182 | < | ji[0] += dt2 * (Tb[0] * eConvert - ji[0]*chi); |
183 | < | ji[1] += dt2 * (Tb[1] * eConvert - ji[1]*chi); |
184 | < | ji[2] += dt2 * (Tb[2] * eConvert - ji[2]*chi); |
185 | < | |
186 | < | dAtom->setJx( ji[0] ); |
187 | < | dAtom->setJy( ji[1] ); |
188 | < | dAtom->setJz( ji[2] ); |
189 | < | } |
190 | < | } |
191 | < | } |
83 | > | scaleFactor = exp(dt*eta); |
84 | ||
85 | < | int NPTi::readyCheck() { |
194 | < | |
195 | < | // First check to see if we have a target temperature. |
196 | < | // Not having one is fatal. |
197 | < | |
198 | < | if (!have_target_temp) { |
85 | > | if ((scaleFactor > 1.1) || (scaleFactor < 0.9)) { |
86 | sprintf( painCave.errMsg, | |
87 | < | "NPTi error: You can't use the NPTi integrator\n" |
88 | < | " without a targetTemp!\n" |
87 | > | "NPTi error: Attempting a Box scaling of more than 10 percent" |
88 | > | " check your tauBarostat, as it is probably too small!\n" |
89 | > | " eta = %lf, scaleFactor = %lf\n", eta, scaleFactor |
90 | ); | |
91 | painCave.isFatal = 1; | |
92 | simError(); | |
93 | < | return -1; |
94 | < | } |
93 | > | } else { |
94 | > | info->scaleBox(scaleFactor); |
95 | > | } |
96 | ||
97 | < | if (!have_target_pressure) { |
98 | < | sprintf( painCave.errMsg, |
99 | < | "NPTi error: You can't use the NPTi integrator\n" |
100 | < | " without a targetPressure!\n" |
101 | < | ); |
102 | < | painCave.isFatal = 1; |
103 | < | simError(); |
104 | < | return -1; |
105 | < | } |
97 | > | } |
98 | > | |
99 | > | template<typename T> bool NPTi<T>::etaConverged() { |
100 | > | |
101 | > | return ( fabs(prevEta - eta) <= etaTolerance ); |
102 | > | } |
103 | > | |
104 | > | template<typename T> double NPTi<T>::getConservedQuantity(void){ |
105 | > | |
106 | > | double conservedQuantity; |
107 | > | double Energy; |
108 | > | double thermostat_kinetic; |
109 | > | double thermostat_potential; |
110 | > | double barostat_kinetic; |
111 | > | double barostat_potential; |
112 | ||
113 | < | // We must set tauThermostat. |
219 | < | |
220 | < | if (!have_tau_thermostat) { |
221 | < | sprintf( painCave.errMsg, |
222 | < | "NPTi error: If you use the NPTi\n" |
223 | < | " integrator, you must set tauThermostat.\n"); |
224 | < | painCave.isFatal = 1; |
225 | < | simError(); |
226 | < | return -1; |
227 | < | } |
113 | > | Energy = tStats->getTotalE(); |
114 | ||
115 | < | // We must set tauBarostat. |
116 | < | |
231 | < | if (!have_tau_barostat) { |
232 | < | sprintf( painCave.errMsg, |
233 | < | "NPTi error: If you use the NPTi\n" |
234 | < | " integrator, you must set tauBarostat.\n"); |
235 | < | painCave.isFatal = 1; |
236 | < | simError(); |
237 | < | return -1; |
238 | < | } |
115 | > | thermostat_kinetic = fkBT* tt2 * chi * chi / |
116 | > | (2.0 * eConvert); |
117 | ||
118 | < | // We need NkBT a lot, so just set it here: |
118 | > | thermostat_potential = fkBT* integralOfChidt / eConvert; |
119 | ||
242 | – | NkBT = (double)info->ndf * kB * targetTemp; |
120 | ||
121 | < | return 1; |
121 | > | barostat_kinetic = 3.0 * NkBT * tb2 * eta * eta / |
122 | > | (2.0 * eConvert); |
123 | > | |
124 | > | barostat_potential = (targetPressure * tStats->getVolume() / p_convert) / |
125 | > | eConvert; |
126 | > | |
127 | > | conservedQuantity = Energy + thermostat_kinetic + thermostat_potential + |
128 | > | barostat_kinetic + barostat_potential; |
129 | > | |
130 | > | // cout.width(8); |
131 | > | // cout.precision(8); |
132 | > | |
133 | > | // cerr << info->getTime() << "\t" << Energy << "\t" << thermostat_kinetic << |
134 | > | // "\t" << thermostat_potential << "\t" << barostat_kinetic << |
135 | > | // "\t" << barostat_potential << "\t" << conservedQuantity << endl; |
136 | > | return conservedQuantity; |
137 | } |
– | Removed lines |
+ | Added lines |
< | Changed lines |
> | Changed lines |