Debian Codegraph @

avogadro/core/unitcell.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_CORE_UNITCELL_H
7 #define AVOGADRO_CORE_UNITCELL_H
8
9 #include "avogadrocoreexport.h"
10
11 #include "avogadrocore.h"
12
13 #include "matrix.h"
14 #include "vector.h"
15
16 namespace Avogadro::Core {
17
18 /**
19 * @class UnitCell unitcell.h <avogadro/core/unitcell.h>
20 * @brief The UnitCell class provides a representation of a crystal's unit cell.
21 */
22 class AVOGADROCORE_EXPORT UnitCell
23 {
24 public:
25 UnitCell();
26 UnitCell(Real a, Real b, Real c, Real alpha, Real beta, Real gamma);
27 UnitCell(const Vector3& a, const Vector3& b, const Vector3& c);
28 explicit UnitCell(const Matrix3& cellMatrix);
29 UnitCell(const UnitCell& other);
30 ~UnitCell() = default;
31 UnitCell& operator=(UnitCell other);
32 friend void swap(UnitCell& lhs, UnitCell& rhs);
33
34 /** The lattice vector in the unit cell. Units: Angstrom @{ */
35 Vector3 aVector() const { return m_cellMatrix.col(0); }
36 Vector3 bVector() const { return m_cellMatrix.col(1); }
37 Vector3 cVector() const { return m_cellMatrix.col(2); }
38 void setAVector(const Vector3& v);
39 void setBVector(const Vector3& v);
40 void setCVector(const Vector3& v);
41 /** @} */
42
43 /** The length of the lattice vector in the unit cell. Units: Angstrom @{ */
44 Real a() const { return m_cellMatrix.col(0).norm(); }
45 Real b() const { return m_cellMatrix.col(1).norm(); }
46 Real c() const { return m_cellMatrix.col(2).norm(); }
47 /** @} */
48
49 /** The angle (radians) between the 'b' and 'c' lattice vectors. */
50 Real alpha() const;
51
52 /** The angle (radians) between the 'c' and 'a' lattice vectors. */
53 Real beta() const;
54
55 /** The angle (radians) between the 'a' and 'b' lattice vectors. */
56 Real gamma() const;
57
58 /**
59 * Set the cell parameters defining the unit cell. @a a, @a b, and @a c are
60 * in Angstrom, @a alpha, @a beta, and @a gamma are in radians.
61 */
62 void setCellParameters(Real a, Real b, Real c, Real alpha, Real beta,
63 Real gamma);
64
65 /**
66 * The volume of the unit cell in cubic Angstroms.
67 */
68 Real volume() const;
69
70 /**
71 * @return A vector pointing to the origin of the translational image that is
72 * @a i images in the a() direction, @a j images in the b() direction, and
73 * @a k images in the c() direction.
74 */
75 Vector3 imageOffset(int i, int j, int k) const;
76
77 /**
78 * The cell matrix with lattice vectors as columns. Units: Angstrom @{
79 */
80 const Matrix3& cellMatrix() const;
81 void setCellMatrix(const Matrix3& m);
82 /** @} */
83
84 /**
85 * The matrix used to convert cartesian to fractional coordinates.
86 */
87 const Matrix3& fractionalMatrix() const;
88 void setFractionalMatrix(const Matrix3& m);
89 /** @} */
90
91 /**
92 * Convert the cartesian coordinate @a cart to fractional (lattice) units. @{
93 */
94 Vector3 toFractional(const Vector3& cart) const;
95 void toFractional(const Vector3& cart, Vector3& frac) const;
96
97 /**
98 * Convert the fractional (lattice) coordinate @a frac to cartesian units. @{
99 */
100 Vector3 toCartesian(const Vector3& frac) const;
101 void toCartesian(const Vector3& frac, Vector3& cart) const;
102 /** @} */
103
104 /**
105 * Adjust the fractional (lattice) coordinate @a frac so that it lies within
106 * the unit cell. @{
107 */
108 Vector3 wrapFractional(const Vector3& frac) const;
109 void wrapFractional(const Vector3& frac, Vector3& wrapped) const;
110 /** @} */
111
112 /**
113 * Adjust the cartesian coordinate @a cart so that it lies within the unit
114 * cell. @{
115 */
116 Vector3 wrapCartesian(const Vector3& cart) const;
117 void wrapCartesian(const Vector3& cart, Vector3& wrapped) const;
118 /** @} */
119
120 /**
121 * Find the minimum fractional image of a fractional vector @a v.
122 * A minimum image has all fractional coordinates between -0.5 and 0.5.
123 */
124 static Vector3 minimumImageFractional(const Vector3& v);
125
126 /**
127 * Find the minimum image of a Cartesian vector @a v.
128 * A minimum image has all fractional coordinates between -0.5 and 0.5
129 */
130 Vector3 minimumImage(const Vector3& v) const;
131
132 /**
133 * Find the shortest distance between vectors @a v1 and @a v2.
134 */
135 Real distance(const Vector3& v1, const Vector3& v2) const;
136 Real distanceSquared(const Vector3& v1, const Vector3& v2) const;
137
138 /**
139 * @return if @a m is regular, i.e. its determinant is nonzero
140 */
141 static bool isRegular(const Matrix3& m);
142 bool isRegular() const { return isRegular(m_cellMatrix); }
143
144 private:
145 static Real signedAngleRadians(const Vector3& v1, const Vector3& v2,
146 const Vector3& axis);
147 void computeCellMatrix() { m_cellMatrix = m_fractionalMatrix.inverse(); }
148 void computeFractionalMatrix()
149 {
150 m_fractionalMatrix = m_cellMatrix.inverse();
151 }
152
153 Matrix3 m_cellMatrix;
154 Matrix3 m_fractionalMatrix;
155 };
156
157 inline UnitCell::UnitCell()
158 : m_cellMatrix(Matrix3::Identity()), m_fractionalMatrix(Matrix3::Identity())
159 {
160 }
161
162 inline UnitCell::UnitCell(Real a_, Real b_, Real c_, Real alpha_, Real beta_,
163 Real gamma_)
164 {
165 setCellParameters(a_, b_, c_, alpha_, beta_, gamma_);
166 }
167
168 inline UnitCell::UnitCell(const Vector3& a_, const Vector3& b_,
169 const Vector3& c_)
170 {
171 m_cellMatrix.col(0) = a_;
172 m_cellMatrix.col(1) = b_;
173 m_cellMatrix.col(2) = c_;
174 computeFractionalMatrix();
175 }
176
177 inline UnitCell::UnitCell(const Matrix3& cellMatrix_)
178 : m_cellMatrix(cellMatrix_)
179 {
180 computeFractionalMatrix();
181 }
182
183 inline UnitCell::UnitCell(const UnitCell& other)
184 : m_cellMatrix(other.m_cellMatrix),
185 m_fractionalMatrix(other.m_fractionalMatrix)
186 {
187 }
188
189 inline UnitCell& UnitCell::operator=(UnitCell other)
190 {
191 using std::swap;
192 swap(*this, other);
193 return *this;
194 }
195
196 inline void swap(UnitCell& lhs, UnitCell& rhs)
197 {
198 using std::swap;
199 swap(lhs.m_cellMatrix, rhs.m_cellMatrix);
200 swap(lhs.m_fractionalMatrix, rhs.m_fractionalMatrix);
201 }
202
203 inline void UnitCell::setAVector(const Vector3& v)
204 {
205 m_cellMatrix.col(0) = v;
206 computeFractionalMatrix();
207 }
208
209 inline void UnitCell::setBVector(const Vector3& v)
210 {
211 m_cellMatrix.col(1) = v;
212 computeFractionalMatrix();
213 }
214
215 inline void UnitCell::setCVector(const Vector3& v)
216 {
217 m_cellMatrix.col(2) = v;
218 computeFractionalMatrix();
219 }
220
221 inline Real UnitCell::alpha() const
222 {
223 return signedAngleRadians(bVector(), cVector(), aVector());
224 }
225
226 inline Real UnitCell::beta() const
227 {
228 return signedAngleRadians(cVector(), aVector(), bVector());
229 }
230
231 inline Real UnitCell::gamma() const
232 {
233 return signedAngleRadians(aVector(), bVector(), cVector());
234 }
235
236 inline Real UnitCell::volume() const
237 {
238 return std::fabs(aVector().cross(bVector()).dot(cVector()));
239 }
240
241 inline Vector3 UnitCell::imageOffset(int i, int j, int k) const
242 {
243 return (static_cast<Real>(i) * m_cellMatrix.col(0) +
244 static_cast<Real>(j) * m_cellMatrix.col(1) +
245 static_cast<Real>(k) * m_cellMatrix.col(2));
246 }
247
248 inline const Matrix3& UnitCell::cellMatrix() const
249 {
250 return m_cellMatrix;
251 }
252
253 inline void UnitCell::setCellMatrix(const Matrix3& m)
254 {
255 m_cellMatrix = m;
256 computeFractionalMatrix();
257 }
258
259 inline const Matrix3& UnitCell::fractionalMatrix() const
260 {
261 return m_fractionalMatrix;
262 }
263
264 inline void UnitCell::setFractionalMatrix(const Matrix3& m)
265 {
266 m_fractionalMatrix = m;
267 computeCellMatrix();
268 }
269
270 inline Vector3 UnitCell::toFractional(const Vector3& cart) const
271 {
272 return m_fractionalMatrix * cart;
273 }
274
275 inline void UnitCell::toFractional(const Vector3& cart, Vector3& frac) const
276 {
277 frac = m_fractionalMatrix * cart;
278 }
279
280 inline Vector3 UnitCell::toCartesian(const Vector3& f) const
281 {
282 return m_cellMatrix * f;
283 }
284
285 inline void UnitCell::toCartesian(const Vector3& frac, Vector3& cart) const
286 {
287 cart = m_cellMatrix * frac;
288 }
289
290 inline Vector3 UnitCell::wrapFractional(const Vector3& f) const
291 {
292 const Real one = static_cast<Real>(1.0);
293 Vector3 result(std::fmod(f[0], one), std::fmod(f[1], one),
294 std::fmod(f[2], one));
295 if (result[0] < static_cast<Real>(0.0))
296 ++result[0];
297 if (result[1] < static_cast<Real>(0.0))
298 ++result[1];
299 if (result[2] < static_cast<Real>(0.0))
300 ++result[2];
301 // set anything at 1.0 to 0.0
302 if (result[0] >= static_cast<Real>(0.999999))
303 result[0] = static_cast<Real>(0.0);
304 if (result[1] >= static_cast<Real>(0.999999))
305 result[1] = static_cast<Real>(0.0);
306 if (result[2] >= static_cast<Real>(0.999999))
307 result[2] = static_cast<Real>(0.0);
308
309 return result;
310 }
311
312 inline void UnitCell::wrapFractional(const Vector3& f, Vector3& wrapped) const
313 {
314 const Real one = static_cast<Real>(1.0);
315 wrapped =
316 Vector3(std::fmod(f[0], one), std::fmod(f[1], one), std::fmod(f[2], one));
317 if (wrapped[0] < static_cast<Real>(0.0))
318 ++wrapped[0];
319 if (wrapped[1] < static_cast<Real>(0.0))
320 ++wrapped[1];
321 if (wrapped[2] < static_cast<Real>(0.0))
322 ++wrapped[2];
323
324 // set anything at 1.0 to 0.0
325 if (wrapped[0] >= static_cast<Real>(0.999999))
326 wrapped[0] = static_cast<Real>(0.0);
327 if (wrapped[1] >= static_cast<Real>(0.999999))
328 wrapped[1] = static_cast<Real>(0.0);
329 if (wrapped[2] >= static_cast<Real>(0.999999))
330 wrapped[2] = static_cast<Real>(0.0);
331 }
332
333 inline Vector3 UnitCell::wrapCartesian(const Vector3& cart) const
334 {
335 Vector3 result = toFractional(cart);
336 wrapFractional(result, result);
337 toCartesian(result, result);
338 return result;
339 }
340
341 inline void UnitCell::wrapCartesian(const Vector3& cart, Vector3& wrapped) const
342 {
343 toFractional(cart, wrapped);
344 wrapFractional(wrapped, wrapped);
345 toCartesian(wrapped, wrapped);
346 }
347
348 inline Vector3 UnitCell::minimumImageFractional(const Vector3& v)
349 {
350 Real x = v[0] - rint(v[0]);
351 Real y = v[1] - rint(v[1]);
352 Real z = v[2] - rint(v[2]);
353 return Vector3(x, y, z);
354 }
355
356 inline Vector3 UnitCell::minimumImage(const Vector3& v) const
357 {
358 return toCartesian(minimumImageFractional(toFractional(v)));
359 }
360
361 inline Real UnitCell::distance(const Vector3& v1, const Vector3& v2) const
362 {
363 return minimumImage(v1 - v2).norm();
364 }
365
366 inline Real UnitCell::distanceSquared(const Vector3& v1,
367 const Vector3& v2) const
368 {
369 return minimumImage(v1 - v2).squaredNorm();
370 }
371
372 } // namespace Avogadro::Core
373
374 #endif // AVOGADRO_CORE_UNITCELL_H