CASM  1.1.0
A Clusters Approach to Statistical Mechanics
dof_space_analysis.cc
Go to the documentation of this file.
2 
3 #include "casm/app/io/json_io.hh"
6 #include "casm/clex/PrimClex.hh"
12 
13 namespace CASM {
14 namespace DoFSpaceIO {
15 
17  jsonParser const &json_options, jsonParser const &cli_options_as_json) {
18  std::map<std::string, std::string> cli_to_combined_keys{
19  {"scelnames", "scelnames"}, // --scelnames
20  {"confignames", "confignames"}, // --confignames
21  {"selection", "config_selection"}, // --selection
22  {"dofs", "dofs"}, // --dofs
23  {"calc_wedge", "calc_wedge"} // --calc-wedge
24  };
25 
26  jsonParser json_combined{json_options};
27  return combine_json_options(cli_to_combined_keys, cli_options_as_json,
28  json_combined);
29 }
30 
38  std::set<std::string> do_not_allow{"scelnames", "supercell_selection",
39  "supercells", "sublats",
40  "sites", "cluster_specs"};
41 
42  for (auto key : do_not_allow) {
43  if (parser.self.contains(key)) {
44  std::stringstream msg;
45  msg << "Error in dof_space_analysis: \"" << key
46  << "\" is not an allowed option.";
47  parser.error.insert(msg.str());
48  }
49  }
50 }
51 
57 void parse_dofs(ParentInputParser &parser, std::vector<DoFKey> &dofs,
58  std::vector<DoFKey> const &all_dof_types) {
59  parser.optional_else(dofs, "dofs", all_dof_types);
60  for (DoFKey const &dof : dofs) {
61  if (std::find(all_dof_types.begin(), all_dof_types.end(), dof) ==
62  all_dof_types.end()) {
63  std::stringstream msg;
64  msg << "Error parsing \"dofs\": \"" << dof << "\" is invalid.";
65  parser.error.insert(msg.str());
66  }
67  }
68 }
69 
70 } // end namespace DoFSpaceIO
71 
73 std::string dof_space_analysis_desc() {
74  std::string description =
75 
76  "Analyze DoF spaces (--dof-space-analysis): \n\n"
77 
78  " The --dof-space-analysis option writes a symmetry analysis of one or "
79  "more \n"
80  " DoF spaces. "
81  " \n\n"
82 
83  " Usage: "
84  " \n"
85  " - Specify one or more DoF spaces and a symmetry group via selection "
86  "of \n"
87  " configurations and DoF type. "
88  " \n"
89  " - The supercell, selected sites, DoF type, and subspace determine "
90  "the \n"
91  " DoF space. "
92  " \n"
93  " - The configuration factor group (symmetry operations that keep the "
94  " \n"
95  " supercell lattice and the configuration DoF values invariant) is "
96  "used \n"
97  " for symmetry analysis. "
98  " \n"
99  " - The analysis finds irreducible representations of the DoF vector "
100  "space and\n"
101  " optionally calculates the symmetrically unique wedges in that "
102  "space. \n\n";
103 
104  std::string custom_options =
105 
106  " JSON options (--input or --settings): "
107  " \n\n"
108 
109  " dofs: array of string (optional, override with --dofs) "
110  " \n"
111  " Name of degree of freedoms for which the DoF space analysis is "
112  "performed \n"
113  " for each input configuration. The default includes all DoF types "
114  "in the \n"
115  " prim. "
116  " \n\n"
117 
118  " calc_wedge: bool (optional, default=false, override with "
119  "--calc-wedge) \n"
120  " Perform calculation of irreducible wedge (may significantly slow "
121  "down \n"
122  " analysis). "
123  " \n\n"
124 
125  " write_symmetry: bool (optional, default=true) "
126  " \n"
127  " If true (default), write the lattice point group, factor group "
128  "(operations \n"
129  " leave the configuration and selected sites invariant), and "
130  "crystal point \n"
131  " group (factor group operations excluding translations) of the "
132  "initial state\n"
133  " for analysis. "
134  " \n\n"
135 
136  " write_structure: bool (optional, default=true) "
137  " \n"
138  " If true (default), write a \"structure.json\" file containing the "
139  "structure\n"
140  " generated from the configuration DoF. "
141  " \n\n"
142 
143  " output_type: string (optional, default=\"symmetry_directory\") "
144  " \n"
145  " Selects how output files are written. Options are: "
146  " \n\n"
147 
148  " \"symmetry_directory\": (default) "
149  " \n"
150  " If selected, only accepts \"confignames\" and "
151  "\"config_selection\" \n"
152  " to specify the initial state for analysis and the results are "
153  "stored in \n"
154  " dedicated folders in the CASM project symmetry directory: "
155  " \n"
156  " \"<project_path>/symmetry/analysis/<configname>\". "
157  " \n\n"
158 
159  " \"sequential\": "
160  " \n"
161  " If selected, accept any input for specifying the initial state "
162  "for \n"
163  " analysis, including \"scelnames\", \"supercell_selection\", "
164  "\"supercells\"\n"
165  " \"sublats\", \"sites\", and \"cluster_specs\", and the results "
166  "are stored\n"
167  " in indexed folders "
168  "\"<output_dir>/dof_space_analysis/state.<index>\". \n\n"
169 
170  " \"combined_json\": "
171  " \n"
172  " If selected, accept any input for specifying the initial state "
173  "for \n"
174  " analysis, including \"scelnames\", \"supercell_selection\", "
175  "\"supercells\",\n"
176  " \"sublats\", \"sites\", and \"cluster_specs\", and the results "
177  "are stored\n"
178  " in a single JSON file \"<output_dir>/dof_space_analysis.json\" "
179  "instead \n"
180  " of being written separately. "
181  " \n\n"
182 
183  " output_dir: string (optional, default=current path) "
184  " \n"
185  " Selects where output files are written. "
186  " \n\n";
187 
188  return description + custom_options + parse_ConfigEnumInput_desc();
189 }
190 
205 void dof_space_analysis(PrimClex &primclex, jsonParser const &json_options,
206  jsonParser const &cli_options_as_json) {
207  using namespace DoFSpaceIO;
208 
209  Log &log = CASM::log();
210 
211  log.subsection().begin<Log::debug>("dof_space_analysis");
212  log.indent() << "json_options:\n" << json_options << std::endl << std::endl;
213  log.indent() << "cli_options_as_json:\n"
214  << cli_options_as_json << std::endl
215  << std::endl;
216  log.end_section();
217 
218  // combine JSON options and CLI options
220  json_options, cli_options_as_json);
221 
222  log.indent() << "Input:\n" << json_combined << std::endl << std::endl;
223 
224  // Read input data from JSON
225  ParentInputParser parser{json_combined};
226  std::runtime_error error_if_invalid{
227  "Error reading `casm sym --dof-space-analysis` input"};
228 
229  // 1) parse options
230 
231  DoFSpaceAnalysisOptions options;
232 
233  // parse "dofs" (optional, default = all dof types)
234  parse_dofs(parser, options.dofs, all_dof_types(primclex.prim().structure()));
235 
236  // parse "calc_wedge" (optional, default = false)
237  parser.optional_else(options.calc_wedge, "calc_wedge", false);
238 
239  // parse "write_symmetry" (optional, default = true)
240  parser.optional_else(options.write_symmetry, "write_symmetry", true);
241 
242  // parse "write_structure" (optional, default = true)
243  parser.optional_else(options.write_structure, "write_structure", true);
244 
245  // parse "output_type"= "symmetry_directory" (default), "sequential", or
246  // "combined_json"
247  std::string output_type;
248  parser.optional_else(output_type, "output_type",
249  std::string("symmetry_directory"));
250 
251  // parse "output_dir" (optional, default = current_path)
252  fs::path output_dir;
253  parser.optional_else(output_dir, "output_dir", fs::current_path());
254 
255  // 2) parse input states
256 
257  typedef std::vector<std::pair<std::string, ConfigEnumInput>>
258  NamedConfigEnumInput;
259  auto input_parser_ptr = parser.parse_as<NamedConfigEnumInput>(
260  primclex.shared_prim(), &primclex, primclex.db<Supercell>(),
261  primclex.db<Configuration>());
262  report_and_throw_if_invalid(parser, log, error_if_invalid);
263  auto const &named_inputs = *input_parser_ptr->value;
264 
265  // 3) Construct output method implementation and run dof space analysis
266 
267  if (output_type == "combined_json") {
268  // write all output to one large JSON file:
269  // <output_dir>/dof_space_analysis.json
270  CombinedJsonOutput output{output_dir};
271  dof_space_analysis(named_inputs, options, output);
272  } else if (output_type == "sequential") {
273  // write output sequentially indexed directories:
274  // <output_dir>/dof_space_analysis/state.<index>/
275  SequentialDirectoryOutput output{output_dir};
276  dof_space_analysis(named_inputs, options, output);
277  } else if (output_type == "symmetry_directory") {
279  report_and_throw_if_invalid(parser, log, error_if_invalid);
280 
281  // write output to CASM project symmetry directory:
282  // <project_path>/symmetry/analysis/<configname>/
283  SymmetryDirectoryOutput output{primclex.dir()};
284  dof_space_analysis(named_inputs, options, output);
285  } else {
286  std::string msg =
287  "Error: 'output_type' must be one of \"symmetry_directory\" (default), "
288  "\"sequential\", or \"combined_json\"";
289  parser.insert_error("output_type", msg);
290  report_and_throw_if_invalid(parser, log, error_if_invalid);
291  }
292 }
293 } // namespace CASM
RequiredType optional_else(fs::path option, const RequiredType &_default, Args &&... args)
Definition: Log.hh:48
void end_section()
End a section.
Definition: Log.hh:192
static const int debug
Definition: Log.hh:54
Log & subsection()
Create a subsection.
Definition: Log.hh:186
Log & indent()
Definition: Log.hh:289
void begin(const std::string &what)
Definition: Log.hh:114
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
Represents a supercell of the primitive parent crystal structure.
Definition: Supercell.hh:51
bool contains(const std::string &name) const
Return true if JSON object contains 'name'.
Definition: jsonParser.cc:601
std::vector< DoFKey > all_dof_types(BasicStructure const &_struc)
jsonParser combine_dof_space_analysis_json_options(jsonParser const &json_options, jsonParser const &cli_options_as_json)
void parse_dofs(ParentInputParser &parser, std::vector< DoFKey > &dofs, std::vector< DoFKey > const &all_dof_types)
void dof_space_analysis(std::vector< std::pair< std::string, ConfigEnumInput >> const &named_inputs, DoFSpaceAnalysisOptions const &options, OutputImpl &output)
void require_database_configurations(ParentInputParser &parser)
Main CASM namespace.
Definition: APICommand.hh:8
std::string description(const SymOp &op, const xtal::Lattice &lat, SymInfoOptions opt=SymInfoOptions())
Print SymInfo to string.
Log & log()
Definition: Log.hh:424
std::string DoFKey
Definition: DoFDecl.hh:7
void dof_space_analysis(PrimClex &primclex, jsonParser const &json_options, jsonParser const &cli_options_as_json)
Perform DoF space analysis.
jsonParser & combine_json_options(std::map< std::string, std::string > const &source_to_combined_keys, jsonParser const &json_source, jsonParser &json_combined)
Copy from json_source to json_combined
Definition: json_io.cc: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
std::string dof_space_analysis_desc()
Describe DoF space analysis input.
std::string parse_ConfigEnumInput_desc()
A string describing the JSON format for parsing named ConfigEnumInput.
void report_and_throw_if_invalid(KwargsParser const &parser, Log &log, ErrorType error)
PrimClex * primclex
Definition: settings.cc:135
jsonParser const & self
Definition: InputParser.hh:81
std::set< std::string > error
Definition: Validator.hh:11