CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
NeighborList.hh
Go to the documentation of this file.
1 #ifndef CASM_NeighborList_HH
2 #define CASM_NeighborList_HH
3 
5 
6 namespace CASM {
7 
8  class PrimGrid;
9 
14  class PrimNeighborList {
25 
26  public:
27 
28  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
29 
30  typedef Index Scalar;
32  typedef Eigen::Matrix<long, Eigen::Dynamic, 1> VectorXType;
33  typedef std::set<UnitCell, std::function<bool (UnitCell, UnitCell)> > NeighborSet;
34  typedef NeighborSet::size_type size_type;
35  typedef NeighborSet::const_iterator const_iterator;
36  typedef std::set<int> SublatIndices;
37 
38 
41  template<typename SublatIterator>
42  PrimNeighborList(const Matrix3Type &W, SublatIterator begin, SublatIterator end);
43 
44 
46  Matrix3Type weight_matrix() const;
47 
49  void expand(UnitCellCoord uccoord);
50 
52  template<typename UnitCellCoordIterator>
53  void expand(UnitCellCoordIterator begin, UnitCellCoordIterator end);
54 
56  size_type size() const;
57 
59  const_iterator begin() const;
60 
62  const_iterator end() const;
63 
65  const_iterator cbegin() const;
66 
68  const_iterator cend() const;
69 
71  const SublatIndices &sublat_indices() const;
72 
74  static Matrix3Type make_weight_matrix(const Eigen::Matrix3d lat_column_mat, Index max_element_value, double tol);
75 
77  std::unique_ptr<PrimNeighborList> clone() const;
78 
79  private:
80 
82  void _expand(Scalar prev_range);
83 
85  Scalar _dist(const UnitCell &A) const;
86 
88  VectorXType _add_dist(const UnitCell &A) const;
89 
91  bool _compare_unitcell(const UnitCell &A, const UnitCell &B) const;
92 
94  static bool _compare_vec(const VectorXType &A, const VectorXType &B);
95 
96 
102  Matrix3Type m_W;
103 
106 
108  NeighborSet m_neighborhood;
109 
111  Scalar m_range;
112 
114  SublatIndices m_sublat_indices;
115  };
116 
126 
127  public:
128 
129  typedef Index size_type;
130 
132  SuperNeighborList(const PrimGrid &prim_grid, const PrimNeighborList &prim_nlist);
133 
135  size_type unitcell_index(size_type site_index) const {
136  return site_index % m_prim_grid_size;
137  }
138 
140  size_type sublat_index(size_type site_index) const {
141  return site_index / m_prim_grid_size;
142  }
143 
145  const std::vector<size_type> &sites(size_type unitcell_index) const;
146 
148  const std::vector<size_type> &unitcells(size_type unitcell_index) const;
149 
151  bool overlaps() const;
152 
154  std::unique_ptr<SuperNeighborList> clone() const;
155 
156  private:
157 
160  size_type m_prim_grid_size;
161 
168  std::vector< std::vector<size_type> > m_site;
169 
171  std::vector< std::vector<size_type> > m_unitcell;
172 
173  };
174 
175 
176 
187  template<typename SublatIterator>
188  PrimNeighborList::PrimNeighborList(const Matrix3Type &W, SublatIterator begin, SublatIterator end) :
189  m_W(W),
190  m_neighborhood(std::bind(&PrimNeighborList::_compare_unitcell, this, std::placeholders::_1, std::placeholders::_2)),
191  m_range(0),
192  m_sublat_indices(begin, end) {
193 
194  // throw if W is not positive definite
195  Eigen::LLT<Eigen::MatrixXd> llt(W.cast<double>());
196  if(llt.info() != Eigen::Success) {
197  std::cerr << "Error constructing PrimNeighborList: weight matrix is not positive definite." << std::endl;
198  std::cerr << "weight matrix: \n" << W << std::endl;
199  throw std::runtime_error("Error constructing PrimNeighborList: weight matrix is not positive definite.");
200  }
201 
202  // store U.inverse()
203  Eigen::MatrixXd U = llt.matrixU();
204  m_Uinv = U.inverse();
205 
206  // add origin unit cell to neighborhood
207  m_neighborhood.insert(UnitCell(0, 0, 0));
208 
209  }
210 
211 
220  template<typename UnitCellCoordIterator>
221  void PrimNeighborList::expand(UnitCellCoordIterator begin, UnitCellCoordIterator end) {
222 
223  // save the old range
224  Scalar prev_range = m_range;
225 
226  bool any_new = false;
227  for(auto it = begin; it != end; ++it) {
228  if(m_neighborhood.insert(it->unitcell()).second) {
229  any_new = true;
230  }
231  }
232 
233  if(!any_new) {
234  return;
235  }
236 
237  // otherwise, ensure all intermediate UnitCell are included
238  _expand(prev_range);
239  }
240 
242 }
243 
244 #endif
Eigen::MatrixXd MatrixXd
const SublatIndices & sublat_indices() const
pair of const_iterators over a range of indices of sublattices to include
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef Index Scalar
Definition: NeighborList.hh:30
size_type m_prim_grid_size
store prim grid size for site index -> unitcell index conversion unitcell_index = site_index % m_prim...
Scalar _dist(const UnitCell &A) const
Calculate A.transpose()*M*A.
std::unique_ptr< PrimNeighborList > clone() const
Clone.
std::unique_ptr< SuperNeighborList > clone() const
Clone.
Unit Cell Coordinates.
const_iterator cend() const
const_iterator over the neighborhood of unit cells
Definition: NeighborList.cc:97
Scalar m_range
the neighborhood, m_neighborhood, contains all UnitCell with r <= m_range
const_iterator cbegin() const
const_iterator over the neighborhood of unit cells
Definition: NeighborList.cc:92
Main CASM namespace.
Definition: complete.cpp:8
std::vector< std::vector< size_type > > m_unitcell
m_unitcell[unitcell_index][neighbor unitcell index]
NeighborSet::size_type size_type
Definition: NeighborList.hh:34
NeighborSet::const_iterator const_iterator
Definition: NeighborList.hh:35
void expand(UnitCellCoord uccoord)
Expand the neighbor list to include the given UnitCellCoord.
Definition: NeighborList.cc:14
Unit Cell Indices.
bool _compare_unitcell(const UnitCell &A, const UnitCell &B) const
Convert [i,j,k] -> [r,i,j,k] and then lexicographically compare.
SuperNeighborList(const PrimGrid &prim_grid, const PrimNeighborList &prim_nlist)
Constructor.
The SuperNeighborList gives the linear indices of neighboring sites and unitcells in a particular Sup...
Matrix3Type m_W
Weighting matrix.
double tol
void _expand(Scalar prev_range)
Ensure that all intermediate UnitCell are included in our neighborhood.
Definition: NeighborList.cc:29
static bool _compare_vec(const VectorXType &A, const VectorXType &B)
Lexicographical comparison.
Eigen::Matrix< long, Eigen::Dynamic, 1 > VectorXType
Definition: NeighborList.hh:32
NeighborSet m_neighborhood
the neighborhood of [i, j, k]
std::vector< std::vector< size_type > > m_site
m_site[unitcell_index][neighbor site index]
const std::vector< size_type > & unitcells(size_type unitcell_index) const
const Access the list of unitcells neighboring a particular unit cell
EigenIndex Index
For long integer indexing:
VectorXType _add_dist(const UnitCell &A) const
Return [r, i, j, k], where r = _dist(A)
static Matrix3Type make_weight_matrix(const Eigen::Matrix3d lat_column_mat, Index max_element_value, double tol)
Returns a NeighborList weighting matrix appropriate for a particular lattice.
std::set< UnitCell, std::function< bool(UnitCell, UnitCell)> > NeighborSet
Definition: NeighborList.hh:33
Eigen::Matrix3d Matrix3d
size_type size() const
size of the neighborhood of unit cells
Definition: NeighborList.cc:77
The PrimNeighborList gives the coordinates of UnitCell that are neighbors of the origin UnitCell...
Definition: NeighborList.hh:24
std::set< int > SublatIndices
Definition: NeighborList.hh:36
SublatIndices m_sublat_indices
the indices of sublattices that should be included
PrimNeighborList(const Matrix3Type &W, SublatIterator begin, SublatIterator end)
Constructor, specifying the weighting matrix to use and the indices of sublattices to include...
size_type unitcell_index(size_type site_index) const
get unit cell index from site index
bool overlaps() const
Returns true if periodic images of the neighbor list overlap.
const_iterator begin() const
const_iterator over the neighborhood of unit cells
Definition: NeighborList.cc:82
Matrix< long int, 3, 3 > Matrix3l
const std::vector< size_type > & sites(size_type unitcell_index) const
const Access the list of sites neighboring a particular unit cell
Eigen::MatrixXd m_Uinv
Cholesky decomposition: W = U.transpose()*U, useful for getting bounding box.
Matrix3Type weight_matrix() const
Return the weighting matrix W used to define the canonical order.
Definition: NeighborList.cc:9
Eigen::Matrix3l Matrix3Type
Definition: NeighborList.hh:31
size_type sublat_index(size_type site_index) const
get sublat index from site index
const_iterator end() const
const_iterator over the neighborhood of unit cells
Definition: NeighborList.cc:87