ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/NVT.cpp
Revision: 1125
Committed: Mon Apr 19 22:13:01 2004 UTC (20 years, 2 months ago) by gezelter
File size: 6725 byte(s)
Log Message:
Fixed a charge bug

File Contents

# Content
1 #include <math.h>
2
3 #include "Atom.hpp"
4 #include "SRI.hpp"
5 #include "AbstractClasses.hpp"
6 #include "SimInfo.hpp"
7 #include "ForceFields.hpp"
8 #include "Thermo.hpp"
9 #include "ReadWrite.hpp"
10 #include "Integrator.hpp"
11 #include "simError.h"
12
13
14 // Basic thermostating via Hoover, Phys.Rev.A, 1985, Vol. 31 (5) 1695-1697
15
16 template<typename T> NVT<T>::NVT ( SimInfo *theInfo, ForceFields* the_ff):
17 T( theInfo, the_ff )
18 {
19 GenericData* data;
20 DoubleData * chiValue;
21 DoubleData * integralOfChidtValue;
22
23 chiValue = NULL;
24 integralOfChidtValue = NULL;
25
26 chi = 0.0;
27 have_tau_thermostat = 0;
28 have_target_temp = 0;
29 have_chi_tolerance = 0;
30 integralOfChidt = 0.0;
31
32
33 if( theInfo->useInitXSstate ){
34
35 // retrieve chi and integralOfChidt from simInfo
36 data = info->getProperty(CHIVALUE_ID);
37 if(data){
38 chiValue = dynamic_cast<DoubleData*>(data);
39 }
40
41 data = info->getProperty(INTEGRALOFCHIDT_ID);
42 if(data){
43 integralOfChidtValue = dynamic_cast<DoubleData*>(data);
44 }
45
46 // chi and integralOfChidt should appear by pair
47 if(chiValue && integralOfChidtValue){
48 chi = chiValue->getData();
49 integralOfChidt = integralOfChidtValue->getData();
50 }
51 }
52
53
54 std::cerr << "building oldVel with \t" << integrableObjects.size() << "\n";
55 oldVel = new double[3*integrableObjects.size()];
56 oldJi = new double[3*integrableObjects.size()];
57 }
58
59 template<typename T> NVT<T>::~NVT() {
60 delete[] oldVel;
61 delete[] oldJi;
62 }
63
64 template<typename T> void NVT<T>::moveA() {
65
66 int i, j;
67 DirectionalAtom* dAtom;
68 double Tb[3], ji[3];
69 double mass;
70 double vel[3], pos[3], frc[3];
71
72 double instTemp;
73
74 // We need the temperature at time = t for the chi update below:
75
76 instTemp = tStats->getTemperature();
77
78 for( i=0; i < integrableObjects.size(); i++ ){
79
80 integrableObjects[i]->getVel( vel );
81 integrableObjects[i]->getPos( pos );
82 integrableObjects[i]->getFrc( frc );
83
84 mass = integrableObjects[i]->getMass();
85
86 for (j=0; j < 3; j++) {
87 // velocity half step (use chi from previous step here):
88 vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*chi);
89 // position whole step
90 pos[j] += dt * vel[j];
91 }
92
93 integrableObjects[i]->setVel( vel );
94 integrableObjects[i]->setPos( pos );
95
96 if( integrableObjects[i]->isDirectional() ){
97
98 // get and convert the torque to body frame
99
100 integrableObjects[i]->getTrq( Tb );
101 integrableObjects[i]->lab2Body( Tb );
102
103 // get the angular momentum, and propagate a half step
104
105 integrableObjects[i]->getJ( ji );
106
107 for (j=0; j < 3; j++)
108 ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);
109
110 this->rotationPropagation( integrableObjects[i], ji );
111
112 integrableObjects[i]->setJ( ji );
113 }
114 }
115
116 if (nConstrained){
117 constrainA();
118 }
119
120 // Finally, evolve chi a half step (just like a velocity) using
121 // temperature at time t, not time t+dt/2
122
123 std::cerr << "targetTemp = " << targetTemp << " instTemp = " << instTemp << " tauThermostat = " << tauThermostat << " integral of Chi = " << integralOfChidt << "\n";
124
125 chi += dt2 * ( instTemp / targetTemp - 1.0) / (tauThermostat*tauThermostat);
126 integralOfChidt += chi*dt2;
127
128 }
129
130 template<typename T> void NVT<T>::moveB( void ){
131 int i, j, k;
132 double Tb[3], ji[3];
133 double vel[3], frc[3];
134 double mass;
135 double instTemp;
136 double oldChi, prevChi;
137
138 // Set things up for the iteration:
139
140 oldChi = chi;
141
142 for( i=0; i < integrableObjects.size(); i++ ){
143
144 integrableObjects[i]->getVel( vel );
145
146 for (j=0; j < 3; j++)
147 oldVel[3*i + j] = vel[j];
148
149 if( integrableObjects[i]->isDirectional() ){
150
151 integrableObjects[i]->getJ( ji );
152
153 for (j=0; j < 3; j++)
154 oldJi[3*i + j] = ji[j];
155
156 }
157 }
158
159 // do the iteration:
160
161 for (k=0; k < 4; k++) {
162
163 instTemp = tStats->getTemperature();
164
165 // evolve chi another half step using the temperature at t + dt/2
166
167 prevChi = chi;
168 chi = oldChi + dt2 * ( instTemp / targetTemp - 1.0) /
169 (tauThermostat*tauThermostat);
170
171 for( i=0; i < integrableObjects.size(); i++ ){
172
173 integrableObjects[i]->getFrc( frc );
174 integrableObjects[i]->getVel(vel);
175
176 mass = integrableObjects[i]->getMass();
177
178 // velocity half step
179 for (j=0; j < 3; j++)
180 vel[j] = oldVel[3*i+j] + dt2 * ((frc[j] / mass ) * eConvert - oldVel[3*i + j]*chi);
181
182 integrableObjects[i]->setVel( vel );
183
184 if( integrableObjects[i]->isDirectional() ){
185
186 // get and convert the torque to body frame
187
188 integrableObjects[i]->getTrq( Tb );
189 integrableObjects[i]->lab2Body( Tb );
190
191 for (j=0; j < 3; j++)
192 ji[j] = oldJi[3*i + j] + dt2 * (Tb[j] * eConvert - oldJi[3*i+j]*chi);
193
194 integrableObjects[i]->setJ( ji );
195 }
196 }
197
198 if (nConstrained){
199 constrainB();
200 }
201
202 if (fabs(prevChi - chi) <= chiTolerance) break;
203 }
204
205 integralOfChidt += dt2*chi;
206 }
207
208 template<typename T> void NVT<T>::resetIntegrator( void ){
209
210 chi = 0.0;
211 integralOfChidt = 0.0;
212 }
213
214 template<typename T> int NVT<T>::readyCheck() {
215
216 //check parent's readyCheck() first
217 if (T::readyCheck() == -1)
218 return -1;
219
220 // First check to see if we have a target temperature.
221 // Not having one is fatal.
222
223 if (!have_target_temp) {
224 sprintf( painCave.errMsg,
225 "NVT error: You can't use the NVT integrator without a targetTemp!\n"
226 );
227 painCave.isFatal = 1;
228 simError();
229 return -1;
230 }
231
232 // We must set tauThermostat.
233
234 if (!have_tau_thermostat) {
235 sprintf( painCave.errMsg,
236 "NVT error: If you use the constant temperature\n"
237 " integrator, you must set tauThermostat.\n");
238 painCave.isFatal = 1;
239 simError();
240 return -1;
241 }
242
243 if (!have_chi_tolerance) {
244 sprintf( painCave.errMsg,
245 "NVT warning: setting chi tolerance to 1e-6\n");
246 chiTolerance = 1e-6;
247 have_chi_tolerance = 1;
248 painCave.isFatal = 0;
249 simError();
250 }
251
252 return 1;
253
254 }
255
256 template<typename T> double NVT<T>::getConservedQuantity(void){
257
258 double conservedQuantity;
259 double fkBT;
260 double Energy;
261 double thermostat_kinetic;
262 double thermostat_potential;
263
264 fkBT = (double)(info->getNDF() ) * kB * targetTemp;
265
266 Energy = tStats->getTotalE();
267
268 thermostat_kinetic = fkBT* tauThermostat * tauThermostat * chi * chi /
269 (2.0 * eConvert);
270
271 thermostat_potential = fkBT * integralOfChidt / eConvert;
272
273 conservedQuantity = Energy + thermostat_kinetic + thermostat_potential;
274
275 return conservedQuantity;
276 }
277
278 template<typename T> string NVT<T>::getAdditionalParameters(void){
279 string parameters;
280 const int BUFFERSIZE = 2000; // size of the read buffer
281 char buffer[BUFFERSIZE];
282
283 sprintf(buffer,"\t%G\t%G;", chi, integralOfChidt);
284 parameters += buffer;
285
286 return parameters;
287 }