-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathCalculable.cc
More file actions
300 lines (266 loc) · 10 KB
/
Calculable.cc
File metadata and controls
300 lines (266 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "casm/clex/Calculable.hh"
#include <algorithm>
#include <boost/filesystem.hpp>
#include "casm/app/ProjectSettings.hh"
#include "casm/clex/PrimClex_impl.hh"
#include "casm/database/DatabaseTypes_impl.hh"
#include "casm/database/PropertiesDatabase.hh"
namespace CASM {
/// \brief Return MappedProperties for requested calctype
///
/// - If nothing calculated, returns empty MappedProperties object
template <typename _Base>
MappedProperties const &Calculable<_Base>::calc_properties(
std::string calctype) const {
if (calctype.empty() && derived().has_primclex()) {
calctype = derived().primclex().settings().default_clex().calctype;
}
auto it = m_calc_properties_map.find(calctype);
if (it == m_calc_properties_map.end()) {
_refresh_calc_properties(calctype);
it = m_calc_properties_map.find(calctype);
}
return it->second;
}
template <typename _Base>
void Calculable<_Base>::set_calc_properties(const MappedProperties &_prop,
std::string calctype) {
if (calctype.empty()) {
if (!derived().has_primclex()) {
m_calc_properties_map.clear();
return;
}
calctype = derived().primclex().settings().default_clex().calctype;
}
m_calc_properties_map[calctype] = _prop;
}
template <typename _Base>
void Calculable<_Base>::refresh_calc_properties(std::string calctype) {
static_cast<const Calculable<_Base> *>(this)->_refresh_calc_properties(
calctype);
}
template <typename _Base>
const jsonParser &Calculable<_Base>::source() const {
return m_source;
}
template <typename _Base>
void Calculable<_Base>::set_source(const jsonParser &source) {
if (source.is_null() || source.size() == 0) {
m_source.put_array();
} else if (!source.is_array()) {
m_source.put_array();
m_source.push_back(source);
} else {
m_source = source;
}
}
template <typename _Base>
void Calculable<_Base>::push_back_source(const jsonParser &source) {
if (!m_source.is_array()) {
m_source.put_array();
}
if (source.is_null() || source.size() == 0) {
return;
}
if (!source.is_array()) {
// check if the new source is already listed, if it is do nothing
for (int i = 0; i < m_source.size(); i++) {
if (m_source[i] == source) return;
}
// else, add the new source
m_source.push_back(source);
} else {
// check all new sources, if already listed skip, if the any of the new
// sources is already listed, if it is do nothing
for (int s = 0; s < source.size(); s++) {
bool found = false;
for (int i = 0; i < m_source.size(); i++) {
if (m_source[i] == source[s]) {
found = true;
break;
}
}
if (!found) {
// else, add the new source
m_source.push_back(source[s]);
}
}
}
}
/// Call in _Base any time DoF may be modified
template <typename _Base>
void Calculable<_Base>::_modify_dof() {
this->clear_name();
m_calc_properties_map.clear();
if (cache_updated()) {
cache_clear();
}
m_source.put_null();
}
template <typename _Base>
void Calculable<_Base>::_refresh_calc_properties(std::string calctype) const {
if (!derived().has_primclex()) {
return;
}
const PrimClex &primclex = derived().primclex();
if (calctype.empty()) {
calctype = primclex.settings().default_clex().calctype;
}
const auto &db = primclex.const_db_props<MostDerived>(calctype);
auto it = db.find_via_to(this->name());
if (it != db.end()) {
m_calc_properties_map[calctype] = *it;
} else {
m_calc_properties_map[calctype] = MappedProperties();
}
}
/// \brief Return true if all required properties have been been calculated for
/// the configuration in the calctype specified (current calctype if none
/// specified)
template <typename ConfigType>
bool is_calculated(const ConfigType &config, std::string calctype) {
auto const &settings = config.primclex().settings();
if (calctype == "") {
calctype = settings.default_clex().calctype;
}
const auto &props =
settings.required_properties(traits<ConfigType>::name, calctype);
return is_calculated(config.calc_properties(calctype), props);
}
template <typename ConfigType>
void reset_properties(ConfigType &config) {
config.set_calc_properties(MappedProperties(), "");
}
/// \brief Status of calculation.
/// A calculation that has been run successfully will be marked 'complete'.
/// Mapped or imported configurations may have 'is_calculated = 1' without
/// 'calc_status = complete'.
template <typename ConfigType>
std::string calc_status(const ConfigType &config, std::string calctype) {
if (calctype == "") {
calctype = config.primclex().settings().default_clex().calctype;
}
fs::path p = calc_status_path(config, calctype);
if (fs::exists(p)) {
jsonParser json(p);
if (json.contains("status")) return json["status"].get<std::string>();
}
return ("not_submitted");
}
// \brief Reason for calculation failure.
template <typename ConfigType>
std::string failure_type(const ConfigType &config, std::string calctype) {
if (calctype == "") {
calctype = config.primclex().settings().default_clex().calctype;
}
fs::path p = calc_status_path(config, calctype);
if (fs::exists(p)) {
jsonParser json(p);
if (json.contains("failure_type"))
return json["failure_type"].get<std::string>();
}
return ("none");
}
template <typename ConfigType>
bool has_calc_status(const ConfigType &config, std::string calctype) {
if (calctype == "") {
calctype = config.primclex().settings().default_clex().calctype;
}
return !calc_status(config, calctype).empty();
}
template <typename ConfigType>
bool has_failure_type(const ConfigType &config, std::string calctype) {
if (calctype == "") {
calctype = config.primclex().settings().default_clex().calctype;
}
return !failure_type(config, calctype).empty();
}
template <typename ConfigType>
std::string calc_properties_path(const ConfigType &config,
std::string calctype) {
if (calctype == "") {
calctype = config.primclex().settings().default_clex().calctype;
}
return calc_properties_path(config.primclex(), config.name(), calctype);
}
template <typename ConfigType>
std::string pos_path(const ConfigType &config) {
return pos_path(config.primclex(), config.name());
}
template <typename ConfigType>
std::string calc_status_path(const ConfigType &config, std::string calctype) {
if (calctype == "") {
calctype = config.primclex().settings().default_clex().calctype;
}
return calc_status_path(config.primclex(), config.name(), calctype);
}
/// \brief Return true if all required properties are included in the JSON
bool is_calculated(const MappedProperties &calc_properties,
const std::vector<std::string> &required_properties) {
return std::all_of(required_properties.begin(), required_properties.end(),
[&](const std::string &key) {
return (calc_properties.global.count(key) ||
calc_properties.site.count(key));
});
}
std::string calc_properties_path(const PrimClex &primclex,
const std::string &configname,
std::string calctype) {
if (calctype == "") {
calctype = primclex.settings().default_clex().calctype;
}
return primclex.dir().calculated_properties(configname, calctype).string();
}
std::string pos_path(const PrimClex &primclex, const std::string &configname) {
return primclex.dir().POS(configname).string();
}
std::string calc_status_path(const PrimClex &primclex,
const std::string &configname,
std::string calctype) {
if (calctype == "") {
calctype = primclex.settings().default_clex().calctype;
}
if (configname[0] == 'd') {
jsonParser calcjson;
std::vector<std::string> name;
boost::split(name, configname, boost::is_any_of("/"),
boost::token_compress_on);
if (fs::exists(primclex.dir().configuration_calc_settings_dir(configname,
calctype) /
"calc.json")) {
calcjson.read(
primclex.dir().configuration_calc_dir(configname, calctype) /
"calc.json");
} else if (fs::exists(primclex.dir().supercell_calc_settings_dir(
name[0] + name[1] + name[2], calctype) /
"calc.json")) {
calcjson.read(primclex.dir().supercell_calc_settings_dir(
name[0] + name[1] + name[2], calctype) /
"calc.json");
} else {
calcjson.read(primclex.dir().calc_settings_dir(calctype) / "calc.json");
}
int n_images;
calcjson.get_else<int>(n_images, "n_images", 0);
fs::path tmp = primclex.dir().calc_status(configname, calctype);
return (tmp.parent_path() / ("/N_images_" + std::to_string(n_images)) /
tmp.filename())
.string();
}
return primclex.dir().calc_status(configname, calctype).string();
}
#define INST_ConfigType(r, data, type) \
template bool is_calculated(const type &config, std::string calctype); \
template void reset_properties(type &config); \
template std::string calc_status(const type &_config, std::string calctype); \
template std::string failure_type(const type &config, std::string calctype); \
template bool has_calc_status(const type &config, std::string calctype); \
template bool has_failure_type(const type &config, std::string calctype); \
template std::string calc_properties_path(const type &config, \
std::string calctype); \
template std::string pos_path(const type &config); \
template std::string calc_status_path(const type &config, \
std::string calctype); \
template class Calculable<CRTPBase<type>>;
BOOST_PP_SEQ_FOR_EACH(INST_ConfigType, _, CASM_DB_CONFIG_TYPES)
} // namespace CASM