1 #ifdef TNG_BUILD_OPENMP_EXAMPLES
8 #include "../../include/tng_io.h"
11 void compute ( int np, int nd, float pos[], float vel[],
12 float mass, float f[], float *pot, float *kin );
13 float dist ( int nd, float r1[], float r2[], float dr[] );
14 void initialize ( int np, int nd, float box[], int *seed, float pos[],
15 float vel[], float acc[] );
16 float r8_uniform_01 ( int *seed );
17 void timestamp ( void );
18 void update ( int np, int nd, float pos[], float vel[], float f[],
19 float acc[], float mass, float dt );
21 /******************************************************************************/
25 /******************************************************************************/
29 MAIN is the main program for MD_OPENMP.
33 MD implements a simple molecular dynamics simulation.
35 The program uses Open MP directives to allow parallel computation.
37 The velocity Verlet time integration scheme is used.
39 The particles interact with a central pair potential.
41 Output of the program is saved in the TNG format, which is why this
42 code is included in the TNG API release. The high-level API of the
43 TNG API is used where appropriate.
47 This code is distributed under the GNU LGPL license.
55 Original FORTRAN77 version by Bill Magro.
56 C version by John Burkardt.
57 TNG trajectory output by Magnus Lundborg.
87 tng_trajectory_t traj;
88 tng_molecule_t molecule;
90 tng_residue_t residue;
95 proc_num = omp_get_num_procs ( );
97 acc = ( float * ) malloc ( nd * np * sizeof ( float ) );
98 box = ( float * ) malloc ( nd * sizeof ( float ) );
99 box_shape = (float *) malloc (9 * sizeof (float));
100 force = ( float * ) malloc ( nd * np * sizeof ( float ) );
101 pos = ( float * ) malloc ( nd * np * sizeof ( float ) );
102 vel = ( float * ) malloc ( nd * np * sizeof ( float ) );
105 printf ( "MD_OPENMP\n" );
106 printf ( " C/OpenMP version\n" );
108 printf ( " A molecular dynamics program.\n" );
111 printf ( " NP, the number of particles in the simulation is %d\n", np );
112 printf ( " STEP_NUM, the number of time steps, is %d\n", step_num );
113 printf ( " DT, the size of each time step, is %f\n", dt );
116 printf ( " Number of processors available = %d\n", proc_num );
117 printf ( " Number of threads = %d\n", omp_get_max_threads ( ) );
121 printf(" Initializing trajectory storage.\n");
122 /* Initialize the TNG trajectory */
123 tng_util_trajectory_open(TNG_EXAMPLE_FILES_DIR "tng_md_out.tng", 'w', &traj);
127 /* Set molecules data */
128 /* N.B. This is still not done using utility functions. The low-level API
130 printf(" Creating molecules in trajectory.\n");
131 tng_molecule_add(traj, "water", &molecule);
132 tng_molecule_chain_add(traj, molecule, "W", &chain);
133 tng_chain_residue_add(traj, chain, "WAT", &residue);
134 if(tng_residue_atom_add(traj, residue, "O", "O", &atom) == TNG_CRITICAL)
136 tng_util_trajectory_close(&traj);
137 printf(" Cannot create molecules.\n");
140 tng_molecule_cnt_set(traj, molecule, np);
144 Set the dimensions of the box.
146 for(i = 0; i < 9; i++)
150 for ( i = 0; i < nd; i++ )
153 /* box_shape stores 9 values according to the TNG specs */
154 box_shape[i*nd + i] = box[i];
159 printf ( " Initializing positions, velocities, and accelerations.\n" );
161 Set initial positions, velocities, and accelerations.
163 initialize ( np, nd, box, &seed, pos, vel, acc );
165 Compute the forces and energies.
168 printf ( " Computing initial forces and energies.\n" );
170 compute ( np, nd, pos, vel, mass, force, &potential, &kinetic );
172 e0 = potential + kinetic;
174 /* Saving frequency */
178 step_print_index = 0;
182 This is the main time stepping loop:
183 Compute forces and energies,
184 Update positions, velocities, accelerations.
186 printf(" Every %d steps box shape, particle positions, velocities and forces are\n",
188 printf(" saved to a TNG trajectory file.\n");
190 printf ( " At certain step intervals, we report the potential and kinetic energies.\n" );
191 printf ( " The sum of these energies should be a constant.\n" );
192 printf ( " As an accuracy check, we also print the relative error\n" );
193 printf ( " in the total energy.\n" );
195 printf ( " Step Potential Kinetic (P+K-E0)/E0\n" );
196 printf ( " Energy P Energy K Relative Energy Error\n" );
200 printf ( " %8d %14f %14f %14e\n",
201 step, potential, kinetic, ( potential + kinetic - e0 ) / e0 );
203 step_print = ( step_print_index * step_num ) / step_print_num;
205 /* Set the output frequency of box shape, positions, velocities and forces */
206 if(tng_util_box_shape_write_frequency_set(traj, step_save) != TNG_SUCCESS)
208 printf("Error setting writing frequency data. %s: %d\n",
212 if(tng_util_pos_write_frequency_set(traj, step_save) != TNG_SUCCESS)
214 printf("Error setting writing frequency data. %s: %d\n",
218 if(tng_util_vel_write_frequency_set(traj, step_save) != TNG_SUCCESS)
220 printf("Error setting writing frequency data. %s: %d\n",
224 if(tng_util_force_write_frequency_set(traj, step_save) != TNG_SUCCESS)
226 printf("Error setting writing frequency data. %s: %d\n",
231 /* Write the first frame of box shape, positions, velocities and forces */
232 if(tng_util_box_shape_write(traj, 0, box_shape) != TNG_SUCCESS)
234 printf("Error writing box shape. %s: %d\n",
238 if(tng_util_pos_write(traj, 0, pos) != TNG_SUCCESS)
240 printf("Error adding data. %s: %d\n", __FILE__, __LINE__);
243 if(tng_util_vel_write(traj, 0, vel) != TNG_SUCCESS)
245 printf("Error adding data. %s: %d\n", __FILE__, __LINE__);
248 if(tng_util_force_write(traj, 0, force) != TNG_SUCCESS)
250 printf("Error adding data. %s: %d\n", __FILE__, __LINE__);
254 wtime = omp_get_wtime ( );
256 for ( step = 1; step < step_num; step++ )
258 compute ( np, nd, pos, vel, mass, force, &potential, &kinetic );
260 if ( step == step_print )
262 printf ( " %8d %14f %14f %14e\n", step, potential, kinetic,
263 ( potential + kinetic - e0 ) / e0 );
265 step_print = ( step_print_index * step_num ) / step_print_num;
267 if(step % step_save == 0)
269 /* Write box shape, positions, velocities and forces */
270 if(tng_util_box_shape_write(traj, step, box_shape) != TNG_SUCCESS)
272 printf("Error writing box shape. %s: %d\n",
276 if(tng_util_pos_write(traj, step, pos) != TNG_SUCCESS)
278 printf("Error adding data. %s: %d\n", __FILE__, __LINE__);
281 if(tng_util_vel_write(traj, step, vel) != TNG_SUCCESS)
283 printf("Error adding data. %s: %d\n", __FILE__, __LINE__);
286 if(tng_util_force_write(traj, step, force) != TNG_SUCCESS)
288 printf("Error adding data. %s: %d\n", __FILE__, __LINE__);
292 update ( np, nd, pos, vel, force, acc, mass, dt );
294 wtime = omp_get_wtime ( ) - wtime;
297 printf ( " Elapsed time for main computation:\n" );
298 printf ( " %f seconds.\n", wtime );
307 /* Close the TNG output. */
308 tng_util_trajectory_close(&traj);
311 printf ( "MD_OPENMP\n" );
312 printf ( " Normal end of execution.\n" );
319 /******************************************************************************/
321 void compute ( int np, int nd, float pos[], float vel[],
322 float mass, float f[], float *pot, float *kin )
324 /******************************************************************************/
328 COMPUTE computes the forces and energies.
332 The computation of forces and energies is fully parallel.
334 The potential function V(X) is a harmonic well which smoothly
335 saturates to a maximum value at PI/2:
337 v(x) = ( sin ( min ( x, PI2 ) ) )**2
339 The derivative of the potential is:
341 dv(x) = 2.0 * sin ( min ( x, PI2 ) ) * cos ( min ( x, PI2 ) )
342 = sin ( 2.0 * min ( x, PI2 ) )
346 This code is distributed under the GNU LGPL license.
354 Original FORTRAN77 version by Bill Magro.
355 C version by John Burkardt.
359 Input, int NP, the number of particles.
361 Input, int ND, the number of spatial dimensions.
363 Input, float POS[ND*NP], the position of each particle.
365 Input, float VEL[ND*NP], the velocity of each particle.
367 Input, float MASS, the mass of each particle.
369 Output, float F[ND*NP], the forces.
371 Output, float *POT, the total potential energy.
373 Output, float *KIN, the total kinetic energy.
383 float PI2 = 3.141592653589793 / 2.0;
389 # pragma omp parallel \
390 shared ( f, nd, np, pos, vel ) \
391 private ( i, j, k, rij, d, d2 )
394 # pragma omp for reduction ( + : pe, ke )
395 for ( k = 0; k < np; k++ )
398 Compute the potential energy and forces.
400 for ( i = 0; i < nd; i++ )
405 for ( j = 0; j < np; j++ )
409 d = dist ( nd, pos+k*nd, pos+j*nd, rij );
411 Attribute half of the potential energy to particle J.
422 pe = pe + 0.5 * pow ( sin ( d2 ), 2 );
424 for ( i = 0; i < nd; i++ )
426 f[i+k*nd] = f[i+k*nd] - rij[i] * sin ( 2.0 * d2 ) / d;
431 Compute the kinetic energy.
433 for ( i = 0; i < nd; i++ )
435 ke = ke + vel[i+k*nd] * vel[i+k*nd];
439 ke = ke * 0.5 * mass;
446 /******************************************************************************/
448 float dist ( int nd, float r1[], float r2[], float dr[] )
450 /******************************************************************************/
454 DIST computes the displacement (and its norm) between two particles.
458 This code is distributed under the GNU LGPL license.
466 Original FORTRAN77 version by Bill Magro.
467 C version by John Burkardt.
471 Input, int ND, the number of spatial dimensions.
473 Input, float R1[ND], R2[ND], the positions of the particles.
475 Output, float DR[ND], the displacement vector.
477 Output, float D, the Euclidean norm of the displacement.
484 for ( i = 0; i < nd; i++ )
486 dr[i] = r1[i] - r2[i];
487 d = d + dr[i] * dr[i];
493 /******************************************************************************/
495 void initialize ( int np, int nd, float box[], int *seed, float pos[],
496 float vel[], float acc[] )
498 /******************************************************************************/
502 INITIALIZE initializes the positions, velocities, and accelerations.
506 This code is distributed under the GNU LGPL license.
514 Original FORTRAN77 version by Bill Magro.
515 C version by John Burkardt.
519 Input, int NP, the number of particles.
521 Input, int ND, the number of spatial dimensions.
523 Input, float BOX[ND], specifies the maximum position
524 of particles in each dimension.
526 Input, int *SEED, a seed for the random number generator.
528 Output, float POS[ND*NP], the position of each particle.
530 Output, float VEL[ND*NP], the velocity of each particle.
532 Output, float ACC[ND*NP], the acceleration of each particle.
538 Give the particles random positions within the box.
540 for ( i = 0; i < nd; i++ )
542 for ( j = 0; j < np; j++ )
544 pos[i+j*nd] = box[i] * r8_uniform_01 ( seed );
548 for ( j = 0; j < np; j++ )
550 for ( i = 0; i < nd; i++ )
555 for ( j = 0; j < np; j++ )
557 for ( i = 0; i < nd; i++ )
564 /******************************************************************************/
566 float r8_uniform_01 ( int *seed )
568 /******************************************************************************/
572 R8_UNIFORM_01 is a unit pseudorandom R8.
576 This routine implements the recursion
578 seed = 16807 * seed mod ( 2**31 - 1 )
579 unif = seed / ( 2**31 - 1 )
581 The integer arithmetic never requires more than 32 bits,
582 including a sign bit.
586 This code is distributed under the GNU LGPL license.
598 Paul Bratley, Bennett Fox, Linus Schrage,
599 A Guide to Simulation,
600 Springer Verlag, pages 201-202, 1983.
604 Implementation and Relative Efficiency of Quasirandom
606 ACM Transactions on Mathematical Software,
607 Volume 12, Number 4, pages 362-376, 1986.
611 Input/output, int *SEED, a seed for the random number generator.
613 Output, float R8_UNIFORM_01, a new pseudorandom variate, strictly between
622 *seed = 16807 * ( *seed - k * 127773 ) - k * 2836;
626 *seed = *seed + 2147483647;
629 r = ( float ) ( *seed ) * 4.656612875E-10;
633 /******************************************************************************/
635 void timestamp ( void )
637 /******************************************************************************/
641 TIMESTAMP prints the current YMDHMS date as a time stamp.
645 31 May 2001 09:45:54 AM
649 This code is distributed under the GNU LGPL license.
664 # define TIME_SIZE 40
666 static char time_buffer[TIME_SIZE];
671 tm = localtime ( &now );
673 strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
675 printf ( "%s\n", time_buffer );
680 /******************************************************************************/
682 void update ( int np, int nd, float pos[], float vel[], float f[],
683 float acc[], float mass, float dt )
685 /******************************************************************************/
689 UPDATE updates positions, velocities and accelerations.
693 The time integration is fully parallel.
695 A velocity Verlet algorithm is used for the updating.
697 x(t+dt) = x(t) + v(t) * dt + 0.5 * a(t) * dt * dt
698 v(t+dt) = v(t) + 0.5 * ( a(t) + a(t+dt) ) * dt
703 This code is distributed under the GNU LGPL license.
711 Original FORTRAN77 version by Bill Magro.
712 C version by John Burkardt.
716 Input, int NP, the number of particles.
718 Input, int ND, the number of spatial dimensions.
720 Input/output, float POS[ND*NP], the position of each particle.
722 Input/output, float VEL[ND*NP], the velocity of each particle.
724 Input, float F[ND*NP], the force on each particle.
726 Input/output, float ACC[ND*NP], the acceleration of each particle.
728 Input, float MASS, the mass of each particle.
730 Input, float DT, the time step.
739 # pragma omp parallel \
740 shared ( acc, dt, f, nd, np, pos, rmass, vel ) \
744 for ( j = 0; j < np; j++ )
746 for ( i = 0; i < nd; i++ )
748 pos[i+j*nd] = pos[i+j*nd] + vel[i+j*nd] * dt + 0.5 * acc[i+j*nd] * dt * dt;
749 vel[i+j*nd] = vel[i+j*nd] + 0.5 * dt * ( f[i+j*nd] * rmass + acc[i+j*nd] );
750 acc[i+j*nd] = f[i+j*nd] * rmass;