ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-2.0/src/integrators/NVT.cpp
Revision: 1901
Committed: Tue Jan 4 22:18:36 2005 UTC (19 years, 6 months ago) by tim
File size: 7231 byte(s)
Log Message:
constraints in progress

File Contents

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