Loading...
Searching...
No Matches
MinImage.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#ifndef GPR_AUXILIARY_MINIMAGE_H_
6#define GPR_AUXILIARY_MINIMAGE_H_
7
8#include <minimage.h>
9
10#include <Eigen/Dense>
11#include <algorithm>
12#include <array>
13#include <cmath>
14#include <limits>
15#include <stdexcept>
16#include <string>
17
18namespace gpr {
19namespace geometry {
20
22[[nodiscard]] inline bool cellIsZero(const double cell[9]) noexcept
23{
24 for (int i = 0; i < 9; ++i) {
25 if (cell[i] != 0.0) return false;
26 }
27 return true;
28}
29
30[[nodiscard]] inline bool cellIsZero(const std::array<double, 9>& cell) noexcept
31{
32 return cellIsZero(cell.data());
33}
34
39[[nodiscard]] inline bool cellIsVacuumBox(const double cell[9]) noexcept
40{
41 constexpr double L = 25.0;
42 constexpr double tol = 1.0e-12;
43 if (cell[0] != L || cell[4] != L || cell[8] != L) return false;
44 return std::abs(cell[1]) <= tol && std::abs(cell[2]) <= tol &&
45 std::abs(cell[3]) <= tol && std::abs(cell[5]) <= tol &&
46 std::abs(cell[6]) <= tol && std::abs(cell[7]) <= tol;
47}
48
49[[nodiscard]] inline bool cellIsVacuumBox(
50 const std::array<double, 9>& cell) noexcept
51{
52 return cellIsVacuumBox(cell.data());
53}
54
56[[nodiscard]] inline bool cellIsMicLattice(const double cell[9]) noexcept
57{
58 return !cellIsZero(cell) && !cellIsVacuumBox(cell);
59}
60
61[[nodiscard]] inline bool cellIsMicLattice(
62 const std::array<double, 9>& cell) noexcept
63{
64 return cellIsMicLattice(cell.data());
65}
66
68inline bool copyMicLattice(double dest[9], const double src[9]) noexcept
69{
70 if (!cellIsMicLattice(src)) return false;
71 for (int i = 0; i < 9; ++i)
72 dest[i] = src[i];
73 return true;
74}
75
76[[nodiscard]] inline std::array<double, 9> micLattice(
77 const double cell[9]) noexcept
78{
79 std::array<double, 9> out{};
80 copyMicLattice(out.data(), cell);
81 return out;
82}
83
84[[nodiscard]] inline std::array<double, 9> micLattice(
85 const std::array<double, 9>& cell) noexcept
86{
87 return micLattice(cell.data());
88}
89
93[[nodiscard]] inline std::array<bool, 3> cellPeriodicAxes(
94 const double cell[9]) noexcept
95{
96 std::array<bool, 3> periodic{};
97 for (int row = 0; row < 3; ++row)
98 periodic[row] = cell[3 * row] != 0.0 || cell[3 * row + 1] != 0.0 ||
99 cell[3 * row + 2] != 0.0;
100 return periodic;
101}
102
112[[nodiscard]] inline double uniqueImageRadius(const double cell[9]) noexcept
113{
114 if (!cellIsMicLattice(cell)) return 0.0;
115 mi_cell box{};
116 for (int row = 0; row < 3; ++row) {
117 const double* src = cell + 3 * row;
118 double* dst = row == 0 ? &box.ax : row == 1 ? &box.bx : &box.cx;
119 dst[0] = src[0];
120 dst[1] = src[1];
121 dst[2] = src[2];
122 }
123 double radius = 0.0;
124 if (minimage_unique_image_radius(&box, &radius) != 0) return 0.0;
125 return radius;
126}
127
128[[nodiscard]] inline double uniqueImageRadius(
129 const std::array<double, 9>& cell) noexcept
130{
131 return uniqueImageRadius(cell.data());
132}
133
136inline void zeroOpenAxes(double cell[9],
137 const std::array<bool, 3>& periodic) noexcept
138{
139 for (int row = 0; row < 3; ++row)
140 if (!periodic[row])
141 for (int k = 0; k < 3; ++k)
142 cell[3 * row + k] = 0.0;
143}
144
156class PairBox {
157public:
158 PairBox() = default;
159
160 explicit PairBox(const double cell[9])
161 {
162 reset(cell);
163 }
164
165 explicit PairBox(const std::array<double, 9>& cell)
166 {
167 reset(cell.data());
168 }
169
172 PairBox(const double cell[9], const std::array<bool, 3>& periodic)
173 {
174 double rows[9];
175 std::copy(cell, cell + 9, rows);
176 zeroOpenAxes(rows, periodic);
177 reset(rows);
178 }
179
180 void reset(const double cell[9])
181 {
182 axes_ = cellPeriodicAxes(cell);
183 periodic_ = axes_[0] || axes_[1] || axes_[2];
184 partial_ = periodic_ && !(axes_[0] && axes_[1] && axes_[2]);
185 if (!periodic_) return;
186 if (!partial_) {
187 if (minimage_cell_from_ase(cell, nullptr, &box_) != 0)
188 throwLastError("minimage: cell_from_ase failed");
189 return;
190 }
191 const int mask[3] = {axes_[0] ? 1 : 0, axes_[1] ? 1 : 0,
192 axes_[2] ? 1 : 0};
193 if (minimage_pbc_cell_from_ase(cell, mask, nullptr, &partial_box_) != 0)
194 throwLastError("minimage: pbc_cell_from_ase failed");
195 }
196
198 [[nodiscard]] bool periodic() const noexcept
199 {
200 return periodic_;
201 }
202
204 [[nodiscard]] const std::array<bool, 3>& periodicAxes() const noexcept
205 {
206 return axes_;
207 }
208
210 void displacement(const double p[3], const double q[3], double dr[3]) const
211 {
212 if (!periodic_) {
213 dr[0] = q[0] - p[0];
214 dr[1] = q[1] - p[1];
215 dr[2] = q[2] - p[2];
216 return;
217 }
218 if (!partial_) {
219 if (minimage_displacement_euclidean(&box_, p, q, dr) != 0)
220 throwLastError("minimage: displacement failed");
221 return;
222 }
223 if (minimage_displacement_euclidean_pbc(&partial_box_, p, q, dr) != 0)
224 throwLastError("minimage: displacement failed");
225 }
226
227 [[nodiscard]] Eigen::Vector3d displacement(const Eigen::Vector3d& p,
228 const Eigen::Vector3d& q) const
229 {
230 Eigen::Vector3d dr;
231 displacement(p.data(), q.data(), dr.data());
232 return dr;
233 }
234
235 [[nodiscard]] double dist2(const Eigen::Vector3d& p,
236 const Eigen::Vector3d& q) const
237 {
238 if (!periodic_) return (q - p).squaredNorm();
239 return displacement(p, q).squaredNorm();
240 }
241
242private:
243 [[noreturn]] static void throwLastError(const char* fallback)
244 {
245 const char* msg = minimage_last_error();
246 throw std::invalid_argument(msg ? std::string(msg)
247 : std::string(fallback));
248 }
249
250 bool periodic_ = false;
251 bool partial_ = false;
252 std::array<bool, 3> axes_{};
253 mi_cell box_{};
254 mi_pbc_cell partial_box_{};
255};
256
259[[nodiscard]] inline Eigen::VectorXd flatMinimumImageDisplacement(
260 const PairBox* box, const Eigen::VectorXd& from, const Eigen::VectorXd& to)
261{
262 if (from.size() != to.size())
263 throw std::invalid_argument("minimum-image displacement size mismatch");
264 if (box == nullptr || !box->periodic()) return to - from;
265 if (from.size() % 3 != 0)
266 throw std::invalid_argument(
267 "minimum-image displacement needs 3N coordinates");
268 Eigen::VectorXd delta(from.size());
269 const int n_atoms = static_cast<int>(from.size() / 3);
270 for (int atom = 0; atom < n_atoms; ++atom) {
271 const Eigen::Vector3d p = from.segment<3>(3 * atom);
272 const Eigen::Vector3d q = to.segment<3>(3 * atom);
273 delta.segment<3>(3 * atom) = box->displacement(p, q);
274 }
275 return delta;
276}
277
278} // namespace geometry
279} // namespace gpr
280
281#endif // GPR_AUXILIARY_MINIMAGE_H_
PairBox(const double cell[9], const std::array< bool, 3 > &periodic)
Definition MinImage.h:172
void reset(const double cell[9])
Definition MinImage.h:180
const std::array< bool, 3 > & periodicAxes() const noexcept
The periodic axes, a, b, c.
Definition MinImage.h:204
bool periodic() const noexcept
Some axis is periodic.
Definition MinImage.h:198
PairBox(const std::array< double, 9 > &cell)
Definition MinImage.h:165
PairBox(const double cell[9])
Definition MinImage.h:160
void displacement(const double p[3], const double q[3], double dr[3]) const
Displacement from p to q. Cartesian q - p when the cell is zero.
Definition MinImage.h:210
double dist2(const Eigen::Vector3d &p, const Eigen::Vector3d &q) const
Definition MinImage.h:235
Eigen::Vector3d displacement(const Eigen::Vector3d &p, const Eigen::Vector3d &q) const
Definition MinImage.h:227
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
bool cellIsVacuumBox(const double cell[9]) noexcept
Definition MinImage.h:39
bool cellIsZero(const double cell[9]) noexcept
Isolated molecule: every entry of the ASE-style 3x3 cell is zero.
Definition MinImage.h:22
void zeroOpenAxes(double cell[9], const std::array< bool, 3 > &periodic) noexcept
Definition MinImage.h:136
double uniqueImageRadius(const double cell[9]) noexcept
Definition MinImage.h:112
bool cellIsMicLattice(const double cell[9]) noexcept
Nonzero ASE 3x3 that is not the 25 A vacuum convention.
Definition MinImage.h:56
bool copyMicLattice(double dest[9], const double src[9]) noexcept
Copy src onto dest only when src is a MIC lattice.
Definition MinImage.h:68
std::array< double, 9 > micLattice(const double cell[9]) noexcept
Definition MinImage.h:76
std::array< bool, 3 > cellPeriodicAxes(const double cell[9]) noexcept
Definition MinImage.h:93
Eigen::VectorXd flatMinimumImageDisplacement(const PairBox *box, const Eigen::VectorXd &from, const Eigen::VectorXd &to)
Definition MinImage.h:259
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16