15 #include "molfile_plugin.h"
18 *((vmdplugin_t **)v) = p;
19 return VMDPLUGIN_SUCCESS;
22 #include <sys/types.h>
28 int main(
int argc,
char **argv) {
30 molfile_timestep_t frame;
31 molfile_plugin_t *plugin;
36 long long int final_step = -1;
37 long long int checkstep = -1;
45 int natoms=MOLFILE_NUMATOMS_UNKNOWN;
51 if ( argc < 4 || argc > 5 ) {
52 fprintf(stderr,
"args: <job_output_root> <num_replicas> <runs_per_frame> [final_step]\n");
55 output_root = argv[1];
56 num_replicas = atoi(argv[2]);
57 runs_per_frame = atoi(argv[3]);
59 sscanf(argv[4],
"%lld", &final_step);
62 filename = (
char*) malloc(strlen(output_root)+100);
63 hist_in = (FILE**) malloc(num_replicas*
sizeof(FILE*));
64 hist_out = (FILE**) malloc(num_replicas*
sizeof(FILE*));
65 colv_in = (FILE**) malloc(num_replicas*
sizeof(FILE*));
66 colv_out = (FILE**) malloc(num_replicas*
sizeof(FILE*));
67 traj_in = (
void**) malloc(num_replicas*
sizeof(FILE*));
68 traj_out = (
void**) malloc(num_replicas*
sizeof(FILE*));
70 for ( i=0; i<num_replicas; ++i ) {
72 if ( strstr(output_root,
"%s") ) {
75 sprintf(filename,output_root,istr);
77 sprintf(filename,output_root,i);
79 root_end = filename + strlen(filename);
81 sprintf(root_end,
".%d.history",i);
82 hist_in[i] = fopen(filename,
"r");
84 fprintf(stderr,
"error opening input file %s: %s\n",
85 filename, strerror(errno));
88 sprintf(root_end,
".%d.dcd",i);
89 traj_in[i] = plugin->open_file_read(filename,
"dcd",&natoms);
91 fprintf(stderr,
"error opening input file %s: %s\n",
92 filename, strerror(errno));
95 sprintf(root_end,
".%d.colvars.traj",i);
96 colv_in[i] = fopen(filename,
"r");
99 printf(
"Found first input colvars trajectory file %s.\n", filename);
101 }
else if ( ! colvars ) {
102 fprintf(stderr,
"missing input colvars trajectory files before %s\n", filename);
108 }
else if ( colvars ) {
109 fprintf(stderr,
"error opening input colvars trajectory file %s: %s\n",
110 filename, strerror(errno));
116 for ( i=0; i<num_replicas; ++i ) {
118 if ( strstr(output_root,
"%s") ) {
120 sprintf(istr,
"%d",i);
121 sprintf(filename,output_root,istr);
123 sprintf(filename,output_root,i);
125 root_end = filename + strlen(filename);
127 sprintf(root_end,
".%d.sort.history",i);
128 hist_out[i] = fopen(filename,
"w");
129 if ( ! hist_out[i] ) {
130 fprintf(stderr,
"error opening output file %s: %s\n",
131 filename, strerror(errno));
134 sprintf(root_end,
".%d.sort.dcd",i);
135 traj_out[i] = plugin->open_file_write(filename,
"dcd",natoms);
136 if ( ! traj_out[i] ) {
137 fprintf(stderr,
"error opening output file %s: %s\n",
138 filename, strerror(errno));
142 sprintf(root_end,
".%d.sort.colvars.traj",i);
143 colv_out[i] = fopen(filename,
"w");
144 if ( ! colv_out[i] ) {
145 fprintf(stderr,
"error opening output file %s: %s\n",
146 filename, strerror(errno));
152 frame.coords = (
float*) malloc(3*natoms*
sizeof(
float));
153 frame.velocities = (
float*) NULL;
155 #define LINE_MAX 10000
158 for ( ; 1; ++i_run ) {
160 for ( i=0; i<num_replicas; ++i ) {
167 r = fgets(line,
LINE_MAX, hist_in[i]);
168 if ( ! r ) {
break; }
169 rc = sscanf(line,
"%lld %n%d%n", &step, &f1, &rep_id, &f2);
171 fprintf(stderr,
"Format error for replica %d at line %d: %s",
176 if ( step <= checkstep ) {
177 fprintf(stderr,
"Step out of order for replica %d at line %d: %s",
182 if ( final_step >= 0 && checkstep > final_step ) {
183 printf(
"Stopping after final step %lld.\n", final_step);
186 }
else if ( step != checkstep ) {
187 fprintf(stderr,
"Step mismatch for replica %d at line %d: %s",
191 if ( rep_id < 0 || rep_id >= num_replicas ) {
192 fprintf(stderr,
"Invalid replica ID for replica %d at line %d: %s",
198 fprintf(hist_out[rep_id],
"%s%d%s",line,i,line+f2);
201 long long int oldcstep = -1;
206 __int64 oldpos = _ftelli64(colv_in[i]);
208 off_t oldpos = ftello(colv_in[i]);
210 r = fgets(cline,
LINE_MAX, colv_in[i]);
211 if ( ! r ) {
break; }
212 if ( cline[0] ==
'#' ) {
213 fprintf(colv_out[rep_id],
"%s",cline);
216 rc = sscanf(cline,
"%lld", &cstep);
218 fprintf(stderr,
"Format error in colvar trajectory for replica %d: %s",
222 if ( cstep == oldcstep )
continue;
223 if ( cstep < oldcstep ) {
224 fprintf(stderr,
"Step out of order in colvar trajectory for replica %d: %s",
228 if ( cstep > step ) {
230 _fseeki64(colv_in[i], oldpos, SEEK_SET);
232 fseeko(colv_in[i], oldpos, SEEK_SET);
236 if ( i_run != 0 || oldcstep != -1 ) {
237 fprintf(colv_out[rep_id],
"%s",cline);
242 if ( (i_run+1) % runs_per_frame )
continue;
243 rc = plugin->read_next_timestep(traj_in[i],natoms,&frame);
244 if ( rc == MOLFILE_SUCCESS ) {
245 plugin->write_timestep(traj_out[rep_id],&frame);
247 fprintf(stderr,
"Unable to read frame for replica %d at line %d: %s",
252 if ( i < num_replicas ) {
253 printf(
"Processed %d runs.\n",i_run);
254 if ( i ) fprintf(stderr,
"Uneven input lengths for replica %d at line %d: %s",
262 for ( i=0; i<num_replicas; ++i ) {
263 if ( fclose(hist_in[i]) ) {
264 fprintf(stderr,
"error closing history input file %d: %s\n", i, strerror(errno));
266 plugin->close_file_read(traj_in[i]);
267 if ( fclose(hist_out[i]) ) {
268 fprintf(stderr,
"error closing history output file %d: %s\n", i, strerror(errno));
270 plugin->close_file_write(traj_out[i]);
272 if ( fclose(colv_in[i]) ) {
273 fprintf(stderr,
"error closing colvars input file %d: %s\n", i, strerror(errno));
275 if ( fclose(colv_out[i]) ) {
276 fprintf(stderr,
"error closing colvars output file %d: %s\n", i, strerror(errno));
int molfile_dcdplugin_init(void)
int register_cb(void *v, vmdplugin_t *p)
int main(int argc, char *argv[])
int molfile_dcdplugin_fini(void)
int molfile_dcdplugin_register(void *, vmdplugin_register_cb)