SSAGES  0.9.3
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

◆ Logger()

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  {}
EventListener(unsigned int frequency)
Constructor.
Definition: EventListener.h:45
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
bool append_
Append mode?
Definition: Logger.h:57
mxx::comm world_
Global MPI communicator.
Definition: Logger.h:44
std::string filename_
Name of logfile.
Definition: Logger.h:51

Member Function Documentation

◆ Build()

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.

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  {
103  if(json["output_file"].size() != GetNumWalkers(world, comm))
104  {
105  throw BuildException({path + ": Multi-walker simulations require a separate output file for each walker."});
106  }
107  name = json["output_file"][wid].asString();
108  }
109  else if(ismulti)
110  throw std::invalid_argument(path + ": Multi-walker simulations require a separate output file for each.");
111  else
112  name = json["output_file"].asString();
113 
114  auto* l = new Logger(freq, name, world, comm);
115  l->SetAppend(json.get("append", false).asBool());
116 
117  // Load CV mask.
118  std::vector<unsigned int> cvmask;
119  for(auto& cv : json["cvs"])
120  {
121  cvmask.push_back(CVManager::LookupCV(cv, path));
122  }
123  l->SetCVMask(cvmask);
124 
125  return l;
126  }
Requirements on an object.
virtual void Parse(Value json, const std::string &path) override
Parse JSON value to generate Requirement(s).
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.
std::vector< std::string > GetErrors()
Get list of error messages.
Definition: Requirement.h:92
bool HasErrors()
Check if errors have occured.
Definition: Requirement.h:86
static int LookupCV(const Json::Value &cv, const std::string &path)
Get CV id from map.
Definition: CVManager.h:112
static unsigned int GetNumWalkers(const MPI_Comm &world, const MPI_Comm &comm)
Get total number of walkers in the simulation.
static unsigned int GetWalkerID(const MPI_Comm &world, const MPI_Comm &comm)
Get walker ID number of specified communicator.
Definition: EventListener.h:89
Logger(unsigned int frequency, const std::string &filename, const MPI_Comm &world, const MPI_Comm &comm)
Constructor.
Definition: Logger.h:69

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

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ PostIntegration()

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.

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  }
static bool IsMasterRank(const MPI_Comm &comm)
Check if current processor is master.
std::ofstream log_
Log file stream.
Definition: Logger.h:54

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

Here is the call graph for this function:

◆ PostSimulation()

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  }

◆ PreSimulation()

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.

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  }

References SSAGES::CVManager::GetCVs().

Here is the call graph for this function:

◆ SetAppend()

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  }

References append_.

◆ SetCVMask()

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  }

References cvmask_.


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