mirror of https://github.com/lammps/lammps.git
implement utils::current_date() convenience function to reduce replicated code
This commit is contained in:
parent
5a6c1abeed
commit
db76edbade
|
@ -203,6 +203,9 @@ Convenience functions
|
|||
.. doxygenfunction:: date2num
|
||||
:project: progguide
|
||||
|
||||
.. doxygenfunction:: current_date
|
||||
:project: progguide
|
||||
|
||||
Customized standard functions
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
|
|
|
@ -30,7 +30,6 @@
|
|||
#include "potential_file_reader.h"
|
||||
#include "tokenizer.h"
|
||||
#include "update.h"
|
||||
#include "fmt/chrono.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
|
@ -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;
|
||||
|
|
|
@ -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 <cmath>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -33,7 +33,6 @@
|
|||
#include "potential_file_reader.h"
|
||||
#include "tokenizer.h"
|
||||
#include "update.h"
|
||||
#include "fmt/chrono.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
|
@ -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;
|
||||
|
|
24
src/bond.cpp
24
src/bond.cpp
|
@ -23,9 +23,6 @@
|
|||
#include "neighbor.h"
|
||||
#include "suffix.h"
|
||||
#include "update.h"
|
||||
#include "fmt/chrono.h"
|
||||
|
||||
#include <ctime>
|
||||
|
||||
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
|
||||
|
|
18
src/pair.cpp
18
src/pair.cpp
|
@ -31,13 +31,11 @@
|
|||
#include "neighbor.h"
|
||||
#include "suffix.h"
|
||||
#include "update.h"
|
||||
#include "fmt/chrono.h"
|
||||
|
||||
#include <cfloat> // IWYU pragma: keep
|
||||
#include <climits> // IWYU pragma: keep
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
|
||||
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)
|
||||
|
|
|
@ -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 <cctype>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
|
||||
#if defined(__linux__)
|
||||
#include <unistd.h> // 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;
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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 <cerrno>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue