SSAGES  0.8.5
Software Suite for Advanced General Ensemble Simulations
Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
SSAGES::Logger Class Reference

Base class for logging SSAGES data. More...

#include <Logger.h>

Inheritance diagram for SSAGES::Logger:
Inheritance graph
[legend]

Public Member Functions

 Logger (unsigned int frequency, const std::string &filename, const MPI_Comm &world, const MPI_Comm &comm)
 Constructor. More...
 
virtual void PreSimulation (Snapshot *snapshot, const class CVManager &cvmanager) override
 Logger call prior to simulation initiation. More...
 
virtual void PostIntegration (Snapshot *snapshot, const class CVManager &cvmanager) override
 Logger call post integration. More...
 
virtual void PostSimulation (Snapshot *snapshot, const class CVManager &cvmanager) override
 Logger call post simulation. More...
 
void SetCVMask (const std::vector< unsigned int > &mask)
 Sets the collective variable mask. More...
 
void SetAppend (bool append)
 Set append mode. More...
 
virtual ~Logger ()
 Destructor.
 
- Public Member Functions inherited from SSAGES::EventListener
 EventListener (unsigned int frequency)
 Constructor. More...
 
unsigned int GetFrequency () const
 Get frequency of event listener. More...
 
virtual ~EventListener ()
 Destructor.
 

Static Public Member Functions

static LoggerBuild (const Json::Value &json, const MPI_Comm &world, const MPI_Comm &comm, const std::string &path)
 Build a Logger from JSON node. More...
 
- Static Public Member Functions inherited from SSAGES::EventListener
static unsigned int GetWalkerID (const MPI_Comm &world, const MPI_Comm &comm)
 Get walker ID number of specified communicator. More...
 
static unsigned int GetNumWalkers (const MPI_Comm &world, const MPI_Comm &comm)
 Get total number of walkers in the simulation. More...
 
static bool IsMasterRank (const MPI_Comm &comm)
 Check if current processor is master. More...
 

Protected Attributes

mxx::comm world_
 Global MPI communicator.
 
mxx::comm comm_
 Local MPI communicator.
 
std::vector< unsigned int > cvmask_
 Mask which identifies which CVs to log.
 
std::string filename_
 Name of logfile.
 
std::ofstream log_
 Log file stream.
 
bool append_
 Append mode?
 

Detailed Description

Base class for logging SSAGES data.

The base class for logging useful data in SSAGES that is not necessarily written out by methods. Primarily this includes the value(s) of the collective variable(s) on the various walkers over time, the magnitude of the bias if a method supports it, and so on.

Definition at line 41 of file Logger.h.

Constructor & Destructor Documentation

SSAGES::Logger::Logger ( unsigned int  frequency,
const std::string &  filename,
const MPI_Comm &  world,
const MPI_Comm &  comm 
)
inline

Constructor.

Parameters
frequencyFrequency of logging.
filenameFile for logging.
worldGlobal MPI communicator.
commMPI communicator of walker.

Constructs an instance of a logger.

Definition at line 69 of file Logger.h.

69  :
70  EventListener(frequency), world_(world), comm_(comm), cvmask_(), filename_(filename),
71  append_(false)
72  {}
mxx::comm comm_
Local MPI communicator.
Definition: Logger.h:45
mxx::comm world_
Global MPI communicator.
Definition: Logger.h:44
EventListener(unsigned int frequency)
Constructor.
Definition: EventListener.h:45
std::string filename_
Name of logfile.
Definition: Logger.h:51
bool append_
Append mode?
Definition: Logger.h:57
std::vector< unsigned int > cvmask_
Mask which identifies which CVs to log.
Definition: Logger.h:48

Member Function Documentation

Logger * SSAGES::Logger::Build ( const Json::Value &  json,
const MPI_Comm &  world,
const MPI_Comm &  comm,
const std::string &  path 
)
static

Build a Logger from JSON node.

Parameters
jsonJSON Value containing all input information.
worldMPI global communicator.
commMPI local communicator.
pathPath for JSON path specification.
Returns
Pointer to the Logger built. nullptr if an unknown error occurred.
Note
Object lifetime is the caller's responsibility.

Definition at line 75 of file Logger.cpp.

References Json::Requirement::GetErrors(), Json::Requirement::HasErrors(), Json::ObjectRequirement::Parse(), and Json::ObjectRequirement::Validate().

Referenced by SSAGES::ResourceHandler::Build().

79  {
80  ObjectRequirement validator;
81  Value schema;
82  CharReaderBuilder rbuilder;
83  CharReader* reader = rbuilder.newCharReader();
84 
85  reader->parse(JsonSchema::Logger.c_str(),
86  JsonSchema::Logger.c_str() + JsonSchema::Logger.size(),
87  &schema, nullptr);
88  validator.Parse(schema, path);
89 
90  // Validate inputs.
91  validator.Validate(json, path);
92  if(validator.HasErrors())
93  throw BuildException(validator.GetErrors());
94 
95  auto freq = json.get("frequency", 1).asInt();
96  std::string name = "cvlog.dat";
97 
98  bool ismulti = GetNumWalkers(world, comm) > 1;
99  unsigned int wid = GetWalkerID(world, comm);
100 
101  if(json["output_file"].isArray())
102  name = json["output_file"][wid].asString();
103  else if(ismulti)
104  throw std::invalid_argument(path + ": Multi-walker simulations require a separate output file for each.");
105  else
106  name = json["output_file"].asString();
107 
108  auto* l = new Logger(freq, name, world, comm);
109  l->SetAppend(json.get("append", false).asBool());
110 
111  // Load CV mask.
112  std::vector<unsigned int> cvmask;
113  for(auto& cv : json["cvs"])
114  {
115  cvmask.push_back(CVManager::LookupCV(cv, path));
116  }
117  l->SetCVMask(cvmask);
118 
119  return l;
120  }
bool HasErrors()
Check if errors have occured.
Definition: Requirement.h:86
virtual void Parse(Value json, const std::string &path) override
Parse JSON value to generate Requirement(s).
static int LookupCV(const Json::Value &cv, const std::string &path)
Get CV id from map.
Definition: CVManager.h:112
std::vector< std::string > GetErrors()
Get list of error messages.
Definition: Requirement.h:92
static unsigned int GetNumWalkers(const MPI_Comm &world, const MPI_Comm &comm)
Get total number of walkers in the simulation.
Requirements on an object.
Logger(unsigned int frequency, const std::string &filename, const MPI_Comm &world, const MPI_Comm &comm)
Constructor.
Definition: Logger.h:69
static unsigned int GetWalkerID(const MPI_Comm &world, const MPI_Comm &comm)
Get walker ID number of specified communicator.
Definition: EventListener.h:89
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.

Here is the call graph for this function:

Here is the caller graph for this function:

void SSAGES::Logger::PostIntegration ( Snapshot snapshot,
const class CVManager cvmanager 
)
overridevirtual

Logger call post integration.

Parameters
snapshotPointer to the simulation snapshot.
cvmanagerCollective variable manager.

This function will be called after each integration step.

Implements SSAGES::EventListener.

Definition at line 55 of file Logger.cpp.

References SSAGES::CVManager::GetCVs(), and SSAGES::Snapshot::GetIteration().

56  {
57  // Get necessary info.
58  auto cvs = cvmanager.GetCVs(cvmask_);
59  if(IsMasterRank(comm_))
60  {
61  log_.precision(8);
62  log_ << snapshot->GetIteration() << " ";
63 
64  // Print out CV values.
65  for(size_t i = 0; i < cvs.size() - 1; ++i)
66  log_ << cvs[i]->GetValue() << " ";
67  log_ << cvs.back()->GetValue() << std::endl;
68  }
69  }
mxx::comm comm_
Local MPI communicator.
Definition: Logger.h:45
std::vector< unsigned int > cvmask_
Mask which identifies which CVs to log.
Definition: Logger.h:48
static bool IsMasterRank(const MPI_Comm &comm)
Check if current processor is master.
std::ofstream log_
Log file stream.
Definition: Logger.h:54

Here is the call graph for this function:

void SSAGES::Logger::PostSimulation ( Snapshot snapshot,
const class CVManager cvmanager 
)
overridevirtual

Logger call post simulation.

Parameters
snapshotPointer to the simulation snapshot.
cvmanagerCollective variable manager.

This function will be called after the end of the simulation run.

Implements SSAGES::EventListener.

Definition at line 71 of file Logger.cpp.

72  {
73  }
void SSAGES::Logger::PreSimulation ( Snapshot snapshot,
const class CVManager cvmanager 
)
overridevirtual

Logger call prior to simulation initiation.

Parameters
snapshotPointer to the simulation snapshot.
cvmanagerCollective variable manager.

This function will be called before the simulation is started.

Implements SSAGES::EventListener.

Definition at line 34 of file Logger.cpp.

References SSAGES::CVManager::GetCVs().

35  {
36  if(IsMasterRank(comm_))
37  {
38  if(append_)
39  log_.open(filename_.c_str(), std::ofstream::out | std::ofstream::app);
40  else
41  {
42  // Write out header.
43  log_.open(filename_.c_str(), std::ofstream::out);
44  log_ << "#";
45  log_ << "Iteration ";
46 
47  auto cvs = cvmanager.GetCVs(cvmask_);
48  for(size_t i = 0; i < cvs.size() - 1; ++i)
49  log_ << "cv_" + std::to_string(i) << " ";
50  log_ << "cv_" + std::to_string(cvs.size() - 1) << std::endl;
51  }
52  }
53  }
mxx::comm comm_
Local MPI communicator.
Definition: Logger.h:45
std::string filename_
Name of logfile.
Definition: Logger.h:51
bool append_
Append mode?
Definition: Logger.h:57
std::vector< unsigned int > cvmask_
Mask which identifies which CVs to log.
Definition: Logger.h:48
static bool IsMasterRank(const MPI_Comm &comm)
Check if current processor is master.
std::ofstream log_
Log file stream.
Definition: Logger.h:54

Here is the call graph for this function:

void SSAGES::Logger::SetAppend ( bool  append)
inline

Set append mode.

Parameters
appendWhether to enable or disable append mode.

Definition at line 112 of file Logger.h.

113  {
114  append_ = append;
115  }
bool append_
Append mode?
Definition: Logger.h:57
void SSAGES::Logger::SetCVMask ( const std::vector< unsigned int > &  mask)
inline

Sets the collective variable mask.

Sets the collective variable mask.

Parameters
maskVector mask which contains the indices of which CV to include in the container.

This function sets the Snapshot's CV mask.

Definition at line 103 of file Logger.h.

104  {
105  cvmask_ = mask;
106  }
std::vector< unsigned int > cvmask_
Mask which identifies which CVs to log.
Definition: Logger.h:48

The documentation for this class was generated from the following files: