Loading...
Searching...
No Matches
ElementPairContactFloor.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3#pragma once
4
5#include <Eigen/Core>
6#include <algorithm>
7#include <array>
8#include <cmath>
9#include <cstddef>
10#include <limits>
11#include <utility>
12#include <vector>
13
15#include "PairDistanceDomain.h"
16
17namespace gpr::geometry {
18
41public:
43 ElementPairContactFloor(double scalar_floor,
44 std::vector<int> atomic_numbers)
45 : scalar_(scalar_floor > 0.0 ? scalar_floor : 0.0),
46 atomic_numbers_(std::move(atomic_numbers))
47 {
48 for (std::size_t i = 0; i < atomic_numbers_.size(); ++i)
49 for (std::size_t j = i + 1; j < atomic_numbers_.size(); ++j)
50 cutoff_ = std::max(cutoff_, floor(i, j));
51 cutoff_ = std::max(cutoff_, scalar_);
52 }
53
54 [[nodiscard]] bool active() const noexcept
55 {
56 return cutoff_ > 0.0;
57 }
58
60 [[nodiscard]] double scalar() const noexcept
61 {
62 return scalar_;
63 }
64
66 [[nodiscard]] double cutoff() const noexcept
67 {
68 return cutoff_;
69 }
70
71 [[nodiscard]] std::size_t atomCount() const noexcept
72 {
73 return atomic_numbers_.size();
74 }
75
77 [[nodiscard]] double floor(std::size_t i, std::size_t j) const noexcept
78 {
79 double bound = scalar_;
80 if (i < atomic_numbers_.size() && j < atomic_numbers_.size())
81 bound = std::max(
82 bound, elementFloor(atomic_numbers_[i], atomic_numbers_[j]));
83 return bound;
84 }
85
89 [[nodiscard]] double elementFloor(int Zi, int Zj) const noexcept
90 {
91 const std::pair<int, int> key{std::min(Zi, Zj), std::max(Zi, Zj)};
92 for (const auto& entry: calibrated_)
93 if (entry.first == key) return entry.second;
95 }
96
103 void calibrateToGeometries(const std::vector<Eigen::VectorXd>& references,
104 const std::array<double, 9>& cell,
105 Eigen::Index moving_count = -1)
106 {
107 if (atomic_numbers_.empty()) return;
108 std::vector<std::pair<std::pair<int, int>, double>> shortest;
109 for (const Eigen::VectorXd& positions: references) {
110 if (positions.size() % 3 != 0 || !positions.allFinite() ||
111 positions.size() / 3 !=
112 static_cast<Eigen::Index>(atomic_numbers_.size()))
113 continue;
114 const Eigen::Index count = positions.size() / 3;
115 const Eigen::Index moving =
116 moving_count < 0 ? count : std::min(moving_count, count);
117 const PairBox box(cell);
118 for (const auto& pair:
119 cutoffNeighbors(positions.data(), count, cell,
120 box.periodicAxes(), 1.125 * cutoff_)) {
121 if (pair.first == pair.second ||
122 std::min(pair.first, pair.second) >=
123 static_cast<std::size_t>(moving))
124 continue;
125 const int zi = atomic_numbers_[pair.first];
126 const int zj = atomic_numbers_[pair.second];
127 const double onset =
129 if (!(onset > 0.0)) continue;
130 const double r =
131 std::sqrt(box.dist2(positions.segment<3>(3 * pair.first),
132 positions.segment<3>(3 * pair.second)));
133 if (!(r > 0.0) || r >= onset) continue;
134 const std::pair<int, int> key{std::min(zi, zj),
135 std::max(zi, zj)};
136 auto it = std::find_if(
137 shortest.begin(), shortest.end(),
138 [&](const auto& entry) { return entry.first == key; });
139 if (it == shortest.end())
140 shortest.emplace_back(key, r);
141 else
142 it->second = std::min(it->second, r);
143 }
144 }
145 for (const auto& shortest_entry: shortest) {
146 const std::pair<int, int>& key = shortest_entry.first;
147 const double contact = shortest_entry.second;
148 const double covalent_sum =
151 if (!(covalent_sum > 0.0)) continue;
153 key.first, key.second);
154 const double calibrated = onset * contact / covalent_sum;
155 auto it = std::find_if(
156 calibrated_.begin(), calibrated_.end(),
157 [&](const auto& entry) { return entry.first == key; });
158 if (it == calibrated_.end())
159 calibrated_.emplace_back(key, calibrated);
160 else
161 it->second = std::min(it->second, calibrated);
162 }
163 cutoff_ = scalar_;
164 for (std::size_t i = 0; i < atomic_numbers_.size(); ++i)
165 for (std::size_t j = i + 1; j < atomic_numbers_.size(); ++j)
166 cutoff_ = std::max(cutoff_, floor(i, j));
167 }
168
174 [[nodiscard]] double minimumRatio(const Eigen::VectorXd& positions,
175 const std::array<double, 9>& cell,
176 Eigen::Index moving_count = -1) const
177 {
178 if (!active()) return 1.0;
179 if (positions.size() % 3 != 0 || !positions.allFinite()) return 0.0;
180 const Eigen::Index count = positions.size() / 3;
181 if (moving_count < 0) moving_count = count;
182 const PairBox box(cell);
183 const std::array<bool, 3> periodic = box.periodicAxes();
184 double minimum = 1.0;
185 // Candidates come from the neighbour list's own distance; a pair
186 // sitting within rounding of its floor must still be ranked by
187 // the minimum-image distance below, so the list reaches past the
188 // largest floor. A pair beyond its floor leaves the minimum at one.
189 const double candidate_cutoff = 1.125 * cutoff_;
190 for (const auto& pair: cutoffNeighbors(positions.data(), count, cell,
191 periodic, candidate_cutoff)) {
192 if (pair.first == pair.second ||
193 std::min(pair.first, pair.second) >=
194 static_cast<std::size_t>(moving_count))
195 continue;
196 const double bound = floor(pair.first, pair.second);
197 if (!(bound > 0.0)) continue;
198 const double r =
199 std::sqrt(box.dist2(positions.segment<3>(3 * pair.first),
200 positions.segment<3>(3 * pair.second)));
201 minimum = std::min(minimum, r / bound);
202 }
203 return minimum;
204 }
205
207 [[nodiscard]] bool violated(const Eigen::VectorXd& positions,
208 const std::array<double, 9>& cell,
209 Eigen::Index moving_count = -1) const
210 {
211 return active() && minimumRatio(positions, cell, moving_count) < 1.0;
212 }
213
217 [[nodiscard]] Eigen::VectorXd limitStep(
218 const Eigen::VectorXd& positions, const Eigen::VectorXd& proposed,
219 const std::array<double, 9>& cell, Eigen::Index moving_count = -1) const
220 {
221 if (!active()) return proposed;
222 if (positions.size() != proposed.size() || positions.size() % 3 != 0 ||
223 !positions.allFinite() || !proposed.allFinite())
224 return positions;
225
226 const Eigen::Index count = positions.size() / 3;
227 if (moving_count < 0) moving_count = count;
228 moving_count = std::min(moving_count, count);
229 const double start_ratio = minimumRatio(positions, cell, moving_count);
230 const Eigen::VectorXd displacement = proposed - positions;
231 double maximum_atom_step = 0.0;
232 for (Eigen::Index atom = 0; atom < count; ++atom)
233 maximum_atom_step = std::max(
234 maximum_atom_step, displacement.segment<3>(3 * atom).norm());
235
236 Eigen::VectorXd path_endpoint = proposed;
237 const double candidate_cutoff = cutoff_ + 2.0 * maximum_atom_step;
238 if (std::isfinite(candidate_cutoff) && candidate_cutoff > 0.0) {
239 const std::array<bool, 3> periodic = PairBox(cell).periodicAxes();
240 double first_contact = 1.0;
241 for (const auto& pair: cutoffNeighbors(
242 positions.data(), static_cast<std::size_t>(count), cell,
243 periodic, candidate_cutoff)) {
244 if (pair.first == pair.second ||
245 std::min(pair.first, pair.second) >=
246 static_cast<std::size_t>(moving_count))
247 continue;
248 const double pair_floor = floor(pair.first, pair.second);
249 if (!(pair_floor > 0.0)) continue;
250
251 const Eigen::Index first =
252 static_cast<Eigen::Index>(pair.first);
253 const Eigen::Index second =
254 static_cast<Eigen::Index>(pair.second);
255 const Eigen::Vector3d relative =
256 positions.segment<3>(3 * second) -
257 positions.segment<3>(3 * first) +
258 pair.shift[0] * Eigen::Vector3d(cell[0], cell[1], cell[2]) +
259 pair.shift[1] * Eigen::Vector3d(cell[3], cell[4], cell[5]) +
260 pair.shift[2] * Eigen::Vector3d(cell[6], cell[7], cell[8]);
261 const double relative_distance = relative.norm();
262 const double bound =
263 std::min(pair_floor * start_ratio, relative_distance);
264 if (!(bound > 0.0)) continue;
265 const Eigen::Vector3d relative_step =
266 displacement.segment<3>(3 * second) -
267 displacement.segment<3>(3 * first);
268 const double quadratic = relative_step.squaredNorm();
269 if (!(quadratic > 0.0) || !std::isfinite(quadratic)) continue;
270 const double linear = 2.0 * relative.dot(relative_step);
271 const double constant =
272 std::max(0.0, relative.squaredNorm() - bound * bound);
273 const double discriminant =
274 linear * linear - 4.0 * quadratic * constant;
275 if (!(discriminant >= 0.0) || !std::isfinite(discriminant))
276 continue;
277 double entry = std::numeric_limits<double>::infinity();
278 if (constant == 0.0) {
279 if (linear < 0.0) entry = 0.0;
280 } else {
281 const double denominator =
282 -linear + std::sqrt(discriminant);
283 if (denominator > 0.0) entry = 2.0 * constant / denominator;
284 }
285 if (entry >= 0.0 && entry < first_contact)
286 first_contact = std::nextafter(entry, 0.0);
287 }
288 if (first_contact < 1.0)
289 path_endpoint = positions + first_contact * displacement;
290 }
291
293 positions, path_endpoint, 1.0, [&](const Eigen::VectorXd& point) {
294 return minimumRatio(point, cell, moving_count);
295 });
296 }
297
298private:
299 double scalar_ = 0.0;
300 double cutoff_ = 0.0;
301 std::vector<int> atomic_numbers_;
302 std::vector<std::pair<std::pair<int, int>, double>> calibrated_;
303};
304
305} // namespace gpr::geometry
double elementFloor(int Zi, int Zj) const noexcept
double scalar() const noexcept
The legacy scalar bound (0 when disabled).
double floor(std::size_t i, std::size_t j) const noexcept
Floor of the pair (i, j); the scalar bound when a Z is unknown.
ElementPairContactFloor(double scalar_floor, std::vector< int > atomic_numbers)
double minimumRatio(const Eigen::VectorXd &positions, const std::array< double, 9 > &cell, Eigen::Index moving_count=-1) const
void calibrateToGeometries(const std::vector< Eigen::VectorXd > &references, const std::array< double, 9 > &cell, Eigen::Index moving_count=-1)
bool violated(const Eigen::VectorXd &positions, const std::array< double, 9 > &cell, Eigen::Index moving_count=-1) const
True when some moving pair sits below its floor.
Eigen::VectorXd limitStep(const Eigen::VectorXd &positions, const Eigen::VectorXd &proposed, const std::array< double, 9 > &cell, Eigen::Index moving_count=-1) const
double cutoff() const noexcept
The largest pair floor: no pair beyond it can violate the domain.
const std::array< bool, 3 > & periodicAxes() const noexcept
The periodic axes, a, b, c.
Definition MinImage.h:204
double dist2(const Eigen::Vector3d &p, const Eigen::Vector3d &q) const
Definition MinImage.h:235
static double repulsionOnsetRadius(int Zi, int Zj) noexcept
static double covalentRadius(int Z) noexcept
Cordero single-bond covalent radius (Angstrom) of atomic number Z.
std::vector< CutoffPair > cutoffNeighbors(const double *positions, std::size_t count, const std::array< double, 9 > &cell, const std::array< bool, 3 > &periodic, double cutoff)
Eigen::VectorXd limitPairDistanceStep(const Eigen::VectorXd &positions, const Eigen::VectorXd &proposed, double floor, Distance &&distance)
STL namespace.