CASM  1.1.0
A Clusters Approach to Statistical Mechanics
DirectoryStructure.cc
Go to the documentation of this file.
2 
3 #include <boost/algorithm/string/classification.hpp>
4 #include <boost/algorithm/string/split.hpp>
5 #include <boost/filesystem.hpp>
6 
7 #include "casm/casm_io/Log.hh"
10 namespace CASM {
11 
14 fs::path find_casmroot(const fs::path &cwd) {
15  fs::path dir(cwd);
16  fs::path casmroot(".casm");
17 
18  while (!dir.empty()) {
19  // if cwd contains ".casm", return cwd
20  if (fs::is_directory(dir / casmroot)) return dir;
21  dir = dir.parent_path();
22  }
23  return dir;
24 };
25 
29 fs::path relative_casmroot(const fs::path &cwd) {
30  fs::path dir(cwd);
31  fs::path casmroot = find_casmroot(cwd);
32  fs::path relpath("");
33 
34  while (dir != casmroot) {
35  dir = dir.parent_path();
36  relpath /= "..";
37  }
38  return relpath;
39 };
40 
42 void recurs_rm_files(fs::path p, bool dry_run, Log &log) {
43  if (!fs::exists(p)) {
44  return;
45  }
46 
47  auto it = fs::directory_iterator(p);
48  auto end = fs::directory_iterator();
49 
50  for (; it != end; ++it) {
51  if (fs::is_regular_file(*it)) {
52  log << "rm " << *it << std::endl;
53  if (!dry_run) {
54  fs::remove(*it);
55  }
56  } else {
57  recurs_rm_files(*it, dry_run, log);
58  log << "rm " << *it << std::endl;
59  }
60  }
61 
62  log << "rm " << p << std::endl;
63  if (!dry_run) {
64  fs::remove(p);
65  }
66 }
67 
68 namespace {
70 Index recurs_cp_files_impl(const fs::path &from_dir, const fs::path &to_dir,
71  bool dry_run, Index count, Log &log) {
72  auto it = fs::directory_iterator(from_dir);
73  auto end = fs::directory_iterator();
74  for (; it != end; ++it) {
75  if (fs::is_regular_file(*it)) {
76  log << "cp " << *it << " " << to_dir << std::endl;
77  count++;
78  if (!dry_run) {
79  fs::copy_file(*it, to_dir / it->path().filename());
80  }
81  } else {
82  fs::path new_to_dir = to_dir / it->path().filename();
83  if (!dry_run) {
84  fs::create_directories(new_to_dir);
85  }
86  count = recurs_cp_files_impl(*it, new_to_dir, dry_run, count, log);
87  }
88  }
89  return count;
90 }
91 } // namespace
92 
94 Index recurs_cp_files(const fs::path &from_dir, const fs::path &to_dir,
95  bool dry_run, Log &log) {
96  Index count = 0;
97  recurs_cp_files_impl(from_dir, to_dir, dry_run, count, log);
98  return count;
99 }
100 
102  _init(fs::absolute(_root));
103 }
104 
105 // ** Query filesystem **
106 
109 std::vector<std::string> DirectoryStructure::all_bset() const {
110  return _all_settings("bset", m_root / m_bset_dir);
111 }
112 
115 std::vector<std::string> DirectoryStructure::all_calctype() const {
116  return _all_settings("calctype", m_root / m_calc_dir / m_set_dir);
117 }
118 
121 std::vector<std::string> DirectoryStructure::all_ref(
122  std::string calctype) const {
123  return _all_settings("ref", calc_settings_dir(calctype));
124 }
125 
128 std::vector<std::string> DirectoryStructure::all_property() const {
129  return _all_settings("clex", m_root / m_clex_dir);
130 }
131 
133 std::vector<std::string> DirectoryStructure::all_eci(std::string property,
134  std::string calctype,
135  std::string ref,
136  std::string bset) const {
137  return _all_settings("eci", m_root / m_clex_dir / _property(property) /
138  _calctype(calctype) / _ref(ref) /
139  _bset(bset));
140 }
141 
142 // ** File and Directory paths **
143 
144 // -- Project directory --------
145 
147 fs::path DirectoryStructure::root_dir() const { return m_root; }
148 
150 fs::path DirectoryStructure::prim() const { return casm_dir() / "prim.json"; }
151 
152 // -- Hidden .casm directory --------
153 
155 fs::path DirectoryStructure::casm_dir() const { return m_root / m_casm_dir; }
156 
159  return m_root / m_casm_dir / "project_settings.json";
160 }
161 
164  return m_root / m_casm_dir / "scel_list.json";
165 }
166 
169  return m_root / m_casm_dir / "config_list.json";
170 }
171 
174  return m_root / m_casm_dir / "enumerators";
175 }
176 
178 template <typename DataObject>
180  return m_root / m_casm_dir / "query" / traits<DataObject>::name;
181 }
182 
185  return m_root / m_casm_dir / "dof";
186 }
187 
190  return m_root / m_casm_dir / "symrep_builder";
191 }
192 
193 template <typename DataObject>
195  return query_plugins<DataObject>() / "master_selection";
196 }
197 
199 template <typename DataObject>
200 fs::path DirectoryStructure::aliases() const {
201  return query_plugins<DataObject>() / "aliases.json";
202 }
203 
204 // -- Symmetry --------
205 
207 fs::path DirectoryStructure::symmetry_dir() const { return m_root / m_sym_dir; }
208 
211 fs::path DirectoryStructure::symmetry_dir(std::string configname) const {
212  return m_root / m_sym_dir / "analysis" / configname;
213 }
214 
217  return m_root / m_sym_dir / "lattice_point_group.json";
218 }
219 
222  return m_root / m_sym_dir / "factor_group.json";
223 }
224 
227  return m_root / m_sym_dir / "crystal_point_group.json";
228 }
229 
230 // -- Basis sets --------
231 
233 fs::path DirectoryStructure::bset_dir(std::string bset) const {
234  return m_root / m_bset_dir / _bset(bset);
235 }
236 
239 std::vector<fs::path> DirectoryStructure::bset_data(std::string project_name,
240  std::string bset) const {
241  return {clust(bset), basis(bset), clexulator_src(project_name, bset),
242  clexulator_o(project_name, bset), clexulator_so(project_name, bset)};
243 }
244 
246 fs::path DirectoryStructure::bspecs(std::string bset) const {
247  return bset_dir(bset) / "bspecs.json";
248 }
249 
250 // \brief Returns path to the clust.json file
251 fs::path DirectoryStructure::clust(std::string bset) const {
252  return bset_dir(bset) / "clust.json";
253 }
254 
255 // \brief Returns path to the basis.json file
256 fs::path DirectoryStructure::basis(std::string bset) const {
257  return bset_dir(bset) / "basis.json";
258 }
259 
261 fs::path DirectoryStructure::clexulator_dir(std::string bset) const {
262  return bset_dir(bset);
263 }
264 
266 fs::path DirectoryStructure::clexulator_src(std::string project_name,
267  std::string bset) const {
268  return bset_dir(bset) / (project_name + "_Clexulator.cc");
269 }
270 
272 fs::path DirectoryStructure::clexulator_o(std::string project_name,
273  std::string bset) const {
274  return bset_dir(bset) / (project_name + "_Clexulator.o");
275 }
276 
278 fs::path DirectoryStructure::clexulator_so(std::string project_name,
279  std::string bset) const {
280  return bset_dir(bset) / (project_name + "_Clexulator.so");
281 }
282 
284 fs::path DirectoryStructure::eci_in(std::string bset) const {
285  return bset_dir(bset) / "eci.in";
286 }
287 
289 fs::path DirectoryStructure::corr_in(std::string bset) const {
290  return bset_dir(bset) / "corr.in";
291 }
292 
293 // -- Calculations and reference --------
294 
297  return m_root / m_calc_dir;
298 }
299 
301 fs::path DirectoryStructure::SCEL() const {
302  return m_root / m_calc_dir / "SCEL";
303 }
304 
308 fs::path DirectoryStructure::supercell_dir(std::string scelname) const {
309  return m_root / m_calc_dir / scelname;
310 }
311 
316 fs::path DirectoryStructure::LAT(std::string scelname) const {
317  return m_root / m_calc_dir / scelname / "LAT";
318 }
319 
323  return m_root / m_calc_dir / configname;
324 }
325 
327 fs::path DirectoryStructure::POS(std::string configname) const {
328  return configuration_dir(configname) / "POS";
329 }
330 
332 fs::path DirectoryStructure::config_json(std::string configname) const {
333  return configuration_dir(configname) / "config.json";
334 }
335 
337 fs::path DirectoryStructure::structure_json(std::string configname) const {
338  return configuration_dir(configname) / "structure.json";
339 }
340 
342 fs::path DirectoryStructure::calc_settings_dir(std::string calctype) const {
344 }
345 
351  std::string scelname, std::string calctype) const {
353 }
354 
358  std::string configname, std::string calctype) const {
360 }
361 
364  std::string configname, std::string calctype) const {
366 }
367 
370  std::string calctype) const {
371  return configuration_calc_dir(configname, calctype) / "properties.calc.json";
372 }
373 
376  std::string calctype) const {
377  return configuration_calc_dir(configname, calctype) / "status.json";
378 }
379 
383  std::string ref) const {
384  return calc_settings_dir(calctype) / _ref(ref);
385 }
386 
389  return casm_dir() / "composition_axes.json";
390 }
391 
394  std::string ref) const {
395  return ref_dir(calctype, ref) / "chemical_reference.json";
396 }
397 
398 // -- Cluster expansions --------
399 
401 fs::path DirectoryStructure::clex_dir(std::string property) const {
402  return m_root / m_clex_dir / _property(property);
403 }
404 
406 fs::path DirectoryStructure::eci_dir(std::string property, std::string calctype,
407  std::string ref, std::string bset,
408  std::string eci) const {
409  return clex_dir(property) / _calctype(calctype) / _ref(ref) / _bset(bset) /
410  _eci(eci);
411 }
412 
414 fs::path DirectoryStructure::eci(std::string property, std::string calctype,
415  std::string ref, std::string bset,
416  std::string eci) const {
417  return eci_dir(property, calctype, ref, bset, eci) / "eci.json";
418 }
419 // -- Reports ---------------------------------------
421  return m_root / m_reports_dir;
422 }
423 
424 // -- other maybe temporary --------------------------
425 
427 fs::path DirectoryStructure::CSPECS(std::string bset) const {
428  return bset_dir(bset) / "CSPECS";
429 }
430 
431 // \brief Returns path to the clust.json file
432 fs::path DirectoryStructure::FCLUST(std::string bset) const {
433  return bset_dir(bset) / "FCLUST.json";
434 }
435 
436 // -- deprecated ------------------------------------
437 
440  return m_root / m_casm_dir / "query_alias.json";
441 }
442 
443 // ** Add directories for additional project data **
444 
446  return fs::create_directory(casm_dir());
447 }
448 
450  return fs::create_directory(symmetry_dir());
451 }
452 
454  return fs::create_directory(reports_dir());
455 }
456 
457 bool DirectoryStructure::new_bset_dir(std::string bset) const {
458  return fs::create_directories(bset_dir(bset));
459 }
460 
462  return fs::create_directories(clex_dir(property));
463 }
464 
466  return fs::create_directories(calc_settings_dir(calctype));
467 }
468 
470  std::string scelname, std::string calctype) const {
471  return fs::create_directories(
473 }
474 
476  std::string configname, std::string calctype) const {
477  return fs::create_directories(
479 }
480 
482  std::string ref) const {
483  return fs::create_directories(ref_dir(calctype, ref));
484 }
485 
486 bool DirectoryStructure::new_eci_dir(std::string property, std::string calctype,
487  std::string ref, std::string bset,
488  std::string eci) const {
489  return fs::create_directories(eci_dir(property, calctype, ref, bset, eci));
490 }
491 
492 void DirectoryStructure::delete_bset_data(std::string project_name,
493  std::string bset) const {
494  fs::remove(clust(bset));
495  fs::remove(basis(bset));
496  fs::remove(clexulator_src(project_name, bset));
497  fs::remove(clexulator_o(project_name, bset));
498  fs::remove(clexulator_so(project_name, bset));
499 }
500 
501 void DirectoryStructure::delete_clexulator(std::string project_name,
502  std::string bset) const {
503  fs::remove(clexulator_src(project_name, bset));
504  fs::remove(clexulator_o(project_name, bset));
505  fs::remove(clexulator_so(project_name, bset));
506 }
507 
509  std::string project_name) const {
510  auto all_bset = this->all_bset();
511  for (auto it = all_bset.begin(); it != all_bset.end(); ++it) {
512  delete_clexulator(project_name, *it);
513  }
514 }
515 
516 std::string DirectoryStructure::_bset(std::string bset) const {
517  return std::string("bset.") + bset;
518 }
519 
520 std::string DirectoryStructure::_calctype(std::string calctype) const {
521  return std::string("calctype.") + calctype;
522 }
523 
524 std::string DirectoryStructure::_ref(std::string ref) const {
525  return std::string("ref.") + ref;
526 }
527 
528 std::string DirectoryStructure::_property(std::string property) const {
529  return std::string("clex.") + property;
530 }
531 
532 std::string DirectoryStructure::_eci(std::string eci) const {
533  return std::string("eci.") + eci;
534 }
535 
536 std::string DirectoryStructure::_ref_state(int index) const {
537  return std::string("properties.ref_state.") + std::to_string(index) + ".json";
538 }
539 
540 void DirectoryStructure::_init(const fs::path &_root) {
541  m_root = _root;
542  m_casm_dir = ".casm";
543  m_bset_dir = "basis_sets";
544  m_calc_dir = "training_data";
545  m_set_dir = "settings";
546  m_sym_dir = "symmetry";
547  m_clex_dir = "cluster_expansions";
548  m_reports_dir = "reports";
549 }
550 
553 std::vector<std::string> DirectoryStructure::_all_settings(
554  std::string pattern, fs::path location) const {
555  std::vector<std::string> all;
556  std::string dir;
557  pattern += ".";
558 
559  // get all
560  if (!fs::exists(location)) {
561  return all;
562  }
563  fs::directory_iterator it(location);
564  fs::directory_iterator end_it;
565  for (; it != end_it; ++it) {
566  if (fs::is_directory(*it)) {
567  dir = it->path().filename().string();
568  if (dir.substr(0, pattern.size()) == pattern) {
569  all.push_back(dir.substr(pattern.size(), dir.size()));
570  }
571  }
572  }
573 
574  std::sort(all.begin(), all.end());
575 
576  return all;
577 }
578 
580  if (dir.root_dir().empty()) {
581  throw std::runtime_error("Error accessing project: No root directory set.");
582  }
583 }
584 
585 void throw_if_no_basis_set_specs(std::string basis_set_name,
586  DirectoryStructure const &dir) {
588  auto basis_set_specs_path = dir.bspecs(basis_set_name);
589  if (!fs::exists(basis_set_specs_path)) {
590  std::stringstream ss;
591  ss << "Error accessing bset." << basis_set_name
592  << ": Does not exist. "
593  "Checked for bspecs at: "
594  << basis_set_specs_path.string();
595  throw std::runtime_error(ss.str());
596  }
597 }
598 
599 void throw_if_no_clexulator_src(std::string project_name,
600  std::string basis_set_name,
601  DirectoryStructure const &dir) {
602  throw_if_no_basis_set_specs(basis_set_name, dir);
603  fs::path clexulator_src_path =
604  dir.clexulator_src(project_name, basis_set_name);
605  if (!fs::exists(clexulator_src_path)) {
606  std::stringstream ss;
607  ss << "Error accessing bset." << basis_set_name << ": "
608  << clexulator_src_path
609  << " does not exist. The basis set should be regenerated.";
610  throw std::runtime_error(ss.str());
611  }
612 }
613 
614 } // namespace CASM
615 
617 
618 // explicit instantiations
619 #define INST_DirectoryStructure_all(r, data, type) \
620  template fs::path DirectoryStructure::query_plugins<type>() const; \
621  template fs::path DirectoryStructure::master_selection<type>() const; \
622  template fs::path DirectoryStructure::aliases<type>() const;
623 
624 namespace CASM {
625 BOOST_PP_SEQ_FOR_EACH(INST_DirectoryStructure_all, _, CASM_DB_TYPES)
626 }
Index count
#define CASM_DB_TYPES
#define INST_DirectoryStructure_all(r, data, type)
Specification of CASM project directory structure.
fs::path prim() const
Return prim.json path.
fs::path root_dir() const
Return casm project directory path.
std::string _property(std::string property) const
fs::path casm_dir() const
Return hidden .casm dir path.
std::vector< std::string > all_eci(std::string property, std::string calctype, std::string ref, std::string bset) const
Check filesystem directory structure and return list of all eci names.
fs::path configuration_calc_settings_dir(std::string configname, std::string calctype) const
Return calculation settings directory path, for configuration specific settings.
fs::path SCEL() const
Return SCEL path.
fs::path calculated_properties(std::string configname, std::string calctype) const
Return properties.calc.json file path.
fs::path eci_in(std::string bset) const
Returns path to eci.in, in bset directory.
fs::path ref_dir(std::string calctype, std::string ref) const
Return calculation reference settings directory path, for global settings.
void delete_bset_data(std::string project_name, std::string bset) const
Delete Basis set generated files.
bool new_supercell_calc_settings_dir(std::string scelname, std::string calctype) const
Add calculation settings directory path, for supercell specific settings.
fs::path crystal_point_group() const
Return crystal_point_group.json path.
fs::path eci_dir(std::string property, std::string calctype, std::string ref, std::string bset, std::string eci) const
Returns path to eci directory.
void _init(const fs::path &_root)
fs::path symmetry_dir() const
Return symmetry directory path.
fs::path symrep_builder_plugins() const
Return SymrepBuilder plugin dir.
bool new_calc_settings_dir(std::string calctype) const
Add calculation settings directory path.
fs::path config_list() const
Return master config_list.json file path.
fs::path LAT(std::string scelname) const
Return supercell LAT file path.
fs::path project_settings() const
Return project_settings.json path.
fs::path configuration_calc_dir(std::string configname, std::string calctype) const
Return directory containing properties.calc.json.
fs::path clust(std::string bset) const
fs::path structure_json(std::string configname) const
Return path to standard structure.json file location.
std::string _ref_state(int index) const
bool new_bset_dir(std::string bset) const
Add a basis set directory.
fs::path enumerator_plugins() const
Return enumerators plugin dir.
fs::path reports_dir() const
Returns path to reports directory.
fs::path config_json(std::string configname) const
Return path to standard config.json file location.
fs::path training_data() const
Return 'training_data' directorty path.
bool new_configuration_calc_settings_dir(std::string configname, std::string calctype) const
fs::path configuration_dir(std::string configname) const
Return configuration directory path.
fs::path supercell_dir(std::string scelname) const
Return supercell directory path.
fs::path FCLUST(std::string bset) const
std::string _bset(std::string bset) const
fs::path query_alias() const
Query aliases file (deprecated: now stored in project_settings.json)
std::string _calctype(std::string calctype) const
bool new_clex_dir(std::string property) const
Add a cluster expansion directory.
fs::path clexulator_o(std::string project_name, std::string bset) const
Returns path to clexulator o file.
fs::path chemical_reference(std::string calctype, std::string ref) const
Return chemical reference file path.
fs::path corr_in(std::string bset) const
Returns path to corr.in, in bset directory.
void delete_clexulator(std::string project_name, std::string bset) const
Delete Clexulator files.
std::vector< std::string > all_calctype() const
Check filesystem directory structure and return list of all calctype names.
fs::path query_plugins() const
Return enumerators plugin dir.
void delete_all_clexulators(std::string project_name) const
Delete files for all Clexulators.
bool new_casm_dir() const
Create new project data directory.
fs::path CSPECS(std::string bset) const
Return cluster specs (CSPECS) file path.
std::vector< std::string > all_bset() const
Check filesystem directory structure and return list of all basis set names.
fs::path basis(std::string bset) const
std::string _eci(std::string eci) const
fs::path clex_dir(std::string property) const
Returns path to eci directory.
fs::path eci(std::string property, std::string calctype, std::string ref, std::string bset, std::string eci) const
Returns path to eci.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 lattice_point_group() const
Return lattice_point_group.json path.
fs::path bset_dir(std::string bset) const
Return path to directory contain basis set info.
std::vector< fs::path > bset_data(std::string project_name, std::string bset) const
Return paths where bset generated data is stored (excludes bspecs.json)
fs::path master_selection() const
fs::path scel_list() const
Return master scel_list.json path.
std::vector< std::string > _all_settings(std::string pattern, fs::path location) const
Find all directories at 'location' that match 'pattern.something' and return a std::vector of the 'so...
bool new_reports_dir() const
Create new reports directory.
fs::path clexulator_src(std::string project_name, std::string bset) const
Returns path to clexulator source file.
fs::path aliases() const
File containing DataObject name aliases (not query function aliases)
fs::path composition_axes() const
Return composition axes file path.
fs::path POS(std::string configname) const
Return path to standard POS file location.
bool new_ref_dir(std::string calctype, std::string ref) const
Add a ref directory.
bool new_symmetry_dir() const
Create new symmetry directory.
std::string _ref(std::string ref) const
fs::path dof_plugins() const
Return DoF plugin dir.
fs::path factor_group() const
Return factor_group.json path.
bool new_eci_dir(std::string property, std::string calctype, std::string ref, std::string bset, std::string eci) const
Add an eci directory.
fs::path bspecs(std::string bset) const
Return basis function specs (bspecs.json) file path.
std::vector< std::string > all_ref(std::string calctype) const
Check filesystem directory structure and return list of all ref names for a given calctype.
std::vector< std::string > all_property() const
Check filesystem directory structure and return list of all property names.
fs::path clexulator_dir(std::string bset) const
Returns path to directory containing clexulator files.
fs::path clexulator_so(std::string project_name, std::string bset) const
Returns path to clexulator so file.
Definition: Log.hh:48
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: io_traits.hh:172
std::string scelname(const Structure &prim, const Lattice &superlat)
Make supercell name name [deprecated].
Definition: Supercell.cc:497
ConfigIO::GenericConfigFormatter< std::string > configname()
Constructs DataFormmaterDictionary containing all Configuration DatumFormatters.
Definition: ConfigIO.cc:563
Main CASM namespace.
Definition: APICommand.hh:8
void throw_if_no_basis_set_specs(std::string basis_set_name, DirectoryStructure const &dir)
Log & log()
Definition: Log.hh:424
void throw_if_no_clexulator_src(std::string project_name, std::string basis_set_name, DirectoryStructure const &dir)
void throw_if_no_root_dir(DirectoryStructure const &dir)
fs::path relative_casmroot(const fs::path &cwd)
fs::path find_casmroot(const fs::path &cwd)
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 ref
Definition: settings.cc:144
pair_type eci
Definition: settings.cc:146
pair_type calctype
Definition: settings.cc:143
pair_type property
Definition: settings.cc:142
DirectoryStructure const & dir
Definition: settings.cc:136
pair_type bset
Definition: settings.cc:145