CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
ConfigSelection_impl.hh
Go to the documentation of this file.
1 #include "casm/external/boost.hh"
2 #include "casm/clex/ConfigIO.hh"
3 #include "casm/clex/PrimClex.hh"
6 namespace CASM {
7 
8 
9 
10  template <bool IsConst>
11  ConfigSelection<IsConst>::ConfigSelection(typename ConfigSelection<IsConst>::PrimClexType &_primclex, const fs::path &selection_path)
12  : m_primclex(&_primclex), m_name(selection_path.string()) {
13  if(selection_path == "MASTER") {
14  for(auto it = _primclex.config_begin(); it != _primclex.config_end(); ++it) {
15  set_selected(it->name(), it->selected());
16  }
17  }
18  else if(selection_path == "NONE") {
19  for(auto it = _primclex.config_cbegin(); it != _primclex.config_cend(); ++it) {
20  set_selected(it->name(), false);
21  }
22  }
23  else if(selection_path == "ALL") {
24  for(auto it = _primclex.config_cbegin(); it != _primclex.config_cend(); ++it) {
25  set_selected(it->name(), true);
26  }
27  }
28  else if(selection_path == "CALCULATED") {
29  for(auto it = _primclex.config_cbegin(); it != _primclex.config_cend(); ++it) {
30  set_selected(it->name(), is_calculated(*it));
31  }
32  }
33  else {
34  if(!fs::exists(selection_path)) {
35  std::stringstream ss;
36  ss << "ERROR in parsing configuation selection name. \n"
37  << " Expected <filename>, 'ALL', 'NONE', 'CALCULATED', or 'MASTER' <--default \n"
38  << " Received: '" << selection_path << "'\n"
39  << " No file named '" << selection_path << "'.";
40  throw std::runtime_error(ss.str());
41  }
42  m_name = fs::absolute(selection_path).string();
43  if(selection_path.extension() == ".json" || selection_path.extension() == ".JSON")
44  from_json(jsonParser(selection_path));
45  else {
46  std::ifstream select_file(selection_path.string().c_str());
47  read(select_file);
48  select_file.close();
49  }
50  }
51  }
52 
53  //******************************************************************************
54 
55  template <bool IsConst>
56  void ConfigSelection<IsConst>::read(std::istream &_input) {
57  std::string tname;
58  bool tselect;
59  _input >> std::ws;
60  if(_input.peek() == '#') {
61  _input.get();
62  // discard first two columns
63  _input >> tname;
64  _input >> tname;
65  std::getline(_input, tname, '\n');
66  m_col_headers.clear();
67  boost::split(m_col_headers, tname, boost::is_any_of(" \t"), boost::token_compress_on);
68  //_input.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
69  }
70  while(_input >> tname >> tselect) {
71  m_config[tname] = tselect;
72  _input.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
73  }
74  }
75 
76  //******************************************************************************
77 
78  template <bool IsConst>
80  if(!_json.is_array()) {
81  std::cerr << "CRITICAL ERROR: Unable to initialize a ConfigSelection from passed JSON input." << std::endl
82  << " JSON input must be an array of Configuration records." << std::endl
83  << " Exiting..." << std::endl;
84  exit(1);
85  }
86  std::map<std::string, bool> prop_map;
87  std::string tname;
88  bool tselected;
89  bool contains_name;
90  m_config.clear();
91  for(Index i = 0; i < _json.size(); i++) {
92  auto it(_json[i].cbegin()), it_end(_json[i].cend());
93 
94 
95  contains_name = false;
96  tselected = true; // default to selected
97 
98  for(; it != it_end; ++it) {
99  if(it.name() == "configname") {
100  tname = it->get<std::string>();
101  contains_name = true;
102  }
103  else if(it.name() == "selected") {
104  tselected = it->get<bool>();
105  }
106  else
107  prop_map[it.name()] = true;
108  }
109 
110  if(!contains_name) {
111  throw std::runtime_error(
112  std::string("CRITICAL ERROR: Field 'configname' is missing from Configuration ") +
113  std::to_string(i) + " of json Array passed to ConfigSelection::from_json()." +
114  " This field is required.");
115  }
116 
117  m_config[tname] = tselected;
118 
119  }
120 
121  std::map<std::string, bool>::iterator it(prop_map.begin()), it_end(prop_map.end());
122  for(; it != it_end; ++it)
123  m_col_headers.push_back(it->first);
124  return _json;
125  }
126 
127  //******************************************************************************
128 
129  template <bool IsConst>
132  jsonParser &_json,
133  bool only_selected) const {
134  _json.put_array();
135 
137 
138  tformat.append(_dict.parse(m_col_headers));
139 
140  if(only_selected) {
141  _json = tformat(selected_config_cbegin(),
142  selected_config_cend());
143  }
144  else {
145  _json = tformat(config_cbegin(),
146  config_cend());
147  }
148 
149  return _json;
150  }
151  //******************************************************************************
152  template <bool IsConst>
155  std::ostream &_out,
156  bool only_selected) const {
158 
159  tformat.append(_dict.parse(m_col_headers));
160 
161  if(only_selected) {
162  _out << tformat(selected_config_cbegin(),
163  selected_config_cend());
164  }
165  else {
166  _out << tformat(config_cbegin(),
167  config_cend());
168  }
169 
170  }
171  //******************************************************************************
172  /*
173  template< bool IsConst>
174  typename ConfigSelection<IsConst>::value_type &ConfigSelection<IsConst>::operator[](const std::string &configname) const {
175  return m_primclex->configuration(configname);
176  }
177  */
178 }
size_type size() const
Returns array size if *this is a JSON array, object size if *this is a JSON object, 1 otherwise.
Definition: jsonParser.cc:430
DatumFormatterAlias< DataObject > datum_formatter_alias(const std::string &_name, const std::string &_command, const DataFormatterDictionary< DataObject > &_dict, const std::string &_help="")
Make a DatumFormatterAlias.
void from_json(ClexDescription &desc, const jsonParser &json)
ConfigIO::Selected selected_in(const ConfigSelection< IsConst > &_selection)
jsonParser & to_json(const DataFormatterDictionary< Configuration > &_dict, jsonParser &_json, bool only_selected=false) const
void print(const DataFormatterDictionary< Configuration > &_dict, std::ostream &_out, bool only_selected=false) const
void read(std::istream &_input)
Main CASM namespace.
Definition: complete.cpp:8
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: EnumIO.hh:83
const jsonParser & from_json(const jsonParser &_json)
EigenIndex Index
For long integer indexing:
bool is_calculated(const Configuration &config)
Return true if all current properties have been been calculated for the configuration.
T max(const T &A, const T &B)
Definition: CASM_math.hh:32
ConfigIO::GenericConfigFormatter< std::string > configname()
Constructs DataFormmaterDictionary containing all Configuration DatumFormatters.
Definition: ConfigIO.cc:340
Extract data from objects of 'DataObject' class.
ConfigSelection()
Default constructor.
void append(const DataFormatter< DataObject > &_tail)
bool is_array() const
Check if array type.
Definition: jsonParser.cc:281
jsonParser & put_array()
Puts new empty JSON array.
Definition: jsonParser.hh:285
DataFormatter< DataObject > parse(const std::string &input) const
Use the vector of strings to build a DataFormatter
Parsing dictionary for constructing a DataFormatter object.