CASM  1.1.0
A Clusters Approach to Statistical Mechanics
Calculable.cc
Go to the documentation of this file.
2 
3 #include <algorithm>
4 #include <boost/filesystem.hpp>
5 
10 
11 namespace CASM {
12 
16 template <typename _Base>
18  std::string calctype) const {
19  if (calctype.empty() && derived().has_primclex()) {
20  calctype = derived().primclex().settings().default_clex().calctype;
21  }
22  auto it = m_calc_properties_map.find(calctype);
23  if (it == m_calc_properties_map.end()) {
24  _refresh_calc_properties(calctype);
25  it = m_calc_properties_map.find(calctype);
26  }
27 
28  return it->second;
29 }
30 
31 template <typename _Base>
33  std::string calctype) {
34  if (calctype.empty()) {
35  if (!derived().has_primclex()) {
36  m_calc_properties_map.clear();
37  return;
38  }
39  calctype = derived().primclex().settings().default_clex().calctype;
40  }
41  m_calc_properties_map[calctype] = _prop;
42 }
43 
44 template <typename _Base>
46  static_cast<const Calculable<_Base> *>(this)->_refresh_calc_properties(
47  calctype);
48 }
49 
50 template <typename _Base>
52  return m_source;
53 }
54 
55 template <typename _Base>
57  if (source.is_null() || source.size() == 0) {
58  m_source.put_array();
59  } else if (!source.is_array()) {
60  m_source.put_array();
61  m_source.push_back(source);
62  } else {
63  m_source = source;
64  }
65 }
66 
67 template <typename _Base>
69  if (source.is_null() || source.size() == 0) {
70  return;
71  }
72  if (!source.is_array()) {
73  // check if the new source is already listed, if it is do nothing
74  for (int i = 0; i < m_source.size(); i++) {
75  if (m_source[i] == source) return;
76  }
77 
78  // else, add the new source
79  m_source.push_back(source);
80  } else {
81  // check all new sources, if already listed skip, if the any of the new
82  // sources is already listed, if it is do nothing
83 
84  for (int s = 0; s < source.size(); s++) {
85  bool found = false;
86 
87  for (int i = 0; i < m_source.size(); i++) {
88  if (m_source[i] == source[s]) {
89  found = true;
90  break;
91  }
92  }
93 
94  if (!found) {
95  // else, add the new source
96  m_source.push_back(source[s]);
97  }
98  }
99  }
100 }
101 
103 template <typename _Base>
105  this->clear_name();
106  m_calc_properties_map.clear();
107 
108  if (cache_updated()) {
109  cache_clear();
110  }
111 
112  m_source.put_null();
113 }
114 
115 template <typename _Base>
117  if (!derived().has_primclex()) {
118  return;
119  }
120  const PrimClex &primclex = derived().primclex();
121  const auto &db = primclex.const_db_props<MostDerived>(calctype);
122  if (calctype.empty()) {
124  }
125  auto it = db.find_via_to(this->name());
126  if (it != db.end()) {
127  m_calc_properties_map[calctype] = *it;
128  } else {
129  m_calc_properties_map[calctype] = MappedProperties();
130  }
131 }
132 
136 template <typename ConfigType>
137 bool is_calculated(const ConfigType &config, std::string calctype) {
138  auto const &settings = config.primclex().settings();
139  if (calctype == "") {
140  calctype = settings.default_clex().calctype;
141  }
142  const auto &props =
143  settings.required_properties(traits<ConfigType>::name, calctype);
144  return is_calculated(config.calc_properties(calctype), props);
145 }
146 template <typename ConfigType>
147 void reset_properties(ConfigType &config) {
148  config.set_calc_properties(MappedProperties(), "");
149 }
150 
152 template <typename ConfigType>
153 std::string calc_status(const ConfigType &config, std::string calctype) {
154  if (calctype == "") {
155  calctype = config.primclex().settings().default_clex().calctype;
156  }
157  fs::path p = calc_status_path(config, calctype);
158  if (fs::exists(p)) {
159  jsonParser json(p);
160  if (json.contains("status")) return json["status"].get<std::string>();
161  }
162  return ("not_submitted");
163 }
164 
165 // \brief Reason for calculation failure.
166 template <typename ConfigType>
167 std::string failure_type(const ConfigType &config, std::string calctype) {
168  if (calctype == "") {
169  calctype = config.primclex().settings().default_clex().calctype;
170  }
171  fs::path p = calc_status_path(config, calctype);
172  if (fs::exists(p)) {
173  jsonParser json(p);
174  if (json.contains("failure_type"))
175  return json["failure_type"].get<std::string>();
176  }
177  return ("none");
178 }
179 
180 template <typename ConfigType>
181 bool has_calc_status(const ConfigType &config, std::string calctype) {
182  if (calctype == "") {
183  calctype = config.primclex().settings().default_clex().calctype;
184  }
185  return !calc_status(config, calctype).empty();
186 }
187 
188 template <typename ConfigType>
189 bool has_failure_type(const ConfigType &config, std::string calctype) {
190  if (calctype == "") {
191  calctype = config.primclex().settings().default_clex().calctype;
192  }
193  return !failure_type(config, calctype).empty();
194 }
195 
196 template <typename ConfigType>
197 std::string calc_properties_path(const ConfigType &config,
198  std::string calctype) {
199  if (calctype == "") {
200  calctype = config.primclex().settings().default_clex().calctype;
201  }
202  return calc_properties_path(config.primclex(), config.name(), calctype);
203 }
204 
205 template <typename ConfigType>
206 std::string pos_path(const ConfigType &config) {
207  return pos_path(config.primclex(), config.name());
208 }
209 
210 template <typename ConfigType>
211 std::string calc_status_path(const ConfigType &config, std::string calctype) {
212  if (calctype == "") {
213  calctype = config.primclex().settings().default_clex().calctype;
214  }
215  return calc_status_path(config.primclex(), config.name(), calctype);
216 }
217 
219 bool is_calculated(const MappedProperties &calc_properties,
220  const std::vector<std::string> &required_properties) {
221  return std::all_of(required_properties.begin(), required_properties.end(),
222  [&](const std::string &key) {
223  return (calc_properties.global.count(key) ||
224  calc_properties.site.count(key));
225  });
226 }
227 
229  const std::string &configname,
230  std::string calctype) {
231  if (calctype == "") {
233  }
235 }
236 
237 std::string pos_path(const PrimClex &primclex, const std::string &configname) {
238  return primclex.dir().POS(configname).string();
239 }
240 
241 std::string calc_status_path(const PrimClex &primclex,
242  const std::string &configname,
243  std::string calctype) {
244  if (calctype == "") {
246  }
247 
248  if (configname[0] == 'd') {
249  jsonParser calcjson;
250  std::vector<std::string> name;
251  boost::split(name, configname, boost::is_any_of("/"),
252  boost::token_compress_on);
254  calctype) /
255  "calc.json")) {
256  calcjson.read(
258  "calc.json");
259  } else if (fs::exists(primclex.dir().supercell_calc_settings_dir(
260  name[0] + name[1] + name[2], calctype) /
261  "calc.json")) {
263  name[0] + name[1] + name[2], calctype) /
264  "calc.json");
265  } else {
266  calcjson.read(primclex.dir().calc_settings_dir(calctype) / "calc.json");
267  }
268  int n_images;
269  calcjson.get_else<int>(n_images, "n_images", 0);
270  fs::path tmp = primclex.dir().calc_status(configname, calctype);
271  return (tmp.parent_path() / ("/N_images_" + std::to_string(n_images)) /
272  tmp.filename())
273  .string();
274  }
275  return primclex.dir().calc_status(configname, calctype).string();
276 }
277 
278 #define INST_ConfigType(r, data, type) \
279  template bool is_calculated(const type &config, std::string calctype); \
280  template void reset_properties(type &config); \
281  template std::string calc_status(const type &_config, std::string calctype); \
282  template std::string failure_type(const type &config, std::string calctype); \
283  template bool has_calc_status(const type &config, std::string calctype); \
284  template bool has_failure_type(const type &config, std::string calctype); \
285  template std::string calc_properties_path(const type &config, \
286  std::string calctype); \
287  template std::string pos_path(const type &config); \
288  template std::string calc_status_path(const type &config, \
289  std::string calctype); \
290  template class Calculable<CRTPBase<type>>;
291 
292 BOOST_PP_SEQ_FOR_EACH(INST_ConfigType, _, CASM_DB_CONFIG_TYPES)
293 
294 } // namespace CASM
#define INST_ConfigType(r, data, type)
Definition: Calculable.cc:278
std::set< std::string > & s
#define CASM_DB_CONFIG_TYPES
void set_calc_properties(const MappedProperties &_prop, std::string calctype="")
Definition: Calculable.cc:32
MappedProperties const & calc_properties(std::string calctype="") const
Return MappedProperties for requested calctype.
Definition: Calculable.cc:17
void _modify_dof()
Call in MostDerived any time DoF may be modified.
Definition: Calculable.cc:104
Base::MostDerived MostDerived
Definition: Calculable.hh:26
void set_source(const jsonParser &source)
Definition: Calculable.cc:56
void _refresh_calc_properties(std::string calctype="") const
grabs properties from the indicated calctype and adds info to calc_properties_map
Definition: Calculable.cc:116
void push_back_source(const jsonParser &source)
Definition: Calculable.cc:68
void refresh_calc_properties(std::string calctype="")
grabs properties from the indicated calctype and adds info to calc_properties_map
Definition: Calculable.cc:45
const jsonParser & source() const
Definition: Calculable.cc:51
fs::path configuration_calc_settings_dir(std::string configname, std::string calctype) const
Return calculation settings directory path, for configuration specific settings.
fs::path calculated_properties(std::string configname, std::string calctype) const
Return properties.calc.json file path.
fs::path configuration_calc_dir(std::string configname, std::string calctype) const
Return directory containing properties.calc.json.
fs::path calc_settings_dir(std::string calctype) const
Return calculation settings directory path, for global settings.
fs::path supercell_calc_settings_dir(std::string scelname, std::string calctype) const
Return calculation settings directory path, for supercell specific settings.
fs::path calc_status(std::string configname, std::string calctype) const
Return calculation status file path.
fs::path POS(std::string configname) const
Return path to standard POS file location.
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
ClexDescription const & default_clex() const
Get default ClexDescription.
bool contains(const std::string &name) const
Return true if JSON object contains 'name'.
Definition: jsonParser.cc:601
size_type size() const
Definition: jsonParser.cc:487
bool is_array() const
Check if array type.
Definition: jsonParser.cc:275
bool read(std::istream &stream)
Reads json from the stream.
Definition: jsonParser.cc:168
bool is_null() const
Check if null type.
Definition: jsonParser.cc:254
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: io_traits.hh:172
const DB::PropertiesDatabase & const_db_props(std::string calc_type) const
Definition: PrimClex.cc:317
ProjectSettings & settings()
Definition: PrimClex.cc:224
const DirectoryStructure & dir() const
Access DirectoryStructure object. Throw if not set.
Definition: PrimClex.cc:230
bool get_else(T &t, const std::string &key, const T &default_value, Args &&... args) const
Definition: jsonParser.hh:775
T get(Args &&... args) const
Get data from json, using one of several alternatives.
Definition: jsonParser.hh:716
ConfigIO::GenericConfigFormatter< jsonParser > config()
Definition: ConfigIO.cc:777
ConfigIO::GenericConfigFormatter< std::string > configname()
Constructs DataFormmaterDictionary containing all Configuration DatumFormatters.
Definition: ConfigIO.cc:563
Main CASM namespace.
Definition: APICommand.hh:8
std::string calc_properties_path(const ConfigType &config, std::string calctype="")
Definition: Calculable.cc:197
bool is_calculated(const ConfigType &config, std::string calctype="")
Return true if all required properties have been been calculated for the configuration.
Definition: Calculable.cc:137
std::string pos_path(const ConfigType &config, std::string calctype="")
void reset_properties(ConfigType &config)
Definition: Calculable.cc:147
std::string calc_status_path(const ConfigType &config, std::string calctype="")
Definition: Calculable.cc:211
GenericDatumFormatter< std::string, DataObject > name()
std::string failure_type(const ConfigType &config, std::string calctype="")
Definition: Calculable.cc:167
std::string calc_status(const ConfigType &_config, std::string calctype="")
Status of calculation.
Definition: Calculable.cc:153
bool has_calc_status(const ConfigType &config, std::string calctype="")
Definition: Calculable.cc:181
bool has_failure_type(const ConfigType &config, std::string calctype="")
Definition: Calculable.cc:189
PrimClex * primclex
Definition: settings.cc:135
pair_type calctype
Definition: settings.cc:143