CASM  1.1.0
A Clusters Approach to Statistical Mechanics
OccupantFunction.cc
Go to the documentation of this file.
2 
3 #include <iostream>
4 
7 #include "casm/symmetry/SymOp.hh"
8 
9 namespace CASM {
10 
11 //*******************************************************************************************
12 
14  // Function::inner_prod_table[sclass_ID()][sclass_ID()] = new
15  // BasicVarVarScalarProd();
17 
18  // BasicVarPolyProduct does not yet exist
19  // Function::inner_prod_table[sclass_ID()][PolynomialFunction::sclass_ID()]=new
20  // BasicVarMonoProduct();
21 }
22 
23 //*******************************************************************************************
24 
27 }
28 
29 //*******************************************************************************************
30 
33 }
34 
35 //*******************************************************************************************
36 
37 Function *OccupantFunction::copy() const { return new OccupantFunction(*this); }
38 
39 //*******************************************************************************************
40 
42  BasisSet const *home_basis_ptr /*=NULL*/) {
43  return visitor.visit(*this, home_basis_ptr);
44 }
45 
46 //*******************************************************************************************
47 
49  BasisSet const *home_basis_ptr /*=NULL*/) const {
50  return visitor.visit(*this, home_basis_ptr);
51 }
52 
53 //*******************************************************************************************
54 // John G 010413
55 
57  m_formula.clear();
58  m_tex_formula.clear();
59 
60  std::stringstream tformula, ttex;
61  Array<int> var_ind;
62 
63  // count the number of ones in var_ind;
64  Index one_count(0);
65  for (Index i = 0; i < dof().size(); i++) {
66  if (!almost_zero(m_eval_table[i])) var_ind.push_back(i);
67 
68  if (almost_equal(m_eval_table[i], 1.0)) one_count++;
69  }
70  if (!var_ind.size()) {
71  m_formula = "0";
72  m_tex_formula = "0";
73  return;
74  }
75 
76  if (one_count == dof().size()) {
77  m_formula = "1";
78  m_tex_formula = "1";
79  return;
80  }
81 
82  double var_scale(m_eval_table[var_ind[0]]);
83  if (almost_equal(var_scale, -1.0)) {
84  ttex << '-';
85  } else if (!almost_equal(var_scale, 1.0)) {
86  ttex << irrational_to_tex_string(var_scale, 2 * m_eval_table.size());
87  }
88 
89  if (var_ind.size() > 1) {
90  ttex << "(";
91  }
92 
93  for (Index i = 0; i < var_ind.size(); i++) {
94  if (i > 0 && m_eval_table[var_ind[i]] > 0) {
95  tformula << '+';
96  }
97  if (almost_zero(m_eval_table[var_ind[i]] + 1)) {
98  tformula << '-';
99  }
100  if (!almost_zero(std::abs(m_eval_table[var_ind[i]]) - 1)) {
101  tformula << m_eval_table[var_ind[i]];
102  tformula << '*';
103  }
104 
105  if (i > 0 && m_eval_table[var_ind[i]] / var_scale > 0) {
106  ttex << '+';
107  } else if (almost_equal(m_eval_table[var_ind[i]] / var_scale, -1.0)) {
108  ttex << '-';
109  }
110 
111  if (!almost_zero(std::abs(m_eval_table[var_ind[i]] / var_scale) - 1)) {
112  ttex << irrational_to_tex_string(m_eval_table[var_ind[i]] / var_scale,
113  2 * m_eval_table.size());
114  }
115 
116  tformula << "p_" << var_ind[i];
117  ttex << "p_" << var_ind[i];
118  if (valid_index(m_var->ID())) {
119  tformula << "_{" << m_var->ID() << "}";
120  ttex << "_{" << m_var->ID() << "}";
121  }
122  }
123  if (var_ind.size() > 1) {
124  ttex << ')';
125  }
126  m_tex_formula = ttex.str();
127  m_formula = tformula.str();
128  return;
129 }
130 
131 //*******************************************************************************************
132 
134  const std::vector<DoF::RemoteHandle> &remote_handles) {
135  std::vector<DoF::RemoteHandle>::const_iterator it =
136  find(remote_handles.begin(), remote_handles.end(), m_var->handle());
137  if (it != remote_handles.end()) {
138  if (!valid_index(m_var->ID())) {
139  throw std::runtime_error(
140  std::string("In OccupantFunction::register_remotes(), attempting to "
141  "register dof with ID = ") +
142  std::to_string(m_var->ID()) + ", which is out of bounds.\n");
143  }
144  m_var->register_remote(*it);
145  return 1;
146  }
147  return 0;
148 }
149 
150 //*******************************************************************************************
151 
152 bool OccupantFunction::_update_dof_IDs(const std::vector<Index> &before_IDs,
153  const std::vector<Index> &after_IDs) {
154  if (dof().is_locked()) return false;
155 
156  Index ID_ind = find_index(before_IDs, dof().ID());
157 
158  if (ID_ind < after_IDs.size()) {
159  m_var->set_ID(after_IDs[ID_ind]);
160  m_formula.clear();
161  m_tex_formula.clear();
162  return true;
163  }
164  // std::cout << "*****COULD NOT UPDATE ID " << dof().ID() << "!\n";
165  return false;
166 }
167 
168 //*******************************************************************************************
169 
171  return dof().ID() == RHS->dof().ID() &&
172  dof().type_name() == RHS->dof().type_name() &&
174 }
175 
176 //*******************************************************************************************
177 
179  return &m_eval_table;
180 }
181 
182 //\John G 010413
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])) return false;
191  }
192  return true;
193 }
194 
195 //*******************************************************************************************
196 
198  for (EigenIndex i = 0; i < m_eval_table.size(); i++) {
199  if (almost_zero(m_eval_table[i], tol)) m_eval_table[i] = 0;
200  }
201 }
202 
203 //*******************************************************************************************
204 
206  Index tnum(0);
207 
208  for (EigenIndex i = 0; i < m_eval_table.size(); i++) {
209  if (!almost_zero(m_eval_table[i])) tnum++;
210  }
211  return tnum;
212 }
213 
214 //*******************************************************************************************
215 
217  for (EigenIndex i = 0; i < m_eval_table.size(); i++) {
218  if (!almost_zero(m_eval_table[i])) return m_eval_table[i];
219  }
220  return 0.0;
221 }
222 
223 //*******************************************************************************************
224 
226  for (index = 0; EigenIndex(index) < m_eval_table.size(); index++) {
227  if (!almost_zero(m_eval_table[index])) return m_eval_table[index];
228  }
229  return 0.0;
230 }
231 
232 //*******************************************************************************************
233 
235  if (EigenIndex(i) < m_eval_table.size()) return m_eval_table[i];
236 
237  return 0.0;
238 }
239 
240 //*******************************************************************************************
241 
243  if (m_sym_rep_ID.is_identity()) return this;
244 
245  m_formula.clear();
246  refresh_ID();
247  Eigen::MatrixXd const *tmat;
248  tmat = op.get_matrix_rep(m_sym_rep_ID);
249 
250  // Eigen matrix multiplication is alias-safe
251  if (tmat) m_eval_table = (*tmat) * m_eval_table;
252  // m_eval_table.transform(*tmat);
253  else
254  std::cerr << "WARNING: Attempting to reference invalid symmetry matrix in "
255  "OccupantFunction::apply_sym! Continuing...\n";
256  return this;
257 }
258 
259 //*******************************************************************************************
260 
261 void OccupantFunction::scale(double scale_factor) {
262  m_formula.clear();
263  refresh_ID();
264  m_eval_table *= scale_factor;
265 }
266 
267 //*******************************************************************************************
268 
269 double OccupantFunction::discrete_eval(int state) const {
270  return m_eval_table[state];
271 }
272 
273 //*******************************************************************************************
274 
276  return m_eval_table[dof().remote_value()];
277 }
278 
279 //*******************************************************************************************
280 
282  if (dvar.i_ptr() && dvar.i_ptr() == dof().remote_ptr()) {
283  std::cerr << "CRITICAL ERROR: OccupantFunction::remote_deval() is not "
284  "implemented! Exiting...\n";
285  exit(1);
286  }
287  return 0.0;
288 }
289 
290 //*******************************************************************************************
291 
292 bool OccOccOperation::compare(const Function *LHS, const Function *RHS) const {
293  return static_cast<OccupantFunction const *>(LHS)->compare(
294  static_cast<OccupantFunction const *>(RHS));
295 }
296 
297 //*******************************************************************************************
298 //** jsonParser stuff - OccupantFunction
299 //*******************************************************************************************
300 
302  Function::to_json(json);
303 
304  // Just write Function::formula for now
305 
306  return json;
307 }
308 
309 //*******************************************************************************************
310 
311 /*
312 void OccupantFunction::from_json(const jsonParser &json) {
313 
314  // no reading functions for now
315 
316 }
317 */
318 
319 //*******************************************************************************************
320 
322  return func.to_json(json);
323 }
324 
325 /*
326 // No reading functions for now
327 void from_json(OccupantFunction &func, const jsonParser &json) {
328  return func.from_json(json);
329 }
330 */
331 
332 } // namespace CASM
Basic std::vector like container (deprecated)
Definition: Array.hh:45
Index size() const
Definition: Array.hh:131
static int get_class_ID()
Definition: HierarchyID.hh:30
int remote_value() const
Definition: DoF.hh:210
virtual Index size() const =0
Index ID() const
Const access of integer ID.
Definition: DoF.hh:140
std::string type_name() const
Const access of DoF type name.
Definition: DoF.hh:134
const int * i_ptr() const
Definition: DoF.hh:85
Index ID() const
std::string m_tex_formula
std::string m_formula
virtual jsonParser & to_json(jsonParser &json) const
static Array< Array< FunctionOperation * > > operation_table
virtual bool visit(Variable const &host, BasisSet const *bset_ptr) const
bool compare(Function const *LHS, Function const *RHS) const
const DiscreteDoF & dof() const
void small_to_zero(double tol=TOL) override
Eigen::VectorXd const * get_eigen_coeffs() const override
double get_coefficient(Index i) const override
Eigen::VectorXd m_eval_table
jsonParser & to_json(jsonParser &json) const override
bool _update_dof_IDs(const std::vector< Index > &before_IDs, const std::vector< Index > &after_IDs) override
notstd::cloneable_ptr< DiscreteDoF > m_var
void scale(double scale_factor) override
int class_ID() const override
double discrete_eval(int state) const
int register_remotes(const std::vector< DoF::RemoteHandle > &remote_handles) override
Function * _apply_sym(const SymOp &op) override
void make_formula() const override
double leading_coefficient() const override
bool compare(const OccupantFunction *RHS) const
bool is_zero() const override
bool _accept(const FunctionVisitor &visitor, BasisSet const *home_basis_ptr=NULL) override
Function * copy() const override
static void fill_dispatch_table()
Index num_terms() const override
double remote_eval() const override
double remote_deval(const DoF::RemoteHandle &dvar) const override
bool is_identity() const
Returns true if SymGroupRepID corresponds to an Identity representation.
SymOp is the Coordinate representation of a symmetry operation it keeps fraction (FRAC) and Cartesian...
Definition: SymOp.hh:28
Eigen::MatrixXd const * get_matrix_rep(SymGroupRepID _rep_ID) const
get pointer to matrix representation corresponding to rep_ID
void push_back(const T &toPush)
Definition: Array.hh:431
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: io_traits.hh:172
Main CASM namespace.
Definition: APICommand.hh:8
bool almost_equal(ClusterInvariants const &A, ClusterInvariants const &B, double tol)
Check if ClusterInvariants are equal.
Eigen::MatrixXd MatrixXd
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
Index find_index(Iterator begin, Iterator end, const T &value)
Equivalent to std::distance(begin, std::find(begin, end, value))
Definition: algorithm.hh:24
bool almost_zero(const T &val, double tol=TOL)
If T is not integral, use std::abs(val) < tol;.
Definition: CASM_math.hh:104
Iterator find(Iterator begin, Iterator end, const T &value, BinaryCompare q)
Equivalent to std::find(begin, end, value), but with custom comparison.
Definition: algorithm.hh:16
std::string irrational_to_tex_string(double val, int lim, int max_pow=2)
Definition: CASM_math.cc:199
Eigen::MatrixXd::Index EigenIndex
Definition: eigen.hh:7
bool valid_index(Index i)
Definition: definitions.cc:5
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
Eigen::VectorXd VectorXd