CASM  1.1.0
A Clusters Approach to Statistical Mechanics
BasisSet.hh
Go to the documentation of this file.
1 #ifndef BASISSET_HH
2 #define BASISSET_HH
3 
4 #include <iostream>
5 #include <map>
6 #include <memory>
7 #include <set>
8 #include <sstream> //John G 010413
9 
11 #include "casm/basis_set/DoF.hh"
12 #include "casm/misc/CASM_TMP.hh"
13 
14 namespace CASM {
15 
16 class FunctionVisitor;
17 class SymOp;
18 class SymGroup;
19 class SymGroupRep;
20 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 
22 namespace BasisSet_impl {
23 class ArgList : public std::vector<BasisSet const *> {
24  public:
25  using std::vector<BasisSet const *>::push_back;
26 
27  ArgList() {}
28 
29  template <typename BSetResolvable>
30  ArgList(BSetResolvable const &B) {
31  add(B);
32  }
33 
34  void add(BasisSet const &B) { push_back(&B); }
35  void add(BasisSet const *B) { push_back(B); }
36 
37  void add(std::vector<BasisSet> const &BB);
38 
39  void add(std::vector<BasisSet const *> const &BB);
40 
41  void add(Array<BasisSet> const &BB);
42 
43  void add(Array<BasisSet const *> const &BB);
44 };
45 } // namespace BasisSet_impl
46 
47 class BasisSet : private Array<Function *>,
48  public std::enable_shared_from_this<BasisSet> {
49  public:
52  using PolyConstraint = std::pair<SubBasis, Index>;
53 
57 
58  BasisSet(const std::string &name = "", ArgList const &_args = ArgList())
59  : m_name(name),
62  m_max_poly_order(-1) {
63  _set_arguments(_args);
64  }
65 
66  BasisSet(const BasisSet &init_basis);
67  const BasisSet &operator=(const BasisSet &RHS);
68 
69  // Delete Functions upon destruction
70  ~BasisSet();
71 
72  void clear();
73 
74  const std::string &name() const { return m_name; }
75 
76  std::vector<std::shared_ptr<BasisSet> > const &arguments() const {
77  return m_argument;
78  }
79 
81 
84 
87  }
90  }
91  BasisSet poly_quotient_set(const Function *divisor) const;
92 
93  std::shared_ptr<BasisSet> shared_copy() const {
94  return std::make_shared<BasisSet>(*this);
95  }
96 
97  Function const *operator[](Index i) const {
99  }
100 
101  Function const *back() const { return Array<Function *>::back(); }
102 
103  const double &eval_cache(Index i) const {
104  assert(i < size() && i < m_eval_cache.size());
105  return m_eval_cache[i];
106  }
107 
108  const double &deval_cache(Index i) const {
109  assert(i < size() && i < m_deval_cache.size());
110  return m_deval_cache[i];
111  }
112 
113  bool compare(const BasisSet &RHS) const;
114 
115  int dependency_layer() const;
116 
117  void clear_formulae() {
118  for (Index i = 0; i < size(); i++) {
119  at(i)->clear_formula();
120  }
121  }
122 
123  void set_name(const std::string &new_name) { m_name = new_name; }
124 
126 
127  void add_min_poly_constraint(const Array<Index> &expons, Index expon_sum) {
128  m_min_poly_constraints.push_back(PolyConstraint(expons, expon_sum));
129  }
130 
131  void add_max_poly_constraint(const Array<Index> &expons, Index expon_sum) {
132  m_max_poly_constraints.push_back(PolyConstraint(expons, expon_sum));
133  }
134 
135  bool satisfies_exponent_constraints(const Array<Index> &expons) const;
136 
137  bool accept(const FunctionVisitor &visitor);
138 
140  void set_variable_basis(const DoFSet &_dof_set);
141 
142  void set_dof_IDs(const std::vector<Index> &new_IDs);
143 
144  const std::vector<Index> &dof_IDs() const { return m_dof_IDs; }
145 
146  const SubBasis &dof_sub_basis(Index i) const { return m_dof_subbases[i]; }
147 
148  const Array<SubBasis> &dof_sub_bases() const { return m_dof_subbases; }
149 
150  std::vector<std::set<Index> > independent_sub_bases() const;
151 
156  void append(const BasisSet &RHS,
157  std::function<Function *(Function *)> const &transform =
159 
160  void reshape_and_append(const BasisSet &RHS,
161  std::vector<Index> const &compatibility_map);
162 
167  void construct_polynomials_by_order(ArgList const &tsubs, Index order);
168 
169  // expects single basis set of polynomials with the same order( usually just
170  // order 1). makes a basis set of all of the combinations of polynomials with
171  // order spcecified by order.
172  // void construct_polynomials_by_order(ArgList const &tsubs, Index order);
173 
174  void construct_orthonormal_discrete_functions(const DiscreteDoF &allowed_occs,
175  const Eigen::MatrixXd &gram_mat,
176  Index basis_ind,
177  const SymGroup &symgroup);
178 
180  const DiscreteDoF &allowed_occs, const std::vector<double> &occ_probs,
181  Index basis_ind, const SymGroup &symgroup);
182 
183  void construct_invariant_polynomials(ArgList const &tsubs,
184  const SymGroup &head_sym_group,
185  Index order, Index min_dof_order = 1);
186 
187  // void construct_green_lagrange_dot_prods(const ArgList &site_disp_dofs,
188  // BasisSet const *LG_strain_dofs, const Eigen::MatrixXd &ref_clust); void
189  // construct_disp_grad_dot_prods(const ArgList &site_disp_dofs, BasisSet const
190  // *F_strain_dofs, const Eigen::MatrixXd &ref_clust); void
191  // construct_quadratic_ccds(const ArgList &site_disp_dofs, BasisSet const
192  // *strain_dofs, const Eigen::MatrixXd &ref_clust,
193  // const Array<Array<SymOp> > &equiv_map, const
194  // SymGroupRep &permute_group, double sigma);
195 
196  void construct_harmonic_polynomials(const ArgList &tsubs, Index order,
197  Index min_order, bool even_only);
198 
199  void calc_invariant_functions(const SymGroup &head_sym_group);
200 
201  BasisSet calc_normal_basis(const SymGroup &head_sym_group,
202  Eigen::MatrixXd &trans_mat) const;
203 
204  BasisSet transform_copy(const Eigen::MatrixXd &trans_mat) const;
205 
206  BasisSet &apply_sym(const SymOp &op, int dependency_layer = 1);
207 
208  // returns true if Gram_Schmidt leave BasisSet unchanged.
209  bool Gram_Schmidt();
210 
211  bool Gaussian_Elim();
212 
213  void get_symmetry_representation(const SymGroup &head_sym_group) const;
214 
215  // return true if basis_set was already orthogonal to argument
216  bool make_orthogonal_to(Function const *ortho_func);
217  bool make_orthogonal_to(const BasisSet &ortho_basis);
218 
219  jsonParser &to_json(jsonParser &json) const;
220  void from_json(const jsonParser &json);
221 
222  // vv METHODS for run-time BasisSet evaluation vv //
223 
226  void remote_eval_and_add_to(Array<double> &cumulant) const;
227 
230  void remote_deval_and_add_to(Array<double> &cumulant,
231  const DoF::RemoteHandle &dvar) const;
232 
235  template <typename IteratorType>
236  void remote_eval_to(IteratorType result_begin, IteratorType result_end) const;
237 
240  template <typename IteratorType>
241  void remote_deval_to(IteratorType result_begin, IteratorType result_end,
242  const DoF::RemoteHandle &dvar) const;
243 
244  int register_remotes(const std::vector<DoF::RemoteHandle> &remote_handles);
245 
246  private:
248 
249  std::string m_name;
251 
252  std::vector<std::shared_ptr<BasisSet> > m_argument;
253 
254  // When forming tensor products with other basis sets,
255  // what is the minimum and maximum order of polynomial function
256  // that elements of this basis set can be used in
257  // For example, if basis_set1 = {u,v,w} with
258  // min_poly_order 0 and max_poly_order 2
259  // and basis_set2 = {x, y} with min_poly_order 1 and max_poly_order 1
260  // then the product space of basis_set1 and basis_set2 yields
261  // {x, y, x*u, x*v, x*w, y*u, y*v,y*w, x*u*u, x*u*v, x*u*w, ..., y*w*w}
263 
264  std::vector<Index> m_dof_IDs;
267 
268  mutable std::vector<double> m_eval_cache;
269  mutable std::vector<double> m_deval_cache;
270 
271  friend BasisSet direct_sum(BasisSet::ArgList const &_subs);
272 
273  // public push_back is unsafe. Use BasisSet::append() instead,
274  // which calls this private push_back method
275  void push_back(Function *new_func);
276 
277  // non-const access to last function
279 
280  // non-const access to function at Index (i)
282 
283  static Index _new_ID() {
284  static Index ID_COUNT(0);
285  return ID_COUNT++;
286  }
287 
289 
290  void _eval_to_cache() const;
291  void _deval_to_cache(const DoF::RemoteHandle &_dvar) const;
292 
293  Function *_linear_combination(const Eigen::VectorXd &coeffs) const;
294 
295  void _set_arguments(const ArgList &new_args);
296  void _set_arguments(const std::vector<std::shared_ptr<BasisSet> > &new_args) {
297  m_argument = new_args;
298  }
299 
300  bool _update_dof_IDs(const std::vector<Index> before_IDs,
301  const std::vector<Index> &after_IDs);
302 
303  // non-const access to constraints
305  return m_min_poly_constraints;
306  }
308  return m_max_poly_constraints;
309  }
310 };
311 
312 jsonParser &to_json(const BasisSet &bset, jsonParser &json);
313 void from_json(BasisSet &bset, const jsonParser &json);
314 
315 BasisSet operator*(const SymOp &LHS, const BasisSet &RHS);
316 
318 
319 //*******************************************************************************************
322 template <typename IteratorType>
323 void BasisSet::remote_eval_to(IteratorType result_begin,
324  IteratorType result_end) const {
325  for (Index i = 0; i < m_argument.size(); i++) m_argument[i]->_eval_to_cache();
326 
327  Index i(0);
328  for (; result_begin != result_end; ++result_begin) {
329  assert(i < size());
330  if (at(i)) *result_begin = at(i)->cache_eval();
331  i++;
332  }
333 }
334 
335 //*******************************************************************************************
338 template <typename IteratorType>
339 void BasisSet::remote_deval_to(IteratorType result_begin,
340  IteratorType result_end,
341  const DoF::RemoteHandle &dvar) const {
342  for (Index i = 0; i < m_argument.size(); i++)
343  m_argument[i]->_deval_to_cache(dvar);
344 
345  Index i(0);
346  for (; result_begin != result_end; ++result_begin) {
347  assert(i < size());
348  if (at(i)) *result_begin = at(i)->cache_deval(dvar);
349  i++;
350  }
351 }
352 
353 } // namespace CASM
354 #endif
Basic std::vector like container (deprecated)
Definition: Array.hh:45
T & back()
Definition: Array.hh:160
const T & operator[](Index ind) const
Definition: Array.hh:150
Function * & at(Index ind)
Definition: Array.hh:140
Index size() const
Definition: Array.hh:131
ArgList(BSetResolvable const &B)
Definition: BasisSet.hh:30
void add(BasisSet const &B)
Definition: BasisSet.hh:34
void add(BasisSet const *B)
Definition: BasisSet.hh:35
void clear()
Definition: BasisSet.cc:124
void construct_invariant_polynomials(ArgList const &tsubs, const SymGroup &head_sym_group, Index order, Index min_dof_order=1)
Definition: BasisSet.cc:479
void _deval_to_cache(const DoF::RemoteHandle &_dvar) const
Definition: BasisSet.cc:315
void from_json(const jsonParser &json)
void construct_polynomials_by_order(ArgList const &tsubs, Index order)
Definition: BasisSet.cc:454
std::vector< std::shared_ptr< BasisSet > > m_argument
Definition: BasisSet.hh:252
void remote_eval_and_add_to(Array< double > &cumulant) const
Definition: BasisSet.cc:259
Index m_basis_ID
Definition: BasisSet.hh:250
Index m_max_poly_order
Definition: BasisSet.hh:262
SymGroupRepID basis_symrep_ID() const
Definition: BasisSet.hh:80
std::vector< Index > m_dof_IDs
Definition: BasisSet.hh:264
friend BasisSet direct_sum(BasisSet::ArgList const &_subs)
Definition: BasisSet.cc:1324
const Array< SubBasis > & dof_sub_bases() const
Definition: BasisSet.hh:148
Function const * back() const
Definition: BasisSet.hh:101
void set_basis_symrep_ID(SymGroupRepID new_ID)
Definition: BasisSet.hh:125
bool compare(const BasisSet &RHS) const
Definition: BasisSet.cc:284
bool _update_dof_IDs(const std::vector< Index > before_IDs, const std::vector< Index > &after_IDs)
Definition: BasisSet.cc:350
BasisSet calc_normal_basis(const SymGroup &head_sym_group, Eigen::MatrixXd &trans_mat) const
Definition: BasisSet.cc:971
void append(const BasisSet &RHS, std::function< Function *(Function *)> const &transform=CASM_TMP::UnaryIdentity< Function * >())
Append contents of.
Definition: BasisSet.cc:134
bool satisfies_exponent_constraints(const Array< Index > &expons) const
Definition: BasisSet.cc:211
void remote_eval_to(IteratorType result_begin, IteratorType result_end) const
Definition: BasisSet.hh:323
void construct_harmonic_polynomials(const ArgList &tsubs, Index order, Index min_order, bool even_only)
Definition: BasisSet.cc:867
void clear_formulae()
Definition: BasisSet.hh:117
void set_variable_basis(const DoFSet &_dof_set)
Define the basis set to contain only variables (e.g., x,y,z)
Definition: BasisSet.cc:322
void set_dof_IDs(const std::vector< Index > &new_IDs)
Definition: BasisSet.cc:344
std::vector< std::set< Index > > independent_sub_bases() const
Definition: BasisSet.cc:408
void calc_invariant_functions(const SymGroup &head_sym_group)
Definition: BasisSet.cc:947
void _eval_to_cache() const
Definition: BasisSet.cc:309
const Array< PolyConstraint > & max_poly_constraints() const
Definition: BasisSet.hh:88
std::string m_name
Definition: BasisSet.hh:249
void remote_deval_and_add_to(Array< double > &cumulant, const DoF::RemoteHandle &dvar) const
Definition: BasisSet.cc:271
BasisSet(const std::string &name="", ArgList const &_args=ArgList())
Definition: BasisSet.hh:58
void _refresh_ID()
Definition: BasisSet.hh:288
std::pair< SubBasis, Index > PolyConstraint
Definition: BasisSet.hh:52
Array< PolyConstraint > & _min_poly_constraints()
Definition: BasisSet.hh:304
const std::vector< Index > & dof_IDs() const
Definition: BasisSet.hh:144
void _set_arguments(const std::vector< std::shared_ptr< BasisSet > > &new_args)
Definition: BasisSet.hh:296
std::vector< double > m_deval_cache
Definition: BasisSet.hh:269
int dependency_layer() const
Definition: BasisSet.cc:300
jsonParser & to_json(jsonParser &json) const
Definition: BasisSet.cc:1365
void set_name(const std::string &new_name)
Definition: BasisSet.hh:123
Function * _linear_combination(const Eigen::VectorXd &coeffs) const
Definition: BasisSet.cc:1014
bool accept(const FunctionVisitor &visitor)
Definition: BasisSet.cc:234
void push_back(Function *new_func)
Definition: BasisSet.cc:998
void add_min_poly_constraint(const Array< Index > &expons, Index expon_sum)
Definition: BasisSet.hh:127
void construct_orthonormal_discrete_functions(const DiscreteDoF &allowed_occs, const Eigen::MatrixXd &gram_mat, Index basis_ind, const SymGroup &symgroup)
Definition: BasisSet.cc:631
const SubBasis & dof_sub_basis(Index i) const
Definition: BasisSet.hh:146
Array< PolyConstraint > m_min_poly_constraints
Definition: BasisSet.hh:266
Index min_poly_order() const
Definition: BasisSet.hh:83
const double & eval_cache(Index i) const
Definition: BasisSet.hh:103
const std::string & name() const
Definition: BasisSet.hh:74
BasisSet poly_quotient_set(const Function *divisor) const
Definition: BasisSet.cc:198
Array< SubBasis > m_dof_subbases
Definition: BasisSet.hh:265
Function * _back()
Definition: BasisSet.hh:278
void get_symmetry_representation(const SymGroup &head_sym_group) const
Definition: BasisSet.cc:1243
Index m_min_poly_order
Definition: BasisSet.hh:262
Function *& _at(Index i)
Definition: BasisSet.hh:281
BasisSet transform_copy(const Eigen::MatrixXd &trans_mat) const
Definition: BasisSet.cc:1051
Index max_poly_order() const
Definition: BasisSet.hh:82
int register_remotes(const std::vector< DoF::RemoteHandle > &remote_handles)
Definition: BasisSet.cc:440
bool make_orthogonal_to(Function const *ortho_func)
Definition: BasisSet.cc:1279
BasisSet_impl::ArgList ArgList
Definition: BasisSet.hh:50
bool Gaussian_Elim()
Definition: BasisSet.cc:1183
Array< PolyConstraint > & _max_poly_constraints()
Definition: BasisSet.hh:307
SymGroupRepID m_basis_symrep_ID
Definition: BasisSet.hh:247
std::vector< std::shared_ptr< BasisSet > > const & arguments() const
Definition: BasisSet.hh:76
std::shared_ptr< BasisSet > shared_copy() const
Definition: BasisSet.hh:93
void _set_arguments(const ArgList &new_args)
Definition: BasisSet.cc:1098
void reshape_and_append(const BasisSet &RHS, std::vector< Index > const &compatibility_map)
Array< PolyConstraint > m_max_poly_constraints
Definition: BasisSet.hh:266
BasisSet & apply_sym(const SymOp &op, int dependency_layer=1)
Definition: BasisSet.cc:1073
static Index _new_ID()
Definition: BasisSet.hh:283
const double & deval_cache(Index i) const
Definition: BasisSet.hh:108
const BasisSet & operator=(const BasisSet &RHS)
Definition: BasisSet.cc:79
std::vector< double > m_eval_cache
Definition: BasisSet.hh:268
void add_max_poly_constraint(const Array< Index > &expons, Index expon_sum)
Definition: BasisSet.hh:131
const Array< PolyConstraint > & min_poly_constraints() const
Definition: BasisSet.hh:85
bool Gram_Schmidt()
Definition: BasisSet.cc:1133
Function const * operator[](Index i) const
Definition: BasisSet.hh:97
void remote_deval_to(IteratorType result_begin, IteratorType result_end, const DoF::RemoteHandle &dvar) const
Definition: BasisSet.hh:339
virtual double cache_eval() const =0
void clear_formula()
virtual double cache_deval(const DoF::RemoteHandle &dvar) const =0
SymGroup is a collection of symmetry operations that satisfy the group property The symmetry operatio...
Definition: SymGroup.hh:42
Type-safe ID object for communicating and accessing Symmetry representation info.
SymOp is the Coordinate representation of a symmetry operation it keeps fraction (FRAC) and Cartesian...
Definition: SymOp.hh:28
void push_back(const T &toPush)
Definition: Array.hh:431
Main CASM namespace.
Definition: APICommand.hh:8
Eigen::MatrixXd MatrixXd
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
BasisSet direct_sum(BasisSet::ArgList const &_subs)
Definition: BasisSet.cc:1324
BasisSet operator*(const SymOp &LHS, const BasisSet &RHS)
Definition: BasisSet.cc:1408
void from_json(ClexDescription &desc, const jsonParser &json)
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
Eigen::VectorXd VectorXd
pair_type bset
Definition: settings.cc:145
Unary transformation that behaves as Identity (i.e. transform(arg) == arg is true)
Definition: CASM_TMP.hh:85