ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-4/src/integrators/NVT.cpp
Revision: 1913
Committed: Mon Jan 10 22:04:20 2005 UTC (21 years, 5 months ago) by tim
File size: 7186 byte(s)
Log Message:
create a register module to register force fields, integrators and minimizers

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 1837
6 tim 1765 namespace oopse {
7 tim 1762
8 tim 1774 NVT::NVT(SimInfo* info) : VelocityVerletIntegrator(info), chiTolerance_ (1e-6) {
9 tim 1762
10 tim 1841 Globals* simParams = info_->getSimParams();
11 tim 1762
12 tim 1841 if (simParams->getUseInitXSstate()) {
13 tim 1774 Snapshot* currSnapshot = info_->getSnapshotManager()->getCurrentSnapshot();
14     currSnapshot->setChi(0.0);
15     currSnapshot->setIntegralOfChiDt(0.0);
16     }
17    
18 tim 1841 if (!simParams->haveTargetTemp()) {
19 tim 1774 sprintf(painCave.errMsg, "You can't use the NVT integrator without a targetTemp_!\n");
20     painCave.isFatal = 1;
21     painCave.severity = OOPSE_ERROR;
22     simError();
23     } else {
24 tim 1841 targetTemp_ = simParams->getTargetTemp();
25 tim 1774 }
26 tim 1762
27 tim 1774 // We must set tauThermostat_.
28 tim 1762
29 tim 1841 if (!simParams->haveTauThermostat()) {
30 tim 1774 sprintf(painCave.errMsg, "If you use the constant temperature\n"
31     "\tintegrator, you must set tauThermostat_.\n");
32 tim 1762
33 tim 1774 painCave.severity = OOPSE_ERROR;
34     painCave.isFatal = 1;
35     simError();
36     } else {
37 tim 1841 tauThermostat_ = simParams->getTauThermostat();
38 tim 1762 }
39    
40 tim 1774 update();
41 tim 1762 }
42    
43 tim 1867 void NVT::doUpdate() {
44 tim 1774 oldVel_.resize(info_->getNIntegrableObjects());
45     oldJi_.resize(info_->getNIntegrableObjects());
46     }
47 tim 1765 void NVT::moveA() {
48 tim 1821 SimInfo::MoleculeIterator i;
49     Molecule::IntegrableObjectIterator j;
50 tim 1765 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 tim 1867 double chi = currentSnapshot_->getChi();
60     double integralOfChidt = currentSnapshot_->getIntegralOfChiDt();
61 tim 1821
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 tim 1852 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 tim 1871 //convert the torque to body frame
90     Tb = integrableObject->lab2Body(integrableObject->getTrq());
91 tim 1762
92     // get the angular momentum, and propagate a half step
93    
94 tim 1765 ji = integrableObject->getJ();
95 tim 1762
96 tim 1821 //ji[j] += dt2 * (Tb[j] * OOPSEConstant::energyConvert - ji[j]*chi);
97     ji += dt2*OOPSEConstant::energyConvert*Tb - dt2*chi *ji;
98     rotAlgo->rotate(integrableObject, ji, dt);
99 tim 1762
100 tim 1765 integrableObject->setJ(ji);
101 tim 1762 }
102     }
103    
104 tim 1765 }
105    
106 tim 1901 rattle->constraintA();
107 tim 1762
108     // Finally, evolve chi a half step (just like a velocity) using
109     // temperature at time t, not time t+dt/2
110    
111 tim 1765
112 tim 1774 chi += dt2 * (instTemp / targetTemp_ - 1.0) / (tauThermostat_ * tauThermostat_);
113     integralOfChidt += chi * dt2;
114 tim 1762
115 tim 1867 currentSnapshot_->setChi(chi);
116     currentSnapshot_->setIntegralOfChiDt(integralOfChidt);
117 tim 1762 }
118    
119 tim 1765 void NVT::moveB() {
120 tim 1821 SimInfo::MoleculeIterator i;
121     Molecule::IntegrableObjectIterator j;
122 tim 1765 Molecule* mol;
123     StuntDouble* integrableObject;
124    
125     Vector3d Tb;
126     Vector3d ji;
127     Vector3d vel;
128     Vector3d frc;
129 tim 1762 double mass;
130     double instTemp;
131 tim 1765 int index;
132 tim 1762 // Set things up for the iteration:
133    
134 tim 1867 double chi = currentSnapshot_->getChi();
135 tim 1821 double oldChi = chi;
136     double prevChi;
137 tim 1867 double integralOfChidt = currentSnapshot_->getIntegralOfChiDt();
138 tim 1762
139 tim 1765 index = 0;
140     for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
141     for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
142     integrableObject = mol->nextIntegrableObject(j)) {
143     oldVel_[index] = integrableObject->getVel();
144     oldJi_[index] = integrableObject->getJ();
145 tim 1868
146     ++index;
147 tim 1762 }
148 tim 1868
149 tim 1762 }
150    
151     // do the iteration:
152    
153 tim 1765 for(int k = 0; k < maxIterNum_; k++) {
154     index = 0;
155 tim 1821 instTemp = thermo.getTemperature();
156 tim 1762
157     // evolve chi another half step using the temperature at t + dt/2
158    
159     prevChi = chi;
160 tim 1774 chi = oldChi + dt2 * (instTemp / targetTemp_ - 1.0) / (tauThermostat_ * tauThermostat_);
161 tim 1762
162 tim 1765 for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) {
163     for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL;
164     integrableObject = mol->nextIntegrableObject(j)) {
165 tim 1762
166 tim 1765 frc = integrableObject->getFrc();
167     vel = integrableObject->getVel();
168 tim 1762
169 tim 1765 mass = integrableObject->getMass();
170 tim 1762
171 tim 1765 // velocity half step
172     //for(j = 0; j < 3; j++)
173 tim 1821 // vel[j] = oldVel_[3*i+j] + dt2 * ((frc[j] / mass ) * OOPSEConstant::energyConvert - oldVel_[3*i + j]*chi);
174     vel = oldVel_[index] + dt2/mass*OOPSEConstant::energyConvert * frc - dt2*chi*oldVel_[index];
175 tim 1765
176     integrableObject->setVel(vel);
177 tim 1762
178 tim 1765 if (integrableObject->isDirectional()) {
179 tim 1762
180 tim 1765 // get and convert the torque to body frame
181 tim 1762
182 tim 1871 Tb = integrableObject->lab2Body(integrableObject->getTrq());
183 tim 1762
184 tim 1765 //for(j = 0; j < 3; j++)
185 tim 1821 // ji[j] = oldJi_[3*i + j] + dt2 * (Tb[j] * OOPSEConstant::energyConvert - oldJi_[3*i+j]*chi);
186 tim 1868 ji = oldJi_[index] + dt2*OOPSEConstant::energyConvert*Tb - dt2*chi *oldJi_[index];
187 tim 1762
188 tim 1765 integrableObject->setJ(ji);
189     }
190 tim 1852
191    
192     ++index;
193 tim 1762 }
194     }
195 tim 1765
196 tim 1762
197 tim 1901 rattle->constraintB();
198 tim 1762
199 tim 1821 if (fabs(prevChi - chi) <= chiTolerance_)
200 tim 1762 break;
201 tim 1765
202 tim 1762 }
203    
204 tim 1774 integralOfChidt += dt2 * chi;
205 tim 1762
206 tim 1867 currentSnapshot_->setChi(chi);
207     currentSnapshot_->setIntegralOfChiDt(integralOfChidt);
208 tim 1762 }
209    
210    
211 tim 1774 double NVT::calcConservedQuantity() {
212 tim 1867
213     double chi = currentSnapshot_->getChi();
214     double integralOfChidt = currentSnapshot_->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