CASM  1.1.0
A Clusters Approach to Statistical Mechanics
CanonicalSettings_impl.hh
Go to the documentation of this file.
1 #ifndef CASM_CanonicalSettings_impl
2 #define CASM_CanonicalSettings_impl
3 
4 #include <boost/algorithm/string/trim.hpp>
5 
9 #include "casm/clex/Supercell.hh"
11 
12 namespace CASM {
13 namespace Monte {
14 
20 template <typename SamplerInsertIterator>
21 SamplerInsertIterator CanonicalSettings::samplers(
22  const PrimClex &primclex, SamplerInsertIterator result) const {
23  size_type data_maxlength = max_data_length();
24  std::string prop_name;
25  std::string print_name;
26  bool must_converge;
27  double prec;
28  MonteSampler *ptr;
29 
30  // copy so we can add required measurements
31  std::string level1 = "data";
32  std::string level2 = "measurements";
33  jsonParser t_measurements = (*this)[level1][level2];
34 
35  // find existing measurements
36  std::set<std::string> input_measurements;
37  for (auto it = t_measurements.cbegin(); it != t_measurements.cend(); it++) {
38  input_measurements.insert((*it)["quantity"].get<std::string>());
39  }
40 
41  std::vector<std::string> required = {"potential_energy", "formation_energy"};
42 
43  // add required if not already requested
44  for (auto it = required.begin(); it != required.end(); ++it) {
45  if (std::find(input_measurements.begin(), input_measurements.end(), *it) ==
46  input_measurements.end()) {
47  jsonParser json;
48  json["quantity"] = *it;
49  t_measurements.push_back(json);
50  }
51  }
52 
53  try {
54  for (auto it = t_measurements.cbegin(); it != t_measurements.cend(); it++) {
55  prop_name = (*it)["quantity"].get<std::string>();
56 
57  // scalar quantities that we incrementally update
58  std::vector<std::string> scalar_possible = {"formation_energy",
59  "potential_energy"};
60 
61  // check if property found is in list of possible scalar properties
62  if (std::find(scalar_possible.cbegin(), scalar_possible.cend(),
63  prop_name) != scalar_possible.cend()) {
64  std::tie(must_converge, prec) = _get_precision(it);
65 
66  // if 'must converge'
67  if (must_converge) {
68  ptr = new ScalarMonteSampler(prop_name, prop_name, prec, confidence(),
69  data_maxlength);
70  } else {
71  ptr = new ScalarMonteSampler(prop_name, prop_name, confidence(),
72  data_maxlength);
73  }
74 
75  *result++ =
76  std::make_pair(prop_name, notstd::cloneable_ptr<MonteSampler>(ptr));
77  continue;
78  }
79 
80  // scalar quantities that we incrementally update
81  std::vector<std::string> vector_possible = {"all_correlations",
82  "non_zero_eci_correlations"};
83 
84  // check if property found is in list of possible vector properties
85  if (std::find(vector_possible.cbegin(), vector_possible.cend(),
86  prop_name) != vector_possible.cend()) {
87  // construct MonteSamplers for 'all_correlations'
88  if (prop_name == "all_correlations") {
89  result = _make_all_correlations_samplers(primclex, it, result);
90 
91  }
92 
93  // construct MonteSamplers for 'non_zero_eci_correlations'
94  else if (prop_name == "non_zero_eci_correlations") {
95  result =
97  }
98  continue;
99  }
100 
101  // custom query
102  _make_query_samplers(primclex, it, result);
103  }
104 
105  } catch (std::runtime_error &e) {
108  "'MonteSettings::samplers(const PrimClex &primclex, "
109  "SamplerInsertIterator result)'");
110  err_log << "Error reading [\"" << level1 << "\"][\"" << level2 << "\"]\n"
111  << std::endl;
112  throw;
113  }
114 
115  return result;
116 }
117 
118 template <typename jsonParserIteratorType>
119 std::tuple<bool, double> CanonicalSettings::_get_precision(
120  jsonParserIteratorType it) const {
121  if (it->contains("precision")) {
122  return std::make_tuple(true, (*it)["precision"].template get<double>());
123  } else {
124  return std::make_tuple(false, 0.0);
125  }
126 }
127 
128 template <typename jsonParserIteratorType, typename SamplerInsertIterator>
130  const PrimClex &primclex, jsonParserIteratorType it,
131  SamplerInsertIterator result) const {
132  size_type data_maxlength = max_data_length();
133  std::string prop_name;
134  std::string print_name;
135  bool must_converge;
136  double prec;
137  MonteSampler *ptr;
138 
139  for (size_type i = 0;
141  i++) {
142  prop_name = "corr";
143  print_name = std::string("corr(") + std::to_string(i) + ")";
144 
145  std::tie(must_converge, prec) = _get_precision(it);
146 
147  // if 'must converge'
148  if (must_converge) {
149  ptr = new VectorMonteSampler(prop_name, i, print_name, prec, confidence(),
150  data_maxlength);
151  } else {
152  ptr = new VectorMonteSampler(prop_name, i, print_name, confidence(),
153  data_maxlength);
154  }
155 
156  *result++ =
157  std::make_pair(print_name, notstd::cloneable_ptr<MonteSampler>(ptr));
158  }
159 
160  return result;
161 }
162 
163 template <typename jsonParserIteratorType, typename SamplerInsertIterator>
164 SamplerInsertIterator
166  const PrimClex &primclex, jsonParserIteratorType it,
167  SamplerInsertIterator result) const {
168  size_type data_maxlength = max_data_length();
169  std::string prop_name;
170  std::string print_name;
171  bool must_converge;
172  double prec;
173 
174  MonteSampler *ptr;
175 
177 
178  for (size_type ii = 0; ii < _eci.index().size(); ii++) {
179  prop_name = "corr";
180 
181  // store non-zero eci index in i
182  size_type i = _eci.index()[ii];
183 
184  print_name = std::string("corr(") + std::to_string(i) + ")";
185 
186  std::tie(must_converge, prec) = _get_precision(it);
187 
188  // if 'must converge'
189  if (must_converge) {
190  ptr = new VectorMonteSampler(prop_name, i, print_name, prec, confidence(),
191  data_maxlength);
192  } else {
193  ptr = new VectorMonteSampler(prop_name, i, print_name, confidence(),
194  data_maxlength);
195  }
196 
197  *result++ =
198  std::make_pair(print_name, notstd::cloneable_ptr<MonteSampler>(ptr));
199  }
200 
201  return result;
202 }
203 
204 template <typename jsonParserIteratorType, typename SamplerInsertIterator>
206  const PrimClex &primclex, jsonParserIteratorType it,
207  SamplerInsertIterator result) const {
208  size_type data_maxlength = max_data_length();
209  double must_converge;
210  double prec;
211  std::string prop_name = (*it)["quantity"].template get<std::string>();
212  MonteSampler *ptr;
213 
214  const auto &dict = primclex.settings().query_handler<Configuration>().dict();
215 
216  typedef QueryMonteSampler::Formatter FormatterType;
217  std::shared_ptr<FormatterType> formatter =
218  std::make_shared<FormatterType>(dict.parse(prop_name));
219 
220  // make example config to test:
221  Supercell tscel(const_cast<PrimClex *>(&primclex), simulation_cell_matrix());
222  Configuration config(tscel);
223  config.init_occupation();
224  Eigen::VectorXd test = formatter->get().evaluate_as_matrix(config).row(0);
225  auto col = formatter->get().col_header(config);
226 
227  if (test.size() != col.size()) {
228  std::stringstream ss;
229  ss << "Error constructing Monte Carlo samplers from query: '" << prop_name
230  << "'";
232  err_log << ss.str();
233  err_log << "headers: " << col << std::endl;
234  err_log << " Some queries may not be available for sampling at this time."
235  << std::endl;
236  throw std::runtime_error(ss.str());
237  }
238 
239  for (int i = 0; i < col.size(); ++i) {
240  std::string print_name = col[i];
241  boost::algorithm::trim(print_name);
242 
243  std::tie(must_converge, prec) = _get_precision(it);
244 
245  // if 'must converge'
246  if (must_converge) {
247  ptr = new QueryMonteSampler(formatter, i, print_name, prec, confidence(),
248  data_maxlength);
249  } else {
250  ptr = new QueryMonteSampler(formatter, i, print_name, confidence(),
251  data_maxlength);
252  }
253 
254  *result++ =
255  std::make_pair(print_name, notstd::cloneable_ptr<MonteSampler>(ptr));
256  }
257 
258  return result;
259 }
260 
261 } // namespace Monte
262 } // namespace CASM
263 
264 #endif
A sparse container of ECI values and their corresponding orbit indices.
Definition: ECIContainer.hh:12
const std::vector< size_type > & index() const
const Access orbit indices of ECI values
Definition: ECIContainer.hh:44
Definition: Log.hh:48
void error(const std::string &what)
Definition: Log.hh:129
static const int standard
Definition: Log.hh:52
SamplerInsertIterator _make_non_zero_eci_correlations_samplers(const PrimClex &primclex, jsonParserIteratorType it, SamplerInsertIterator result) const
ClexDescription formation_energy(const PrimClex &primclex) const
Get formation energy cluster expansion.
std::tuple< bool, double > _get_precision(jsonParserIteratorType it) const
SamplerInsertIterator samplers(const PrimClex &primclex, SamplerInsertIterator result) const
Construct MonteSamplers as specified in the MonteSettings.
SamplerInsertIterator _make_query_samplers(const PrimClex &primclex, jsonParserIteratorType it, SamplerInsertIterator result) const
SamplerInsertIterator _make_all_correlations_samplers(const PrimClex &primclex, jsonParserIteratorType it, SamplerInsertIterator result) const
size_type max_data_length() const
Figure out how large data containers should be.
An abstract base class for sampling and storing data observations.
Definition: MonteSampler.hh:26
const PrimClex & primclex() const
double confidence() const
Given a settings jsonParser figure out the global tolerance (probably for == operator)....
Eigen::Matrix3l simulation_cell_matrix() const
Supercell matrix defining the simulation cell.
Data structure to make queries occur once each sample time.
Sampler for individual elements of a vector property.
Sampler for a scalar property.
Sampler for individual elements of a vector property.
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
QueryHandler< DataObject > & query_handler()
Represents a supercell of the primitive parent crystal structure.
Definition: Supercell.hh:51
size_type size() const
Definition: jsonParser.cc:487
json_spirit::mObject::size_type size_type
Definition: jsonParser.hh:87
const_iterator cend() const
Returns const_iterator to end of JSON object or JSON array.
Definition: jsonParser.cc:533
const_iterator cbegin() const
Returns const_iterator to beginning of JSON object or JSON array.
Definition: jsonParser.cc:510
A 'cloneable_ptr' can be used in place of 'unique_ptr'.
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: io_traits.hh:172
ProjectSettings & settings()
Definition: PrimClex.cc:224
ECIContainer const & eci(const ClexDescription &key) const
Definition: PrimClex.cc:406
Clexulator clexulator(std::string const &basis_set_name) const
Definition: PrimClex.cc:366
jsonParser & push_back(const T &value, Args &&... args)
Definition: jsonParser.hh:684
T get(Args &&... args) const
Get data from json, using one of several alternatives.
Definition: jsonParser.hh:716
ConfigIO::GenericConfigFormatter< jsonParser > config()
Definition: ConfigIO.cc:777
Main CASM namespace.
Definition: APICommand.hh:8
Iterator find(Iterator begin, Iterator end, const T &value, BinaryCompare q)
Equivalent to std::find(begin, end, value), but with custom comparison.
Definition: algorithm.hh:16
Eigen::VectorXd VectorXd
Log & err_log()
Definition: Log.hh:426
PrimClex * primclex
Definition: settings.cc:135
pair_type bset
Definition: settings.cc:145