Keep the full Cartesian step inside the contact domain. Checking only the endpoint permits a long step to pass through a contact and emerge on the other side with a feasible pair distance.
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;
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) {
295 });
296 }
Eigen::VectorXd limitPairDistanceStep(const Eigen::VectorXd &positions, const Eigen::VectorXd &proposed, double floor, Distance &&distance)