CASM  1.1.0
A Clusters Approach to Statistical Mechanics
Coordinate.hh
Go to the documentation of this file.
1 #ifndef COORDINATE_HH
2 #define COORDINATE_HH
3 
4 #include <cassert>
5 #include <cmath>
6 #include <iostream>
7 
11 
12 namespace CASM {
13 namespace xtal {
14 struct SymOp;
15 class Lattice;
16 
17 namespace Coordinate_impl {
18 class FracCoordinate;
20 class CartCoordinate;
22 
23 } // namespace Coordinate_impl
24 
34 class Coordinate {
35  public:
36  typedef Eigen::Vector3d vector_type;
38 
40  static Coordinate origin(const Lattice &_home);
41 
42  // NOTE: Coordinate does not have a default constructor
43  // e.g: this is not allowed-> Coordinate() : home(nullptr) {
44  // is_current[FRAC]=false; is_current[CART]=false;};
45 
47  explicit Coordinate(const Lattice &_home)
48  : m_home(&_home),
49  m_frac_coord(vector_type::Zero()),
50  m_cart_coord(vector_type::Zero()) {
51  assert(m_home && "home lattice pointer was set to null!");
52  }
53 
54  Coordinate(Eigen::Ref<const vector_type> const &_vec, const Lattice &_home,
55  COORD_TYPE _mode);
56 
57  Coordinate(double _x, double _y, double _z, const Lattice &_home,
58  COORD_TYPE _mode);
59 
62 
64  const vector_type &frac() const { return m_frac_coord; }
65 
68  const vector_type &const_frac() const { return m_frac_coord; }
69 
72 
74  const double &frac(size_type index) const { return m_frac_coord(index); }
75 
78  const double &const_frac(size_type index) const {
79  return m_frac_coord(index);
80  }
81 
85 
87  const vector_type &cart() const { return m_cart_coord; }
88 
90  const vector_type &const_cart() const { return m_cart_coord; }
91 
94 
96  const double &cart(size_type index) const { return m_cart_coord(index); }
97 
99  const double &const_cart(size_type index) const {
100  return m_cart_coord(index);
101  }
102 
104  if (_mode == FRAC) {
105  return const_frac();
106  } else if (_mode == CART) {
107  return const_cart();
108  } else {
109  throw std::runtime_error(
110  "Error: In Coordinate::as_vec, mode must be FRAC or CART");
111  }
112  }
113 
115  Coordinate &operator+=(const Coordinate &RHS);
116 
118  Coordinate &operator-=(const Coordinate &RHS);
119 
121  Coordinate operator-() const;
122 
123  bool operator==(const Coordinate &RHS) const;
124 
125  bool operator!=(const Coordinate &RHS) const { return !(*this == RHS); }
126 
127  bool almost_equal(const Coordinate &RHS) const;
128 
130  bool compare(const Coordinate &RHS) const;
131 
135  bool compare(const Coordinate &RHS, Coordinate &translation) const;
136 
138  bool compare_type(const Coordinate &RHS) const;
139 
142  bool within();
143 
148  bool within(Coordinate &translation);
149 
151  bool is_within() const;
152 
155  int voronoi_number() const;
156 
159  int voronoi_number(const Lattice &cell) const;
160 
162  bool voronoi_within();
163 
168  bool voronoi_within(Coordinate &translation);
169 
172  bool is_lattice_shift(double tol = TOL) const;
173 
194  void set_lattice(
195  const Lattice &new_lat,
196  COORD_TYPE mode); // John G, use to specify whether to keep CART or FRAC
197  // the same, when assigning a new lattice
198 
200  const Lattice &home() const {
201  assert(m_home && "Coordinate doesn't have valid home lattice");
202  return *m_home;
203  }
204 
206  const Lattice &lattice() const { return home(); }
207 
208  // term is terminal character, prec is precision, pad is field width -
209  // precision (should be greater than 3)
210  void read(std::istream &stream, COORD_TYPE mode);
211  void print(std::ostream &stream, COORD_TYPE mode, char term = 0,
212  Eigen::IOFormat format = Eigen::IOFormat(7, 12)) const;
213  void print(std::ostream &stream, char term = 0,
214  Eigen::IOFormat format = Eigen::IOFormat(7, 12)) const;
215 
217  void print_axis(std::ostream &stream, COORD_TYPE mode, char term = 0,
218  Eigen::IOFormat format = Eigen::IOFormat(7, 12)) const;
219 
221  double dist(const Coordinate &neighbor) const;
222 
226  double min_dist(const Coordinate &neighbor) const;
227 
233  Coordinate min_translation(const Coordinate &neighbor) const;
234 
239  double robust_min_dist(const Coordinate &neighbor) const;
240 
243  double min_dist2(const Coordinate &neighbor,
244  const Eigen::Ref<const Eigen::Matrix3d> &metric) const;
245 
246  private:
248 
249  void _update_frac() {
250  assert(this->m_home && "No home lattice to access");
252  }
253 
254  void _set_frac(const Eigen::Ref<const vector_type> &f) {
255  m_frac_coord = f;
256  _update_cart();
257  }
258 
259  void _set_frac(size_type ind, double val) {
260  m_frac_coord[ind] = val;
261  _update_cart();
262  }
263 
264  void _set_cart(const Eigen::Ref<const vector_type> &c) {
265  m_cart_coord = c;
266  _update_frac();
267  }
268 
269  void _set_cart(size_type ind, double val) {
270  m_cart_coord[ind] = val;
271  _update_frac();
272  }
273 
278 
279  Lattice const *m_home;
280 
282 };
283 
284 inline Coordinate Coordinate::origin(const Lattice &_home) {
285  return Coordinate(_home);
286 }
287 
288 Coordinate operator*(const SymOp &LHS, const Coordinate &RHS); // AAB
289 
290 inline Coordinate operator+(const Coordinate &LHS, const Coordinate &RHS) {
291  return Coordinate(LHS) += RHS;
292 }
293 
294 inline Coordinate operator-(const Coordinate &LHS, const Coordinate &RHS) {
295  return Coordinate(LHS) -= RHS;
296 }
297 
300 namespace Coordinate_impl {
301 
314  public:
315  explicit FracCoordinate(Coordinate &coord) : m_coord(&coord) {}
316 
318  const Eigen::Ref<const Coordinate::vector_type> &vec) {
319  m_coord->_set_frac(vec);
320  return *this;
321  }
322 
325  return *this;
326  }
327 
329  const Eigen::Ref<const Coordinate::vector_type> &vec) {
330  (m_coord->m_frac_coord) += vec;
332  return *this;
333  }
334 
336  const Eigen::Ref<const Coordinate::vector_type> &vec) {
337  (m_coord->m_frac_coord) -= vec;
339  return *this;
340  }
341 
342  FracCoordinate &operator*=(double val) {
343  (m_coord->m_frac_coord) *= val;
344  (m_coord->m_cart_coord) *= val;
345  return *this;
346  }
347 
348  FracCoordinate &operator/=(double val) {
349  (m_coord->m_frac_coord) /= val;
350  (m_coord->m_cart_coord) /= val;
351  return *this;
352  }
353 
354  operator const Eigen::MatrixBase<Eigen::Vector3d> &() const {
355  return m_coord->m_frac_coord;
356  }
357 
358  operator const Eigen::Vector3d &() const { return m_coord->m_frac_coord; }
359 
360  operator Eigen::Ref<const Eigen::Vector3d>() const {
361  return m_coord->m_frac_coord;
362  }
363 
364  private:
366 };
367 
383  public:
385  Coordinate::size_type index)
386  : m_coord(&coord), m_index(index) {}
387 
389  m_coord->_set_frac(m_index, val);
390  return *this;
391  }
392 
395  return *this;
396  }
397 
400  return *this;
401  }
402 
405  return *this;
406  }
407 
410  return *this;
411  }
412 
415  return *this;
416  }
417 
418  operator const double &() const { return m_coord->m_frac_coord(m_index); }
419 
420  private:
423 };
424 
437  public:
438  explicit CartCoordinate(Coordinate &coord) : m_coord(&coord) {}
439 
441  const Eigen::Ref<const Coordinate::vector_type> &vec) {
442  m_coord->_set_cart(vec);
443  return *this;
444  }
445 
448  return *this;
449  }
450 
452  const Eigen::Ref<const Coordinate::vector_type> &vec) {
453  (m_coord->m_cart_coord) += vec;
455  return *this;
456  }
457 
459  const Eigen::Ref<const Coordinate::vector_type> &vec) {
460  (m_coord->m_cart_coord) -= vec;
462  return *this;
463  }
464 
465  CartCoordinate &operator*=(double val) {
466  (m_coord->m_frac_coord) *= val;
467  (m_coord->m_cart_coord) *= val;
468  return *this;
469  }
470 
471  CartCoordinate &operator/=(double val) {
472  (m_coord->m_frac_coord) /= val;
473  (m_coord->m_cart_coord) /= val;
474  return *this;
475  }
476 
477  operator const Eigen::MatrixBase<Eigen::Vector3d> &() const {
478  return m_coord->m_cart_coord;
479  }
480 
481  operator const Eigen::Vector3d &() const { return m_coord->m_cart_coord; }
482 
483  operator Eigen::Ref<const Eigen::Vector3d>() const {
484  return m_coord->m_cart_coord;
485  }
486 
487  private:
489 };
490 
506  public:
508  Coordinate::size_type index)
509  : m_coord(&coord), m_index(index) {}
510 
512  m_coord->_set_cart(m_index, val);
513  return *this;
514  }
515 
518  return *this;
519  }
520 
523  return *this;
524  }
525 
528  return *this;
529  }
530 
533  return *this;
534  }
535 
538  return *this;
539  }
540 
541  operator const double &() const { return m_coord->m_cart_coord(m_index); }
542 
543  private:
546 };
547 } // namespace Coordinate_impl
548 
551  return Coordinate_impl::FracCoordinate(*this);
552 }
553 
556  Coordinate::size_type index) {
557  return Coordinate_impl::FracCoordinateComponent(*this, index);
558 }
559 
563  return Coordinate_impl::CartCoordinate(*this);
564 }
565 
568  Coordinate::size_type index) {
569  return Coordinate_impl::CartCoordinateComponent(*this, index);
570 }
571 
572 } // namespace xtal
573 } // namespace CASM
574 
575 namespace CASM {
576 namespace sym {
581 
582 template <typename ExternSymOp>
583 xtal::Coordinate copy_apply(const ExternSymOp &op, xtal::Coordinate coord) {
585  coord);
586 }
587 } // namespace sym
588 } // namespace CASM
589 
590 namespace std {
591 template <>
592 struct is_floating_point<CASM::xtal::Coordinate_impl::FracCoordinateComponent> {
593  static const bool value = true;
594 };
595 
596 template <>
597 struct is_floating_point<CASM::xtal::Coordinate_impl::CartCoordinateComponent> {
598  static const bool value = true;
599 };
600 } // namespace std
601 #endif
A class to enable assignment to a component of the Cartesian vector of a Coordinate.
Definition: Coordinate.hh:505
CartCoordinateComponent & operator+=(double val)
Definition: Coordinate.hh:521
CartCoordinateComponent & operator/=(double val)
Definition: Coordinate.hh:536
CartCoordinateComponent & operator=(const CartCoordinateComponent &RHS)
Definition: Coordinate.hh:516
CartCoordinateComponent & operator*=(double val)
Definition: Coordinate.hh:531
CartCoordinateComponent(Coordinate &coord, Coordinate::size_type index)
Definition: Coordinate.hh:507
CartCoordinateComponent & operator=(double val)
Definition: Coordinate.hh:511
CartCoordinateComponent & operator-=(double val)
Definition: Coordinate.hh:526
A class to enable vector assignment to the Cartesian vector of a Coordinate.
Definition: Coordinate.hh:436
CartCoordinate & operator/=(double val)
Definition: Coordinate.hh:471
CartCoordinate & operator=(const Eigen::Ref< const Coordinate::vector_type > &vec)
Definition: Coordinate.hh:440
CartCoordinate & operator-=(const Eigen::Ref< const Coordinate::vector_type > &vec)
Definition: Coordinate.hh:458
CartCoordinate & operator+=(const Eigen::Ref< const Coordinate::vector_type > &vec)
Definition: Coordinate.hh:451
CartCoordinate & operator=(const CartCoordinate &RHS)
Definition: Coordinate.hh:446
CartCoordinate & operator*=(double val)
Definition: Coordinate.hh:465
A class to enable assignment to a component of the fractional vector of a Coordinate.
Definition: Coordinate.hh:382
FracCoordinateComponent & operator=(const FracCoordinateComponent &RHS)
Definition: Coordinate.hh:393
FracCoordinateComponent & operator-=(double val)
Definition: Coordinate.hh:403
FracCoordinateComponent & operator*=(double val)
Definition: Coordinate.hh:408
FracCoordinateComponent(Coordinate &coord, Coordinate::size_type index)
Definition: Coordinate.hh:384
FracCoordinateComponent & operator+=(double val)
Definition: Coordinate.hh:398
FracCoordinateComponent & operator=(double val)
Definition: Coordinate.hh:388
FracCoordinateComponent & operator/=(double val)
Definition: Coordinate.hh:413
A class to enable vector assignment to the fractional vector of a Coordinate.
Definition: Coordinate.hh:313
FracCoordinate & operator*=(double val)
Definition: Coordinate.hh:342
FracCoordinate & operator/=(double val)
Definition: Coordinate.hh:348
FracCoordinate & operator=(const FracCoordinate &RHS)
Definition: Coordinate.hh:323
FracCoordinate & operator-=(const Eigen::Ref< const Coordinate::vector_type > &vec)
Definition: Coordinate.hh:335
FracCoordinate & operator+=(const Eigen::Ref< const Coordinate::vector_type > &vec)
Definition: Coordinate.hh:328
FracCoordinate & operator=(const Eigen::Ref< const Coordinate::vector_type > &vec)
Definition: Coordinate.hh:317
Represents cartesian and fractional coordinates.
Definition: Coordinate.hh:34
bool almost_equal(const Coordinate &RHS) const
Definition: Coordinate.cc:66
bool is_within() const
Checks to see if coordinate is in the unit cell, but does not translate it.
Definition: Coordinate.cc:244
const double & const_frac(size_type index) const
user override to force const Access the fractional coordinate vector
Definition: Coordinate.hh:78
const Lattice & home() const
Access the home lattice of the coordinate.
Definition: Coordinate.hh:200
void print(std::ostream &stream, COORD_TYPE mode, char term=0, Eigen::IOFormat format=Eigen::IOFormat(7, 12)) const
Definition: Coordinate.cc:121
double min_dist2(const Coordinate &neighbor, const Eigen::Ref< const Eigen::Matrix3d > &metric) const
Definition: Coordinate.cc:178
Coordinate & operator+=(const Coordinate &RHS)
Positive translation of this coordinate by RHS.cart()
Definition: Coordinate.cc:37
Coordinate_impl::CartCoordinate cart()
Set Cartesian coordinate vector and update fractional coordinate vector.
Definition: Coordinate.hh:562
void set_lattice(const Lattice &new_lat, COORD_TYPE mode)
Change the home lattice of the coordinate, selecting one representation (either CART or FRAC) that re...
Definition: Coordinate.cc:190
Lattice const * m_home
Definition: Coordinate.hh:279
bool is_lattice_shift(double tol=TOL) const
Definition: Coordinate.cc:311
int voronoi_number() const
Definition: Coordinate.cc:259
const Lattice & lattice() const
Access the home lattice of the coordinate.
Definition: Coordinate.hh:206
void print_axis(std::ostream &stream, COORD_TYPE mode, char term=0, Eigen::IOFormat format=Eigen::IOFormat(7, 12)) const
Print normalized vector.
Definition: Coordinate.cc:135
Eigen::Vector3d vector_type
Definition: Coordinate.hh:36
const vector_type & const_frac() const
user override to force const Access the fractional coordinate vector
Definition: Coordinate.hh:68
void _set_cart(size_type ind, double val)
Definition: Coordinate.hh:269
void _set_frac(size_type ind, double val)
Definition: Coordinate.hh:259
bool compare(const Coordinate &RHS) const
Returns true if this->min_dist(RHS)<this->lattice().tol()
Definition: Coordinate.cc:72
Coordinate(const Lattice &_home)
Minimal constructor only takes a lattice.
Definition: Coordinate.hh:47
const double & cart(size_type index) const
const Access a component of the Cartesian coordinate vector
Definition: Coordinate.hh:96
vector_type m_frac_coord
Definition: Coordinate.hh:281
Coordinate min_translation(const Coordinate &neighbor) const
Returns translation coordinate (in Angstr) to nearest periodic image of neighbor.
Definition: Coordinate.cc:156
double robust_min_dist(const Coordinate &neighbor) const
Returns distance (in Angstr) to nearest periodic image of neighbor.
Definition: Coordinate.cc:168
vector_type m_cart_coord
Definition: Coordinate.hh:281
bool compare_type(const Coordinate &RHS) const
Return true – Exists to allow duck-typing with Site.
Definition: Coordinate.cc:84
const vector_type & cart() const
const Access the Cartesian coordinate vector
Definition: Coordinate.hh:87
bool voronoi_within()
Map coordinate into the voronoi cell using a lattice translation.
Definition: Coordinate.cc:274
const double & const_cart(size_type index) const
const Access a component of the Cartesian coordinate vector
Definition: Coordinate.hh:99
bool operator==(const Coordinate &RHS) const
Definition: Coordinate.cc:60
vector_type::Index size_type
Definition: Coordinate.hh:37
Coordinate & operator-=(const Coordinate &RHS)
Negative translation of this coordinate by RHS.cart()
Definition: Coordinate.cc:44
vector_type as_vec(COORD_TYPE _mode) const
Definition: Coordinate.hh:103
void _set_cart(const Eigen::Ref< const vector_type > &c)
Definition: Coordinate.hh:264
bool operator!=(const Coordinate &RHS) const
Definition: Coordinate.hh:125
void read(std::istream &stream, COORD_TYPE mode)
Definition: Coordinate.cc:88
const double & frac(size_type index) const
const Access a component of the fractional coordinate vector
Definition: Coordinate.hh:74
double min_dist(const Coordinate &neighbor) const
Returns distance (in Angstr) to nearest periodic image of neighbor.
Definition: Coordinate.cc:152
Coordinate(Eigen::Ref< const vector_type > const &_vec, const Lattice &_home, COORD_TYPE _mode)
void _set_frac(const Eigen::Ref< const vector_type > &f)
Definition: Coordinate.hh:254
Coordinate_impl::FracCoordinate frac()
Set the fractional coordinate vector.
Definition: Coordinate.hh:550
const vector_type & frac() const
const Access the fractional coordinate vector
Definition: Coordinate.hh:64
const vector_type & const_cart() const
user override to force const Access the Cartesian coordinate vector
Definition: Coordinate.hh:90
Coordinate operator-() const
unary minus of this coordinate
Definition: Coordinate.cc:54
double dist(const Coordinate &neighbor) const
distance (in Angstr) of neighbor from *this
Definition: Coordinate.cc:148
const Eigen::Matrix3d & lat_column_mat() const
3x3 matrix with lattice vectors as its columne
Definition: Lattice.hh:110
const Eigen::Matrix3d & inv_lat_column_mat() const
Inverse of Lattice::lat_column_mat() It is the transformation matrix 'C2F', such that f = C2F * c whe...
Definition: Lattice.hh:117
Coordinate operator*(const SymOp &LHS, const Coordinate &RHS)
Definition: Coordinate.cc:329
Coordinate operator-(const Coordinate &LHS, const Coordinate &RHS)
Definition: Coordinate.hh:294
Coordinate operator+(const Coordinate &LHS, const Coordinate &RHS)
Definition: Coordinate.hh:290
static Coordinate origin(const Lattice &_home)
construct a coordinate describing origin of _home lattice
Definition: Coordinate.hh:284
xtal::Coordinate copy_apply(const xtal::SymOp &op, xtal::Coordinate coord)
Copy and apply SymOp to a Coordinate.
Definition: Coordinate.cc:354
xtal::Coordinate & apply(const xtal::SymOp &op, xtal::Coordinate &coord)
apply SymOp to a Coordinate
Definition: Coordinate.cc:347
Main CASM namespace.
Definition: APICommand.hh:8
COORD_TYPE
Definition: enum.hh:6
const COORD_TYPE FRAC
Definition: enum.hh:8
const double TOL
Definition: definitions.hh:30
const COORD_TYPE CART
Definition: enum.hh:9
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
Definition: stream_io.hh:24