CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
Common.cpp
Go to the documentation of this file.
1 #include "Common.hh"
2 
3 #include <thread>
4 #include <chrono>
8 
9 namespace test {
10 
22  bool check(std::string test,
23  const jsonParser &expected,
24  const jsonParser &calculated,
25  fs::path test_cases_path,
26  bool quiet,
27  double tol) {
28  if(!expected.contains(test) && !quiet) {
29  std::cout << "Test case: " << expected["title"] << " has no \"" << test << "\" test data." << std::endl;
30  std::cout << "To use the current CASM results, add the following to the " << expected["title"]
31  << " test case in " << test_cases_path << std::endl;
32  jsonParser j = jsonParser::object();
33  j[test] = calculated;
34  std::cout << j << std::endl;
35  }
36 
37  bool ok;
38  fs::path diff_path;
39  if(tol == 0.0) {
40  ok = (expected[test] == calculated);
41  if(!ok) {
42  diff_path = find_diff(expected[test], calculated);
43  }
44  }
45  else {
46  ok = expected[test].almost_equal(calculated, tol);
47  if(!ok) {
48  fs::path diff_path = find_diff(expected[test], calculated, tol);
49  }
50  }
51 
52  if(!ok) {
53  std::cout << "Difference at: " << diff_path << std::endl;
54  std::cout << "Expected: \n" << expected[test].at(diff_path) << "\n"
55  << "Found: \n" << calculated.at(diff_path) << std::endl;
56  }
57  BOOST_CHECK(ok);
58 
59  return ok;
60  }
61 
64  fs::path proj_dir(fs::path init) {
65 
66  fs::path result = fs::absolute(init);
67  int index = 0;
68  std::string dot = ".";
69  while(!fs::create_directories(result)) {
70  result = fs::path(init.string() + dot + std::to_string(index));
71  ++index;
72  }
73 
74  return result;
75  }
76 
78  void Proj::make() {
79 
80  if(!fs::exists(dir / ".casm")) {
81  jsonParser json;
82  write_prim(prim, json, FRAC);
83  json["description"] = desc;
84 
85  json.write(dir / "prim.json");
86 
87  // build a project
88  ProjectBuilder builder(dir, title, "formation_energy");
89  builder.build();
90  }
91 
92  // (re)load ProjectSettings
93  m_set = notstd::make_cloneable<ProjectSettings>(dir);
94  }
95 
97  void check_symgroup(const jsonParser &json, int N_op, int N_class) {
98  BOOST_CHECK_EQUAL(json["character_table"].size(), N_class);
99  BOOST_CHECK_EQUAL(json["conjugacy_class"].size(), N_class);
100 
101  BOOST_CHECK_EQUAL(json["symop"].size(), N_op);
102  BOOST_CHECK_EQUAL(json["symop"][0]["type"].get<std::string>(), "identity");
103 
104  BOOST_CHECK_EQUAL(json["inverse"].size(), N_op);
105  BOOST_CHECK_EQUAL(json["multiplication_table"].size(), N_op);
106  for(auto i = 0; i < json["multiplication_table"].size(); ++i) {
107  BOOST_CHECK_EQUAL(json["multiplication_table"][i].size(), N_op);
108  }
109  }
110 
112  void Proj::check_init() {
113 
114  make();
115 
116  m_set->set_casm_prefix(fs::current_path());
117 
118  // handle scons and autotools
119  if(!fs::exists(m_set->casm_libdir().first / "libcasm.dylib") &&
120  !fs::exists(m_set->casm_libdir().first / "libcasm.so")) {
121  m_set->set_casm_libdir(fs::current_path() / ".libs");
122  }
123 
124  m_set->commit();
125 
126  BOOST_CHECK_EQUAL(true, fs::exists(dir));
127 
128  // prim and settings
129  BOOST_CHECK_EQUAL(true, fs::exists(m_dirs.prim()));
130  BOOST_CHECK_EQUAL(true, fs::exists(m_dirs.project_settings()));
131 
132  // symmetry
133  BOOST_CHECK_EQUAL(true, fs::exists(m_dirs.crystal_point_group()));
134  BOOST_CHECK_EQUAL(true, fs::exists(m_dirs.factor_group()));
135  BOOST_CHECK_EQUAL(true, fs::exists(m_dirs.lattice_point_group()));
136 
137  // composition axes
138  BOOST_CHECK_EQUAL(true, fs::exists(m_dirs.composition_axes()));
139  }
140 
142  void Proj::check_symmetry() {
143  m_p.popen(cd_and() + "ccasm sym");
144  BOOST_CHECK_EQUAL(m_p.exit_code(), 0);
145  }
146 
148  void Proj::_check_symmetry(int lat_pg_op, int lat_pg_class,
149  int xtal_pg_op, int xtal_pg_class,
150  int fg_op, int fg_class,
151  std::string lat_pg_name, std::string xtal_pg_name) {
152 
153 
154  check_symgroup(jsonParser(m_dirs.lattice_point_group()), lat_pg_op, lat_pg_class);
155  check_symgroup(jsonParser(m_dirs.crystal_point_group()), xtal_pg_op, xtal_pg_class);
156  check_symgroup(jsonParser(m_dirs.factor_group()), fg_op, fg_class);
157 
158  m_p.popen(cd_and() + "ccasm sym");
159 
160  BOOST_CHECK_EQUAL(boost::regex_search(m_p.gets(), m_match, boost::regex(R"(Lattice point group is:\s+)" + lat_pg_name)), true);
161  BOOST_CHECK_EQUAL(boost::regex_search(m_p.gets(), m_match, boost::regex(R"(Crystal point group is:\s+)" + xtal_pg_name)), true);
162  }
163 
165  void Proj::check_composition() {
166  m_p.popen(cd_and() + "ccasm composition -d");
167  BOOST_CHECK_EQUAL(m_p.exit_code(), 0);
168  }
169 
172  void Proj::check_enum() {
173  m_p.popen(cd_and() + "ccasm enum --method ScelEnum --max 2");
174  BOOST_CHECK_EQUAL(m_p.exit_code(), 0);
175 
176  m_p.popen(cd_and() + "ccasm enum --method ConfigEnumAllOccupations --all");
177  BOOST_CHECK_EQUAL(m_p.exit_code(), 0);
178  }
179 
181  void Proj::check_select() {
182  m_p.popen(cd_and() + "ccasm select --set-on");
183  BOOST_CHECK_EQUAL(m_p.exit_code(), 0);
184 
185  m_p.popen(cd_and() + "ccasm select --set-off");
186  BOOST_CHECK_EQUAL(m_p.exit_code(), 0);
187  }
188 
190  void Proj::check_query() {
191  m_p.popen(cd_and() + "ccasm query --columns comp");
192  BOOST_CHECK_MESSAGE(m_p.exit_code() == 0, m_p.gets());
193 
194  }
195 
196 }
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 commit() const
Save settings to project settings file.
fs::path dir
Definition: Proj.hh:34
boost::filesystem::path find_diff(const jsonParser &A, const jsonParser &B, boost::filesystem::path diff=boost::filesystem::path())
Return the location at which jsonParser 'A' != 'B' as a boost::filesystem::path.
Definition: jsonParser.cc:380
Popen m_p
Definition: Proj.hh:73
bool set_casm_prefix(fs::path dir)
Set casm prefix (empty string to use default)
fs::path composition_axes() const
Return composition axes file path.
void write(const std::string &file_name, unsigned int indent=2, unsigned int prec=12) const
Write json to file.
Definition: jsonParser.cc:191
std::string gets() const
Returns the stdout resulting from the last popen call.
Definition: Popen.cc:44
Definition: Common.hh:7
void write_prim(const BasicStructure< Site > &prim, fs::path filename, COORD_TYPE mode)
Write prim.json to file.
Definition: AppIO.cc:107
void _check_symmetry(int lat_pg_op, int lat_pg_class, int xtal_pg_op, int xtal_pg_class, int fg_op, int fg_class, std::string lat_pg_name, std::string xtal_pg_name)
jsonParser & at(const fs::path &path)
Definition: jsonParser.cc:321
void popen(std::string _command)
Execute popen for a given command.
Definition: Popen.cc:19
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: EnumIO.hh:83
DirectoryStructure m_dirs
Definition: Proj.hh:75
fs::path prim() const
Return prim.json path.
void make()
Build a CASM project at 'proj_dir' using the prim.
fs::path proj_dir(fs::path init)
Create a new project directory, appending ".(#)" to ensure it is a new project.
virtual void check_enum()
Check "casm enum".
double tol
fs::path crystal_point_group() const
Return crystal_point_group.json path.
void check_symgroup(const jsonParser &json, int N_op, int N_class)
Check some aspects of a SymGroup json, including the expected number of conjugacy classes and operati...
fs::path project_settings() const
Return project_settings.json path.
Sets up directories and files for a new CASM project.
virtual void check_symmetry()
Check symmetry.
std::pair< fs::path, std::string > casm_libdir() const
Get casm libdir.
std::string cd_and() const
Definition: Proj.hh:39
bool set_casm_libdir(fs::path dir)
Set casm libdir (empty string to use default)
virtual void check_query()
Check "casm query".
T dot(const Tensor< T > &LHS, const Tensor< T > &RHS)
Definition: Tensor.hh:961
BasicStructure< Site > prim
Definition: Proj.hh:35
boost::smatch m_match
Definition: Proj.hh:74
fs::path lattice_point_group() const
Return lattice_point_group.json path.
bool check(std::string test, const jsonParser &expected, const jsonParser &calculated, fs::path test_cases_path, bool quiet, double tol=0.0)
Check expected JSON vs calculated JSON using BOOST_CHECK_EQUAL.
std::string title
Definition: Proj.hh:36
int exit_code() const
Returns pclose(fp)/256.
Definition: Popen.cc:55
virtual void check_composition()
Check "casm composition".
std::string desc
Definition: Proj.hh:37
bool almost_equal(const jsonParser &B, double tol) const
Definition: jsonParser.cc:211
bool contains(const std::string &name) const
Return true if JSON object contains 'name'.
Definition: jsonParser.cc:500
virtual void check_select()
Check "casm select".
notstd::cloneable_ptr< ProjectSettings > m_set
Definition: Proj.hh:76
fs::path factor_group() const
Return factor_group.json path.
virtual void check_init()
Check project initialization.