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