Loading...
Searching...
No Matches
gpr::neb::PairSplineModel Class Reference

Pair-additive energy with a cubic B-spline radial function. More...

#include <gpr/neb/PairSplineModel.h>

Public Member Functions

 PairSplineModel (Eigen::Index n_moving, Eigen::VectorXd frozen_positions, const std::array< double, 9 > &cell, double r_min, double r_cut, double spacing)
Eigen::Index nMoving () const noexcept
Eigen::Index nBasis () const noexcept
double cutoff () const noexcept
bool ready () const noexcept
int observations () const noexcept
void setNoise (double energy_sigma, double gradient_sigma)
 Noise standard deviations of the energy and gradient rows.
void setPrior (double weight_sigma, double curvature_sigma)
void setTailSplit (double split, double refresh)
void staleTail (int slot)
 Marks the slot's tail sum for recomputation at the next evaluation.
void observe (const Eigen::VectorXd &x, double energy, const Eigen::VectorXd &gradient, int slot=0)
 Accumulate one measurement into the normal equations.
bool fit ()
 Solve for the weights. Returns false without enough rows.
double gradientResidual () const noexcept
double energy (const Eigen::VectorXd &x, Eigen::VectorXd *gradient, int slot=0) const
double partialEnergy (const Eigen::VectorXd &x, const std::vector< char > &atom_mask, Eigen::VectorXd *gradient, int slot=0) const
double maskedEnergy (const Eigen::VectorXd &x, const std::vector< char > &atom_mask, Eigen::VectorXd *gradient, int slot=0) const
Eigen::MatrixXd hessian (const Eigen::VectorXd &x, const std::vector< Eigen::Index > &dofs, int slot=0) const
 Hessian block over the listed coordinates at x.
void radial (double r, double &phi, double &dphi, double &d2phi) const
 Radial function and its first two derivatives at r.
double minimumSeparation (const Eigen::VectorXd &x) const
 Smallest moving-moving or moving-frozen separation at x.
int listBuilds () const noexcept
double listSeconds () const noexcept

Detailed Description

Pair-additive energy with a cubic B-spline radial function.

E(x) = c + sum over pairs within the cutoff of phi(r), with phi a linear combination of cardinal cubic B-splines on a uniform knot grid. The weights are the posterior mean of a Bayesian linear model fitted to every measured energy and gradient, which makes the model the mean of a Gaussian process with a degenerate kernel. Pairs run over the moving atoms and between moving and frozen atoms under the minimum image convention of the cell; frozen-frozen pairs are a constant.

A Verlet list with a skin keeps the per-call cost at the listed pairs between rebuilds.

Definition at line 31 of file PairSplineModel.h.

Constructor & Destructor Documentation

◆ PairSplineModel()

gpr::neb::PairSplineModel::PairSplineModel ( Eigen::Index n_moving,
Eigen::VectorXd frozen_positions,
const std::array< double, 9 > & cell,
double r_min,
double r_cut,
double spacing )

Definition at line 53 of file PairSplineModel.cpp.

57 : n_moving_(n_moving),
58 frozen_(std::move(frozen_positions)),
59 box_(cell),
60 cell_(cell),
61 r_min_(r_min),
62 r_cut_(r_cut),
63 h_(spacing > 0.0 ? spacing : 0.25)
64{
65 if (!(r_cut_ > r_min_)) r_cut_ = r_min_ + 4.0 * h_;
66 n_basis_ =
67 static_cast<Eigen::Index>(std::floor((r_cut_ - r_min_) / h_)) + 4;
68 const Eigen::Index m = n_basis_ + 1;
69 ete_.setZero(m, m);
70 ety_.setZero(m);
71 gtg_.setZero(n_basis_, n_basis_);
72 gty_.setZero(n_basis_);
73 weights_.setZero(m);
74}

Member Function Documentation

◆ cutoff()

double gpr::neb::PairSplineModel::cutoff ( ) const
inlinenoexcept

Definition at line 45 of file PairSplineModel.h.

46 {
47 return r_cut_;
48 }

◆ energy()

double gpr::neb::PairSplineModel::energy ( const Eigen::VectorXd & x,
Eigen::VectorXd * gradient,
int slot = 0 ) const

Energy and, when requested, the gradient on every moving coordinate. slot names the neighbour list to reuse: callers that evaluate several geometries in turn, such as band images, keep one list each.

Definition at line 733 of file PairSplineModel.cpp.

735{
736 if (gradient) gradient->setZero(x.size());
737 if (!ready_ || x.size() != 3 * n_moving_) return 0.0;
738 List& list = ensureList(x, slot);
739 loadCoordinates(x);
740 const std::size_t n_pairs = list.oi.size();
741 double* gp = gradient ? gradient->data() : nullptr;
742 if (list.n_near >= n_pairs) {
743 double e = weights_(0);
744 sumRange(list, 0, n_pairs, e, gp);
745 return e;
746 }
747 // Tail pairs change slowly: their sum is held as a linear model of the
748 // geometry it was taken at, so the surrogate stays one fixed function
749 // between refreshes.
750 bool stale = list.tail_fit != fit_count_ ||
751 list.tail_positions.size() != x.size() ||
752 list.tail_gradient.size() != x.size();
753 if (!stale && tail_refresh_ > 0.0) {
754 for (Eigen::Index i = 0; i < n_moving_ && !stale; ++i)
755 stale =
756 (x.segment<3>(3 * i) - list.tail_positions.segment<3>(3 * i))
757 .squaredNorm() > tail_refresh_ * tail_refresh_;
758 }
759 if (stale) {
760 list.tail_energy = 0.0;
761 list.tail_gradient.setZero(x.size());
762 sumRange(list, list.n_near, n_pairs, list.tail_energy,
763 list.tail_gradient.data());
764 list.tail_positions = x;
765 list.tail_fit = fit_count_;
766 }
767 double e = weights_(0) + list.tail_energy +
768 list.tail_gradient.dot(x - list.tail_positions);
769 if (gradient) *gradient = list.tail_gradient;
770 sumRange(list, 0, list.n_near, e, gp);
771 return e;
772}
@ x
Definition Enums.h:28

◆ fit()

bool gpr::neb::PairSplineModel::fit ( )

Solve for the weights. Returns false without enough rows.

Definition at line 508 of file PairSplineModel.cpp.

509{
510 if (n_obs_ == 0) return false;
511 const Eigen::Index m = n_basis_ + 1;
512 const double we = 1.0 / (noise_energy_ * noise_energy_);
513 const double wg = 1.0 / (noise_gradient_ * noise_gradient_);
514 Eigen::MatrixXd A = we * ete_;
515 const Eigen::MatrixXd gtg = gtg_.selfadjointView<Eigen::Lower>();
516 A.block(1, 1, n_basis_, n_basis_) += wg * gtg;
517 Eigen::VectorXd rhs = we * ety_;
518 rhs.tail(n_basis_) += wg * gty_;
519 // Weight prior and a second-difference curvature prior along the knots.
520 const double pw = 1.0 / (prior_weight_ * prior_weight_);
521 const double pc = 1.0 / (prior_curvature_ * prior_curvature_);
522 for (Eigen::Index k = 1; k < m; ++k)
523 A(k, k) += pw;
524 for (Eigen::Index k = 1; k + 2 < m; ++k) {
525 Eigen::Vector3d d(1.0, -2.0, 1.0);
526 A.block<3, 3>(k, k) += pc * (d * d.transpose());
527 }
528 A(0, 0) += 1e-12;
529 Eigen::LDLT<Eigen::MatrixXd> ldlt(A);
530 if (ldlt.info() != Eigen::Success) return false;
531 weights_ = ldlt.solve(rhs);
532 if (!weights_.allFinite()) return false;
533 ready_ = true;
534 ++fit_count_;
535 tabulate();
536 const Eigen::VectorXd w = weights_.tail(n_basis_);
537 const double ss = sum_g2_ - 2.0 * w.dot(gty_) + w.dot(gtg * w);
538 gradient_residual_ =
539 n_gradient_rows_ > 0 && ss > 0.0
540 ? std::sqrt(ss / static_cast<double>(n_gradient_rows_))
541 : 0.0;
542 return true;
543}

◆ gradientResidual()

double gpr::neb::PairSplineModel::gradientResidual ( ) const
inlinenoexcept

Root-mean-square gradient residual of the retained measurements after the last fit (the fit quality on the force rows).

Definition at line 78 of file PairSplineModel.h.

79 {
80 return gradient_residual_;
81 }

◆ hessian()

Eigen::MatrixXd gpr::neb::PairSplineModel::hessian ( const Eigen::VectorXd & x,
const std::vector< Eigen::Index > & dofs,
int slot = 0 ) const

Hessian block over the listed coordinates at x.

Definition at line 812 of file PairSplineModel.cpp.

815{
816 const Eigen::Index n = static_cast<Eigen::Index>(dofs.size());
817 Eigen::MatrixXd H = Eigen::MatrixXd::Zero(n, n);
818 if (!ready_ || x.size() != 3 * n_moving_) return H;
819 const auto& list_ = ensureList(x, slot).pairs;
820 // Atom -> first row of its block in H, or -1 when absent. Only whole
821 // atoms are supported; a partial atom contributes its present rows.
822 std::vector<Eigen::Index> row_of(static_cast<std::size_t>(x.size()), -1);
823 for (Eigen::Index k = 0; k < n; ++k)
824 row_of[static_cast<std::size_t>(dofs[k])] = k;
825 Eigen::Vector3d d;
826 double r;
827 for (const auto& p: list_) {
828 if (!separation(p, x, d, r)) continue;
829 double phi, dphi, d2phi;
830 radial(r, phi, dphi, d2phi);
831 const Eigen::Vector3d nvec = d / r;
832 const Eigen::Matrix3d block =
833 d2phi * (nvec * nvec.transpose()) +
834 (dphi / r) *
835 (Eigen::Matrix3d::Identity() - nvec * nvec.transpose());
836 const auto add = [&](int ai, int aj, double sign) {
837 for (int a = 0; a < 3; ++a) {
838 const Eigen::Index ra =
839 row_of[static_cast<std::size_t>(3 * ai + a)];
840 if (ra < 0) continue;
841 for (int c = 0; c < 3; ++c) {
842 const Eigen::Index rc =
843 row_of[static_cast<std::size_t>(3 * aj + c)];
844 if (rc < 0) continue;
845 H(ra, rc) += sign * block(a, c);
846 }
847 }
848 };
849 add(p.i, p.i, 1.0);
850 if (!p.frozen) {
851 add(p.j, p.j, 1.0);
852 add(p.i, p.j, -1.0);
853 add(p.j, p.i, -1.0);
854 }
855 }
856 return H;
857}
void radial(double r, double &phi, double &dphi, double &d2phi) const
Radial function and its first two derivatives at r.

◆ listBuilds()

int gpr::neb::PairSplineModel::listBuilds ( ) const
inlinenoexcept

Definition at line 180 of file PairSplineModel.h.

181 {
182 return list_builds_;
183 }

◆ listSeconds()

double gpr::neb::PairSplineModel::listSeconds ( ) const
inlinenoexcept

Definition at line 184 of file PairSplineModel.h.

185 {
186 return list_seconds_;
187 }

◆ maskedEnergy()

double gpr::neb::PairSplineModel::maskedEnergy ( const Eigen::VectorXd & x,
const std::vector< char > & atom_mask,
Eigen::VectorXd * gradient,
int slot = 0 ) const

The same sum over the slot's flat arrays, from an index list of the pairs that touch a masked atom kept with the slot while the mask and the list stand; the gradient lands on every atom those pairs touch.

Definition at line 706 of file PairSplineModel.cpp.

709{
710 if (gradient) gradient->setZero(x.size());
711 if (!ready_ || x.size() != 3 * n_moving_ ||
712 atom_mask.size() != static_cast<std::size_t>(n_moving_))
713 return 0.0;
714 List& list = ensureList(x, slot);
715 loadCoordinates(x);
716 if (list.masked_atoms != atom_mask) {
717 const int nm3 = static_cast<int>(3 * n_moving_);
718 list.masked.clear();
719 for (int k = 0; k < static_cast<int>(list.oi.size()); ++k) {
720 const int a = list.oi[static_cast<std::size_t>(k)] / 3;
721 const int b = list.oj[static_cast<std::size_t>(k)];
722 if (atom_mask[static_cast<std::size_t>(a)] ||
723 (b < nm3 && atom_mask[static_cast<std::size_t>(b / 3)]))
724 list.masked.push_back(k);
725 }
726 list.masked_atoms = atom_mask;
727 }
728 double e = 0.0;
729 sumIndexed(list, list.masked, e, gradient ? gradient->data() : nullptr);
730 return e;
731}

◆ minimumSeparation()

double gpr::neb::PairSplineModel::minimumSeparation ( const Eigen::VectorXd & x) const

Smallest moving-moving or moving-frozen separation at x.

Definition at line 76 of file PairSplineModel.cpp.

77{
78 double best = std::numeric_limits<double>::infinity();
79 if (x.size() != 3 * n_moving_) return best;
80 const Eigen::Index n_frozen = frozen_.size() / 3;
81 const double* xp = x.data();
82 const double* fp = frozen_.data();
83 auto dist2 = [&](const double* a, const double* b) {
84 return box_.dist2(Eigen::Vector3d(a[0], a[1], a[2]),
85 Eigen::Vector3d(b[0], b[1], b[2]));
86 };
87 for (Eigen::Index i = 0; i < n_moving_; ++i) {
88 for (Eigen::Index j = i + 1; j < n_moving_; ++j)
89 best = std::min(best, dist2(xp + 3 * i, xp + 3 * j));
90 for (Eigen::Index j = 0; j < n_frozen; ++j)
91 best = std::min(best, dist2(xp + 3 * i, fp + 3 * j));
92 }
93 return std::sqrt(best);
94}

◆ nBasis()

Eigen::Index gpr::neb::PairSplineModel::nBasis ( ) const
inlinenoexcept

Definition at line 41 of file PairSplineModel.h.

42 {
43 return n_basis_;
44 }

◆ nMoving()

Eigen::Index gpr::neb::PairSplineModel::nMoving ( ) const
inlinenoexcept

Definition at line 37 of file PairSplineModel.h.

38 {
39 return n_moving_;
40 }

◆ observations()

int gpr::neb::PairSplineModel::observations ( ) const
inlinenoexcept

Definition at line 53 of file PairSplineModel.h.

54 {
55 return n_obs_;
56 }

◆ observe()

void gpr::neb::PairSplineModel::observe ( const Eigen::VectorXd & x,
double energy,
const Eigen::VectorXd & gradient,
int slot = 0 )

Accumulate one measurement into the normal equations.

Definition at line 484 of file PairSplineModel.cpp.

486{
487 if (x.size() != 3 * n_moving_ || gradient.size() != x.size()) return;
488 if (!std::isfinite(energy) || !x.allFinite() || !gradient.allFinite())
489 return;
490 Eigen::VectorXd phi;
491 Eigen::MatrixXd dphi;
492 features(x, phi, dphi, slot);
493 const Eigen::Index m = n_basis_ + 1;
494 // Energy row: [1, phi].
495 Eigen::VectorXd row(m);
496 row(0) = 1.0;
497 row.tail(n_basis_) = phi;
498 ete_ += row * row.transpose();
499 ety_ += energy * row;
500 // Gradient rows: [0, dphi_i].
501 gtg_.selfadjointView<Eigen::Lower>().rankUpdate(dphi.transpose());
502 gty_ += dphi.transpose() * gradient;
503 sum_g2_ += gradient.squaredNorm();
504 n_gradient_rows_ += gradient.size();
505 ++n_obs_;
506}
double energy(const Eigen::VectorXd &x, Eigen::VectorXd *gradient, int slot=0) const

◆ partialEnergy()

double gpr::neb::PairSplineModel::partialEnergy ( const Eigen::VectorXd & x,
const std::vector< char > & atom_mask,
Eigen::VectorXd * gradient,
int slot = 0 ) const

Energy restricted to the pairs that involve one of atoms, with the gradient on those atoms' coordinates written into gradient at the atoms' flat positions. The omitted pairs are constant while the other atoms stay put.

Definition at line 774 of file PairSplineModel.cpp.

777{
778 if (gradient && gradient->size() != x.size()) gradient->setZero(x.size());
779 if (!ready_ || x.size() != 3 * n_moving_ ||
780 atom_mask.size() != static_cast<std::size_t>(n_moving_))
781 return 0.0;
782 const auto& list_ = ensureList(x, slot).pairs;
783 double e = 0.0;
784 Eigen::Vector3d d;
785 double r;
786 int first;
787 double b[4], db[4], d2b[4];
788 for (const auto& p: list_) {
789 const bool mi = atom_mask[static_cast<std::size_t>(p.i)] != 0;
790 const bool mj =
791 !p.frozen && atom_mask[static_cast<std::size_t>(p.j)] != 0;
792 if (!mi && !mj) continue;
793 if (!separation(p, x, d, r)) continue;
794 basis(r, first, b, db, d2b);
795 double phi = 0.0, dphi = 0.0;
796 for (int k = 0; k < 4; ++k) {
797 const int index = first + k;
798 if (index < 0 || index >= n_basis_) continue;
799 phi += weights_(1 + index) * b[k];
800 dphi += weights_(1 + index) * db[k];
801 }
802 e += phi;
803 if (gradient) {
804 const Eigen::Vector3d g = (dphi / r) * d;
805 if (mi) gradient->segment<3>(3 * p.i) += g;
806 if (mj) gradient->segment<3>(3 * p.j) -= g;
807 }
808 }
809 return e;
810}

◆ radial()

void gpr::neb::PairSplineModel::radial ( double r,
double & phi,
double & dphi,
double & d2phi ) const

Radial function and its first two derivatives at r.

Definition at line 172 of file PairSplineModel.cpp.

174{
175 phi = dphi = d2phi = 0.0;
176 if (!ready_ || r < r_min_ || r >= r_cut_) return;
177 (void)lookup(r, phi, dphi, d2phi);
178}

◆ ready()

bool gpr::neb::PairSplineModel::ready ( ) const
inlinenoexcept

Definition at line 49 of file PairSplineModel.h.

50 {
51 return ready_;
52 }

◆ setNoise()

void gpr::neb::PairSplineModel::setNoise ( double energy_sigma,
double gradient_sigma )

Noise standard deviations of the energy and gradient rows.

Definition at line 96 of file PairSplineModel.cpp.

97{
98 noise_energy_ = std::max(energy_sigma, 1e-8);
99 noise_gradient_ = std::max(gradient_sigma, 1e-8);
100}

◆ setPrior()

void gpr::neb::PairSplineModel::setPrior ( double weight_sigma,
double curvature_sigma )

Prior standard deviation of the weights and of their second differences along the knot grid.

Definition at line 102 of file PairSplineModel.cpp.

103{
104 prior_weight_ = std::max(weight_sigma, 1e-8);
105 prior_curvature_ = std::max(curvature_sigma, 1e-8);
106}

◆ setTailSplit()

void gpr::neb::PairSplineModel::setTailSplit ( double split,
double refresh )

Pairs beyond split are summed once per slot and held as a linear model until staleTail(), a list rebuild, a refit, or (when refresh is positive) an atom moving farther than refresh from that sum. A split of 0 evaluates every pair on every call.

Definition at line 108 of file PairSplineModel.cpp.

109{
110 split_ = (split > r_min_ && split + skin_ < r_cut_) ? split : 0.0;
111 tail_refresh_ = std::max(refresh, 0.0);
112 for (auto& list: lists_)
113 list.positions.resize(0);
114}

◆ staleTail()

void gpr::neb::PairSplineModel::staleTail ( int slot)

Marks the slot's tail sum for recomputation at the next evaluation.

Definition at line 116 of file PairSplineModel.cpp.

117{
118 if (slot >= 0 && static_cast<std::size_t>(slot) < lists_.size())
119 lists_[static_cast<std::size_t>(slot)].tail_fit = -1;
120}

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