SSAGES  0.1
A MetaDynamics Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
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  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  }
130 
131 }
bool HasErrors()
Check if errors have occured.
Definition: Requirement.h:86
Collective variable manager.
Definition: CVManager.h:40
int GetIteration() const
Get the current iteration.
Definition: Snapshot.h:103
Class containing a snapshot of the current simulation in time.
Definition: Snapshot.h:43
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
std::vector< CollectiveVariable * > GetCVs(const std::vector< uint > &mask=std::vector< uint >()) const
Get CV iterator.
Definition: CVManager.h:80
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.