diff --git a/doc/src/Developer_utils.rst b/doc/src/Developer_utils.rst index 54a21e5e44..17da8bef22 100644 --- a/doc/src/Developer_utils.rst +++ b/doc/src/Developer_utils.rst @@ -203,6 +203,9 @@ Convenience functions .. doxygenfunction:: date2num :project: progguide +.. doxygenfunction:: current_date + :project: progguide + Customized standard functions ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/EXTRA-FIX/fix_ttm.cpp b/src/EXTRA-FIX/fix_ttm.cpp index 05727d8648..abcbd2ba1e 100644 --- a/src/EXTRA-FIX/fix_ttm.cpp +++ b/src/EXTRA-FIX/fix_ttm.cpp @@ -30,7 +30,6 @@ #include "potential_file_reader.h" #include "tokenizer.h" #include "update.h" -#include "fmt/chrono.h" #include #include @@ -527,14 +526,11 @@ void FixTTM::write_electron_temperatures(const std::string &filename) { if (comm->me) return; - time_t tv = time(nullptr); - std::tm current_date = fmt::localtime(tv); - FILE *fp = fopen(filename.c_str(),"w"); if (!fp) error->one(FLERR,"Fix ttm could not open output file {}: {}", filename,utils::getsyserror()); - fmt::print(fp,"# DATE: {:%Y-%m-%d} UNITS: {} COMMENT: Electron temperature " - "{}x{}x{} grid at step {}. Created by fix {}\n", current_date, + fmt::print(fp,"# DATE: {} UNITS: {} COMMENT: Electron temperature " + "{}x{}x{} grid at step {}. Created by fix {}\n", utils::current_date(), update->unit_style, nxgrid, nygrid, nzgrid, update->ntimestep, style); int ix,iy,iz; diff --git a/src/EXTRA-FIX/fix_ttm_grid.cpp b/src/EXTRA-FIX/fix_ttm_grid.cpp index accc792b4a..f3e84c4693 100644 --- a/src/EXTRA-FIX/fix_ttm_grid.cpp +++ b/src/EXTRA-FIX/fix_ttm_grid.cpp @@ -23,18 +23,15 @@ #include "comm.h" #include "domain.h" #include "error.h" -#include "force.h" #include "gridcomm.h" #include "memory.h" #include "neighbor.h" #include "random_mars.h" #include "tokenizer.h" #include "update.h" -#include "fmt/chrono.h" #include #include -#include using namespace LAMMPS_NS; using namespace FixConst; @@ -355,14 +352,11 @@ void FixTTMGrid::read_electron_temperatures(const std::string &filename) void FixTTMGrid::write_electron_temperatures(const std::string &filename) { if (comm->me == 0) { - time_t tv = time(nullptr); - std::tm current_date = fmt::localtime(tv); - FPout = fopen(filename.c_str(), "w"); if (!FPout) error->one(FLERR, "Fix ttm/grid could not open output file"); - fmt::print(FPout,"# DATE: {:%Y-%m-%d} UNITS: {} COMMENT: Electron temperature " - "{}x{}x{} grid at step {}. Created by fix {}\n", current_date, + fmt::print(FPout,"# DATE: {} UNITS: {} COMMENT: Electron temperature " + "{}x{}x{} grid at step {}. Created by fix {}\n", utils::current_date(), update->unit_style, nxgrid, nygrid, nzgrid, update->ntimestep, style); } diff --git a/src/EXTRA-FIX/fix_ttm_mod.cpp b/src/EXTRA-FIX/fix_ttm_mod.cpp index cec97bd280..0aef2ec4ff 100644 --- a/src/EXTRA-FIX/fix_ttm_mod.cpp +++ b/src/EXTRA-FIX/fix_ttm_mod.cpp @@ -33,7 +33,6 @@ #include "potential_file_reader.h" #include "tokenizer.h" #include "update.h" -#include "fmt/chrono.h" #include #include @@ -620,14 +619,11 @@ void FixTTMMod::write_electron_temperatures(const std::string &filename) { if (comm->me) return; - time_t tv = time(nullptr); - std::tm current_date = fmt::localtime(tv); - FILE *fp = fopen(filename.c_str(),"w"); if (!fp) error->one(FLERR,"Fix ttm/mod could not open output file {}: {}", filename, utils::getsyserror()); - fmt::print(fp,"# DATE: {:%Y-%m-%d} UNITS: {} COMMENT: Electron temperature " - "{}x{}x{} grid at step {}. Created by fix {}\n", current_date, + fmt::print(fp,"# DATE: {} UNITS: {} COMMENT: Electron temperature " + "{}x{}x{} grid at step {}. Created by fix {}\n", utils::current_date(), update->unit_style, nxgrid, nygrid, nzgrid, update->ntimestep, style); int ix,iy,iz; diff --git a/src/bond.cpp b/src/bond.cpp index cc13ad6b86..e636ae9bf8 100644 --- a/src/bond.cpp +++ b/src/bond.cpp @@ -23,9 +23,6 @@ #include "neighbor.h" #include "suffix.h" #include "update.h" -#include "fmt/chrono.h" - -#include using namespace LAMMPS_NS; @@ -276,26 +273,21 @@ void Bond::write_file(int narg, char **arg) if (utils::file_is_readable(table_file)) { std::string units = utils::get_potential_units(table_file,"table"); if (!units.empty() && (units != update->unit_style)) { - error->one(FLERR,"Trying to append to a table file " - "with UNITS: {} while units are {}", - units, update->unit_style); + error->one(FLERR,"Trying to append to a table file with UNITS: {} while units are {}", + units, update->unit_style); } std::string date = utils::get_potential_date(table_file,"table"); - utils::logmesg(lmp,"Appending to table file {} with " - "DATE: {}\n", table_file, date); + utils::logmesg(lmp,"Appending to table file {} with DATE: {}\n", table_file, date); fp = fopen(table_file.c_str(),"a"); } else { - time_t tv = time(nullptr); - std::tm current_date = fmt::localtime(tv); - utils::logmesg(lmp,"Creating table file {} with " - "DATE: {:%Y-%m-%d}\n", table_file, current_date); + utils::logmesg(lmp,"Creating table file {} with DATE: {}\n", + table_file, utils::current_date()); fp = fopen(table_file.c_str(),"w"); - if (fp) fmt::print(fp,"# DATE: {:%Y-%m-%d} UNITS: {} Created by bond_write\n", - current_date, update->unit_style); + if (fp) fmt::print(fp,"# DATE: {} UNITS: {} Created by bond_write\n", + utils::current_date(), update->unit_style); } if (fp == nullptr) - error->one(FLERR,"Cannot open bond_write file {}: {}", - arg[4], utils::getsyserror()); + error->one(FLERR,"Cannot open bond_write file {}: {}", arg[4], utils::getsyserror()); } // initialize potentials before evaluating bond potential diff --git a/src/pair.cpp b/src/pair.cpp index 6edc8dc831..38a21481cd 100644 --- a/src/pair.cpp +++ b/src/pair.cpp @@ -31,13 +31,11 @@ #include "neighbor.h" #include "suffix.h" #include "update.h" -#include "fmt/chrono.h" #include // IWYU pragma: keep #include // IWYU pragma: keep #include #include -#include using namespace LAMMPS_NS; using namespace MathConst; @@ -1815,21 +1813,17 @@ void Pair::write_file(int narg, char **arg) units, update->unit_style); } std::string date = utils::get_potential_date(table_file,"table"); - utils::logmesg(lmp,"Appending to table file {} with DATE: {}\n", - table_file, date); + utils::logmesg(lmp,"Appending to table file {} with DATE: {}\n", table_file, date); fp = fopen(table_file.c_str(),"a"); } else { - time_t tv = time(nullptr); - std::tm current_date = fmt::localtime(tv); - utils::logmesg(lmp,"Creating table file {} with DATE: {:%Y-%m-%d}\n", - table_file, current_date); + utils::logmesg(lmp,"Creating table file {} with DATE: {}\n", + table_file, utils::current_date()); fp = fopen(table_file.c_str(),"w"); - if (fp) fmt::print(fp,"# DATE: {:%Y-%m-%d} UNITS: {} Created by pair_write\n", - current_date, update->unit_style); + if (fp) fmt::print(fp,"# DATE: {} UNITS: {} Created by pair_write\n", + utils::current_date(), update->unit_style); } if (fp == nullptr) - error->one(FLERR,"Cannot open pair_write file {}: {}", - table_file, utils::getsyserror()); + error->one(FLERR,"Cannot open pair_write file {}: {}",table_file, utils::getsyserror()); fprintf(fp,"# Pair potential %s for atom types %d %d: i,r,energy,force\n", force->pair_style,itype,jtype); if (style == RLINEAR) diff --git a/src/utils.cpp b/src/utils.cpp index 866b3773d1..b2a1e801f7 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -18,6 +18,7 @@ #include "compute.h" #include "error.h" #include "fix.h" +#include "fmt/chrono.h" #include "memory.h" #include "modify.h" #include "text_file_reader.h" @@ -27,6 +28,7 @@ #include #include #include +#include #if defined(__linux__) #include // for readlink @@ -1316,7 +1318,21 @@ int utils::date2num(const std::string &date) return num; } -/* binary search in vector of ascending doubles */ +/* ---------------------------------------------------------------------- + get formatted string of current date from fmtlib +------------------------------------------------------------------------- */ + +std::string utils::current_date() +{ + time_t tv = time(nullptr); + std::tm today = fmt::localtime(tv); + return fmt::format("{:%Y-%m-%d}", today); +} + +/* ---------------------------------------------------------------------- + binary search in vector of ascending doubles +------------------------------------------------------------------------- */ + int utils::binary_search(const double needle, const int n, const double *haystack) { int lo = 0; diff --git a/src/utils.h b/src/utils.h index a4f94d90cf..c261ab3229 100644 --- a/src/utils.h +++ b/src/utils.h @@ -541,6 +541,14 @@ namespace utils { int date2num(const std::string &date); + /*! Return current date as string + * + * This will generate a string containing the current date in YYYY-MM-DD format. + * + * \return string with current date */ + + std::string current_date(); + /*! Binary search in a vector of ascending doubles of length N * * If the value is smaller than the smallest value in the vector, 0 is returned. diff --git a/unittest/utils/test_utils.cpp b/unittest/utils/test_utils.cpp index 5fb45cdf77..e0bc4984cc 100644 --- a/unittest/utils/test_utils.cpp +++ b/unittest/utils/test_utils.cpp @@ -14,8 +14,11 @@ #include "lmptype.h" #include "pointers.h" #include "utils.h" +#include "tokenizer.h" + #include "gmock/gmock.h" #include "gtest/gtest.h" + #include #include #include @@ -875,6 +878,19 @@ TEST(Utils, date2num) ASSERT_EQ(utils::date2num("31December100"), 1001231); } +TEST(Utils, current_date) +{ + auto vals = ValueTokenizer(utils::current_date(),"-"); + int year = vals.next_int(); + int month = vals.next_int(); + int day = vals.next_int(); + ASSERT_GT(year,2020); + ASSERT_GE(month,1); + ASSERT_GE(day,1); + ASSERT_LE(month,12); + ASSERT_LE(day,31); +} + TEST(Utils, binary_search) { double data[] = {-2.0, -1.8, -1.0, -1.0, -1.0, -0.5, -0.2, 0.0, 0.1, 0.1,