CASM  1.1.0
A Clusters Approach to Statistical Mechanics
FunctionVisitor.cc
Go to the documentation of this file.
2 
8 #include "casm/misc/CASM_math.hh"
9 namespace CASM {
10 std::vector<std::string> parse_label_template(std::string const &_template) {
11  // parse _template into the Array m_sub_strings
12  std::vector<std::string> result;
13  if (_template.size()) result.push_back(std::string());
14  for (Index i = 0; i < _template.size(); i++) {
15  if (_template[i] == '%') {
16  if (result.back().size()) result.push_back(std::string());
17  result.back() = _template.substr(i, 2);
18  if ((++i) + 1 < _template.size()) result.push_back(std::string());
19  } else
20  result.back().push_back(_template[i]);
21  }
22  // std::cout << "substring expression: " << result << '\n';
23  return result;
24 }
25 
27  BasisSet const *bset_ptr) const {
28  return this->_generic_visit(host, bset_ptr);
29 }
30 
31 bool FunctionVisitor::visit(Variable &host, BasisSet const *bset_ptr) const {
32  return this->_generic_visit(host, bset_ptr);
33 }
34 
36  BasisSet const *bset_ptr) const {
37  return this->_generic_visit(host, bset_ptr);
38 }
39 
41  BasisSet const *bset_ptr) const {
42  return this->_generic_visit(host, bset_ptr);
43 }
44 
46  BasisSet const *bset_ptr) const {
47  return this->_generic_visit(host, bset_ptr);
48 }
49 
51  BasisSet const *bset_ptr) const {
52  return this->_generic_visit(host, bset_ptr);
53 }
54 
56  BasisSet const *bset_ptr) const {
57  Function const &const_host(host);
58  return this->_generic_visit(const_host, bset_ptr);
59 }
60 
62  BasisSet const *bset_ptr) const {
63  return false;
64 }
65 
67  BasisSet const *bset_ptr) const {
69  return false;
70 }
71 
73  BasisSet const *bset_ptr) const {
75  return false;
76 }
77 
78 OccFuncLabeler::OccFuncLabeler(const std::string &_template) {
80 }
81 
82 //*******************************************************************************************
83 
85  BasisSet const *bset_ptr) const {
86  std::stringstream ss;
87 
88  for (Index i = 0; i < m_sub_strings.size(); i++) {
89  if (m_sub_strings[i] == "%n") {
90  if (valid_index(host.dof().ID()))
91  ss << host.dof().ID();
92  else
93  ss << '?';
94  } else if (m_sub_strings[i] == "%f") {
95  if (valid_index(host.occ_func_ind()))
96  ss << host.occ_func_ind();
97  else
98  ss << '?';
99  } else if (m_sub_strings[i] == "%b") {
100  if (valid_index(host.basis_ind()))
101  ss << host.basis_ind();
102  else
103  ss << '?';
104  } else
105  ss << m_sub_strings[i];
106  }
107  // std::cout << "Paying a visit. Formula before: " << host.formula() << "\n";
108  host.set_formula(ss.str());
109  // std::cout << " Formula after: " << host.formula() << "\n";
110  return true;
111 }
112 
113 //*******************************************************************************************
114 
115 VariableLabeler::VariableLabeler(std::string const &_type_name,
116  const std::string &_template)
117  : m_type_name(_type_name) {
118  m_sub_strings = parse_label_template(_template);
119 }
120 
121 //*******************************************************************************************
122 
123 bool VariableLabeler::visit(Variable &host, BasisSet const *bset_ptr) const {
124  if (host.dof_set().type_name() != m_type_name) return false;
125 
126  std::stringstream tformula, ttex;
127  Array<int> var_ind;
128 
129  for (Index i = 0; i < host.dof_set().size(); i++) {
130  if (!almost_zero(host.coeffs()[i])) {
131  var_ind.push_back(i);
132  }
133  }
134 
135  if (!var_ind.size()) {
136  host.set_formula("0");
137  host.set_tex_formula("0");
138  return false;
139  }
140 
141  double var_scale(host.coeffs()[var_ind[0]]);
142  if (almost_zero(var_scale + 1)) {
143  ttex << '-';
144  } else if (!almost_zero(var_scale - 1)) {
145  ttex << irrational_to_tex_string(
146  var_scale, 10 * host.coeffs().size() * host.coeffs().size());
147  }
148 
149  if (var_ind.size() > 1) {
150  tformula << "(";
151  ttex << "(";
152  }
153 
154  double coeff;
155  for (Index i = 0; i < var_ind.size(); i++) {
156  const ContinuousDoF &dof(host.dof_set()[var_ind[i]]);
157  coeff = host.coeffs()[var_ind[i]];
158 
159  if (i > 0 && coeff > 0) {
160  tformula << '+';
161  }
162  if (almost_zero(coeff + 1)) {
163  tformula << '-';
164  }
165  if (!almost_zero(std::abs(coeff) - 1)) {
166  tformula << coeff;
167  tformula << '*';
168  }
169 
170  if (i > 0 && coeff / var_scale > 0) {
171  ttex << '+';
172  }
173  if (almost_zero(coeff / var_scale + 1)) {
174  ttex << '-';
175  }
176  if (!almost_zero(std::abs(coeff / var_scale) - 1)) {
177  ttex << irrational_to_tex_string(
178  coeff / var_scale,
179  10 * host.coeffs().size() * host.coeffs().size())
180  << ' ';
181  }
182 
183  for (Index j = 0; j < m_sub_strings.size(); j++) {
184  if (m_sub_strings[j] == "%n") {
185  if (valid_index(dof.ID())) {
186  ttex << dof.ID();
187  tformula << dof.ID();
188  } else {
189  // std::cout << "type_name is " << dof.type_name() << ", ID is " <<
190  // dof.ID() << "\n";
191  ttex << '?';
192  tformula << '?';
193  }
194  } else if (m_sub_strings[j] == "%p") {
195  std::string prefix = dof.type_name();
196  if (prefix.empty()) prefix = "?";
197  ttex << prefix;
198  tformula << prefix;
199  } else if (m_sub_strings[j] == "%s") {
200  std::string suffix = dof.var_name();
201  if (suffix.empty()) suffix = "?";
202  ttex << suffix;
203  tformula << suffix;
204  } else if (m_sub_strings[j] == "%f") {
205  ttex << var_ind[i];
206  tformula << var_ind[i];
207  } else {
208  ttex << m_sub_strings[j];
209  tformula << m_sub_strings[j];
210  }
211  }
212  }
213  if (var_ind.size() > 1) {
214  tformula << ")";
215  ttex << ')';
216  }
217  host.set_tex_formula(ttex.str());
218  host.set_formula(tformula.str());
219  return true;
220 }
221 
222 //*******************************************************************************************
223 
225  BasisSet const *bset_ptr) const {
227  return true;
228 }
229 
230 //*******************************************************************************************
231 SubExpressionLabeler::SubExpressionLabeler(const std::string &_bset_name,
232  const std::string &_template)
233  : m_bset_name(_bset_name) {
234  m_sub_strings = parse_label_template(_template);
235 }
236 
237 //*******************************************************************************************
238 
240  BasisSet const *bset_ptr) const {
241  // if(host.type_name()=="PolynomialFunction"){
242  // std::cout << "Visiting function, with bset_name: '" << bset_ptr->name() <<
243  // "' and directive " << m_bset_name << ", " << m_sub_strings << "\n";
244  //}
245 
246  if (bset_ptr == nullptr || (bset_ptr->name()).find(m_bset_name) != 0) {
247  // std::cout << "_generic_visit(): bset_ptr is " << bset_ptr << "\n";
248  return false;
249  }
250  std::stringstream ss;
251 
252  for (Index i = 0; i < m_sub_strings.size(); i++) {
253  if (m_sub_strings[i] == "%N") {
254  if (bset_ptr->dof_IDs().size() == 0) {
255  ss << '?';
256  } else {
257  for (Index i = 0; i < bset_ptr->dof_IDs().size(); i++) {
258  ss << (bset_ptr->dof_IDs())[i];
259  if (i + 1 < bset_ptr->dof_IDs().size()) ss << '_';
260  }
261  }
262  }
263  if (m_sub_strings[i] == "%n") {
264  auto IDs = host.dof_IDs();
265  if (IDs.empty()) {
266  ss << '?';
267  } else {
268  Index i = 0;
269  for (Index id : IDs) {
270  ss << id;
271  if (++i < IDs.size()) ss << '_';
272  }
273  }
274  } else if (m_sub_strings[i] == "%f") {
275  Index f = bset_ptr->find(&host);
276  if (f < bset_ptr->size())
277  ss << f;
278  else
279  ss << '?';
280  } else if (m_sub_strings[i][0] == '%') {
281  ss << host.identifier(m_sub_strings[i][1]);
282  } else
283  ss << m_sub_strings[i];
284  }
285  // std::cout << "Paying a visit. Formula before: " << host.formula() << "\n";
286  host.set_formula(ss.str());
287  // std::cout << " Formula after: " << host.formula() << "\n";
288  return true;
289 }
290 } // namespace CASM
Basic std::vector like container (deprecated)
Definition: Array.hh:45
Index size() const
Definition: Array.hh:131
const std::vector< Index > & dof_IDs() const
Definition: BasisSet.hh:144
const std::string & name() const
Definition: BasisSet.hh:74
Index ID() const
Const access of integer ID.
Definition: DoF.hh:140
std::string var_name() const
Const access of variable name.
Definition: DoF.hh:137
std::string type_name() const
Const access of DoF type name.
Definition: DoF.hh:134
std::string const & type_name() const
Definition: DoFSet.hh:197
std::string identifier(char _key) const
void set_tex_formula(const std::string &new_formula)
virtual std::set< Index > dof_IDs() const =0
void set_formula(const std::string &new_formula)
virtual bool visit(Variable const &host, BasisSet const *bset_ptr) const
virtual bool _generic_visit(Function &host, BasisSet const *bset_ptr) const
bool visit(OccupantFunction &host, BasisSet const *bset_ptr) const
bool visit(OccupantFunction &host, BasisSet const *bset_ptr) const
OccFuncLabeler(const std::string &_template)
std::vector< std::string > m_sub_strings
bool visit(OccupantFunction &host, BasisSet const *bset_ptr) const
const DiscreteDoF & dof() const
double discrete_eval(int state) const
void set_basis_ind(int new_ind)
SubExpressionLabeler(const std::string &_bset_name, const std::string &_template)
std::vector< std::string > m_sub_strings
bool _generic_visit(Function &host, BasisSet const *bset_ptr) const
const DoFSet & dof_set() const
Definition: Variable.hh:63
const Eigen::VectorXd & coeffs() const
Definition: Variable.hh:67
VariableLabeler(std::string const &m_type_name, std::string const &_template)
std::vector< std::string > m_sub_strings
bool visit(Variable &host, BasisSet const *bset_ptr) const
void push_back(const T &toPush)
Definition: Array.hh:431
Index find(const T &test_elem) const
Definition: Array.hh:618
Main CASM namespace.
Definition: APICommand.hh:8
bool almost_zero(const T &val, double tol=TOL)
If T is not integral, use std::abs(val) < tol;.
Definition: CASM_math.hh:104
std::string irrational_to_tex_string(double val, int lim, int max_pow=2)
Definition: CASM_math.cc:199
bool valid_index(Index i)
Definition: definitions.cc:5
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
std::vector< std::string > parse_label_template(std::string const &_template)