sortreplicas.c File Reference

#include "largefiles.h"
#include "vmdplugin.h"
#include "molfile_plugin.h"
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

Go to the source code of this file.

Defines

#define LINE_MAX   10000

Functions

int molfile_dcdplugin_init (void)
int molfile_dcdplugin_register (void *, vmdplugin_register_cb)
int molfile_dcdplugin_fini (void)
static int register_cb (void *v, vmdplugin_t *p)
int main (int argc, char **argv)

Define Documentation

#define LINE_MAX   10000

Referenced by main().


Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 28 of file sortreplicas.c.

References LINE_MAX, molfile_dcdplugin_fini(), molfile_dcdplugin_init(), molfile_dcdplugin_register(), and register_cb().

00028                                 {
00029 
00030   molfile_timestep_t frame;
00031   molfile_plugin_t *plugin;
00032   char *output_root;
00033   char *filename;
00034   int num_replicas;
00035   int runs_per_frame;
00036   long long int final_step = -1;
00037   long long int checkstep = -1;
00038   int colvars;
00039   FILE **hist_in;
00040   FILE **hist_out;
00041   FILE **colv_in;
00042   FILE **colv_out;
00043   void **traj_in;
00044   void **traj_out;
00045   int natoms=MOLFILE_NUMATOMS_UNKNOWN;
00046   int i, i_run;
00047 
00048   molfile_dcdplugin_init();
00049   molfile_dcdplugin_register(&plugin, register_cb);
00050 
00051   if ( argc < 4 || argc > 5 ) {
00052     fprintf(stderr, "args: <job_output_root> <num_replicas> <runs_per_frame> [final_step]\n");
00053     exit(-1);
00054   }
00055   output_root = argv[1];
00056   num_replicas = atoi(argv[2]);
00057   runs_per_frame = atoi(argv[3]);
00058   if ( argc > 4 ) {
00059     sscanf(argv[4], "%lld", &final_step);
00060   }
00061 
00062   filename = (char*) malloc(strlen(output_root)+100);
00063   hist_in = (FILE**) malloc(num_replicas*sizeof(FILE*));
00064   hist_out = (FILE**) malloc(num_replicas*sizeof(FILE*));
00065   colv_in = (FILE**) malloc(num_replicas*sizeof(FILE*));
00066   colv_out = (FILE**) malloc(num_replicas*sizeof(FILE*));
00067   traj_in = (void**) malloc(num_replicas*sizeof(FILE*));
00068   traj_out = (void**) malloc(num_replicas*sizeof(FILE*));
00069 
00070   for ( i=0; i<num_replicas; ++i ) {
00071     char *root_end;
00072     if ( strstr(output_root,"%s") ) {
00073       char istr[10];
00074       sprintf(istr,"%d",i);
00075       sprintf(filename,output_root,istr);
00076     } else {
00077       sprintf(filename,output_root,i);
00078     }
00079     root_end = filename + strlen(filename);
00080 
00081     sprintf(root_end,".%d.history",i);
00082     hist_in[i] = fopen(filename,"r");
00083     if ( ! hist_in[i] ) {
00084       fprintf(stderr, "error opening input file %s: %s\n",
00085                                         filename, strerror(errno));
00086       exit(-1);
00087     }
00088     sprintf(root_end,".%d.dcd",i);
00089     traj_in[i] = plugin->open_file_read(filename,"dcd",&natoms);
00090     if ( ! traj_in[i] ) {
00091       fprintf(stderr, "error opening input file %s: %s\n",
00092                                         filename, strerror(errno));
00093       exit(-1);
00094     }
00095     sprintf(root_end,".%d.colvars.traj",i);
00096     colv_in[i] = fopen(filename,"r");
00097     if ( colv_in[i] ) {
00098       if ( i == 0 ) {
00099         printf("Found first input colvars trajectory file %s.\n", filename);
00100         colvars = 1;
00101       } else if ( ! colvars ) {
00102         fprintf(stderr, "missing input colvars trajectory files before %s\n", filename);
00103         exit(-1);
00104       }
00105     } else {
00106       if ( i == 0 ) {
00107         colvars = 0;
00108       } else if ( colvars ) {
00109         fprintf(stderr, "error opening input colvars trajectory file %s: %s\n",
00110                                         filename, strerror(errno));
00111         exit(-1);
00112       }
00113     }
00114   }
00115 
00116   for ( i=0; i<num_replicas; ++i ) {
00117     char *root_end;
00118     if ( strstr(output_root,"%s") ) {
00119       char istr[10];
00120       sprintf(istr,"%d",i);
00121       sprintf(filename,output_root,istr);
00122     } else {
00123       sprintf(filename,output_root,i);
00124     }
00125     root_end = filename + strlen(filename);
00126 
00127     sprintf(root_end,".%d.sort.history",i);
00128     hist_out[i] = fopen(filename,"w");
00129     if ( ! hist_out[i] ) {
00130       fprintf(stderr, "error opening output file %s: %s\n",
00131                                         filename, strerror(errno));
00132       exit(-1);
00133     }
00134     sprintf(root_end,".%d.sort.dcd",i);
00135     traj_out[i] = plugin->open_file_write(filename,"dcd",natoms);
00136     if ( ! traj_out[i] ) {
00137       fprintf(stderr, "error opening output file %s: %s\n",
00138                                         filename, strerror(errno));
00139       exit(-1);
00140     }
00141     if ( colvars ) {
00142       sprintf(root_end,".%d.sort.colvars.traj",i);
00143       colv_out[i] = fopen(filename,"w");
00144       if ( ! colv_out[i] ) {
00145         fprintf(stderr, "error opening output file %s: %s\n",
00146                                         filename, strerror(errno));
00147         exit(-1);
00148       }
00149     }
00150   }
00151 
00152   frame.coords = (float*) malloc(3*natoms*sizeof(float));
00153   frame.velocities = (float*) NULL;
00154 
00155 #define LINE_MAX 10000
00156 
00157   i_run = 0;
00158   for ( ; 1; ++i_run ) { /* loop until read fails */
00159     char line[LINE_MAX];
00160     for ( i=0; i<num_replicas; ++i ) {
00161       char *r;
00162       char sav;
00163       int f1,f2;
00164       int rc;
00165       int rep_id = -1;
00166       long long int step;
00167       r = fgets(line, LINE_MAX, hist_in[i]);
00168       if ( ! r ) { break; }
00169       rc = sscanf(line, "%lld %n%d%n", &step, &f1, &rep_id, &f2);
00170       if ( rc != 2 ) {
00171         fprintf(stderr,"Format error for replica %d at line %d: %s",
00172                                                         i, i_run, line);
00173         exit(-1);
00174       }
00175       if ( i == 0 ) {
00176         if ( step <= checkstep ) {
00177           fprintf(stderr,"Step out of order for replica %d at line %d: %s",
00178                                                         i, i_run, line);
00179           exit(-1);
00180         }
00181         checkstep = step;
00182         if ( final_step >= 0 && checkstep > final_step ) {
00183           printf("Stopping after final step %lld.\n", final_step);
00184           break;
00185         }
00186       } else if ( step != checkstep ) {
00187         fprintf(stderr,"Step mismatch for replica %d at line %d: %s",
00188                                                         i, i_run, line);
00189         exit(-1);
00190       }
00191       if ( rep_id < 0 || rep_id >= num_replicas ) {
00192         fprintf(stderr,"Invalid replica ID for replica %d at line %d: %s",
00193                                                         i, i_run, line);
00194         exit(-1);
00195       }
00196       sav = line[f1];
00197       line[f1] = 0;
00198       fprintf(hist_out[rep_id],"%s%d%s",line,i,line+f2);
00199       line[f1] = sav;
00200       if ( colvars ) {
00201        long long int oldcstep = -1;
00202        while ( 1 ) {
00203         long long int cstep;
00204         char cline[LINE_MAX];
00205 #ifdef WIN32
00206         __int64 oldpos = _ftelli64(colv_in[i]);
00207 #else
00208         off_t oldpos = ftello(colv_in[i]);
00209 #endif
00210         r = fgets(cline, LINE_MAX, colv_in[i]);
00211         if ( ! r ) { break; }
00212         if ( cline[0] == '#' ) {
00213           fprintf(colv_out[rep_id],"%s",cline);
00214           continue;
00215         } 
00216         rc = sscanf(cline, "%lld", &cstep);
00217         if ( rc != 1 ) {
00218           fprintf(stderr,"Format error in colvar trajectory for replica %d: %s",
00219                                                         i, cline);
00220           exit(-1);
00221         }
00222         if ( cstep == oldcstep ) continue;  /* filter out repeats */
00223         if ( cstep < oldcstep ) {
00224           fprintf(stderr,"Step out of order in colvar trajectory for replica %d: %s",
00225                                                         i, cline);
00226           exit(-1);
00227         }
00228         if ( cstep > step ) {
00229 #ifdef WIN32
00230           _fseeki64(colv_in[i], oldpos, SEEK_SET);
00231 #else
00232           fseeko(colv_in[i], oldpos, SEEK_SET);
00233 #endif
00234           break;
00235         }
00236         if ( i_run != 0 || oldcstep != -1 ) {  /* skip first entry */
00237           fprintf(colv_out[rep_id],"%s",cline);
00238         }
00239         oldcstep = cstep;
00240        }
00241       }
00242       if ( (i_run+1) % runs_per_frame ) continue;
00243       rc = plugin->read_next_timestep(traj_in[i],natoms,&frame);
00244       if ( rc == MOLFILE_SUCCESS ) {
00245         plugin->write_timestep(traj_out[rep_id],&frame);
00246       } else {
00247         fprintf(stderr,"Unable to read frame for replica %d at line %d: %s",
00248                                                         i, i_run, line);
00249         break;
00250       }
00251     }
00252     if ( i < num_replicas ) {
00253       printf("Processed %d runs.\n",i_run);
00254       if ( i ) fprintf(stderr,"Uneven input lengths for replica %d at line %d: %s",
00255                                                         i, i_run, line);
00256       break;
00257     }
00258   }
00259 
00260   free(frame.coords);
00261 
00262   for ( i=0; i<num_replicas; ++i ) {
00263     if ( fclose(hist_in[i]) ) {
00264       fprintf(stderr, "error closing history input file %d: %s\n", i, strerror(errno));
00265     }
00266     plugin->close_file_read(traj_in[i]);
00267     if ( fclose(hist_out[i]) ) {
00268       fprintf(stderr, "error closing history output file %d: %s\n", i, strerror(errno));
00269     }
00270     plugin->close_file_write(traj_out[i]);
00271     if ( colvars ) {
00272       if ( fclose(colv_in[i]) ) {
00273         fprintf(stderr, "error closing colvars input file %d: %s\n", i, strerror(errno));
00274       }
00275       if ( fclose(colv_out[i]) ) {
00276         fprintf(stderr, "error closing colvars output file %d: %s\n", i, strerror(errno));
00277       }
00278     }
00279   }
00280 
00281   molfile_dcdplugin_fini();
00282   exit(0);
00283 }

int molfile_dcdplugin_fini ( void   ) 

Referenced by main(), and ScriptTcl::~ScriptTcl().

int molfile_dcdplugin_init ( void   ) 

Referenced by main(), and ScriptTcl::ScriptTcl().

int molfile_dcdplugin_register ( void *  ,
vmdplugin_register_cb   
)

Referenced by main(), and ScriptTcl::ScriptTcl().

static int register_cb ( void *  v,
vmdplugin_t *  p 
) [static]

Definition at line 17 of file sortreplicas.c.

00017                                                 {
00018   *((vmdplugin_t **)v) = p;
00019   return VMDPLUGIN_SUCCESS;
00020 }


Generated on 23 Sep 2019 for NAMD by  doxygen 1.6.1