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: 1821
Committed: Thu Dec 2 00:26:23 2004 UTC (19 years, 9 months ago) by tim
File size: 7413 byte(s)
Log Message:
NVT get built

File Contents

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