sortreplicas.c

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

Generated on Sat Sep 23 01:17:16 2017 for NAMD by  doxygen 1.4.7