CASM  1.1.0
A Clusters Approach to Statistical Mechanics
RemoveSupercell.cc
Go to the documentation of this file.
2 
4 #include "casm/app/rm.hh"
8 
9 namespace CASM {
10 namespace DB {
11 
12 Remove<Supercell>::Remove(const PrimClex &_primclex, std::string report_dir)
13  : m_primclex(_primclex), m_report_dir(report_dir) {}
14 
15 std::string Remove<Supercell>::desc() {
16  std::string res =
17  "Remove a supercell, including all enumerated configurations and "
18  "calculation results: \n\n"
19 
20  " 'casm remove --type scel' options: \n\n"
21 
22  " - Supercells to be erased can be specified with the --names and \n"
23  " --selection options.\n"
24  " - Use without additional options to remove all enumerated "
25  "configurations\n"
26  " that do not have any associated files or data for each specified "
27  "suprecell.\n"
28  " If no data or files, erase supercell.\n"
29  " - Use --data (-d) to remove all configuration data, but not "
30  "enumerated \n"
31  " configurations, for each specified supercell. \n"
32  " - Use --force (-f) to remove specified supercells including all data "
33  "and \n"
34  " enumerated configurations. \n"
35  " - Use --dry-run (-n) to do a \"dry-run\". \n\n";
36  return res;
37 }
38 
41  EraseScelConfigsBase(Remove<Supercell> &_remover, std::string _scelname,
42  bool _dry_run)
43  : primclex(_remover.primclex()),
44  remover(_remover),
45  scelname(_scelname),
46  dry_run(_dry_run),
47  remaining(0) {}
48 
49  template <typename T>
51  DB::Selection<T> selection(remover.primclex(), "NONE");
52  auto it = primclex.db<T>().scel_range(scelname).begin();
53  auto end = primclex.db<T>().scel_range(scelname).end();
54  for (; it != end; ++it) {
55  selection.data()[it.name()] = true;
56  }
57  return selection;
58  }
59 
60  template <typename T>
61  void count_remaining() {
62  if (!dry_run) {
63  remaining +=
64  boost::distance(remover.primclex().db<T>().scel_range(scelname));
65  } else {
68  auto it = primclex.db<T>().scel_range(scelname).begin();
69  auto end = primclex.db<T>().scel_range(scelname).end();
70  for (; it != end; ++it) {
71  if (data.has_existing_data_or_files(it.name())) {
72  remaining++;
73  }
74  }
75  }
76  }
77 
80  std::string scelname;
81  bool dry_run;
83 };
84 
88  EraseScelConfigs(Remove<Supercell> &_remover, std::string _scelname,
89  bool _dry_run)
90  : EraseScelConfigsBase(_remover, _scelname, _dry_run) {}
91 
92  template <typename T>
93  void eval() {
95  DB::Selection<T> selection = make_selection<T>();
96  f.erase(selection, dry_run);
97  count_remaining<T>();
98  }
99 };
100 
104  EraseDataScelConfigs(Remove<Supercell> &_remover, std::string _scelname,
105  bool _dry_run)
106  : EraseScelConfigsBase(_remover, _scelname, _dry_run) {}
107 
108  template <typename T>
109  void eval() {
111  DB::Selection<T> selection = make_selection<T>();
112  f.erase_data(selection, dry_run);
113  count_remaining<T>();
114  }
115 };
116 
120  EraseAllScelConfigs(Remove<Supercell> &_remover, std::string _scelname,
121  bool _dry_run)
122  : EraseScelConfigsBase(_remover, _scelname, _dry_run) {}
123 
124  template <typename T>
125  void eval() {
127  DB::Selection<T> selection = make_selection<T>();
128  f.erase_all(selection, dry_run);
129  count_remaining<T>();
130  }
131 };
132 
136  bool dry_run) {
137  bool did_erase = false;
138 
139  // call Remove<ConfigType>::erase for each ConfigType
140  auto it = selection.selected().begin();
141  auto end = selection.selected().end();
142  for (; it != end; ++it) {
143  EraseScelConfigs f(*this, it.name(), dry_run);
145 
146  // if no existing data or files, erase Supercell ...
147  if (!f.remaining) {
148  log() << "will erase " << it.name() << "\n";
149  if (!dry_run) {
150  primclex().db<Supercell>().erase(it.name());
151  did_erase = true;
152  }
153  } else {
154  log() << "skipping " << it.name() << ": has " << f.remaining
155  << " configurations remaining.\n";
156  }
157  }
158 
159  if (did_erase) {
160  primclex().db<Supercell>().commit();
161  }
162 }
163 
167  bool dry_run) {
168  // call Remove<ConfigType>::erase for each ConfigType
169  auto it = selection.selected().begin();
170  auto end = selection.selected().end();
171  for (; it != end; ++it) {
172  EraseDataScelConfigs f(*this, it.name(), dry_run);
174  }
175 }
176 
180  bool dry_run) {
181  // call Remove<ConfigType>::erase for each ConfigType
182  auto it = selection.selected().begin();
183  auto end = selection.selected().end();
184  for (; it != end; ++it) {
185  EraseAllScelConfigs f(*this, it.name(), dry_run);
187 
188  // if no existing data or files, erase Supercell ...
189  if (!f.remaining) {
190  log() << "will erase " << it.name() << "\n";
191  if (dry_run) {
192  primclex().db<Supercell>().erase(it.name());
193  }
194  } else {
195  log() << "unknown error: erase_all called, but " << it.name()
196  << " still has " << f.remaining << " configurations.\n";
197  log() << "stopping..." << std::endl;
198  return;
199  }
200  }
201 
202  primclex().db<Supercell>().commit();
203 }
204 
206  const Completer::RmOption &opt) {
207  // -- read selection --
209  for (const auto &name : opt.name_strs()) {
210  if (primclex.db<Supercell>().count(name)) {
211  selection.data()[name] = true;
212  } else {
213  std::stringstream msg;
214  msg << "Invalid Supercell name: " << name;
215  throw CASM::runtime_error(msg.str(), ERR_INVALID_ARG);
216  }
217  }
218 
219  // get remove report_dir, check if exists, and create new report_dir.i if
220  // necessary
221  fs::path report_dir = primclex.dir().root_dir() / "remove_report";
222  report_dir = create_report_dir(report_dir.string());
223 
224  // -- erase --
225  Remove<Supercell> f(primclex, report_dir.string());
226 
227  if (opt.force()) {
228  f.erase_all(selection, opt.dry_run());
229  } else if (opt.data()) {
230  f.erase_data(selection, opt.dry_run());
231  } else {
232  f.erase(selection, opt.dry_run());
233  }
234  return 0;
235 }
236 
238 
239 std::string Remove<Supercell>::report_dir() const { return m_report_dir; }
240 
241 } // namespace DB
242 } // namespace CASM
#define ERR_INVALID_ARG
Definition: errors.hh:7
const fs::path & selection_path() const
Returns the string corresponding to add_config_suboption()
Definition: Handlers.cc:338
const std::vector< std::string > & name_strs() const
Definition: Handlers.cc:721
bool data() const
Definition: rm.cc:21
bool force() const
Definition: rm.cc:19
bool has_existing_data_or_files(const std::string &to_configname) const
Definition: ConfigData.cc:261
const PrimClex & primclex() const
Definition: ConfigData.hh:152
const PrimClex & m_primclex
Definition: ConfigData.hh:191
std::string report_dir() const
void erase_data(const DB::Selection< Supercell > &selection, bool dry_run)
Erase data and files (permanently), but not configs / supercells.
void erase_all(const DB::Selection< Supercell > &selection, bool dry_run)
Removes supercell, including all configurations, data, and files (permanently)
void erase(const DB::Selection< Supercell > &selection, bool dry_run)
Erase all enumerated configs that have no data; if no data, erase supercell.
const PrimClex & primclex() const
static std::string desc()
Definition: Remove_impl.hh:104
Remove(const PrimClex &primclex, std::string report_dir)
Definition: Remove_impl.hh:100
static int run(const PrimClex &, const Completer::RmOption &opt)
Definition: Remove_impl.hh:130
void erase(const DB::Selection< ConfigType > &selection, bool dry_run)
Erase Configurations that have no data.
Definition: Remove_impl.hh:27
void erase_data(const DB::Selection< ConfigType > &selection, bool dry_run)
Erase data and files (permanently), but not Configuration.
Definition: Remove_impl.hh:51
void erase_all(const DB::Selection< ConfigType > &selection, bool dry_run)
Removes Configurations and data and files (permanently)
Definition: Remove_impl.hh:77
boost::iterator_range< iterator > selected()
Definition: Selection.cc:237
map_type & data()
Definition: Selection.cc:250
fs::path root_dir() const
Return casm project directory path.
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
Represents a supercell of the primitive parent crystal structure.
Definition: Supercell.hh:51
DB::Database< T > & db() const
Definition: PrimClex.cc:302
const DirectoryStructure & dir() const
Access DirectoryStructure object. Throw if not set.
Definition: PrimClex.cc:230
void commit(ProjectSettings const &set)
std::string create_report_dir(std::string report_dir)
Create a new report directory to avoid overwriting existing results.
Definition: ConfigData.cc:58
void for_each_config_type(F f)
Main CASM namespace.
Definition: APICommand.hh:8
Log & log()
Definition: Log.hh:424
Log & null_log()
Definition: Log.hh:438
GenericDatumFormatter< std::string, DataObject > name()
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
Log & err_log()
Definition: Log.hh:426
PrimClex * primclex
Definition: settings.cc:135
EraseAllScelConfigs(Remove< Supercell > &_remover, std::string _scelname, bool _dry_run)
EraseDataScelConfigs(Remove< Supercell > &_remover, std::string _scelname, bool _dry_run)
Helper struct base class.
EraseScelConfigsBase(Remove< Supercell > &_remover, std::string _scelname, bool _dry_run)
const Remove< Supercell > & remover
DB::Selection< T > make_selection() const
EraseScelConfigs(Remove< Supercell > &_remover, std::string _scelname, bool _dry_run)