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