SSAGES  0.9.3
Software Suite for Advanced General Ensemble Simulations
CVManager.h
1 
20 #pragma once
21 
22 #include "CollectiveVariable.h"
23 #include "Drivers/DriverException.h"
24 #include "Validator/Requirement.h"
25 #include <vector>
26 
27 namespace SSAGES
28 {
30 
42  class CVManager
43  {
44  private:
46  std::vector<CollectiveVariable*> cvs_;
47 
49  static std::map<std::string, unsigned int> cvmap_;
50 
51  public:
52  CVManager() = default;
53 
55 
59  {
60  if(std::find(cvs_.begin(), cvs_.end(), cv) == cvs_.end())
61  cvs_.push_back(cv);
62  }
63 
65 
68  void ClearCVs()
69  {
70  for(auto& cv : cvs_)
71  delete cv;
72  cvs_.clear();
73  }
74 
76 
81  CVList GetCVs(const std::vector<unsigned int>& mask = std::vector<unsigned int>()) const
82  {
83  if(mask.empty())
84  return cvs_;
85 
86  // Pack from mask.
87  std::vector<CollectiveVariable*> cvs;
88  for(auto& i : mask)
89  cvs.push_back(cvs_[i]);
90 
91  return cvs;
92  }
93 
95 
101  static void AddCVtoMap(const std::string& name, unsigned int id)
102  {
103  cvmap_[name] = id;
104  }
105 
107 
112  static int LookupCV(const Json::Value& cv, const std::string& path)
113  {
114  int id = -1;
115  if (cv.isString())
116  {
117  auto name = cv.asString();
118  if(cvmap_.find(name) == cvmap_.end())
119  {
120  throw BuildException({path + ": CV mask name \"" + name + "\" does not exist."});
121  }
122  id = cvmap_.at(name);
123  }
124  else if(cv.isIntegral())
125  {
126  id = cv.asInt();
127  if(id < 0 || id >= static_cast<int>(cvmap_.size()))
128  {
129  throw BuildException({path + ": CV mask index of " + std::to_string(id) + " does not exist. " +
130  "Index must be nonnegative and less than " + std::to_string(cvmap_.size()) + "."});
131  }
132  }
133  else
134  {
135  throw BuildException({path + ": CV mask must contain strings or nonnegative integers."});
136  }
137 
138  return id;
139  }
140 
141  ~CVManager()
142  {
143  for(auto& cv : cvs_)
144  delete cv;
145  }
146  };
147 }
Exception to be thrown when building the Driver fails.
Collective variable manager.
Definition: CVManager.h:43
static void AddCVtoMap(const std::string &name, unsigned int id)
Register CV name with map.
Definition: CVManager.h:101
static int LookupCV(const Json::Value &cv, const std::string &path)
Get CV id from map.
Definition: CVManager.h:112
std::vector< CollectiveVariable * > cvs_
List of collective variables.
Definition: CVManager.h:46
CVList GetCVs(const std::vector< unsigned int > &mask=std::vector< unsigned int >()) const
Get CV iterator.
Definition: CVManager.h:81
static std::map< std::string, unsigned int > cvmap_
Map between CV names and ID's.
Definition: CVManager.h:49
void AddCV(CollectiveVariable *cv)
Adds a CV to the CV manager.
Definition: CVManager.h:58
void ClearCVs()
Clears CVs from CV manager.
Definition: CVManager.h:68
Abstract class for a collective variable.
std::vector< CollectiveVariable * > CVList
List of Collective Variables.
Definition: types.h:51