From fec0d8a4023c44c5884707f8c415daa3f8c518b1 Mon Sep 17 00:00:00 2001
From: "Kurt A. O'Hearn" <ohearnk@msu.edu>
Date: Fri, 21 Dec 2018 15:30:41 -0500
Subject: [PATCH] sPuReMD: clean-up geometry file I/O code. Remove a few
 compiler warnings.

---
 sPuReMD/src/geo_tools.c | 217 +++++++++++++++++++---------------------
 sPuReMD/src/init_md.c   |   2 +-
 2 files changed, 104 insertions(+), 115 deletions(-)

diff --git a/sPuReMD/src/geo_tools.c b/sPuReMD/src/geo_tools.c
index 2e09f60b..df50baa7 100644
--- a/sPuReMD/src/geo_tools.c
+++ b/sPuReMD/src/geo_tools.c
@@ -31,17 +31,17 @@ static void Count_Geo_Atoms( FILE *geo, reax_system *system )
 {
     int i, serial;
     rvec x;
-    char element[3], name[9], line[MAX_LINE + 1];
+    char element[3], name[9], line[MAX_LINE];
 
     /* total number of atoms */
-    fscanf( geo, " %d", &(system->N) );
+    fscanf( geo, " %d", &system->N );
 
     /* count atoms */
     for ( i = 0; i < system->N; ++i )
     {
         fscanf( geo, CUSTOM_ATOM_FORMAT,
                 &serial, element, name, &x[0], &x[1], &x[2] );
-        Fit_to_Periodic_Box( &(system->box), &x );
+        Fit_to_Periodic_Box( &system->box, &x );
     }
 
     fseek( geo, 0, SEEK_SET );
@@ -58,7 +58,7 @@ static int Read_Box_Info( reax_system *system, FILE *geo, int geo_format )
 {
     int ret, cryst_len;
     char *cryst;
-    char line[MAX_LINE + 1];
+    char line[MAX_LINE];
     char descriptor[9];
     char s_a[12], s_b[12], s_c[12], s_alpha[12], s_beta[12], s_gamma[12];
     char s_group[12], s_zValue[12];
@@ -83,7 +83,7 @@ static int Read_Box_Info( reax_system *system, FILE *geo, int geo_format )
     }
 
     /* locate the cryst line in the geo file, read it and
-       initialize the big box */
+     * initialize the big box */
     while ( fgets( line, MAX_LINE, geo ) )
     {
         if ( strncmp( line, cryst, cryst_len ) == 0 )
@@ -91,10 +91,8 @@ static int Read_Box_Info( reax_system *system, FILE *geo, int geo_format )
             if ( geo_format == PDB )
             {
                 sscanf( line, PDB_CRYST1_FORMAT,
-                        &descriptor[0],
-                        &s_a[0], &s_b[0], &s_c[0],
-                        &s_alpha[0], &s_beta[0], &s_gamma[0],
-                        &s_group[0], &s_zValue[0] );
+                        descriptor, s_a, s_b, s_c,
+                        s_alpha, s_beta, s_gamma, s_group, s_zValue );
 
                 /* compute full volume tensor from the angles */
                 Setup_Box( atof(s_a),  atof(s_b), atof(s_c),
@@ -146,15 +144,16 @@ void Read_Geo( const char * const geo_file, reax_system* system, control_params
     {
         fscanf( geo, CUSTOM_ATOM_FORMAT,
                 &serial, element, name, &x[0], &x[1], &x[2] );
-        Fit_to_Periodic_Box( &(system->box), &x );
+        Fit_to_Periodic_Box( &system->box, &x );
+
 #if defined(DEBUG)
         fprintf( stderr, "atom%d: %s %s %f %f %f\n",
                  serial, element, name, x[0], x[1], x[2] );
 #endif
 
-        atom = &(system->atoms[top]);
+        atom = &system->atoms[top];
         workspace->orig_id[i] = serial;
-        atom->type = Get_Atom_Type( &(system->reaxprm), element );
+        atom->type = Get_Atom_Type( &system->reaxprm, element );
         strncpy( atom->name, name, 8 );
         rvec_Copy( atom->x, x );
         rvec_MakeZero( atom->v );
@@ -171,7 +170,7 @@ void Read_Geo( const char * const geo_file, reax_system* system, control_params
 static void Count_PDB_Atoms( FILE *geo, reax_system *system )
 {
     char *endptr = NULL;
-    char line[MAX_LINE + 1];
+    char line[MAX_LINE];
     char s_x[9], s_y[9], s_z[9];
     rvec x;
 
@@ -251,7 +250,7 @@ void Read_PDB( const char * const pdb_file, reax_system* system, control_params
     while ( fgets( s, MAX_LINE, pdb ) )
     {
         /* read new line and tokenize it */
-        strncpy( s1, s, MAX_LINE - 1 );
+        strncpy( s1, s, MAX_LINE );
         c1 = Tokenize( s, &tmp );
 
         /* process new line */
@@ -259,75 +258,70 @@ void Read_PDB( const char * const pdb_file, reax_system* system, control_params
         {
             if ( strncmp(tmp[0], "ATOM", 4) == 0 )
             {
-                strncpy( &descriptor[0], s1, 6 );
+                strncpy( descriptor, s1, 6 );
                 descriptor[6] = 0;
-                strncpy( &serial[0], s1 + 6, 5 );
+                strncpy( serial, s1 + 6, 5 );
                 serial[5] = 0;
-                strncpy( &atom_name[0], s1 + 12, 4 );
+                strncpy( atom_name, s1 + 12, 4 );
                 atom_name[4] = 0;
-                //strncpy( &serial[0], s1+6, 7 );       serial[7] = 0;
-                //strncpy( &atom_name[0], s1+13, 3 );   atom_name[3] = 0;
                 alt_loc = s1[16];
-                strncpy( &res_name[0], s1 + 17, 3 );
+                strncpy( res_name, s1 + 17, 3 );
                 res_name[3] = 0;
                 chain_id = s1[21];
-                strncpy( &res_seq[0], s1 + 22, 4 );
+                strncpy( res_seq, s1 + 22, 4 );
                 res_seq[4] = 0;
                 icode = s1[26];
-                strncpy( &s_x[0], s1 + 30, 8 );
+                strncpy( s_x, s1 + 30, 8 );
                 s_x[8] = 0;
-                strncpy( &s_y[0], s1 + 38, 8 );
+                strncpy( s_y, s1 + 38, 8 );
                 s_y[8] = 0;
-                strncpy( &s_z[0], s1 + 46, 8 );
+                strncpy( s_z, s1 + 46, 8 );
                 s_z[8] = 0;
-                strncpy( &occupancy[0], s1 + 54, 6 );
+                strncpy( occupancy, s1 + 54, 6 );
                 occupancy[6] = 0;
-                strncpy( &temp_factor[0], s1 + 60, 6 );
+                strncpy( temp_factor, s1 + 60, 6 );
                 temp_factor[6] = 0;
-                strncpy( &seg_id[0], s1 + 72, 4 );
+                strncpy( seg_id, s1 + 72, 4 );
                 seg_id[4] = 0;
-                strncpy( &element[0], s1 + 76, 2 );
+                strncpy( element, s1 + 76, 2 );
                 element[2] = 0;
-                strncpy( &charge[0], s1 + 78, 2 );
+                strncpy( charge, s1 + 78, 2 );
                 charge[2] = 0;
             }
             else if ( strncmp(tmp[0], "HETATM", 6) == 0 )
             {
-                strncpy( &descriptor[0], s1, 6 );
+                strncpy( descriptor, s1, 6 );
                 descriptor[6] = 0;
-                strncpy( &serial[0], s1 + 6, 5 );
+                strncpy( serial, s1 + 6, 5 );
                 serial[5] = 0;
-                strncpy( &atom_name[0], s1 + 12, 4 );
+                strncpy( atom_name, s1 + 12, 4 );
                 atom_name[4] = 0;
-                //strncpy( &serial[0], s1+6, 7 );       serial[7] = 0;
-                //strncpy( &atom_name[0], s1+13, 3 );   atom_name[3] = 0;
                 alt_loc = s1[16];
-                strncpy( &res_name[0], s1 + 17, 3 );
+                strncpy( res_name, s1 + 17, 3 );
                 res_name[3] = 0;
                 chain_id = s1[21];
-                strncpy( &res_seq[0], s1 + 22, 4 );
+                strncpy( res_seq, s1 + 22, 4 );
                 res_seq[4] = 0;
                 icode = s1[26];
-                strncpy( &s_x[0], s1 + 30, 8 );
+                strncpy( s_x, s1 + 30, 8 );
                 s_x[8] = 0;
-                strncpy( &s_y[0], s1 + 38, 8 );
+                strncpy( s_y, s1 + 38, 8 );
                 s_y[8] = 0;
-                strncpy( &s_z[0], s1 + 46, 8 );
+                strncpy( s_z, s1 + 46, 8 );
                 s_z[8] = 0;
-                strncpy( &occupancy[0], s1 + 54, 6 );
+                strncpy( occupancy, s1 + 54, 6 );
                 occupancy[6] = 0;
-                strncpy( &temp_factor[0], s1 + 60, 6 );
+                strncpy( temp_factor, s1 + 60, 6 );
                 temp_factor[6] = 0;
-                //strncpy( &seg_id[0], s1+72, 4 );      seg_id[4] = 0;
-                strncpy( &element[0], s1 + 76, 2 );
+                strncpy( element, s1 + 76, 2 );
                 element[2] = 0;
-                strncpy( &charge[0], s1 + 78, 2 );
+                strncpy( charge, s1 + 78, 2 );
                 charge[2] = 0;
             }
 
             /* if the point is inside my_box, add it to my lists */
-            Make_Point( strtod( &s_x[0], &endptr ), strtod( &s_y[0], &endptr ),
-                    strtod( &s_z[0], &endptr ), &x );
+            Make_Point( strtod( s_x, &endptr ), strtod( s_y, &endptr ),
+                    strtod( s_z, &endptr ), &x );
 
             Fit_to_Periodic_Box( &system->box, &x );
 
@@ -335,7 +329,7 @@ void Read_PDB( const char * const pdb_file, reax_system* system, control_params
             {
                 /* store orig_id, type, name and coord info of the new atom */
                 atom = &system->atoms[top];
-                pdb_serial = (int) strtod( &serial[0], &endptr );
+                pdb_serial = (int) strtod( serial, &endptr );
                 workspace->orig_id[top] = pdb_serial;
 
                 Trim_Spaces( element, 9 );
@@ -421,18 +415,18 @@ void Write_PDB( reax_system* system, reax_list* bonds, simulation_data *data,
     FILE *pdb;
 
     /* Writing Box information */
-    gamma = ACOS( (system->box.box[0][0] * system->box.box[1][0] +
-                system->box.box[0][1] * system->box.box[1][1] +
-                system->box.box[0][2] * system->box.box[1][2]) /
-                  (system->box.box_norms[0] * system->box.box_norms[1]) );
-    beta  = ACOS( (system->box.box[0][0] * system->box.box[2][0] +
-                system->box.box[0][1] * system->box.box[2][1] +
-                system->box.box[0][2] * system->box.box[2][2]) /
-            (system->box.box_norms[0] * system->box.box_norms[2]) );
-    alpha = ACOS( (system->box.box[2][0] * system->box.box[1][0] +
-                system->box.box[2][1] * system->box.box[1][1] +
-                system->box.box[2][2] * system->box.box[1][2]) /
-            (system->box.box_norms[2] * system->box.box_norms[1]) );
+    gamma = ACOS( (system->box.box[0][0] * system->box.box[1][0]
+                + system->box.box[0][1] * system->box.box[1][1]
+                + system->box.box[0][2] * system->box.box[1][2])
+            / (system->box.box_norms[0] * system->box.box_norms[1]) );
+    beta  = ACOS( (system->box.box[0][0] * system->box.box[2][0]
+                + system->box.box[0][1] * system->box.box[2][1]
+                + system->box.box[0][2] * system->box.box[2][2])
+            / (system->box.box_norms[0] * system->box.box_norms[2]) );
+    alpha = ACOS( (system->box.box[2][0] * system->box.box[1][0]
+                + system->box.box[2][1] * system->box.box[1][1]
+                + system->box.box[2][2] * system->box.box[1][2])
+            / (system->box.box_norms[2] * system->box.box_norms[1]) );
 
     /* open pdb and write header */
     snprintf( fname, MAX_STR + 9, "%s-%d.pdb", control->sim_name, data->step );
@@ -446,18 +440,17 @@ void Write_PDB( reax_system* system, reax_list* bonds, simulation_data *data,
     /* write atom lines to buffer */
     for ( i = 0; i < system->N; i++)
     {
-        p_atom = &(system->atoms[i]);
+        p_atom = &system->atoms[i];
 
         strncpy( name, p_atom->name, 8 );
         Trim_Spaces( name, 8 );
 
         memcpy( x, p_atom->x, 3 * sizeof(real) );
-        Fit_to_Periodic_Box( &(system->box), &x );
+        Fit_to_Periodic_Box( &system->box, &x );
 
         snprintf( buffer, PDB_ATOM_FORMAT_O_LENGTH, PDB_ATOM_FORMAT_O,
                 "ATOM  ", workspace->orig_id[i], p_atom->name, ' ', "REX", ' ', 1, ' ',
-                x[0], x[1], x[2],
-                1.0, 0.0, "0", name, "  " );
+                x[0], x[1], x[2], 1.0, 0.0, "0", name, "  " );
 
         buffer[PDB_ATOM_FORMAT_O_LENGTH] = '\n';
 
@@ -494,15 +487,12 @@ void Read_BGF( const char * const bgf_file, reax_system* system, control_params
     bgf = sfopen( bgf_file, "r" );
 
     /* allocate memory for tokenizing biograf file lines */
-    line = (char*) smalloc( sizeof(char)  * MAX_LINE,
-           "Read_BGF::line" );
-    backup = (char*) smalloc( sizeof(char)  * MAX_LINE,
-           "Read_BGF::backup" );
-    tokens = (char**) smalloc( sizeof(char*) * MAX_TOKENS,
-           "Read_BGF::tokens" );
+    line = smalloc( sizeof(char) * MAX_LINE, "Read_BGF::line" );
+    backup = smalloc( sizeof(char) * MAX_LINE, "Read_BGF::backup" );
+    tokens = smalloc( sizeof(char*) * MAX_TOKENS, "Read_BGF::tokens" );
     for ( i = 0; i < MAX_TOKENS; i++ )
     {
-        tokens[i] = (char*) smalloc( sizeof(char) * MAX_TOKEN_LEN,
+        tokens[i] = smalloc( sizeof(char) * MAX_TOKEN_LEN,
                "Read_BGF::tokens[i]" );
     }
 
@@ -536,25 +526,25 @@ void Read_BGF( const char * const bgf_file, reax_system* system, control_params
     sfclose( bgf, "Read_BGF::bgf" );
 
     /* memory allocations for atoms, atom maps, bond restrictions */
-    system->atoms = (reax_atom*) scalloc( system->N, sizeof(reax_atom),
+    system->atoms = scalloc( system->N, sizeof(reax_atom),
             "Read_BGF::system->atoms" );
 
-    workspace->map_serials = (int*) scalloc( MAX_ATOM_ID, sizeof(int),
+    workspace->map_serials = scalloc( MAX_ATOM_ID, sizeof(int),
             "Read_BGF::workspace->map_serials" );
     for ( i = 0; i < MAX_ATOM_ID; ++i )
     {
         workspace->map_serials[i] = -1;
     }
 
-    workspace->orig_id = (int*) scalloc( system->N, sizeof(int),
+    workspace->orig_id = scalloc( system->N, sizeof(int),
             "Read_BGF::workspace->orig_id" );
-    workspace->restricted  = (int*) scalloc( system->N, sizeof(int),
+    workspace->restricted  = scalloc( system->N, sizeof(int),
             "Read_BGF::workspace->restricted" );
-    workspace->restricted_list = (int**) scalloc( system->N, sizeof(int*),
+    workspace->restricted_list = scalloc( system->N, sizeof(int*),
             "Read_BGF::workspace->restricted_list" );
     for ( i = 0; i < system->N; ++i )
     {
-        workspace->restricted_list[i] = (int*) scalloc( MAX_RESTRICT, sizeof(int),
+        workspace->restricted_list[i] = scalloc( MAX_RESTRICT, sizeof(int),
                 "Read_BGF::workspace->restricted_list[i]" );
     }
 
@@ -566,7 +556,7 @@ void Read_BGF( const char * const bgf_file, reax_system* system, control_params
     while ( fgets( line, MAX_LINE, bgf ) )
     {
         /* read new line and tokenize it */
-        strncpy( backup, line, MAX_LINE - 1 );
+        strncpy( backup, line, MAX_LINE );
         token_cnt = Tokenize( line, &tokens );
 
         /* process new line */
@@ -574,80 +564,80 @@ void Read_BGF( const char * const bgf_file, reax_system* system, control_params
         {
             if ( !strncmp(tokens[0], "ATOM", 4) )
             {
-                strncpy( &descriptor[0], backup, 6 );
+                strncpy( descriptor, backup, 6 );
                 descriptor[6] = 0;
-                strncpy( &serial[0], backup + 7, 5 );
+                strncpy( serial, backup + 7, 5 );
                 serial[5] = 0;
-                strncpy( &atom_name[0], backup + 13, 5 );
+                strncpy( atom_name, backup + 13, 5 );
                 atom_name[5] = 0;
-                strncpy( &res_name[0], backup + 19, 3 );
+                strncpy( res_name, backup + 19, 3 );
                 res_name[3] = 0;
                 chain_id = backup[23];
-                strncpy( &res_seq[0], backup + 25, 5 );
+                strncpy( res_seq, backup + 25, 5 );
                 res_seq[5] = 0;
-                strncpy( &s_x[0], backup + 30, 10 );
+                strncpy( s_x, backup + 30, 10 );
                 s_x[10] = 0;
-                strncpy( &s_y[0], backup + 40, 10 );
+                strncpy( s_y, backup + 40, 10 );
                 s_y[10] = 0;
-                strncpy( &s_z[0], backup + 50, 10 );
+                strncpy( s_z, backup + 50, 10 );
                 s_z[10] = 0;
-                strncpy( &element[0], backup + 61, 5 );
+                strncpy( element, backup + 61, 5 );
                 element[5] = 0;
-                strncpy( &occupancy[0], backup + 66, 3 );
+                strncpy( occupancy, backup + 66, 3 );
                 occupancy[3] = 0;
-                strncpy( &temp_factor[0], backup + 69, 2 );
+                strncpy( temp_factor, backup + 69, 2 );
                 temp_factor[2] = 0;
-                strncpy( &charge[0], backup + 72, 8 );
+                strncpy( charge, backup + 72, 8 );
                 charge[8] = 0;
             }
             else if ( !strncmp(tokens[0], "HETATM", 6) )
             {
                 /* bgf hetatm:
                    (7x,i5,1x,a5,1x,a3,1x,a1,1x,a5,3f10.5,1x,a5,i3,i2,1x,f8.5) */
-                strncpy( &descriptor[0], backup, 6 );
+                strncpy( descriptor, backup, 6 );
                 descriptor[6] = 0;
-                strncpy( &serial[0], backup + 7, 5 );
+                strncpy( serial, backup + 7, 5 );
                 serial[5] = 0;
-                strncpy( &atom_name[0], backup + 13, 5 );
+                strncpy( atom_name, backup + 13, 5 );
                 atom_name[5] = 0;
-                strncpy( &res_name[0], backup + 19, 3 );
+                strncpy( res_name, backup + 19, 3 );
                 res_name[3] = 0;
                 chain_id = backup[23];
-                strncpy( &res_seq[0], backup + 25, 5 );
+                strncpy( res_seq, backup + 25, 5 );
                 res_seq[5] = 0;
-                strncpy( &s_x[0], backup + 30, 10 );
+                strncpy( s_x, backup + 30, 10 );
                 s_x[10] = 0;
-                strncpy( &s_y[0], backup + 40, 10 );
+                strncpy( s_y, backup + 40, 10 );
                 s_y[10] = 0;
-                strncpy( &s_z[0], backup + 50, 10 );
+                strncpy( s_z, backup + 50, 10 );
                 s_z[10] = 0;
-                strncpy( &element[0], backup + 61, 5 );
+                strncpy( element, backup + 61, 5 );
                 element[5] = 0;
-                strncpy( &occupancy[0], backup + 66, 3 );
+                strncpy( occupancy, backup + 66, 3 );
                 occupancy[3] = 0;
-                strncpy( &temp_factor[0], backup + 69, 2 );
+                strncpy( temp_factor, backup + 69, 2 );
                 temp_factor[2] = 0;
-                strncpy( &charge[0], backup + 72, 8 );
+                strncpy( charge, backup + 72, 8 );
                 charge[8] = 0;
             }
 
             /* add to mapping */
-            bgf_serial = strtod( &serial[0], &endptr );
+            bgf_serial = strtod( serial, &endptr );
             Check_Input_Range( bgf_serial, 0, MAX_ATOM_ID, "Invalid bgf serial" );
             workspace->map_serials[ bgf_serial ] = atom_cnt;
             workspace->orig_id[ atom_cnt ] = bgf_serial;
             // fprintf( stderr, "map %d --> %d\n", bgf_serial, atom_cnt );
 
             /* copy atomic positions */
-            system->atoms[atom_cnt].x[0] = strtod( &s_x[0], &endptr );
-            system->atoms[atom_cnt].x[1] = strtod( &s_y[0], &endptr );
-            system->atoms[atom_cnt].x[2] = strtod( &s_z[0], &endptr );
+            system->atoms[atom_cnt].x[0] = strtod( s_x, &endptr );
+            system->atoms[atom_cnt].x[1] = strtod( s_y, &endptr );
+            system->atoms[atom_cnt].x[2] = strtod( s_z, &endptr );
 
             /* atom name and type */
             strncpy( system->atoms[atom_cnt].name, atom_name, 8 );
             Trim_Spaces( element, 10 );
             system->atoms[atom_cnt].type =
-                Get_Atom_Type( &(system->reaxprm), element );
+                Get_Atom_Type( &system->reaxprm, element );
 
             /* fprintf( stderr,
             "a:%3d(%1d) c:%10.5f%10.5f%10.5f q:%10.5f occ:%s temp:%s seg_id:%s element:%s\n",
@@ -659,18 +649,17 @@ void Read_BGF( const char * const bgf_file, reax_system* system, control_params
 
             atom_cnt++;
         }
-        else if (!strncmp( tokens[0], "CRYSTX", 6 ))
+        else if ( !strncmp( tokens[0], "CRYSTX", 6 ) )
         {
-            sscanf( backup, BGF_CRYSTX_FORMAT, &descriptor[0],
-                    &s_a[0], &s_b[0], &s_c[0],
-                    &s_alpha[0], &s_beta[0], &s_gamma[0] );
+            sscanf( backup, BGF_CRYSTX_FORMAT, descriptor,
+                    s_a, s_b, s_c, s_alpha, s_beta, s_gamma );
 
             /* Compute full volume tensor from the angles */
             Setup_Box( atof(s_a), atof(s_b), atof(s_c),
                     atof(s_alpha), atof(s_beta), atof(s_gamma),
-                    &(system->box) );
+                    &system->box );
         }
-        else if (!strncmp( tokens[0], "CONECT", 6 ))
+        else if ( !strncmp( tokens[0], "CONECT", 6 ) )
         {
             /* check number of restrictions */
             Check_Input_Range( token_cnt - 2, 0, MAX_RESTRICT,
@@ -687,8 +676,8 @@ void Read_BGF( const char * const bgf_file, reax_system* system, control_params
             {
                 if ( is_Valid_Serial( workspace, bgf_serial = atoi(tokens[i]) ) )
                 {
-                    workspace->restricted_list[ ratom ][ i - 2 ] =
-                        workspace->map_serials[ bgf_serial ];
+                    workspace->restricted_list[ratom][i - 2] =
+                        workspace->map_serials[bgf_serial];
                 }
             }
 
diff --git a/sPuReMD/src/init_md.c b/sPuReMD/src/init_md.c
index e0899c70..2195900e 100644
--- a/sPuReMD/src/init_md.c
+++ b/sPuReMD/src/init_md.c
@@ -907,7 +907,7 @@ static void Init_Out_Controls( reax_system *system, control_params *control,
     if ( control->ensemble == aNPT || control->ensemble == iNPT
             || control->ensemble == sNPT )
     {
-        strncpy( temp, control->sim_name, MAX_STR );
+        strncpy( temp, control->sim_name, TEMP_SIZE );
         strcat( temp, ".prs" );
         out_control->prs = sfopen( temp, "w" );
         fprintf( out_control->prs, "%-6s%13s%13s%13s%13s%13s%13s%13s%13s\n",
-- 
GitLab