CASM  1.1.0
A Clusters Approach to Statistical Mechanics
Configuration_json_io.cc
Go to the documentation of this file.
2 
3 #include "casm/casm_io/Log.hh"
6 #include "casm/clex/ConfigDoF.hh"
9 #include "casm/clex/Supercell.hh"
11 
12 namespace CASM {
13 
19  jsonParser const &json,
20  std::shared_ptr<const Structure> const &shared_prim) {
21  return std::move(*jsonMake<Configuration>::make_from_json(json, shared_prim));
22 }
23 
28 std::unique_ptr<Configuration> jsonMake<Configuration>::make_from_json(
29  jsonParser const &json,
30  std::shared_ptr<const Structure> const &shared_prim) {
31  auto &log = CASM::log();
32  ParentInputParser parser{json};
33  std::runtime_error error_if_invalid{
34  "Error reading Configuration from JSON input"};
35 
37  parser.require(T, "transformation_matrix_to_supercell");
38  report_and_throw_if_invalid(parser, log, error_if_invalid);
39  auto shared_supercell = std::make_shared<Supercell const>(shared_prim, T);
40 
41  ConfigDoF configdof = make_configdof(*shared_supercell);
42  parser.optional(configdof, "dof");
43 
44  if (configdof.n_vol() != shared_supercell->volume()) {
45  std::stringstream msg;
46  msg << "Error reading Configuration from JSON: "
47  << "supercell size (" << shared_supercell->volume() << ") and "
48  << "configdof size (" << configdof.n_vol() << ") are inconsistent.";
49  msg << "supercell_name: " << json["supercell_name"].get<std::string>();
50  parser.error.insert(msg.str());
51  }
52  report_and_throw_if_invalid(parser, log, error_if_invalid);
53 
54  return notstd::make_unique<Configuration>(shared_supercell, configdof);
55 }
56 
85 jsonParser &to_json(Configuration const &configuration, jsonParser &json) {
86  if (!json.is_obj()) {
87  throw std::runtime_error(
88  "Error inserting configuration to json: not an object");
89  }
90  Supercell const &supercell = configuration.supercell();
91  SupercellSymInfo const &sym_info = supercell.sym_info();
92  json["supercell_name"] = supercell.name();
93  json["transformation_matrix_to_supercell"] =
95  json["dof"] = configuration.configdof();
96  // TODO: include properties, cache, source?
97 
98  return json;
99 }
100 
120 void from_json(Configuration &configuration, jsonParser const &json) {
122  json, configuration.supercell().shared_prim());
123  // TODO: include properties, cache, source?
124 }
125 
126 } // namespace CASM
std::shared_ptr< Structure const > shared_prim
Index n_vol() const
Integer volume of ConfigDoF.
Definition: ConfigDoF.cc:14
const ConfigDoF & configdof() const
const Access the DoF
const Supercell & supercell() const
Get the Supercell for this Configuration.
std::string name() const
Definition: Named_impl.hh:14
Represents a supercell of the primitive parent crystal structure.
Definition: Supercell.hh:51
std::shared_ptr< Structure const > const & shared_prim() const
Definition: Supercell.cc:115
const SupercellSymInfo & sym_info() const
Definition: Supercell.cc:265
A class that collects all symmetry information for for performing symmetry transformations on the sit...
Eigen::Matrix3l transformation_matrix_to_super() const
long-int transformation from primitive lattice vectors to supercell lattice vectors supercell_lattice...
bool is_obj() const
Check if object type.
Definition: jsonParser.cc:272
T get(Args &&... args) const
Get data from json, using one of several alternatives.
Definition: jsonParser.hh:716
Main CASM namespace.
Definition: APICommand.hh:8
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
Log & log()
Definition: Log.hh:424
void from_json(ClexDescription &desc, const jsonParser &json)
ConfigDoF make_configdof(Structure const &prim, Index volume)
Construct zero-valued ConfigDoF.
void report_and_throw_if_invalid(KwargsParser const &parser, Log &log, ErrorType error)
Matrix< long int, 3, 3 > Matrix3l
Definition: eigen.hh:12
static ReturnType from_json(const jsonParser &json)
Default from_json is equivalent to.
Definition: jsonParser.hh:551
Helper struct for constructing objects that need additional data.
Definition: jsonParser.hh:564
static std::unique_ptr< ValueType > make_from_json(const jsonParser &json)
Default make_from_json is equivalent to.
Definition: jsonParser.hh:567