Loading...
Searching...
No Matches
gpr::geometry::InternalCoordinateSet Class Reference

#include <gpr/auxiliary/InternalCoordinates.h>

Classes

struct  RetractionReport

Public Member Functions

 InternalCoordinateSet (int n_atoms, std::vector< char > moving_atoms, std::vector< PrimitiveInternal > primitives, std::array< double, 9 > cell={})
bool hasSameDefinition (const InternalCoordinateSet &other) const noexcept
int numAtoms () const noexcept
const std::vector< char > & movingAtoms () const noexcept
const std::vector< PrimitiveInternal > & primitives () const
const std::vector< Eigen::Index > & activeColumns () const
const std::array< double, 9 > & cell () const noexcept
Eigen::VectorXd values (const Eigen::VectorXd &positions) const
Eigen::MatrixXd jacobian (const Eigen::VectorXd &positions) const
Eigen::MatrixXd directionalJacobian (const Eigen::VectorXd &positions, const Eigen::VectorXd &direction) const
Eigen::MatrixXd activeJacobian (const Eigen::VectorXd &positions) const
Eigen::VectorXd activeStep (const Eigen::VectorXd &full_step) const
Eigen::VectorXd linearizedChange (const Eigen::VectorXd &positions, const Eigen::VectorXd &direction) const
RetractionReport retractLinearizedReport (const Eigen::VectorXd &positions, const Eigen::VectorXd &cartesian_step) const
Eigen::VectorXd retractLinearized (const Eigen::VectorXd &positions, const Eigen::VectorXd &cartesian_step) const

Detailed Description

Definition at line 144 of file InternalCoordinates.h.

Constructor & Destructor Documentation

◆ InternalCoordinateSet()

gpr::geometry::InternalCoordinateSet::InternalCoordinateSet ( int n_atoms,
std::vector< char > moving_atoms,
std::vector< PrimitiveInternal > primitives,
std::array< double, 9 > cell = {} )

Definition at line 865 of file InternalCoordinates.cpp.

868 : n_atoms_(n_atoms),
869 moving_atoms_(std::move(moving_atoms)),
870 primitives_(std::move(primitives)),
871 active_columns_(),
872 cell_(cell)
873{
874 if (n_atoms_ < 0) {
875 throw std::invalid_argument(
876 "internal coordinate atom count is invalid");
877 }
878 validateMovingAtoms(n_atoms_, moving_atoms_);
879 for (const auto& primitive: primitives_) {
880 for (int i = 0; i < primitive.arity(); ++i) {
881 validateAtomIndex(primitive.atoms[static_cast<std::size_t>(i)],
882 n_atoms_);
883 }
884 }
885
886 active_columns_.reserve(static_cast<std::size_t>(3 * n_atoms_));
887 for (int atom = 0; atom < n_atoms_; ++atom) {
888 const bool moving = moving_atoms_.empty() ||
889 moving_atoms_[static_cast<std::size_t>(atom)] != 0;
890 if (!moving) continue;
891 for (int dim = 0; dim < 3; ++dim) {
892 active_columns_.push_back(3 * atom + dim);
893 }
894 }
895}
const std::vector< PrimitiveInternal > & primitives() const
const std::array< double, 9 > & cell() const noexcept

Member Function Documentation

◆ activeColumns()

const std::vector< Eigen::Index > & gpr::geometry::InternalCoordinateSet::activeColumns ( ) const
inlinenodiscard

Definition at line 167 of file InternalCoordinates.h.

168 {
169 return active_columns_;
170 }

◆ activeJacobian()

Eigen::MatrixXd gpr::geometry::InternalCoordinateSet::activeJacobian ( const Eigen::VectorXd & positions) const
nodiscard

Definition at line 998 of file InternalCoordinates.cpp.

1000{
1001 validatePositions(positions);
1002 Eigen::MatrixXd J(static_cast<Eigen::Index>(primitives_.size()),
1003 static_cast<Eigen::Index>(active_columns_.size()));
1004 J.setZero();
1005 const PairBox box(cell_);
1006 Eigen::VectorXd full_gradient(positions.size());
1007 for (Eigen::Index row = 0;
1008 row < static_cast<Eigen::Index>(primitives_.size()); ++row) {
1009 primitiveGradient(primitives_[static_cast<std::size_t>(row)], positions,
1010 box, full_gradient);
1011 for (Eigen::Index col = 0;
1012 col < static_cast<Eigen::Index>(active_columns_.size()); ++col) {
1013 J(row, col) =
1014 full_gradient(active_columns_[static_cast<std::size_t>(col)]);
1015 }
1016 }
1017 return J;
1018}

◆ activeStep()

Eigen::VectorXd gpr::geometry::InternalCoordinateSet::activeStep ( const Eigen::VectorXd & full_step) const
nodiscard

Definition at line 1020 of file InternalCoordinates.cpp.

1022{
1023 if (full_step.size() != static_cast<Eigen::Index>(3 * n_atoms_) ||
1024 !full_step.allFinite()) {
1025 throw std::invalid_argument(
1026 "internal coordinate step has invalid shape or values");
1027 }
1028 Eigen::VectorXd step(static_cast<Eigen::Index>(active_columns_.size()));
1029 for (Eigen::Index col = 0;
1030 col < static_cast<Eigen::Index>(active_columns_.size()); ++col) {
1031 step(col) = full_step(active_columns_[static_cast<std::size_t>(col)]);
1032 }
1033 return step;
1034}

◆ cell()

const std::array< double, 9 > & gpr::geometry::InternalCoordinateSet::cell ( ) const
inlinenodiscardnoexcept

Definition at line 171 of file InternalCoordinates.h.

172 {
173 return cell_;
174 }

◆ directionalJacobian()

Eigen::MatrixXd gpr::geometry::InternalCoordinateSet::directionalJacobian ( const Eigen::VectorXd & positions,
const Eigen::VectorXd & direction ) const
nodiscard

Derivative of jacobian(positions) along a Cartesian direction, exact by forward-over-forward AutoDiff of each primitive. Throws UndefinedInternalCoordinate where a primitive has no curvature, including a valence angle at collinearity.

Definition at line 955 of file InternalCoordinates.cpp.

957{
958 validatePositions(positions);
959 if (direction.size() != positions.size() || !direction.allFinite()) {
960 throw std::invalid_argument(
961 "internal coordinate direction has invalid shape or values");
962 }
963 Eigen::MatrixXd out = Eigen::MatrixXd::Zero(
964 static_cast<Eigen::Index>(primitives_.size()), positions.size());
965 const PairBox box(cell_);
966 Eigen::VectorXd gradient(positions.size()), row_out(positions.size());
967 for (Eigen::Index row = 0;
968 row < static_cast<Eigen::Index>(primitives_.size()); ++row) {
969 const auto& primitive = primitives_[static_cast<std::size_t>(row)];
970 // Validates the geometry the same way the Jacobian does.
971 primitiveGradient(primitive, positions, box, gradient);
972 if (primitive.kind == PrimitiveKind::Angle) {
973 const Eigen::Vector3d b =
974 atomPosition(positions, primitive.atoms[1]);
975 const Eigen::Vector3d u = box.displacement(
976 b, atomPosition(positions, primitive.atoms[0]));
977 const Eigen::Vector3d v = box.displacement(
978 b, atomPosition(positions, primitive.atoms[2]));
979 const double sin_theta = u.cross(v).norm() / (u.norm() * v.norm());
980 if (angleIsCollinear(sin_theta * sin_theta)) {
981 throw UndefinedInternalCoordinate(
982 "internal coordinate angle curvature is undefined at "
983 "collinearity");
984 }
985 }
986 row_out.setZero();
987 primitiveDirectionalGradient(primitive, positions, direction, box,
988 row_out);
989 if (!row_out.allFinite()) {
990 throw UndefinedInternalCoordinate(
991 "internal coordinate curvature is undefined at this geometry");
992 }
993 out.row(row) = row_out.transpose();
994 }
995 return out;
996}
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
bool angleIsCollinear(double sin2)

◆ hasSameDefinition()

bool gpr::geometry::InternalCoordinateSet::hasSameDefinition ( const InternalCoordinateSet & other) const
nodiscardnoexcept

Exact coordinate definition, including primitive order and bend axes. Equal definitions share a chart at the same Cartesian anchor.

Definition at line 897 of file InternalCoordinates.cpp.

899{
900 if (n_atoms_ != other.n_atoms_ || moving_atoms_ != other.moving_atoms_ ||
901 cell_ != other.cell_ || primitives_.size() != other.primitives_.size())
902 return false;
903 for (std::size_t i = 0; i < primitives_.size(); ++i) {
904 const auto& a = primitives_[i];
905 const auto& b = other.primitives_[i];
906 if (a.kind != b.kind || a.atoms != b.atoms ||
907 (a.reference.array() != b.reference.array()).any())
908 return false;
909 }
910 return true;
911}

◆ jacobian()

Eigen::MatrixXd gpr::geometry::InternalCoordinateSet::jacobian ( const Eigen::VectorXd & positions) const
nodiscard

Definition at line 937 of file InternalCoordinates.cpp.

939{
940 validatePositions(positions);
941 Eigen::MatrixXd J(static_cast<Eigen::Index>(primitives_.size()),
942 positions.size());
943 J.setZero();
944 const PairBox box(cell_);
945 Eigen::VectorXd gradient(positions.size());
946 for (Eigen::Index row = 0;
947 row < static_cast<Eigen::Index>(primitives_.size()); ++row) {
948 primitiveGradient(primitives_[static_cast<std::size_t>(row)], positions,
949 box, gradient);
950 J.row(row) = gradient.transpose();
951 }
952 return J;
953}

◆ linearizedChange()

Eigen::VectorXd gpr::geometry::InternalCoordinateSet::linearizedChange ( const Eigen::VectorXd & positions,
const Eigen::VectorXd & direction ) const
nodiscard

Primitive changes B * direction without forming the dense Wilson matrix. Rows are the primitive order of this set.

Definition at line 1036 of file InternalCoordinates.cpp.

1038{
1039 validatePositions(positions);
1040 if (direction.size() != positions.size() || !direction.allFinite()) {
1041 throw std::invalid_argument(
1042 "internal coordinate direction has invalid shape or values");
1043 }
1044 const PairBox box(cell_);
1045 Eigen::VectorXd change(static_cast<Eigen::Index>(primitives_.size()));
1046 Eigen::VectorXd gradient(positions.size());
1047 for (Eigen::Index row = 0; row < change.size(); ++row) {
1048 primitiveGradient(primitives_[static_cast<std::size_t>(row)], positions,
1049 box, gradient);
1050 change(row) = gradient.dot(direction);
1051 }
1052 return change;
1053}

◆ movingAtoms()

const std::vector< char > & gpr::geometry::InternalCoordinateSet::movingAtoms ( ) const
inlinenodiscardnoexcept

Definition at line 159 of file InternalCoordinates.h.

160 {
161 return moving_atoms_;
162 }

◆ numAtoms()

int gpr::geometry::InternalCoordinateSet::numAtoms ( ) const
inlinenodiscardnoexcept

Definition at line 155 of file InternalCoordinates.h.

156 {
157 return n_atoms_;
158 }

◆ primitives()

const std::vector< PrimitiveInternal > & gpr::geometry::InternalCoordinateSet::primitives ( ) const
inlinenodiscard

Definition at line 163 of file InternalCoordinates.h.

164 {
165 return primitives_;
166 }

◆ retractLinearized()

Eigen::VectorXd gpr::geometry::InternalCoordinateSet::retractLinearized ( const Eigen::VectorXd & positions,
const Eigen::VectorXd & cartesian_step ) const
inlinenodiscard

Definition at line 232 of file InternalCoordinates.h.

235 {
236 const RetractionReport report =
237 retractLinearizedReport(positions, cartesian_step);
238 if (report.status == RetractionStatus::Failed) {
239 throw InternalCoordinateRetractionFailed(
240 "internal coordinate retraction failed");
241 }
242 return report.positions;
243 }
RetractionReport retractLinearizedReport(const Eigen::VectorXd &positions, const Eigen::VectorXd &cartesian_step) const

◆ retractLinearizedReport()

InternalCoordinateSet::RetractionReport gpr::geometry::InternalCoordinateSet::retractLinearizedReport ( const Eigen::VectorXd & positions,
const Eigen::VectorXd & cartesian_step ) const
nodiscard

Gauss-Newton retraction onto the linearized internal-coordinate target. Retracted requires an accepted step whose residual improved, a constraint residual within tolerance, or least-squares stationarity of a usable Jacobian. A correction below the coordinate resolution does not succeed on its own. That resolution is the spread of the involved atoms about their centroid, so a translation of the molecule does not change it. An undefined primitive gradient fails the retraction. Redundant primitives make the linearized target inconsistent, so the residual floor is generally far above epsilon and stationarity of a usable Jacobian ends the iteration. The anchor row space comes from the chart's sparse QR and compact-R SVD. A wide Wilson matrix refines the unresolved tail of B^T B with that SVD, because a left-looking QR of the Gabriel complex does not fit the retraction budget. Rows below the dense retraction's relative cutoff stay in the reported rank and are not inverted.

Definition at line 1415 of file InternalCoordinates.cpp.

1418{
1419 validatePositions(positions);
1420 RetractionReport report;
1421 if (cartesian_step.size() != positions.size() ||
1422 !cartesian_step.allFinite()) {
1423 throw std::invalid_argument(
1424 "internal coordinate retraction step has invalid shape or values");
1425 }
1426
1427 const Eigen::VectorXd active_step = activeStep(cartesian_step);
1428 const PairBox box(cell_);
1429 std::vector<Eigen::Index> active_of_full(
1430 static_cast<std::size_t>(positions.size()), -1);
1431 for (Eigen::Index col = 0;
1432 col < static_cast<Eigen::Index>(active_columns_.size()); ++col) {
1433 const Eigen::Index full =
1434 active_columns_[static_cast<std::size_t>(col)];
1435 if (full >= 0 && static_cast<std::size_t>(full) < active_of_full.size())
1436 active_of_full[static_cast<std::size_t>(full)] = col;
1437 }
1438
1439 std::vector<std::size_t> regular_rows;
1440 std::vector<double> target_values;
1441 bool target_in_domain = true;
1442 const Eigen::Index n_active =
1443 static_cast<Eigen::Index>(active_columns_.size());
1444 std::vector<Eigen::Triplet<double>> anchor_entries;
1445 anchor_entries.reserve(12 * primitives_.size());
1446 Eigen::VectorXd full_gradient(positions.size());
1447 SparseRow sparse;
1448 for (std::size_t row = 0; row < primitives_.size(); ++row) {
1449 try {
1450 const double value =
1451 primitiveValue(primitives_[row], positions, box);
1452 primitiveGradient(primitives_[row], positions, box, full_gradient);
1453 harvestRow(primitives_[row], full_gradient, active_of_full, true,
1454 sparse);
1455 ++report.wilson_rows_evaluated;
1456 regular_rows.push_back(row);
1457 const double target = value + sparseDot(sparse, active_step);
1458 target_values.push_back(target);
1459 const Eigen::Index subset_row =
1460 static_cast<Eigen::Index>(regular_rows.size() - 1);
1461 for (int i = 0; i < sparse.nnz; ++i) {
1462 anchor_entries.emplace_back(
1463 subset_row, sparse.column[static_cast<std::size_t>(i)],
1464 sparse.value[static_cast<std::size_t>(i)]);
1465 }
1466 if (primitives_[row].kind == PrimitiveKind::Angle &&
1467 (target < 0.0 || target > kPi))
1468 target_in_domain = false;
1469 } catch (const std::invalid_argument&) {
1470 // A primitive that cannot be evaluated at these positions (a
1471 // degenerate angle, say) contributes no row: it is left out of
1472 // regular_rows and target_values rather than aborting the build.
1473 }
1474 }
1475
1476 Eigen::VectorXd x = positions;
1477 for (Eigen::Index col = 0; col < n_active; ++col) {
1478 const Eigen::Index full_col =
1479 active_columns_[static_cast<std::size_t>(col)];
1480 x(full_col) += cartesian_step(full_col);
1481 }
1482 // A valence-angle target outside [0, pi] has no Cartesian inverse.
1483 // Crossing the cone point is valid atomic motion, so this chart-domain
1484 // case retains the requested Cartesian step with frozen atoms fixed
1485 // and reports that status. An empty primitive set is a failure: the
1486 // Cartesian trial is not returned in its place.
1487 if (!target_in_domain) {
1488 report.positions = x;
1489 report.residual = std::numeric_limits<double>::infinity();
1490 report.status = RetractionStatus::AngleDomain;
1491 return report;
1492 }
1493 if (regular_rows.empty()) {
1494 report.status = RetractionStatus::Failed;
1495 return report;
1496 }
1497
1498 Eigen::VectorXd target(static_cast<Eigen::Index>(target_values.size()));
1499 for (Eigen::Index row = 0; row < target.size(); ++row) {
1500 target(row) = target_values[static_cast<std::size_t>(row)];
1501 }
1502 const auto subsetValues = [&](const Eigen::VectorXd& pos,
1503 Eigen::VectorXd& out) {
1504 out.resize(static_cast<Eigen::Index>(regular_rows.size()));
1505 for (Eigen::Index row = 0; row < out.size(); ++row) {
1506 const std::size_t primitive_row =
1507 regular_rows[static_cast<std::size_t>(row)];
1508 try {
1509 out(row) = primitiveValue(primitives_[primitive_row], pos, box);
1510 } catch (const std::invalid_argument&) {
1511 return false;
1512 }
1513 }
1514 return true;
1515 };
1516 const auto subsetResidual = [&](const Eigen::VectorXd& current) {
1517 Eigen::VectorXd residual = target - current;
1518 for (Eigen::Index row = 0; row < residual.size(); ++row) {
1519 const auto kind =
1520 primitives_[regular_rows[static_cast<std::size_t>(row)]].kind;
1521 if (kind == PrimitiveKind::Angle ||
1522 kind == PrimitiveKind::Dihedral) {
1523 residual(row) = wrapAngularResidual(residual(row));
1524 }
1525 }
1526 return residual;
1527 };
1528
1529 Eigen::VectorXd best = x;
1530 Eigen::VectorXd current_values;
1531 if (!subsetValues(x, current_values)) {
1532 report.status = RetractionStatus::Failed;
1533 return report;
1534 }
1535 double best_residual = subsetResidual(current_values).norm();
1536 const double residual_tol =
1537 std::numeric_limits<double>::epsilon() * std::max(1.0, target.norm()) *
1538 std::max<double>(
1539 1.0, static_cast<double>(target.size() + active_columns_.size()));
1540 auto fail = [&]() {
1541 report.positions.resize(0);
1542 report.residual = best_residual;
1543 report.status = RetractionStatus::Failed;
1544 return report;
1545 };
1546 auto finish = [&](const Eigen::VectorXd& retracted) {
1547 // An accepted residual can sit on a singular primitive gradient
1548 // (a valence angle on its cone has a value and no derivative).
1549 // That geometry is Failed, not a retraction.
1550 for (const std::size_t row: regular_rows) {
1551 // The recheck is a Wilson evaluation of the accepted geometry,
1552 // so it counts with the anchor pass and the iteration passes.
1553 ++report.wilson_rows_evaluated;
1554 try {
1555 primitiveGradient(primitives_[row], retracted, box,
1556 full_gradient);
1557 } catch (const std::invalid_argument&) {
1558 return fail();
1559 }
1560 }
1561 report.positions = retracted;
1562 report.residual = best_residual;
1563 report.status = RetractionStatus::Retracted;
1564 return report;
1565 };
1566 if (best_residual <= residual_tol) return finish(best);
1567
1568 // Corrections lie in the anchor Jacobian's row space. Its nullspace
1569 // contains allowed Cartesian motion that the primitives cannot resolve
1570 // to first order; fitting their nonlinear residual must preserve it.
1571 // The row space is the chart factorisation: a sparse QR of B and the
1572 // SVD of compact R when that QR fits, and the same SVD on the
1573 // unresolved tail of B^T B when it does not. Either way a rigid-body
1574 // eigenvalue at the Gram floor is a null singular value and is dropped.
1575 // Rows between that floor and the retraction cutoff stay in the reported
1576 // rank, but inverting them replaces a second-order residual with a
1577 // first-order Cartesian move, so the step uses the resolved rows only.
1578 Eigen::MatrixXd correction_basis;
1579 Eigen::MatrixXd step_basis;
1580 {
1581 SparseWilsonMatrix anchor_wilson(
1582 static_cast<Eigen::Index>(regular_rows.size()), n_active);
1583 anchor_wilson.setFromTriplets(anchor_entries.begin(),
1584 anchor_entries.end());
1585 if (n_active == 0) return fail();
1586 if (n_active <= kExactWilsonQrColumns) {
1587 SparseWilsonQr anchor_qr;
1588 Eigen::Index anchor_compact = 0;
1589 Eigen::MatrixXd anchor_inverse;
1590 Eigen::VectorXd anchor_sigma;
1591 if (!factorSparseWilson(anchor_wilson, anchor_qr, anchor_compact,
1592 correction_basis, anchor_inverse,
1593 &anchor_sigma)) {
1594 return fail();
1595 }
1596 const double sigma_max =
1597 anchor_sigma.size() == 0 ? 0.0 : anchor_sigma.maxCoeff();
1598 Eigen::MatrixXd chart_basis;
1599 selectRowSpace(correction_basis, anchor_sigma, sigma_max,
1600 anchor_wilson.rows(), anchor_wilson.cols(),
1601 chart_basis, step_basis);
1602 correction_basis = std::move(chart_basis);
1603 } else if (!refinedWilsonRowSpace(anchor_wilson, correction_basis,
1604 step_basis)) {
1605 return fail();
1606 }
1607 }
1608 report.anchor_rank = correction_basis.cols();
1609 if (step_basis.cols() <= 0) return fail();
1610
1611 const int max_steps = std::max<int>(1, static_cast<int>(n_active));
1612 const double eps = std::numeric_limits<double>::epsilon();
1613 bool any_accepted = false;
1614 // current_values contains the primitives at the accepted geometry x.
1615 for (int iter = 0; iter < max_steps; ++iter) {
1616 report.iterations = iter + 1;
1617 const Eigen::VectorXd residual = subsetResidual(current_values);
1618 if (residual.norm() <= residual_tol) return finish(x);
1619 // J and J^T residual from one sparse pass. The Gauss-Newton step is
1620 // the min-norm solution of J V y = residual. V is the resolved part
1621 // of the anchor row space. The same sparse QR and compact-R SVD
1622 // produce the step when V is narrow enough; a wide V is already
1623 // deflated, so the normal matrix is factored directly.
1624 const bool wide_step = step_basis.cols() > kExactWilsonQrColumns;
1625 std::vector<Eigen::Triplet<double>> entries;
1626 Eigen::MatrixXd jtj;
1627 if (!wide_step)
1628 entries.reserve(static_cast<std::size_t>(residual.size()) * 12);
1629 else
1630 jtj = Eigen::MatrixXd::Zero(n_active, n_active);
1631 Eigen::VectorXd jtr = Eigen::VectorXd::Zero(n_active);
1632 bool rows_defined = true;
1633 for (Eigen::Index row = 0; row < residual.size(); ++row) {
1634 const std::size_t primitive_row =
1635 regular_rows[static_cast<std::size_t>(row)];
1636 try {
1637 primitiveGradient(primitives_[primitive_row], x, box,
1638 full_gradient);
1639 } catch (const std::invalid_argument&) {
1640 rows_defined = false;
1641 break;
1642 }
1643 harvestRow(primitives_[primitive_row], full_gradient,
1644 active_of_full, true, sparse);
1645 ++report.wilson_rows_evaluated;
1646 for (int i = 0; i < sparse.nnz; ++i) {
1647 const Eigen::Index column =
1648 sparse.column[static_cast<std::size_t>(i)];
1649 const double value = sparse.value[static_cast<std::size_t>(i)];
1650 if (!wide_step)
1651 entries.emplace_back(row, column, value);
1652 else {
1653 for (int j = 0; j < sparse.nnz; ++j) {
1654 jtj(column,
1655 sparse.column[static_cast<std::size_t>(j)]) +=
1656 value * sparse.value[static_cast<std::size_t>(j)];
1657 }
1658 }
1659 jtr(column) += value * residual(row);
1660 }
1661 }
1662 // An undefined or vanishing primitive gradient is not a retracted
1663 // point, including when an earlier iterate had already been accepted.
1664 if (!rows_defined || !jtr.allFinite()) return fail();
1665 Eigen::VectorXd coefficients;
1666 if (!wide_step) {
1667 SparseWilsonMatrix wilson(residual.size(), n_active);
1668 wilson.setFromTriplets(entries.begin(), entries.end());
1669 SparseWilsonQr qr;
1670 Eigen::Index compact_rows = 0;
1671 Eigen::MatrixXd r_factor;
1672 if (!qrSparseWilson(wilson, qr, compact_rows, r_factor))
1673 return fail();
1674 const Eigen::MatrixXd permuted_basis =
1675 qr.colsPermutation().transpose() * step_basis;
1676 const Eigen::MatrixXd projected_r = r_factor * permuted_basis;
1677 if (!projected_r.allFinite()) return fail();
1678 const double jacobian_norm = projected_r.norm();
1679 if (!(jacobian_norm > 0.0)) return fail();
1680 const Eigen::VectorXd projected = step_basis.transpose() * jtr;
1681 if (!projected.allFinite()) return fail();
1682 // First-order stationarity of the least-squares objective: the
1683 // residual is orthogonal to the allowed correction derivatives
1684 // up to the resolution of the product. A redundant target need
1685 // not have a zero residual at this fixed point.
1686 if (projected.norm() <= eps * jacobian_norm * residual.norm()) {
1687 return finish(best);
1688 }
1689 CompactRFactor reduced;
1690 if (!factorCompactR(projected_r, retractionStepThreshold(),
1691 reduced)) {
1692 return any_accepted ? finish(best) : fail();
1693 }
1694 const Eigen::VectorXd qt = qr.matrixQ().adjoint() * residual;
1695 if (qt.size() < compact_rows ||
1696 !qt.head(compact_rows).allFinite()) {
1697 return any_accepted ? finish(best) : fail();
1698 }
1699 coefficients =
1700 reduced.right * (reduced.inverse_left * qt.head(compact_rows));
1701 } else {
1702 if (!jtj.allFinite()) return fail();
1703 const Eigen::MatrixXd normal =
1704 step_basis.transpose() * jtj * step_basis;
1705 const Eigen::VectorXd projected = step_basis.transpose() * jtr;
1706 double gram_trace = 0.0;
1707 for (Eigen::Index i = 0; i < normal.rows(); ++i)
1708 gram_trace += normal(i, i);
1709 const double jacobian_norm = std::sqrt(std::max(0.0, gram_trace));
1710 if (!normal.allFinite() || !projected.allFinite() ||
1711 !(jacobian_norm > 0.0)) {
1712 return fail();
1713 }
1714 if (projected.norm() <= eps * jacobian_norm * residual.norm()) {
1715 return finish(best);
1716 }
1717 if (!solveDeflatedNormal(normal, projected, coefficients)) {
1718 return any_accepted ? finish(best) : fail();
1719 }
1720 }
1721 const Eigen::VectorXd delta_active = step_basis * coefficients;
1722 if (delta_active.size() != n_active || !delta_active.allFinite() ||
1723 !(delta_active.norm() > 0.0)) {
1724 return any_accepted ? finish(best) : fail();
1725 }
1726 // A correction below the resolution of the interatomic vectors
1727 // does not move the geometry. That is not a retraction unless a
1728 // step was already accepted or the constraint residual itself is
1729 // within tolerance.
1730 const double coordinate_scale =
1731 translationInvariantCoordinateScale(x, primitives_, regular_rows);
1732 const double delta_norm = delta_active.norm();
1733 if (delta_norm <= eps * coordinate_scale) {
1734 if (any_accepted || residual.norm() <= residual_tol)
1735 return finish(best);
1736 return fail();
1737 }
1738 const double residual_before = best_residual;
1739
1740 bool accepted = false;
1741 double alpha = 1.0;
1742 for (int ls = 0; ls < std::numeric_limits<double>::digits; ++ls) {
1743 // Further halving would not change the stored coordinates.
1744 if (alpha * delta_norm <= eps * coordinate_scale) break;
1745 Eigen::VectorXd trial = x;
1746 for (Eigen::Index col = 0; col < delta_active.size(); ++col) {
1747 trial(active_columns_[static_cast<std::size_t>(col)]) +=
1748 alpha * delta_active(col);
1749 }
1750 if (!trial.allFinite()) {
1751 alpha *= 0.5;
1752 continue;
1753 }
1754 Eigen::VectorXd trial_values;
1755 if (!subsetValues(trial, trial_values)) {
1756 alpha *= 0.5;
1757 continue;
1758 }
1759 const double trial_residual = subsetResidual(trial_values).norm();
1760 if (std::isfinite(trial_residual) &&
1761 trial_residual < best_residual) {
1762 x = trial;
1763 best = trial;
1764 best_residual = trial_residual;
1765 current_values = trial_values;
1766 accepted = true;
1767 any_accepted = true;
1768 break;
1769 }
1770 alpha *= 0.5;
1771 }
1772 if (!accepted) {
1773 if (any_accepted || best_residual <= residual_tol)
1774 return finish(best);
1775 return fail();
1776 }
1777 if (best_residual <= residual_tol) return finish(best);
1778 // Stationarity: a decrease below the resolution of the residual is
1779 // the least-squares floor of an inconsistent (redundant) target.
1780 if (residual_before - best_residual <= eps * residual_before) {
1781 return finish(best);
1782 }
1783 }
1784 return any_accepted ? finish(best) : fail();
1785}
@ x
Definition Enums.h:28
Eigen::VectorXd activeStep(const Eigen::VectorXd &full_step) const
Panel finish(Real left, Real right, const Value &k, const Value &g, const Value &absolute)

◆ values()

Eigen::VectorXd gpr::geometry::InternalCoordinateSet::values ( const Eigen::VectorXd & positions) const
nodiscard

Definition at line 923 of file InternalCoordinates.cpp.

925{
926 validatePositions(positions);
927 const PairBox box(cell_);
928 Eigen::VectorXd out(static_cast<Eigen::Index>(primitives_.size()));
929 for (Eigen::Index row = 0;
930 row < static_cast<Eigen::Index>(primitives_.size()); ++row) {
931 out(row) = primitiveValue(primitives_[static_cast<std::size_t>(row)],
932 positions, box);
933 }
934 return out;
935}

The documentation for this class was generated from the following files: