CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
ConfigIOHull.hh
Go to the documentation of this file.
1 #ifndef CONFIGIOHULL_HH
2 #define CONFIGIOHULL_HH
3 
5 #include "casm/hull/Hull.hh"
7 #include "casm/clex/PrimClex.hh"
8 
9 namespace CASM {
10 
11  class Configuration;
12 
13  namespace ConfigIO {
14 
17 
20 
22  std::string default_hull_calculator();
23 
24 
29  template<typename ValueType>
30  class BaseHull: public BaseValueFormatter<ValueType, Configuration> {
31 
32  public:
33 
35  BaseHull(const std::string &_name,
36  const std::string &_desc,
37  const std::string &_default_selection = "MASTER",
38  const std::string &_default_composition_type = default_hull_calculator(),
39  const Hull::CalculatorOptions &_calculator_map = hull_calculator_options(),
40  double _singular_value_tol = 1e-8,
41  double _bottom_facet_tol = 1e-8);
42 
44  void init(const Configuration &_tmplt) const override;
45 
47  std::string short_header(const Configuration &_config) const override;
48 
50  bool parse_args(const std::string &args) override;
51 
52 
53  protected:
54 
56  const Hull &_hull() const;
57 
58  // for parse_args: determine energy type based on composition type
60 
61  // detect and avoid printing -0.0
62  constexpr static double m_dist_to_hull_tol = 1e-14;
63 
64  private:
65 
66  // tol used to detect zero singular values during principal component analysis preprocessing before hull calculation
68 
69  // tol used to detect which facets are on the bottom of the convex hull
71 
72  // the hull object
73  mutable std::shared_ptr<Hull> m_hull;
74 
75  // Parsed arguments
76  // -- what selection to use for constructing the hull
77  std::string m_selection;
78 
79  // Parsed arguments
80  // -- what composition to use for constructing the hull
81  // -- determines comp calculator and energy calculator, via m_calculator_map
82  std::string m_composition_type;
83 
84  // Prevent re-parsing args
86 
87  };
88 
104  class OnHull: public BaseHull<bool> {
105 
106  public:
107 
108  static const std::string Name;
109  static const std::string Desc;
110 
111 
113  OnHull();
114 
116  std::unique_ptr<OnHull> clone() const {
117  return std::unique_ptr<OnHull>(this->_clone());
118  }
119 
121  bool validate(const Configuration &_config) const override;
122 
124  bool evaluate(const Configuration &_config) const override;
125 
126  private:
127 
129  OnHull *_clone() const override {
130  return new OnHull(*this);
131  }
132  };
133 
145  class HullDist: public BaseHull<double> {
146 
147  public:
148 
149  static const std::string Name;
150  static const std::string Desc;
151 
152 
154  HullDist();
155 
157  std::unique_ptr<HullDist> clone() const {
158  return std::unique_ptr<HullDist>(this->_clone());
159  }
160 
162  bool validate(const Configuration &_config) const override;
163 
165  double evaluate(const Configuration &_config) const override;
166 
167  private:
168 
170  HullDist *_clone() const override {
171  return new HullDist(*this);
172  }
173  };
174 
175 
191  class OnClexHull: public BaseHull<bool> {
192 
193  public:
194 
195  static const std::string Name;
196  static const std::string Desc;
197 
198 
200  OnClexHull();
201 
203  std::unique_ptr<OnClexHull> clone() const {
204  return std::unique_ptr<OnClexHull>(this->_clone());
205  }
206 
208  virtual bool validate(const Configuration &_config) const override;
209 
211  virtual bool evaluate(const Configuration &_config) const override;
212 
213  private:
214 
216  OnClexHull *_clone() const override {
217  return new OnClexHull(*this);
218  }
219 
220  };
221 
233  class ClexHullDist: public BaseHull<double> {
234 
235  public:
236 
237  static const std::string Name;
238  static const std::string Desc;
239 
240 
242  ClexHullDist();
243 
245  std::unique_ptr<ClexHullDist> clone() const {
246  return std::unique_ptr<ClexHullDist>(this->_clone());
247  }
248 
250  virtual bool validate(const Configuration &_config) const override;
251 
253  virtual double evaluate(const Configuration &_config) const override;
254 
255  private:
256 
258  ClexHullDist *_clone() const override {
259  return new ClexHullDist(*this);
260  }
261 
262  };
263 
264 
265 
266 
267  // --- BaseHull Definitions -------------------
268 
280  template<typename ValueType>
281  BaseHull<ValueType>::BaseHull(const std::string &_name,
282  const std::string &_desc,
283  const std::string &_default_selection,
284  const std::string &_default_composition_type,
285  const Hull::CalculatorOptions &_calculator_map,
286  double _singular_value_tol,
287  double _bottom_facet_tol) :
288  BaseValueFormatter<ValueType, Configuration>(_name, _desc),
289  m_calculator_map(_calculator_map),
290  m_singular_value_tol(_singular_value_tol),
291  m_bottom_facet_tol(_bottom_facet_tol),
292  m_selection(_default_selection),
293  m_composition_type(_default_composition_type),
294  m_initialized(false) {}
295 
302  template<typename ValueType>
303  void BaseHull<ValueType>::init(const Configuration &_tmplt) const {
304 
305  ConstConfigSelection selection;
306  const PrimClex &primclex = _tmplt.get_primclex();
307 
308  if(m_selection == "ALL") {
309  selection = ConstConfigSelection(primclex);
310  for(auto it = primclex.config_cbegin(); it != primclex.config_cend(); ++it) {
311  selection.set_selected(it->name(), true);
312  }
313  }
314  else if(m_selection == "MASTER") {
315  selection = ConstConfigSelection(primclex);
316  }
317  else if(m_selection == "CALCULATED") {
318  selection = ConstConfigSelection(primclex);
319  for(auto it = primclex.config_cbegin(); it != primclex.config_cend(); ++it) {
320  selection.set_selected(it->name(), is_calculated(*it));
321  }
322 
323  }
324  else {
325  if(!fs::exists(m_selection)) {
326  throw std::runtime_error(
327  std::string("ERROR in $selection argument for '") + short_header(_tmplt) + "'." +
328  " Expected <filename>, 'ALL', 'CALCULATED', or 'MASTER' <--default" +
329  " No file named '" + m_selection + "'.");
330  }
331  selection = ConstConfigSelection(primclex, m_selection);
332  }
333 
334  // Hull typedefs:
335  //typedef std::pair<notstd::cloneable_ptr<CompCalculator>,
336  // notstd::cloneable_ptr<EnergyCalculator> > CalculatorPair;
337  //typedef std::map<std::string, CalculatorPair> CalculatorOptions;
338 
339  m_calculator_map.find(m_composition_type)->second.first->init(_tmplt);
340  m_calculator_map.find(m_composition_type)->second.second->init(_tmplt);
341 
342  m_hull = std::make_shared<Hull>(selection,
343  *m_calculator_map.find(m_composition_type)->second.first,
344  *m_calculator_map.find(m_composition_type)->second.second,
345  m_singular_value_tol,
346  m_bottom_facet_tol);
347 
348  }
349 
356  template<typename ValueType>
357  std::string BaseHull<ValueType>::short_header(const Configuration &_config) const {
358  std::stringstream t_ss;
359  t_ss << this->name() << "(" << m_selection << "," << m_composition_type << ")";
360  return t_ss.str();
361  }
362 
383  template<typename ValueType>
384  bool BaseHull<ValueType>::parse_args(const std::string &args) {
385 
386  if(m_initialized) {
387  return false;
388  }
389 
390  std::vector<std::string> splt_vec;
391  boost::split(splt_vec, args, boost::is_any_of(","), boost::token_compress_on);
392 
393  if(splt_vec.size() > 4) {
394  throw std::runtime_error("Attempted to initialize format tag " + this->name()
395  + " with " + std::to_string(splt_vec.size()) + " arguments ("
396  + args + "), but only up to 4 arguments allowed.\n");
397  return false;
398  }
399 
400  while(splt_vec.size() < 4) {
401  splt_vec.push_back("");
402  }
403 
404  m_selection = splt_vec[0].empty() ? m_selection : splt_vec[0];
405  m_composition_type = splt_vec[1].empty() ? m_composition_type : splt_vec[1];
406  m_singular_value_tol = splt_vec[2].empty() ? m_singular_value_tol : std::stod(splt_vec[2]);
407  m_bottom_facet_tol = splt_vec[3].empty() ? m_bottom_facet_tol : std::stod(splt_vec[3]);
408  m_initialized = true;
409 
410  auto it = m_calculator_map.find(m_composition_type);
411  if(it == m_calculator_map.end()) {
412 
413  std::stringstream ss;
414  ss << "Composition type '" << m_composition_type << "' is not recognized. Options are: ";
415  for(auto it = m_calculator_map.begin(); it != m_calculator_map.end(); ++it) {
416  std::cerr << it->first << " ";
417  if(it != m_calculator_map.begin()) {
418  ss << ", ";
419  }
420  ss << "'" << it->first << "'";
421  }
422  throw std::runtime_error(ss.str());
423 
424  }
425 
426  // prevent combining formatters
427  return false;
428  }
429 
431  template<typename ValueType>
433  return *m_hull;
434  }
435 
436 
437 
438  }
439 }
440 
441 #endif
Generate and inspect the convex hull generated from a selection of Configurations.
Definition: Hull.hh:15
static const std::string Name
PrimClex & get_primclex() const
Get the PrimClex for this Configuration.
void init(const Configuration &_tmplt) const override
Calculates the convex hull.
static const std::string Name
Returns the distance, in eV, of a configuration's clex(formation_energy_per_atom) above the predicted...
PrimClex * primclex
Definition: settings.cc:101
iterator find(const std::string &configname)
std::unique_ptr< ClexHullDist > clone() const
Clone.
HullDist * _clone() const override
Clone.
virtual bool validate(const Configuration &_config) const override
Validate that the Configuration has a cluster expanded formation energy per species.
Main CASM namespace.
Definition: complete.cpp:8
ClexHullDist * _clone() const override
Clone.
std::map< std::string, CalculatorPair > CalculatorOptions
Definition: Hull.hh:23
Hull::CalculatorOptions clex_hull_calculator_options()
Returns a map containing default clex hull calculators.
Definition: ConfigIOHull.cc:20
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: EnumIO.hh:83
Base class for creating scalar DatumFormatter.
static const std::string Desc
bool parse_args(const std::string &args) override
Determine the selection to use to generate the hull.
double evaluate(const Configuration &_config) const override
Return the distance to the hull.
Definition: ConfigIOHull.cc:99
std::shared_ptr< Hull > m_hull
Definition: ConfigIOHull.hh:73
OnHull * _clone() const override
Clone.
HullDist()
Constructor.
Definition: ConfigIOHull.cc:90
static const std::string Desc
Returns the distance, in eV, of a configuration's formation_energy_per_atom above the convex hull...
std::unique_ptr< OnHull > clone() const
Clone.
void set_selected(const std::string &configname, bool is_selected)
static const std::string Name
bool validate(const Configuration &_config) const override
Validate that the Configuration has a formation energy per species.
Definition: ConfigIOHull.cc:56
OnClexHull * _clone() const override
Clone.
config_const_iterator config_cend() const
const Configuration iterator: end
Definition: PrimClex.cc:407
virtual bool validate(const Configuration &_config) const override
Validate that the Configuration has a cluster expanded formation energy per species.
bool evaluate(const Configuration &_config) const override
Check if the Configuration is a hull vertex.
Definition: ConfigIOHull.cc:64
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:52
Returns a boolean indicating if a Configuration is a convex hull vertex.
config_const_iterator config_cbegin() const
const Configuration iterator: begin
Definition: PrimClex.cc:399
std::string default_hull_calculator()
Returns "atom_frac".
Definition: ConfigIOHull.cc:31
static constexpr double m_dist_to_hull_tol
Definition: ConfigIOHull.hh:62
ConfigIO::GenericConfigFormatter< bool > is_calculated()
Definition: ConfigIO.cc:467
static const std::string Desc
BaseHull(const std::string &_name, const std::string &_desc, const std::string &_default_selection="MASTER", const std::string &_default_composition_type=default_hull_calculator(), const Hull::CalculatorOptions &_calculator_map=hull_calculator_options(), double _singular_value_tol=1e-8, double _bottom_facet_tol=1e-8)
Constructor.
std::unique_ptr< HullDist > clone() const
Clone.
const Hull & _hull() const
const Access the Hull object
static const std::string Name
bool validate(const Configuration &_config) const override
Validate that the Configuration has a formation energy per species.
Definition: ConfigIOHull.cc:94
Hull::CalculatorOptions hull_calculator_options()
Returns a map containing default hull calculators.
Definition: ConfigIOHull.cc:12
ConfigSelection< true > ConstConfigSelection
virtual double evaluate(const Configuration &_config) const override
Return the distance to the hull.
Hull::CalculatorOptions m_calculator_map
Definition: ConfigIOHull.hh:59
std::string m_composition_type
Definition: ConfigIOHull.hh:82
Returns a boolean indicating if a Configuration is a predicted convex hull vertex.
static const std::string Desc
OnHull()
Constructor.
Definition: ConfigIOHull.cc:52
Base class for hull info formatters.
Definition: ConfigIOHull.hh:30
virtual bool evaluate(const Configuration &_config) const override
Check if the Configuration is a hull vertex.
std::string short_header(const Configuration &_config) const override
column header to use
A Configuration represents the values of all degrees of freedom in a Supercell.
std::unique_ptr< OnClexHull > clone() const
Clone.