SSAGES  0.8.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.
 
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...
 

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.
Todo:
wid should be obtainable from somewhere.

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:44
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

◆ 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.

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, NULL);
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  //TODO walker id should be obtainable in method as
99  // opposed to calculated like this.
100  bool ismulti = mxx::comm(world).size() > mxx::comm(comm).size();
101  unsigned int wid = mxx::comm(world).rank()/mxx::comm(comm).size();
102  if(json["output_file"].isArray())
103  name = json["output_file"][wid].asString();
104  else if(ismulti)
105  throw std::invalid_argument(path + ": Multi-walker simulations require a separate output file for each.");
106  else
107  name = json["output_file"].asString();
108 
109  auto* l = new Logger(freq, name, world, comm);
110  l->SetAppend(json.get("append", false).asBool());
111 
112  // Load cv mask.
113  std::vector<unsigned int> cvmask;
114  for(auto& v : json["cvs"])
115  {
116  if(v.isString())
117  {
118  auto id = CVManager::LookupCV(v.asString());
119  if(id == -1)
120  throw std::invalid_argument(path + ": CV mask name \"" + v.asString() + "\" does not exist.");
121 
122  cvmask.push_back(CVManager::LookupCV(v.asString()));
123  }
124  else if(v.isIntegral() && v.asInt() >= 0)
125  cvmask.push_back(v.asUInt());
126  else
127  throw std::invalid_argument(path + ": CV mask must contain strings or unsigned integers.");
128  }
129  l->SetCVMask(cvmask);
130 
131  return l;
132  }
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).
std::vector< std::string > GetErrors()
Get list of error messages.
Definition: Requirement.h:92
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 int LookupCV(const std::string &name)
Get CV id from map.
Definition: CVManager.h:110
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:

◆ 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.

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

56  {
57  // Get necessary info.
58  auto cvs = cvmanager.GetCVs(cvmask_);
59  if(comm_.rank() ==0)
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
std::ofstream log_
Log file stream.
Definition: Logger.h:54
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.

References SSAGES::CVManager::GetCVs().

35  {
36  if(comm_.rank() == 0)
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
std::ofstream log_
Log file stream.
Definition: Logger.h:54
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 111 of file Logger.h.

112  {
113  append_ = append;
114  }
bool append_
Append mode?
Definition: Logger.h:57

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