CASM  1.1.0
A Clusters Approach to Statistical Mechanics
LinearIndexConverter.cc
Go to the documentation of this file.
2 
3 #include <exception>
4 #include <stdexcept>
5 #include <string>
6 #include <vector>
7 
8 namespace CASM {
9 namespace xtal {
10 
12  int basis_sites_in_prim) {
13  if (basis_sites_in_prim < 1) {
14  throw std::runtime_error(
15  "UnitCellCoords require at least one basis site in the tiling unit, "
16  "but you specified " +
17  std::to_string(basis_sites_in_prim));
18  }
19  return;
20 }
21 
23  if (ix < 0 || ix >= this->total_sites()) {
24  throw std::runtime_error("The specified index is out of range. There are " +
25  std::to_string(this->total_sites()) +
26  " available sites, but you specified index " +
27  std::to_string(ix));
28  }
29  return;
30 }
31 
33  const UnitCellCoord &bijk) const {
34  if (bijk.sublattice() >= m_basis_sites_in_prim) {
35  throw std::runtime_error(
36  "The given UnitCellCoord has a sublattice index that exceeds the "
37  "expected value");
38  }
39 
41  m_bijk_to_linear_index.find(bijk) == m_bijk_to_linear_index.end()) {
42  throw std::runtime_error(
43  "The given UnitCellCoord lands outside of the superlattice, and you "
44  "requested to not allow this");
45  }
46 
47  return;
48 }
49 
50 std::vector<UnitCellCoord>
52  const impl::OrderedLatticePointGenerator &make_point,
53  int basis_sites_in_prim) {
54  std::vector<UnitCellCoord> all_bijk_values;
55  auto total_lattice_points = make_point.size();
56 
57  for (int b = 0; b < basis_sites_in_prim; ++b) {
58  for (Index ijk_ix = 0; ijk_ix < total_lattice_points; ++ijk_ix) {
59  auto ijk = make_point(ijk_ix);
60  all_bijk_values.emplace_back(b, std::move(ijk));
61  }
62  }
63 
64  return all_bijk_values;
65 }
66 
69 }
70 
73 }
74 
76  const UnitCellCoord &bijk) const {
77  // equivalent to requesting index of bijk, then using the index to get the
78  // bijk within the superlattice
79  return UnitCellCoord(bijk.sublattice(),
80  this->m_bring_within_f(bijk.unitcell()));
81 }
82 
85  return m_linear_index_to_bijk[ix];
86 }
87 
89  // Make sure the UntiCellCoord is allowed
90  this->_throw_if_incompatible_bijk(bijk);
91 
92  // If only requesting UnitCellCoord conversions that are within, you already
93  // have the value
95  return m_bijk_to_linear_index.at(bijk);
96  }
97 
98  // Otherwise you have to bring the UnitCellCoord within the superlattice
99  // before checking, but maybe you already hit it
100  auto found_it = m_bijk_to_linear_index_outside_of_superlattice.find(bijk);
101  if (found_it != m_bijk_to_linear_index_outside_of_superlattice.end()) {
102  return found_it->second;
103  }
104 
105  // You've never seen this UnitCellCoord before. Bring it within the
106  // superlattice, figure out its index, and cache the result for the future
107  auto bijk_within = m_bring_within_f(bijk);
108  auto ix_within = m_bijk_to_linear_index.at(bijk_within);
109 
111  return ix_within;
112 }
113 } // namespace xtal
114 } // namespace CASM
Unit Cell Coordinates.
const UnitCell & unitcell() const
UnitCellCoord bring_within(const UnitCellCoord &bijk) const
std::vector< UnitCellCoord > m_linear_index_to_bijk
Convert from linear index to UnitCellCoord.
void _throw_if_incompatible_index(Index ix) const
Throws exception if the specified index is out of the allowed range.
static void _throw_if_bad_basis_sites_in_prim(int basis_sites_in_prim)
void _throw_if_incompatible_bijk(const UnitCellCoord &bijk) const
std::unordered_map< UnitCellCoord, Index > m_bijk_to_linear_index
Convert from UnitCellCoord to linear index.
std::unordered_map< UnitCellCoord, Index > m_bijk_to_linear_index_outside_of_superlattice
static std::vector< UnitCellCoord > _make_all_ordered_bijk_values(const impl::OrderedLatticePointGenerator &make_point, int basis_sites_in_prim)
Index total_sites() const
Returns the total number of sites within the superlattice.
const UnitCellCoord & operator()(Index ix) const
Given the linear index, retreive the corresponding UnitCellCoord.
IntegralCoordinateWithin_f m_bring_within_f
Functor to bring UnitCellCoord values back into the superlattice.
long int size() const
Returns the total number of unique lattice points that can be generated.
std::string to_string(ENUM val)
Return string representation of enum class.
Definition: io_traits.hh:172
Main CASM namespace.
Definition: APICommand.hh:8
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39