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.
1418{
1419 validatePositions(positions);
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 }
1467 (target < 0.0 || target > kPi))
1468 target_in_domain = false;
1469 } catch (const std::invalid_argument&) {
1470
1471
1472
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
1483
1484
1485
1486
1487 if (!target_in_domain) {
1488 report.positions =
x;
1489 report.residual = std::numeric_limits<double>::infinity();
1491 return report;
1492 }
1493 if (regular_rows.empty()) {
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;
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)) {
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;
1544 return report;
1545 };
1546 auto finish = [&](
const Eigen::VectorXd& retracted) {
1547
1548
1549
1550 for (const std::size_t row: regular_rows) {
1551
1552
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;
1564 return report;
1565 };
1566 if (best_residual <= residual_tol)
return finish(best);
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
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
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
1620
1621
1622
1623
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
1663
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
1683
1684
1685
1686 if (projected.norm() <= eps * jacobian_norm * residual.norm()) {
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()) {
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
1727
1728
1729
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)
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
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) {
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)
1775 return fail();
1776 }
1777 if (best_residual <= residual_tol)
return finish(best);
1778
1779
1780 if (residual_before - best_residual <= eps * residual_before) {
1782 }
1783 }
1784 return any_accepted ?
finish(best) : fail();
1785}
Eigen::VectorXd activeStep(const Eigen::VectorXd &full_step) const
Panel finish(Real left, Real right, const Value &k, const Value &g, const Value &absolute)