SSAGES  0.8.3
Software Suite for Advanced General Ensemble Simulations
Constraint.cpp
1 
21 #include "Constraint.h"
22 #include "json/json.h"
23 #include "schema.h"
24 #include "../Drivers/DriverException.h"
25 #include "../Validator/ObjectRequirement.h"
26 #include "../Validator/ArrayRequirement.h"
27 #include "COPSS.h"
28 #include "COPSSImage.h"
29 using namespace Json;
30 
31 namespace SSAGES
32 {
33  Constraint* Constraint::BuildConstraint(const Json::Value &json,
34  boost::mpi::communicator& comm)
35  {
36  return BuildConstraint(json, comm, "#/Constraints");
37  }
38 
39  Constraint* Constraint::BuildConstraint(const Json::Value &json,
40  boost::mpi::communicator& comm,
41  const std::string& path)
42  {
43  ObjectRequirement validator;
44  Value schema;
45  Reader reader;
46 
47  Constraint* constraint = nullptr;
48 
49  // Random device for seed generation.
50  // std::random_device rd;
51  // auto maxi = std::numeric_limits<int>::max();
52  // auto seed = json.get("seed", rd() % maxi).asUInt();
53 
54  // Get move type.
55  std::string type = json.get("type", "none").asString();
56 
57  if(type == "COPSS")
58  {
59  reader.parse(JsonSchema::COPSSConstraint, schema);
60  validator.Parse(schema, path);
61 
62  // Validate inputs.
63  validator.Validate(json, path);
64  if(validator.HasErrors())
65  throw BuildException(validator.GetErrors());
66 
67  auto* c = new COPSS(comm, 1);
68 
69  constraint = static_cast<Constraint*>(c);
70  }
71  else if(type == "COPSSImage")
72  {
73  reader.parse(JsonSchema::COPSSImageConstraint, schema);
74  validator.Parse(schema, path);
75 
76  // Validate inputs.
77  validator.Validate(json, path);
78  if(validator.HasErrors())
79  throw BuildException(validator.GetErrors());
80 
81  double einner = json.get("einner",1).asDouble();
82 
83  int ion_type_start = json.get("ion_type_start",1).asInt();
84 
85  std::vector<double> atomTypeRadius;
86  for(auto& atomType : json["atom type radius"])
87  atomTypeRadius.push_back(atomType.asDouble());
88 
89  auto* c = new COPSSImage(comm, 1, einner, ion_type_start, atomTypeRadius);
90 
91  constraint = static_cast<Constraint*>(c);
92 
93  }
94  else
95  {
96  throw BuildException({path + ": Unknown constraint type specified."+type+" is not a valid type!"});
97  }
98 
99  return constraint;
100  }
101 
102  void Constraint::BuildConstraint(const Json::Value &json,
103  ConstraintList &clist,
104  boost::mpi::communicator& comm,
105  const std::string &path)
106  {
107  ArrayRequirement validator;
108  Value schema;
109  Reader reader;
110 
111  reader.parse(JsonSchema::constraints, schema);
112  validator.Parse(schema, path);
113 
114  // Validate high level schema.
115  validator.Validate(json, path);
116  if(validator.HasErrors())
117  throw BuildException(validator.GetErrors());
118 
119  // Loop through CVs.
120  int i = 0;
121  for(auto& m : json)
122  {
123  clist.push_back(BuildConstraint(m, comm, path + "/" + std::to_string(i)));
124  ++i;
125  }
126  }
127 }
bool HasErrors()
Check if errors have occured.
Definition: Requirement.h:86
Array of Requirements.
static void BuildConstraint(const Json::Value &json, ConstraintList &clist, boost::mpi::communicator &comm, const std::string &path)
Build constraint.
Definition: Constraint.cpp:102
Wrapper class for COPSS simulations.
Definition: COPSS.h:32
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.
std::vector< Constraint * > ConstraintList
List of Constraints.
Definition: Constraint.h:38
virtual void Validate(const Value &json, const std::string &path) override
Validate json value.
virtual void Parse(Value json, const std::string &path) override
Parse JSON value and fill array.
Interface for Constraint implementations.
Definition: Constraint.h:41
Image method.
Definition: COPSSImage.h:39
virtual void Validate(const Value &json, const std::string &path) override
Validate JSON value.