diff --git a/src/citeme.cpp b/src/citeme.cpp index 4b1b627298..5082f058b5 100644 --- a/src/citeme.cpp +++ b/src/citeme.cpp @@ -12,6 +12,7 @@ ------------------------------------------------------------------------- */ #include "citeme.h" +#include "comm.h" #include "universe.h" using namespace LAMMPS_NS; @@ -21,55 +22,99 @@ static const char cite_header[] = "following references. See https://lammps.sandia.gov/cite.html\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"; +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; 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() { - if (universe->me || cs->size() == 0) { - delete cs; - return; - } - + flush(); delete cs; - if (fp) { - if (screen) fprintf(screen,cite_nagline); - if (logfile) fprintf(logfile,cite_nagline); - - fclose(fp); - } + if (fp) 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) { - if (universe->me) return; + if (comm->me != 0) return; if (cs->find(ref) != cs->end()) return; cs->insert(ref); - if (!fp) { - fp = fopen("log.cite","w"); - if (!fp) return; - fputs(cite_header,fp); + if (fp) { + fputs(ref,fp); fflush(fp); } - fputs(ref,fp); - fflush(fp); + if (scrbuffer.empty()) { + 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; +} + diff --git a/src/citeme.h b/src/citeme.h index c383ec2227..9a3d30e8e8 100644 --- a/src/citeme.h +++ b/src/citeme.h @@ -21,27 +21,32 @@ namespace LAMMPS_NS { class CiteMe : protected Pointers { public: - CiteMe(class LAMMPS *); + CiteMe(class LAMMPS *, int, int, const char *); 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: - 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 citeset; citeset *cs; // registered set of publications }; - } #endif /* 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 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. */ diff --git a/src/integrate.cpp b/src/integrate.cpp index 5c700527a1..f866840f1c 100644 --- a/src/integrate.cpp +++ b/src/integrate.cpp @@ -12,12 +12,14 @@ ------------------------------------------------------------------------- */ #include "integrate.h" -#include "update.h" + +#include "citeme.h" +#include "compute.h" #include "force.h" -#include "pair.h" #include "kspace.h" #include "modify.h" -#include "compute.h" +#include "pair.h" +#include "update.h" using namespace LAMMPS_NS; @@ -45,6 +47,7 @@ Integrate::~Integrate() void Integrate::init() { + if (lmp->citeme) lmp->citeme->flush(); update->atimestep = update->ntimestep; // allow pair and Kspace compute() to be turned off via modify flags diff --git a/src/lammps.cpp b/src/lammps.cpp index 69baec5557..b882c76032 100644 --- a/src/lammps.cpp +++ b/src/lammps.cpp @@ -171,6 +171,9 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) : int restart2dump = 0; int restartremap = 0; int citeflag = 1; + int citescreen = CiteMe::TERSE; + int citelogfile = CiteMe::VERBOSE; + char *citefile = nullptr; int helpflag = 0; suffix = suffix2 = suffixp = nullptr; @@ -190,7 +193,35 @@ LAMMPS::LAMMPS(int narg, char **arg, MPI_Comm communicator) : iarg = 1; 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) { if (iarg+2 > narg) 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 - if (citeflag) citeme = new CiteMe(this); + if (citeflag) citeme = new CiteMe(this,citescreen,citelogfile,citefile); else citeme = nullptr; // allocate input class now that MPI is fully setup @@ -669,8 +700,8 @@ LAMMPS::~LAMMPS() { const int me = comm->me; - destroy(); delete citeme; + destroy(); if (num_package) { for (int i = 0; i < num_package; i++) { diff --git a/src/lammps.h b/src/lammps.h index 49d55d4e37..553db6597e 100644 --- a/src/lammps.h +++ b/src/lammps.h @@ -56,7 +56,6 @@ class LAMMPS { char *exename; // pointer to argv[0] char ***packargs; // arguments for 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 void *cslib; // client/server messaging via CSlib @@ -66,9 +65,9 @@ class LAMMPS { class AtomKokkos *atomKK; // KOKKOS version of Atom 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); static const char * installed_packages[]; diff --git a/src/minimize.cpp b/src/minimize.cpp index a909afdaa5..8e55d6e0ea 100644 --- a/src/minimize.cpp +++ b/src/minimize.cpp @@ -13,6 +13,7 @@ #include "minimize.h" +#include "citeme.h" #include "domain.h" #include "error.h" #include "finish.h" @@ -46,6 +47,7 @@ void Minimize::command(int narg, char **arg) if (update->etol < 0.0 || update->ftol < 0.0) error->all(FLERR,"Illegal minimize command"); + if (lmp->citeme) lmp->citeme->flush(); update->whichflag = 2; update->beginstep = update->firststep = update->ntimestep; update->endstep = update->laststep = update->firststep + update->nsteps;