ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-3.0/src/integrators/NVT.cpp
Revision: 1774
Committed: Tue Nov 23 23:12:23 2004 UTC (19 years, 9 months ago) by tim
File size: 6973 byte(s)
Log Message:
refactory NPT integrator

File Contents

# User Rev Content
1 tim 1765 #inlcude "integrators/NVT.hpp"
2 tim 1762
3 tim 1765 namespace oopse {
4 tim 1762
5 tim 1774 NVT::NVT(SimInfo* info) : VelocityVerletIntegrator(info), chiTolerance_ (1e-6) {
6 tim 1762
7 tim 1774 Globals* globals = info_->getGlobals();
8 tim 1762
9 tim 1774 if (globals->getUseInitXSstate()) {
10     Snapshot* currSnapshot = info_->getSnapshotManager()->getCurrentSnapshot();
11     currSnapshot->setChi(0.0);
12     currSnapshot->setIntegralOfChiDt(0.0);
13     }
14    
15     if (!globals->haveTargetTemp()) {
16     sprintf(painCave.errMsg, "You can't use the NVT integrator without a targetTemp_!\n");
17     painCave.isFatal = 1;
18     painCave.severity = OOPSE_ERROR;
19     simError();
20     } else {
21     targetTemp_ = globals->getTargetTemp();
22     }
23 tim 1762
24 tim 1774 // We must set tauThermostat_.
25 tim 1762
26 tim 1774 if (!globals->haveTauThermostat()) {
27     sprintf(painCave.errMsg, "If you use the constant temperature\n"
28     "\tintegrator, you must set tauThermostat_.\n");
29 tim 1762
30 tim 1774 painCave.severity = OOPSE_ERROR;
31     painCave.isFatal = 1;
32     simError();
33     } else {
34     tauThermostat_ = globals->getTauThermostat();
35 tim 1762 }
36    
37 tim 1774 update();
38 tim 1762 }
39    
40 tim 1765 NVT::~NVT() {
41 tim 1762 }
42    
43 tim 1774 void NVT::update() {
44     oldVel_.resize(info_->getNIntegrableObjects());
45     oldJi_.resize(info_->getNIntegrableObjects());
46     }
47 tim 1765 void NVT::moveA() {
48     typename SimInfo::MoleculeIterator i;
49     typename Molecule::IntegrableObjectIterator j;
50     Molecule* mol;
51     StuntDouble* integrableObject;
52 tim 1762 Vector3d Tb;
53     Vector3d ji;
54     double mass;
55     Vector3d vel;
56     Vector3d pos;
57     Vector3d frc;
58    
59     double instTemp;
60    
61     // We need the temperature at time = t for the chi update below:
62    
63     instTemp = tStats->getTemperature();
64    
65 tim 1765 for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
66     for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
67     integrableObject = mol->nextIntegrableObject(j)) {
68 tim 1762
69 tim 1765 vel = integrableObject->getVel();
70     pos = integrableObject->getPos();
71     frc = integrableObject->getFrc();
72 tim 1762
73 tim 1765 mass = integrableObject->getMass();
74 tim 1762
75 tim 1765 // velocity half step (use chi from previous step here):
76 tim 1774 //vel[j] += dt2 * ((frc[j] / mass ) * eConvert - vel[j]*chi);
77     vel = dt2 *eConvert/mass*frc - dt2*chi*vel;
78 tim 1765
79     // position whole step
80     //pos[j] += dt * vel[j];
81 tim 1774 pos += dt * vel;
82 tim 1762
83 tim 1765 integrableObject->setVel(vel);
84     integrableObject->setPos(pos);
85 tim 1762
86 tim 1765 if (integrableObject->isDirectional()) {
87 tim 1762
88     // get and convert the torque to body frame
89    
90 tim 1765 Tb = integrableObject->getTrq();
91     integrableObject->lab2Body(Tb);
92 tim 1762
93     // get the angular momentum, and propagate a half step
94    
95 tim 1765 ji = integrableObject->getJ();
96 tim 1762
97 tim 1774 //ji[j] += dt2 * (Tb[j] * eConvert - ji[j]*chi);
98     ji += dt2*eConvert*Tb - dt2*ch *ji;
99 tim 1765 this->rotationPropagation(integrableObject, ji);
100 tim 1762
101 tim 1765 integrableObject->setJ(ji);
102 tim 1762 }
103     }
104    
105 tim 1765 }
106    
107 tim 1774 //constraintAlgorithm->doConstrainA();
108 tim 1762
109     // Finally, evolve chi a half step (just like a velocity) using
110     // temperature at time t, not time t+dt/2
111    
112 tim 1765 Snapshot* currSnapshot = info_->getSnapshotManager()->getCurrentSnapshot();
113     double chi = currSnapshot->getChi();
114     double integralOfChidt = currSnapshot->getIntegralOfChiDt();
115    
116 tim 1774 chi += dt2 * (instTemp / targetTemp_ - 1.0) / (tauThermostat_ * tauThermostat_);
117     integralOfChidt += chi * dt2;
118 tim 1762
119 tim 1765 currSnapshot->setChi(chi);
120     currSnapshot->setIntegralOfChiDt(integralOfChidt);
121 tim 1762 }
122    
123 tim 1765 void NVT::moveB() {
124     typename SimInfo::MoleculeIterator i;
125     typename Molecule::IntegrableObjectIterator j;
126     Molecule* mol;
127     StuntDouble* integrableObject;
128    
129     Vector3d Tb;
130     Vector3d ji;
131     Vector3d vel;
132     Vector3d frc;
133 tim 1762 double mass;
134     double instTemp;
135     double oldChi, prevChi;
136 tim 1765 int index;
137 tim 1762 // Set things up for the iteration:
138    
139 tim 1765 Snapshot* currSnapshot = info_->getSnapshotManager()->getCurrentSnapshot();
140     double chi = currSnapshot->getChi();
141     double integralOfChidt = currSnapshot->getIntegralOfChiDt();
142 tim 1762 oldChi = chi;
143    
144 tim 1765 index = 0;
145     for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
146     for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
147     integrableObject = mol->nextIntegrableObject(j)) {
148     oldVel_[index] = integrableObject->getVel();
149     oldJi_[index] = integrableObject->getJ();
150 tim 1762 }
151 tim 1765 ++index;
152 tim 1762 }
153    
154     // do the iteration:
155    
156 tim 1765 for(int k = 0; k < maxIterNum_; k++) {
157     index = 0;
158 tim 1762 instTemp = tStats->getTemperature();
159    
160     // evolve chi another half step using the temperature at t + dt/2
161    
162     prevChi = chi;
163 tim 1774 chi = oldChi + dt2 * (instTemp / targetTemp_ - 1.0) / (tauThermostat_ * tauThermostat_);
164 tim 1762
165 tim 1765 for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
166     for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
167     integrableObject = mol->nextIntegrableObject(j)) {
168 tim 1762
169 tim 1765 frc = integrableObject->getFrc();
170     vel = integrableObject->getVel();
171 tim 1762
172 tim 1765 mass = integrableObject->getMass();
173 tim 1762
174 tim 1765 // velocity half step
175     //for(j = 0; j < 3; j++)
176 tim 1774 // vel[j] = oldVel_[3*i+j] + dt2 * ((frc[j] / mass ) * eConvert - oldVel_[3*i + j]*chi);
177     vel = oldVel_ + dt2/mass*eConvert * frc - dt2*chi*oldVel_[index];
178 tim 1765
179     integrableObject->setVel(vel);
180 tim 1762
181 tim 1765 if (integrableObject->isDirectional()) {
182 tim 1762
183 tim 1765 // get and convert the torque to body frame
184 tim 1762
185 tim 1765 Tb = integrableObject->getTrq();
186     integrableObject->lab2Body(Tb);
187 tim 1762
188 tim 1765 //for(j = 0; j < 3; j++)
189 tim 1774 // ji[j] = oldJi_[3*i + j] + dt2 * (Tb[j] * eConvert - oldJi_[3*i+j]*chi);
190     ji += dt2*eConvert*Tb - dt2*ch *oldJi_[index];
191 tim 1762
192 tim 1765 integrableObject->setJ(ji);
193     }
194 tim 1762 }
195     }
196 tim 1765
197 tim 1762
198 tim 1774 //constraintAlgorithm->doConstrainB();
199 tim 1762
200     if (fabs(prevChi - chi) <= chiTolerance)
201     break;
202 tim 1765
203     ++index;
204 tim 1762 }
205    
206 tim 1774 integralOfChidt += dt2 * chi;
207 tim 1762
208 tim 1765 currSnapshot->setChi(chi);
209     currSnapshot->setIntegralOfChiDt(integralOfChidt);
210 tim 1762 }
211    
212    
213 tim 1774 double NVT::calcConservedQuantity() {
214 tim 1762 double conservedQuantity;
215     double fkBT;
216     double Energy;
217     double thermostat_kinetic;
218     double thermostat_potential;
219    
220 tim 1765 fkBT = info_->getNdf() *kB *targetTemp_;
221 tim 1762
222     Energy = tStats->getTotalE();
223    
224 tim 1765 thermostat_kinetic = fkBT * tauThermostat_ * tauThermostat_ * chi * chi / (2.0 * eConvert);
225 tim 1762
226     thermostat_potential = fkBT * integralOfChidt / eConvert;
227    
228     conservedQuantity = Energy + thermostat_kinetic + thermostat_potential;
229    
230     return conservedQuantity;
231     }
232    
233    
234 tim 1765 }//end namespace oopse