CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
OccupantFunction.cc
Go to the documentation of this file.
1 
3 
4 #include "casm/symmetry/SymOp.hh"
6 
7 namespace CASM {
8 
9 
10  //*******************************************************************************************
11 
12 
14  //Function::inner_prod_table[sclass_ID()][sclass_ID()] = new BasicVarVarScalarProd();
16 
17  //BasicVarPolyProduct does not yet exist
18  //Function::inner_prod_table[sclass_ID()][PolynomialFunction::sclass_ID()]=new BasicVarMonoProduct();
19  }
20 
21  //*******************************************************************************************
22 
23 
26  }
27 
28  //*******************************************************************************************
29 
30 
33  }
34 
35  //*******************************************************************************************
36 
37 
39  return new OccupantFunction(*this);
40  }
41 
42  //*******************************************************************************************
43 
44 
45  //*******************************************************************************************
46 
47  bool OccupantFunction::_accept(const FunctionVisitor &visitor, BasisSet const *home_basis_ptr/*=NULL*/) {
48  return visitor.visit(*this, home_basis_ptr);
49  }
50 
51  //*******************************************************************************************
52  //John G 010413
53 
54 
56  m_formula.clear();
57  m_tex_formula.clear();
58 
59  std::stringstream tformula, ttex;
60  Array<int> var_ind;
61 
62  // count the number of ones in var_ind;
63  Index one_count(0);
64  for(Index i = 0; i < dof().size(); i++) {
65  if(!almost_zero(m_eval_table[i]))
66  var_ind.push_back(i);
67 
68  if(almost_equal(m_eval_table[i], 1.0))
69  one_count++;
70  }
71  if(!var_ind.size()) {
72  m_formula = "0";
73  m_tex_formula = "0";
74  return;
75  }
76 
77  if(one_count == dof().size()) {
78  m_formula = "1";
79  m_tex_formula = "1";
80  return;
81  }
82 
83  double var_scale(m_eval_table[var_ind[0]]);
84  if(almost_equal(var_scale, -1.0)) {
85  ttex << '-';
86  }
87  else if(!almost_equal(var_scale, 1.0)) {
88  ttex << irrational_to_tex_string(var_scale, 2 * m_eval_table.size());
89  }
90 
91  if(var_ind.size() > 1) {
92  ttex << "(";
93  }
94 
95  for(Index i = 0; i < var_ind.size(); i++) {
96  if(i > 0 && m_eval_table[var_ind[i]] > 0) {
97  tformula << '+';
98  }
99  if(almost_zero(m_eval_table[var_ind[i]] + 1)) {
100  tformula << '-';
101  }
102  if(!almost_zero(std::abs(m_eval_table[var_ind[i]]) - 1)) {
103  tformula << m_eval_table[var_ind[i]];
104  tformula << '*';
105  }
106 
107  if(i > 0 && m_eval_table[var_ind[i]] / var_scale > 0) {
108  ttex << '+';
109  }
110  else if(almost_equal(m_eval_table[var_ind[i]] / var_scale, -1.0)) {
111  ttex << '-';
112  }
113 
114  if(!almost_zero(std::abs(m_eval_table[var_ind[i]] / var_scale) - 1)) {
115  ttex << irrational_to_tex_string(m_eval_table[var_ind[i]] / var_scale, 2 * m_eval_table.size());
116  }
117 
118  //tformula << "p_" << m_var[var_ind[i]].name;
119  //tformula << "p_" << m_var[var_ind[i]].name;
120  }
121  if(var_ind.size() > 1) {
122  ttex << ')';
123  }
124  m_tex_formula = ttex.str();
125  m_formula = tformula.str();
126  return;
127  }
128 
129  //*******************************************************************************************
130 
131  int OccupantFunction::register_remotes(const std::string &dof_name, const Array<DoF::RemoteHandle> &remote_handles) {
132 
133  if(dof().type_name() == dof_name) {
134  if(!valid_index(dof().ID()) || dof().ID() >= remote_handles.size()) {
135  std::cerr << "CRITICAL ERROR: In OccupantFunction::register_remotes(), dof().ID() = " << dof().ID() << " is out of bounds.\n"
136  << " Exiting...\n";
137  exit(1);
138  }
139  //std::cout << "Setting remote at Occ DoF " << dof().ID() << " of " << discrete_remotes.size() << "\n";
140  m_var->register_remote(remote_handles[dof().ID()]);
141  return 1;
142  }
143 
144  return 0;
145  }
146 
147  //*******************************************************************************************
148 
149 
150  bool OccupantFunction::_update_dof_IDs(const Array<Index> &before_IDs, const Array<Index> &after_IDs) {
151  if(dof().is_locked()) return false;
152 
153  Index ID_ind = before_IDs.find(dof().ID());
154 
155  if(ID_ind < after_IDs.size()) {
156  m_var->set_ID(after_IDs[ID_ind]);
157  m_formula.clear();
158  m_tex_formula.clear();
159  return true;
160  }
161  //std::cout << "*****COULD NOT UPDATE ID " << dof().ID() << "!\n";
162  return false;
163  }
164 
165  //*******************************************************************************************
166 
167 
169  return dof().ID() == RHS->dof().ID()
170  && dof().type_name() == RHS->dof().type_name()
172  }
173 
174  //*******************************************************************************************
175 
176 
178  return &m_eval_table;
179  }
180 
181  //\John G 010413
182 
183  //*******************************************************************************************
184 
185 
187  if(m_eval_table.size() == 0) return true;
188 
189  for(EigenIndex i = 0; i < m_eval_table.size(); i++) {
190  if(!almost_zero(m_eval_table[i]))
191  return false;
192  }
193  return true;
194  }
195 
196  //*******************************************************************************************
197 
198 
200 
201  for(EigenIndex i = 0; i < m_eval_table.size(); i++) {
202  if(almost_zero(m_eval_table[i], tol))
203  m_eval_table[i] = 0;
204  }
205 
206  }
207 
208  //*******************************************************************************************
209 
210 
212  Index tnum(0);
213 
214  for(EigenIndex i = 0; i < m_eval_table.size(); i++) {
215  if(!almost_zero(m_eval_table[i]))
216  tnum++;
217  }
218  return tnum;
219  }
220 
221  //*******************************************************************************************
222 
223 
225 
226  for(EigenIndex i = 0; i < m_eval_table.size(); i++) {
227  if(!almost_zero(m_eval_table[i]))
228  return m_eval_table[i];
229  }
230  return 0.0;
231  }
232 
233  //*******************************************************************************************
234 
235 
237 
238  for(index = 0; EigenIndex(index) < m_eval_table.size(); index++) {
239  if(!almost_zero(m_eval_table[index]))
240  return m_eval_table[index];
241  }
242  return 0.0;
243  }
244 
245  //*******************************************************************************************
246 
247 
249  if(EigenIndex(i) < m_eval_table.size())
250  return m_eval_table[i];
251 
252  return 0.0;
253  }
254 
255  //*******************************************************************************************
256 
257 
259  if(m_sym_rep_ID.is_identity()) return this;
260 
261  m_formula.clear();
262  refresh_ID();
263  Eigen::MatrixXd const *tmat;
264  tmat = op.get_matrix_rep(m_sym_rep_ID);
265 
266  //Eigen matrix multiplication is alias-safe
267  if(tmat)
268  m_eval_table = (*tmat) * m_eval_table;
269  //m_eval_table.transform(*tmat);
270  else
271  std::cerr << "WARNING: Attempting to reference invalid symmetry matrix in OccupantFunction::apply_sym! Continuing...\n";
272  return this;
273 
274  }
275 
276  //*******************************************************************************************
277 
278 
279  void OccupantFunction::scale(double scale_factor) {
280  m_formula.clear();
281  refresh_ID();
282  m_eval_table *= scale_factor;
283  }
284 
285  //*******************************************************************************************
286 
287 
289  return m_eval_table[dof().remote_value()];
290  }
291 
292  //*******************************************************************************************
293 
295  if(dvar.i_ptr() && dvar.i_ptr() == dof().remote_ptr()) {
296  std::cerr << "CRITICAL ERROR: OccupantFunction::remote_deval() is not implemented! Exiting...\n";
297  exit(1);
298  }
299  return 0.0;
300  }
301 
302  //*******************************************************************************************
303 
304 
305  double OccupantFunction::eval(const Array<Index> &dof_IDs, const Array<Index> &var_states) const {
306  Index which_state = dof_IDs.find(dof().ID());
307  if(which_state < var_states.size())
308  return m_eval_table[var_states[which_state]];
309  //else
310  std::cerr << "CRITICAL ERROR: In OccupantFunction::eval(), DoF ID of this OccupantFunction (" << dof().ID() << ") is not among specified IDs: " << dof_IDs << "\n"
311  << " Exiting...\n";
312  assert(0);
313  exit(1);
314  }
315 
316  //*******************************************************************************************
317 
318 
319  bool OccOccOperation::compare(const Function *LHS, const Function *RHS) const {
320  return static_cast<OccupantFunction const *>(LHS)->compare(static_cast<OccupantFunction const *>(RHS));
321 
322  }
323 
324  //*******************************************************************************************
325  //** jsonParser stuff - OccupantFunction
326  //*******************************************************************************************
327 
329 
330  Function::to_json(json);
331 
332  // Just write Function::formula for now
333 
334  return json;
335  }
336 
337  //*******************************************************************************************
338 
339  /*
340  void OccupantFunction::from_json(const jsonParser &json) {
341 
342  // no reading functions for now
343 
344  }
345  */
346 
347  //*******************************************************************************************
348 
350  return func.to_json(json);
351  }
352 
353  /*
354  // No reading functions for now
355  void from_json(OccupantFunction &func, const jsonParser &json) {
356  return func.from_json(json);
357  }
358  */
359 
360 }
Eigen::MatrixXd MatrixXd
static int get_class_ID()
Definition: HierarchyID.hh:32
void set_ID(Index new_ID)
Definition: DoF.hh:75
virtual bool visit(Variable &host, BasisSet const *bset_ptr) const
bool almost_zero(const T &val, double tol=TOL)
If T is not integral, use std::abs(val) < tol;.
Definition: CASM_math.hh:41
Function * _apply_sym(const SymOp &op)
Index ID() const
Definition: DoF.hh:71
double leading_coefficient() const
Index size() const
Definition: Array.hh:145
const int * i_ptr() const
Definition: DoF.hh:36
bool compare(const OccupantFunction *RHS) const
void push_back(const T &toPush)
Definition: Array.hh:513
virtual jsonParser & to_json(jsonParser &json) const
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
void scale(double scale_factor)
bool is_identity() const
Returns true if SymGroupRepID corresponds to an Identity representation.
Main CASM namespace.
Definition: complete.cpp:8
const std::string & type_name() const
Definition: DoF.hh:49
double eval(const Array< Index > &dof_IDs, const Array< Index > &var_states) const
double tol
Eigen::MatrixXd const * get_matrix_rep(SymGroupRepID _rep_ID) const
get pointer to matrix representation corresponding to rep_ID
Eigen::MatrixXd::Index EigenIndex
For integer indexing:
SymOp is the Coordinate representation of a symmetry operation it keeps fraction (FRAC) and Cartesian...
Definition: SymOp.hh:28
bool compare(Function const *LHS, Function const *RHS) const
EigenIndex Index
For long integer indexing:
Eigen::VectorXd VectorXd
std::string m_formula
virtual Index size() const =0
bool _accept(const FunctionVisitor &visitor, BasisSet const *home_basis_ptr=NULL)
std::string irrational_to_tex_string(double val, int lim, int max_pow=2)
Definition: CASM_math.cc:224
Index find(const T &test_elem) const
Definition: Array.hh:707
Eigen::VectorXd const * get_eigen_coeffs() const
void small_to_zero(double tol=TOL)
Index ID() const
std::string m_tex_formula
Function * copy() const
double remote_eval() const
int register_remotes(const std::string &dof_name, const Array< DoF::RemoteHandle > &remote_handles)
static Array< Array< FunctionOperation * > > operation_table
Eigen::VectorXd m_eval_table
int const * remote_ptr() const
Definition: DoF.hh:158
int remote_value() const
Definition: DoF.hh:153
bool _update_dof_IDs(const Array< Index > &before_IDs, const Array< Index > &after_IDs)
void register_remote(const RemoteHandle &handle)
Definition: DoF.hh:162
double remote_deval(const DoF::RemoteHandle &dvar) const
jsonParser & to_json(jsonParser &json) const
bool almost_equal(const GenericCluster< CoordType > &LHS, const GenericCluster< CoordType > &RHS, double tol)
const DiscreteDoF & dof() const
static void fill_dispatch_table()
double get_coefficient(Index i) const
std::string type_name() const
bool valid_index(Index i)