From d04f428c1af11ef6381e1fc940a7c777428294e6 Mon Sep 17 00:00:00 2001 From: Luthaf Date: Fri, 17 Dec 2021 10:54:43 +0100 Subject: [PATCH] netcdf: default to float variable for everything The standard convention require all values to be stored as float, users still have the ability to use double with `dump_modify double yes` --- src/NETCDF/dump_netcdf.cpp | 25 +++++++++++++------------ src/NETCDF/dump_netcdf.h | 2 +- src/NETCDF/dump_netcdf_mpiio.cpp | 24 +++++++++++++----------- src/NETCDF/dump_netcdf_mpiio.h | 2 +- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/NETCDF/dump_netcdf.cpp b/src/NETCDF/dump_netcdf.cpp index 5f30c941ca..2ad952e885 100644 --- a/src/NETCDF/dump_netcdf.cpp +++ b/src/NETCDF/dump_netcdf.cpp @@ -181,7 +181,7 @@ DumpNetCDF::DumpNetCDF(LAMMPS *lmp, int narg, char **arg) : int_buffer = nullptr; double_buffer = nullptr; - double_precision = false; + type_nc_real = NC_FLOAT; thermo = false; thermovar = nullptr; @@ -381,14 +381,14 @@ void DumpNetCDF::openfile() NCERRX( nc_def_var(ncid, NC_CELL_ANGULAR_STR, NC_CHAR, 2, dims, &cell_angular_var), NC_CELL_ANGULAR_STR ); dims[0] = frame_dim; - NCERRX( nc_def_var(ncid, NC_TIME_STR, NC_DOUBLE, 1, dims, &time_var), NC_TIME_STR); + NCERRX( nc_def_var(ncid, NC_TIME_STR, type_nc_real, 1, dims, &time_var), NC_TIME_STR); dims[0] = frame_dim; dims[1] = cell_spatial_dim; - NCERRX( nc_def_var(ncid, NC_CELL_ORIGIN_STR, NC_DOUBLE, 2, dims, &cell_origin_var), NC_CELL_ORIGIN_STR ); - NCERRX( nc_def_var(ncid, NC_CELL_LENGTHS_STR, NC_DOUBLE, 2, dims, &cell_lengths_var), NC_CELL_LENGTHS_STR ); + NCERRX( nc_def_var(ncid, NC_CELL_ORIGIN_STR, type_nc_real, 2, dims, &cell_origin_var), NC_CELL_ORIGIN_STR ); + NCERRX( nc_def_var(ncid, NC_CELL_LENGTHS_STR, type_nc_real, 2, dims, &cell_lengths_var), NC_CELL_LENGTHS_STR ); dims[0] = frame_dim; dims[1] = cell_angular_dim; - NCERRX( nc_def_var(ncid, NC_CELL_ANGLES_STR, NC_DOUBLE, 2, dims, &cell_angles_var), NC_CELL_ANGLES_STR ); + NCERRX( nc_def_var(ncid, NC_CELL_ANGLES_STR, type_nc_real, 2, dims, &cell_angles_var), NC_CELL_ANGLES_STR ); // variables specified in the input file dims[0] = frame_dim; @@ -397,7 +397,6 @@ void DumpNetCDF::openfile() for (int i = 0; i < n_perat; i++) { nc_type xtype; - // Type mangling if (vtype[perat[i].field[0]] == Dump::INT) { xtype = NC_INT; @@ -406,10 +405,7 @@ void DumpNetCDF::openfile() } else if (vtype[perat[i].field[0]] == Dump::STRING) { error->all(FLERR,"Dump netcdf currently does not support dumping string properties"); } else { - if (double_precision) - xtype = NC_DOUBLE; - else - xtype = NC_FLOAT; + xtype = type_nc_real; } if (perat[i].constant) { @@ -437,7 +433,7 @@ void DumpNetCDF::openfile() Thermo *th = output->thermo; for (int i = 0; i < th->nfield; i++) { if (th->vtype[i] == Thermo::FLOAT) { - NCERRX( nc_def_var(ncid, th->keyword[i], NC_DOUBLE, 1, dims, + NCERRX( nc_def_var(ncid, th->keyword[i], type_nc_real, 1, dims, &thermovar[i]), th->keyword[i] ); } else if (th->vtype[i] == Thermo::INT) { NCERRX( nc_def_var(ncid, th->keyword[i], NC_INT, 1, dims, @@ -872,7 +868,12 @@ int DumpNetCDF::modify_param(int narg, char **arg) if (strcmp(arg[iarg],"double") == 0) { iarg++; if (iarg >= narg) error->all(FLERR,"expected 'yes' or 'no' after 'double' keyword."); - double_precision = utils::logical(FLERR,arg[iarg],false,lmp) == 1; + + if (utils::logical(FLERR,arg[iarg],false,lmp) == 1) + type_nc_real = NC_DOUBLE; + else + type_nc_real = NC_FLOAT; + iarg++; return 2; } else if (strcmp(arg[iarg],"at") == 0) { diff --git a/src/NETCDF/dump_netcdf.h b/src/NETCDF/dump_netcdf.h index dd9c50873e..b3bd055701 100644 --- a/src/NETCDF/dump_netcdf.h +++ b/src/NETCDF/dump_netcdf.h @@ -62,7 +62,7 @@ class DumpNetCDF : public DumpCustom { int *thermovar; // NetCDF variables for thermo output - bool double_precision; // write everything as double precision + int type_nc_real; // netcdf type to use for real variables: float or double bool thermo; // write thermo output to netcdf file bigint n_buffer; // size of buffer diff --git a/src/NETCDF/dump_netcdf_mpiio.cpp b/src/NETCDF/dump_netcdf_mpiio.cpp index 0a76203f96..8241d159ae 100644 --- a/src/NETCDF/dump_netcdf_mpiio.cpp +++ b/src/NETCDF/dump_netcdf_mpiio.cpp @@ -179,7 +179,7 @@ DumpNetCDFMPIIO::DumpNetCDFMPIIO(LAMMPS *lmp, int narg, char **arg) : int_buffer = nullptr; double_buffer = nullptr; - double_precision = false; + type_nc_real = NC_FLOAT; thermo = false; thermovar = nullptr; @@ -382,14 +382,14 @@ void DumpNetCDFMPIIO::openfile() NCERRX( ncmpi_def_var(ncid, NC_CELL_ANGULAR_STR, NC_CHAR, 2, dims, &cell_angular_var), NC_CELL_ANGULAR_STR ); dims[0] = frame_dim; - NCERRX( ncmpi_def_var(ncid, NC_TIME_STR, NC_DOUBLE, 1, dims, &time_var), NC_TIME_STR); + NCERRX( ncmpi_def_var(ncid, NC_TIME_STR, type_nc_real, 1, dims, &time_var), NC_TIME_STR); dims[0] = frame_dim; dims[1] = cell_spatial_dim; - NCERRX( ncmpi_def_var(ncid, NC_CELL_ORIGIN_STR, NC_DOUBLE, 2, dims, &cell_origin_var), NC_CELL_ORIGIN_STR ); - NCERRX( ncmpi_def_var(ncid, NC_CELL_LENGTHS_STR, NC_DOUBLE, 2, dims, &cell_lengths_var), NC_CELL_LENGTHS_STR ); + NCERRX( ncmpi_def_var(ncid, NC_CELL_ORIGIN_STR, type_nc_real, 2, dims, &cell_origin_var), NC_CELL_ORIGIN_STR ); + NCERRX( ncmpi_def_var(ncid, NC_CELL_LENGTHS_STR, type_nc_real, 2, dims, &cell_lengths_var), NC_CELL_LENGTHS_STR ); dims[0] = frame_dim; dims[1] = cell_angular_dim; - NCERRX( ncmpi_def_var(ncid, NC_CELL_ANGLES_STR, NC_DOUBLE, 2, dims, &cell_angles_var), NC_CELL_ANGLES_STR ); + NCERRX( ncmpi_def_var(ncid, NC_CELL_ANGLES_STR, type_nc_real, 2, dims, &cell_angles_var), NC_CELL_ANGLES_STR ); // variables specified in the input file dims[0] = frame_dim; @@ -405,10 +405,7 @@ void DumpNetCDFMPIIO::openfile() } else if (vtype[perat[i].field[0]] == Dump::BIGINT) { xtype = NC_INT64; } else { - if (double_precision) - xtype = NC_DOUBLE; - else - xtype = NC_FLOAT; + xtype = type_nc_real; } if (perat[i].dims == 1) { @@ -425,7 +422,7 @@ void DumpNetCDFMPIIO::openfile() Thermo *th = output->thermo; for (int i = 0; i < th->nfield; i++) { if (th->vtype[i] == Thermo::FLOAT) { - NCERRX( ncmpi_def_var(ncid, th->keyword[i], NC_DOUBLE, 1, dims, &thermovar[i]), th->keyword[i] ); + NCERRX( ncmpi_def_var(ncid, th->keyword[i], type_nc_real, 1, dims, &thermovar[i]), th->keyword[i] ); } else if (th->vtype[i] == Thermo::INT) { NCERRX( ncmpi_def_var(ncid, th->keyword[i], NC_INT, 1, dims, &thermovar[i]), th->keyword[i] ); } else if (th->vtype[i] == Thermo::BIGINT) { @@ -867,7 +864,12 @@ int DumpNetCDFMPIIO::modify_param(int narg, char **arg) if (strcmp(arg[iarg],"double") == 0) { iarg++; if (iarg >= narg) error->all(FLERR,"expected 'yes' or 'no' after 'double' keyword."); - double_precision = utils::logical(FLERR,arg[iarg],false,lmp) == 1; + + if (utils::logical(FLERR,arg[iarg],false,lmp) == 1) + type_nc_real = NC_DOUBLE; + else + type_nc_real = NC_FLOAT; + iarg++; return 2; } else if (strcmp(arg[iarg],"at") == 0) { diff --git a/src/NETCDF/dump_netcdf_mpiio.h b/src/NETCDF/dump_netcdf_mpiio.h index 56c07fc3d3..d1fb4fd364 100644 --- a/src/NETCDF/dump_netcdf_mpiio.h +++ b/src/NETCDF/dump_netcdf_mpiio.h @@ -61,7 +61,7 @@ class DumpNetCDFMPIIO : public DumpCustom { int *thermovar; // NetCDF variables for thermo output - bool double_precision; // write everything as double precision + int type_nc_real; // netcdf type to use for real variables: float or double bool thermo; // write thermo output to netcdf file bigint n_buffer; // size of buffer