CASM  1.1.0
A Clusters Approach to Statistical Mechanics
PrimInfoInterface.cc
Go to the documentation of this file.
2 
4 #include "casm/casm_io/Log.hh"
9 #include "casm/clex/PrimClex.hh"
13 
14 namespace CASM {
15 
16 namespace {
17 
18 std::shared_ptr<Structure const> open_shared_prim(fs::path root) {
19  ProjectSettings settings = open_project_settings(root);
20  return std::make_shared<Structure const>(
21  read_prim(settings.dir().prim(), settings.crystallography_tol()));
22 }
23 
24 } // namespace
25 
26 std::string PrimInfoInterface::desc() const {
27  std::string description = "Get information about a prim structure.\n\n";
28 
29  std::string custom_options =
30  " prim: JSON object (optional, default=prim of current project) \n"
31  " See `casm format --prim` for details on the prim format.\n\n"
32 
33  " properties: array of string (optional, default=[]) \n"
34  " An array of strings specifying which prim properties to output. \n"
35  " The default value of an empty array will print all properties. \n"
36  " The allowed options are: \n\n";
37 
38  std::stringstream ss;
39  auto dict = make_dictionary<std::shared_ptr<Structure const>>();
40  dict.print_help(ss, DatumFormatterClass::Property);
41 
42  return name() + ": \n\n" + description + custom_options + ss.str();
43 }
44 
45 std::string PrimInfoInterface::name() const { return "PrimInfo"; }
46 
48 void PrimInfoInterface::run(jsonParser const &json_options,
49  PrimClex const *primclex) const {
50  Log &log = CASM::log();
51 
52  ParentInputParser parser{json_options};
53  std::runtime_error error_if_invalid{"Error reading PrimInfo input"};
54 
55  std::shared_ptr<Structure const> shared_prim;
56  if (parser.self.contains("prim")) {
57  // prim provided in input
58  xtal::BasicStructure basic_structure;
59  parser.optional<xtal::BasicStructure>(basic_structure, "prim", TOL);
60  if (parser.valid()) {
61  shared_prim = std::make_shared<Structure const>(basic_structure);
62  }
63  } else if (primclex != nullptr) {
64  // if project provided via api
65  shared_prim = primclex->shared_prim();
66  } else {
67  // if project contains current working directory
68  fs::path root = find_casmroot(fs::current_path());
69  if (!root.empty()) {
70  try {
71  shared_prim = open_shared_prim(root);
72  } catch (std::exception &e) {
73  parser.insert_error("prim", e.what());
74  }
75  } else {
76  std::stringstream msg;
77  msg << "Error in PrimInfo: No \"prim\" in input and no project provided "
78  "or found.";
79  parser.insert_error("prim", msg.str());
80  }
81  }
82 
83  std::vector<std::string> properties;
84  parser.optional(properties, "properties");
85  report_and_throw_if_invalid(parser, log, error_if_invalid);
86 
87  auto dict = make_dictionary<std::shared_ptr<Structure const>>();
88  if (properties.empty()) {
89  for (auto it = dict.begin(); it != dict.end(); ++it) {
90  if (it->type() == DatumFormatterClass::Property) {
91  properties.push_back(it->name());
92  }
93  }
94  }
95  auto formatter = dict.parse(properties);
96 
97  jsonParser json;
98  formatter.to_json(shared_prim, json);
99  log << json << std::endl;
100 }
101 
102 } // namespace CASM
std::shared_ptr< Structure const > shared_prim
Definition: Log.hh:48
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
void run(jsonParser const &json_options, PrimClex const *primclex=nullptr) const override
Run prim info method.
std::string name() const override
Enumeration method name (i.e. "prim", "supercell", "dof_space", etc.)
std::string desc() const override
BasicStructure specifies the lattice and atomic basis of a crystal.
ProjectSettings open_project_settings(fs::path path_in_project)
ConfigIO::GenericConfigFormatter< jsonParser > properties()
Definition: ConfigIO.cc:785
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
const double TOL
Definition: definitions.hh:30
xtal::BasicStructure read_prim(fs::path filename, double xtal_tol, ParsingDictionary< AnisoValTraits > const *_aniso_val_dict=nullptr)
fs::path find_casmroot(const fs::path &cwd)
void report_and_throw_if_invalid(KwargsParser const &parser, Log &log, ErrorType error)
PrimClex * primclex
Definition: settings.cc:135