CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
DoF.cc
Go to the documentation of this file.
1 #include "casm/basis_set/DoF.hh"
2 
3 // needed for to/from json... this should be fixed
5 
6 namespace CASM {
7 
8  // ** OccupantDoF **
9 
11  // because we want to be able to do: void from_json(DoF *dof, const jsonParser &json)
12  //
13 
14  // int version
15  template<> jsonParser &OccupantDoF<int>::to_json(jsonParser &json) const {
16  json.put_obj();
17  json["DoF_type"] = "OccupantDoF";
18  json["DoF_template_type"] = "int";
19  json["m_type_name"] = m_type_name;
20  json["m_domain"] = m_domain;
21  json["m_current_state"] = m_current_state;
22  return json;
23  }
24 
26  return dof.to_json(json);
27  }
28 
29  void from_json(OccupantDoF<int> &dof, const jsonParser &json) {
30  try {
31  std::string name = json["m_type_name"].get<std::string>();
32  Array<int> domain = json["m_domain"].get<Array<int> >();
33  int current_state = json["m_current_state"].get<int>();
34 
35  dof = OccupantDoF<int>(name, domain, current_state);
36  }
37  catch(...) {
39  throw;
40  }
41  }
42 
43  // molecule version
45  json.put_obj();
46  json["DoF_type"] = "OccupantDoF";
47  json["DoF_template_type"] = "Molecule";
48  json["m_type_name"] = m_type_name;
49 
50  json["m_domain"] = m_domain;
51  json["m_current_state"] = m_current_state;
52 
53  return json;
54  }
55 
57  return dof.to_json(json);
58  }
59 
60  // Note: as a hack this expects dof.domain[0] to be present and have the right lattice!!!
61  // it's just used to set the lattice for all the Molecules
62  void from_json(OccupantDoF<Molecule> &dof, const jsonParser &json) {
63  try {
64  std::string name = json["m_type_name"].get<std::string>();
65 
66  int current_state = json["m_current_state"].get<int>();
67 
68  Array<Molecule> domain;
69  //std::cout<<"The size of the dof is "<<dof.get_domain().size()<<std::endl;
71  Molecule mol(*(dof[0].home()));
72  //domain.clear();
73  //std::cout<<"Done initializing molecule"<<std::endl;
74  for(int i = 0; i < json["m_domain"].size(); i++) {
75  from_json(mol, json["m_domain"][i]);
76  domain.push_back(mol);
77  }
78  //std::cout<<"Done adding the molecules to the list, trying to create"
79  // <<" OccupantDoF<Molecule>"<<std::endl;
80 
81  dof = OccupantDoF<Molecule>(name, domain, current_state);
82  }
83  catch(...) {
85  throw;
86  }
87  }
88 
89 
90  // ** ContinuousDoF **
91 
93  return dof.to_json(json);
94  }
95 
96  void from_json(ContinuousDoF &dof, const jsonParser &json) {
97  try {
98  dof = ContinuousDoF(json["m_type_name"].get<std::string>(), json["min"].get<double>(), json["max"].get<double>());
99  }
100  catch(...) {
102  throw;
103  }
104  }
105 
106 
107  // ** DoF **
108 
110  jsonParser &to_json(const DoF *dof, jsonParser &json) {
111  return dof->to_json(json);
112  }
113 
117  void from_json(DoF *dof, const jsonParser &json, const Lattice &lat) {
118  try {
119  if(json["DoF_type"] == "OccupantDoF") {
120  if(json["DoF_template_type"] == "int") {
121 
122  // prepare a OccupantDoF<int> and then read from json
123  OccupantDoF<int> tdof;
124  CASM::from_json(tdof, json);
125 
126  // copy to dof
127  dof = new OccupantDoF<int>(tdof);
128 
129  }
130  else if(json["DoF_template_type"] == "Molecule") {
131 
132  // prepare a OccupantDoF<Molecule> and then read from json
133  Array<Molecule> init_domain;
134  init_domain.push_back(Molecule(lat));
135  OccupantDoF<Molecule> tdof(json["m_type_name"].get<std::string>(), init_domain);
136 
137  // this expects dof.domain[0] has a Molecule with the right lattice
138  CASM::from_json(tdof, json);
139 
140  // copy to dof
141  dof = new OccupantDoF<Molecule>(tdof);
142 
143  }
144  else {
145  std::cerr << "Error in 'jsonParser from_json(DoF *dof, const jsonParser &json, const Lattice &lat)'" << std::endl;
146  std::cerr << "Unrecognized 'DoF_template_type': '" << json["DoF_template_type"] << "'." << std::endl;
147  exit(1);
148  }
149  }
150  else if(json["DoF_type"] == "ContinuousDoF") {
151 
152  // prepare a OccupantDoF<Molecule> and then read from json
153  ContinuousDoF tdof(json["m_type_name"].get<std::string>(), 0.0, 0.0);
154  CASM::from_json(tdof, json);
155 
156  // copy to dof
157  dof = new ContinuousDoF(tdof);
158 
159  }
160  else {
161  std::cerr << "Error in 'void from_json(DoF *dof, const jsonParser &json, const Lattice &lat)'" << std::endl;
162  std::cerr << "Unrecognized 'DoF_type': '" << json["DoF_type"] << "'." << std::endl;
163  std::cerr << "Options are: 'OccupantDoF', or 'ContinuousDoF'." << std::endl;
164  exit(1);
165  }
166  }
167  catch(...) {
169  throw;
170  }
171  }
172 
173 }
174 
size_type size() const
Returns array size if *this is a JSON array, object size if *this is a JSON object, 1 otherwise.
Definition: jsonParser.cc:430
void from_json(ClexDescription &desc, const jsonParser &json)
virtual jsonParser & to_json(jsonParser &json) const =0
void push_back(const T &toPush)
Definition: Array.hh:513
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
Main CASM namespace.
Definition: complete.cpp:8
Definition: DoF.hh:21
T get(Args...args) const
Get data from json, using one of several alternatives.
Definition: jsonParser.hh:729
jsonParser & to_json(jsonParser &json) const
Definition: DoF.hh:399
jsonParser & to_json(jsonParser &json) const override
jsonParser & put_obj()
Puts new empty JSON object.
Definition: jsonParser.hh:276