CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
ConfigEnumEquivalents_test.cpp
Go to the documentation of this file.
1 #define BOOST_TEST_DYN_LINK
2 #include <boost/test/unit_test.hpp>
3 
8 
12 #include "casm/clex/PrimClex.hh"
13 #include "casm/app/AppIO.hh"
15 #include "Common.hh"
16 
17 using namespace CASM;
18 using namespace test;
19 
20 BOOST_AUTO_TEST_SUITE(ConfigEnumEquivalentsTest)
21 
23 
24  // tests ConfigEnumAllOccupations and ConfigEnumEquivalents
25 
26  // read test file
27  fs::path test_cases_path("tests/unit/clex/ConfigEnumEquivalents_test_cases.json");
28  jsonParser tests(test_cases_path);
29  fs::path test_proj_dir("tests/unit/clex/test_proj");
30 
31  for(auto test_it = tests.begin(); test_it != tests.end(); ++test_it) {
32 
33  // input and expected output data
34  jsonParser &j = *test_it;
35 
36  // if false: print calculated results if no test data; if true: suppress
37  bool quiet = false;
38  j.get_else(quiet, "quiet", false);
39 
40  BOOST_CHECK_MESSAGE(j.contains("title"), "test case 'title' is required");
41  BOOST_CHECK_MESSAGE(j.contains("prim"), "test case 'prim' is required");
42  BOOST_CHECK_MESSAGE(j.contains("max_vol"), "test case 'max_vol' is required");
43 
44  // generate prim
45  Structure prim(read_prim(j["prim"]));
46 
47  // clean up test proj
48  if(fs::exists(test_proj_dir / ".casm")) {
49  fs::remove_all(test_proj_dir);
50  }
51 
52  fs::create_directory(test_proj_dir);
53 
54  j["prim"].write(test_proj_dir / "prim.json");
55 
56  // build a project
57  ProjectBuilder builder(test_proj_dir, j["title"].get<std::string>(), "formation_energy");
58  builder.build();
59 
60  // read primclex
61  PrimClex primclex(test_proj_dir, null_log());
62  double tol = primclex.crystallography_tol();
63 
64  // generate supercells
65  ScelEnumProps enum_props(1, j["max_vol"].get<int>() + 1);
66  primclex.generate_supercells(enum_props);
67 
68  // generate configurations
69  std::map<Index, Index> prim_count;
70  std::map<Index, Index> total_count;
71 
72  for(Index i = 0; i < primclex.get_supercell_list().size(); ++i) {
73 
74  // for each supercell, enumerate unique configurations
75  Supercell &scel = primclex.get_supercell(i);
76  //std::cout << "scel: " << scel.get_name() << std::endl;
77  //std::cout << "max_occupation: " << scel.max_allowed_occupation() << std::endl;
78 
79  ConfigEnumAllOccupations enumerator(scel);
80  total_count[i] = 0;
81  prim_count[i] = 0;
82 
83  // add prim configurations from smaller supercells that tile scel
84  for(Index j = 0; j < i; j++) {
85  jsonParser json;
86  ScelEnumEquivalents e(primclex.get_supercell(j));
87  for(const auto &tunit : e) {
88  if(is_supercell(scel.get_real_super_lattice(), tunit.get_real_super_lattice(), tol).first) {
89  total_count[i] += prim_count[j];
90  }
91  }
92  }
93 
94  for(auto &config : enumerator) {
95  ConfigEnumEquivalents equiv_enum(config);
96  for(auto &equiv : equiv_enum) {
97  ++total_count[i];
98  ++prim_count[i];
99  (void) equiv;
100  }
101  }
102 
103  auto max_occ = scel.max_allowed_occupation();
104  long prod = std::accumulate(max_occ.begin(), max_occ.end(), long {1}, [](long a, int b) {
105  return a * (b + 1);
106  });
107 
108  // check that the number of Configurations enumerated is equal to
109  // the product of the number of allowed occupants on each site in the
110  // Supercell
111  BOOST_CHECK_EQUAL(total_count[i], prod);
112  }
113 
114  // run checks:
115  // ... add more here ...
116 
117  // clean up test proj
118  if(fs::exists(test_proj_dir / ".casm")) {
119  fs::remove_all(test_proj_dir);
120  }
121  }
122 }
123 
124 BOOST_AUTO_TEST_SUITE_END()
Enumerate over all possible occupations in a particular Supercell.
boost::container::stable_vector< Supercell > & get_supercell_list()
Access entire supercell_list.
Definition: PrimClex.cc:299
ReturnArray< int > max_allowed_occupation() const
Definition: Supercell.cc:61
iterator end()
Returns iterator to end of JSON object or JSON array.
Definition: jsonParser.cc:465
void write(const std::string &file_name, unsigned int indent=2, unsigned int prec=12) const
Write json to file.
Definition: jsonParser.cc:191
Structure specifies the lattice and atomic basis of a crystal.
Definition: Structure.hh:29
Data structure for holding supercell enumeration properties.
PrimClex * primclex
Definition: settings.cc:101
Definition: Common.hh:7
const Lattice & get_real_super_lattice() const
Definition: Supercell.hh:267
Main CASM namespace.
Definition: complete.cpp:8
Log & null_log()
Definition: Log.hh:211
Represents a supercell of the primitive parent crystal structure.
Definition: Supercell.hh:37
iterator begin()
Returns const_iterator to beginning of JSON object or JSON array.
Definition: jsonParser.cc:440
void generate_supercells(const ScelEnumProps &enum_props)
Use the given CSPECS.
Definition: PrimClex.cc:492
BasicStructure< Site > read_prim(fs::path filename)
Definition: AppIO.cc:11
double tol
double crystallography_tol() const
Definition: PrimClex.hh:124
Sets up directories and files for a new CASM project.
BOOST_AUTO_TEST_CASE(Test1)
EigenIndex Index
For long integer indexing:
bool get_else(T &t, const std::string &key, const T &default_value, Args...args) const
Definition: jsonParser.hh:749
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:52
void build() const
Builds a new CASM project.
const Supercell & get_supercell(Index i) const
const Access supercell by index
Definition: PrimClex.cc:311
bool contains(const std::string &name) const
Return true if JSON object contains 'name'.
Definition: jsonParser.cc:500
std::pair< bool, Eigen::MatrixXi > is_supercell(const Lattice &scel, const Lattice &unit, double tol)
Check if scel is a supercell of unitcell unit and some integer transformation matrix T...
Definition: Lattice.cc:1196
Enumerate equivalent Supercell.
Enumerate all equivalent Configurations in a Supercell, as generated by Supercell factor group symmet...