Loading...
Searching...
No Matches
StencilIndex.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 GPRIDGEIZATION_STENCIL_INDEX_H_
6#define GPRIDGEIZATION_STENCIL_INDEX_H_
7
8#include <Eigen/Dense>
9#include <algorithm>
10#include <cmath>
11#include <iterator>
12#include <limits>
13#include <utility>
14#include <vector>
15
16namespace gpr::optim {
17
24 bool valid = false;
26 Eigen::VectorXd curvatures;
28 Eigen::MatrixXd modes;
29 double resolution = 0.0;
31 Eigen::MatrixXd hessian;
32};
33
36inline Eigen::MatrixXd massWeightedRigidMotions(
37 const Eigen::VectorXd& positions, const Eigen::VectorXd& masses)
38{
39 const Eigen::Index n = masses.size();
40 Eigen::Vector3d centre = Eigen::Vector3d::Zero();
41 for (Eigen::Index i = 0; i < n; ++i)
42 centre += masses(i) * positions.segment<3>(3 * i);
43 centre /= masses.sum();
44 Eigen::MatrixXd basis = Eigen::MatrixXd::Zero(3 * n, 6);
45 for (Eigen::Index i = 0; i < n; ++i) {
46 const double root = std::sqrt(masses(i));
47 const Eigen::Vector3d r = positions.segment<3>(3 * i) - centre;
48 for (int k = 0; k < 3; ++k) {
49 basis(3 * i + k, k) = root;
50 Eigen::Vector3d axis = Eigen::Vector3d::Zero();
51 axis(k) = 1.0;
52 basis.block<3, 1>(3 * i, 3 + k) = root * r.cross(axis);
53 }
54 }
55 return basis;
56}
57
62inline Eigen::MatrixXd vibrationalComplement(const Eigen::VectorXd& positions,
63 const Eigen::VectorXd& masses)
64{
65 const Eigen::MatrixXd rigid = massWeightedRigidMotions(positions, masses);
66 const Eigen::JacobiSVD<Eigen::MatrixXd> svd(rigid, Eigen::ComputeFullU);
67 return svd.matrixU().rightCols(positions.size() - svd.rank());
68}
69
81 const Eigen::MatrixXd& columns, const Eigen::VectorXd& positions,
82 const Eigen::VectorXd& masses, const Eigen::MatrixXd* measured = nullptr)
83{
85 const Eigen::Index dof = positions.size();
86 if (columns.rows() != dof || columns.cols() != dof ||
87 masses.size() * 3 != dof || !columns.allFinite())
88 return out;
89 Eigen::VectorXd inverse_root(dof);
90 for (Eigen::Index k = 0; k < dof; ++k)
91 inverse_root(k) = 1.0 / std::sqrt(masses(k / 3));
92 const Eigen::MatrixXd weight = inverse_root * inverse_root.transpose();
93 const Eigen::MatrixXd antisymmetric =
94 (0.5 * (columns - columns.transpose())).cwiseProduct(weight);
95 out.hessian = (0.5 * (columns + columns.transpose())).cwiseProduct(weight);
96 out.resolution = antisymmetric.size() > 0
97 ? Eigen::JacobiSVD<Eigen::MatrixXd>(antisymmetric)
98 .singularValues()(0)
99 : 0.0;
100 const bool reduced = measured != nullptr && measured->rows() == dof &&
101 measured->cols() > 0 && measured->cols() < dof &&
102 measured->allFinite();
103 Eigen::MatrixXd vibrational;
104 if (reduced) {
105 Eigen::VectorXd root(dof);
106 for (Eigen::Index k = 0; k < dof; ++k)
107 root(k) = std::sqrt(masses(k / 3));
108 const Eigen::HouseholderQR<Eigen::MatrixXd> qr(root.asDiagonal() *
109 *measured);
110 const Eigen::MatrixXd image =
111 qr.householderQ() *
112 Eigen::MatrixXd::Identity(dof, measured->cols());
113 const Eigen::MatrixXd block = image.transpose() * antisymmetric * image;
114 out.resolution =
115 Eigen::JacobiSVD<Eigen::MatrixXd>(block).singularValues()(0);
116 // A periodic stencil also measures the rotations, which are not
117 // symmetries there; the spectrum still leaves them out, as it does
118 // for a complete stencil. Directions that do not span the
119 // complement (frozen atoms, other masses) keep their own image.
120 vibrational = vibrationalComplement(positions, masses);
121 const double outside =
122 (vibrational - image * (image.transpose() * vibrational)).norm();
123 if (vibrational.cols() == 0 || !(outside <= 1e-8)) vibrational = image;
124 } else {
125 vibrational = vibrationalComplement(positions, masses);
126 }
127 const Eigen::MatrixXd projected =
128 vibrational.transpose() * out.hessian * vibrational;
129 // A molecule whose rigid motions already span every coordinate has an
130 // empty vibrational block. LAPACKE's DSYEV rejects that 0-by-0 problem
131 // (LDA = 0), and Eigen's solver reads off a 0x0 temporary. A non-finite
132 // projection is not a spectrum either. Leave the index invalid, which
133 // is what a failed solve reports, without calling the solver.
134 if (projected.rows() == 0 || projected.cols() == 0 ||
135 !projected.allFinite())
136 return out;
137 // Materialize before the solver. Passing the expression straight in
138 // makes Eigen's norm reduction read off the end of a temporary.
139 const Eigen::MatrixXd symmetric = 0.5 * (projected + projected.transpose());
140 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> spectrum(symmetric);
141 if (spectrum.info() != Eigen::Success) return out;
142 out.curvatures = spectrum.eigenvalues();
143 out.modes = vibrational * spectrum.eigenvectors();
144 const double scale = out.curvatures.cwiseAbs().maxCoeff();
145 out.resolution =
146 std::max(out.resolution, std::numeric_limits<double>::epsilon() *
147 static_cast<double>(std::max<Eigen::Index>(
148 1, out.curvatures.size())) *
149 scale);
150 out.valid = true;
151 return out;
152}
153
159 bool valid = false;
161 Eigen::VectorXd curvatures;
163 Eigen::MatrixXd modes;
165 double error = 0.0;
167 Eigen::MatrixXd action;
168 Eigen::MatrixXd truncation;
174 Eigen::MatrixXd directions;
176 Eigen::VectorXd inverse_mass;
177 Eigen::MatrixXd even;
178 double step = 0.0;
182 double noise_floor = 0.0;
183};
184
199template <typename Gradients>
200RefinedWeakModes refineWeakModes(const Eigen::VectorXd& positions,
201 const Eigen::VectorXd& masses,
202 const Eigen::MatrixXd& weak, double step,
203 const Gradients& gradients,
204 double noise_multiplier = 0.0)
205{
207 const Eigen::Index dof = positions.size();
208 const Eigen::Index k = weak.cols();
209 if (k == 0 || weak.rows() != dof) return out;
210 Eigen::VectorXd root(dof);
211 for (Eigen::Index i = 0; i < dof; ++i)
212 root(i) = std::sqrt(masses(i / 3));
213 Eigen::MatrixXd directions(dof, k);
214 Eigen::VectorXd norms(k);
215 for (Eigen::Index j = 0; j < k; ++j) {
216 const Eigen::VectorXd cartesian = weak.col(j).cwiseQuotient(root);
217 norms(j) = cartesian.norm();
218 if (!(norms(j) > 0.0)) return out;
219 directions.col(j) = cartesian / norms(j);
220 }
221 Eigen::MatrixXd points(dof, 4 * k);
222 const double offsets[4] = {step, -step, 0.5 * step, -0.5 * step};
223 for (Eigen::Index j = 0; j < k; ++j)
224 for (int o = 0; o < 4; ++o)
225 points.col(4 * j + o) = positions + offsets[o] * directions.col(j);
226 const Eigen::MatrixXd g = gradients(points);
227 if (g.rows() != 4 * k || g.cols() != dof || !g.allFinite()) return out;
228 Eigen::MatrixXd action(dof, k), truncation(dof, k), even(dof, k);
229 for (Eigen::Index j = 0; j < k; ++j) {
230 const Eigen::VectorXd coarse =
231 (g.row(4 * j) - g.row(4 * j + 1)).transpose() / (2.0 * step);
232 const Eigen::VectorXd fine =
233 (g.row(4 * j + 2) - g.row(4 * j + 3)).transpose() / step;
234 // Mass-weighted action of the unit mass-weighted mode.
235 action.col(j) =
236 ((4.0 * fine - coarse) / 3.0).cwiseQuotient(root) * norms(j);
237 truncation.col(j) =
238 ((coarse - fine) / 3.0).cwiseQuotient(root) * norms(j);
239 even.col(j) = 0.5 * (g.row(4 * j) + g.row(4 * j + 1)).transpose();
240 }
241 const Eigen::MatrixXd projected = weak.transpose() * action;
242 const Eigen::MatrixXd symmetric = 0.5 * (projected + projected.transpose());
243 const Eigen::MatrixXd projected_error = weak.transpose() * truncation;
244 if (noise_multiplier > 0.0 && std::isfinite(noise_multiplier)) {
245 // projected_error holds (coarse - fine) / 3 on the refined modes.
246 const double spread =
247 3.0 * std::sqrt(projected_error.diagonal().squaredNorm() /
248 static_cast<double>(k));
249 out.noise_floor = noise_multiplier * spread;
250 }
251 Eigen::MatrixXd envelope =
252 0.5 * (projected_error.cwiseAbs() +
253 projected_error.transpose().cwiseAbs()) +
254 (0.5 * (projected - projected.transpose())).cwiseAbs();
255 envelope.array() += out.noise_floor;
256 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> spectrum(symmetric);
257 if (spectrum.info() != Eigen::Success) return out;
258 out.curvatures = spectrum.eigenvalues();
259 out.modes = weak * spectrum.eigenvectors();
260 out.error = Eigen::JacobiSVD<Eigen::MatrixXd>(envelope).singularValues()(0);
261 out.action = action;
262 out.truncation = truncation;
263 out.directions = directions;
264 out.inverse_mass = norms.array().square();
265 out.even = even;
266 out.step = step;
267 out.valid = true;
268 return out;
269}
270
275 bool higher_order = false;
276 double second = std::numeric_limits<double>::quiet_NaN();
277 double error = std::numeric_limits<double>::quiet_NaN();
278};
279
280template <typename Gradients>
282 const Eigen::VectorXd& positions, const Eigen::VectorXd& masses,
283 const Eigen::MatrixXd& mass_weighted_modes, double step,
284 const Gradients& gradients, double noise_multiplier = 0.0)
285{
287 if (mass_weighted_modes.cols() < 2 || !(step > 0.0)) return out;
288 const Eigen::MatrixXd modes = mass_weighted_modes.leftCols(2);
289 const auto refined = refineWeakModes(positions, masses, modes, step,
290 gradients, noise_multiplier);
291 if (!refined.valid || refined.curvatures.size() < 2) return out;
292 out.second = refined.curvatures(1);
293 out.error = refined.error;
294 out.higher_order = std::isfinite(refined.curvatures(1)) &&
295 std::isfinite(refined.error) &&
296 refined.curvatures(1) < -std::max(refined.error, 0.0);
297 return out;
298}
299
303 bool first_order = false;
304 bool higher_order = false;
305 double negative_upper = std::numeric_limits<double>::infinity();
306 double positive_lower = -std::numeric_limits<double>::infinity();
307 double negative_pair_upper = std::numeric_limits<double>::infinity();
308 Eigen::Index refined_positive_modes = 0;
309 double weak_lower = -std::numeric_limits<double>::infinity();
310 double weak_error = std::numeric_limits<double>::infinity();
311 double weak_block_lower = -std::numeric_limits<double>::infinity();
312 double weak_block_error = std::numeric_limits<double>::infinity();
313 Eigen::MatrixXd hessian;
314 Eigen::VectorXd escape;
318 bool weak_higher_order = false;
324 Eigen::VectorXd weak_direction;
325 Eigen::VectorXd weak_even;
326 double weak_step = 0.0;
328 double weak_inverse_mass = 1.0;
333 Eigen::MatrixXd refinement_points;
334 Eigen::MatrixXd refinement_gradients;
335};
336
342 const Eigen::MatrixXd& block, const Eigen::MatrixXd& errors,
343 const Eigen::MatrixXd& coupling, const Eigen::VectorXd& tail,
344 double allowance)
345{
346 const double unresolved = -std::numeric_limits<double>::infinity();
347 const Eigen::Index weak = block.rows();
348 const Eigen::Index dimension = weak + tail.size();
349 if (weak < 2) return unresolved;
350 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> spectrum(block);
351 if (spectrum.info() != Eigen::Success) return unresolved;
352 const Eigen::MatrixXd rotation = spectrum.eigenvectors();
353 const Eigen::MatrixXd magnitude = rotation.cwiseAbs();
354 const Eigen::MatrixXd transformed = rotation.transpose() * block * rotation;
355 const Eigen::MatrixXd symmetric =
356 0.5 * (transformed + transformed.transpose());
357 const Eigen::MatrixXd envelope = magnitude.transpose() * errors * magnitude;
358 const Eigen::MatrixXd cross = coupling * magnitude;
359 Eigen::MatrixXd comparison = Eigen::MatrixXd::Zero(dimension, dimension);
360 comparison.topLeftCorner(weak, weak) = -symmetric.cwiseAbs() - envelope;
361 comparison.diagonal().head(weak) =
362 symmetric.diagonal() - envelope.diagonal();
363 comparison.bottomLeftCorner(tail.size(), weak) = -cross;
364 comparison.topRightCorner(weak, tail.size()) = -cross.transpose();
365 comparison.diagonal().tail(tail.size()) = tail;
366 const double roundoff =
367 64.0 * std::numeric_limits<double>::epsilon() * dimension;
368 const double gram_defect = (rotation.transpose() * rotation -
369 Eigen::MatrixXd::Identity(weak, weak))
370 .norm() +
371 roundoff * magnitude.squaredNorm();
372 if (!(gram_defect < 1.0) || !comparison.allFinite()) return unresolved;
373 const double scale =
374 (magnitude.transpose() * (block.cwiseAbs() + errors) * magnitude)
375 .norm() +
376 cross.norm() + comparison.norm();
377 // Congruence also transports the original roundoff perturbation.
378 const double rotated_allowance =
379 (1.0 + gram_defect) * allowance + roundoff * scale;
380 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> lower(
381 comparison, Eigen::EigenvaluesOnly);
382 if (lower.info() != Eigen::Success) return unresolved;
383 const double bound = lower.eigenvalues()(0) - rotated_allowance;
384 return bound / (bound >= 0.0 ? 1.0 + gram_defect : 1.0 - gram_defect);
385}
386
390template <typename Gradients>
392 const StencilIndex& index, const Eigen::VectorXd& positions,
393 const Eigen::VectorXd& masses, double step, const Gradients& gradients,
394 Eigen::Index minimum_weak = 0, double noise_multiplier = 0.0)
395{
397 const Eigen::Index dimension = index.curvatures.size();
398 if (!index.valid || dimension == 0) return out;
399 out.hessian = index.hessian;
400 out.negative_upper = index.curvatures(0) + index.resolution;
401 if (dimension > 1 && index.curvatures(1) < -index.resolution) {
402 out.higher_order = true;
403 out.negative_pair_upper = index.curvatures(1) + index.resolution;
404 out.escape = index.modes.col(1);
405 return out;
406 }
407 const bool refine_lowest =
408 std::abs(index.curvatures(0)) <= index.resolution;
409 Eigen::Index weak = 0;
410 while (weak + 1 < dimension &&
411 index.curvatures(weak + 1) <= index.resolution)
412 ++weak;
413 weak = std::max(weak, std::min(minimum_weak, dimension - 1));
414 out.refined_positive_modes = weak;
415 if (weak == 0) {
416 out.positive_lower = dimension > 1
417 ? index.curvatures(1) - index.resolution
418 : std::numeric_limits<double>::infinity();
419 if (!refine_lowest) {
420 out.first_order = out.negative_upper < 0.0;
421 return out;
422 }
423 }
424 if (index.curvatures(0) > index.resolution) return out;
425
426 const Eigen::Index begin = refine_lowest ? 0 : 1;
427 const Eigen::Index count = weak + (refine_lowest ? 1 : 0);
428 const Eigen::MatrixXd basis = index.modes.middleCols(begin, count);
429 const auto refined = refineWeakModes(positions, masses, basis, step,
430 gradients, noise_multiplier);
431 if (!refined.valid) return out;
432 // The pooled refinement spread rests on one difference per refined
433 // mode and can come out small by chance when few modes are refined.
434 // The antisymmetric part of the full stencil pools every entry: its
435 // spectral norm is about sqrt(n) times the noise s of one coarse
436 // curvature. A coarse-minus-fine difference has standard deviation
437 // sqrt(5) s, so the same multiplier on sqrt(5) s is the floor's lower
438 // limit.
439 double noise = refined.noise_floor;
440 if (noise_multiplier > 0.0 && std::isfinite(noise_multiplier))
441 noise = std::max(noise, noise_multiplier * std::sqrt(5.0) *
442 index.resolution /
443 std::sqrt(static_cast<double>(dimension)));
444
445 const Eigen::MatrixXd projected = index.modes.transpose() * refined.action;
446 Eigen::MatrixXd nominal = index.curvatures.asDiagonal();
447 nominal.middleCols(begin, count) = projected;
448 nominal.middleRows(begin, count) = projected.transpose();
449 const Eigen::MatrixXd refined_block = projected.middleRows(begin, count);
450 nominal.block(begin, begin, count, count) =
451 0.5 * (refined_block + refined_block.transpose());
452 Eigen::MatrixXd correction = nominal;
453 correction.diagonal() -= index.curvatures;
454 out.hessian += index.modes * correction * index.modes.transpose();
455
456 if (weak > 0) {
457 out.weak_direction = refined.directions.col(1 - begin);
458 out.weak_even = refined.even.col(1 - begin);
459 out.weak_step = refined.step;
460 out.weak_inverse_mass = refined.inverse_mass(1 - begin);
461 }
462 const auto positive_modes = index.modes.middleCols(1, weak);
463 const auto action = refined.action.rightCols(weak);
464 const auto truncation = refined.truncation.rightCols(weak);
465 const Eigen::MatrixXd block = positive_modes.transpose() * action;
466 const Eigen::MatrixXd symmetric = 0.5 * (block + block.transpose());
467 const Eigen::MatrixXd block_error = positive_modes.transpose() * truncation;
468 Eigen::MatrixXd errors =
469 0.5 * (block_error.cwiseAbs() + block_error.transpose().cwiseAbs()) +
470 (0.5 * (block - block.transpose())).cwiseAbs();
471 errors.array() += noise;
472 Eigen::MatrixXd comparison =
473 Eigen::MatrixXd::Zero(dimension - 1, dimension - 1);
474 comparison.topLeftCorner(weak, weak) = -symmetric.cwiseAbs() - errors;
475 comparison.diagonal().head(weak) = symmetric.diagonal() - errors.diagonal();
476 const Eigen::Index tail = dimension - weak - 1;
477 Eigen::MatrixXd coupling(tail, weak);
478 if (tail > 0) {
479 const auto tail_modes = index.modes.rightCols(tail);
480 coupling = (tail_modes.transpose() * action).cwiseAbs() +
481 (tail_modes.transpose() * truncation).cwiseAbs();
482 coupling.array() += noise;
483 comparison.bottomLeftCorner(tail, weak) = -coupling;
484 comparison.topRightCorner(weak, tail) = -coupling.transpose();
485 comparison.diagonal().tail(tail) =
486 index.curvatures.tail(tail).array() - index.resolution;
487 }
488 const double orthogonality =
489 (index.modes.transpose() * index.modes -
490 Eigen::MatrixXd::Identity(dimension, dimension))
491 .norm();
492 const double allowance =
493 (orthogonality +
494 64.0 * std::numeric_limits<double>::epsilon() * dimension) *
495 (comparison.norm() + refined.action.norm() + refined.truncation.norm() +
496 index.curvatures.norm());
497 if (dimension > 1) {
498 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> lower(
499 comparison, Eigen::EigenvaluesOnly);
500 if (lower.info() != Eigen::Success) return out;
501 out.positive_lower = lower.eigenvalues()(0) - allowance;
502 if (weak > 1 && out.positive_lower <= 0.0)
503 out.positive_lower =
504 std::max(out.positive_lower,
506 symmetric, errors, coupling,
507 comparison.diagonal().tail(tail), allowance));
508 }
509 if (refine_lowest)
510 out.negative_upper =
511 index.modes.col(0).dot(refined.action.col(0)) +
512 std::abs(index.modes.col(0).dot(refined.truncation.col(0))) +
513 noise + allowance;
514 out.first_order = out.negative_upper < 0.0 && out.positive_lower > 0.0;
515
516 if (weak > 0 && out.negative_upper < 0.0) {
517 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> spectrum(
518 symmetric);
519 if (spectrum.info() != Eigen::Success) return out;
520 const Eigen::VectorXd mode = spectrum.eigenvectors().col(0);
521 const Eigen::VectorXd magnitude = mode.cwiseAbs();
522 out.weak_error = magnitude.dot(errors * magnitude) + allowance;
523 out.weak_lower = spectrum.eigenvalues()(0) - out.weak_error;
524 out.weak_block_error = errors.norm() + allowance;
525 if (weak == dimension - 1) {
526 out.weak_block_lower = out.positive_lower;
527 } else {
528 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> lower(
529 comparison.topLeftCorner(weak, weak), Eigen::EigenvaluesOnly);
530 if (lower.info() != Eigen::Success) return out;
531 out.weak_block_lower = lower.eigenvalues()(0) - allowance;
532 if (weak > 1 && out.weak_block_lower <= 0.0)
533 out.weak_block_lower =
534 std::max(out.weak_block_lower,
536 symmetric, errors, Eigen::MatrixXd(0, weak),
537 Eigen::VectorXd(0), allowance));
538 }
539 // The entrywise envelope bounds this Rayleigh quotient without
540 // assigning errors from other directions to it. Coupling to the
541 // primary negative mode remains in the two-dimensional restriction.
542 const double upper = spectrum.eigenvalues()(0) +
543 magnitude.dot(errors * magnitude) + allowance;
544 Eigen::RowVectorXd cross_error =
545 (index.modes.col(0).transpose() * truncation).cwiseAbs();
546 cross_error.array() += noise;
547 const double coupling =
548 std::abs(index.modes.col(0).dot(action * mode)) +
549 (cross_error * magnitude)(0) + allowance;
550 Eigen::Matrix2d majorant;
551 majorant << out.negative_upper, coupling, coupling, upper;
552 const Eigen::SelfAdjointEigenSolver<Eigen::Matrix2d> pair(
553 majorant, Eigen::EigenvaluesOnly);
554 if (pair.info() == Eigen::Success) {
555 out.negative_pair_upper = pair.eigenvalues()(1) + allowance;
556 out.higher_order = out.negative_pair_upper < 0.0;
557 out.weak_higher_order = out.higher_order;
558 if (out.higher_order) out.escape = positive_modes * mode;
559 }
560 }
561 return out;
562}
563
569 int negative = 0;
570 int unresolved = 0;
571 bool first_order = false;
572 bool higher_order = false;
573 double resolution = std::numeric_limits<double>::quiet_NaN();
574};
575
577template <typename Gradients>
579 const Eigen::MatrixXd& columns, const Eigen::VectorXd& positions,
580 const Eigen::VectorXd& masses, double step, const Gradients& gradients,
581 const Eigen::MatrixXd* measured = nullptr)
582{
584 const StencilIndex index =
585 analyzeStencilIndex(columns, positions, masses, measured);
586 if (!index.valid) return out;
587 out.resolution = index.resolution;
588 for (Eigen::Index i = 0; i < index.curvatures.size(); ++i) {
589 const double value = index.curvatures(i);
590 if (value < -index.resolution)
591 ++out.negative;
592 else if (!(value > index.resolution))
593 ++out.unresolved;
594 }
595 const StencilIndexCertificate certified =
596 certifyStencilIndex(index, positions, masses, step, gradients);
597 out.first_order = certified.first_order;
598 out.higher_order = certified.higher_order;
599 if (certified.first_order) {
600 out.negative = 1;
601 out.unresolved = 0;
602 } else if (certified.higher_order) {
603 if (out.negative < 2) out.negative = 2;
604 // A weak mode the refinement proved negative is no longer
605 // unresolved. A second negative the coarse stencil already
606 // resolved leaves any other unresolved mode as it was.
607 if (certified.weak_higher_order) out.unresolved = 0;
608 }
609 return out;
610}
611
622 bool valid = false;
624 Eigen::VectorXd direction;
625 double length = 0.0;
627 double third = 0.0;
628};
629
631 const Eigen::VectorXd& direction, double third, double step, double lower,
632 double resolution, double step_bound)
633{
635 if (direction.size() == 0 || !direction.allFinite() ||
636 !(direction.norm() > 0.0) || !(step > 0.0) || !std::isfinite(step) ||
637 !std::isfinite(third))
638 return out;
639 const double deficit = std::isfinite(lower) ? std::max(-lower, 0.0) : 0.0;
640 const double lift = 2.0 * deficit + 0.1 * std::max(resolution, 0.0);
641 const double length = std::abs(third) > 0.0
642 ? lift / std::abs(third)
643 : std::numeric_limits<double>::infinity();
644 const double bound =
645 std::isfinite(step_bound) && step_bound > 0.0 ? step_bound : 0.05;
646 out.direction = (third < 0.0 ? -1.0 : 1.0) * direction.normalized();
647 out.length = std::clamp(length, std::min(step, bound), bound);
648 out.third = third;
649 out.valid = true;
650 return out;
651}
652
654 const StencilIndexCertificate& certificate,
655 const Eigen::VectorXd& centre_gradient, double resolution,
656 double step_bound)
657{
658 const double h = certificate.weak_step;
659 const double inverse_mass = certificate.weak_inverse_mass;
660 const Eigen::VectorXd& d = certificate.weak_direction;
661 if (d.size() != centre_gradient.size() ||
662 certificate.weak_even.size() != d.size() || !(h > 0.0) ||
663 !std::isfinite(h) || !centre_gradient.allFinite() ||
664 !certificate.weak_even.allFinite() || !(inverse_mass > 0.0) ||
665 !std::isfinite(inverse_mass))
666 return {};
667 const double third = 2.0 * inverse_mass *
668 d.dot(certificate.weak_even - centre_gradient) /
669 (h * h);
670 return boundedWeakModeContinuation(d, third, h, certificate.positive_lower,
671 resolution, step_bound);
672}
673
677 bool higher_order = false;
678 double upper = std::numeric_limits<double>::infinity();
680};
681
682template <typename Gradients>
684 const Eigen::MatrixXd& previous_cartesian_hessian,
685 const Eigen::VectorXd& positions, const Eigen::VectorXd& masses,
686 const Eigen::VectorXd& centre_gradient, double step, double step_bound,
687 const Gradients& gradients)
688{
690 if (positions.size() < 6 || masses.size() * 3 != positions.size() ||
691 centre_gradient.size() != positions.size() || !positions.allFinite() ||
692 !masses.allFinite() || (masses.array() <= 0.0).any() ||
693 !centre_gradient.allFinite() || !(step > 0.0) || !std::isfinite(step))
694 return out;
695 // The Hessian selects a physical basis at this geometry. Its curvatures
696 // are proposals; only the fresh actions determine the rejection.
697 const auto proposal =
698 analyzeStencilIndex(previous_cartesian_hessian, positions, masses);
699 if (!proposal.valid || proposal.modes.cols() < 2) return out;
700 const Eigen::MatrixXd basis = proposal.modes.leftCols(2);
701 const auto refined =
702 refineWeakModes(positions, masses, basis, step, gradients);
703 if (!refined.valid) return out;
704 const Eigen::Matrix2d block = basis.transpose() * refined.action;
705 const Eigen::Matrix2d symmetric = 0.5 * (block + block.transpose());
706 const Eigen::Matrix2d projected_error =
707 basis.cwiseAbs().transpose() * refined.truncation.cwiseAbs();
708 const Eigen::Matrix2d error =
709 0.5 * (projected_error + projected_error.transpose()) +
710 (0.5 * (block - block.transpose())).cwiseAbs();
711 const double gram_defect =
712 (basis.transpose() * basis - Eigen::Matrix2d::Identity()).norm();
713 const double allowance =
714 (gram_defect + 64.0 * std::numeric_limits<double>::epsilon() *
715 static_cast<double>(positions.size())) *
716 (symmetric.norm() + refined.action.norm() + refined.truncation.norm());
717 if (!(gram_defect < 1.0) || !error.allFinite() || !std::isfinite(allowance))
718 return out;
719 Eigen::Matrix2d majorant = symmetric.cwiseAbs() + error;
720 majorant.diagonal() = symmetric.diagonal() + error.diagonal();
721 const Eigen::SelfAdjointEigenSolver<Eigen::Matrix2d> spectrum(majorant);
722 if (spectrum.info() != Eigen::Success) return out;
723 // For abs(D) <= error, z^T (block + D) z is bounded by the
724 // majorant evaluated at abs(z). A negative upper bound gives two negative
725 // modes by restriction, conditional on the measured truncation envelope.
726 out.upper = spectrum.eigenvalues()(1) + allowance;
727 out.higher_order = out.upper < 0.0;
728 if (!out.higher_order) return out;
729 const Eigen::VectorXd direction = refined.directions.col(1);
730 const double third = 2.0 * refined.inverse_mass(1) *
731 direction.dot(refined.even.col(1) - centre_gradient) /
732 (step * step);
733 out.continuation = boundedWeakModeContinuation(
734 direction, third, step, symmetric(1, 1) - error(1, 1) - allowance,
735 error(1, 1) + allowance, step_bound);
736 return out;
737}
738
757
758inline StencilAction nextStencilAction(bool higher_order,
759 bool weak_higher_order,
760 bool weak_uncertified,
761 int& continuations,
762 int maximum_continuations)
763{
764 if (higher_order) return StencilAction::LeaveHigherOrder;
765 if (weak_higher_order) return StencilAction::AcceptUncertified;
766 if (!weak_uncertified) return StencilAction::Accept;
767 if (continuations < maximum_continuations) {
768 ++continuations;
770 }
772}
773
778inline constexpr double defaultStencilStep() noexcept
779{
780 return 0.01;
781}
782
784inline constexpr double stencilStepOrDefault(double requested) noexcept
785{
786 return requested > 0.0 ? requested : defaultStencilStep();
787}
788
792inline constexpr int forceStopAdaptiveStepReductions() noexcept
793{
794 return 4;
795}
796
803template <typename Gradients>
805 const StencilIndex& index, const Eigen::VectorXd& positions,
806 const Eigen::VectorXd& masses, double step, const Gradients& gradients,
807 double noise_multiplier = 0.0)
808{
809 Eigen::MatrixXd retained_points(positions.size(), 0);
810 Eigen::MatrixXd retained_gradients(0, positions.size());
811 const auto measure = [&](const Eigen::MatrixXd& points) -> Eigen::MatrixXd {
812 std::vector<Eigen::Index> slots(points.cols());
813 std::vector<Eigen::Index> missing;
814 const Eigen::Index held = retained_points.cols();
815 for (Eigen::Index j = 0; j < points.cols(); ++j) {
816 Eigen::Index found = 0;
817 while (found < held && !(retained_points.col(found).array() ==
818 points.col(j).array())
819 .all())
820 ++found;
821 if (found == held) {
822 const auto pending =
823 std::find_if(missing.begin(), missing.end(),
824 [&](Eigen::Index candidate) {
825 return (points.col(candidate).array() ==
826 points.col(j).array())
827 .all();
828 });
829 slots[j] = held + std::distance(missing.begin(), pending);
830 if (pending == missing.end()) missing.push_back(j);
831 } else {
832 slots[j] = found;
833 }
834 }
835 if (!missing.empty()) {
836 Eigen::MatrixXd requests(points.rows(), missing.size());
837 for (Eigen::Index j = 0; j < requests.cols(); ++j)
838 requests.col(j) = points.col(missing[j]);
839 const Eigen::MatrixXd measured = gradients(requests);
840 if (measured.rows() != requests.cols() ||
841 measured.cols() != requests.rows() || !measured.allFinite())
842 return Eigen::MatrixXd::Constant(
843 points.cols(), points.rows(),
844 std::numeric_limits<double>::quiet_NaN());
845 retained_points.conservativeResize(Eigen::NoChange,
846 held + requests.cols());
847 retained_gradients.conservativeResize(held + requests.cols(),
848 Eigen::NoChange);
849 retained_points.rightCols(requests.cols()) = requests;
850 retained_gradients.bottomRows(requests.cols()) = measured;
851 }
852 Eigen::MatrixXd result(points.cols(), points.rows());
853 for (Eigen::Index j = 0; j < points.cols(); ++j)
854 result.row(j) = retained_gradients.row(slots[j]);
855 return result;
856 };
857 const auto stamp = [&](StencilIndexCertificate& cert) {
858 cert.refinement_points = retained_points;
859 cert.refinement_gradients = retained_gradients;
860 };
861 auto out = certifyStencilIndex(index, positions, masses, step, measure, 0,
862 noise_multiplier);
863 stamp(out);
864 int step_reductions = 0;
865 while (!out.first_order && !out.higher_order && out.negative_upper < 0.0 &&
866 std::isfinite(out.weak_block_lower) &&
867 std::isfinite(out.weak_block_error)) {
868 Eigen::Index weak = out.refined_positive_modes;
869 const bool smaller_step = out.weak_block_lower <= 0.0;
870 if (smaller_step) {
871 if (step_reductions >= forceStopAdaptiveStepReductions()) break;
872 const double next = 0.5 * step;
873 if (!(next > 0.0) || next == step) break;
874 step = next;
875 ++step_reductions;
876 } else {
877 if (weak + 1 >= index.curvatures.size()) break;
878 ++weak;
879 }
880 auto refined = certifyStencilIndex(index, positions, masses, step,
881 measure, weak, noise_multiplier);
882 stamp(refined);
883 if (refined.first_order || refined.higher_order) return refined;
884 if (smaller_step && !(refined.weak_block_error < out.weak_block_error))
885 break;
886 out = std::move(refined);
887 }
888 stamp(out);
889 return out;
890}
891
900 bool measured = false;
901 double lower = 0.0;
902 double upper = 0.0;
904 double asymmetry = 0.0;
906 Eigen::MatrixXd points;
907 Eigen::MatrixXd gradients;
908};
909
910template <typename Gradients>
911PairRitzProbe probePairRitz(const Eigen::VectorXd& center,
912 const Eigen::VectorXd& masses,
913 const Eigen::VectorXd& u, const Eigen::VectorXd& v,
914 double step, const Gradients& gradients)
915{
917 const Eigen::Index dof = center.size();
918 if (masses.size() * 3 != dof || u.size() != dof || v.size() != dof ||
919 !(step > 0.0))
920 return out;
921 Eigen::VectorXd root(dof);
922 for (Eigen::Index k = 0; k < dof; ++k)
923 root(k) = std::sqrt(masses(k / 3));
924 Eigen::VectorXd yu = root.cwiseProduct(u);
925 if (!(yu.norm() > 0.0)) return out;
926 yu.normalize();
927 Eigen::VectorXd yv = root.cwiseProduct(v);
928 yv -= yu.dot(yv) * yu;
929 if (!(yv.norm() > 1e-8)) return out;
930 yv.normalize();
931 out.points.resize(dof, 4);
932 const Eigen::VectorXd xu = yu.cwiseQuotient(root);
933 const Eigen::VectorXd xv = yv.cwiseQuotient(root);
934 out.points.col(0) = center + step * xu;
935 out.points.col(1) = center - step * xu;
936 out.points.col(2) = center + step * xv;
937 out.points.col(3) = center - step * xv;
938 out.gradients = gradients(out.points);
939 if (out.gradients.rows() != 4 || out.gradients.cols() != dof ||
940 !out.gradients.allFinite())
941 return out;
942 // H_mw y = M^{-1/2} H x, H x by the central difference.
943 const Eigen::VectorXd hu =
944 ((out.gradients.row(0) - out.gradients.row(1)).transpose() /
945 (2.0 * step))
946 .cwiseQuotient(root);
947 const Eigen::VectorXd hv =
948 ((out.gradients.row(2) - out.gradients.row(3)).transpose() /
949 (2.0 * step))
950 .cwiseQuotient(root);
951 const double a = yu.dot(hu);
952 const double d = yv.dot(hv);
953 const double b_uv = yu.dot(hv);
954 const double b_vu = yv.dot(hu);
955 const double b = 0.5 * (b_uv + b_vu);
956 const double radius = std::hypot(0.5 * (a - d), b);
957 out.lower = 0.5 * (a + d) - radius;
958 out.upper = 0.5 * (a + d) + radius;
959 out.asymmetry = 0.5 * std::abs(b_uv - b_vu);
960 out.measured = std::isfinite(out.lower) && std::isfinite(out.upper);
961 return out;
962}
963
973 bool measured = false;
975 Eigen::VectorXd values;
977 Eigen::MatrixXd directions;
979 double asymmetry = 0.0;
981 Eigen::MatrixXd basis;
984 Eigen::MatrixXd points;
985 Eigen::MatrixXd gradients;
986};
987
988template <typename Gradients>
989SubspaceRitzProbe probeSubspaceRitz(const Eigen::VectorXd& center,
990 const Eigen::VectorXd& masses,
991 const Eigen::MatrixXd& candidates,
992 double step, const Gradients& gradients)
993{
995 const Eigen::Index dof = center.size();
996 if (masses.size() * 3 != dof || candidates.rows() != dof ||
997 candidates.cols() == 0 || !(step > 0.0) || !candidates.allFinite())
998 return out;
999 Eigen::VectorXd root(dof);
1000 for (Eigen::Index k = 0; k < dof; ++k)
1001 root(k) = std::sqrt(masses(k / 3));
1002 std::vector<Eigen::VectorXd> kept;
1003 for (Eigen::Index c = 0; c < candidates.cols(); ++c) {
1004 Eigen::VectorXd y = root.cwiseProduct(candidates.col(c));
1005 if (!(y.norm() > 0.0)) continue;
1006 y.normalize();
1007 for (int pass = 0; pass < 2; ++pass)
1008 for (const auto& k: kept)
1009 y -= k.dot(y) * k;
1010 if (!(y.norm() > 1e-8)) continue;
1011 y.normalize();
1012 kept.push_back(y);
1013 }
1014 const Eigen::Index m = static_cast<Eigen::Index>(kept.size());
1015 if (m == 0) return out;
1016 out.basis.resize(dof, m);
1017 out.points.resize(dof, 2 * m);
1018 for (Eigen::Index j = 0; j < m; ++j) {
1019 out.basis.col(j) = kept[static_cast<std::size_t>(j)];
1020 const Eigen::VectorXd x = out.basis.col(j).cwiseQuotient(root);
1021 out.points.col(2 * j) = center + step * x;
1022 out.points.col(2 * j + 1) = center - step * x;
1023 }
1024 out.gradients = gradients(out.points);
1025 if (out.gradients.rows() != 2 * m || out.gradients.cols() != dof ||
1026 !out.gradients.allFinite())
1027 return out;
1028 // H_mw y_j = M^{-1/2} H x_j by the central difference.
1029 Eigen::MatrixXd actions(dof, m);
1030 for (Eigen::Index j = 0; j < m; ++j)
1031 actions.col(j) =
1032 ((out.gradients.row(2 * j) - out.gradients.row(2 * j + 1))
1033 .transpose() /
1034 (2.0 * step))
1035 .cwiseQuotient(root);
1036 const Eigen::MatrixXd block = out.basis.transpose() * actions;
1037 const Eigen::MatrixXd symmetric = 0.5 * (block + block.transpose());
1038 out.asymmetry = 0.5 * (block - block.transpose()).cwiseAbs().maxCoeff();
1039 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> spectrum(symmetric);
1040 if (spectrum.info() != Eigen::Success) return out;
1041 out.values = spectrum.eigenvalues();
1042 out.directions.resize(dof, m);
1043 for (Eigen::Index j = 0; j < m; ++j) {
1044 Eigen::VectorXd direction =
1045 (out.basis * spectrum.eigenvectors().col(j)).cwiseQuotient(root);
1046 const double norm = direction.norm();
1047 out.directions.col(j) = norm > 0.0 ? direction / norm : direction;
1048 }
1049 out.measured = out.values.allFinite() && out.directions.allFinite() &&
1050 std::isfinite(out.asymmetry);
1051 return out;
1052}
1053
1057
1074
1084 bool measure_stencil = true;
1085 bool resume_force_loop = false;
1088 bool defer_certificate = false;
1091 bool reuse_columns = false;
1094 bool hand_off = false;
1095};
1096
1098 bool measured = false;
1099 bool first_order = false;
1100 bool higher_order = false;
1101 int calls = 0;
1104 Eigen::MatrixXd hessian;
1107 Eigen::MatrixXd lowest_modes;
1109 Eigen::VectorXd center;
1110 Eigen::MatrixXd positions;
1111 Eigen::MatrixXd gradients;
1112 Eigen::VectorXd energies;
1113 double step = 0.0;
1114};
1115
1139
1143 const ForceStopMeasurement& measured) noexcept
1144{
1145 return measured.measured && !measured.first_order;
1146}
1147
1151inline constexpr int forceStopSoftRefinementLimit() noexcept
1152{
1153 return 1;
1154}
1155
1161inline constexpr int forceStopRefinementCertified() noexcept
1162{
1163 return 0;
1164}
1165
1169inline constexpr int forceStopRefinementStillHigher() noexcept
1170{
1171 return -2;
1172}
1173
1175inline bool sameStencilGeometry(const Eigen::VectorXd& first,
1176 const Eigen::VectorXd& second) noexcept
1177{
1178 if (first.size() == 0 || first.size() != second.size() ||
1179 !first.allFinite() || !second.allFinite())
1180 return false;
1181 return (first - second).norm() <=
1182 1e-8 * (1.0 + std::max(first.norm(), second.norm()));
1183}
1184
1186inline constexpr int forceStopMinModeEscapes() noexcept
1187{
1188 return 16;
1189}
1190
1192{
1194 // The family is recorded so a caller can show the plan does not
1195 // branch on it. It is not a switch.
1196 (void)query.family;
1197 if (!query.requested) {
1198 plan.measure_stencil = false;
1199 plan.refine_soft_modes = false;
1200 return plan;
1201 }
1202 if (query.columns_already_measured) {
1203 plan.measure_stencil = false;
1204 plan.reuse_columns = true;
1207 return plan;
1208 }
1210 // The band stencil on a following saddle is not the certificate.
1211 // It ends with unresolved modes and is measured again after the
1212 // saddle moves. Hand the climbing image over and measure once,
1213 // at the final saddle.
1214 plan.measure_stencil = false;
1215 plan.refine_soft_modes = false;
1216 plan.defer_certificate = true;
1217 plan.hand_off = true;
1218 }
1219 return plan;
1220}
1221
1223 bool requested)
1224{
1226 query.family = family;
1227 query.requested = requested;
1228 return planForceStopStencil(query);
1229}
1230
1234 const ForceStopStencilPlan& plan) noexcept
1235{
1236 return plan.measure_stencil || plan.reuse_columns;
1237}
1238
1241inline Eigen::Index forceStopHeldColumn(const Eigen::MatrixXd& held,
1242 const Eigen::VectorXd& query,
1243 double relative = 1e-6)
1244{
1245 if (held.rows() != query.size() || held.cols() == 0 || !query.allFinite())
1246 return -1;
1247 Eigen::Index nearest = -1;
1248 double best = std::numeric_limits<double>::infinity();
1249 for (Eigen::Index col = 0; col < held.cols(); ++col) {
1250 if (!held.col(col).allFinite()) continue;
1251 const double distance = (held.col(col) - query).norm();
1252 if (distance < best) {
1253 best = distance;
1254 nearest = col;
1255 }
1256 }
1257 if (nearest < 0 || !(best <= relative * (1.0 + query.norm()))) return -1;
1258 return nearest;
1259}
1260
1263inline Eigen::MatrixXd forceStopLowestModes(
1264 const Eigen::MatrixXd& columns, const Eigen::VectorXd& positions,
1265 const Eigen::VectorXd& masses, Eigen::Index count,
1266 const Eigen::MatrixXd* measured = nullptr)
1267{
1268 const StencilIndex index =
1269 analyzeStencilIndex(columns, positions, masses, measured);
1270 if (!index.valid || index.modes.cols() == 0 || count <= 0) return {};
1271 const Eigen::Index n = std::min(count, index.modes.cols());
1272 Eigen::VectorXd root(positions.size());
1273 for (Eigen::Index k = 0; k < positions.size(); ++k)
1274 root(k) = std::sqrt(masses(k / 3));
1275 Eigen::MatrixXd cartesian(positions.size(), n);
1276 for (Eigen::Index j = 0; j < n; ++j) {
1277 Eigen::VectorXd direction = index.modes.col(j).cwiseQuotient(root);
1278 const double norm = direction.norm();
1279 cartesian.col(j) = norm > 0.0 ? direction / norm : direction;
1280 }
1281 return cartesian;
1282}
1283
1286inline bool forceStopStencilCertifies(bool measured, bool first_order) noexcept
1287{
1288 return measured && first_order;
1289}
1290
1303template <typename Measure, typename Refine>
1305 Measure measure, Refine refine, ForceStopStencilPlan plan,
1306 const ForceStopMeasurement* prior = nullptr)
1307{
1309 out.resume_force_loop = plan.resume_force_loop;
1310 const auto finish = [&]() {
1311 out.needs_min_mode_escape =
1312 out.measured && out.higher_order && !out.certified && !out.hand_off;
1313 return out;
1314 };
1315 const bool have_prior =
1316 plan.reuse_columns && prior != nullptr && prior->measured;
1317 if (plan.defer_certificate && !have_prior) {
1318 out.deferred_to_following_stage = true;
1319 out.hand_off = true;
1320 return out;
1321 }
1322 bool measure_now = true;
1323 for (int pass = 0;; ++pass) {
1324 if (measure_now) {
1326 if (pass == 0 && have_prior) {
1327 const bool inconclusive = prior->measured &&
1328 !prior->first_order &&
1329 !prior->higher_order;
1330 const bool hand_reading =
1331 plan.hand_off &&
1332 (inconclusive || prior->higher_order || prior->first_order);
1333 // The stored columns are the measurement. A final stage
1334 // whose prior is only unresolved still asks `measure` for
1335 // new displacements; that callback must not repeat these
1336 // columns.
1337 if (hand_reading || prior->first_order || prior->higher_order) {
1338 got = *prior;
1339 got.calls = 0;
1340 out.reused_columns = true;
1341 } else if (plan.measure_stencil) {
1342 got = measure();
1343 out.reused_columns = true;
1344 } else {
1345 got = *prior;
1346 got.calls = 0;
1347 out.reused_columns = true;
1348 }
1349 } else {
1350 if (!plan.measure_stencil && pass == 0) return finish();
1351 got = measure();
1352 }
1353 out.measured = got.measured;
1354 out.higher_order = got.higher_order;
1355 out.first_order =
1356 got.measured && got.first_order && !got.higher_order;
1357 out.measurements += 1;
1358 out.calls += got.calls;
1359 if (pass == 0) out.handed = got;
1360 const bool inconclusive =
1361 got.measured && !out.first_order && !out.higher_order;
1362 if (plan.hand_off && pass == 0 && have_prior &&
1363 (inconclusive || got.higher_order)) {
1364 out.hand_off = true;
1365 out.certified = false;
1366 out.handed = got;
1367 out.handed.calls = 0;
1368 return finish();
1369 }
1370 if (forceStopStencilCertifies(got.measured, out.first_order)) {
1371 out.certified = true;
1372 return finish();
1373 }
1374 }
1375 measure_now = true;
1376 const bool refine_now = plan.refine_soft_modes && out.measured &&
1377 out.higher_order &&
1379 if (!refine_now) return finish();
1380 const int spent = refine();
1381 if (spent == forceStopRefinementStillHigher()) {
1382 out.soft_refinements += 1;
1383 out.measured = true;
1384 out.higher_order = true;
1385 out.first_order = false;
1386 out.certified = false;
1387 measure_now = false;
1388 continue;
1389 }
1390 if (spent < 0) return finish();
1391 out.soft_refinements += 1;
1392 if (spent == forceStopRefinementCertified()) {
1393 out.certified = true;
1394 out.first_order = true;
1395 out.higher_order = false;
1396 return finish();
1397 }
1398 out.calls += spent;
1399 }
1400}
1401
1402} // namespace gpr::optim
1403
1404#endif
@ x
Definition Enums.h:28
@ y
Definition Enums.h:28
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
constexpr int forceStopRefinementCertified() noexcept
StencilAction nextStencilAction(bool higher_order, bool weak_higher_order, bool weak_uncertified, int &continuations, int maximum_continuations)
MeasuredIndexDecision decideMeasuredIndex(const Eigen::MatrixXd &columns, const Eigen::VectorXd &positions, const Eigen::VectorXd &masses, double step, const Gradients &gradients, const Eigen::MatrixXd *measured=nullptr)
measured is analyzeStencilIndex's.
constexpr double stencilStepOrDefault(double requested) noexcept
The requested stencil step, or the default when none was requested.
WeakModeContinuation boundedWeakModeContinuation(const Eigen::VectorXd &direction, double third, double step, double lower, double resolution, double step_bound)
constexpr int forceStopAdaptiveStepReductions() noexcept
StencilIndexCertificate certifyAdaptiveStencilIndex(const StencilIndex &index, const Eigen::VectorXd &positions, const Eigen::VectorXd &masses, double step, const Gradients &gradients, double noise_multiplier=0.0)
bool forceStopCertificatePlanned(const ForceStopStencilPlan &plan) noexcept
Eigen::Index forceStopHeldColumn(const Eigen::MatrixXd &held, const Eigen::VectorXd &query, double relative=1e-6)
constexpr int forceStopSoftRefinementLimit() noexcept
SubspaceRitzProbe probeSubspaceRitz(const Eigen::VectorXd &center, const Eigen::VectorXd &masses, const Eigen::MatrixXd &candidates, double step, const Gradients &gradients)
double rotatedPositiveRestrictionLowerBound(const Eigen::MatrixXd &block, const Eigen::MatrixXd &errors, const Eigen::MatrixXd &coupling, const Eigen::VectorXd &tail, double allowance)
Eigen::MatrixXd forceStopLowestModes(const Eigen::MatrixXd &columns, const Eigen::VectorXd &positions, const Eigen::VectorXd &masses, Eigen::Index count, const Eigen::MatrixXd *measured=nullptr)
Eigen::MatrixXd vibrationalComplement(const Eigen::VectorXd &positions, const Eigen::VectorXd &masses)
bool sameStencilGeometry(const Eigen::VectorXd &first, const Eigen::VectorXd &second) noexcept
Two geometries share one stencil when a later request has not moved.
constexpr double defaultStencilStep() noexcept
StencilIndex analyzeStencilIndex(const Eigen::MatrixXd &columns, const Eigen::VectorXd &positions, const Eigen::VectorXd &masses, const Eigen::MatrixXd *measured=nullptr)
WeakModeContinuation weakModeContinuation(const StencilIndexCertificate &certificate, const Eigen::VectorXd &centre_gradient, double resolution, double step_bound)
bool forceStopStencilCertifies(bool measured, bool first_order) noexcept
Eigen::MatrixXd massWeightedRigidMotions(const Eigen::VectorXd &positions, const Eigen::VectorXd &masses)
bool forceStopMeasurementInconclusiveOrHigher(const ForceStopMeasurement &measured) noexcept
ForceStopStencilPlan planForceStopStencil(ForceStopStencilQuery query)
RefinedWeakModes refineWeakModes(const Eigen::VectorXd &positions, const Eigen::VectorXd &masses, const Eigen::MatrixXd &weak, double step, const Gradients &gradients, double noise_multiplier=0.0)
PairRitzProbe probePairRitz(const Eigen::VectorXd &center, const Eigen::VectorXd &masses, const Eigen::VectorXd &u, const Eigen::VectorXd &v, double step, const Gradients &gradients)
StencilIndexCertificate certifyStencilIndex(const StencilIndex &index, const Eigen::VectorXd &positions, const Eigen::VectorXd &masses, double step, const Gradients &gradients, Eigen::Index minimum_weak=0, double noise_multiplier=0.0)
SoftModeRefusal refuseSecondNegativeMode(const Eigen::VectorXd &positions, const Eigen::VectorXd &masses, const Eigen::MatrixXd &mass_weighted_modes, double step, const Gradients &gradients, double noise_multiplier=0.0)
NegativePairRejection rejectMeasuredNegativePair(const Eigen::MatrixXd &previous_cartesian_hessian, const Eigen::VectorXd &positions, const Eigen::VectorXd &masses, const Eigen::VectorXd &centre_gradient, double step, double step_bound, const Gradients &gradients)
constexpr int forceStopMinModeEscapes() noexcept
Min-mode escapes after a higher-order certificate, for every search.
constexpr int forceStopRefinementStillHigher() noexcept
ForceStopCertificateResult runForceStopCertificate(Measure measure, Refine refine, ForceStopStencilPlan plan, const ForceStopMeasurement *prior=nullptr)
std::vector< int > measured
1 for a mode at least 99% inside the measured directions.
bool hand_off
A following saddle receives handed.
bool deferred_to_following_stage
This stage did not measure. The following saddle certifies.
Eigen::VectorXd center
Stencil geometries (columns) and gradients (rows) already paid for.
bool defer_certificate
Do not measure. The following saddle certifies the point it reaches.
bool columns_already_measured
Finite-difference columns at this geometry were already paid for.
WeakModeContinuation continuation
Eigen::MatrixXd points
The four displaced points (columns) and their gradients (rows).
double asymmetry
Half the asymmetry of the measured 2 x 2 block.
Eigen::MatrixXd gradients
Eigen::VectorXd inverse_mass
Inverse effective mass of each Cartesian unit direction, 1/(d^T M d).
Eigen::MatrixXd modes
Their mass-weighted directions on the 3N coordinates.
double error
Entrywise error envelope of the refined block, as a spectral bound.
Eigen::VectorXd curvatures
Refined curvatures of the weak block, ascending, eV/(A^2 amu).
Eigen::MatrixXd action
Measured actions and signed truncation estimates in the input basis.
double weak_inverse_mass
Converts the Cartesian curvature rate to eV/(A^3 amu).
Eigen::MatrixXd modes
Mass-weighted eigenvectors on the 3N coordinates.
Eigen::MatrixXd hessian
Symmetrized mass-weighted Hessian on the 3N coordinates.
Eigen::VectorXd curvatures
Vibrational eigenvalues, ascending, eV/(A^2 amu).
Eigen::VectorXd values
Ascending Ritz curvatures.
double asymmetry
Half the largest asymmetry of the measured block.
Eigen::MatrixXd directions
Ritz vectors as unit Cartesian directions, one column per value.
Eigen::MatrixXd basis
Mass-weighted orthonormal basis measured (dof x m).
Eigen::VectorXd direction
Unit Cartesian direction, signed so that the weak curvature rises.
double third
Mass-weighted curvature change per Cartesian length, eV/(A^3 amu).