[Swift-commit] r8131 - SwiftTutorials/ATPESC_2014-08-14/swift-t/examples/05-md

wozniak at ci.uchicago.edu wozniak at ci.uchicago.edu
Wed Aug 13 11:30:05 CDT 2014


Author: wozniak
Date: 2014-08-13 11:30:05 -0500 (Wed, 13 Aug 2014)
New Revision: 8131

Modified:
   SwiftTutorials/ATPESC_2014-08-14/swift-t/examples/05-md/main.c
   SwiftTutorials/ATPESC_2014-08-14/swift-t/examples/05-md/md.c
   SwiftTutorials/ATPESC_2014-08-14/swift-t/examples/05-md/md.h
Log:
Move more into simulate()


Modified: SwiftTutorials/ATPESC_2014-08-14/swift-t/examples/05-md/main.c
===================================================================
--- SwiftTutorials/ATPESC_2014-08-14/swift-t/examples/05-md/main.c	2014-08-13 16:23:58 UTC (rev 8130)
+++ SwiftTutorials/ATPESC_2014-08-14/swift-t/examples/05-md/main.c	2014-08-13 16:30:05 UTC (rev 8131)
@@ -66,8 +66,7 @@
     None
 */
 {
-  double *acc;
-  double *box;
+
   double ctime;
   double ctime1;
   double ctime2;
@@ -80,7 +79,7 @@
   double mass = 1.0 * .0001;
   int nd;
   int np;
-  double *pos;
+
   double potential;
   int seed = 123456789;
   int step;
@@ -190,70 +189,12 @@
   printf ( "  SCALE_FACTOR, the particle position scaling factor, is %f\n", scale_factor );
   printf ( "  SCALE_OFFSET, the particle position scaling offset, is %f\n", scale_offset );
   printf ( "  SEED, the simulation randomization seed, is %d\n", seed );
-/*
-  Allocate memory.
-*/
-  acc = ( double * ) malloc ( nd * np * sizeof ( double ) );
-  box = ( double * ) malloc ( nd * sizeof ( double ) );
-  force = ( double * ) malloc ( nd * np * sizeof ( double ) );
-  pos = ( double * ) malloc ( nd * np * sizeof ( double ) );
-  vel = ( double * ) malloc ( nd * np * sizeof ( double ) );
-/*
-  Set the dimensions of the box.
-*/
-  for ( i = 0; i < nd; i++ )
-  {
-    box[i] = 10.0;
-  }
 
-  printf ( "\n" );
-  printf ( "  Initializing positions, velocities, and accelerations.\n" );
-/*
-  Set initial positions, velocities, and accelerations.
-*/
-  initialize ( np, nd, box, &seed, pos, vel, acc );
-/*
-  Compute the forces and energies.
-*/
-  printf ( "\n" );
-  printf ( "  Computing initial forces and energies.\n" );
-
-  compute ( np, nd, pos, vel, mass, force, &potential, &kinetic );
-
-  e0 = potential + kinetic;
-/*
-  This is the main time stepping loop:
-    Compute forces and energies,
-    Update positions, velocities, accelerations.
-*/
-  printf ( "\n" );
-  printf ( "  At each step, we report the potential and kinetic energies.\n" );
-  printf ( "  The sum of these energies should be a constant.\n" );
-  printf ( "  As an accuracy check, we also print the relative error\n" );
-  printf ( "  in the total energy.\n" );
-  printf ( "\n" );
-  printf ( "      Step      Potential       Kinetic        (P+K-E0)/E0\n" );
-  printf ( "                Energy P        Energy K       Relative Energy Error\n" );
-  printf ( "\n" );
-
-  FILE *ofile = fopen(outfile,"w");
-  fprintf (ofile, "      Step      Potential       Kinetic        RelativeErr\n" );
-
-  printf ( "  %8d  %14f  %14f  %14e\n",
-    step, potential, kinetic, ( potential + kinetic - e0 ) / e0 );
-  fprintf ( ofile, "  %8d  %14f  %14f  %14e\n",
-    step, potential, kinetic, ( potential + kinetic - e0 ) / e0 );
-  step_print_index = step_print_index + 1;
-  step_print = ( step_print_index * step_num ) / step_print_num;
-
   ctime1 = cpu_time ( );
 
   simulate (step_num, step_print_num, step_print, step_print_index,
-            np, nd, pos, vel,
-            mass, force, acc,
-            potential, kinetic, e0,
-            dt,
-            ofile);
+            np, nd, mass, potential, kinetic, e0,
+            dt, seed, outfile);
 
   ctime2 = cpu_time ( );
   ctime = ctime2 - ctime1;
@@ -262,11 +203,7 @@
   printf ( "  Elapsed cpu time for main computation:\n" );
   printf ( "  %f seconds.\n", ctime );
 
-  free ( acc );
-  free ( box );
-  free ( force );
-  free ( pos );
-  free ( vel );
+
   char tarcmd[2000];
   sprintf(tarcmd,"tar zcf %s md??.trj",trjfile);
   system(tarcmd);
@@ -280,6 +217,5 @@
   printf ( "\n" );
   timestamp ( );
 
-  fclose(ofile);
   return 0;
 }

Modified: SwiftTutorials/ATPESC_2014-08-14/swift-t/examples/05-md/md.c
===================================================================
--- SwiftTutorials/ATPESC_2014-08-14/swift-t/examples/05-md/md.c	2014-08-13 16:23:58 UTC (rev 8130)
+++ SwiftTutorials/ATPESC_2014-08-14/swift-t/examples/05-md/md.c	2014-08-13 16:30:05 UTC (rev 8131)
@@ -19,13 +19,72 @@
 
 void simulate (int step_num, int step_print_num,
                int step_print, int step_print_index,
-               int np, int nd, double pos[], double vel[],
-               double mass, double force[], double acc[],
+               int np, int nd,
+               double mass,
                double potential, double kinetic, double e0,
                double dt,
-               FILE* ofile)
+               int seed,
+               char* outfile)
 {
-  for (int step = 1; step <= step_num; step++ )
+  /*
+    Allocate memory.
+  */
+    double* acc = ( double * ) malloc ( nd * np * sizeof ( double ) );
+    double* box = ( double * ) malloc ( nd * sizeof ( double ) );
+    double* force = ( double * ) malloc ( nd * np * sizeof ( double ) );
+    double* pos = ( double * ) malloc ( nd * np * sizeof ( double ) );
+    double* vel = ( double * ) malloc ( nd * np * sizeof ( double ) );
+  /*
+    Set the dimensions of the box.
+  */
+    for (int i = 0; i < nd; i++ )
+    {
+      box[i] = 10.0;
+    }
+
+    printf ( "\n" );
+    printf ( "  Initializing positions, velocities, and accelerations.\n" );
+  /*
+    Set initial positions, velocities, and accelerations.
+  */
+    initialize ( np, nd, box, &seed, pos, vel, acc );
+  /*
+    Compute the forces and energies.
+  */
+    printf ( "\n" );
+    printf ( "  Computing initial forces and energies.\n" );
+
+    compute ( np, nd, pos, vel, mass, force, &potential, &kinetic );
+
+    e0 = potential + kinetic;
+  /*
+    This is the main time stepping loop:
+      Compute forces and energies,
+      Update positions, velocities, accelerations.
+  */
+    printf ( "\n" );
+    printf ( "  At each step, we report the potential and kinetic energies.\n" );
+    printf ( "  The sum of these energies should be a constant.\n" );
+    printf ( "  As an accuracy check, we also print the relative error\n" );
+    printf ( "  in the total energy.\n" );
+    printf ( "\n" );
+    printf ( "      Step      Potential       Kinetic        (P+K-E0)/E0\n" );
+    printf ( "                Energy P        Energy K       Relative Energy Error\n" );
+    printf ( "\n" );
+
+    FILE *ofile = fopen(outfile,"w");
+    fprintf (ofile, "      Step      Potential       Kinetic        RelativeErr\n" );
+
+    int step = 0;
+    printf ( "  %8d  %14f  %14f  %14e\n",
+      step, potential, kinetic, ( potential + kinetic - e0 ) / e0 );
+    fprintf ( ofile, "  %8d  %14f  %14f  %14e\n",
+      step, potential, kinetic, ( potential + kinetic - e0 ) / e0 );
+    step_print_index = step_print_index + 1;
+    step_print = ( step_print_index * step_num ) / step_print_num;
+
+
+  for (step = 1; step <= step_num; step++ )
   {
     compute ( np, nd, pos, vel, mass, force, &potential, &kinetic );
 
@@ -41,6 +100,14 @@
     }
     update ( np, nd, pos, vel, force, acc, mass, dt );
   }
+
+  free ( acc );
+  free ( box );
+  free ( force );
+  free ( pos );
+  free ( vel );
+
+  fclose(ofile);
 }
 
 /******************************************************************************/

Modified: SwiftTutorials/ATPESC_2014-08-14/swift-t/examples/05-md/md.h
===================================================================
--- SwiftTutorials/ATPESC_2014-08-14/swift-t/examples/05-md/md.h	2014-08-13 16:23:58 UTC (rev 8130)
+++ SwiftTutorials/ATPESC_2014-08-14/swift-t/examples/05-md/md.h	2014-08-13 16:30:05 UTC (rev 8131)
@@ -23,10 +23,11 @@
 
 void simulate (int step_num, int step_print_num,
                int step_print, int step_print_index,
-               int np, int nd, double pos[], double vel[],
-               double mass, double force[], double acc[],
+               int np, int nd,
+               double mass,
                double potential, double kinetic, double e0,
                double dt,
-               FILE* ofile);
+               int seed,
+               char* outfile);
 
 #endif




More information about the Swift-commit mailing list