CASM  1.1.0
A Clusters Approach to Statistical Mechanics
Molecule.cc
Go to the documentation of this file.
2 
3 #include <type_traits>
4 #include <vector>
5 
8 
9 namespace CASM {
10 namespace xtal {
11 
12 bool AtomPosition::identical(AtomPosition const &RHS, double _tol) const {
13  return almost_equal(cart(), RHS.cart(), _tol) &&
14  compare_type(*this, RHS, _tol);
15 }
16 
17 //****************************************************
18 
19 bool compare_type(AtomPosition const &A, AtomPosition const &B, double tol) {
20  // compare number of attributes
21  if (A.attributes().size() != B.attributes().size()) return false;
22  if (A.name() != B.name()) return false;
23  // compare attributes
24  auto it_A(A.attributes().cbegin()), end_it(A.attributes().cend());
25  for (; it_A != end_it; ++it_A) {
26  auto it_B = B.attributes().find(it_A->first);
27  if (it_B == B.attributes().cend() ||
28  !(it_A->second).identical(it_B->second, tol))
29  return false;
30  }
31  return true;
32 }
33 
34 //****************************************************
35 
36 bool Molecule::is_atomic() const {
37  if (size() != 1) return false;
38  if (!attributes().empty()) return false;
39  for (AtomPosition const &atom : atoms()) {
40  if (atom.cart().norm() > TOL) return false;
41  if (!atom.attributes().empty()) return false;
42  }
43  return true;
44 }
45 //****************************************************
46 
47 bool Molecule::is_vacancy() const {
48  // return m_atoms.empty();
50 }
51 
52 bool Molecule::identical(Molecule const &RHS, double _tol) const {
53  // compare number of attributes
54  if (m_attribute_map.size() != RHS.m_attribute_map.size()) return false;
55 
56  // compare number of atoms
57  if (size() != RHS.size()) return false;
58 
59  // compare atoms, irrespective of order
60  for (Index i = 0; i < RHS.size(); i++) {
61  Index j = 0;
62  for (j = 0; j < size(); j++) {
63  if (atom(i).identical(RHS.atom(j), _tol)) break;
64  }
65  if (j == size()) return false;
66  }
67 
68  // compare attributes
69  auto it(m_attribute_map.cbegin()), end_it(m_attribute_map.cend());
70  for (; it != end_it; ++it) {
71  auto it_RHS = RHS.m_attribute_map.find(it->first);
72  if (it_RHS == RHS.m_attribute_map.cend() ||
73  !(it->second).identical(it_RHS->second, _tol))
74  return false;
75  }
76 
77  return true;
78 }
79 
80 bool Molecule::contains(std::string const &_name) const {
81  for (Index i = 0; i < size(); i++)
82  if (atom(i).name() == _name) return true;
83  return false;
84 }
85 
86 Molecule Molecule::make_atom(std::string const &atom_name) {
87  return Molecule(atom_name, {AtomPosition(0., 0., 0., atom_name)});
88 }
89 
91  // return Molecule("Va", {});
92  return make_atom("Va");
93 }
94 
95 } // namespace xtal
96 } // namespace CASM
97 
98 namespace CASM {
99 namespace sym {
101  xtal::AtomPosition &mutating_atom_pos) {
102  xtal::AtomPosition transformed_atom_pos = copy_apply(op, mutating_atom_pos);
103  std::swap(mutating_atom_pos, transformed_atom_pos);
104  return mutating_atom_pos;
105 }
106 
108  xtal::AtomPosition atom_pos) {
109  Eigen::Vector3d transformed_position = get_matrix(op) * atom_pos.cart();
110  xtal::AtomPosition transformed_atom_pos(transformed_position,
111  atom_pos.name());
112  std::map<std::string, xtal::SpeciesAttribute> transformed_attribute_map;
113  for (const auto &name_attr_pr : atom_pos.attributes()) {
114  transformed_attribute_map.emplace(name_attr_pr.first,
115  sym::copy_apply(op, name_attr_pr.second));
116  }
117  transformed_atom_pos.set_attributes(transformed_attribute_map);
118 
119  return transformed_atom_pos;
120 }
121 
122 xtal::Molecule &apply(const xtal::SymOp &op, xtal::Molecule &mutating_mol) {
123  std::vector<xtal::AtomPosition> transformed_atoms;
124  for (const xtal::AtomPosition &atom_pos : mutating_mol.atoms()) {
125  transformed_atoms.emplace_back(copy_apply(op, atom_pos));
126  }
127  mutating_mol.set_atoms(transformed_atoms);
128 
129  std::map<std::string, xtal::SpeciesAttribute> transformed_attribute_map;
130  for (const auto &name_attr_pr : mutating_mol.attributes()) {
131  transformed_attribute_map.emplace(name_attr_pr.first,
132  copy_apply(op, name_attr_pr.second));
133  }
134  mutating_mol.set_attributes(transformed_attribute_map);
135 
136  return mutating_mol;
137 }
138 
140  xtal::Molecule transformed_mol = apply(op, mol);
141  return transformed_mol;
142 }
143 } // namespace sym
144 } // namespace CASM
An atomic species associated with a position in space.
Definition: Molecule.hh:27
Class representing a Molecule.
Definition: Molecule.hh:93
void set_attributes(std::map< std::string, SpeciesAttribute > _attr)
Set all constitutent attributes of Molecule overwrites any existing attributes.
Definition: Molecule.hh:151
bool identical(Molecule const &RHS, double _tol) const
Check equality of two molecules, within specified tolerance. Compares atoms, irrespective of order,...
Definition: Molecule.cc:52
std::vector< AtomPosition > const & atoms() const
Const access of all contained AtomPositions.
Definition: Molecule.hh:120
bool compare_type(AtomPosition const &A, AtomPosition const &B, double tol)
Definition: Molecule.cc:19
static Molecule make_atom(std::string const &atom_name)
Return an atomic Molecule with specified name.
Definition: Molecule.cc:86
std::vector< AtomPosition > m_atoms
Definition: Molecule.hh:175
std::map< std::string, SpeciesAttribute > m_attribute_map
Definition: Molecule.hh:178
Molecule(std::string const &_name, std::vector< AtomPosition > _atoms={}, bool _divisible=false)
Construct with designated name, a list of atoms, and whether molecule is chemically divisible.
Definition: Molecule.hh:106
Eigen::Vector3d const & cart() const
Const access of Cartesian position of atom.
Definition: Molecule.hh:43
bool is_vacancy(const std::string &name)
A vacancy is any Specie/Molecule with (name == "VA" || name == "va" || name == "Va")
Definition: Molecule.hh:187
bool is_vacancy() const
True if Molecule represents vacancy.
Definition: Molecule.cc:47
void set_atoms(std::vector< AtomPosition > _atoms)
set all constituent atoms of Molecule overwrites any existing atoms
Definition: Molecule.hh:157
void set_attributes(std::map< std::string, SpeciesAttribute > _attr)
Definition: Molecule.hh:56
bool is_atomic() const
True if Molecule is atom with no other attributes.
Definition: Molecule.cc:36
AtomPosition const & atom(Index i) const
returns i'th atom position
Definition: Molecule.hh:123
std::string const & name() const
Designated name of Molecule (may be unrelated to constituent species)
Definition: Molecule.hh:117
static Molecule make_vacancy()
Return a vacancy Molecule.
Definition: Molecule.cc:90
bool identical(AtomPosition const &RHS, double _tol) const
Comparison with tolerance (max allowed distance between LHS and RHS, in Angstr.)
Definition: Molecule.cc:12
Index size() const
Number of atoms contained Molecule.
Definition: Molecule.hh:113
bool contains(std::string const &atom_name) const
Returns true of molecule contains atom of specified name.
Definition: Molecule.cc:80
std::string const & name() const
Const access of species name.
Definition: Molecule.hh:40
std::map< std::string, SpeciesAttribute > const & attributes() const
Definition: Molecule.hh:52
std::map< std::string, SpeciesAttribute > const & attributes() const
Returns dictionary of all constituent attributes of the Molecule Does not include attributes associat...
Definition: Molecule.hh:145
const SymOp::matrix_type & get_matrix(const SymOp &op)
Definition: SymOp.cc:167
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
bool almost_equal(ClusterInvariants const &A, ClusterInvariants const &B, double tol)
Check if ClusterInvariants are equal.
void swap(ConfigDoF &A, ConfigDoF &B)
Definition: ConfigDoF.cc:260
const double TOL
Definition: definitions.hh:30
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39