SSAGES  0.1
A MetaDynamics Package
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 (uint 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< uint > &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 (uint frequency)
 Constructor. More...
 
uint 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< uint > 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 ( uint  frequency,
const std::string &  filename,
const MPI_Comm &  world,
const MPI_Comm &  comm 
)
inline

Constructor.

Parameters
frequencyFrequency of logging.
worldGlobal MPI communicator.
commMPI communicator of walker.
Todo:
wid should be obtainable from somewhere.

Definition at line 68 of file Logger.h.

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

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  Reader reader;
83 
84  reader.parse(JsonSchema::Logger, schema);
85  validator.Parse(schema, path);
86 
87  // Validate inputs.
88  validator.Validate(json, path);
89  if(validator.HasErrors())
90  throw BuildException(validator.GetErrors());
91 
92  auto freq = json.get("frequency", 1).asInt();
93  std::string name = "cvlog.dat";
94 
95  //TODO walker id should be obtainable in method as
96  // opposed to calculated like this.
97  bool ismulti = mxx::comm(world).size() > mxx::comm(comm).size();
98  uint wid = mxx::comm(world).rank()/mxx::comm(comm).size();
99  if(json["output_file"].isArray())
100  name = json["output_file"][wid].asString();
101  else if(ismulti)
102  throw std::invalid_argument(path + ": Multi-walker simulations require a separate output file for each.");
103  else
104  name = json["output_file"].asString();
105 
106  auto* l = new Logger(freq, name, world, comm);
107  l->SetAppend(json.get("append", false).asBool());
108 
109  // Load cv mask.
110  std::vector<uint> cvmask;
111  for(auto& v : json["cvs"])
112  {
113  if(v.isString())
114  {
115  auto id = CVManager::LookupCV(v.asString());
116  if(id == -1)
117  throw std::invalid_argument(path + ": CV mask name \"" + v.asString() + "\" does not exist.");
118 
119  cvmask.push_back(CVManager::LookupCV(v.asString()));
120  }
121  else if(v.isIntegral() && v.asInt() >= 0)
122  cvmask.push_back(v.asUInt());
123  else
124  throw std::invalid_argument(path + ": CV mask must contain strings or unsigned integers.");
125  }
126  l->SetCVMask(cvmask);
127 
128  return l;
129  }
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(uint frequency, const std::string &filename, const MPI_Comm &world, const MPI_Comm &comm)
Constructor.
Definition: Logger.h:68
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:

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

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(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< uint > 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:

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

Set append mode.

Parameters
appendWhether to enable or disable append mode.

Definition at line 110 of file Logger.h.

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

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