SSAGES  0.9.3
Software Suite for Advanced General Ensemble Simulations
Logger.cpp
1 
22 #include <stdexcept>
23 #include "Logger.h"
24 #include "Snapshot.h"
25 #include "CVs/CVManager.h"
26 #include "Validator/ObjectRequirement.h"
27 #include "Drivers/DriverException.h"
28 #include "schema.h"
29 
30 using namespace Json;
31 
32 namespace SSAGES
33 {
34  void Logger::PreSimulation(Snapshot* /* snapshot */, const CVManager& cvmanager)
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  }
54 
55  void Logger::PostIntegration(Snapshot* snapshot, const CVManager& cvmanager)
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  }
70 
71  void Logger::PostSimulation(Snapshot* /* snapshot */, const class CVManager& /* cvmanager */)
72  {
73  }
74 
75  Logger* Logger::Build(const Value& json,
76  const MPI_Comm& world,
77  const MPI_Comm& comm,
78  const std::string& path)
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  }
127 
128 }
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
Exception to be thrown when building the Driver fails.
Collective variable manager.
Definition: CVManager.h:43
CVList GetCVs(const std::vector< unsigned int > &mask=std::vector< unsigned int >()) const
Get CV iterator.
Definition: CVManager.h:81
Base class for logging SSAGES data.
Definition: Logger.h:42
Class containing a snapshot of the current simulation in time.
Definition: Snapshot.h:48
size_t GetIteration() const
Get the current iteration.
Definition: Snapshot.h:105