implement utils::current_date() convenience function to reduce replicated code
This commit is contained in:
@ -203,6 +203,9 @@ Convenience functions
|
|||||||
.. doxygenfunction:: date2num
|
.. doxygenfunction:: date2num
|
||||||
:project: progguide
|
:project: progguide
|
||||||
|
|
||||||
|
.. doxygenfunction:: current_date
|
||||||
|
:project: progguide
|
||||||
|
|
||||||
Customized standard functions
|
Customized standard functions
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
|||||||
@ -30,7 +30,6 @@
|
|||||||
#include "potential_file_reader.h"
|
#include "potential_file_reader.h"
|
||||||
#include "tokenizer.h"
|
#include "tokenizer.h"
|
||||||
#include "update.h"
|
#include "update.h"
|
||||||
#include "fmt/chrono.h"
|
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@ -527,14 +526,11 @@ void FixTTM::write_electron_temperatures(const std::string &filename)
|
|||||||
{
|
{
|
||||||
if (comm->me) return;
|
if (comm->me) return;
|
||||||
|
|
||||||
time_t tv = time(nullptr);
|
|
||||||
std::tm current_date = fmt::localtime(tv);
|
|
||||||
|
|
||||||
FILE *fp = fopen(filename.c_str(),"w");
|
FILE *fp = fopen(filename.c_str(),"w");
|
||||||
if (!fp) error->one(FLERR,"Fix ttm could not open output file {}: {}",
|
if (!fp) error->one(FLERR,"Fix ttm could not open output file {}: {}",
|
||||||
filename,utils::getsyserror());
|
filename,utils::getsyserror());
|
||||||
fmt::print(fp,"# DATE: {:%Y-%m-%d} UNITS: {} COMMENT: Electron temperature "
|
fmt::print(fp,"# DATE: {} UNITS: {} COMMENT: Electron temperature "
|
||||||
"{}x{}x{} grid at step {}. Created by fix {}\n", current_date,
|
"{}x{}x{} grid at step {}. Created by fix {}\n", utils::current_date(),
|
||||||
update->unit_style, nxgrid, nygrid, nzgrid, update->ntimestep, style);
|
update->unit_style, nxgrid, nygrid, nzgrid, update->ntimestep, style);
|
||||||
|
|
||||||
int ix,iy,iz;
|
int ix,iy,iz;
|
||||||
|
|||||||
@ -23,18 +23,15 @@
|
|||||||
#include "comm.h"
|
#include "comm.h"
|
||||||
#include "domain.h"
|
#include "domain.h"
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
#include "force.h"
|
|
||||||
#include "gridcomm.h"
|
#include "gridcomm.h"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "neighbor.h"
|
#include "neighbor.h"
|
||||||
#include "random_mars.h"
|
#include "random_mars.h"
|
||||||
#include "tokenizer.h"
|
#include "tokenizer.h"
|
||||||
#include "update.h"
|
#include "update.h"
|
||||||
#include "fmt/chrono.h"
|
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <ctime>
|
|
||||||
|
|
||||||
using namespace LAMMPS_NS;
|
using namespace LAMMPS_NS;
|
||||||
using namespace FixConst;
|
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)
|
void FixTTMGrid::write_electron_temperatures(const std::string &filename)
|
||||||
{
|
{
|
||||||
if (comm->me == 0) {
|
if (comm->me == 0) {
|
||||||
time_t tv = time(nullptr);
|
|
||||||
std::tm current_date = fmt::localtime(tv);
|
|
||||||
|
|
||||||
FPout = fopen(filename.c_str(), "w");
|
FPout = fopen(filename.c_str(), "w");
|
||||||
if (!FPout) error->one(FLERR, "Fix ttm/grid could not open output file");
|
if (!FPout) error->one(FLERR, "Fix ttm/grid could not open output file");
|
||||||
|
|
||||||
fmt::print(FPout,"# DATE: {:%Y-%m-%d} UNITS: {} COMMENT: Electron temperature "
|
fmt::print(FPout,"# DATE: {} UNITS: {} COMMENT: Electron temperature "
|
||||||
"{}x{}x{} grid at step {}. Created by fix {}\n", current_date,
|
"{}x{}x{} grid at step {}. Created by fix {}\n", utils::current_date(),
|
||||||
update->unit_style, nxgrid, nygrid, nzgrid, update->ntimestep, style);
|
update->unit_style, nxgrid, nygrid, nzgrid, update->ntimestep, style);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -33,7 +33,6 @@
|
|||||||
#include "potential_file_reader.h"
|
#include "potential_file_reader.h"
|
||||||
#include "tokenizer.h"
|
#include "tokenizer.h"
|
||||||
#include "update.h"
|
#include "update.h"
|
||||||
#include "fmt/chrono.h"
|
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@ -620,14 +619,11 @@ void FixTTMMod::write_electron_temperatures(const std::string &filename)
|
|||||||
{
|
{
|
||||||
if (comm->me) return;
|
if (comm->me) return;
|
||||||
|
|
||||||
time_t tv = time(nullptr);
|
|
||||||
std::tm current_date = fmt::localtime(tv);
|
|
||||||
|
|
||||||
FILE *fp = fopen(filename.c_str(),"w");
|
FILE *fp = fopen(filename.c_str(),"w");
|
||||||
if (!fp) error->one(FLERR,"Fix ttm/mod could not open output file {}: {}",
|
if (!fp) error->one(FLERR,"Fix ttm/mod could not open output file {}: {}",
|
||||||
filename, utils::getsyserror());
|
filename, utils::getsyserror());
|
||||||
fmt::print(fp,"# DATE: {:%Y-%m-%d} UNITS: {} COMMENT: Electron temperature "
|
fmt::print(fp,"# DATE: {} UNITS: {} COMMENT: Electron temperature "
|
||||||
"{}x{}x{} grid at step {}. Created by fix {}\n", current_date,
|
"{}x{}x{} grid at step {}. Created by fix {}\n", utils::current_date(),
|
||||||
update->unit_style, nxgrid, nygrid, nzgrid, update->ntimestep, style);
|
update->unit_style, nxgrid, nygrid, nzgrid, update->ntimestep, style);
|
||||||
|
|
||||||
int ix,iy,iz;
|
int ix,iy,iz;
|
||||||
|
|||||||
22
src/bond.cpp
22
src/bond.cpp
@ -23,9 +23,6 @@
|
|||||||
#include "neighbor.h"
|
#include "neighbor.h"
|
||||||
#include "suffix.h"
|
#include "suffix.h"
|
||||||
#include "update.h"
|
#include "update.h"
|
||||||
#include "fmt/chrono.h"
|
|
||||||
|
|
||||||
#include <ctime>
|
|
||||||
|
|
||||||
using namespace LAMMPS_NS;
|
using namespace LAMMPS_NS;
|
||||||
|
|
||||||
@ -276,26 +273,21 @@ void Bond::write_file(int narg, char **arg)
|
|||||||
if (utils::file_is_readable(table_file)) {
|
if (utils::file_is_readable(table_file)) {
|
||||||
std::string units = utils::get_potential_units(table_file,"table");
|
std::string units = utils::get_potential_units(table_file,"table");
|
||||||
if (!units.empty() && (units != update->unit_style)) {
|
if (!units.empty() && (units != update->unit_style)) {
|
||||||
error->one(FLERR,"Trying to append to a table file "
|
error->one(FLERR,"Trying to append to a table file with UNITS: {} while units are {}",
|
||||||
"with UNITS: {} while units are {}",
|
|
||||||
units, update->unit_style);
|
units, update->unit_style);
|
||||||
}
|
}
|
||||||
std::string date = utils::get_potential_date(table_file,"table");
|
std::string date = utils::get_potential_date(table_file,"table");
|
||||||
utils::logmesg(lmp,"Appending to table file {} with "
|
utils::logmesg(lmp,"Appending to table file {} with DATE: {}\n", table_file, date);
|
||||||
"DATE: {}\n", table_file, date);
|
|
||||||
fp = fopen(table_file.c_str(),"a");
|
fp = fopen(table_file.c_str(),"a");
|
||||||
} else {
|
} else {
|
||||||
time_t tv = time(nullptr);
|
utils::logmesg(lmp,"Creating table file {} with DATE: {}\n",
|
||||||
std::tm current_date = fmt::localtime(tv);
|
table_file, utils::current_date());
|
||||||
utils::logmesg(lmp,"Creating table file {} with "
|
|
||||||
"DATE: {:%Y-%m-%d}\n", table_file, current_date);
|
|
||||||
fp = fopen(table_file.c_str(),"w");
|
fp = fopen(table_file.c_str(),"w");
|
||||||
if (fp) fmt::print(fp,"# DATE: {:%Y-%m-%d} UNITS: {} Created by bond_write\n",
|
if (fp) fmt::print(fp,"# DATE: {} UNITS: {} Created by bond_write\n",
|
||||||
current_date, update->unit_style);
|
utils::current_date(), update->unit_style);
|
||||||
}
|
}
|
||||||
if (fp == nullptr)
|
if (fp == nullptr)
|
||||||
error->one(FLERR,"Cannot open bond_write file {}: {}",
|
error->one(FLERR,"Cannot open bond_write file {}: {}", arg[4], utils::getsyserror());
|
||||||
arg[4], utils::getsyserror());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize potentials before evaluating bond potential
|
// initialize potentials before evaluating bond potential
|
||||||
|
|||||||
18
src/pair.cpp
18
src/pair.cpp
@ -31,13 +31,11 @@
|
|||||||
#include "neighbor.h"
|
#include "neighbor.h"
|
||||||
#include "suffix.h"
|
#include "suffix.h"
|
||||||
#include "update.h"
|
#include "update.h"
|
||||||
#include "fmt/chrono.h"
|
|
||||||
|
|
||||||
#include <cfloat> // IWYU pragma: keep
|
#include <cfloat> // IWYU pragma: keep
|
||||||
#include <climits> // IWYU pragma: keep
|
#include <climits> // IWYU pragma: keep
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <ctime>
|
|
||||||
|
|
||||||
using namespace LAMMPS_NS;
|
using namespace LAMMPS_NS;
|
||||||
using namespace MathConst;
|
using namespace MathConst;
|
||||||
@ -1815,21 +1813,17 @@ void Pair::write_file(int narg, char **arg)
|
|||||||
units, update->unit_style);
|
units, update->unit_style);
|
||||||
}
|
}
|
||||||
std::string date = utils::get_potential_date(table_file,"table");
|
std::string date = utils::get_potential_date(table_file,"table");
|
||||||
utils::logmesg(lmp,"Appending to table file {} with DATE: {}\n",
|
utils::logmesg(lmp,"Appending to table file {} with DATE: {}\n", table_file, date);
|
||||||
table_file, date);
|
|
||||||
fp = fopen(table_file.c_str(),"a");
|
fp = fopen(table_file.c_str(),"a");
|
||||||
} else {
|
} else {
|
||||||
time_t tv = time(nullptr);
|
utils::logmesg(lmp,"Creating table file {} with DATE: {}\n",
|
||||||
std::tm current_date = fmt::localtime(tv);
|
table_file, utils::current_date());
|
||||||
utils::logmesg(lmp,"Creating table file {} with DATE: {:%Y-%m-%d}\n",
|
|
||||||
table_file, current_date);
|
|
||||||
fp = fopen(table_file.c_str(),"w");
|
fp = fopen(table_file.c_str(),"w");
|
||||||
if (fp) fmt::print(fp,"# DATE: {:%Y-%m-%d} UNITS: {} Created by pair_write\n",
|
if (fp) fmt::print(fp,"# DATE: {} UNITS: {} Created by pair_write\n",
|
||||||
current_date, update->unit_style);
|
utils::current_date(), update->unit_style);
|
||||||
}
|
}
|
||||||
if (fp == nullptr)
|
if (fp == nullptr)
|
||||||
error->one(FLERR,"Cannot open pair_write file {}: {}",
|
error->one(FLERR,"Cannot open pair_write file {}: {}",table_file, utils::getsyserror());
|
||||||
table_file, utils::getsyserror());
|
|
||||||
fprintf(fp,"# Pair potential %s for atom types %d %d: i,r,energy,force\n",
|
fprintf(fp,"# Pair potential %s for atom types %d %d: i,r,energy,force\n",
|
||||||
force->pair_style,itype,jtype);
|
force->pair_style,itype,jtype);
|
||||||
if (style == RLINEAR)
|
if (style == RLINEAR)
|
||||||
|
|||||||
@ -18,6 +18,7 @@
|
|||||||
#include "compute.h"
|
#include "compute.h"
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
#include "fix.h"
|
#include "fix.h"
|
||||||
|
#include "fmt/chrono.h"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "modify.h"
|
#include "modify.h"
|
||||||
#include "text_file_reader.h"
|
#include "text_file_reader.h"
|
||||||
@ -27,6 +28,7 @@
|
|||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
#include <unistd.h> // for readlink
|
#include <unistd.h> // for readlink
|
||||||
@ -1316,7 +1318,21 @@ int utils::date2num(const std::string &date)
|
|||||||
return num;
|
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 utils::binary_search(const double needle, const int n, const double *haystack)
|
||||||
{
|
{
|
||||||
int lo = 0;
|
int lo = 0;
|
||||||
|
|||||||
@ -541,6 +541,14 @@ namespace utils {
|
|||||||
|
|
||||||
int date2num(const std::string &date);
|
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
|
/*! 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.
|
* If the value is smaller than the smallest value in the vector, 0 is returned.
|
||||||
|
|||||||
@ -14,8 +14,11 @@
|
|||||||
#include "lmptype.h"
|
#include "lmptype.h"
|
||||||
#include "pointers.h"
|
#include "pointers.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
#include "tokenizer.h"
|
||||||
|
|
||||||
#include "gmock/gmock.h"
|
#include "gmock/gmock.h"
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -875,6 +878,19 @@ TEST(Utils, date2num)
|
|||||||
ASSERT_EQ(utils::date2num("31December100"), 1001231);
|
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)
|
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,
|
double data[] = {-2.0, -1.8, -1.0, -1.0, -1.0, -0.5, -0.2, 0.0, 0.1, 0.1,
|
||||||
|
|||||||
Reference in New Issue
Block a user