CASM  1.1.0
A Clusters Approach to Statistical Mechanics
ClexParamPack.hh
Go to the documentation of this file.
1 #ifndef CLEXPARAMPACK_HH
2 #define CLEXPARAMPACK_HH
3 #include <cstddef>
4 #include <map>
5 #include <vector>
6 
8 
9 namespace CASM {
13 // ClexParamPack is an abstract data structure for storing and retrieving 2D and
14 // 1D Arrays of real values The data stored in a ClexParamPack is used to
15 // perform calculations within a Clexulator. Values stores within a
16 // ClexParamPack represent independent variables, intermediate computations, and
17 // final evaluated functions. ClexParamPack allows the underlying Scalar value
18 // type to be obscured, in order to implement special functionality, such as
19 // automatic differentiation Clexulators may implement their own ClexParamPack,
20 // or utilitize a pre-defined ClexParamPack specialization
21 class ClexParamPack;
22 
23 // ClexParamKey is used externally to Clexulator and ClexParamPack to access
24 // stored and derived values from ClexParamPack. ClexParamKey holds a pointer to
25 // ClexParamPack_impl::BaseKey, which implements read and write functionality.
26 // Keys are allocated and registered by ClexParamPack.
27 class ClexParamKey;
28 
29 // Struct for templated read/write access of parameter values
30 template <typename Scalar>
31 struct ValAccess {};
32 
33 namespace ClexParamPack_impl {
39 class BaseKey {
40  public:
48  BaseKey(std::string const &_name, bool _standalone,
49  std::vector<Index> const &_offset = {},
50  std::vector<Index> const &_stride = {})
51  : m_offset(_offset),
52  m_stride(_stride),
53  m_identifiers(_offset.size(), 0),
54  m_name(_name),
55  m_standalone(_standalone) {}
56 
58  virtual ~BaseKey() {}
59 
62  std::string const &name() const { return m_name; }
63 
70  template <typename... Args>
71  void set_identifiers(Args const &... args) {
72  _set_identifiers(0, args...);
73  }
74 
76  std::unique_ptr<BaseKey> clone() const {
77  return std::unique_ptr<BaseKey>(_clone());
78  }
79 
82  bool standalone() const { return m_standalone; }
83 
84  protected:
86  Index _l(Index i) const { return m_identifiers[i]; }
87 
89  template <typename T, typename... Args>
90  void _set_identifiers(Index i, T const &_val, Args const &... args) {
91  _set_identifiers(i, _val);
92  _set_identifiers(++i, args...);
93  }
94 
96  void _set_identifiers(Index i, Index _ind) {
97  m_identifiers[i] = m_offset[i] + _ind;
98  }
99 
101  void _set_identifiers(Index i, std::pair<Index, Index> _inds) {
102  _set_identifiers(i, m_stride[i] * _inds.first + _inds.second);
103  }
104 
106  virtual BaseKey *_clone() const = 0;
107 
108  std::vector<Index> m_offset;
109  std::vector<Index> m_stride;
110  std::vector<Index> m_identifiers;
111 
112  private:
113  std::string m_name;
115 };
116 } // namespace ClexParamPack_impl
117 
121  public:
124  return ParamPackMixIn("BasicClexParamPack",
125  {{"ParamPack::DEFAULT", "double"}});
126  }
127 
130  return ParamPackMixIn("DiffClexParamPack",
131  {{"ParamPack::DEFAULT", "double"},
132  {"ParamPack::DIFF", "ParamPack::DiffScalar"}});
133  }
134 
138  ParamPackMixIn(std::string const &_name,
139  std::map<std::string, std::string> const &_specializations)
140  : m_name(_name), m_scalar_specializations(_specializations) {}
141 
143  virtual ~ParamPackMixIn() {}
144 
146  std::string const &name() const { return m_name; }
147 
151  std::map<std::string, std::string> const &scalar_specializations() const {
153  }
154 
156  virtual std::string cpp_includes_string() const {
157  return "#include \"casm/clex/" + name() + ".hh\"\n";
158  }
159 
162  virtual std::string cpp_definitions_string(std::string const &_indent) const {
163  return "";
164  }
165 
167  std::unique_ptr<ParamPackMixIn> clone() const {
168  return std::unique_ptr<ParamPackMixIn>(_clone());
169  }
170 
171  private:
173  virtual ParamPackMixIn *_clone() const { return new ParamPackMixIn(*this); }
174 
175  std::string m_name;
176  std::map<std::string, std::string> m_scalar_specializations;
177 };
178 
182  public:
185 
188  : m_key_ptr(_key.clone()) {}
189 
191  std::string const &name() const { return m_key_ptr->name(); }
192 
195  return m_key_ptr.unique().get();
196  }
197 
207  template <typename... Args>
208  ClexParamKey &operator()(Args const &... args) {
209  m_key_ptr->set_identifiers(args...);
210  return *this;
211  }
212 
213  private:
217 };
218 
231  public:
232  typedef unsigned int size_type;
233 
235  virtual ~ClexParamPack() {}
236 
239  std::map<std::string, ClexParamKey> const &keys() const { return m_keys; }
240 
242  ClexParamKey const &key(std::string const &_name) const {
243  auto it = keys().find(_name);
244  if (it == keys().end()) {
245  std::stringstream ss;
246  for (it = keys().begin(); it != keys().end(); ++it)
247  ss << " " << it->first << "\n";
248  throw std::runtime_error(
249  "In ClexParamPack::key(), ClexParamPack does not contain parameters "
250  "corresponding to name " +
251  _name + ". Options are:\n" + ss.str());
252  }
253  return it->second;
254  }
255 
258  virtual size_type size(ClexParamKey const &_key) const = 0;
259 
262  virtual size_type dim(ClexParamKey const &_key) const = 0;
263 
271  virtual std::string eval_mode(ClexParamKey const &_key) const = 0;
272 
281  virtual Eigen::MatrixXd const &read(ClexParamKey const &_key) const = 0;
282 
285  virtual double const &read(ClexParamKey const &_key, size_type _i) const = 0;
286 
290  virtual double const &read(ClexParamKey const &_key, size_type _i,
291  size_type _j) const = 0;
292 
300  virtual void set_eval_mode(ClexParamKey const &_key,
301  std::string const &_mode) = 0;
302 
304  virtual void write(ClexParamKey const &_key,
305  Eigen::Ref<const Eigen::MatrixXd> const &_val) = 0;
306 
309  virtual void write(ClexParamKey const &_key, size_type _i, double val) = 0;
310 
313  virtual void write(ClexParamKey const &_key, size_type _i, size_type _j,
314  double val) = 0;
315 
318  void pre_eval() {}
319 
322  void post_eval() {}
323 
324  protected:
325  std::map<std::string, ClexParamKey> m_keys;
326 
327  private:
328  // possible implementation:
329  // std::vector<Eigen::MatrixXd> m_data;
330 };
331 
333 } // namespace CASM
334 
335 #endif
Key for indexing clexulator parameters Contains pointer to implementation of the Key.
ClexParamKey & operator()(Args const &... args)
use parentheses operator to set identifiers. Permits syntax like
notstd::cloneable_ptr< ClexParamPack_impl::BaseKey > m_key_ptr
ptr to BaseKey class that hides implementation-specific access attributes
std::string const & name() const
Returns name of key.
ClexParamKey(ClexParamPack_impl::BaseKey const &_key)
Construct ClexParamKey by cloning specialization of BaseKey.
ClexParamPack_impl::BaseKey const * ptr() const
Returns pointer to Key implementation.
ClexParamKey()
Allow default construction.
BaseKey is base class from which all ClexParamKey implementation classes inherit. Hides implementatio...
std::vector< Index > m_identifiers
bool standalone() const
Returns true if key refers directly to managed data, false if it refers to values derived from manage...
std::string const & name() const
Name of this Key, should be descriptive of managed data. Ex.: "disp_var" of "magspin_site_basis".
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.
virtual ~BaseKey()
Abstract class must define virtual destructor.
std::unique_ptr< BaseKey > clone() const
Clone the ClexParamKey.
void _set_identifiers(Index i, std::pair< Index, Index > _inds)
Overload for pair indexed identifier.
void set_identifiers(Args const &... args)
Specify supplemental identifiers, either via linear indices or pair indices. Internally,...
void _set_identifiers(Index i, Index _ind)
Overload for linear indexed identifier.
virtual BaseKey * _clone() const =0
Clone the ClexParamKey.
void _set_identifiers(Index i, T const &_val, Args const &... args)
Recursive unrolling of variadic template to set all identifiers.
Index _l(Index i) const
Returns i'th linear index identifier.
Abstract base class for reading/writing clexulator parameters Parameters are assumed to be stored as,...
virtual std::string eval_mode(ClexParamKey const &_key) const =0
Check evaluation mode for ClexParamPack parameter Choices are at least.
ClexParamKey const & key(std::string const &_name) const
Obtain key for managed data block by name.
virtual void write(ClexParamKey const &_key, size_type _i, double val)=0
Write element to 1D parameter array for parameter specified by.
virtual double const & read(ClexParamKey const &_key, size_type _i, size_type _j) const =0
Returns const reference to element of 2D parameter array for parameter specified by.
virtual void write(ClexParamKey const &_key, size_type _i, size_type _j, double val)=0
Write element to 2D parameter array for parameter specified by.
virtual size_type size(ClexParamKey const &_key) const =0
'N' dimension of parameter array (either '1', or size of unit cell neighborhood) returns number of co...
void post_eval()
May be specialized to perform postprocessing after function evaluation.
virtual void set_eval_mode(ClexParamKey const &_key, std::string const &_mode)=0
Sets evaluation mode for ClexParamPack parameter Choices are at least.
virtual Eigen::MatrixXd const & read(ClexParamKey const &_key) const =0
Returns const reference to parameter array for parameter specified by.
unsigned int size_type
virtual void write(ClexParamKey const &_key, Eigen::Ref< const Eigen::MatrixXd > const &_val)=0
Write parameter array for parameter specified by.
virtual size_type dim(ClexParamKey const &_key) const =0
'm' dimension of parameter array returns number of rows in parameter array
virtual ~ClexParamPack()
Abstract class must define virtual destructor.
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
void pre_eval()
May be specialized to perform preprocessing before function evaluation.
virtual double const & read(ClexParamKey const &_key, size_type _i) const =0
Returns const reference to element of 1D parameter array for parameter specified by.
ParamPackMixIn is interface class to control ClexParamPack portion of Clexulator printing Used primar...
static ParamPackMixIn basic_mix_in()
static factory function for BasicClexParamPack
static ParamPackMixIn diff_mix_in()
static factory function for DiffClexParamPack
std::map< std::string, std::string > m_scalar_specializations
virtual ParamPackMixIn * _clone() const
Clone the ParamPackMixIn (implementation)
ParamPackMixIn(std::string const &_name, std::map< std::string, std::string > const &_specializations)
Constructor Constructed with.
std::map< std::string, std::string > const & scalar_specializations() const
Dictionary of pairs ("EvalMode", "ScalarType") These correspond to the underlying scalar type to be u...
virtual std::string cpp_includes_string() const
returns string with include directives for Clexulator.
virtual ~ParamPackMixIn()
Abstract class must define virtual destructor.
std::unique_ptr< ParamPackMixIn > clone() const
Clone the ParamPackMixIn.
std::string const & name() const
typename of the corresponding ClexParamPack
virtual std::string cpp_definitions_string(std::string const &_indent) const
returns string with c++ definitions to be written before Clexulator.
A 'cloneable_ptr' can be used in place of 'unique_ptr'.
Main CASM namespace.
Definition: APICommand.hh:8
Eigen::MatrixXd MatrixXd
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
std::unique_ptr< T > clone(const T &obj)