SSAGES  0.8.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(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  }
54 
55  void Logger::PostIntegration(Snapshot* snapshot, const CVManager& cvmanager)
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  }
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, 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  }
133 
134 }
std::vector< CollectiveVariable * > GetCVs(const std::vector< unsigned int > &mask=std::vector< unsigned int >()) const
Get CV iterator.
Definition: CVManager.h:80
bool HasErrors()
Check if errors have occured.
Definition: Requirement.h:86
Collective variable manager.
Definition: CVManager.h:40
Class containing a snapshot of the current simulation in time.
Definition: Snapshot.h:43
size_t GetIteration() const
Get the current iteration.
Definition: Snapshot.h:103
virtual void Parse(Value json, const std::string &path) override
Parse JSON value to generate Requirement(s).
Exception to be thrown when building the Driver fails.
std::vector< std::string > GetErrors()
Get list of error messages.
Definition: Requirement.h:92
Requirements on an object.
Base class for logging SSAGES data.
Definition: Logger.h:41
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.