CASM  1.1.0
A Clusters Approach to Statistical Mechanics
ConfigData.cc
Go to the documentation of this file.
1 #include <boost/filesystem.hpp>
2 
8 
9 namespace CASM {
10 namespace Local {
21 static std::string _resolve_properties_path(std::string pos_path,
22  PrimClex const &_pclex) {
23  // check 1: is a JSON file
24  if (pos_path.find(".json") != std::string::npos ||
25  pos_path.find(".JSON") != std::string::npos) {
26  return pos_path;
27  }
28 
29  // check 2: /path/to/POS -> /path/to/calctype.current/properties.calc.json
30  {
31  fs::path dft_path = pos_path;
32  dft_path.remove_filename();
33  (dft_path /= ("calctype." + _pclex.settings().default_clex().calctype)) /=
34  "properties.calc.json";
35  if (fs::exists(dft_path)) {
36  return dft_path.string();
37  }
38  }
39 
40  // check 3: /path/to/POS -> /path/to/properties.calc.json
41  {
42  fs::path dft_path = pos_path;
43  dft_path.remove_filename();
44  dft_path /= "properties.calc.json";
45  if (fs::exists(dft_path)) {
46  return dft_path.string();
47  }
48  }
49 
50  // not found, return empty path
51  return "";
52 }
53 } // namespace Local
54 
55 namespace DB {
56 
58 std::string create_report_dir(std::string report_dir) {
59  Index i = 0;
60  while (fs::exists(report_dir + "." + std::to_string(i))) {
61  ++i;
62  }
63  report_dir += ("." + std::to_string(i));
64  fs::create_directory(report_dir);
65  return report_dir;
66 }
67 
68 namespace ConfigIO {
69 
72  "initial_path", "", [](const Result &res) { return res.pos_path; });
73 }
74 
77  "final_path", "",
78  [](const Result &res) { return res.properties.file_data.path(); });
79 }
80 
83  "fail_msg", "", [](const Result &res) { return res.fail_msg; });
84 }
85 
88  "data_origin", "",
89  [](const Result &res) { return res.properties.origin; });
90 }
91 
94  "to_configname", "", [](const Result &res) { return res.properties.to; });
95 }
96 
99  "has_data", "", [](const Result &res) { return res.has_data; });
100 }
101 
104  "has_complete_data", "", [](const Result &res) { return res.has_data; });
105 }
106 
109  "preexisting_data", "",
110  [&](const Result &res) { return res.import_data.preexisting; });
111 }
112 
115  "import_data", "", [&](const Result &res) {
116  return res.import_data.is_best; // && it->second.import_data;
117  });
118 }
119 
122  "import_additional_files", "",
123  [&](const Result &res) { return res.import_data.copy_more; });
124 }
125 
128  "lattice_deformation_cost", "",
129  [](const Result &res) {
130  return res.properties.scalar("lattice_deformation_cost");
131  },
132  [](const Result &res) {
133  return res.properties.has_scalar("lattice_deformation_cost");
134  });
135 }
136 
139  "atomic_deformation_cost", "",
140  [](const Result &res) {
141  return res.properties.scalar("atomic_deformation_cost");
142  },
143  [](const Result &res) {
144  return res.properties.has_scalar("atomic_deformation_cost");
145  });
146 }
147 
150  "energy", "",
151  [](const Result &res) { return res.properties.scalar("energy"); },
152  [](const Result &res) {
153  return res.properties.has_scalar("energy");
154  });
155 }
156 
158  PropertiesDatabase &db_props) {
160  "score", "",
161  [&](const Result &res) { return db_props.score(res.properties); },
162  [&](const Result &res) { return res.has_data; });
163 }
164 
166  PropertiesDatabase &db_props) {
168  "best_score", "",
169  [&](const Result &res) { return db_props.best_score(res.properties.to); },
170  [&](const Result &res) {
171  return db_props.find_via_to(res.properties.to) != db_props.end();
172  });
173 }
174 
176  PropertiesDatabase &db_props) {
178  "is_best", "",
179  [&](const Result &res) {
180  return res.properties.origin ==
181  db_props.find_via_to(res.properties.to)->origin;
182  },
183  [&](const Result &res) {
184  return db_props.find_via_to(res.properties.to) != db_props.end();
185  });
186 }
187 
191  "selected", "", [](const Result &res) { return false; });
192 }
193 
197  "is_new_config", "", [](const Result &res) { return res.is_new_config; });
198 }
199 
202  PropertiesDatabase &db_props) {
203  default_update_formatters(dict, db_props);
204 
206 }
207 
210  PropertiesDatabase &db_props) {
211  dict.insert(initial_path(), final_path(), fail_msg(),
212  // configname(),
215  atomic_deformation_cost(), energy(), score(db_props),
216  best_score(db_props), is_best(db_props), selected());
217 }
218 } // namespace ConfigIO
219 
220 // If pos_path can be used to resolve a properties.calc.json, return its path.
221 // Otherwise return pos_path
222 std::string ConfigData::resolve_struc_path(std::string pos_path,
223  PrimClex const &_pclex) {
224  std::string p = Local::_resolve_properties_path(pos_path, _pclex);
225  if (!p.empty()) pos_path = p;
226  return pos_path;
227 }
228 
231  return primclex().db<Supercell>();
232 }
233 
235 
237 std::string ConfigData::calc_dir(const std::string configname) const {
238  return primclex()
239  .dir()
241  primclex().settings().default_clex().calctype)
242  .string();
243 }
244 
247 bool ConfigData::has_existing_files(const std::string &to_configname) const {
248  fs::path p = calc_dir(to_configname);
249  if (!fs::exists(p)) {
250  return false;
251  }
252  return std::distance(fs::directory_iterator(p), fs::directory_iterator());
253 }
254 
257 bool ConfigData::has_existing_data(const std::string &to_configname) const {
258  return db_props().find_via_to(to_configname) != db_props().end();
259 }
260 
262  const std::string &configname) const {
264 }
265 
269 bool ConfigData::no_change(const std::string &configname) const {
271 
272  auto it = db_props().find_via_origin(tfile_data.path());
273  if (it != db_props().end()) {
274  return it->file_data == tfile_data;
275  }
276 
277  return !tfile_data.exists();
278 }
279 
282 void ConfigData::rm_files(const std::string &configname, bool dry_run) const {
283  fs::path p = calc_dir(configname);
284  if (!fs::exists(p)) {
285  return;
286  }
287  log().custom(std::string("Remove calculation files: ") + configname);
288  recurs_rm_files(p, dry_run, log());
289  log() << std::endl;
290 }
291 
307 void ConfigData::cp_files(ConfigIO::Result &res, bool dry_run,
308  bool copy_additional_files) const {
309  res.import_data.copy_data = false;
310  res.import_data.copy_more = false;
311 
312  fs::path p = calc_dir(res.properties.to);
313  if (!fs::exists(p)) {
314  if (!dry_run) {
315  fs::create_directories(p);
316  }
317  }
318 
319  fs::path origin_props_path = Local::_resolve_properties_path(
320  res.properties.file_data.path(), primclex());
321  if (origin_props_path.empty()) {
322  return;
323  }
324 
325  log().custom(std::string("Copy calculation files: ") + res.properties.to);
326  if (!copy_additional_files) {
327  log() << "cp " << origin_props_path << " " << p / "properties.calc.json"
328  << std::endl;
329  res.import_data.copy_data = true;
330  if (!dry_run) {
331  fs::copy_file(origin_props_path, p / "properties.calc.json");
332  }
333  } else {
334  Index count =
335  recurs_cp_files(origin_props_path.remove_filename(), p, dry_run, log());
336  if (count) {
337  res.import_data.copy_more = true;
338  }
339  }
340  res.properties.file_data = FileData((p / "properties.calc.json").string());
341  log() << std::endl;
342  return;
343 }
344 
345 } // namespace DB
346 } // namespace CASM
Index count
bool no_change(const std::string &configname) const
Check if 'properties.calc.json' file has not changed since last read.
Definition: ConfigData.cc:269
bool has_existing_data_or_files(const std::string &to_configname) const
Definition: ConfigData.cc:261
Database< Supercell > & db_supercell() const
Path to default calctype training_data directory for config.
Definition: ConfigData.cc:230
bool has_existing_files(const std::string &to_configname) const
Return true if there are existing files in the traning_data directory for a particular configuration.
Definition: ConfigData.cc:247
std::function< PropertiesDatabase &()> m_db_props_func
Definition: ConfigData.hh:192
PropertiesDatabase & db_props() const
Uses primclex().settings().default_clex().calctype.
Definition: ConfigData.cc:234
std::string calc_dir(const std::string configname) const
Path to default calctype training_data directory for config.
Definition: ConfigData.cc:237
const PrimClex & primclex() const
Definition: ConfigData.hh:152
static std::string resolve_struc_path(std::string pos_path, PrimClex const &_pclex)
Checks if pos_path can be used to resolve a properties.calc.json, and return its path Return path to ...
Definition: ConfigData.cc:222
bool has_existing_data(const std::string &to_configname) const
Return true if there are existing files in the traning_data directory for a particular configuration.
Definition: ConfigData.cc:257
void rm_files(const std::string &configname, bool dry_run) const
Remove existing files in the traning_data directory for a particular configuration.
Definition: ConfigData.cc:282
void cp_files(ConfigIO::Result &res, bool dry_run, bool copy_additional_files) const
Copy files in the same directory as properties.calc.json into the traning_data directory for a partic...
Definition: ConfigData.cc:307
double best_score(std::string to_configname) const
Best score of configurations that relaxed 'origin'->'to'.
virtual iterator end() const =0
End iterator over data entries.
double score(std::string origin) const
Score mapping 'origin'->'to'.
virtual iterator find_via_to(std::string to_configname) const =0
Return iterator to data entry that is the best mapping to specified config.
virtual iterator find_via_origin(std::string origin) const =0
Return iterator to data entry that is from the specified origin.
Parsing dictionary for constructing a DataFormatter<DataObject> object.
std::pair< iterator, bool > insert(const value_type &value)
Insert single value.
Definition: unique_map.hh:149
fs::path configuration_calc_dir(std::string configname, std::string calctype) const
Return directory containing properties.calc.json.
Interface class to check and/or store path and last_write_time of file on disk. Used to determine if ...
Definition: FileData.hh:9
bool exists() const
checks path for existing file. Does not alter stored timestamp
Definition: FileData.cc:9
std::string const & path() const
return stored path
Definition: FileData.hh:24
A DatumFormatter that returns a value of specified type, via functions that may be specified at runti...
void custom(const std::string &what)
Definition: Log.hh:139
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
ClexDescription const & default_clex() const
Get default ClexDescription.
Represents a supercell of the primitive parent crystal structure.
Definition: Supercell.hh:51
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: io_traits.hh:172
DB::Database< T > & db() const
Definition: PrimClex.cc:302
ProjectSettings & settings()
Definition: PrimClex.cc:224
const DirectoryStructure & dir() const
Access DirectoryStructure object. Throw if not set.
Definition: PrimClex.cc:230
ConfigIO::GenericConfigFormatter< std::string > configname()
Constructs DataFormmaterDictionary containing all Configuration DatumFormatters.
Definition: ConfigIO.cc:563
GenericDatumFormatter< std::string, Result > data_origin()
Definition: ConfigData.cc:86
GenericDatumFormatter< double, Result > lattice_deformation_cost()
Definition: ConfigData.cc:126
GenericDatumFormatter< bool, Result > import_additional_files()
Definition: ConfigData.cc:120
GenericDatumFormatter< std::string, Result > final_path()
Definition: ConfigData.cc:75
GenericDatumFormatter< double, Result > score()
GenericDatumFormatter< double, Result > best_score()
GenericDatumFormatter< bool, Result > has_data()
Definition: ConfigData.cc:97
GenericDatumFormatter< bool, Result > import_data()
Definition: ConfigData.cc:113
GenericDatumFormatter< double, Result > energy()
Definition: ConfigData.cc:148
GenericDatumFormatter< bool, Result > preexisting_data()
Definition: ConfigData.cc:107
GenericDatumFormatter< bool, Result > has_complete_data()
Definition: ConfigData.cc:102
GenericDatumFormatter< bool, Result > selected()
Gives a 'selected' column, set all to false.
Definition: ConfigData.cc:189
GenericDatumFormatter< bool, Result > is_new_config()
Gives a 'selected' column, set all to false.
Definition: ConfigData.cc:195
GenericDatumFormatter< std::string, Result > to_configname()
Definition: ConfigData.cc:92
GenericDatumFormatter< double, Result > atomic_deformation_cost()
Definition: ConfigData.cc:137
void default_import_formatters(DataFormatterDictionary< Result > &dict, PropertiesDatabase &db_props)
Insert default formatters to dictionary, for 'casm import'.
Definition: ConfigData.cc:201
GenericDatumFormatter< std::string, Result > initial_path()
Definition: ConfigData.cc:70
GenericDatumFormatter< std::string, Result > fail_msg()
Definition: ConfigData.cc:81
GenericDatumFormatter< bool, Result > is_best()
void default_update_formatters(DataFormatterDictionary< Result > &dict, PropertiesDatabase &db_props)
Insert default formatters to dictionary, for 'casm update'.
Definition: ConfigData.cc:209
std::string create_report_dir(std::string report_dir)
Create a new report directory to avoid overwriting existing results.
Definition: ConfigData.cc:58
static std::string _resolve_properties_path(std::string pos_path, PrimClex const &_pclex)
Return path to properties.calc.json that will be imported checking a couple possible locations relati...
Definition: ConfigData.cc:21
Main CASM namespace.
Definition: APICommand.hh:8
std::string calc_properties_path(const ConfigType &config, std::string calctype="")
Definition: Calculable.cc:197
Log & log()
Definition: Log.hh:424
std::string pos_path(const ConfigType &config, std::string calctype="")
void recurs_rm_files(fs::path p, bool dry_run, Log &log)
Remove files recursively.
Index recurs_cp_files(const fs::path &from_dir, const fs::path &to_dir, bool dry_run, Log &log)
Copy files recursively, and returns a count of copied files.
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
pair_type calctype
Definition: settings.cc:143
Data structure for mapping / import results.
Definition: ConfigData.hh:56
MappedProperties properties
Definition: ConfigData.hh:60
double const & scalar(std::string const &_name) const
bool has_scalar(std::string const &_name) const