# | Line 1 | Line 1 | |
---|---|---|
1 | #include <iostream> | |
2 | < | #include <cstdlib> |
3 | < | #include <cmath> |
2 | > | #include <stdlib.h> |
3 | > | #include <math.h> |
4 | ||
5 | #ifdef IS_MPI | |
6 | #include "mpiSimulation.hpp" | |
7 | #include <unistd.h> | |
8 | #endif //is_mpi | |
9 | ||
10 | + | #ifdef PROFILE |
11 | + | #include "mdProfile.hpp" |
12 | + | #endif // profile |
13 | + | |
14 | #include "Integrator.hpp" | |
15 | #include "simError.h" | |
16 | ||
17 | ||
18 | < | Integrator::Integrator( SimInfo *theInfo, ForceFields* the_ff ){ |
19 | < | |
18 | > | template<typename T> Integrator<T>::Integrator(SimInfo* theInfo, |
19 | > | ForceFields* the_ff){ |
20 | info = theInfo; | |
21 | myFF = the_ff; | |
22 | isFirst = 1; | |
# | Line 21 | Line 25 | Integrator::Integrator( SimInfo *theInfo, ForceFields* | |
25 | nMols = info->n_mol; | |
26 | ||
27 | // give a little love back to the SimInfo object | |
24 | – | |
25 | – | if( info->the_integrator != NULL ) delete info->the_integrator; |
26 | – | info->the_integrator = this; |
28 | ||
29 | + | if (info->the_integrator != NULL){ |
30 | + | delete info->the_integrator; |
31 | + | } |
32 | + | |
33 | nAtoms = info->n_atoms; | |
34 | ||
35 | // check for constraints | |
36 | < | |
37 | < | constrainedA = NULL; |
38 | < | constrainedB = NULL; |
36 | > | |
37 | > | constrainedA = NULL; |
38 | > | constrainedB = NULL; |
39 | constrainedDsqr = NULL; | |
40 | < | moving = NULL; |
41 | < | moved = NULL; |
42 | < | oldPos = NULL; |
43 | < | |
40 | > | moving = NULL; |
41 | > | moved = NULL; |
42 | > | oldPos = NULL; |
43 | > | |
44 | nConstrained = 0; | |
45 | ||
46 | checkConstraints(); | |
47 | } | |
48 | ||
49 | < | Integrator::~Integrator() { |
50 | < | |
46 | < | if( nConstrained ){ |
49 | > | template<typename T> Integrator<T>::~Integrator(){ |
50 | > | if (nConstrained){ |
51 | delete[] constrainedA; | |
52 | delete[] constrainedB; | |
53 | delete[] constrainedDsqr; | |
# | Line 51 | Line 55 | Integrator::~Integrator() { | |
55 | delete[] moved; | |
56 | delete[] oldPos; | |
57 | } | |
54 | – | |
58 | } | |
59 | ||
60 | < | void Integrator::checkConstraints( void ){ |
58 | < | |
59 | < | |
60 | > | template<typename T> void Integrator<T>::checkConstraints(void){ |
61 | isConstrained = 0; | |
62 | ||
63 | < | Constraint *temp_con; |
64 | < | Constraint *dummy_plug; |
63 | > | Constraint* temp_con; |
64 | > | Constraint* dummy_plug; |
65 | temp_con = new Constraint[info->n_SRI]; | |
66 | nConstrained = 0; | |
67 | int constrained = 0; | |
68 | < | |
68 | > | |
69 | SRI** theArray; | |
70 | < | for(int i = 0; i < nMols; i++){ |
71 | < | |
72 | < | theArray = (SRI**) molecules[i].getMyBonds(); |
72 | < | for(int j=0; j<molecules[i].getNBonds(); j++){ |
73 | < | |
70 | > | for (int i = 0; i < nMols; i++){ |
71 | > | theArray = (SRI * *) molecules[i].getMyBonds(); |
72 | > | for (int j = 0; j < molecules[i].getNBonds(); j++){ |
73 | constrained = theArray[j]->is_constrained(); | |
74 | < | |
75 | < | if(constrained){ |
76 | < | |
77 | < | dummy_plug = theArray[j]->get_constraint(); |
78 | < | temp_con[nConstrained].set_a( dummy_plug->get_a() ); |
79 | < | temp_con[nConstrained].set_b( dummy_plug->get_b() ); |
80 | < | temp_con[nConstrained].set_dsqr( dummy_plug->get_dsqr() ); |
81 | < | |
82 | < | nConstrained++; |
84 | < | constrained = 0; |
74 | > | |
75 | > | if (constrained){ |
76 | > | dummy_plug = theArray[j]->get_constraint(); |
77 | > | temp_con[nConstrained].set_a(dummy_plug->get_a()); |
78 | > | temp_con[nConstrained].set_b(dummy_plug->get_b()); |
79 | > | temp_con[nConstrained].set_dsqr(dummy_plug->get_dsqr()); |
80 | > | |
81 | > | nConstrained++; |
82 | > | constrained = 0; |
83 | } | |
84 | } | |
85 | ||
86 | < | theArray = (SRI**) molecules[i].getMyBends(); |
87 | < | for(int j=0; j<molecules[i].getNBends(); j++){ |
90 | < | |
86 | > | theArray = (SRI * *) molecules[i].getMyBends(); |
87 | > | for (int j = 0; j < molecules[i].getNBends(); j++){ |
88 | constrained = theArray[j]->is_constrained(); | |
89 | < | |
90 | < | if(constrained){ |
91 | < | |
92 | < | dummy_plug = theArray[j]->get_constraint(); |
93 | < | temp_con[nConstrained].set_a( dummy_plug->get_a() ); |
94 | < | temp_con[nConstrained].set_b( dummy_plug->get_b() ); |
95 | < | temp_con[nConstrained].set_dsqr( dummy_plug->get_dsqr() ); |
96 | < | |
97 | < | nConstrained++; |
101 | < | constrained = 0; |
89 | > | |
90 | > | if (constrained){ |
91 | > | dummy_plug = theArray[j]->get_constraint(); |
92 | > | temp_con[nConstrained].set_a(dummy_plug->get_a()); |
93 | > | temp_con[nConstrained].set_b(dummy_plug->get_b()); |
94 | > | temp_con[nConstrained].set_dsqr(dummy_plug->get_dsqr()); |
95 | > | |
96 | > | nConstrained++; |
97 | > | constrained = 0; |
98 | } | |
99 | } | |
100 | ||
101 | < | theArray = (SRI**) molecules[i].getMyTorsions(); |
102 | < | for(int j=0; j<molecules[i].getNTorsions(); j++){ |
107 | < | |
101 | > | theArray = (SRI * *) molecules[i].getMyTorsions(); |
102 | > | for (int j = 0; j < molecules[i].getNTorsions(); j++){ |
103 | constrained = theArray[j]->is_constrained(); | |
104 | < | |
105 | < | if(constrained){ |
106 | < | |
107 | < | dummy_plug = theArray[j]->get_constraint(); |
108 | < | temp_con[nConstrained].set_a( dummy_plug->get_a() ); |
109 | < | temp_con[nConstrained].set_b( dummy_plug->get_b() ); |
110 | < | temp_con[nConstrained].set_dsqr( dummy_plug->get_dsqr() ); |
111 | < | |
112 | < | nConstrained++; |
118 | < | constrained = 0; |
104 | > | |
105 | > | if (constrained){ |
106 | > | dummy_plug = theArray[j]->get_constraint(); |
107 | > | temp_con[nConstrained].set_a(dummy_plug->get_a()); |
108 | > | temp_con[nConstrained].set_b(dummy_plug->get_b()); |
109 | > | temp_con[nConstrained].set_dsqr(dummy_plug->get_dsqr()); |
110 | > | |
111 | > | nConstrained++; |
112 | > | constrained = 0; |
113 | } | |
114 | } | |
115 | } | |
116 | ||
117 | < | if(nConstrained > 0){ |
124 | < | |
117 | > | if (nConstrained > 0){ |
118 | isConstrained = 1; | |
119 | ||
120 | < | if(constrainedA != NULL ) delete[] constrainedA; |
121 | < | if(constrainedB != NULL ) delete[] constrainedB; |
122 | < | if(constrainedDsqr != NULL ) delete[] constrainedDsqr; |
120 | > | if (constrainedA != NULL) |
121 | > | delete[] constrainedA; |
122 | > | if (constrainedB != NULL) |
123 | > | delete[] constrainedB; |
124 | > | if (constrainedDsqr != NULL) |
125 | > | delete[] constrainedDsqr; |
126 | ||
127 | < | constrainedA = new int[nConstrained]; |
128 | < | constrainedB = new int[nConstrained]; |
127 | > | constrainedA = new int[nConstrained]; |
128 | > | constrainedB = new int[nConstrained]; |
129 | constrainedDsqr = new double[nConstrained]; | |
130 | < | |
131 | < | for( int i = 0; i < nConstrained; i++){ |
136 | < | |
130 | > | |
131 | > | for (int i = 0; i < nConstrained; i++){ |
132 | constrainedA[i] = temp_con[i].get_a(); | |
133 | constrainedB[i] = temp_con[i].get_b(); | |
134 | constrainedDsqr[i] = temp_con[i].get_dsqr(); | |
140 | – | |
135 | } | |
136 | ||
137 | < | |
138 | < | // save oldAtoms to check for lode balanceing later on. |
139 | < | |
137 | > | |
138 | > | // save oldAtoms to check for lode balancing later on. |
139 | > | |
140 | oldAtoms = nAtoms; | |
141 | < | |
141 | > | |
142 | moving = new int[nAtoms]; | |
143 | < | moved = new int[nAtoms]; |
143 | > | moved = new int[nAtoms]; |
144 | ||
145 | < | oldPos = new double[nAtoms*3]; |
145 | > | oldPos = new double[nAtoms * 3]; |
146 | } | |
147 | < | |
147 | > | |
148 | delete[] temp_con; | |
149 | } | |
150 | ||
151 | ||
152 | < | void Integrator::integrate( void ){ |
152 | > | template<typename T> void Integrator<T>::integrate(void){ |
153 | ||
154 | < | int i, j; // loop counters |
155 | < | |
156 | < | double runTime = info->run_time; |
163 | < | double sampleTime = info->sampleTime; |
164 | < | double statusTime = info->statusTime; |
154 | > | double runTime = info->run_time; |
155 | > | double sampleTime = info->sampleTime; |
156 | > | double statusTime = info->statusTime; |
157 | double thermalTime = info->thermalTime; | |
158 | + | double resetTime = info->resetTime; |
159 | ||
160 | + | |
161 | double currSample; | |
162 | double currThermal; | |
163 | double currStatus; | |
164 | < | double currTime; |
164 | > | double currReset; |
165 | ||
166 | int calcPot, calcStress; | |
173 | – | int isError; |
167 | ||
168 | + | tStats = new Thermo(info); |
169 | + | statOut = new StatWriter(info); |
170 | + | dumpOut = new DumpWriter(info); |
171 | ||
176 | – | |
177 | – | tStats = new Thermo( info ); |
178 | – | statOut = new StatWriter( info ); |
179 | – | dumpOut = new DumpWriter( info ); |
180 | – | |
172 | atoms = info->atoms; | |
182 | – | DirectionalAtom* dAtom; |
173 | ||
174 | dt = info->dt; | |
175 | dt2 = 0.5 * dt; | |
176 | ||
177 | + | readyCheck(); |
178 | + | |
179 | // initialize the forces before the first step | |
180 | ||
181 | < | myFF->doForces(1,1); |
182 | < | |
183 | < | if( info->setTemp ){ |
184 | < | |
185 | < | tStats->velocitize(); |
181 | > | calcForce(1, 1); |
182 | > | |
183 | > | if (nConstrained){ |
184 | > | preMove(); |
185 | > | constrainA(); |
186 | > | calcForce(1, 1); |
187 | > | constrainB(); |
188 | } | |
189 | ||
190 | < | dumpOut->writeDump( 0.0 ); |
191 | < | statOut->writeStat( 0.0 ); |
192 | < | |
190 | > | if (info->setTemp){ |
191 | > | thermalize(); |
192 | > | } |
193 | > | |
194 | calcPot = 0; | |
195 | calcStress = 0; | |
196 | < | currSample = sampleTime; |
197 | < | currThermal = thermalTime; |
198 | < | currStatus = statusTime; |
199 | < | currTime = 0.0;; |
196 | > | currSample = sampleTime + info->getTime(); |
197 | > | currThermal = thermalTime+ info->getTime(); |
198 | > | currStatus = statusTime + info->getTime(); |
199 | > | currReset = resetTime + info->getTime(); |
200 | ||
201 | + | dumpOut->writeDump(info->getTime()); |
202 | + | statOut->writeStat(info->getTime()); |
203 | ||
207 | – | readyCheck(); |
204 | ||
205 | #ifdef IS_MPI | |
206 | < | strcpy( checkPointMsg, |
211 | < | "The integrator is ready to go." ); |
206 | > | strcpy(checkPointMsg, "The integrator is ready to go."); |
207 | MPIcheckPoint(); | |
208 | #endif // is_mpi | |
209 | ||
210 | < | |
211 | < | pos = Atom::getPosArray(); |
217 | < | vel = Atom::getVelArray(); |
218 | < | frc = Atom::getFrcArray(); |
219 | < | trq = Atom::getTrqArray(); |
220 | < | Amat = Atom::getAmatArray(); |
221 | < | |
222 | < | while( currTime < runTime ){ |
223 | < | |
224 | < | if( (currTime+dt) >= currStatus ){ |
210 | > | while (info->getTime() < runTime){ |
211 | > | if ((info->getTime() + dt) >= currStatus){ |
212 | calcPot = 1; | |
213 | calcStress = 1; | |
214 | } | |
215 | ||
216 | < | integrateStep( calcPot, calcStress ); |
217 | < | |
218 | < | currTime += dt; |
216 | > | #ifdef PROFILE |
217 | > | startProfile( pro1 ); |
218 | > | #endif |
219 | > | |
220 | > | integrateStep(calcPot, calcStress); |
221 | ||
222 | < | if( info->setTemp ){ |
223 | < | if( currTime >= currThermal ){ |
224 | < | tStats->velocitize(); |
225 | < | currThermal += thermalTime; |
222 | > | #ifdef PROFILE |
223 | > | endProfile( pro1 ); |
224 | > | |
225 | > | startProfile( pro2 ); |
226 | > | #endif // profile |
227 | > | |
228 | > | info->incrTime(dt); |
229 | > | |
230 | > | if (info->setTemp){ |
231 | > | if (info->getTime() >= currThermal){ |
232 | > | thermalize(); |
233 | > | currThermal += thermalTime; |
234 | } | |
235 | } | |
236 | ||
237 | < | if( currTime >= currSample ){ |
238 | < | dumpOut->writeDump( currTime ); |
237 | > | if (info->getTime() >= currSample){ |
238 | > | dumpOut->writeDump(info->getTime()); |
239 | currSample += sampleTime; | |
240 | } | |
241 | ||
242 | < | if( currTime >= currStatus ){ |
243 | < | statOut->writeStat( currTime ); |
244 | < | calcPot = 0; |
242 | > | if (info->getTime() >= currStatus){ |
243 | > | statOut->writeStat(info->getTime()); |
244 | > | calcPot = 0; |
245 | calcStress = 0; | |
246 | currStatus += statusTime; | |
247 | < | } |
247 | > | } |
248 | ||
249 | + | if (info->resetIntegrator){ |
250 | + | if (info->getTime() >= currReset){ |
251 | + | this->resetIntegrator(); |
252 | + | currReset += resetTime; |
253 | + | } |
254 | + | } |
255 | + | |
256 | + | #ifdef PROFILE |
257 | + | endProfile( pro2 ); |
258 | + | #endif //profile |
259 | + | |
260 | #ifdef IS_MPI | |
261 | < | strcpy( checkPointMsg, |
254 | < | "successfully took a time step." ); |
261 | > | strcpy(checkPointMsg, "successfully took a time step."); |
262 | MPIcheckPoint(); | |
263 | #endif // is_mpi | |
257 | – | |
264 | } | |
265 | ||
260 | – | dumpOut->writeFinal(currTime); |
261 | – | |
266 | delete dumpOut; | |
267 | delete statOut; | |
268 | } | |
269 | ||
270 | < | void Integrator::integrateStep( int calcPot, int calcStress ){ |
271 | < | |
268 | < | |
269 | < | |
270 | > | template<typename T> void Integrator<T>::integrateStep(int calcPot, |
271 | > | int calcStress){ |
272 | // Position full step, and velocity half step | |
273 | ||
274 | + | #ifdef PROFILE |
275 | + | startProfile(pro3); |
276 | + | #endif //profile |
277 | + | |
278 | preMove(); | |
279 | + | |
280 | + | #ifdef PROFILE |
281 | + | endProfile(pro3); |
282 | + | |
283 | + | startProfile(pro4); |
284 | + | #endif // profile |
285 | + | |
286 | moveA(); | |
274 | – | if( nConstrained ) constrainA(); |
287 | ||
288 | + | #ifdef PROFILE |
289 | + | endProfile(pro4); |
290 | + | |
291 | + | startProfile(pro5); |
292 | + | #endif//profile |
293 | + | |
294 | + | |
295 | + | #ifdef IS_MPI |
296 | + | strcpy(checkPointMsg, "Succesful moveA\n"); |
297 | + | MPIcheckPoint(); |
298 | + | #endif // is_mpi |
299 | + | |
300 | + | |
301 | // calc forces | |
302 | ||
303 | < | myFF->doForces(calcPot,calcStress); |
303 | > | calcForce(calcPot, calcStress); |
304 | ||
305 | + | #ifdef IS_MPI |
306 | + | strcpy(checkPointMsg, "Succesful doForces\n"); |
307 | + | MPIcheckPoint(); |
308 | + | #endif // is_mpi |
309 | + | |
310 | + | #ifdef PROFILE |
311 | + | endProfile( pro5 ); |
312 | + | |
313 | + | startProfile( pro6 ); |
314 | + | #endif //profile |
315 | + | |
316 | // finish the velocity half step | |
317 | < | |
317 | > | |
318 | moveB(); | |
319 | < | if( nConstrained ) constrainB(); |
320 | < | |
319 | > | |
320 | > | #ifdef PROFILE |
321 | > | endProfile(pro6); |
322 | > | #endif // profile |
323 | > | |
324 | > | #ifdef IS_MPI |
325 | > | strcpy(checkPointMsg, "Succesful moveB\n"); |
326 | > | MPIcheckPoint(); |
327 | > | #endif // is_mpi |
328 | } | |
329 | ||
330 | ||
331 | < | void Integrator::moveA( void ){ |
332 | < | |
290 | < | int i,j,k; |
291 | < | int atomIndex, aMatIndex; |
331 | > | template<typename T> void Integrator<T>::moveA(void){ |
332 | > | int i, j; |
333 | DirectionalAtom* dAtom; | |
334 | < | double Tb[3]; |
335 | < | double ji[3]; |
336 | < | double angle; |
334 | > | double Tb[3], ji[3]; |
335 | > | double vel[3], pos[3], frc[3]; |
336 | > | double mass; |
337 | ||
338 | + | for (i = 0; i < nAtoms; i++){ |
339 | + | atoms[i]->getVel(vel); |
340 | + | atoms[i]->getPos(pos); |
341 | + | atoms[i]->getFrc(frc); |
342 | ||
343 | + | mass = atoms[i]->getMass(); |
344 | ||
345 | < | for( i=0; i<nAtoms; i++ ){ |
346 | < | atomIndex = i * 3; |
347 | < | aMatIndex = i * 9; |
345 | > | for (j = 0; j < 3; j++){ |
346 | > | // velocity half step |
347 | > | vel[j] += (dt2 * frc[j] / mass) * eConvert; |
348 | > | // position whole step |
349 | > | pos[j] += dt * vel[j]; |
350 | > | } |
351 | ||
352 | < | // velocity half step |
353 | < | for( j=atomIndex; j<(atomIndex+3); j++ ) |
305 | < | vel[j] += ( dt2 * frc[j] / atoms[i]->getMass() ) * eConvert; |
352 | > | atoms[i]->setVel(vel); |
353 | > | atoms[i]->setPos(pos); |
354 | ||
355 | < | // position whole step |
356 | < | for( j=atomIndex; j<(atomIndex+3); j++ ) pos[j] += dt * vel[j]; |
309 | < | |
310 | < | if( atoms[i]->isDirectional() ){ |
355 | > | if (atoms[i]->isDirectional()){ |
356 | > | dAtom = (DirectionalAtom *) atoms[i]; |
357 | ||
312 | – | dAtom = (DirectionalAtom *)atoms[i]; |
313 | – | |
358 | // get and convert the torque to body frame | |
359 | < | |
360 | < | Tb[0] = dAtom->getTx(); |
361 | < | Tb[1] = dAtom->getTy(); |
362 | < | Tb[2] = dAtom->getTz(); |
319 | < | |
320 | < | dAtom->lab2Body( Tb ); |
321 | < | |
359 | > | |
360 | > | dAtom->getTrq(Tb); |
361 | > | dAtom->lab2Body(Tb); |
362 | > | |
363 | // get the angular momentum, and propagate a half step | |
364 | < | |
365 | < | ji[0] = dAtom->getJx() + ( dt2 * Tb[0] ) * eConvert; |
366 | < | ji[1] = dAtom->getJy() + ( dt2 * Tb[1] ) * eConvert; |
367 | < | ji[2] = dAtom->getJz() + ( dt2 * Tb[2] ) * eConvert; |
368 | < | |
369 | < | // use the angular velocities to propagate the rotation matrix a |
370 | < | // full time step |
371 | < | |
372 | < | // rotate about the x-axis |
332 | < | angle = dt2 * ji[0] / dAtom->getIxx(); |
333 | < | this->rotate( 1, 2, angle, ji, &Amat[aMatIndex] ); |
334 | < | |
335 | < | // rotate about the y-axis |
336 | < | angle = dt2 * ji[1] / dAtom->getIyy(); |
337 | < | this->rotate( 2, 0, angle, ji, &Amat[aMatIndex] ); |
338 | < | |
339 | < | // rotate about the z-axis |
340 | < | angle = dt * ji[2] / dAtom->getIzz(); |
341 | < | this->rotate( 0, 1, angle, ji, &Amat[aMatIndex] ); |
342 | < | |
343 | < | // rotate about the y-axis |
344 | < | angle = dt2 * ji[1] / dAtom->getIyy(); |
345 | < | this->rotate( 2, 0, angle, ji, &Amat[aMatIndex] ); |
346 | < | |
347 | < | // rotate about the x-axis |
348 | < | angle = dt2 * ji[0] / dAtom->getIxx(); |
349 | < | this->rotate( 1, 2, angle, ji, &Amat[aMatIndex] ); |
350 | < | |
351 | < | dAtom->setJx( ji[0] ); |
352 | < | dAtom->setJy( ji[1] ); |
353 | < | dAtom->setJz( ji[2] ); |
364 | > | |
365 | > | dAtom->getJ(ji); |
366 | > | |
367 | > | for (j = 0; j < 3; j++) |
368 | > | ji[j] += (dt2 * Tb[j]) * eConvert; |
369 | > | |
370 | > | this->rotationPropagation( dAtom, ji ); |
371 | > | |
372 | > | dAtom->setJ(ji); |
373 | } | |
355 | – | |
374 | } | |
375 | + | |
376 | + | if (nConstrained){ |
377 | + | constrainA(); |
378 | + | } |
379 | } | |
380 | ||
381 | ||
382 | < | void Integrator::moveB( void ){ |
383 | < | int i,j,k; |
362 | < | int atomIndex; |
382 | > | template<typename T> void Integrator<T>::moveB(void){ |
383 | > | int i, j; |
384 | DirectionalAtom* dAtom; | |
385 | < | double Tb[3]; |
386 | < | double ji[3]; |
385 | > | double Tb[3], ji[3]; |
386 | > | double vel[3], frc[3]; |
387 | > | double mass; |
388 | ||
389 | < | for( i=0; i<nAtoms; i++ ){ |
390 | < | atomIndex = i * 3; |
389 | > | for (i = 0; i < nAtoms; i++){ |
390 | > | atoms[i]->getVel(vel); |
391 | > | atoms[i]->getFrc(frc); |
392 | ||
393 | + | mass = atoms[i]->getMass(); |
394 | + | |
395 | // velocity half step | |
396 | < | for( j=atomIndex; j<(atomIndex+3); j++ ) |
397 | < | vel[j] += ( dt2 * frc[j] / atoms[i]->getMass() ) * eConvert; |
396 | > | for (j = 0; j < 3; j++) |
397 | > | vel[j] += (dt2 * frc[j] / mass) * eConvert; |
398 | ||
399 | < | if( atoms[i]->isDirectional() ){ |
400 | < | |
401 | < | dAtom = (DirectionalAtom *)atoms[i]; |
402 | < | |
403 | < | // get and convert the torque to body frame |
404 | < | |
405 | < | Tb[0] = dAtom->getTx(); |
406 | < | Tb[1] = dAtom->getTy(); |
407 | < | Tb[2] = dAtom->getTz(); |
408 | < | |
409 | < | dAtom->lab2Body( Tb ); |
410 | < | |
411 | < | // get the angular momentum, and complete the angular momentum |
412 | < | // half step |
413 | < | |
414 | < | ji[0] = dAtom->getJx() + ( dt2 * Tb[0] ) * eConvert; |
415 | < | ji[1] = dAtom->getJy() + ( dt2 * Tb[1] ) * eConvert; |
416 | < | ji[2] = dAtom->getJz() + ( dt2 * Tb[2] ) * eConvert; |
417 | < | |
393 | < | dAtom->setJx( ji[0] ); |
394 | < | dAtom->setJy( ji[1] ); |
395 | < | dAtom->setJz( ji[2] ); |
399 | > | atoms[i]->setVel(vel); |
400 | > | |
401 | > | if (atoms[i]->isDirectional()){ |
402 | > | dAtom = (DirectionalAtom *) atoms[i]; |
403 | > | |
404 | > | // get and convert the torque to body frame |
405 | > | |
406 | > | dAtom->getTrq(Tb); |
407 | > | dAtom->lab2Body(Tb); |
408 | > | |
409 | > | // get the angular momentum, and propagate a half step |
410 | > | |
411 | > | dAtom->getJ(ji); |
412 | > | |
413 | > | for (j = 0; j < 3; j++) |
414 | > | ji[j] += (dt2 * Tb[j]) * eConvert; |
415 | > | |
416 | > | |
417 | > | dAtom->setJ(ji); |
418 | } | |
419 | } | |
420 | ||
421 | + | if (nConstrained){ |
422 | + | constrainB(); |
423 | + | } |
424 | } | |
425 | ||
426 | < | void Integrator::preMove( void ){ |
427 | < | int i; |
426 | > | template<typename T> void Integrator<T>::preMove(void){ |
427 | > | int i, j; |
428 | > | double pos[3]; |
429 | ||
430 | < | if( nConstrained ){ |
430 | > | if (nConstrained){ |
431 | > | for (i = 0; i < nAtoms; i++){ |
432 | > | atoms[i]->getPos(pos); |
433 | ||
434 | < | for(i=0; i<(nAtoms*3); i++) oldPos[i] = pos[i]; |
434 | > | for (j = 0; j < 3; j++){ |
435 | > | oldPos[3 * i + j] = pos[j]; |
436 | > | } |
437 | > | } |
438 | } | |
439 | < | } |
439 | > | } |
440 | ||
441 | < | void Integrator::constrainA(){ |
442 | < | |
412 | < | int i,j,k; |
441 | > | template<typename T> void Integrator<T>::constrainA(){ |
442 | > | int i, j; |
443 | int done; | |
444 | + | double posA[3], posB[3]; |
445 | + | double velA[3], velB[3]; |
446 | double pab[3]; | |
447 | double rab[3]; | |
448 | int a, b, ax, ay, az, bx, by, bz; | |
# | Line 422 | Line 454 | void Integrator::constrainA(){ | |
454 | double gab; | |
455 | int iteration; | |
456 | ||
457 | < | |
426 | < | |
427 | < | for( i=0; i<nAtoms; i++){ |
428 | < | |
457 | > | for (i = 0; i < nAtoms; i++){ |
458 | moving[i] = 0; | |
459 | < | moved[i] = 1; |
459 | > | moved[i] = 1; |
460 | } | |
461 | ||
462 | iteration = 0; | |
463 | done = 0; | |
464 | < | while( !done && (iteration < maxIteration )){ |
436 | < | |
464 | > | while (!done && (iteration < maxIteration)){ |
465 | done = 1; | |
466 | < | for(i=0; i<nConstrained; i++){ |
439 | < | |
466 | > | for (i = 0; i < nConstrained; i++){ |
467 | a = constrainedA[i]; | |
468 | b = constrainedB[i]; | |
442 | – | |
443 | – | ax = (a*3) + 0; |
444 | – | ay = (a*3) + 1; |
445 | – | az = (a*3) + 2; |
469 | ||
470 | < | bx = (b*3) + 0; |
471 | < | by = (b*3) + 1; |
472 | < | bz = (b*3) + 2; |
470 | > | ax = (a * 3) + 0; |
471 | > | ay = (a * 3) + 1; |
472 | > | az = (a * 3) + 2; |
473 | ||
474 | < | if( moved[a] || moved[b] ){ |
475 | < | |
476 | < | pab[0] = pos[ax] - pos[bx]; |
454 | < | pab[1] = pos[ay] - pos[by]; |
455 | < | pab[2] = pos[az] - pos[bz]; |
474 | > | bx = (b * 3) + 0; |
475 | > | by = (b * 3) + 1; |
476 | > | bz = (b * 3) + 2; |
477 | ||
478 | < | //periodic boundary condition |
478 | > | if (moved[a] || moved[b]){ |
479 | > | atoms[a]->getPos(posA); |
480 | > | atoms[b]->getPos(posB); |
481 | ||
482 | < | info->wrapVector( pab ); |
482 | > | for (j = 0; j < 3; j++) |
483 | > | pab[j] = posA[j] - posB[j]; |
484 | ||
485 | < | pabsq = pab[0] * pab[0] + pab[1] * pab[1] + pab[2] * pab[2]; |
485 | > | //periodic boundary condition |
486 | ||
487 | < | rabsq = constrainedDsqr[i]; |
464 | < | diffsq = rabsq - pabsq; |
487 | > | info->wrapVector(pab); |
488 | ||
489 | < | // the original rattle code from alan tidesley |
467 | < | if (fabs(diffsq) > (tol*rabsq*2)) { |
468 | < | rab[0] = oldPos[ax] - oldPos[bx]; |
469 | < | rab[1] = oldPos[ay] - oldPos[by]; |
470 | < | rab[2] = oldPos[az] - oldPos[bz]; |
489 | > | pabsq = pab[0] * pab[0] + pab[1] * pab[1] + pab[2] * pab[2]; |
490 | ||
491 | < | info->wrapVector( rab ); |
491 | > | rabsq = constrainedDsqr[i]; |
492 | > | diffsq = rabsq - pabsq; |
493 | ||
494 | < | rpab = rab[0] * pab[0] + rab[1] * pab[1] + rab[2] * pab[2]; |
494 | > | // the original rattle code from alan tidesley |
495 | > | if (fabs(diffsq) > (tol * rabsq * 2)){ |
496 | > | rab[0] = oldPos[ax] - oldPos[bx]; |
497 | > | rab[1] = oldPos[ay] - oldPos[by]; |
498 | > | rab[2] = oldPos[az] - oldPos[bz]; |
499 | ||
500 | < | rpabsq = rpab * rpab; |
500 | > | info->wrapVector(rab); |
501 | ||
502 | + | rpab = rab[0] * pab[0] + rab[1] * pab[1] + rab[2] * pab[2]; |
503 | ||
504 | < | if (rpabsq < (rabsq * -diffsq)){ |
504 | > | rpabsq = rpab * rpab; |
505 | ||
506 | + | |
507 | + | if (rpabsq < (rabsq * -diffsq)){ |
508 | #ifdef IS_MPI | |
509 | < | a = atoms[a]->getGlobalIndex(); |
510 | < | b = atoms[b]->getGlobalIndex(); |
509 | > | a = atoms[a]->getGlobalIndex(); |
510 | > | b = atoms[b]->getGlobalIndex(); |
511 | #endif //is_mpi | |
512 | < | sprintf( painCave.errMsg, |
513 | < | "Constraint failure in constrainA at atom %d and %d.\n", |
514 | < | a, b ); |
515 | < | painCave.isFatal = 1; |
516 | < | simError(); |
517 | < | } |
512 | > | sprintf(painCave.errMsg, |
513 | > | "Constraint failure in constrainA at atom %d and %d.\n", a, |
514 | > | b); |
515 | > | painCave.isFatal = 1; |
516 | > | simError(); |
517 | > | } |
518 | ||
519 | < | rma = 1.0 / atoms[a]->getMass(); |
520 | < | rmb = 1.0 / atoms[b]->getMass(); |
519 | > | rma = 1.0 / atoms[a]->getMass(); |
520 | > | rmb = 1.0 / atoms[b]->getMass(); |
521 | ||
522 | < | gab = diffsq / ( 2.0 * ( rma + rmb ) * rpab ); |
522 | > | gab = diffsq / (2.0 * (rma + rmb) * rpab); |
523 | ||
524 | dx = rab[0] * gab; | |
525 | dy = rab[1] * gab; | |
526 | dz = rab[2] * gab; | |
527 | ||
528 | < | pos[ax] += rma * dx; |
529 | < | pos[ay] += rma * dy; |
530 | < | pos[az] += rma * dz; |
528 | > | posA[0] += rma * dx; |
529 | > | posA[1] += rma * dy; |
530 | > | posA[2] += rma * dz; |
531 | ||
532 | < | pos[bx] -= rmb * dx; |
506 | < | pos[by] -= rmb * dy; |
507 | < | pos[bz] -= rmb * dz; |
532 | > | atoms[a]->setPos(posA); |
533 | ||
534 | + | posB[0] -= rmb * dx; |
535 | + | posB[1] -= rmb * dy; |
536 | + | posB[2] -= rmb * dz; |
537 | + | |
538 | + | atoms[b]->setPos(posB); |
539 | + | |
540 | dx = dx / dt; | |
541 | dy = dy / dt; | |
542 | dz = dz / dt; | |
543 | ||
544 | < | vel[ax] += rma * dx; |
514 | < | vel[ay] += rma * dy; |
515 | < | vel[az] += rma * dz; |
544 | > | atoms[a]->getVel(velA); |
545 | ||
546 | < | vel[bx] -= rmb * dx; |
547 | < | vel[by] -= rmb * dy; |
548 | < | vel[bz] -= rmb * dz; |
546 | > | velA[0] += rma * dx; |
547 | > | velA[1] += rma * dy; |
548 | > | velA[2] += rma * dz; |
549 | ||
550 | < | moving[a] = 1; |
551 | < | moving[b] = 1; |
552 | < | done = 0; |
553 | < | } |
550 | > | atoms[a]->setVel(velA); |
551 | > | |
552 | > | atoms[b]->getVel(velB); |
553 | > | |
554 | > | velB[0] -= rmb * dx; |
555 | > | velB[1] -= rmb * dy; |
556 | > | velB[2] -= rmb * dz; |
557 | > | |
558 | > | atoms[b]->setVel(velB); |
559 | > | |
560 | > | moving[a] = 1; |
561 | > | moving[b] = 1; |
562 | > | done = 0; |
563 | > | } |
564 | } | |
565 | } | |
566 | < | |
567 | < | for(i=0; i<nAtoms; i++){ |
529 | < | |
566 | > | |
567 | > | for (i = 0; i < nAtoms; i++){ |
568 | moved[i] = moving[i]; | |
569 | moving[i] = 0; | |
570 | } | |
# | Line 534 | Line 572 | void Integrator::constrainA(){ | |
572 | iteration++; | |
573 | } | |
574 | ||
575 | < | if( !done ){ |
576 | < | |
577 | < | sprintf( painCave.errMsg, |
578 | < | "Constraint failure in constrainA, too many iterations: %d\n", |
541 | < | iteration ); |
575 | > | if (!done){ |
576 | > | sprintf(painCave.errMsg, |
577 | > | "Constraint failure in constrainA, too many iterations: %d\n", |
578 | > | iteration); |
579 | painCave.isFatal = 1; | |
580 | simError(); | |
581 | } | |
582 | ||
583 | } | |
584 | ||
585 | < | void Integrator::constrainB( void ){ |
586 | < | |
550 | < | int i,j,k; |
585 | > | template<typename T> void Integrator<T>::constrainB(void){ |
586 | > | int i, j; |
587 | int done; | |
588 | + | double posA[3], posB[3]; |
589 | + | double velA[3], velB[3]; |
590 | double vxab, vyab, vzab; | |
591 | double rab[3]; | |
592 | int a, b, ax, ay, az, bx, by, bz; | |
593 | double rma, rmb; | |
594 | double dx, dy, dz; | |
595 | < | double rabsq, pabsq, rvab; |
558 | < | double diffsq; |
595 | > | double rvab; |
596 | double gab; | |
597 | int iteration; | |
598 | ||
599 | < | for(i=0; i<nAtoms; i++){ |
599 | > | for (i = 0; i < nAtoms; i++){ |
600 | moving[i] = 0; | |
601 | moved[i] = 1; | |
602 | } | |
603 | ||
604 | done = 0; | |
605 | iteration = 0; | |
606 | < | while( !done && (iteration < maxIteration ) ){ |
570 | < | |
606 | > | while (!done && (iteration < maxIteration)){ |
607 | done = 1; | |
608 | ||
609 | < | for(i=0; i<nConstrained; i++){ |
574 | < | |
609 | > | for (i = 0; i < nConstrained; i++){ |
610 | a = constrainedA[i]; | |
611 | b = constrainedB[i]; | |
612 | ||
613 | < | ax = (a*3) + 0; |
614 | < | ay = (a*3) + 1; |
615 | < | az = (a*3) + 2; |
613 | > | ax = (a * 3) + 0; |
614 | > | ay = (a * 3) + 1; |
615 | > | az = (a * 3) + 2; |
616 | ||
617 | < | bx = (b*3) + 0; |
618 | < | by = (b*3) + 1; |
619 | < | bz = (b*3) + 2; |
617 | > | bx = (b * 3) + 0; |
618 | > | by = (b * 3) + 1; |
619 | > | bz = (b * 3) + 2; |
620 | ||
621 | < | if( moved[a] || moved[b] ){ |
622 | < | |
623 | < | vxab = vel[ax] - vel[bx]; |
589 | < | vyab = vel[ay] - vel[by]; |
590 | < | vzab = vel[az] - vel[bz]; |
621 | > | if (moved[a] || moved[b]){ |
622 | > | atoms[a]->getVel(velA); |
623 | > | atoms[b]->getVel(velB); |
624 | ||
625 | < | rab[0] = pos[ax] - pos[bx]; |
626 | < | rab[1] = pos[ay] - pos[by]; |
627 | < | rab[2] = pos[az] - pos[bz]; |
595 | < | |
596 | < | info->wrapVector( rab ); |
597 | < | |
598 | < | rma = 1.0 / atoms[a]->getMass(); |
599 | < | rmb = 1.0 / atoms[b]->getMass(); |
625 | > | vxab = velA[0] - velB[0]; |
626 | > | vyab = velA[1] - velB[1]; |
627 | > | vzab = velA[2] - velB[2]; |
628 | ||
629 | < | rvab = rab[0] * vxab + rab[1] * vyab + rab[2] * vzab; |
630 | < | |
603 | < | gab = -rvab / ( ( rma + rmb ) * constrainedDsqr[i] ); |
629 | > | atoms[a]->getPos(posA); |
630 | > | atoms[b]->getPos(posB); |
631 | ||
632 | < | if (fabs(gab) > tol) { |
633 | < | |
607 | < | dx = rab[0] * gab; |
608 | < | dy = rab[1] * gab; |
609 | < | dz = rab[2] * gab; |
610 | < | |
611 | < | vel[ax] += rma * dx; |
612 | < | vel[ay] += rma * dy; |
613 | < | vel[az] += rma * dz; |
632 | > | for (j = 0; j < 3; j++) |
633 | > | rab[j] = posA[j] - posB[j]; |
634 | ||
635 | < | vel[bx] -= rmb * dx; |
636 | < | vel[by] -= rmb * dy; |
637 | < | vel[bz] -= rmb * dz; |
638 | < | |
639 | < | moving[a] = 1; |
640 | < | moving[b] = 1; |
641 | < | done = 0; |
642 | < | } |
635 | > | info->wrapVector(rab); |
636 | > | |
637 | > | rma = 1.0 / atoms[a]->getMass(); |
638 | > | rmb = 1.0 / atoms[b]->getMass(); |
639 | > | |
640 | > | rvab = rab[0] * vxab + rab[1] * vyab + rab[2] * vzab; |
641 | > | |
642 | > | gab = -rvab / ((rma + rmb) * constrainedDsqr[i]); |
643 | > | |
644 | > | if (fabs(gab) > tol){ |
645 | > | dx = rab[0] * gab; |
646 | > | dy = rab[1] * gab; |
647 | > | dz = rab[2] * gab; |
648 | > | |
649 | > | velA[0] += rma * dx; |
650 | > | velA[1] += rma * dy; |
651 | > | velA[2] += rma * dz; |
652 | > | |
653 | > | atoms[a]->setVel(velA); |
654 | > | |
655 | > | velB[0] -= rmb * dx; |
656 | > | velB[1] -= rmb * dy; |
657 | > | velB[2] -= rmb * dz; |
658 | > | |
659 | > | atoms[b]->setVel(velB); |
660 | > | |
661 | > | moving[a] = 1; |
662 | > | moving[b] = 1; |
663 | > | done = 0; |
664 | > | } |
665 | } | |
666 | } | |
667 | ||
668 | < | for(i=0; i<nAtoms; i++){ |
668 | > | for (i = 0; i < nAtoms; i++){ |
669 | moved[i] = moving[i]; | |
670 | moving[i] = 0; | |
671 | } | |
672 | < | |
672 | > | |
673 | iteration++; | |
674 | } | |
675 | ||
676 | < | if( !done ){ |
677 | < | |
678 | < | |
679 | < | sprintf( painCave.errMsg, |
638 | < | "Constraint failure in constrainB, too many iterations: %d\n", |
639 | < | iteration ); |
676 | > | if (!done){ |
677 | > | sprintf(painCave.errMsg, |
678 | > | "Constraint failure in constrainB, too many iterations: %d\n", |
679 | > | iteration); |
680 | painCave.isFatal = 1; | |
681 | simError(); | |
682 | < | } |
643 | < | |
682 | > | } |
683 | } | |
684 | ||
685 | + | template<typename T> void Integrator<T>::rotationPropagation |
686 | + | ( DirectionalAtom* dAtom, double ji[3] ){ |
687 | ||
688 | + | double angle; |
689 | + | double A[3][3], I[3][3]; |
690 | ||
691 | + | // use the angular velocities to propagate the rotation matrix a |
692 | + | // full time step |
693 | ||
694 | + | dAtom->getA(A); |
695 | + | dAtom->getI(I); |
696 | ||
697 | + | // rotate about the x-axis |
698 | + | angle = dt2 * ji[0] / I[0][0]; |
699 | + | this->rotate( 1, 2, angle, ji, A ); |
700 | ||
701 | + | // rotate about the y-axis |
702 | + | angle = dt2 * ji[1] / I[1][1]; |
703 | + | this->rotate( 2, 0, angle, ji, A ); |
704 | ||
705 | < | void Integrator::rotate( int axes1, int axes2, double angle, double ji[3], |
706 | < | double A[9] ){ |
705 | > | // rotate about the z-axis |
706 | > | angle = dt * ji[2] / I[2][2]; |
707 | > | this->rotate( 0, 1, angle, ji, A); |
708 | ||
709 | < | int i,j,k; |
709 | > | // rotate about the y-axis |
710 | > | angle = dt2 * ji[1] / I[1][1]; |
711 | > | this->rotate( 2, 0, angle, ji, A ); |
712 | > | |
713 | > | // rotate about the x-axis |
714 | > | angle = dt2 * ji[0] / I[0][0]; |
715 | > | this->rotate( 1, 2, angle, ji, A ); |
716 | > | |
717 | > | dAtom->setA( A ); |
718 | > | } |
719 | > | |
720 | > | template<typename T> void Integrator<T>::rotate(int axes1, int axes2, |
721 | > | double angle, double ji[3], |
722 | > | double A[3][3]){ |
723 | > | int i, j, k; |
724 | double sinAngle; | |
725 | double cosAngle; | |
726 | double angleSqr; | |
# | Line 664 | Line 732 | void Integrator::rotate( int axes1, int axes2, double | |
732 | ||
733 | // initialize the tempA | |
734 | ||
735 | < | for(i=0; i<3; i++){ |
736 | < | for(j=0; j<3; j++){ |
737 | < | tempA[j][i] = A[3*i + j]; |
735 | > | for (i = 0; i < 3; i++){ |
736 | > | for (j = 0; j < 3; j++){ |
737 | > | tempA[j][i] = A[i][j]; |
738 | } | |
739 | } | |
740 | ||
741 | // initialize the tempJ | |
742 | ||
743 | < | for( i=0; i<3; i++) tempJ[i] = ji[i]; |
744 | < | |
743 | > | for (i = 0; i < 3; i++) |
744 | > | tempJ[i] = ji[i]; |
745 | > | |
746 | // initalize rot as a unit matrix | |
747 | ||
748 | rot[0][0] = 1.0; | |
# | Line 683 | Line 752 | void Integrator::rotate( int axes1, int axes2, double | |
752 | rot[1][0] = 0.0; | |
753 | rot[1][1] = 1.0; | |
754 | rot[1][2] = 0.0; | |
755 | < | |
755 | > | |
756 | rot[2][0] = 0.0; | |
757 | rot[2][1] = 0.0; | |
758 | rot[2][2] = 1.0; | |
759 | < | |
759 | > | |
760 | // use a small angle aproximation for sin and cosine | |
761 | ||
762 | < | angleSqr = angle * angle; |
762 | > | angleSqr = angle * angle; |
763 | angleSqrOver4 = angleSqr / 4.0; | |
764 | top = 1.0 - angleSqrOver4; | |
765 | bottom = 1.0 + angleSqrOver4; | |
# | Line 703 | Line 772 | void Integrator::rotate( int axes1, int axes2, double | |
772 | ||
773 | rot[axes1][axes2] = sinAngle; | |
774 | rot[axes2][axes1] = -sinAngle; | |
775 | < | |
775 | > | |
776 | // rotate the momentum acoording to: ji[] = rot[][] * ji[] | |
777 | < | |
778 | < | for(i=0; i<3; i++){ |
777 | > | |
778 | > | for (i = 0; i < 3; i++){ |
779 | ji[i] = 0.0; | |
780 | < | for(k=0; k<3; k++){ |
780 | > | for (k = 0; k < 3; k++){ |
781 | ji[i] += rot[i][k] * tempJ[k]; | |
782 | } | |
783 | } | |
784 | ||
785 | < | // rotate the Rotation matrix acording to: |
785 | > | // rotate the Rotation matrix acording to: |
786 | // A[][] = A[][] * transpose(rot[][]) | |
787 | ||
788 | ||
# | Line 721 | Line 790 | void Integrator::rotate( int axes1, int axes2, double | |
790 | // calculation as: | |
791 | // transpose(A[][]) = transpose(A[][]) * transpose(rot[][]) | |
792 | ||
793 | < | for(i=0; i<3; i++){ |
794 | < | for(j=0; j<3; j++){ |
795 | < | A[3*j + i] = 0.0; |
796 | < | for(k=0; k<3; k++){ |
797 | < | A[3*j + i] += tempA[i][k] * rot[j][k]; |
793 | > | for (i = 0; i < 3; i++){ |
794 | > | for (j = 0; j < 3; j++){ |
795 | > | A[j][i] = 0.0; |
796 | > | for (k = 0; k < 3; k++){ |
797 | > | A[j][i] += tempA[i][k] * rot[j][k]; |
798 | } | |
799 | } | |
800 | } | |
801 | } | |
802 | + | |
803 | + | template<typename T> void Integrator<T>::calcForce(int calcPot, int calcStress){ |
804 | + | myFF->doForces(calcPot, calcStress); |
805 | + | } |
806 | + | |
807 | + | template<typename T> void Integrator<T>::thermalize(){ |
808 | + | tStats->velocitize(); |
809 | + | } |
810 | + | |
811 | + | template<typename T> double Integrator<T>::getConservedQuantity(void){ |
812 | + | return tStats->getTotalE(); |
813 | + | } |
814 | + | template<typename T> string Integrator<T>::getAdditionalParameters(void){ |
815 | + | //By default, return a null string |
816 | + | //The reason we use string instead of char* is that if we use char*, we will |
817 | + | //return a pointer point to local variable which might cause problem |
818 | + | return string(); |
819 | + | } |
– | Removed lines |
+ | Added lines |
< | Changed lines |
> | Changed lines |