"overwrite" option in "fix ave/time"


I have occasionally encountered unwanted output behavior when using the "overwrite" option in a "fix ave/time" command to compute a single file of time-averaged RDFs.

In some 1/10 cases or so (serial and parallel), there was one or multiple additional line(s) at the end of the output file (i.e., after the last row/bin), which I chased down to be due to using the fseek method for repositioning within the file and varying length of output via format descriptor "%g" in "fix_ave_time.cpp". The point is that if the previous file contained more characters than the current file the excess of characters will be displayed in the current file on its last line(s). For example, I got this as the last 5 lines when I expected the first line displayed here to be the last one in my file:
500 8.991 0.971134 99.3598 0.994786 198.731 0.989333 198.809
73598 99.3299 0.995568 198.664 0.986053 198.742
500 8.991 0.974799 99.2917 0.995324 198.566 0.98501 198.665
0.98355 198.544

I am not aware of an operating-system independent c++ function/method to remove excess characters from a file pointer that is open in "w" mode. Therefore, I suggest to get around this problem by closing and reopening "fp" if "overwrite" is chosen. This requires however that you copy the file name and titles to variables in the constructor/initialization of the fix and retain them throughout the simulation. I've attached an edited version of "fix_ave_time.cpp" and its header file that addresses this issue. The true LAMMPS experts should check them because I'm a LAMMPS newbie.

I grepped all other cpp files in the "src" folder that contain fseek, which are dump_dcd.cpp, fix_ave_correlate.cpp, fix_ave_histo.cpp, and fix_ave_spatial.cpp. I don't expect there to be a problem for the binary dcd file because of very strict and well-defined format used there. But it seems that similar changes should be done on fix_ave_correlate.cpp, fix_ave_histo.cpp, and fix_ave_spatial.cpp because they also implement the overwrite capability by exploiting fseek.

FYI, I'm currently using LAMMPS version as of Aug 21, 2013, and here comes the important part of my input script with respect to the problem described:

compute all_rdf all rdf 500 1 1 1 2 2 2

fix all_rdf_avetime all ave/time 100 1 100 &
     c_all_rdf[1] c_all_rdf[2] c_all_rdf[3] c_all_rdf[4] c_all_rdf[5] &
     c_all_rdf[6] c_all_rdf[7] &
     file rdf.dat mode vector ave running overwrite &
     title3 "# bin r/A g(r_O-O) coord(r_O-O) g(r_O-H) coord(r_O-H) g(r_H-O) coord(r_H-O) g(r_H-H) coord(r_H-H)"


fix_ave_time.cpp (31.1 KB)

fix_ave_time.h (3.95 KB)