ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/md_code/Verlet.cpp
Revision: 248
Committed: Mon Jan 27 19:28:21 2003 UTC (21 years, 5 months ago) by chuckv
File size: 8577 byte(s)
Log Message:
final version before the single processor build

File Contents

# User Rev Content
1 mmeineke 10 #include <iostream>
2     #include <stdlib.h>
3    
4     #include "Atom.hpp"
5     #include "SRI.hpp"
6     #include "LRI.hpp"
7     #include "Integrator.hpp"
8     #include "SimInfo.hpp"
9     #include "Thermo.hpp"
10     #include "ReadWrite.hpp"
11    
12     extern "C"{
13    
14     void v_constrain_a_( double &dt, int &n_atoms, double* mass,
15     double* Rx, double* Ry, double* Rz,
16     double* Vx, double* Vy, double* Vz,
17     double* Fx, double* Fy, double* Fz,
18     int &n_constrained, double *constr_sqr,
19     int* constr_i, int* constr_j,
20     double &box_x, double &box_y, double &box_z );
21    
22     void v_constrain_b_( double &dt, int &n_atoms, double* mass,
23     double* Rx, double* Ry, double* Rz,
24     double* Vx, double* Vy, double* Vz,
25     double* Fx, double* Fy, double* Fz,
26     double &Kinetic,
27     int &n_constrained, double *constr_sqr,
28     int* constr_i, int* constr_j,
29     double &box_x, double &box_y, double &box_z );
30     }
31    
32    
33 chuckv 248 Verlet::Verlet( SimInfo &info, ForceField* the_ff ){
34 mmeineke 10
35     // get what information we need from the SimInfo object
36    
37     entry_plug = &info;
38 chuckv 248 myFF = the_ff;
39 mmeineke 10
40 chuckv 248
41 mmeineke 10 c_natoms = info.n_atoms;
42     c_atoms = info.atoms;
43     c_sr_interactions = info.sr_interactions;
44     longRange = info.longRange;
45     c_n_SRI = info.n_SRI;
46     c_is_constrained = 0;
47     c_box_x = info.box_x;
48     c_box_y = info.box_y;
49     c_box_z = info.box_z;
50    
51     // give a little love back to the SimInfo object
52    
53     if( info.the_integrator != NULL ) delete info.the_integrator;
54     info.the_integrator = this;
55    
56     // the rest are initialization issues
57    
58     is_first = 1; // let the integrate method know when the first call is
59    
60     // mass array setup
61    
62     c_mass = new double[c_natoms];
63    
64     for(int i = 0; i < c_natoms; i++){
65     c_mass[i] = c_atoms[i]->getMass();
66     }
67    
68     // check for constraints
69    
70     Constraint *temp_con;
71     Constraint *dummy_plug;
72     temp_con = new Constraint[c_n_SRI];
73    
74     c_n_constrained = 0;
75     int constrained = 0;
76    
77     for(int i = 0; i < c_n_SRI; i++){
78    
79     constrained = c_sr_interactions[i]->is_constrained();
80    
81     if(constrained){
82    
83     dummy_plug = c_sr_interactions[i]->get_constraint();
84     temp_con[c_n_constrained].set_a( dummy_plug->get_a() );
85     temp_con[c_n_constrained].set_b( dummy_plug->get_b() );
86     temp_con[c_n_constrained].set_dsqr( dummy_plug->get_dsqr() );
87    
88     c_n_constrained++;
89     constrained = 0;
90     }
91     }
92    
93     if(c_n_constrained > 0){
94    
95     c_is_constrained = 1;
96     c_constrained_i = new int[c_n_constrained];
97     c_constrained_j = new int[c_n_constrained];
98     c_constrained_dsqr = new double[c_n_constrained];
99    
100     for( int i = 0; i < c_n_constrained; i++){
101    
102     /* add 1 to the index for the fortran arrays. */
103    
104     c_constrained_i[i] = temp_con[i].get_a() + 1;
105     c_constrained_j[i] = temp_con[i].get_b() + 1;
106     c_constrained_dsqr[i] = temp_con[i].get_dsqr();
107     }
108     }
109    
110     delete[] temp_con;
111     }
112    
113    
114     Verlet::~Verlet(){
115    
116     if( c_is_constrained ){
117    
118     delete[] c_constrained_i;
119     delete[] c_constrained_j;
120     delete[] c_constrained_dsqr;
121     }
122    
123     delete[] c_mass;
124     c_mass = 0;
125     }
126    
127    
128     void Verlet::integrate( void ){
129    
130     int i, j; /* loop counters */
131    
132     double kE;
133    
134     double *Rx = new double[c_natoms];
135     double *Ry = new double[c_natoms];
136     double *Rz = new double[c_natoms];
137    
138     double *Vx = new double[c_natoms];
139     double *Vy = new double[c_natoms];
140     double *Vz = new double[c_natoms];
141    
142     double *Fx = new double[c_natoms];
143     double *Fy = new double[c_natoms];
144     double *Fz = new double[c_natoms];
145    
146 mmeineke 25 int time;
147    
148 mmeineke 10 double dt = entry_plug->dt;
149     double runTime = entry_plug->run_time;
150     double sampleTime = entry_plug->sampleTime;
151     double statusTime = entry_plug->statusTime;
152     double thermalTime = entry_plug->thermalTime;
153    
154     int n_loops = (int)( runTime / dt );
155     int sample_n = (int)( sampleTime / dt );
156     int status_n = (int)( statusTime / dt );
157     int vel_n = (int)( thermalTime / dt );
158    
159     Thermo *tStats = new Thermo( entry_plug );
160    
161     StatWriter* e_out = new StatWriter( entry_plug );
162     DumpWriter* dump_out = new DumpWriter( entry_plug );
163    
164     // the first time integrate is called, the forces need to be initialized
165    
166    
167 chuckv 248 myFF->doForces();
168 mmeineke 10
169     if( entry_plug->setTemp ){
170     tStats->velocitize();
171     }
172    
173 mmeineke 25 dump_out->writeDump( 0.0 );
174     e_out->writeStat( 0.0 );
175    
176 mmeineke 10 if( c_is_constrained ){
177     for(i = 0; i < n_loops; i++){
178    
179     // fill R, V, and F arrays and RATTLE in fortran
180    
181     for( j=0; j<c_natoms; j++ ){
182    
183     Rx[j] = c_atoms[j]->getX();
184     Ry[j] = c_atoms[j]->getY();
185     Rz[j] = c_atoms[j]->getZ();
186    
187     Vx[j] = c_atoms[j]->get_vx();
188     Vy[j] = c_atoms[j]->get_vy();
189     Vz[j] = c_atoms[j]->get_vz();
190    
191     Fx[j] = c_atoms[j]->getFx();
192     Fy[j] = c_atoms[j]->getFy();
193     Fz[j] = c_atoms[j]->getFz();
194    
195     }
196    
197     v_constrain_a_( dt, c_natoms, c_mass, Rx, Ry, Rz, Vx, Vy, Vz,
198     Fx, Fy, Fz,
199     c_n_constrained, c_constrained_dsqr,
200     c_constrained_i, c_constrained_j,
201     c_box_x, c_box_y, c_box_z );
202    
203     for( j=0; j<c_natoms; j++ ){
204    
205     c_atoms[j]->setX(Rx[j]);
206     c_atoms[j]->setY(Ry[j]);
207     c_atoms[j]->setZ(Rz[j]);
208    
209     c_atoms[j]->set_vx(Vx[j]);
210     c_atoms[j]->set_vy(Vy[j]);
211     c_atoms[j]->set_vz(Vz[j]);
212     }
213    
214     // calculate the forces
215    
216 chuckv 248 myFF->doForces();
217 mmeineke 10
218     // finish the constrain move ( same as above. )
219    
220     for( j=0; j<c_natoms; j++ ){
221    
222     Rx[j] = c_atoms[j]->getX();
223     Ry[j] = c_atoms[j]->getY();
224     Rz[j] = c_atoms[j]->getZ();
225    
226     Vx[j] = c_atoms[j]->get_vx();
227     Vy[j] = c_atoms[j]->get_vy();
228     Vz[j] = c_atoms[j]->get_vz();
229    
230     Fx[j] = c_atoms[j]->getFx();
231     Fy[j] = c_atoms[j]->getFy();
232     Fz[j] = c_atoms[j]->getFz();
233     }
234    
235     v_constrain_b_( dt, c_natoms, c_mass, Rx, Ry, Rz, Vx, Vy, Vz,
236     Fx, Fy, Fz,
237     kE, c_n_constrained, c_constrained_dsqr,
238     c_constrained_i, c_constrained_j,
239     c_box_x, c_box_y, c_box_z );
240    
241     for( j=0; j<c_natoms; j++ ){
242    
243     c_atoms[j]->setX(Rx[j]);
244     c_atoms[j]->setY(Ry[j]);
245     c_atoms[j]->setZ(Rz[j]);
246    
247     c_atoms[j]->set_vx(Vx[j]);
248     c_atoms[j]->set_vy(Vy[j]);
249     c_atoms[j]->set_vz(Vz[j]);
250     }
251    
252 mmeineke 25 time = i + 1;
253    
254 mmeineke 10 if( entry_plug->setTemp ){
255 mmeineke 25 if( !(time % vel_n) ) tStats->velocitize();
256 mmeineke 10 }
257 mmeineke 25 if( !(time % sample_n) ) dump_out->writeDump( time * dt );
258     if( !(time % status_n) ) e_out->writeStat( time * dt );
259 mmeineke 10 }
260     }
261     else{
262     for(i = 0; i < n_loops; i++){
263    
264     move_a( dt );
265    
266     // calculate the forces
267    
268 chuckv 248 myFF->doForces();
269 mmeineke 10
270     // complete the verlet move
271    
272     move_b( dt );
273    
274 mmeineke 25 time = i + 1;
275    
276 mmeineke 10 if( entry_plug->setTemp ){
277 mmeineke 25 if( !(time % vel_n) ) tStats->velocitize();
278 mmeineke 10 }
279 mmeineke 25 if( !(time % sample_n) ) dump_out->writeDump( time * dt );
280     if( !(time % status_n) ) e_out->writeStat( time * dt );
281 mmeineke 10 }
282     }
283    
284     dump_out->writeFinal();
285    
286     delete dump_out;
287     delete e_out;
288    
289     }
290    
291    
292     void Verlet::move_a(double dt){
293    
294     const double e_convert = 4.184e-4; // converts kcal/mol -> amu*A^2/fs^2
295    
296     double qx, qy, qz;
297     double vx, vy, vz;
298     int ma;
299     double h_dt = 0.5 * dt;
300     double h_dt2 = h_dt * dt;
301    
302     for( ma = 0; ma < c_natoms; ma++){
303    
304     qx = c_atoms[ma]->getX() + dt * c_atoms[ma]->get_vx() +
305     h_dt2 * c_atoms[ma]->getFx() * e_convert / c_atoms[ma]->getMass();
306     qy = c_atoms[ma]->getY() + dt * c_atoms[ma]->get_vy() +
307     h_dt2 * c_atoms[ma]->getFy() * e_convert / c_atoms[ma]->getMass();
308     qz = c_atoms[ma]->getZ() + dt * c_atoms[ma]->get_vz() +
309     h_dt2 * c_atoms[ma]->getFz() * e_convert / c_atoms[ma]->getMass();
310    
311     vx = c_atoms[ma]->get_vx() +
312     h_dt * c_atoms[ma]->getFx() * e_convert / c_atoms[ma]->getMass();
313     vy = c_atoms[ma]->get_vy() +
314     h_dt * c_atoms[ma]->getFy() * e_convert / c_atoms[ma]->getMass();
315     vz = c_atoms[ma]->get_vz() +
316     h_dt * c_atoms[ma]->getFz() * e_convert / c_atoms[ma]->getMass();
317    
318     c_atoms[ma]->setX(qx);
319     c_atoms[ma]->setY(qy);
320     c_atoms[ma]->setZ(qz);
321    
322     c_atoms[ma]->set_vx(vx);
323     c_atoms[ma]->set_vy(vy);
324     c_atoms[ma]->set_vz(vz);
325     }
326     }
327    
328     void Verlet::move_b( double dt ){
329    
330     const double e_convert = 4.184e-4; // converts kcal/mol -> amu*A^2/fs^2
331    
332     double vx, vy, vz;
333     int mb;
334     double h_dt = 0.5 * dt;
335    
336    
337     for( mb = 0; mb < c_natoms; mb++){
338    
339     vx = c_atoms[mb]->get_vx() +
340     h_dt * c_atoms[mb]->getFx() * e_convert / c_atoms[mb]->getMass();
341     vy = c_atoms[mb]->get_vy() +
342     h_dt * c_atoms[mb]->getFy() * e_convert / c_atoms[mb]->getMass();
343     vz = c_atoms[mb]->get_vz() +
344     h_dt * c_atoms[mb]->getFz() * e_convert / c_atoms[mb]->getMass();
345    
346     c_atoms[mb]->set_vx(vx);
347     c_atoms[mb]->set_vy(vy);
348     c_atoms[mb]->set_vz(vz);
349     }
350     }