CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
DoF.hh
Go to the documentation of this file.
1 #ifndef DOF_HH
2 #define DOF_HH
3 #include<vector>
4 #include<boost/algorithm/string.hpp>
7 #include "casm/misc/CASM_math.hh"
9 
10 //#include "../basis_set/MathExpressions.cc"
11 
12 namespace CASM {
13  class Molecule;
14  class Lattice;
15  class jsonParser;
16  //*******************************************************************************************
17 
18  // Since we will have different types of DoFs, we make DoF a virtual, or abstract class.
19  // The specific implementation will depend on the type of site variable.
20  // Not sure if it still makes sense to try to treat continuous and discrete DoFs equivalently
21  class DoF {
22  protected:
23  std::string m_type_name;
28  bool m_ID_lock;
29  public:
30  class RemoteHandle {
31  double const *const m_d_ptr;
32  int const *const m_i_ptr;
33  public:
34  RemoteHandle(const double &d) : m_d_ptr(&d), m_i_ptr(nullptr) {}
35  RemoteHandle(const int &i) : m_d_ptr(nullptr), m_i_ptr(&i) {}
36  const int *i_ptr() const {
37  return m_i_ptr;
38  }
39  const double *d_ptr() const {
40  return m_d_ptr;
41  }
42  };
43 
44  public:
45  DoF(const std::string &_name = "", Index _ID = -1) : m_type_name(_name), m_dof_ID(_ID), m_ID_lock(false) {}
46  virtual ~DoF() {} //= 0;
47  //virtual double get_val() const = 0; // get the current value
48 
49  const std::string &type_name() const {
50  return m_type_name;
51  }
52 
53  std::string type_name_prefix() const {
54  std::vector<std::string> split_string;
55  boost::split(split_string, type_name(), boost::is_any_of("_"), boost::token_compress_on);
56  if(split_string.size())
57  return split_string[0];
58  else
59  return std::string();
60  }
61 
62  std::string type_name_suffix() const {
63  std::vector<std::string> split_string;
64  boost::split(split_string, type_name(), boost::is_any_of("_"), boost::token_compress_on);
65  if(split_string.size())
66  return split_string.back();
67  else
68  return std::string();
69  }
70 
71  Index ID() const {
72  return m_dof_ID;
73  }
74 
75  void set_ID(Index new_ID) {
76  if(m_ID_lock)
77  return;
78  //std::cout << "DoF " << this << " ID updated from " << ID() << " to " << new_ID << '\n';
79  m_dof_ID = new_ID;
80  }
81 
82  bool is_locked() const {
83  return m_ID_lock;
84  }
85 
86  void lock_ID() {
87  m_ID_lock = true;
88  }
89 
90  void unlock_ID() {
91  m_ID_lock = false;
92  }
93 
94  virtual jsonParser &to_json(jsonParser &json) const = 0;
95 
96  };
97 
98 
99  jsonParser &to_json(const DoF *dof, jsonParser &json);
100 
104  void from_json(DoF *dof, const jsonParser &json, const Lattice &lat);
105 
106 
107  //*******************************************************************************************
108 
109  class DiscreteDoF : public DoF {
110  protected:
113 
115  const int *m_remote_state;
116 
118 
119  public:
120  DiscreteDoF(): DoF(""), m_current_state(0), m_remote_state(nullptr), m_sym_rep_ID(SymGroupRepID::identity(0)) {}
121 
122  DiscreteDoF(const std::string &_name, int _current_state = 0, SymGroupRepID _id = SymGroupRepID::identity(0)):
123  DoF(_name),
124  m_current_state(_current_state),
125  m_remote_state(nullptr),
126  m_sym_rep_ID(_id) {
127 
128  }
129 
130  virtual ~DiscreteDoF() {}
131 
133  return m_sym_rep_ID;
134  }
135 
137  m_sym_rep_ID = _id;
138  }
139 
140  bool is_specified() const {
141  return valid_index(m_current_state);
142  }
143 
144  void invalidate() {
145  m_current_state = -1;
146  }
147 
148 
149  int value() const {
150  return m_current_state;
151  }
152 
153  int remote_value() const {
154  assert(m_remote_state && "In DiscreteDoF::remote_value() m_remote_state is nullptr.\n");
155  return *m_remote_state;
156  }
157 
158  int const *remote_ptr() const {
159  return m_remote_state;
160  }
161 
162  void register_remote(const RemoteHandle &handle) {
163  assert(handle.i_ptr() && "In DiscreteDoF::register_remote(), attempting to register to nullptr!");
164  m_remote_state = handle.i_ptr();
165  }
166 
167  virtual DiscreteDoF *copy() const = 0;
168  // return new DiscreteDoF(*this);
169  //}
170 
171  virtual Index size() const = 0;
172 
173  //John G 050513
174  //Make an operator for this
175  virtual void set_value(int new_state) {
176  m_current_state = new_state;
177  return;
178  }
179 
180  virtual bool operator==(const DiscreteDoF &RHS) const {
181  if(value() != RHS.value())
182  return false;
183 
184  if(size() != RHS.size())
185  return false;
186 
187  return true;
188  }
189 
190  void print(std::ostream &out) const {
191  out << value();
192  }
193 
194  virtual jsonParser &to_json(jsonParser &json) const = 0;
195 
196  };
197 
198  //*******************************************************************************************
199  template< typename T>
200  class OccupantDoF : public DiscreteDoF {
201  /**** Inherited from DiscreteDoF ****
202  // index into domain of the current state, -1 if unspecified
203  // int m_current_state;
204 
205  // Allows DoF to point to a remote value for faster/easier evaluation
206  // const int *m_remote_state;
207  **/
208 
211 
212  public:
214  OccupantDoF(const std::string &_name, const Array<T> &_domain, int _current_state = 0) :
215  DiscreteDoF(_name, _current_state, SymGroupRepID::identity(_domain.size())), m_domain(_domain) { }
216 
217  OccupantDoF(const Array<T> &_domain, int _current_state = 0) :
218  DiscreteDoF("occupation_occ", _current_state, SymGroupRepID::identity(_domain.size())), m_domain(_domain) { }
219 
220  OccupantDoF(std::initializer_list<T> _domain, int _current_state = 0) :
221  DiscreteDoF("occupation_occ", _current_state), m_domain(_domain.begin(), _domain.end()) {
223  }
224 
225  const T &get_occ() const {
226  return m_domain[m_current_state];
227  }
228 
229  DiscreteDoF *copy() const override {
230  return new OccupantDoF(*this);
231  }
232 
233 
234  //John G 050513
235  //Make an operator for this
236  void set_value(int new_state) override {
237  if(Index(new_state) >= m_domain.size()) {
238  std::cerr << "In OccupantDoF::set_value(): Bad Assignment, new_state>=size Go check! EXITING \n";
239  assert(0);
240  exit(1);
241  }
242  m_current_state = new_state;
243  return;
244  }
245 
246  void set_current_state(const T &new_state) {
247  Index new_index = m_domain.find(new_state);
248  if(new_index >= m_domain.size()) {
249  std::cerr << "In OccupantDoF::set_current_state(const T &new_state): Bad Assignment, new_state not found in domain. Go check! EXITING \n";
250  assert(0);
251  exit(1);
252  }
253  m_current_state = new_index;
254  return;
255  }
256 
257  bool compare(const OccupantDoF &RHS, bool compare_value) const {
258  if(compare_value && (value() != RHS.value()))
259  return false;
260 
261  if(size() != RHS.size())
262  return false;
263 
264  for(Index i = 0; i < size(); i++) {
265  if((*this)[i] != RHS[i])
266  return false;
267  }
268 
269  return true;
270  }
271 
272  bool operator==(const OccupantDoF &RHS) const {
273  return compare(RHS, true);
274  }
275 
276  void set_domain(const Array<T> &new_dom) {
277  m_domain = new_dom;
278  if(new_dom.size() == 1)
279  m_current_state = 0;
280  else
281  m_current_state = -1;
283  }
284 
285  const Array<T> &get_domain() const {
286  return m_domain;
287  }
288 
290  return m_domain[i];
291  }
292 
293  const T &operator[](Index i)const {
294  return m_domain[i];
295  }
296 
297  Index size() const override {
298  return m_domain.size();
299  }
300 
301  void print(std::ostream &out) const {
302  for(Index i = 0; i < size(); i++) {
303  if(i == 0)
304  out << m_domain[i].name;
305  else
306  out << ' ' << m_domain[i].name;
307  }
308  }
309 
310  void print_occ(std::ostream &out) const {
311  if(valid_index(m_current_state))
312  out << get_occ().name;
313  else
314  out << '?';
315  }
316 
317  jsonParser &to_json(jsonParser &json) const override;
318 
319  };
320 
321 
323  // because we want to be able to do: void from_json(DoF *dof, const jsonParser &json)
324  //
325 
326  // int version
327  jsonParser &to_json(const OccupantDoF<int> &dof, jsonParser &json);
328 
329  void from_json(OccupantDoF<int> &dof, const jsonParser &json);
330 
331  // molecule version
332  jsonParser &to_json(const OccupantDoF<Molecule> &dof, jsonParser &json);
333 
334  // Note: as a hack this expects dof.m_domain[0] to be present and have the right lattice!!!
335  // it's just used to set the lattice for all the Molecules
336  void from_json(OccupantDoF<Molecule> &dof, const jsonParser &json);
337 
338 
339  //*******************************************************************************************
340 
341  class ContinuousDoF : public DoF {
344 
346  const double *m_remote_ptr;
347  public:
348  ContinuousDoF() : min_val(NAN), max_val(NAN), current_val(NAN),
349  current_min(NAN), current_max(NAN), m_remote_ptr(nullptr) {}
350 
351  ContinuousDoF(const std::string &tname, double tmin, double tmax) :
352  DoF(tname), min_val(tmin), max_val(tmax), current_val(NAN),
353  current_min(min_val), current_max(max_val), m_remote_ptr(nullptr) {}
354 
355  ContinuousDoF(const std::string &tname, Index _ID, double tmin, double tmax) :
356  DoF(tname, _ID), min_val(tmin), max_val(tmax), current_val(NAN),
357  current_min(min_val), current_max(max_val), m_remote_ptr(nullptr) {}
358 
359  double value() const {
360  return current_val;
361  }
362 
363  bool compare(const ContinuousDoF &RHS, bool compare_value) const {
364  if(compare_value && !almost_equal(value(), RHS.value()))
365  return false;
366 
367  return(type_name() == RHS.type_name()
368  && (ID() == RHS.ID()));
369  }
370 
371  bool operator==(const ContinuousDoF &RHS) const {
372  return((type_name() == RHS.type_name())
373  && (ID() == RHS.ID())
374  && almost_equal(value(), RHS.value()));
375  }
376 
377  double remote_value() const {
378  assert(m_remote_ptr && "In ContinuousDoF::remote_value() m_remote_val is nullptr.\n");
379  return *m_remote_ptr;
380  }
381 
382  double const *remote_ptr() const {
383  return m_remote_ptr;
384  }
385 
386  void register_remote(const RemoteHandle &handle) {
387  assert(handle.d_ptr() && "In ContinuousDoF::register_remote(), attempting to register to nullptr!");
388  m_remote_ptr = handle.d_ptr();
389  }
390 
391  void perturb_dof() {} //sets current_val to something between min_val and max_val
392 
393  // ~ContinuousDoF(){} //This has to be defined;
394 
395  ContinuousDoF *copy() const {
396  return new ContinuousDoF(*this);
397  }
398 
399  jsonParser &to_json(jsonParser &json) const {
400  json.put_obj();
401  json["DoF_type"] = "ContinuousDoF";
402  json["m_type_name"] = m_type_name;
403  json["min"] = min_val;
404  json["max"] = max_val;
405  json["current"] = current_val;
406  return json;
407  }
408 
409  };
410 
411  void from_json(ContinuousDoF &dof, const jsonParser &json);
412 
413  jsonParser &to_json(const ContinuousDoF &dof, jsonParser &json);
414 
415 }
416 #endif
void set_current_state(const T &new_state)
Definition: DoF.hh:246
void set_domain(const Array< T > &new_dom)
Definition: DoF.hh:276
void set_ID(Index new_ID)
Definition: DoF.hh:75
Index ID() const
Definition: DoF.hh:71
void set_value(int new_state) override
Definition: DoF.hh:236
virtual bool operator==(const DiscreteDoF &RHS) const
Definition: DoF.hh:180
void print(std::ostream &out) const
Definition: DoF.hh:190
const double * d_ptr() const
Definition: DoF.hh:39
void from_json(ClexDescription &desc, const jsonParser &json)
Index size() const
Definition: Array.hh:145
bool is_specified() const
Definition: DoF.hh:140
virtual jsonParser & to_json(jsonParser &json) const =0
DiscreteDoF(const std::string &_name, int _current_state=0, SymGroupRepID _id=SymGroupRepID::identity(0))
Definition: DoF.hh:122
Type-safe ID object for communicating and accessing Symmetry representation info. ...
virtual ~DoF()
Definition: DoF.hh:46
double min_val
Definition: DoF.hh:342
const int * i_ptr() const
Definition: DoF.hh:36
virtual ~DiscreteDoF()
Definition: DoF.hh:130
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
DiscreteDoF * copy() const override
Definition: DoF.hh:229
double current_val
Definition: DoF.hh:342
SymGroupRepID m_sym_rep_ID
Definition: DoF.hh:117
void invalidate()
Definition: DoF.hh:144
const double * m_remote_ptr
Allows DoF to point to a remote value for faster/easier evaluation.
Definition: DoF.hh:346
Main CASM namespace.
Definition: complete.cpp:8
const std::string & type_name() const
Definition: DoF.hh:49
void unlock_ID()
Definition: DoF.hh:90
void print_occ(std::ostream &out) const
Definition: DoF.hh:310
Definition: DoF.hh:21
virtual void set_value(int new_state)
Definition: DoF.hh:175
jsonParser & to_json(jsonParser &json) const
Definition: DoF.hh:399
T & operator[](Index i)
Definition: DoF.hh:289
SymGroupRepID sym_rep_ID() const
Definition: DoF.hh:132
std::string type_name_suffix() const
Definition: DoF.hh:62
virtual jsonParser & to_json(jsonParser &json) const =0
double remote_value() const
Definition: DoF.hh:377
jsonParser & to_json(jsonParser &json) const override
bool m_ID_lock
Definition: DoF.hh:28
void print(std::ostream &out) const
Definition: DoF.hh:301
double const *const m_d_ptr
Definition: DoF.hh:31
const int * m_remote_state
Allows DoF to point to a remote value for faster/easier evaluation.
Definition: DoF.hh:115
EigenIndex Index
For long integer indexing:
OccupantDoF(const Array< T > &_domain, int _current_state=0)
Definition: DoF.hh:217
bool operator==(const OccupantDoF &RHS) const
Definition: DoF.hh:272
virtual DiscreteDoF * copy() const =0
Index m_dof_ID
Definition: DoF.hh:27
const Array< T > & get_domain() const
Definition: DoF.hh:285
Index size() const override
Definition: DoF.hh:297
void lock_ID()
Definition: DoF.hh:86
std::string m_type_name
Definition: DoF.hh:23
bool is_locked() const
Definition: DoF.hh:82
double current_max
Definition: DoF.hh:343
void perturb_dof()
Definition: DoF.hh:391
virtual Index size() const =0
int value() const
Definition: DoF.hh:149
DoF(const std::string &_name="", Index _ID=-1)
Definition: DoF.hh:45
std::string type_name_prefix() const
Definition: DoF.hh:53
Index find(const T &test_elem) const
Definition: Array.hh:707
double value() const
Definition: DoF.hh:359
const T & operator[](Index i) const
Definition: DoF.hh:293
void set_sym_rep_ID(SymGroupRepID _id)
Definition: DoF.hh:136
int m_current_state
index into domain of the current state, -1 if unspecified
Definition: DoF.hh:112
const T & get_occ() const
Definition: DoF.hh:225
bool compare(const ContinuousDoF &RHS, bool compare_value) const
Definition: DoF.hh:363
double current_min
Definition: DoF.hh:343
jsonParser & put_obj()
Puts new empty JSON object.
Definition: jsonParser.hh:276
ContinuousDoF(const std::string &tname, double tmin, double tmax)
Definition: DoF.hh:351
Array< T > m_domain
Allowed values, assume class T has member 'T.name'.
Definition: DoF.hh:210
RemoteHandle(const double &d)
Definition: DoF.hh:34
int const *const m_i_ptr
Definition: DoF.hh:32
ContinuousDoF * copy() const
Definition: DoF.hh:395
double max_val
Definition: DoF.hh:342
OccupantDoF(const std::string &_name, const Array< T > &_domain, int _current_state=0)
Definition: DoF.hh:214
int const * remote_ptr() const
Definition: DoF.hh:158
int remote_value() const
Definition: DoF.hh:153
double const * remote_ptr() const
Definition: DoF.hh:382
OccupantDoF(std::initializer_list< T > _domain, int _current_state=0)
Definition: DoF.hh:220
void register_remote(const RemoteHandle &handle)
Definition: DoF.hh:162
ContinuousDoF(const std::string &tname, Index _ID, double tmin, double tmax)
Definition: DoF.hh:355
void register_remote(const RemoteHandle &handle)
Definition: DoF.hh:386
Basic std::vector like container (deprecated)
bool operator==(const ContinuousDoF &RHS) const
Definition: DoF.hh:371
bool almost_equal(const GenericCluster< CoordType > &LHS, const GenericCluster< CoordType > &RHS, double tol)
bool compare(const OccupantDoF &RHS, bool compare_value) const
Definition: DoF.hh:257
static SymGroupRepID identity(Index dim)
Static function to construct an ID for identity representations.
bool valid_index(Index i)
RemoteHandle(const int &i)
Definition: DoF.hh:35