CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
run.cc
Go to the documentation of this file.
1 #include<cstring>
2 #include<unistd.h>
3 
5 #include "casm/clex/PrimClex.hh"
9 
10 namespace CASM {
11 
12  namespace Completer {
13 
15 
19 
20  m_desc.add_options()
21  ("exec,e", po::value<std::string>(&m_exec_str)->required()->value_name(ArgHandler::command()), "Command to execute");
22  return;
23  }
24 
25  const std::string &RunOption::exec_str() const {
26  return m_exec_str;
27  };
28 
29  }
30 
31  // ///////////////////////////////////////
32  // 'run' function for casm
33  // (add an 'if-else' statement in casm.cpp to call this)
34 
35  int run_command(const CommandArgs &args) {
36  std::string exec;
37  fs::path selection;
38  po::variables_map vm;
39 
41  Completer::RunOption run_opt;
42 
43  try {
44  po::store(po::parse_command_line(args.argc, args.argv, run_opt.desc()), vm); // can throw
45 
48  if(vm.count("help")) {
49  args.log << "\n";
50  args.log << run_opt.desc() << std::endl;
51 
52  return 0;
53  }
54 
55  if(vm.count("desc")) {
56  args.log << "\n";
57  args.log << run_opt.desc() << std::endl;
58  args.log << "DESCRIPTION\n"
59  << " Executes the requested command for each selected configuration,\n"
60  << " with the path to the configuration as an argument. \n\n"
61  << " Example: casm run --exec \"vasp.relax\"\n"
62  << " - calls:\n"
63  << " 'vasp.relax $ROOT/training_data/$SCELNAME/$CONFIGID'\n"
64  << " for each config selected in config_list\n\n";
65 
66  return 0;
67  }
68 
69  po::notify(vm); // throws on error, so do after help in case
70  // there are any problems
71 
72  selection = run_opt.selection_path();
73  }
74  catch(po::error &e) {
75  args.err_log << "ERROR: " << e.what() << std::endl << std::endl;
76  args.err_log << run_opt.desc() << std::endl;
77  return 1;
78  }
79  catch(std::exception &e) {
80  args.err_log << "ERROR: " << e.what() << ".\n Exiting..." << std::endl;
81  return 1;
82 
83  }
84 
85  const fs::path &root = args.root;
86  if(root.empty()) {
87  args.err_log.error("No casm project found");
88  args.err_log << std::endl;
89  return ERR_NO_PROJ;
90  }
91 
92  // If 'args.primclex', use that, else construct PrimClex in 'uniq_primclex'
93  // Then whichever exists, store reference in 'primclex'
94  std::unique_ptr<PrimClex> uniq_primclex;
95  PrimClex &primclex = make_primclex_if_not(args, uniq_primclex);
96  const auto &dir = primclex.dir();
97 
98  try {
99  if(!vm.count("config") || (selection == "MASTER")) {
100  for(auto it = primclex.selected_config_begin(); it != primclex.selected_config_end(); ++it) {
101  if(!fs::exists(dir.POS(it->name()))) {
102  it->write_pos();
103  }
104 
105  Popen process;
106 
107  process.popen(run_opt.exec_str() + " " + it->get_path().string());
108 
109  process.print(args.log);
110  }
111  }
112  else if(vm.count("config") && fs::exists(selection)) {
113  ConfigSelection<true> config_select(primclex, selection);
114  for(auto it = config_select.selected_config_begin(); it != config_select.selected_config_end(); ++it) {
115  if(!fs::exists(dir.POS(it->name()))) {
116  it->write_pos();
117  }
118 
119  Popen process;
120 
121  process.popen(run_opt.exec_str() + " " + it->get_path().string());
122 
123  process.print(args.log);
124  }
125 
126  }
127  else {
128  args.err_log << "ERROR: Invalid input. Option '--config' accepts one argument (either 'MASTER' or a path to a valid configuration selection file)." << std::endl
129  << " Exiting...\n";
130  return 1;
131  }
132  }
133  catch(std::exception &e) {
134  args.err_log << "ERROR: Invalid input. Option '--config' accepts one argument (either 'MASTER' or a path to a valid configuration selection file)." << std::endl
135  << " Exiting...\n";
136  return 1;
137  }
138 
139 
140 
141 
142  args.log << "\n***************************\n" << std::endl;
143 
144  args.log << std::endl;
145 
146  return 0;
147  };
148 
149 }
150 
151 
const std::string & exec_str() const
Definition: run.cc:25
Data structure holding basic CASM command info.
const DirectoryStructure & dir() const
Definition: PrimClex.hh:112
PrimClex * primclex
Definition: settings.cc:101
void add_help_suboption()
Add a plain –help suboption.
Definition: Handlers.cc:276
iterator selected_config_begin()
void popen(std::string _command)
Execute popen for a given command.
Definition: Popen.cc:19
void add_configlist_suboption(const fs::path &_default="MASTER")
Add –config suboption (defaults to MASTER)
Definition: Handlers.cc:238
void print(std::ostream &sout) const
Print the last command executed and the resulting stdout.
Definition: Popen.cc:49
Main CASM namespace.
Definition: complete.cpp:8
config_iterator selected_config_end()
Configuration iterator: end.
Definition: PrimClex.cc:424
const fs::path & selection_path() const
Returns the string corresponding to add_config_suboption()
Definition: Handlers.cc:168
Remember how to use popen.
Definition: Popen.hh:12
const po::options_description & desc()
Get the program options, filled with the initialized values.
Definition: Handlers.cc:160
static std::string command()
Get value_type string for command completion (i.e. stuff in your $PATH)
Definition: Handlers.cc:43
int run_command(const CommandArgs &args)
Definition: run.cc:35
po::options_description m_desc
Boost program options. All the derived classes have them, but will fill them up themselves.
Definition: Handlers.hh:126
config_iterator selected_config_begin()
Configuration iterator: begin.
Definition: PrimClex.cc:413
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:52
fs::path POS(std::string configname) const
Return path to POS file.
DirectoryStructure & dir
Definition: settings.cc:102
PrimClex & make_primclex_if_not(const CommandArgs &args, std::unique_ptr< PrimClex > &uniq_primclex)
If !_primclex, construct new PrimClex stored in uniq_primclex, then return reference to existing or c...
void error(const std::string &what)
Definition: Log.hh:86
void initialize() override
Fill in the options descriptions accordingly.
Definition: run.cc:16
#define ERR_NO_PROJ