CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
CanonicalSettings.cc
Go to the documentation of this file.
4 #include "casm/app/AppIO.hh"
5 
6 namespace CASM {
7  namespace Monte {
8  std::string _help() {
9  std::string s =
10  "For CanonicalConditions, expect a JSON object of form:\n"
11  " {\n"
12  " \"comp\": { // option 1: parameteric composition object\n"
13  " \"a\" : 0.3,\n"
14  " ...\n"
15  " },\n"
16  " \"comp\": [0.3, 0.2, ...], // option 2: parameteric composition array\n"
17  " \"comp_n\": { // option 3: mol per prim composition object\n"
18  " \"A\" : 1.2,\n"
19  " ...\n"
20  " },\n"
21  " \"comp_n\": [1.2, 0.3, ...], // option 4: mol per prim composition array\n"
22  " \"temperature\" : 350.0,\n"
23  " \"tolerance\" : 0.001\n"
24  " }\n";
25  return s;
26  }
27 
29  CanonicalSettings::CanonicalSettings(const PrimClex &_primclex, const fs::path &read_path) :
30  EquilibriumMonteSettings(_primclex, read_path) {
31 
32  if(!_primclex.has_composition_axes()) {
33  throw std::runtime_error("No composition axes selected.");
34  }
35 
36  }
37 
38  // --- CanonicalConditions settings ---------------------
39 
43  return _conditions("initial_conditions");
44  }
46  return custom_conditions()[0];
47  }
48  else {
49  throw std::runtime_error("ERROR: Invalid drive mode.");
50  }
51  }
52 
55  return _conditions("final_conditions");
56  }
57 
60  return _conditions("incremental_conditions");
61  }
62 
64  std::vector<CanonicalConditions> CanonicalSettings::custom_conditions() const {
65  std::string level1 = "driver";
66  std::string level2 = "custom_conditions";
67 
68  try {
69  std::vector<CanonicalConditions> cond;
70  const jsonParser &json = (*this)[level1][level2];
71  for(auto it = json.begin(); it != json.end(); ++it) {
72  cond.push_back(_conditions(*it));
73  }
74  return cond;
75  }
76  catch(std::runtime_error &e) {
78  err_log.error<Log::standard>("Reading Monte Carlo settings");
79  err_log << "Tried to read an array of CanonicalConditions from [\"" << level1 << "\"][\"" << level2 << "\"]" << std::endl;
80  err_log << _help() << std::endl;
81  throw e;
82  }
83  }
84 
85  // --- Project settings ---------------------
86 
89  const ProjectSettings &set = primclex.settings();
90  std::string level1 = "model";
91  // deprecated
92  if(_is_setting(level1, "clex")) {
93 
94  // expect "clex" is "formation_energy"
95 
96  std::vector<std::string> var {"clex", "calctype", "ref", "bset", "eci"};
97  std::vector<std::string> help {
98  "string\n Names the cluster expansion to be used.\n",
99  "string\n Names the calctype to be used.\n",
100  "string\n Names the reference to be used.\n",
101  "string\n Names the basis set to be used.\n",
102  "string\n Names the ECI to be used.\n"
103  };
104 
105  return ClexDescription(
106  _get_setting<std::string>(level1, var[0], help[0]),
107  _get_setting<std::string>(level1, var[0], help[0]),
108  _get_setting<std::string>(level1, var[1], help[1]),
109  _get_setting<std::string>(level1, var[2], help[2]),
110  _get_setting<std::string>(level1, var[3], help[3]),
111  _get_setting<std::string>(level1, var[4], help[4]));
112  }
113 
114  std::string help = "(string, default='formation_energy')\n"
115  " Names the formation_energy cluster expansion to be used.\n";
116 
117  std::string formation_energy = "formation_energy";
118  if(_is_setting(level1, "formation_energy")) {
119  formation_energy = _get_setting<std::string>(level1, "formation_energy", help);
120  }
121 
122  if(!set.has_clex(formation_energy)) {
124  err_log.error<Log::standard>("Reading Monte Carlo settings");
125  err_log << "Error reading [\"model\"][\"formation_energy\"]\n";
126  err_log << "[\"model\"][\"formation_energy\"]: (string, optional, default='formation_energy')\n";
127  err_log << " Names the cluster expansion to be used for calculating formation_energy.\n";
128  err_log << "No cluster expansion named: '" << formation_energy << "' exists.\n";
129  }
130  return set.clex(formation_energy);
131  }
132 
133  // --- Sampler settings ---------------------
134 
137  std::string level1 = "data";
138  std::string level2 = "measurements";
139  try {
140  const jsonParser &json = (*this)[level1][level2];
141  for(auto it = json.cbegin(); it != json.cend(); ++it) {
142  if(it->contains("quantity") && (*it)["quantity"].get<std::string>() == "all_correlations") {
143  return true;
144  }
145  }
146  return false;
147  }
148  catch(std::runtime_error &e) {
150  err_log.error<Log::standard>("Reading Monte Carlo settings");
151  err_log << "Error checking if 'all_correlations' should be sampled.\n";
152  err_log << "Error reading settings at [\"" << level1 << "\"][\"" << level2 << "\"]\n";
153  err_log << "See 'casm format --monte' for help.\n" << std::endl;
154  throw e;
155  }
156 
157  }
158 
160 
161  std::string level1 = "driver";
162  std::string level2 = name;
163  try {
164  return _conditions((*this)[level1][level2]);
165  }
166  catch(std::runtime_error &e) {
168  err_log.error<Log::standard>("Reading Monte Carlo settings");
169  err_log << "Error reading: " << name << std::endl;
170  err_log << "Tried to construct CanonicalCondtions from [\"" << level1 << "\"][\"" << level2 << "\"]" << std::endl;
171  err_log << _help() << std::endl;
172  throw e;
173  }
174  }
175 
177  CanonicalConditions result;
178  from_json(result, primclex(), json);
179  return result;
180  }
181  }
182 }
183 
CanonicalConditions _conditions(std::string name) const
CanonicalSettings()
Default constructor.
Specifies a particular cluster expansion.
iterator end()
Returns iterator to end of JSON object or JSON array.
Definition: jsonParser.cc:465
std::string help()
Print help message describing recognized strings for allowed enum values.
Definition: EnumIO.hh:38
const PrimClex & primclex() const
bool all_correlations() const
Return true if all correlations should be sampled.
CanonicalConditions initial_conditions() const
Expects initial_conditions.
PrimClex * primclex
Definition: settings.cc:101
ClexDescription formation_energy(const PrimClex &primclex) const
Get formation energy cluster expansion.
bool has_composition_axes() const
check if CompositionConverter object initialized
Definition: PrimClex.cc:231
Main CASM namespace.
Definition: complete.cpp:8
std::string _help()
iterator begin()
Returns const_iterator to beginning of JSON object or JSON array.
Definition: jsonParser.cc:440
const_iterator cend() const
Returns const_iterator to end of JSON object or JSON array.
Definition: jsonParser.cc:480
ProjectSettings & set
Definition: settings.cc:103
std::vector< CanonicalConditions > custom_conditions() const
Expects custom_conditions.
const ClexDescription & clex(std::string name) const
static const int standard
Definition: Log.hh:15
CanonicalConditions incremental_conditions() const
Expects incremental_conditions.
Read/modify settings of an already existing CASM project.
CanonicalConditions final_conditions() const
Expects final_conditions.
void from_json(CanonicalConditions &conditions, const PrimClex &primclex, const jsonParser &json)
Read CanonicalConditions from JSON format.
Definition: CanonicalIO.cc:137
ProjectSettings & settings()
Definition: PrimClex.hh:116
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:52
bool has_clex(std::string name) const
bool _is_setting(std::string level1, std::string level2) const
Returns true if (*this)[level1].contains(level2)
virtual const Monte::DRIVE_MODE drive_mode() const
Given a settings jsonParser figure out the drive mode. Expects drive_mode/incremental,custom.
jsonParser & push_back(const T &value)
Puts new valued element at end of array of any type T for which 'jsonParser& to_json( const T &value...
Definition: jsonParser.hh:696
Log & err_log
Definition: settings.cc:106
Log & default_err_log()
Definition: Log.hh:206
void error(const std::string &what)
Definition: Log.hh:86
Definition: Log.hh:9
const_iterator cbegin() const
Returns const_iterator to beginning of JSON object or JSON array.
Definition: jsonParser.cc:455