CASM  1.1.0
A Clusters Approach to Statistical Mechanics
MappedProperties.cc
Go to the documentation of this file.
2 
3 #include <utility>
4 
7 
8 namespace CASM {
9 /*
10 const std::string traits<ScoreMappedProperties::Method>::name = "method";
11 
12 
13 const std::multimap<ScoreMappedProperties::Method, std::vector<std::string> >
14 traits<ScoreMappedProperties::Method>::strval = {
15  {ScoreMappedProperties::Method::deformation_cost, {"deformation_cost"} },
16  {ScoreMappedProperties::Method::minimum, {"minimum"} },
17  {ScoreMappedProperties::Method::maximum, {"maximum"} },
18  {ScoreMappedProperties::Method::direct_selection, {"direct_selection"} }
19 };
20 */
21 /*
22 ENUM_IO_DEF(CASM::ScoreMappedProperties::Method)
23 */
24 
25 bool MappedProperties::has_scalar(std::string const &_name) const {
26  auto it = global.find(_name);
27 
28  return it != global.end() &&
29  ((it->second).rows() == 1 && (it->second).cols() == 1);
30 }
31 
32 double const &MappedProperties::scalar(std::string const &_name) const {
33  auto it = global.find(_name);
34  if (it == global.end()) {
35  throw std::invalid_argument("Invalid scalar property '" + _name + "'");
36  }
37  assert((it->second).rows() == 1 && (it->second).cols() == 1);
38  return (it->second)(0, 0);
39 }
40 
41 double &MappedProperties::scalar(std::string const &_name) {
42  auto it = global.find(_name);
43  if (it == global.end()) {
44  it = global
45  .emplace(std::make_pair(_name, Eigen::MatrixXd::Zero(1, 1).eval()))
46  .first;
47  }
48  assert((it->second).rows() == 1 && (it->second).cols() == 1);
49  return (it->second)(0, 0);
50 }
52  json.put_obj();
53  json["origin"] = obj.origin;
54  json["init"] = obj.init_config;
55  json["to"] = obj.to;
56  if (!obj.file_data.empty()) {
57  json["path"] = obj.file_data.path();
58  json["timestamp"] = obj.file_data.timestamp();
59  }
60  json["global"] = obj.global;
61  json["site"] = obj.site;
62 
63  return json;
64 }
65 
66 jsonParser const &from_json(MappedProperties &obj, const jsonParser &json) {
67  from_json(obj.origin, json["origin"]);
68  from_json(obj.init_config, json["init"]);
69  from_json(obj.to, json["to"]);
70  from_json(obj.global, json["global"]);
71  from_json(obj.site, json["site"]);
72  if (json.contains("path"))
73  obj.file_data = FileData(json["path"].get<std::string>(),
74  json["timestamp"].get<std::time_t>());
75  return json;
76 }
77 
79  double _lattice_weight)
80  : method(_m), name(std::move(_name)), lattice_weight(_lattice_weight) {
81  if (_m == Method::minimum || _m == Method::maximum) {
82  if (name.empty())
83  throw std::invalid_argument(
84  "Scoring method '" + method_name(_m) +
85  "' requires additional string field \"property\".");
86  } else if (_m == Method::direct_selection) {
87  if (name.empty())
88  throw std::invalid_argument(
89  "Scoring method '" + method_name(_m) +
90  "' requires additional string field \"name\".");
91  } else if (_m == Method::deformation_cost) {
92  if (lattice_weight < 0. || lattice_weight > 1.)
93  throw std::invalid_argument("Scoring method '" + method_name(_m) +
94  "' requires numerical parameter "
95  "\"lattice_weight\" between 0.0 and 1.0.");
96  }
97 }
98 
100 
102  : m_opt(_opt) {}
103 
105  switch (m_opt.method) {
106  case Method::minimum: {
107  return obj.scalar(m_opt.name);
108  }
109  case Method::maximum: {
110  return -obj.scalar(m_opt.name);
111  }
113  return obj.scalar("lattice_deformation_cost") * m_opt.lattice_weight +
114  obj.scalar("atomic_deformation_cost") *
115  (1.0 - m_opt.lattice_weight);
116  }
118  if (obj.to == m_opt.name) {
119  return 0.0;
120  } else {
121  return 1.0;
122  }
123  }
124  default: {
126  }
127  }
128 }
129 
131  switch (m_opt.method) {
132  case Method::minimum:
133  case Method::maximum:
134  return obj.global.count(m_opt.name);
136  return obj.has_scalar("lattice_deformation_cost") &&
137  obj.has_scalar("atomic_deformation_cost");
139  return true;
140  default:
141  return false;
142  }
143 }
144 
146  return m_opt.method == B.m_opt.method && m_opt.name == B.m_opt.name;
147 }
148 
150  return !(*this == B);
151 }
152 
154  switch (score.option().method) {
156  json["method"] = "minimum";
157  json["property"] = score.option().name;
158  break;
160  json["method"] = "maximum";
161  json["property"] = score.option().name;
162  break;
164  json["method"] = "deformation_cost";
165  break;
167  json["method"] = "direct_selection";
168  json["name"] = score.option().name;
169  break;
170  }
171 
172  return json;
173 }
174 
176  const jsonParser &json) {
178  if (json.contains("method")) {
179  std::string method = json["method"].get<std::string>();
180  if (method == "minimum") {
182  opt.name = json["property"].get<std::string>();
183  } else if (method == "maximum") {
185  opt.name = json["property"].get<std::string>();
186  } else if (method == "deformation_cost") {
188  opt.name = "";
189  } else if (method == "direct_selection") {
191  opt.name = json["name"].get<std::string>();
192  }
193  }
195  return json;
196 }
197 
198 /*
199  const jsonParser &ScoreMappedProperties::params() const {
200  return m_params;
201  }*/
202 
203 /*
204  jsonParser &to_json(const ScoreMappedProperties &score, jsonParser &json) {
205  json = score.params();
206  return json;
207  }
208 
209  void from_json(ScoreMappedProperties &score, const jsonParser &json) {
210  score = ScoreMappedProperties(json);
211  }
212 */
213 } // namespace CASM
Interface class to check and/or store path and last_write_time of file on disk. Used to determine if ...
Definition: FileData.hh:9
std::time_t timestamp() const
return stored timestamp. Timestamp corresponds to last_write_time of file timestamp defaults to 0 if ...
Definition: FileData.hh:28
bool empty() const
Returns true if path is empty.
Definition: FileData.hh:36
std::string const & path() const
return stored path
Definition: FileData.hh:24
bool operator==(const ScoreMappedProperties &B) const
bool operator!=(const ScoreMappedProperties &B) const
bool validate(const MappedProperties &obj) const
ScoreMappedProperties(Option _opt=Option(Method::minimum, "energy"))
Default uses minimum energy.
static std::string method_name(Method m)
double operator()(const MappedProperties &obj) const
bool contains(const std::string &name) const
Return true if JSON object contains 'name'.
Definition: jsonParser.cc:601
jsonParser & put_obj()
Puts new empty JSON object.
Definition: jsonParser.hh:354
T get(Args &&... args) const
Get data from json, using one of several alternatives.
Definition: jsonParser.hh:716
GenericDatumFormatter< double, Result > score()
Main CASM namespace.
Definition: APICommand.hh:8
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
GenericDatumFormatter< std::string, DataObject > name()
void from_json(ClexDescription &desc, const jsonParser &json)
T max(const T &A, const T &B)
Definition: CASM_math.hh:95
Definition: stream_io.hh:24
double const & scalar(std::string const &_name) const
std::map< std::string, Eigen::MatrixXd > site
bool has_scalar(std::string const &_name) const
std::map< std::string, Eigen::MatrixXd > global
Option(Method _method=Method::minimum, std::string _name="energy")
Method method
Method for scoring.
std::string name
Property name or configname used for scoring.