SSAGES  0.1
A MetaDynamics Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
Method.cpp
1 
20 #include "Method.h"
21 #include "ABF.h"
22 #include "Umbrella.h"
23 #include "BasisFunc.h"
24 #include "ForwardFlux.h"
25 #include "Meta.h"
26 #include "StringMethod.h"
27 #include "schema.h"
28 #include "json/json.h"
29 #include "Drivers/DriverException.h"
30 #include "Validator/ObjectRequirement.h"
31 #include "CVs/CVManager.h"
32 #include <stdexcept>
33 
34 using namespace Json;
35 
36 namespace SSAGES
37 {
38  Method* Method::BuildMethod(const Value& json,
39  const MPI_Comm& world,
40  const MPI_Comm& comm,
41  const std::string& path)
42  {
43  ObjectRequirement validator;
44  Value schema;
45  Reader reader;
46 
47  reader.parse(JsonSchema::Method, schema);
48  validator.Parse(schema, path);
49  validator.Validate(json, path);
50  if(validator.HasErrors())
51  throw BuildException(validator.GetErrors());
52 
53  Method* method = nullptr;
54 
55  if(json["type"] == "ABF")
56  method = ABF::Build(json, world, comm, path);
57  else if(json["type"] == "Basis")
58  method = Basis::Build(json, world, comm, path);
59  else if(json["type"] == "ForwardFlux")
60  method = ForwardFlux::Build(json, world, comm, path);
61  else if(json["type"] == "Metadynamics")
62  method = Meta::Build(json, world, comm, path);
63  else if(json["type"] == "Umbrella")
64  method = Umbrella::Build(json, world, comm, path);
65  else if(json["type"] == "String")
66  method = StringMethod::Build(json, world, comm, path);
67  else
68  throw std::invalid_argument(path + ": Unknown method type specified.");
69 
70  // Load cv mask.
71  std::vector<uint> cvmask;
72  for(auto& v : json["cvs"])
73  {
74  if(v.isString())
75  {
76  auto id = CVManager::LookupCV(v.asString());
77  if(id == -1)
78  throw std::invalid_argument(path + ": CV mask name \"" + v.asString() + "\" does not exist.");
79 
80  cvmask.push_back(CVManager::LookupCV(v.asString()));
81  }
82  else if(v.isIntegral() && v.asInt() >= 0)
83  cvmask.push_back(v.asUInt());
84  else
85  throw std::invalid_argument(path + ": CV mask must contain strings or unsigned integers.");
86 
87  }
88 
89  method->SetCVMask(cvmask);
90  return method;
91  }
92 }
93 
bool HasErrors()
Check if errors have occured.
Definition: Requirement.h:86
Interface for Method implementations.
Definition: Method.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
Requirements on an object.
void SetCVMask(const std::vector< uint > &mask)
Sets the collective variable mask.
Definition: Method.h:93
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.