CASM  1.1.0
A Clusters Approach to Statistical Mechanics
NeighborList.hh
Go to the documentation of this file.
1 #ifndef CASM_NeighborList_HH
2 #define CASM_NeighborList_HH
3 
4 #include <memory>
5 #include <set>
6 #include <vector>
7 
9 
10 namespace CASM {
11 
12 namespace xtal {
13 class UnitCell;
14 class UnitCellCoord;
15 class Superlattice;
16 } // namespace xtal
17 using xtal::UnitCell;
18 using xtal::UnitCellCoord;
19 
40  public:
41  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
42 
43  typedef Index Scalar;
45  typedef Eigen::Matrix<long, Eigen::Dynamic, 1> VectorXType;
46  typedef std::set<UnitCell, std::function<bool(UnitCell, UnitCell)> >
49  typedef NeighborSet::const_iterator const_iterator;
50  typedef std::set<int> SublatIndices;
51 
54  template <typename SublatIterator>
55  PrimNeighborList(const Matrix3Type &W, SublatIterator begin,
56  SublatIterator end);
57 
59  Matrix3Type weight_matrix() const;
60 
62  void expand(UnitCell const &uc);
63 
65  void expand(UnitCellCoord const &uccoord);
66 
69  template <typename UnitCellCoordIterator>
70  void expand(UnitCellCoordIterator begin, UnitCellCoordIterator end);
71 
73  size_type size() const;
74 
76  const_iterator begin() const;
77 
79  const_iterator end() const;
80 
82  const_iterator cbegin() const;
83 
85  const_iterator cend() const;
86 
89  const SublatIndices &sublat_indices() const;
90 
93  static Matrix3Type make_weight_matrix(const Eigen::Matrix3d lat_column_mat,
94  Index max_element_value, double tol);
95 
98  Scalar neighbor_index(UnitCellCoord const &_ucc);
99 
102  template <typename UnitCellCoordIterator>
103  std::vector<Scalar> neighbor_indices(UnitCellCoordIterator _begin,
104  UnitCellCoordIterator _end);
105 
107  std::unique_ptr<PrimNeighborList> clone() const;
108 
109  private:
112  Scalar _neighbor_index(UnitCellCoord const &_ucc) const;
113 
116  bool _expand(UnitCell const &uc);
117 
120  bool _expand(UnitCellCoord const &uccoord);
121 
124  void _expand(Scalar prev_range);
125 
127  Scalar _dist(const UnitCell &A) const;
128 
130  VectorXType _add_dist(const UnitCell &A) const;
131 
133  bool _compare_unitcell(const UnitCell &A, const UnitCell &B) const;
134 
136  static bool _compare_vec(const VectorXType &A, const VectorXType &B);
137 
145 
149 
152 
156 
159 };
160 
178  public:
179  typedef Index size_type;
180 
183  PrimNeighborList const &prim_nlist);
184 
186  SuperNeighborList(const xtal::Superlattice &prim_grid,
187  const PrimNeighborList &prim_nlist);
188 
190  size_type unitcell_index(size_type site_index) const {
191  return site_index % m_prim_grid_size;
192  }
193 
195  size_type sublat_index(size_type site_index) const {
196  return site_index / m_prim_grid_size;
197  }
198 
200  const std::vector<size_type> &sites(size_type unitcell_index) const;
201 
204  const std::vector<size_type> &unitcells(size_type unitcell_index) const;
205 
207  bool overlaps() const;
208 
210  std::unique_ptr<SuperNeighborList> clone() const;
211 
212  private:
216 
225  std::vector<std::vector<size_type> > m_site;
226 
228  std::vector<std::vector<size_type> > m_unitcell;
229 };
230 
243 template <typename SublatIterator>
244 PrimNeighborList::PrimNeighborList(const Matrix3Type &W, SublatIterator begin,
245  SublatIterator end)
246  : m_W(W),
247  m_neighborhood(std::bind(&PrimNeighborList::_compare_unitcell, this,
248  std::placeholders::_1, std::placeholders::_2)),
249  m_range(0),
250  m_sublat_indices(begin, end) {
251  // throw if W is not positive definite
252  Eigen::LLT<Eigen::MatrixXd> llt(W.cast<double>());
253  if (llt.info() != Eigen::Success) {
254  std::cerr << "Error constructing PrimNeighborList: weight matrix is not "
255  "positive definite."
256  << std::endl;
257  std::cerr << "weight matrix: \n" << W << std::endl;
258  throw std::runtime_error(
259  "Error constructing PrimNeighborList: weight matrix is not positive "
260  "definite.");
261  }
262 
263  // store U.inverse()
264  Eigen::MatrixXd U = llt.matrixU();
265  m_Uinv = U.inverse();
266 
267  // add origin unit cell to neighborhood
268  m_neighborhood.insert(UnitCell(0, 0, 0));
269 }
270 
283 template <typename UnitCellCoordIterator>
284 void PrimNeighborList::expand(UnitCellCoordIterator begin,
285  UnitCellCoordIterator end) {
286  // save the old range
287  Scalar prev_range = m_range;
288 
289  bool any_new = false;
290  for (auto it = begin; it != end; ++it) {
291  any_new = _expand(*it) || any_new;
292  }
293 
294  if (!any_new) {
295  return;
296  }
297 
298  // otherwise, ensure all intermediate UnitCell are included
299  _expand(prev_range);
300 }
301 
304 template <typename UnitCellCoordIterator>
305 std::vector<PrimNeighborList::Scalar> PrimNeighborList::neighbor_indices(
306  UnitCellCoordIterator _begin, UnitCellCoordIterator _end) {
307  expand(_begin, _end);
308  std::vector<Scalar> result;
309  std::transform(_begin, _end, std::back_inserter(result),
310  [this](UnitCellCoord const &A) -> Scalar {
311  return this->_neighbor_index(A);
312  });
313  return result;
314 }
315 
317 } // namespace CASM
318 
319 #endif
The PrimNeighborList gives the coordinates of UnitCell that are neighbors of the origin UnitCell.
Definition: NeighborList.hh:39
bool _expand(UnitCell const &uc)
Expand the neighbor list to include the given UnitCell, but do not do additional updates returns true...
Definition: NeighborList.cc:42
Scalar m_range
the neighborhood, m_neighborhood, contains all UnitCell with r <= m_range
std::vector< Scalar > neighbor_indices(UnitCellCoordIterator _begin, UnitCellCoordIterator _end)
Get neighborlist indices of a collection of UnitCells, stored in.
NeighborSet m_neighborhood
the neighborhood of [i, j, k]
Scalar _neighbor_index(UnitCellCoord const &_ucc) const
Get neighborlist index of UnitCellCoord.
Scalar _dist(const UnitCell &A) const
Calculate A.transpose()*M*A.
Scalar neighbor_index(UnitCellCoord const &_ucc)
Get neighborlist index of UnitCellCoord.
const_iterator cend() const
const_iterator over the neighborhood of unit cells
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef Index Scalar
Definition: NeighborList.hh:43
std::unique_ptr< PrimNeighborList > clone() const
Clone.
void expand(UnitCell const &uc)
Expand the neighbor list to include the given UnitCellCoord.
Definition: NeighborList.cc:23
const_iterator cbegin() const
const_iterator over the neighborhood of unit cells
size_type size() const
size of the neighborhood of unit cells
Definition: NeighborList.cc:92
std::set< int > SublatIndices
Definition: NeighborList.hh:50
std::set< UnitCell, std::function< bool(UnitCell, UnitCell)> > NeighborSet
Definition: NeighborList.hh:47
const_iterator end() const
const_iterator over the neighborhood of unit cells
const_iterator begin() const
const_iterator over the neighborhood of unit cells
Definition: NeighborList.cc:97
NeighborSet::size_type size_type
Definition: NeighborList.hh:48
Eigen::MatrixXd m_Uinv
Cholesky decomposition: W = U.transpose()*U, useful for getting bounding box.
const SublatIndices & sublat_indices() const
pair of const_iterators over a range of indices of sublattices to include
SublatIndices m_sublat_indices
the indices of sublattices that should be included
bool _compare_unitcell(const UnitCell &A, const UnitCell &B) const
Convert [i,j,k] -> [r,i,j,k] and then lexicographically compare.
Matrix3Type m_W
Weighting matrix.
Matrix3Type weight_matrix() const
Return the weighting matrix W used to define the canonical order.
Definition: NeighborList.cc:13
static bool _compare_vec(const VectorXType &A, const VectorXType &B)
Lexicographical comparison.
Eigen::Matrix< long, Eigen::Dynamic, 1 > VectorXType
Definition: NeighborList.hh:45
Eigen::Matrix3l Matrix3Type
Definition: NeighborList.hh:44
NeighborSet::const_iterator const_iterator
Definition: NeighborList.hh:49
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.
VectorXType _add_dist(const UnitCell &A) const
Return [r, i, j, k], where r = _dist(A)
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 unitcell_index from site_index.
std::vector< std::vector< size_type > > m_site
m_site[unitcell_index][neighbor site index]
std::vector< std::vector< size_type > > m_unitcell
m_unitcell[unitcell_index][neighbor unitcell index]
size_type sublat_index(size_type site_index) const
Get sublattice index from site_index.
const std::vector< size_type > & unitcells(size_type unitcell_index) const
const Access the list of unitcells neighboring a particular unit cell
bool overlaps() const
Returns true if periodic images of the neighbor list overlap.
std::unique_ptr< SuperNeighborList > clone() const
Clone.
SuperNeighborList(Eigen::Matrix3l const &transformation_matrix_to_super, PrimNeighborList const &prim_nlist)
Constructor.
size_type m_prim_grid_size
store prim grid size for site index -> unitcell index conversion unitcell_index = site_index % m_prim...
const std::vector< size_type > & sites(size_type unitcell_index) const
const Access the list of sites neighboring a particular unit cell
Unit Cell Coordinates.
Unit Cell Indices.
MatrixXiSupercellSymInfoFormatter transformation_matrix_to_super()
Main CASM namespace.
Definition: APICommand.hh:8
Eigen::MatrixXd MatrixXd
Eigen::Matrix3d Matrix3d
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
Matrix< long int, 3, 3 > Matrix3l
Definition: eigen.hh:12
Definition: stream_io.hh:24
ProjectSettings & set
Definition: settings.cc:137