CASM  1.1.0
A Clusters Approach to Statistical Mechanics
BasicClexParamPack.hh
Go to the documentation of this file.
1 #ifndef BASICCLEXPARAMPACK_HH
2 #define BASICCLEXPARAMPACK_HH
3 #include <cstddef>
4 #include <iostream>
5 #include <sstream>
6 #include <string>
7 
12 
13 namespace CASM {
18  public:
20 
21  BasicClexParamKey(std::string const &_name = "", bool _standalone = false,
22  size_type _ind = -1)
23  : ClexParamPack_impl::BaseKey(_name, _standalone), m_index(_ind) {}
24 
26 
27  size_type index() const { return m_index; }
28 
29  protected:
30  ClexParamPack_impl::BaseKey *_clone() const override {
31  return new BasicClexParamKey(*this);
32  }
33 
35 };
36 
40  public:
41  enum class EvalMode { DEFAULT, READ, DYNAM };
42 
43  static const EvalMode DEFAULT;
44  static const EvalMode READ;
45  static const EvalMode DYNAM;
46 
48  using DoubleReference = Eigen::MatrixXd::CoeffReturnType;
49 
50  template <typename Scalar>
52 
53  template <typename Scalar>
54  friend class ValAccess;
55 
56  size_type size(ClexParamKey const &_key) const override {
57  return size(*static_cast<Key const *>(_key.ptr()));
58  }
59 
60  size_type size(Key const &_key) const { return m_data[_key.index()].cols(); }
61 
62  size_type dim(ClexParamKey const &_key) const override {
63  return dim(*static_cast<Key const *>(_key.ptr()));
64  }
65 
66  size_type dim(Key const &_key) const { return m_data[_key.index()].rows(); }
67 
68  std::string eval_mode(ClexParamKey const &_key) const override {
69  return to_string(eval_mode(*static_cast<Key const *>(_key.ptr())));
70  }
71 
72  EvalMode eval_mode(Key const &_key) const { return m_eval[_key.index()]; }
73 
74  Eigen::MatrixXd const &read(ClexParamKey const &_key) const override {
75  return read(*static_cast<Key const *>(_key.ptr()));
76  }
77 
78  Eigen::MatrixXd const &read(Key const &_key) const {
79  return m_data[_key.index()];
80  }
81 
82  double const &read(ClexParamKey const &_key, size_type _ind) const override {
83  return read(*static_cast<Key const *>(_key.ptr()), _ind);
84  }
85 
86  double const &read(Key const &_key, size_type _ind) const {
87  return m_data[_key.index()](_ind, 0);
88  }
89 
90  double const &read(ClexParamKey const &_key, size_type _i,
91  size_type _j) const override {
92  return read(*static_cast<Key const *>(_key.ptr()), _i, _j);
93  }
94 
95  double const &read(Key const &_key, size_type _i, size_type _j) const {
96  return m_data[_key.index()](_i, _j);
97  }
98 
99  void set_eval_mode(ClexParamKey const &_key,
100  std::string const &_mode) override {
101  set_eval_mode(*static_cast<Key const *>(_key.ptr()),
102  from_string<EvalMode>(_mode));
103  }
104 
105  void set_eval_mode(Key const &_key, EvalMode _mode) {
106  m_eval[_key.index()] = _mode;
107  }
108 
109  void write(ClexParamKey const &_key,
110  Eigen::Ref<const Eigen::MatrixXd> const &_val) override {
111  write(*static_cast<Key const *>(_key.ptr()), _val);
112  }
113 
114  void write(Key const &_key, Eigen::Ref<const Eigen::MatrixXd> const &_val) {
115  m_data[_key.index()] = _val;
116  }
117 
118  void write(ClexParamKey const &_key, size_type _i, double _val) override {
119  write(*static_cast<Key const *>(_key.ptr()), _i, _val);
120  }
121 
122  void write(Key const &_key, size_type _i, double _val) {
123  m_data[_key.index()](_i, 0) = _val;
124  }
125 
126  void write(ClexParamKey const &_key, size_type _i, size_type _j,
127  double _val) override {
128  write(*static_cast<Key const *>(_key.ptr()), _i, _j, _val);
129  }
130 
131  void write(Key const &_key, size_type _i, size_type _j, double _val) {
132  m_data[_key.index()](_i, _j) = _val;
133  }
134 
135  Key allocate(std::string const &_keyname, Index _rows, Index _cols,
136  bool _independent) {
137  auto it = keys().find(_keyname);
138  if (it != keys().end())
139  throw std::runtime_error(
140  "Naming collision in BasicClexParamPack::allocate(), ClexParamPack "
141  "already managing parameter allocation corresponding to name " +
142  _keyname + ".");
143 
144  Key protokey(_keyname, true, m_data.size());
145 
146  m_data.push_back(Eigen::MatrixXd::Zero(_rows, _cols));
147 
148  m_eval.push_back(EvalMode::DEFAULT);
149 
150  m_keys[_keyname] = protokey;
151  return protokey;
152  }
153 
154  private:
155  std::vector<Eigen::MatrixXd> m_data;
156  std::vector<EvalMode> m_eval;
157 };
158 
159 template <>
160 struct ValAccess<double> {
162 
163  static double const &get(BasicClexParamPack const &_pack,
164  BasicClexParamKey const &_key, size_type i) {
165  return _pack.m_data[_key.index()](i, 0);
166  }
167 
168  static double const &get(BasicClexParamPack const &_pack,
169  BasicClexParamKey const &_key, size_type i,
170  size_type j) {
171  return _pack.m_data[_key.index()](i, j);
172  }
173 
174  static void set(BasicClexParamPack &_pack, BasicClexParamKey const &_key,
175  Eigen::Ref<const Eigen::MatrixXd> const &_val) {
176  _pack.m_data[_key.index()] = _val;
177  }
178 
179  template <typename Scalar2>
180  static void set(BasicClexParamPack &_pack, BasicClexParamKey const &_key,
181  size_type i, Scalar2 const &_val) {
182  _pack.m_data[_key.index()](i, 0) = _val;
183  }
184 
185  template <typename Scalar2>
186  static void set(BasicClexParamPack &_pack, BasicClexParamKey const &_key,
187  size_type i, size_type j, Scalar2 const &_val) {
188  _pack.m_data[_key.index()](i, j) = _val;
189  }
190 };
191 
192 template <>
194  static const std::string name;
195 
196  static const std::multimap<BasicClexParamPack::EvalMode,
197  std::vector<std::string> >
199 };
200 
201 std::ostream &operator<<(std::ostream &sout,
202  const BasicClexParamPack::EvalMode &val) {
203  sout << to_string<BasicClexParamPack::EvalMode>(val);
204  return sout;
205 }
206 
207 std::istream &operator>>(std::istream &sin, BasicClexParamPack::EvalMode &val) {
208  std::string s;
209  sin >> s;
210  val = from_string<BasicClexParamPack::EvalMode>(s);
211  return sin;
212 }
213 
214 const std::string traits<BasicClexParamPack::EvalMode>::name = "clex_eval_mode";
215 
216 const std::multimap<BasicClexParamPack::EvalMode, std::vector<std::string> >
219  {"Default", "DEFAULT", "default"}},
220  {BasicClexParamPack::EvalMode::READ, {"Read", "READ", "read"}},
222  {"Dynamic", "Dynam", "DYNAMIC", "DYNAM", "dynamic", "dynam"}}};
223 
230 
232 } // namespace CASM
233 #endif
std::set< std::string > & s
ClexParamPack::size_type size_type
ClexParamPack_impl::BaseKey * _clone() const override
Clone the ClexParamKey.
BasicClexParamKey(std::string const &_name="", bool _standalone=false, size_type _ind=-1)
Abstract base class for reading/writing clexulator parameters Parameters are assume be naturally repr...
double const & read(Key const &_key, size_type _ind) const
void write(ClexParamKey const &_key, size_type _i, double _val) override
Write element to 1D parameter array for parameter specified by.
EvalMode eval_mode(Key const &_key) const
Eigen::MatrixXd::CoeffReturnType DoubleReference
std::vector< EvalMode > m_eval
void set_eval_mode(Key const &_key, EvalMode _mode)
double const & read(ClexParamKey const &_key, size_type _ind) const override
Returns const reference to element of 1D parameter array for parameter specified by.
static const EvalMode DYNAM
double const & read(ClexParamKey const &_key, size_type _i, size_type _j) const override
Returns const reference to element of 2D parameter array for parameter specified by.
Eigen::MatrixXd const & read(Key const &_key) const
void write(Key const &_key, size_type _i, double _val)
Eigen::MatrixXd const & read(ClexParamKey const &_key) const override
Returns const reference to parameter array for parameter specified by.
void write(Key const &_key, size_type _i, size_type _j, double _val)
double const & read(Key const &_key, size_type _i, size_type _j) const
size_type dim(Key const &_key) const
static const EvalMode READ
Key allocate(std::string const &_keyname, Index _rows, Index _cols, bool _independent)
size_type dim(ClexParamKey const &_key) const override
'm' dimension of parameter array returns number of rows in parameter array
std::vector< Eigen::MatrixXd > m_data
static const EvalMode DEFAULT
size_type size(Key const &_key) const
std::string eval_mode(ClexParamKey const &_key) const override
Check evaluation mode for ClexParamPack parameter Choices are at least.
void write(ClexParamKey const &_key, size_type _i, size_type _j, double _val) override
Write element to 2D parameter array for parameter specified by.
void write(Key const &_key, Eigen::Ref< const Eigen::MatrixXd > const &_val)
void set_eval_mode(ClexParamKey const &_key, std::string const &_mode) override
Sets evaluation mode for ClexParamPack parameter Choices are at least.
void write(ClexParamKey const &_key, Eigen::Ref< const Eigen::MatrixXd > const &_val) override
Write parameter array for parameter specified by.
size_type size(ClexParamKey const &_key) const override
'N' dimension of parameter array (either '1', or size of unit cell neighborhood) returns number of co...
Key for indexing clexulator parameters Contains pointer to implementation of the Key.
ClexParamPack_impl::BaseKey const * ptr() const
Returns pointer to Key implementation.
BaseKey is base class from which all ClexParamKey implementation classes inherit. Hides implementatio...
BaseKey(std::string const &_name, bool _standalone, std::vector< Index > const &_offset={}, std::vector< Index > const &_stride={})
Must be constructed with at least a name.
Abstract base class for reading/writing clexulator parameters Parameters are assumed to be stored as,...
unsigned int size_type
std::map< std::string, ClexParamKey > const & keys() const
Obtain registry of all keys for data blocks managed by this ClexParamPack.
std::map< std::string, ClexParamKey > m_keys
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: io_traits.hh:172
Main CASM namespace.
Definition: APICommand.hh:8
Eigen::MatrixXd MatrixXd
std::ostream & operator<<(std::ostream &_stream, const FormattedPrintable &_formatted)
std::istream & operator>>(std::istream &_in, std::vector< T > &vec)
Definition: stream_io.hh:10
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
static void set(BasicClexParamPack &_pack, BasicClexParamKey const &_key, size_type i, Scalar2 const &_val)
static double const & get(BasicClexParamPack const &_pack, BasicClexParamKey const &_key, size_type i, size_type j)
static void set(BasicClexParamPack &_pack, BasicClexParamKey const &_key, Eigen::Ref< const Eigen::MatrixXd > const &_val)
BasicClexParamPack::size_type size_type
static void set(BasicClexParamPack &_pack, BasicClexParamKey const &_key, size_type i, size_type j, Scalar2 const &_val)
static double const & get(BasicClexParamPack const &_pack, BasicClexParamKey const &_key, size_type i)
static const std::multimap< BasicClexParamPack::EvalMode, std::vector< std::string > > strval