Extends header of new binary format of dump atom

This commit is contained in:
Richard Berger
2020-08-11 17:37:46 -04:00
parent 1238ad5d83
commit 560c29a0e1
3 changed files with 61 additions and 17 deletions

View File

@ -166,13 +166,44 @@ void DumpAtom::write_data(int n, double *mybuf)
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
void DumpAtom::header_binary(bigint ndump) void DumpAtom::format_magic_string_binary()
{ {
// use negative ntimestep as marker for new format // use negative ntimestep as marker for new format
bigint fmtlen = strlen("DUMPATOM"); bigint fmtlen = strlen(MAGIC_STRING);
bigint marker = -fmtlen; bigint marker = -fmtlen;
fwrite(&marker, sizeof(bigint), 1, fp); fwrite(&marker, sizeof(bigint), 1, fp);
fwrite("DUMPATOM",sizeof(char),fmtlen,fp); fwrite(MAGIC_STRING, sizeof(char), fmtlen, fp);
}
/* ---------------------------------------------------------------------- */
void DumpAtom::format_endian_binary()
{
int endian = ENDIAN;
fwrite(&endian, sizeof(int), 1, fp);
}
/* ---------------------------------------------------------------------- */
void DumpAtom::format_revision_binary()
{
int revision = FORMAT_REVISION;
fwrite(&revision, sizeof(int), 1, fp);
}
/* ---------------------------------------------------------------------- */
void DumpAtom::header_format_binary()
{
format_magic_string_binary();
format_endian_binary();
format_revision_binary();
}
/* ---------------------------------------------------------------------- */
void DumpAtom::header_binary(bigint ndump)
{
header_format_binary();
fwrite(&update->ntimestep,sizeof(bigint),1,fp); fwrite(&update->ntimestep,sizeof(bigint),1,fp);
fwrite(&ndump,sizeof(bigint),1,fp); fwrite(&ndump,sizeof(bigint),1,fp);
fwrite(&domain->triclinic,sizeof(int),1,fp); fwrite(&domain->triclinic,sizeof(int),1,fp);
@ -197,11 +228,7 @@ void DumpAtom::header_binary(bigint ndump)
void DumpAtom::header_binary_triclinic(bigint ndump) void DumpAtom::header_binary_triclinic(bigint ndump)
{ {
// use negative ntimestep as marker for new format header_format_binary();
bigint fmtlen = strlen("DUMPATOM");
bigint marker = -fmtlen;
fwrite(&marker,sizeof(bigint),1,fp);
fwrite("DUMPATOM",sizeof(char),fmtlen,fp);
fwrite(&update->ntimestep,sizeof(bigint),1,fp); fwrite(&update->ntimestep,sizeof(bigint),1,fp);
fwrite(&ndump,sizeof(bigint),1,fp); fwrite(&ndump,sizeof(bigint),1,fp);
fwrite(&domain->triclinic,sizeof(int),1,fp); fwrite(&domain->triclinic,sizeof(int),1,fp);

View File

@ -28,6 +28,10 @@ class DumpAtom : public Dump {
public: public:
DumpAtom(LAMMPS *, int, char**); DumpAtom(LAMMPS *, int, char**);
const char * MAGIC_STRING = "DUMPATOM";
const int FORMAT_REVISION = 0x0002;
const int ENDIAN = 0x0001;
protected: protected:
int scale_flag; // 1 if atom coords are scaled, 0 if no int scale_flag; // 1 if atom coords are scaled, 0 if no
int image_flag; // 1 if append box count to atom coords, 0 if no int image_flag; // 1 if append box count to atom coords, 0 if no
@ -41,6 +45,11 @@ class DumpAtom : public Dump {
int convert_string(int, double *); int convert_string(int, double *);
void write_data(int, double *); void write_data(int, double *);
void header_format_binary();
void format_magic_string_binary();
void format_endian_binary();
void format_revision_binary();
typedef void (DumpAtom::*FnPtrHeader)(bigint); typedef void (DumpAtom::*FnPtrHeader)(bigint);
FnPtrHeader header_choice; // ptr to write header functions FnPtrHeader header_choice; // ptr to write header functions
void header_binary(bigint); void header_binary(bigint);

View File

@ -91,13 +91,15 @@ int main(int narg, char **arg)
delete [] filetxt; delete [] filetxt;
// detect newer format // detect newer format
char * formatname = nullptr; char * magic_string = nullptr;
char * columns = nullptr; char * columns = nullptr;
// loop over snapshots in file // loop over snapshots in file
while (1) { while (1) {
int endian = 0x0001;
int revision = 0x0001;
fread(&ntimestep,sizeof(bigint),1,fp); fread(&ntimestep,sizeof(bigint),1,fp);
@ -112,12 +114,18 @@ int main(int narg, char **arg)
// detect newer format // detect newer format
if (ntimestep < 0) { if (ntimestep < 0) {
// first bigint encodes negative format name length // first bigint encodes negative format name length
bigint formatlen = -ntimestep; bigint magic_string_len = -ntimestep;
delete [] formatname; delete [] magic_string;
formatname = new char[formatlen + 1]; magic_string = new char[magic_string_len + 1];
fread(formatname, sizeof(char), formatlen, fp); fread(magic_string, sizeof(char), magic_string_len, fp);
formatname[formatlen] = '\0'; magic_string[magic_string_len] = '\0';
// read endian flag
fread(&endian, sizeof(int), 1, fp);
// read revision number
fread(&revision, sizeof(int), 1, fp);
// read the real ntimestep // read the real ntimestep
fread(&ntimestep,sizeof(bigint),1,fp); fread(&ntimestep,sizeof(bigint),1,fp);
@ -139,7 +147,7 @@ int main(int narg, char **arg)
} }
fread(&size_one,sizeof(int),1,fp); fread(&size_one,sizeof(int),1,fp);
if (formatname) { if (magic_string && revision > 0x0001) {
// newer format includes columns string // newer format includes columns string
int len = 0; int len = 0;
fread(&len, sizeof(int), 1, fp); fread(&len, sizeof(int), 1, fp);
@ -220,7 +228,7 @@ int main(int narg, char **arg)
} }
printf("\n"); printf("\n");
delete [] columns; delete [] columns;
delete [] formatname; delete [] magic_string;
} }
if (buf) delete [] buf; if (buf) delete [] buf;