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