From e413aaf2172c44a7d81f3204917073a782704ed4 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Thu, 12 Sep 2019 14:37:42 -0400 Subject: [PATCH] add support for optionally including unit information in standard dump files --- src/COMPRESS/dump_atom_gz.cpp | 19 +++++++++---------- src/COMPRESS/dump_custom_gz.cpp | 19 +++++++++---------- src/dump.cpp | 11 +++++++++++ src/dump.h | 2 ++ src/dump_atom.cpp | 10 ++++++++-- src/dump_custom.cpp | 9 ++++++++- src/dump_local.cpp | 5 ++++- 7 files changed, 51 insertions(+), 24 deletions(-) diff --git a/src/COMPRESS/dump_atom_gz.cpp b/src/COMPRESS/dump_atom_gz.cpp index ef7e6583be..78f2c6d4f7 100644 --- a/src/COMPRESS/dump_atom_gz.cpp +++ b/src/COMPRESS/dump_atom_gz.cpp @@ -108,27 +108,26 @@ void DumpAtomGZ::openfile() void DumpAtomGZ::write_header(bigint ndump) { if ((multiproc) || (!multiproc && me == 0)) { + if (unit_flag && !unit_count) { + ++unit_count; + gzprintf(gzFp,"ITEM: UNITS\n%s\n",update->unit_style); + } + gzprintf(gzFp,"ITEM: TIMESTEP\n"); + gzprintf(gzFp,BIGINT_FORMAT "\n",update->ntimestep); + gzprintf(gzFp,"ITEM: NUMBER OF ATOMS\n"); + gzprintf(gzFp,BIGINT_FORMAT "\n",ndump); if (domain->triclinic == 0) { - gzprintf(gzFp,"ITEM: TIMESTEP\n"); - gzprintf(gzFp,BIGINT_FORMAT "\n",update->ntimestep); - gzprintf(gzFp,"ITEM: NUMBER OF ATOMS\n"); - gzprintf(gzFp,BIGINT_FORMAT "\n",ndump); gzprintf(gzFp,"ITEM: BOX BOUNDS %s\n",boundstr); gzprintf(gzFp,"%g %g\n",boxxlo,boxxhi); gzprintf(gzFp,"%g %g\n",boxylo,boxyhi); gzprintf(gzFp,"%g %g\n",boxzlo,boxzhi); - gzprintf(gzFp,"ITEM: ATOMS %s\n",columns); } else { - gzprintf(gzFp,"ITEM: TIMESTEP\n"); - gzprintf(gzFp,BIGINT_FORMAT "\n",update->ntimestep); - gzprintf(gzFp,"ITEM: NUMBER OF ATOMS\n"); - gzprintf(gzFp,BIGINT_FORMAT "\n",ndump); gzprintf(gzFp,"ITEM: BOX BOUNDS xy xz yz %s\n",boundstr); gzprintf(gzFp,"%g %g %g\n",boxxlo,boxxhi,boxxy); gzprintf(gzFp,"%g %g %g\n",boxylo,boxyhi,boxxz); gzprintf(gzFp,"%g %g %g\n",boxzlo,boxzhi,boxyz); - gzprintf(gzFp,"ITEM: ATOMS %s\n",columns); } + gzprintf(gzFp,"ITEM: ATOMS %s\n",columns); } } diff --git a/src/COMPRESS/dump_custom_gz.cpp b/src/COMPRESS/dump_custom_gz.cpp index 9c30f4742f..7eb77cb697 100644 --- a/src/COMPRESS/dump_custom_gz.cpp +++ b/src/COMPRESS/dump_custom_gz.cpp @@ -108,27 +108,26 @@ void DumpCustomGZ::openfile() void DumpCustomGZ::write_header(bigint ndump) { if ((multiproc) || (!multiproc && me == 0)) { + if (unit_flag && !unit_count) { + ++unit_count; + gzprintf(gzFp,"ITEM: UNITS\n%s\n",update->unit_style); + } + gzprintf(gzFp,"ITEM: TIMESTEP\n"); + gzprintf(gzFp,BIGINT_FORMAT "\n",update->ntimestep); + gzprintf(gzFp,"ITEM: NUMBER OF ATOMS\n"); + gzprintf(gzFp,BIGINT_FORMAT "\n",ndump); if (domain->triclinic == 0) { - gzprintf(gzFp,"ITEM: TIMESTEP\n"); - gzprintf(gzFp,BIGINT_FORMAT "\n",update->ntimestep); - gzprintf(gzFp,"ITEM: NUMBER OF ATOMS\n"); - gzprintf(gzFp,BIGINT_FORMAT "\n",ndump); gzprintf(gzFp,"ITEM: BOX BOUNDS %s\n",boundstr); gzprintf(gzFp,"%-1.16g %-1.16g\n",boxxlo,boxxhi); gzprintf(gzFp,"%-1.16g %-1.16g\n",boxylo,boxyhi); gzprintf(gzFp,"%-1.16g %-1.16g\n",boxzlo,boxzhi); - gzprintf(gzFp,"ITEM: ATOMS %s\n",columns); } else { - gzprintf(gzFp,"ITEM: TIMESTEP\n"); - gzprintf(gzFp,BIGINT_FORMAT "\n",update->ntimestep); - gzprintf(gzFp,"ITEM: NUMBER OF ATOMS\n"); - gzprintf(gzFp,BIGINT_FORMAT "\n",ndump); gzprintf(gzFp,"ITEM: BOX BOUNDS xy xz yz %s\n",boundstr); gzprintf(gzFp,"%-1.16g %-1.16g %-1.16g\n",boxxlo,boxxhi,boxxy); gzprintf(gzFp,"%-1.16g %-1.16g %-1.16g\n",boxylo,boxyhi,boxxz); gzprintf(gzFp,"%-1.16g %-1.16g %-1.16g\n",boxzlo,boxzhi,boxyz); - gzprintf(gzFp,"ITEM: ATOMS %s\n",columns); } + gzprintf(gzFp,"ITEM: ATOMS %s\n",columns); } } diff --git a/src/dump.cpp b/src/dump.cpp index 57a8decbb0..e995644a36 100644 --- a/src/dump.cpp +++ b/src/dump.cpp @@ -87,6 +87,8 @@ Dump::Dump(LAMMPS *lmp, int /*narg*/, char **arg) : Pointers(lmp) buffer_flag = 0; padflag = 0; pbcflag = 0; + unit_flag = 0; + unit_count = 0; delay_flag = 0; maxfiles = -1; @@ -545,6 +547,8 @@ void Dump::openfile() if (singlefile_opened) return; if (multifile == 0) singlefile_opened = 1; + unit_count = 0; + // if one file per timestep, replace '*' with current timestep char *filecurrent = filename; @@ -1119,6 +1123,13 @@ void Dump::modify_params(int narg, char **arg) } iarg += 2; + } else if (strcmp(arg[iarg],"units") == 0) { + if (iarg+2 > narg) error->all(FLERR,"Illegal dump_modify command"); + if (strcmp(arg[iarg+1],"yes") == 0) unit_flag = 1; + else if (strcmp(arg[iarg+1],"no") == 0) unit_flag = 0; + else error->all(FLERR,"Illegal dump_modify command"); + iarg += 2; + } else { int n = modify_param(narg-iarg,&arg[iarg]); if (n == 0) error->all(FLERR,"Illegal dump_modify command"); diff --git a/src/dump.h b/src/dump.h index bc7fd2d5a5..43855c9362 100644 --- a/src/dump.h +++ b/src/dump.h @@ -75,6 +75,8 @@ class Dump : protected Pointers { int sortcol; // 0 to sort on ID, 1-N on columns int sortcolm1; // sortcol - 1 int sortorder; // ASCEND or DESCEND + int unit_flag; // 1 if dump should contain unit information + int unit_count; // # of times the unit information was written int delay_flag; // 1 if delay output until delaystep bigint delaystep; diff --git a/src/dump_atom.cpp b/src/dump_atom.cpp index 8f09b93f1a..d5eba98078 100644 --- a/src/dump_atom.cpp +++ b/src/dump_atom.cpp @@ -209,7 +209,10 @@ void DumpAtom::header_binary_triclinic(bigint ndump) void DumpAtom::header_item(bigint ndump) { - fprintf(fp,"ITEM: UNITS\n%s\n",update->unit_style); + if (unit_flag && !unit_count) { + ++unit_count; + fprintf(fp,"ITEM: UNITS\n%s\n",update->unit_style); + } fprintf(fp,"ITEM: TIMESTEP\n"); fprintf(fp,BIGINT_FORMAT "\n",update->ntimestep); fprintf(fp,"ITEM: NUMBER OF ATOMS\n"); @@ -225,7 +228,10 @@ void DumpAtom::header_item(bigint ndump) void DumpAtom::header_item_triclinic(bigint ndump) { - fprintf(fp,"ITEM: UNITS\n%s\n",update->unit_style); + if (unit_flag && !unit_count) { + ++unit_count; + fprintf(fp,"ITEM: UNITS\n%s\n",update->unit_style); + } fprintf(fp,"ITEM: TIMESTEP\n"); fprintf(fp,BIGINT_FORMAT "\n",update->ntimestep); fprintf(fp,"ITEM: NUMBER OF ATOMS\n"); diff --git a/src/dump_custom.cpp b/src/dump_custom.cpp index 3de0e6fb3b..a99151f890 100644 --- a/src/dump_custom.cpp +++ b/src/dump_custom.cpp @@ -420,7 +420,10 @@ void DumpCustom::header_binary_triclinic(bigint ndump) void DumpCustom::header_item(bigint ndump) { - fprintf(fp,"ITEM: UNITS\n%s\n",update->unit_style); + if (unit_flag && !unit_count) { + ++unit_count; + fprintf(fp,"ITEM: UNITS\n%s\n",update->unit_style); + } fprintf(fp,"ITEM: TIMESTEP\n"); fprintf(fp,BIGINT_FORMAT "\n",update->ntimestep); fprintf(fp,"ITEM: NUMBER OF ATOMS\n"); @@ -436,6 +439,10 @@ void DumpCustom::header_item(bigint ndump) void DumpCustom::header_item_triclinic(bigint ndump) { + if (unit_flag && !unit_count) { + ++unit_count; + fprintf(fp,"ITEM: UNITS\n%s\n",update->unit_style); + } fprintf(fp,"ITEM: TIMESTEP\n"); fprintf(fp,BIGINT_FORMAT "\n",update->ntimestep); fprintf(fp,"ITEM: NUMBER OF ATOMS\n"); diff --git a/src/dump_local.cpp b/src/dump_local.cpp index 7cdc3ea16f..52a222eaeb 100644 --- a/src/dump_local.cpp +++ b/src/dump_local.cpp @@ -256,7 +256,10 @@ int DumpLocal::modify_param(int narg, char **arg) void DumpLocal::write_header(bigint ndump) { if (me == 0) { - fprintf(fp,"ITEM: UNITS\n%s\n",update->unit_style); + if (unit_flag && !unit_count) { + ++unit_count; + fprintf(fp,"ITEM: UNITS\n%s\n",update->unit_style); + } fprintf(fp,"ITEM: TIMESTEP\n"); fprintf(fp,BIGINT_FORMAT "\n",update->ntimestep); fprintf(fp,"ITEM: NUMBER OF %s\n",label);