SSAGES  0.8.3
Software Suite for Advanced General Ensemble Simulations
Method.cpp
1 
20 #include "Method.h"
21 #include "ABF.h"
22 #include "ANN.h"
23 #include "Umbrella.h"
24 #include "BasisFunc.h"
25 #include "ForwardFlux.h"
26 #include "Meta.h"
27 #include "StringMethod.h"
28 #include "schema.h"
29 #include "json/json.h"
30 #include "Drivers/DriverException.h"
31 #include "Validator/ObjectRequirement.h"
32 #include "CVs/CVManager.h"
33 #include <stdexcept>
34 
35 using namespace Json;
36 
37 namespace SSAGES
38 {
39  Method* Method::BuildMethod(const Value& json,
40  const MPI_Comm& world,
41  const MPI_Comm& comm,
42  const std::string& path)
43  {
44  ObjectRequirement validator;
45  Value schema;
46  CharReaderBuilder rbuilder;
47  CharReader* reader = rbuilder.newCharReader();
48 
49  reader->parse(JsonSchema::Method.c_str(),
50  JsonSchema::Method.c_str() + JsonSchema::Method.size(),
51  &schema, NULL);
52  validator.Parse(schema, path);
53  validator.Validate(json, path);
54  if(validator.HasErrors())
55  throw BuildException(validator.GetErrors());
56 
57  Method* method = nullptr;
58 
59  if(json["type"] == "ABF")
60  method = ABF::Build(json, world, comm, path);
61  else if(json["type"] == "ANN")
62  method = ANN::Build(json, world, comm, path);
63  else if(json["type"] == "BFSMethod")
64  method = BFS::Build(json, world, comm, path);
65  else if(json["type"] == "ForwardFlux")
66  method = ForwardFlux::Build(json, world, comm, path);
67  else if(json["type"] == "Metadynamics")
68  method = Meta::Build(json, world, comm, path);
69  else if(json["type"] == "Umbrella")
70  method = Umbrella::Build(json, world, comm, path);
71  else if(json["type"] == "String")
72  method = StringMethod::Build(json, world, comm, path);
73  else
74  throw std::invalid_argument(path + ": Unknown method type specified.");
75 
76  // Load cv mask.
77  std::vector<unsigned int> cvmask;
78  for(auto& v : json["cvs"])
79  {
80  if(v.isString())
81  {
82  auto id = CVManager::LookupCV(v.asString());
83  if(id == -1)
84  throw std::invalid_argument(path + ": CV mask name \"" + v.asString() + "\" does not exist.");
85 
86  cvmask.push_back(CVManager::LookupCV(v.asString()));
87  }
88  else if(v.isIntegral() && v.asInt() >= 0)
89  cvmask.push_back(v.asUInt());
90  else
91  throw std::invalid_argument(path + ": CV mask must contain strings or unsigned integers.");
92 
93  }
94 
95  method->SetCVMask(cvmask);
96  return method;
97  }
98 }
99 
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< unsigned int > &mask)
Sets the collective variable mask.
Definition: Method.h:93
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.