netcdf: deduplicate gettings units as strings

This commit is contained in:
Luthaf
2021-12-17 17:13:29 +01:00
parent f8ee6dc680
commit 1c25c96aaa
6 changed files with 210 additions and 214 deletions

View File

@ -19,6 +19,7 @@
#if defined(LMP_HAS_PNETCDF)
#include "dump_netcdf_mpiio.h"
#include "netcdf_units.h"
#include "atom.h"
#include "comm.h"
@ -102,7 +103,7 @@ DumpNetCDFMPIIO::DumpNetCDFMPIIO(LAMMPS *lmp, int narg, char **arg) :
int ndims = 1;
std::string mangled = earg[i];
bool constant = false;
Quantity quantity = Quantity::Unknown;
Quantity quantity = Quantity::UNKNOWN;
// name mangling
// in the AMBER specification
@ -110,12 +111,12 @@ DumpNetCDFMPIIO::DumpNetCDFMPIIO(LAMMPS *lmp, int narg, char **arg) :
idim = mangled[0] - 'x';
ndims = 3;
mangled = "coordinates";
quantity = Quantity::Distance;
quantity = Quantity::DISTANCE;
} else if ((mangled == "vx") || (mangled == "vy") || (mangled == "vz")) {
idim = mangled[1] - 'x';
ndims = 3;
mangled = "velocities";
quantity = Quantity::Velocity;
quantity = Quantity::VELOCITY;
} else if ((mangled == "xs") || (mangled == "ys") || (mangled == "zs")) {
idim = mangled[0] - 'x';
ndims = 3;
@ -125,17 +126,17 @@ DumpNetCDFMPIIO::DumpNetCDFMPIIO(LAMMPS *lmp, int narg, char **arg) :
idim = mangled[0] - 'x';
ndims = 3;
mangled = "unwrapped_coordinates";
quantity = Quantity::Distance;
quantity = Quantity::DISTANCE;
} else if ((mangled == "fx") || (mangled == "fy") || (mangled == "fz")) {
idim = mangled[1] - 'x';
ndims = 3;
mangled = "forces";
quantity = Quantity::Force;
quantity = Quantity::FORCE;
} else if ((mangled == "mux") || (mangled == "muy") || (mangled == "muz")) {
idim = mangled[2] - 'x';
ndims = 3;
mangled = "mu";
quantity = Quantity::DipoleMoment;
quantity = Quantity::DIPOLE_MOMENT;
} else if (utils::strmatch(mangled, "^c_")) {
std::size_t found = mangled.find('[');
if (found != std::string::npos) {
@ -423,7 +424,7 @@ void DumpNetCDFMPIIO::openfile()
NCERRX( ncmpi_def_var(ncid, perat[i].name, xtype, 3, dims, &perat[i].var), perat[i].name );
}
std::string unit = this->unit_of(perat[i].quantity);
std::string unit = get_unit_for(update->unit_style, perat[i].quantity, error);
if (!unit.empty()) {
NCERR( ncmpi_put_att_text(ncid, perat[i].var, NC_UNITS_STR, unit.size(), unit.c_str()) );
}
@ -455,10 +456,10 @@ void DumpNetCDFMPIIO::openfile()
NCERR( ncmpi_put_att_text(ncid, NC_GLOBAL, "programVersion", strlen(lmp->version), lmp->version) );
// units & scale
std::string unit = this->unit_of(Quantity::Time);
std::string unit = get_unit_for(update->unit_style, Quantity::TIME, error);
NCERR( ncmpi_put_att_text(ncid, time_var, NC_UNITS_STR, unit.size(), unit.c_str()) );
unit = this->unit_of(Quantity::Distance);
unit = get_unit_for(update->unit_style, Quantity::DISTANCE, error);
NCERR( ncmpi_put_att_text(ncid, cell_origin_var, NC_UNITS_STR, unit.size(), unit.c_str()) );
NCERR( ncmpi_put_att_text(ncid, cell_lengths_var, NC_UNITS_STR, unit.size(), unit.c_str()) );
@ -888,84 +889,4 @@ void DumpNetCDFMPIIO::ncerr(int err, const char *descr, int line)
}
}
/* ---------------------------------------------------------------------- */
std::string DumpNetCDF::unit_of(Quantity quantity) {
if (quantity == Quantity::Unknown) {
return "";
}
if (!strcmp(update->unit_style, "lj")) {
return "lj";
} else if (!strcmp(update->unit_style, "real")) {
switch (quantity) {
case Quantity::Time:
return "femtosecond";
case Quantity::Distance:
return "Angstrom";
case Quantity::Velocity:
return "Angstrom/femtosecond";
case Quantity::Force:
return "(Kcal/mol)/Angstrom)";
case Quantity::DipoleMoment:
return "e * Angstrom";
}
} else if (!strcmp(update->unit_style, "metal")) {
switch (quantity) {
case Quantity::Time:
return "picosecond";
case Quantity::Distance:
return "Angstrom";
case Quantity::Velocity:
return "Angstrom/picosecond";
case Quantity::Force:
return "eV/Angstrom";
case Quantity::DipoleMoment:
return "e * Angstrom";
}
} else if (!strcmp(update->unit_style, "si")) {
switch (quantity) {
case Quantity::Time:
return "second";
case Quantity::Distance:
return "meter";
case Quantity::Velocity:
return "meter/second";
case Quantity::Force:
return "Newton";
case Quantity::DipoleMoment:
return "Coulomb * meter";
}
} else if (!strcmp(update->unit_style, "cgs")) {
switch (quantity) {
case Quantity::Time:
return "second";
case Quantity::Distance:
return "centimeter";
case Quantity::Velocity:
return "centimeter/second";
case Quantity::Force:
return "dynes";
case Quantity::DipoleMoment:
return "statcoul * cm";
}
} else if (!strcmp(update->unit_style, "electron")) {
switch (quantity) {
case Quantity::Time:
return "second";
case Quantity::Distance:
return "centimeter";
case Quantity::Velocity:
return "centimeter/second";
case Quantity::Force:
return "Hartrees/Bohr";
case Quantity::DipoleMoment:
return "Debye";
}
}
error->all(FLERR, "Unsupported unit style: {}", update->unit_style);
return "";
}
#endif /* defined(LMP_HAS_PNETCDF) */