initial attempt to refactor the citation logging in LAMMPS

this implements the basic features and flow of control.
to be done are the specific texts and the documentation.
This commit is contained in:
Axel Kohlmeyer
2020-12-27 12:14:12 -05:00
parent ab5a1f229e
commit 4912cde2ae
6 changed files with 122 additions and 37 deletions

View File

@ -12,6 +12,7 @@
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
#include "citeme.h" #include "citeme.h"
#include "comm.h"
#include "universe.h" #include "universe.h"
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
@ -21,55 +22,99 @@ static const char cite_header[] =
"following references. See https://lammps.sandia.gov/cite.html\n" "following references. See https://lammps.sandia.gov/cite.html\n"
"for details.\n\n"; "for details.\n\n";
static const char cite_nagline[] = "\nPlease see the log.cite file " static const char cite_nagline[] = "Please see the log.cite file "
"for references relevant to this simulation\n\n"; "for references relevant to this simulation\n\n";
static const char cite_seefile[] = "Please see the citation file "
"for references relevant to this simulation\n\n";
static const char cite_separator[] =
"\nCITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE\n\n";
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
CiteMe::CiteMe(LAMMPS *lmp) : Pointers(lmp) CiteMe::CiteMe(LAMMPS *lmp, int _screen, int _logfile, const char *_file)
: Pointers(lmp)
{ {
fp = nullptr; fp = nullptr;
cs = new citeset(); cs = new citeset();
screen_flag = _screen;
scrbuffer.clear();
logfile_flag = _logfile;
logbuffer.clear();
if (_file && universe->me == 0) {
fp = fopen(_file,"w");
if (fp) {
fputs(cite_header,fp);
fflush(fp);
} else {
utils::logmesg(lmp, "Unable to open citation file '" + std::string(_file)
+ "': " + utils::getsyserror() + "\n");
}
}
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
write out nag-line at the end of the regular output and clean up write out remaining citations at end of the regular output and clean up
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
CiteMe::~CiteMe() CiteMe::~CiteMe()
{ {
if (universe->me || cs->size() == 0) { flush();
delete cs;
return;
}
delete cs; delete cs;
if (fp) { if (fp) fclose(fp);
if (screen) fprintf(screen,cite_nagline);
if (logfile) fprintf(logfile,cite_nagline);
fclose(fp);
}
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
write out and register a citation so it will be written only once process an added citation so it will be shown only once and as requested
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
void CiteMe::add(const char *ref) void CiteMe::add(const char *ref)
{ {
if (universe->me) return; if (comm->me != 0) return;
if (cs->find(ref) != cs->end()) return; if (cs->find(ref) != cs->end()) return;
cs->insert(ref); cs->insert(ref);
if (!fp) { if (fp) {
fp = fopen("log.cite","w"); fputs(ref,fp);
if (!fp) return;
fputs(cite_header,fp);
fflush(fp); fflush(fp);
} }
fputs(ref,fp); if (scrbuffer.empty()) {
fflush(fp); scrbuffer += cite_separator;
if (screen_flag == VERBOSE) scrbuffer += cite_header;
if (screen_flag == TERSE) scrbuffer += cite_nagline;
}
if (logbuffer.empty()) {
logbuffer += cite_separator;
if (logfile_flag == VERBOSE) logbuffer += cite_header;
if (logfile_flag == TERSE) logbuffer += cite_nagline;
}
std::string reference = ref;
std::size_t found = reference.find_first_of("\n");
std::string header = reference.substr(0,found+1);
if (screen_flag == VERBOSE) scrbuffer += reference;
if (screen_flag == TERSE) scrbuffer += header;
if (logfile_flag == VERBOSE) logbuffer += reference;
if (logfile_flag == TERSE) logbuffer += header;
} }
void CiteMe::flush()
{
if (comm->me == 0) {
scrbuffer += cite_separator;
logbuffer += cite_separator;
if (screen) fputs(scrbuffer.c_str(),screen);
if (logfile) fputs(logbuffer.c_str(),logfile);
scrbuffer.clear();
logbuffer.clear();
}
return;
}

View File

@ -21,27 +21,32 @@ namespace LAMMPS_NS {
class CiteMe : protected Pointers { class CiteMe : protected Pointers {
public: public:
CiteMe(class LAMMPS *); CiteMe(class LAMMPS *, int, int, const char *);
virtual ~CiteMe(); virtual ~CiteMe();
void add(const char *); // print out and register publication void add(const char *); // register publication for output
void flush(); // flush buffers to screen and logfile
enum {VERBOSE, TERSE};
private: private:
FILE *fp; // opaque pointer to log.cite file object FILE *fp; // explicit citation file pointer or NULL
int screen_flag; // determine whether verbose or terse output
int logfile_flag; // determine whether verbose or terse output
std::string scrbuffer; // output buffer for screen
std::string logbuffer; // output buffer for logfile
typedef std::set<const char *> citeset; typedef std::set<const char *> citeset;
citeset *cs; // registered set of publications citeset *cs; // registered set of publications
}; };
} }
#endif #endif
/* ERROR/WARNING messages: /* ERROR/WARNING messages:
E: Cannot open log.cite file E: Cannot open citation file
This file is created when you use some LAMMPS features, to indicate This file is created when you use some LAMMPS features, to indicate
what paper you should cite on behalf of those who implemented what paper you should cite on behalf of those who implemented
the feature. Check that you have write privileges into the directory the feature. Check that you have write privileges in the directory
you are running in. you are running in.
*/ */

View File

@ -12,12 +12,14 @@
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
#include "integrate.h" #include "integrate.h"
#include "update.h"
#include "citeme.h"
#include "compute.h"
#include "force.h" #include "force.h"
#include "pair.h"
#include "kspace.h" #include "kspace.h"
#include "modify.h" #include "modify.h"
#include "compute.h" #include "pair.h"
#include "update.h"
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
@ -45,6 +47,7 @@ Integrate::~Integrate()
void Integrate::init() void Integrate::init()
{ {
if (lmp->citeme) lmp->citeme->flush();
update->atimestep = update->ntimestep; update->atimestep = update->ntimestep;
// allow pair and Kspace compute() to be turned off via modify flags // allow pair and Kspace compute() to be turned off via modify flags

View File

@ -171,6 +171,9 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) :
int restart2dump = 0; int restart2dump = 0;
int restartremap = 0; int restartremap = 0;
int citeflag = 1; int citeflag = 1;
int citescreen = CiteMe::TERSE;
int citelogfile = CiteMe::VERBOSE;
char *citefile = nullptr;
int helpflag = 0; int helpflag = 0;
suffix = suffix2 = suffixp = nullptr; suffix = suffix2 = suffixp = nullptr;
@ -190,7 +193,35 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) :
iarg = 1; iarg = 1;
while (iarg < narg) { while (iarg < narg) {
if (strcmp(arg[iarg],"-echo") == 0 || if (strcmp(arg[iarg],"-cite") == 0 ||
strcmp(arg[iarg],"-c") == 0) {
if (iarg+2 > narg)
error->universe_all(FLERR,"Invalid command-line argument");
if (strcmp(arg[iarg+1],"both") == 0) {
citescreen = CiteMe::VERBOSE;
citelogfile = CiteMe::VERBOSE;
citefile = nullptr;
} else if (strcmp(arg[iarg+1],"none") == 0) {
citescreen = CiteMe::TERSE;
citelogfile = CiteMe::TERSE;
citefile = nullptr;
} else if (strcmp(arg[iarg+1],"screen") == 0) {
citescreen = CiteMe::VERBOSE;
citelogfile = CiteMe::TERSE;
citefile = nullptr;
} else if (strcmp(arg[iarg+1],"log") == 0) {
citescreen = CiteMe::TERSE;
citelogfile = CiteMe::VERBOSE;
citefile = nullptr;
} else {
citescreen = CiteMe::TERSE;
citelogfile = CiteMe::TERSE;
citefile = arg[iarg+1];
}
iarg += 2;
} else if (strcmp(arg[iarg],"-echo") == 0 ||
strcmp(arg[iarg],"-e") == 0) { strcmp(arg[iarg],"-e") == 0) {
if (iarg+2 > narg) if (iarg+2 > narg)
error->universe_all(FLERR,"Invalid command-line argument"); error->universe_all(FLERR,"Invalid command-line argument");
@ -605,7 +636,7 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) :
// allocate CiteMe class if enabled // allocate CiteMe class if enabled
if (citeflag) citeme = new CiteMe(this); if (citeflag) citeme = new CiteMe(this,citescreen,citelogfile,citefile);
else citeme = nullptr; else citeme = nullptr;
// allocate input class now that MPI is fully setup // allocate input class now that MPI is fully setup
@ -669,8 +700,8 @@ LAMMPS::~LAMMPS()
{ {
const int me = comm->me; const int me = comm->me;
destroy();
delete citeme; delete citeme;
destroy();
if (num_package) { if (num_package) {
for (int i = 0; i < num_package; i++) { for (int i = 0; i < num_package; i++) {

View File

@ -56,7 +56,6 @@ class LAMMPS {
char *exename; // pointer to argv[0] char *exename; // pointer to argv[0]
char ***packargs; // arguments for cmdline package commands char ***packargs; // arguments for cmdline package commands
int num_package; // number of cmdline package commands int num_package; // number of cmdline package commands
int cite_enable; // 1 if generating log.cite, 0 if disabled
int clientserver; // 0 = neither, 1 = client, 2 = server int clientserver; // 0 = neither, 1 = client, 2 = server
void *cslib; // client/server messaging via CSlib void *cslib; // client/server messaging via CSlib
@ -66,9 +65,9 @@ class LAMMPS {
class AtomKokkos *atomKK; // KOKKOS version of Atom class class AtomKokkos *atomKK; // KOKKOS version of Atom class
class MemoryKokkos *memoryKK; // KOKKOS version of Memory class class MemoryKokkos *memoryKK; // KOKKOS version of Memory class
class Python * python; // Python interface class Python *python; // Python interface
class CiteMe *citeme; // citation info class CiteMe *citeme; // handle citation info
const char *match_style(const char *style, const char *name); const char *match_style(const char *style, const char *name);
static const char * installed_packages[]; static const char * installed_packages[];

View File

@ -13,6 +13,7 @@
#include "minimize.h" #include "minimize.h"
#include "citeme.h"
#include "domain.h" #include "domain.h"
#include "error.h" #include "error.h"
#include "finish.h" #include "finish.h"
@ -46,6 +47,7 @@ void Minimize::command(int narg, char **arg)
if (update->etol < 0.0 || update->ftol < 0.0) if (update->etol < 0.0 || update->ftol < 0.0)
error->all(FLERR,"Illegal minimize command"); error->all(FLERR,"Illegal minimize command");
if (lmp->citeme) lmp->citeme->flush();
update->whichflag = 2; update->whichflag = 2;
update->beginstep = update->firststep = update->ntimestep; update->beginstep = update->firststep = update->ntimestep;
update->endstep = update->laststep = update->firststep + update->nsteps; update->endstep = update->laststep = update->firststep + update->nsteps;