CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
ConfigIOStrucScore.cc
Go to the documentation of this file.
1 #include <functional>
2 #include "casm/external/boost.hh"
6 #include "casm/clex/ConfigIO.hh"
9 
10 namespace CASM {
11 
12  namespace ConfigIO {
13  bool StrucScore::parse_args(const std::string &args) {
14  std::vector<std::string> splt_vec;
15  double _lattice_weight(0.5);
16  bool already_initialized = !m_prim_path.empty();
17  int pushed_args = 0;
18  boost::split(splt_vec, args, boost::is_any_of(", "), boost::token_compress_on);
19  if(splt_vec.size() < 2 || splt_vec.size() > 4) {
20  throw std::runtime_error("Attempted to initialize format tag " + name()
21  + " with " + std::to_string(splt_vec.size()) + " arguments ("
22  + args + "). You must provide at least 2 arguments, but no more than 4.\n");
23  return false;
24  }
25  if(!m_prim_path.empty() && fs::path(splt_vec[0]) != m_prim_path)
26  return false;
27  if(m_prim_path.empty()) {
28  m_prim_path = splt_vec[0];
29  if(!fs::exists(m_prim_path)) {
30  throw std::runtime_error("Attempted to initialize format tag " + name()
31  + " invalid file path '" + fs::absolute(m_prim_path).string() + "'. File does not exist.\n");
32  }
33 
35  }
36  for(Index i = 1; i < splt_vec.size(); ++i) {
37  if(splt_vec[i] != "basis_score" && splt_vec[i] != "lattice_score" && splt_vec[i] != "total_score") {
38  try {
39  _lattice_weight = std::stod(splt_vec[i]);
40  }
41  catch(...) {
42  throw std::runtime_error("Attempted to initialize format tag " + name()
43  + " with invalid argument '" + splt_vec[i] + "'. Valid arguments are [ basis_score | lattice_score | total_score ]\n");
44  }
45  if(already_initialized && !almost_equal(_lattice_weight, m_configmapper.lattice_weight())) {
46  for(; pushed_args > 0; pushed_args--)
47  m_prop_names.pop_back();
48  return false;
49  }
50  }
51  else {
52  m_prop_names.push_back(splt_vec[i]);
53  ++pushed_args;
54  }
55  }
56  m_configmapper = ConfigMapper(*m_altprimclex, _lattice_weight);
57  return true;
58  }
59 
60  //****************************************************************************************
61 
62  bool StrucScore::validate(const Configuration &_config) const {
63  return fs::exists(_config.calc_properties_path());
64  }
65 
66  //****************************************************************************************
67 
68  std::vector<std::string> StrucScore::col_header(const Configuration &_tmplt) const {
69  std::vector<std::string> col;
70  for(Index i = 0; i < m_prop_names.size(); i++) {
71  std::stringstream t_ss;
72  t_ss << " " << name() << '(' << m_prim_path.string() << ',' << m_prop_names[i] << ',' << m_configmapper.lattice_weight() << ')';
73  col.push_back(t_ss.str());
74  }
75  return col;
76  }
77 
78 
79  //****************************************************************************************
80 
81  std::string StrucScore::short_header(const Configuration &_tmplt) const {
82  std::stringstream t_ss;
83  t_ss << name() << '(' << m_prim_path.string();
84  for(Index i = 0; i < m_prop_names.size(); i++)
85  t_ss << ',' << m_prop_names[i];
86  t_ss << ',' << m_configmapper.lattice_weight() << ')';
87  return t_ss.str();
88  }
89 
90  //****************************************************************************************
92  std::vector<double> result_vec;
93 
94  BasicStructure<Site> relaxed_struc;
95  ConfigDoF mapped_configdof;
96  Lattice mapped_lat;
97 
98  auto lambda = [&](const std::vector<double> &result_vec) {
99  Eigen::VectorXd res = Eigen::VectorXd::Zero(result_vec.size());
100  for(Index i = 0; i < res.size(); ++i) {
101  res(i) = result_vec[i];
102  }
103  return res;
104  };
105 
106  from_json(simple_json(relaxed_struc, "relaxed_"), jsonParser(_config.calc_properties_path()));
107 
108  if(!m_configmapper.struc_to_configdof(relaxed_struc, mapped_configdof, mapped_lat)) {
109  for(Index i = 0; i < m_prop_names.size(); i++) {
110  result_vec.push_back(1e9);
111  }
112 
113  return lambda(result_vec);
114  }
115  for(Index i = 0; i < m_prop_names.size(); i++) {
116  if(m_prop_names[i] == "basis_score")
117  result_vec.push_back(ConfigMapping::basis_cost(mapped_configdof, relaxed_struc.basis.size()));
118  else if(m_prop_names[i] == "lattice_score")
119  result_vec.push_back(ConfigMapping::strain_cost(relaxed_struc.lattice(), mapped_configdof, relaxed_struc.basis.size()));
120  else if(m_prop_names[i] == "total_score") {
121  double sc = ConfigMapping::strain_cost(relaxed_struc.lattice(), mapped_configdof, relaxed_struc.basis.size());
122 
123  double bc = ConfigMapping::basis_cost(mapped_configdof, relaxed_struc.basis.size());
124 
125  double w = m_configmapper.lattice_weight();
126  result_vec.push_back(w * sc + (1.0 - w)*bc);
127  }
128  }
129 
130  return lambda(result_vec);
131  }
132  /*
133  // ****************************************************************************************
134  void StrucScore::inject(const Configuration &_config, DataStream &_stream, Index) const {
135  if(!validate(_config)) {
136  _stream << DataStream::failbit << std::vector<double>(m_prop_names.size(), NAN);
137  }
138  else {
139  _stream << _evaluate(_config);
140  }
141  }
142 
143  // ****************************************************************************************
144 
145  void StrucScore::print(const Configuration &_config, std::ostream &_stream, Index) const {
146  _stream.flags(std::ios::showpoint | std::ios::fixed | std::ios::right);
147  _stream.precision(8);
148 
149  if(!validate(_config)) {
150  for(auto it = m_prop_names.cbegin(); it != m_prop_names.cend(); ++it)
151  _stream << " unknown";
152  }
153 
154  else {
155  std::vector<double> result_vec(_evaluate(_config));
156  for(auto it = result_vec.cbegin(); it != result_vec.cend(); ++it)
157  _stream << " " << *it;
158  }
159 
160  }
161 
162  // ****************************************************************************************
163 
164  jsonParser &StrucScore::to_json(const Configuration &_config, jsonParser &json)const {
165  if(validate(_config))
166  json = _evaluate(_config);
167 
168  return json;
169  }
170  */
171  }
172 }
173 
const std::string & name() const
Returns a name for the formatter, which becomes the tag used for parsing.
void from_json(ClexDescription &desc, const jsonParser &json)
Index size() const
Definition: Array.hh:145
bool struc_to_configdof(const BasicStructure< Site > &_struc, ConfigDoF &mapped_configdof, Lattice &mapped_lat) const
Low-level routine to map a structure onto a ConfigDof.
Structure specifies the lattice and atomic basis of a crystal.
Definition: Structure.hh:29
fs::path calc_properties_path() const
Main CASM namespace.
Definition: complete.cpp:8
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: EnumIO.hh:83
const Lattice & lattice() const
Eigen::VectorXd evaluate(const Configuration &_config) const override
bool validate(const Configuration &_config) const override
bool parse_args(const std::string &args) override
Default implementation calls _parse_index_expression.
EigenIndex Index
For long integer indexing:
A container class for the different degrees of freedom a Configuration might have.
Definition: ConfigDoF.hh:27
double lattice_weight() const
std::vector< std::string > m_prop_names
Array< CoordType > basis
Lattice vectors that specifies periodicity of the crystal.
std::string short_header(const Configuration &_config) const override
Eigen::VectorXd VectorXd
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:52
SimpleJSonSiteStructure< true > simple_json(const BasicStructure< Site > &struc, const std::string &prefix)
Definition: jsonStruc.hh:110
double basis_cost(const ConfigDoF &_dof, Index Nsites)
Calculate the basis cost function of a ConfigDoF as the mean-square displacement of its atoms...
std::unique_ptr< PrimClex > m_altprimclex
std::vector< std::string > col_header(const Configuration &_config) const override
bool almost_equal(const GenericCluster< CoordType > &LHS, const GenericCluster< CoordType > &RHS, double tol)
A Configuration represents the values of all degrees of freedom in a Supercell.
double strain_cost(const Lattice &relaxed_lat, const ConfigDoF &_dof, Index Nsites)
Calculate the strain cost function of a ConfigDoF using LatticeMap::calc_strain_cost() ...