CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
Args.cc
Go to the documentation of this file.
1 #include "casm/casm_io/Args.hh"
2 
3 namespace CASM {
4 
5  // ////////////////////////////////////////
6  // ////////////////////////////////////////
7  // Member functions
8 
10  clear();
11  };
12  Args::Args(const Array<std::string> args, std::ostream &out) {
13  clear();
14  set(args, out);
15  };
16 
17  void Args::set(const Array<std::string> args, std::ostream &out) {
18  Index i;
19  std::string arg;
20 
21  // args[0] shold be "casmtools or /path/to/casmtools"
22  if(args.size() < 2) {
23  all_ok = false;
24  return;
25  }
26 
27  // args[1] shold be function to call (-clust, -hclust, etc.)
28  if(args[1][0] != '-') {
29  all_ok = false;
30  err_list.push_back(args[1]);
31  }
32  else {
33  function_name = args[1];
34  }
35 
36  // read the rest of the arguments
37  for(i = 2; i < args.size(); i++) {
38  if(args[i] == "-PRIM") {
39  while(((i + 1) < args.size()) && (args[i + 1][0] != '-')) {
40  i++;
41  prim_filenames.push_back(args[i]);
42  }
43  if(prim_filenames.size() == 0)
44  all_ok = false;
45  }
46  else if(args[i] == "-CSPECS") {
47  while(((i + 1) < args.size()) && (args[i + 1][0] != '-')) {
48  i++;
49  cspecs_filenames.push_back(args[i]);
50  }
51  if(cspecs_filenames.size() == 0)
52  all_ok = false;
53  }
54  else if(args[i] == "-HSPECS") {
55  while(((i + 1) < args.size()) && (args[i + 1][0] != '-')) {
56  i++;
57  hspecs_filenames.push_back(args[i]);
58  }
59  if(hspecs_filenames.size() == 0)
60  all_ok = false;
61  }
62  else if(args[i] == "-LCSPECS") {
63  while(((i + 1) < args.size()) && (args[i + 1][0] != '-')) {
64  i++;
66  }
67  if(lcspecs_filenames.size() == 0)
68  all_ok = false;
69  }
70  else if(args[i] == "-STRUC") {
71  while(((i + 1) < args.size()) && (args[i + 1][0] != '-')) {
72  i++;
74  }
75  if(structure_filenames.size() == 0)
76  all_ok = false;
77  }
78  else if(args[i] == "-MATRIX") {
79  while(((i + 1) < args.size()) && (args[i + 1][0] != '-')) {
80  i++;
81  matrix_filenames.push_back(args[i]);
82  }
83  if(matrix_filenames.size() == 0)
84  all_ok = false;
85  }
86  else if(args[i] == "-TOL") {
87  if(((i + 1) < args.size()) && (args[i + 1][0] != '-')) {
88  i++;
89  tol = std::stod(args[i]);
90  }
91  else {
92  all_ok = false;
93  }
94  }
95  else if(args[i] == "-COORD") {
96  if(((i + 1) < args.size()) && (args[i + 1][0] != '-')) {
97  i++;
98  if(args[i] == "FRAC")
100  else if(args[i] == "CART")
102  else {
103  all_ok = false;
104  err_list.push_back(args[i]);
105  }
106 
107  }
108  else {
109  all_ok = false;
110  }
111  }
112  else if(args[i][0] == '-') { // keywords not pre-specified, "-keyword val0 val1 ..."
113  Index index = keywords.find(args[i]);
114  bool new_keyword = false;
115  if(index == keywords.size()) {
116  keywords.push_back(args[i]);
117  new_keyword = true;
118  }
119 
120  while(((i + 1) < args.size()) && (args[i + 1][0] != '-')) {
121  i++;
122  if(new_keyword) {
123  values.push_back(Array<std::string>(1, args[i]));
124  new_keyword = false;
125  }
126  else {
127  values[index].push_back(args[i]);
128  }
129  }
130 
131  }
132  else {
133  all_ok = false;
134  err_list.push_back(args[i]);
135  }
136 
137  }
138 
139  if(!all_ok) {
140  out << "ERROR. Error reading arguments." << std::endl;
141  out << "Expect format: " << "casmtools -functionname -OPTION1 Val -OPTION2 Val" << std::endl;
142  out << std::endl;
143  out << "INPUT: ";
144  for(i = 0; i < args.size(); i++)
145  out << "'" << args[i] << "' ";
146  out << std::endl;
147  out << std::endl;
148  print(out);
149  exit(1);
150  }
151  };
152 
153  void Args::clear() {
154  // set all to default
155 
162 
163  all_ok = true;
164 
165  tol = CASM::TOL;
167 
168  };
169 
170  // ////////////////////////////////////////
171  // Accessors
172 
173  bool Args::is_ok() const {
174  return all_ok;
175  };
176 
177  const std::string &Args::get_function_name() const {
178  return function_name;
179  };
180 
182  return prim_filenames;
183  };
185  return cspecs_filenames;
186  };
188  return hspecs_filenames;
189  };
191  return lcspecs_filenames;
192  };
194  return structure_filenames;
195  };
197  return matrix_filenames;
198  };
199  const Array<std::string> &Args::get_keyvals(std::string key) const {
200  Index index = keywords.find(key);
201  if(index == keywords.size())
202  return null_values;
203  else
204  return values[index];
205  };
206 
207 
208  double Args::get_tol() const {
209  return tol;
210  };
212  return coord_mode;
213  };
214 
215  // ////////////////////////////////////////
216  // Mutators
217 
218  void Args::set_function_name(const std::string &in) {
219  function_name = in;
220  };
221 
223  prim_filenames = in;
224  };
226  cspecs_filenames = in;
227  };
229  hspecs_filenames = in;
230  };
232  lcspecs_filenames = in;
233  };
235  structure_filenames = in;
236  };
238  matrix_filenames = in;
239  };
240  void Args::set_keyvals(const std::string &key, const Array<std::string> &invals) {
241  Index index = keywords.find(key);
242  if(index == keywords.size()) {
243  keywords.push_back(key);
244  values.push_back(invals);
245  }
246  else {
247  for(Index i = 0; i < invals.size(); i++)
248  values[index].push_back(invals[i]);
249  }
250  };
251 
252 
254  return prim_filenames;
255  };
257  return cspecs_filenames;
258  };
260  return hspecs_filenames;
261  };
263  return lcspecs_filenames;
264  };
266  return structure_filenames;
267  };
269  return matrix_filenames;
270  };
271 
272  void Args::set_tol(double in) {
273  tol = in;
274  };
276  coord_mode = in;
277  };
278 
279  // ////////////////////////////////////////
280  // Print
281 
282  void Args::print(std::ostream &out) const {
283  out << "# Input file names" << std::endl;
284  out << "prim_filenames: " << prim_filenames << std::endl;
285  out << "cspec_filenames: " << cspecs_filenames << std::endl;
286  out << "hspec_filenames: " << hspecs_filenames << std::endl;
287  out << "lcspec_filenames: " << lcspecs_filenames << std::endl;
288  out << "structure_filenames: " << structure_filenames << std::endl;
289  out << "matrix_filenames: " << matrix_filenames << std::endl;
290  out << std::endl;
291  out << "# Additional settings" << std::endl;
292  if(coord_mode == CASM::FRAC)
293  out << "coord_mode: " << "FRAC" << std::endl;
294  else if(coord_mode == CASM::CART)
295  out << "coord_mode: " << "CART" << std::endl;
296  out << "tol: " << tol << std::endl;
297  out << std::endl;
298  out << "# Additional Keywords:" << std::endl;
299  for(Index i = 0; i < keywords.size(); i++) {
300  out << keywords[i] << ": ";
301  if(values.size() >= i)
302  for(Index j = 0; j < values[i].size(); j++) {
303  out << values[i][j] << " ";
304  }
305  out << "\n" << std::flush;
306  }
307  out << std::endl;
308  out << "# Function to call" << std::endl;
309  out << "function_name: " << function_name << std::endl;
310  out << std::endl;
311  if(!all_ok) {
312  out << "# Strings I don't understand" << std::endl;
313  out << "Err: " << err_list << std::endl;
314  out << std::endl;
315  }
316  }
317 
318 };
319 
const Array< std::string > & get_prim_filenames() const
Definition: Args.cc:181
Array< std::string > null_values
Definition: Args.hh:62
Index size() const
Definition: Array.hh:145
void set_tol(double in)
Definition: Args.cc:272
bool is_ok() const
Definition: Args.cc:173
void set_function_name(const std::string &in)
Definition: Args.cc:218
const Array< std::string > & get_hspecs_filenames() const
Definition: Args.cc:187
void push_back(const T &toPush)
Definition: Array.hh:513
void clear()
Definition: Args.cc:153
COORD_TYPE get_coord_mode() const
Definition: Args.cc:211
const std::string & get_function_name() const
Definition: Args.cc:177
const Array< std::string > & get_keyvals(std::string key) const
Definition: Args.cc:199
Array< std::string > structure_filenames
Definition: Args.hh:56
Main CASM namespace.
Definition: complete.cpp:8
bool all_ok
Definition: Args.hh:45
const double TOL
const Array< std::string > & get_lcspecs_filenames() const
Definition: Args.cc:190
Array< std::string > hspecs_filenames
Definition: Args.hh:54
const Array< std::string > & get_matrix_filenames() const
Definition: Args.cc:196
void set_lcspecs_filenames(const Array< std::string > &in)
Definition: Args.cc:231
void clear()
Definition: Array.hh:216
void set_matrix_filenames(const Array< std::string > &in)
Definition: Args.cc:237
const Array< std::string > & get_structure_filenames() const
Definition: Args.cc:193
Array< std::string > prim_filenames
Definition: Args.hh:52
EigenIndex Index
For long integer indexing:
Args()
Definition: Args.cc:9
COORD_TYPE coord_mode
Definition: Args.hh:67
double tol
Definition: Args.hh:66
void set(const Array< std::string > args, std::ostream &out)
Definition: Args.cc:17
Index find(const T &test_elem) const
Definition: Array.hh:707
Array< Array< std::string > > values
Definition: Args.hh:61
const Array< std::string > & get_cspecs_filenames() const
Definition: Args.cc:184
Array< std::string > cspecs_filenames
Definition: Args.hh:53
Array< std::string > lcspecs_filenames
Definition: Args.hh:55
void print(std::ostream &out) const
Definition: Args.cc:282
void set_hspecs_filenames(const Array< std::string > &in)
Definition: Args.cc:228
Array< std::string > keywords
Definition: Args.hh:60
Array< std::string > err_list
Definition: Args.hh:75
Array< std::string > matrix_filenames
Definition: Args.hh:57
void set_coord_mode(COORD_TYPE in)
Definition: Args.cc:275
void set_prim_filenames(const Array< std::string > &in)
Definition: Args.cc:222
double get_tol() const
Definition: Args.cc:208
void set_cspecs_filenames(const Array< std::string > &in)
Definition: Args.cc:225
void set_keyvals(const std::string &key, const Array< std::string > &invals)
Definition: Args.cc:240
std::string function_name
Definition: Args.hh:71
void set_structure_filenames(const Array< std::string > &in)
Definition: Args.cc:234