Loading...
Searching...
No Matches
eonc::LBFGS Class Referencefinal

#include <LBFGS.h>

Inheritance diagram for eonc::LBFGS:

Public Member Functions

 LBFGS (std::shared_ptr< ObjectiveFunction > a_objf, const Parameters &a_params)
 ~LBFGS ()=default
int step (double a_maxMove) override
int run (size_t a_maxIterations, double a_maxMove) override
int update (const Eigen::VectorXd &a_r1, const Eigen::VectorXd &a_r0, const Eigen::VectorXd &a_f1, const Eigen::VectorXd &a_f0, double a_e1, double a_e0)
void reset ()
Public Member Functions inherited from eonc::Optimizer
 Optimizer (std::shared_ptr< ObjectiveFunction > a_objf, const OptimizerConfig &a_config)
 Optimizer (std::shared_ptr< ObjectiveFunction > a_objf, OptType a_optype, const OptimizerConfig &a_config)
 Optimizer (std::shared_ptr< ObjectiveFunction > a_objf, const Parameters &a_params)
 Optimizer (std::shared_ptr< ObjectiveFunction > a_objf, OptType a_optype, const Parameters &a_params)
virtual ~Optimizer ()=default
virtual void onTranslationPulledBack ()

Private Member Functions

Eigen::VectorXd getStep (double a_maxMove, const Eigen::VectorXd &a_f)
Eigen::Vector3d micRij (const Eigen::VectorXd &pos, int i, int j) const
bool usesPrecon () const
Eigen::MatrixXd buildPrecon (const Eigen::VectorXd &pos) const
Eigen::VectorXd applyH0 (const Eigen::VectorXd &q, double H0, const Eigen::VectorXd &pos) const
Eigen::VectorXd hessianStep (double a_maxMove, const Eigen::VectorXd &a_f)

Private Attributes

int m_iteration {0}
int m_memory {0}
std::deque< Eigen::VectorXd > m_s
std::deque< Eigen::VectorXd > m_y
std::deque< double > m_rho
std::deque< double > m_eHist
Eigen::VectorXd m_rPrev
Eigen::VectorXd m_fPrev
double m_ePrev {0.0}
eonc::log::FileScoped m_log {"lbfgs", "_lbfgs.log"}

Additional Inherited Members

Protected Attributes inherited from eonc::Optimizer
const OptimizerConfig m_optConfig
std::shared_ptr< ObjectiveFunction > m_objf

Detailed Description

Definition at line 25 of file LBFGS.h.

Constructor & Destructor Documentation

◆ LBFGS()

eonc::LBFGS::LBFGS ( std::shared_ptr< ObjectiveFunction > a_objf,
const Parameters & a_params )
inline

Definition at line 28 of file LBFGS.h.

29 : Optimizer(a_objf, OptType::LBFGS,
31 m_memory{std::min(
32 a_objf->degreesOfFreedom(),
33 static_cast<int>(a_params.optimizer_options().lbfgs.memory))},
34 m_rPrev{Eigen::VectorXd::Zero(a_objf->degreesOfFreedom())},
35 m_fPrev{Eigen::VectorXd::Zero(a_objf->degreesOfFreedom())} {}
const optimizer_options_t & optimizer_options() const
Eigen::VectorXd m_fPrev
Definition LBFGS.h:69
Eigen::VectorXd m_rPrev
Definition LBFGS.h:68
int m_memory
Definition LBFGS.h:61
Optimizer(std::shared_ptr< ObjectiveFunction > a_objf, const OptimizerConfig &a_config)
Definition Optimizer.h:77
static OptimizerConfig fromParams(const Parameters &p)
Definition Optimizer.h:60
struct eonc::optimizer_options_t::lbfgs_t lbfgs

◆ ~LBFGS()

eonc::LBFGS::~LBFGS ( )
default

Member Function Documentation

◆ applyH0()

Eigen::VectorXd eonc::LBFGS::applyH0 ( const Eigen::VectorXd & q,
double H0,
const Eigen::VectorXd & pos ) const
nodiscardprivate

Definition at line 101 of file LBFGS.cpp.

102 {
103 const auto &cfg = m_optConfig.opts.lbfgs;
104 if (usesPrecon() && pos.size() >= 6 && pos.size() % 3 == 0) {
105 const Eigen::MatrixXd P = buildPrecon(pos);
106 // PartialPivLU, not LDLT: manylinux Eigen + C++20 rewrites
107 // Array==Scalar in LDLT::unblocked as a non-bool comparison.
108 Eigen::PartialPivLU<Eigen::MatrixXd> lu(P);
109 const Eigen::VectorXd z = lu.solve(q);
110 if (z.allFinite()) {
111 return z;
112 }
113 QUILL_LOG_DEBUG(m_log, "[LBFGS] Packwood P failed to factor, using H0 I");
114 }
115 return H0 * q;
116}
bool usesPrecon() const
Definition LBFGS.cpp:88
eonc::log::FileScoped m_log
Definition LBFGS.h:71
Eigen::MatrixXd buildPrecon(const Eigen::VectorXd &pos) const
Definition LBFGS.cpp:95
const OptimizerConfig m_optConfig
Definition Optimizer.h:73

◆ buildPrecon()

Eigen::MatrixXd eonc::LBFGS::buildPrecon ( const Eigen::VectorXd & pos) const
nodiscardprivate

Definition at line 95 of file LBFGS.cpp.

95 {
96 const auto &cfg = m_optConfig.opts.lbfgs;
97 return pairhess::build(pos, cfg.precon, m_optConfig.potential, cfg.precon_A,
98 cfg.precon_mu, cfg.precon_rcut, *m_objf);
99}
std::shared_ptr< ObjectiveFunction > m_objf
Definition Optimizer.h:74
Eigen::MatrixXd build(const Eigen::VectorXd &pos, const std::string &kind, PotType pot, double A, double mu, double rcut_in, ObjectiveFunction &objf)
Analytic pair or model Hessian.

◆ getStep()

Eigen::VectorXd eonc::LBFGS::getStep ( double a_maxMove,
const Eigen::VectorXd & a_f )
nodiscardprivate

Definition at line 164 of file LBFGS.cpp.

164 {
165 const std::string &step = m_optConfig.opts.lbfgs.step;
166 if (step == "newton" || step == "rfo") {
167 return hessianStep(a_maxMove, a_f);
168 }
169 double H0 = m_optConfig.opts.lbfgs.inverse_curvature;
170 Eigen::VectorXd r = m_objf->getPositions();
171 Eigen::VectorXd f = a_f;
172 maybeProjectRigid(f, r, m_optConfig.opts.lbfgs.project_rigid);
173
174 if (m_iteration > 0) {
175 Eigen::VectorXd dr = m_objf->difference(r, m_rPrev);
176 const Eigen::VectorXd y = m_fPrev - f;
177 const double sy = eonc::exact::dot(dr, y);
178 const double yy = eonc::exact::squaredNorm(y);
179 const double ss = eonc::exact::squaredNorm(dr);
180 const double C = eonc::safemath::safe_div(yy, sy, -1.0);
181 if (C < 0) {
182 QUILL_LOG_DEBUG(
183 m_log, "[LBFGS] Negative curvature: {:.4f} eV/A^2 take max move step",
184 C);
185 if (m_optConfig.opts.lbfgs.curvature == "reset") {
186 reset();
187 }
188 return eonc::geometry::maxAtomMotionAppliedV(1000 * f, a_maxMove);
189 }
190
191 if (m_optConfig.opts.lbfgs.auto_scale && sy > 0.0 && yy > 0.0) {
192 const double g_syyy = sy / yy;
193 const double g_sssy = ss / sy;
194 const std::string &h0 = m_optConfig.opts.lbfgs.h0;
195 if (h0 == "ss_sy") {
196 H0 = g_sssy;
197 } else if (h0 == "adaptive") {
198 H0 = std::min(g_syyy, g_sssy);
199 } else {
200 H0 = g_syyy;
201 }
202 if (m_optConfig.opts.lbfgs.max_inverse_curvature > 0.0) {
203 H0 = std::min(H0, m_optConfig.opts.lbfgs.max_inverse_curvature);
204 }
205 QUILL_LOG_DEBUG(m_log, "[LBFGS] H0={:.4e} ({} scale)", H0, h0);
206 }
207 }
208
209 const std::optional<double> known =
210 m_iteration == 0 && m_optConfig.opts.lbfgs.auto_scale
211 ? m_objf->knownCurvature()
212 : std::nullopt;
213 // A reported curvature is the classic dimer's |C_tau|. The translated
214 // force on a negative mode is F - 2 (F·tau) tau. When |C_tau| is steep
215 // and H0 = 1/|C_tau| already asks for more than max_move, the cap
216 // chooses the length and the first step leaves the basin r2549 finds
217 // with one finite-difference probe of F_eff. A milder mode keeps the
218 // capped |C_tau| step. Improved dimer and Lanczos report nothing, so
219 // they take that probe on every first step.
220 constexpr double kSteepMode = 30.0;
221 bool knownFits = false;
222 if (known && std::isfinite(*known) && *known > 0.0) {
223 double hKnown = 1.0 / *known;
224 if (m_optConfig.opts.lbfgs.max_inverse_curvature > 0.0) {
225 hKnown = std::min(hKnown, m_optConfig.opts.lbfgs.max_inverse_curvature);
226 }
227 const double knownMotion = eonc::geometry::maxAtomMotionV(hKnown * f);
228 if (knownMotion < a_maxMove || *known < kSteepMode) {
229 H0 = hKnown;
230 knownFits = true;
231 QUILL_LOG_DEBUG(m_log, "[LBFGS] H0={:.4e} (known curvature)", H0);
232 }
233 }
234 if (!knownFits && m_iteration == 0 && m_optConfig.opts.lbfgs.auto_scale &&
235 m_objf->supportsFiniteDifferenceCurvature()) {
236 const Eigen::VectorXd fhat = eonc::exact::normalized(a_f);
237 m_objf->setPositions(mulAdd(r, m_optConfig.finiteDifference, fhat));
238 Eigen::VectorXd dg = m_objf->getGradient(true) + a_f;
239 const double C = eonc::exact::dot(dg, fhat) / m_optConfig.finiteDifference;
240 H0 = eonc::safemath::safe_recip(C, -1.0);
241 m_objf->setPositions(r);
242 if (H0 < 0) {
243 QUILL_LOG_WARNING(m_log,
244 "[LBFGS] Negative curvature calculated via FD: {:.4e} "
245 "eV/A^2, take max move step",
246 C);
247 reset();
248 return eonc::geometry::maxAtomMotionAppliedV(1000 * a_f, a_maxMove);
249 }
250 QUILL_LOG_DEBUG(m_log, "[LBFGS] Curvature calculated via FD: {:.4e} eV/A^2",
251 C);
252 }
253
254 const auto idx =
255 twoLoopIndex(static_cast<int>(m_s.size()),
256 static_cast<int>(m_optConfig.opts.lbfgs.extra_updates));
257 const int loopmax = static_cast<int>(idx.size());
258 std::vector<double> a(static_cast<size_t>(loopmax));
259
260 Eigen::VectorXd q = -f;
261
262 for (int k = loopmax - 1; k >= 0; k--) {
263 const int i = idx[static_cast<size_t>(k)];
264 a[static_cast<size_t>(k)] =
265 m_rho[static_cast<size_t>(i)] *
266 eonc::exact::orderedDot(m_s[static_cast<size_t>(i)], q);
267 mulAddInto(q, -a[static_cast<size_t>(k)], m_y[static_cast<size_t>(i)]);
268 }
269
270 Eigen::VectorXd z = applyH0(q, H0, r);
271
272 for (int k = 0; k < loopmax; k++) {
273 const int i = idx[static_cast<size_t>(k)];
274 const double b = m_rho[static_cast<size_t>(i)] *
275 eonc::exact::orderedDot(m_y[static_cast<size_t>(i)], z);
276 mulAddInto(z, a[static_cast<size_t>(k)] - b, m_s[static_cast<size_t>(i)]);
277 }
278
279 Eigen::VectorXd d = -z;
280
281 double distance = eonc::geometry::maxAtomMotionV(d);
282 if (distance >= a_maxMove && m_optConfig.opts.lbfgs.distance_reset) {
283 QUILL_LOG_DEBUG(m_log,
284 "[LBFGS] reset memory, proposed step too large: {:.4f}",
285 distance);
286 reset();
287 return eonc::geometry::maxAtomMotionAppliedV(H0 * f, a_maxMove);
288 }
289
290 double vd =
292 if (vd > 1.0)
293 vd = 1.0;
294 if (vd < -1.0)
295 vd = -1.0;
296 double angle = eonc::safemath::safe_acos(vd) * (180.0 / eonc::helpers::pi);
297 if (angle > 90.0 && m_optConfig.opts.lbfgs.angle_reset) {
298 QUILL_LOG_DEBUG(m_log,
299 "[LBFGS] reset memory, angle between LBFGS angle and "
300 "force too large: {:.4f}",
301 angle);
302 reset();
303 return eonc::geometry::maxAtomMotionAppliedV(H0 * f, a_maxMove);
304 }
305
306 maybeProjectRigid(d, r, m_optConfig.opts.lbfgs.project_rigid);
307 return eonc::geometry::maxAtomMotionAppliedV(d, a_maxMove);
308}
std::deque< double > m_rho
Definition LBFGS.h:65
int m_iteration
Definition LBFGS.h:60
std::deque< Eigen::VectorXd > m_y
Definition LBFGS.h:64
std::deque< Eigen::VectorXd > m_s
Definition LBFGS.h:63
int step(double a_maxMove) override
Definition LBFGS.cpp:411
Eigen::VectorXd hessianStep(double a_maxMove, const Eigen::VectorXd &a_f)
Definition LBFGS.cpp:118
void reset()
Definition LBFGS.cpp:310
Eigen::VectorXd applyH0(const Eigen::VectorXd &q, double H0, const Eigen::VectorXd &pos) const
Definition LBFGS.cpp:101
double squaredNorm(const Eigen::MatrixBase< A > &x)
A::PlainObject normalized(const Eigen::MatrixBase< A > &x, double min_norm=1e-300)
x / norm(x), or zero below min_norm (the safemath convention).
double dot(const double *x, const double *y, std::size_t n)
Round-to-nearest-even value of the exact sum of x[i] * y[i].
double orderedDot(const double *x, const double *y, std::size_t n)
A fixed summation tree: four lanes (term i goes to lane i mod 4, each lane a left-to-right sum of rou...
VectorXd maxAtomMotionAppliedV(const VectorXd v1, double maxMotion)
double maxAtomMotionV(const VectorXd v1)
constexpr double pi
constexpr double safe_div(double num, double denom, double fallback=0.0)
Definition SafeMath.h:21
double safe_acos(double x)
Definition SafeMath.h:34
constexpr double safe_recip(double x, double fallback=0.0)
Definition SafeMath.h:29

◆ hessianStep()

Eigen::VectorXd eonc::LBFGS::hessianStep ( double a_maxMove,
const Eigen::VectorXd & a_f )
nodiscardprivate

Definition at line 118 of file LBFGS.cpp.

119 {
120 Eigen::VectorXd r = m_objf->getPositions();
121 Eigen::VectorXd f = a_f;
122 if (!usesPrecon()) {
124 m_optConfig.opts.lbfgs.inverse_curvature * f, a_maxMove);
125 }
126 maybeProjectRigid(f, r, m_optConfig.opts.lbfgs.project_rigid);
127 const Eigen::VectorXd g = -f;
128 Eigen::MatrixXd H = buildPrecon(r);
129 Eigen::VectorXd d;
130 if (m_optConfig.opts.lbfgs.step == "rfo") {
131 // Banerjee, Adams, Simons, Shepard, J. Phys. Chem. 1985.
132 // Baker, J. Comput. Chem. 1986. Lowest mode of the augmented Hessian.
133 const int n = static_cast<int>(H.rows());
134 Eigen::MatrixXd A = Eigen::MatrixXd::Zero(n + 1, n + 1);
135 A.topLeftCorner(n, n) = H;
136 A.col(n).head(n) = g;
137 A.row(n).head(n) = g.transpose();
138 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(A);
139 if (es.info() != Eigen::Success) {
140 return eonc::geometry::maxAtomMotionAppliedV(f, a_maxMove);
141 }
142 const Eigen::VectorXd v = es.eigenvectors().col(0);
143 if (std::abs(v(n)) < 1.0e-14) {
144 return eonc::geometry::maxAtomMotionAppliedV(f, a_maxMove);
145 }
146 d = v.head(n) / v(n);
147 } else {
148 // Regularized Newton: H + mu I with mu = max(0, eps - lambda_min).
149 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(H);
150 if (es.info() != Eigen::Success) {
151 return eonc::geometry::maxAtomMotionAppliedV(f, a_maxMove);
152 }
153 Eigen::VectorXd ev = es.eigenvalues();
154 const double lmin = ev.minCoeff();
155 const double mu = (lmin < 1.0e-8) ? (1.0e-8 - lmin) : 0.0;
156 ev.array() += mu;
157 d = -es.eigenvectors() *
158 (es.eigenvectors().transpose() * g).cwiseQuotient(ev);
159 }
160 maybeProjectRigid(d, r, m_optConfig.opts.lbfgs.project_rigid);
161 return eonc::geometry::maxAtomMotionAppliedV(d, a_maxMove);
162}

◆ micRij()

Eigen::Vector3d eonc::LBFGS::micRij ( const Eigen::VectorXd & pos,
int i,
int j ) const
nodiscardprivate

Definition at line 82 of file LBFGS.cpp.

82 {
83 Eigen::Vector3d dr = pos.segment<3>(3 * i) - pos.segment<3>(3 * j);
84 m_objf->minimumImage(dr);
85 return dr;
86}

◆ reset()

void eonc::LBFGS::reset ( )

Definition at line 310 of file LBFGS.cpp.

310 {
311 m_s.clear();
312 m_y.clear();
313 m_rho.clear();
314 m_eHist.clear();
315}
std::deque< double > m_eHist
Definition LBFGS.h:66

◆ run()

int eonc::LBFGS::run ( size_t a_maxIterations,
double a_maxMove )
nodiscardoverridevirtual

Implements eonc::Optimizer.

Definition at line 481 of file LBFGS.cpp.

481 {
482 int status;
483 while (!m_objf->isConverged() && m_iteration < a_maxSteps) {
484 status = step(a_maxMove);
485 if (status < 0)
486 return -1;
487 }
488 return m_objf->isConverged() ? 1 : 0;
489}

◆ step()

int eonc::LBFGS::step ( double a_maxMove)
nodiscardoverridevirtual

Implements eonc::Optimizer.

Definition at line 411 of file LBFGS.cpp.

411 {
412 int status = 0;
413 Eigen::VectorXd r = m_objf->getPositions();
414 Eigen::VectorXd f = -m_objf->getGradient();
415 const std::string &accept = m_optConfig.opts.lbfgs.accept;
416 const bool energyTests = m_objf->gradientMatchesEnergy();
417 const bool energyRequested = accept == "energy" || accept == "nonmonotone" ||
418 m_optConfig.opts.lbfgs.secant == "zhangxu";
419 if (energyRequested && !energyTests && m_iteration == 0) {
420 EONC_LOG_WARNING("[LBFGS] energy tests off (lbfgs_accept = {}, "
421 "lbfgs_secant = {}): a band or saddle objective's "
422 "gradient is not its energy gradient",
423 accept, m_optConfig.opts.lbfgs.secant);
424 }
425 const bool energyAccept =
426 energyTests && (accept == "energy" || accept == "nonmonotone");
427 const bool needE =
428 energyAccept ||
429 (energyTests && m_optConfig.opts.lbfgs.secant == "zhangxu");
430 const double e0 = needE ? m_objf->getEnergy() : 0.0;
431
432 if (m_iteration > 0) {
433 status = update(r, m_rPrev, f, m_fPrev, e0, m_ePrev);
434 }
435 if (status < 0)
436 return -1;
437
438 Eigen::VectorXd dr = getStep(a_maxMove, f);
439 if (energyAccept) {
440 constexpr double max_erise = 1.0e-8;
441 double ref = e0;
442 if (accept == "nonmonotone" && !m_eHist.empty()) {
443 ref = *std::max_element(m_eHist.begin(), m_eHist.end());
444 }
445 double alpha = 1.0;
446 bool accepted = false;
447 double e_acc = e0;
448 for (int dec = 0; dec < 10; ++dec) {
449 m_objf->setPositions(mulAdd(r, alpha, dr));
450 e_acc = m_objf->getEnergy();
451 if (e_acc - ref <= max_erise) {
452 accepted = true;
453 break;
454 }
455 alpha *= 0.5;
456 }
457 if (!accepted) {
458 m_objf->setPositions(r);
459 reset();
460 m_objf->setPositions(
461 r + eonc::geometry::maxAtomMotionAppliedV(0.1 * f, a_maxMove));
462 e_acc = m_objf->getEnergy();
463 }
464 m_eHist.push_back(e_acc);
465 if (m_eHist.size() > 5) {
466 m_eHist.pop_front();
467 }
468 } else {
469 m_objf->setPositions(r + dr);
470 }
471
472 m_rPrev = r;
473 m_fPrev = f;
474 m_ePrev = e0;
475
476 m_iteration++;
477
478 return m_objf->isConverged() ? 1 : 0;
479}
#define EONC_LOG_WARNING(...)
Definition EonLogger.h:322
int update(const Eigen::VectorXd &a_r1, const Eigen::VectorXd &a_r0, const Eigen::VectorXd &a_f1, const Eigen::VectorXd &a_f0, double a_e1, double a_e0)
Definition LBFGS.cpp:317
double m_ePrev
Definition LBFGS.h:70
Eigen::VectorXd getStep(double a_maxMove, const Eigen::VectorXd &a_f)
Definition LBFGS.cpp:164

◆ update()

int eonc::LBFGS::update ( const Eigen::VectorXd & a_r1,
const Eigen::VectorXd & a_r0,
const Eigen::VectorXd & a_f1,
const Eigen::VectorXd & a_f0,
double a_e1,
double a_e0 )
nodiscard

Definition at line 317 of file LBFGS.cpp.

319 {
320 Eigen::VectorXd s0 = m_objf->difference(a_r1, a_r0);
321
322 // y0 is the change in the gradient, not the force
323 Eigen::VectorXd y0 = a_f0 - a_f1;
324 double sy = eonc::exact::dot(s0, y0);
325 const auto &cfg = m_optConfig.opts.lbfgs;
326 const std::string &curv = cfg.curvature;
327 const double H0 = std::max(cfg.inverse_curvature, 1.0e-16);
328 const double ss = eonc::exact::squaredNorm(s0);
329
330 if (cfg.secant == "zhangxu" && m_objf->gradientMatchesEnergy() &&
331 ss > kLbfgsEps) {
332 // Zhang, Deng, Chen, JOTA 102, 147 (1999); Zhang and Xu, JOTA 2001.
333 // t = 6(f_k - f_{k+1}) + 3(g_k + g_{k+1})·s, ŷ = y + (t - y·s)/||s||^2 s.
334 // Forces f = -g, so (g_k + g_{k+1})·s = -(f0 + f1)·s.
335 const double t =
336 6.0 * (a_e0 - a_e1) - 3.0 * eonc::exact::dot(a_f0 + a_f1, s0);
337 const double theta = t - sy;
338 mulAddInto(y0, theta / ss, s0);
339 sy = eonc::exact::dot(s0, y0);
340 QUILL_LOG_DEBUG(m_log, "[LBFGS] Zhang-Xu θ={:.4e} s·ŷ={:.4e}", theta, sy);
341 }
342
343 double sBs = ss / H0;
344 if (usesPrecon() && a_r1.size() >= 6 && a_r1.size() % 3 == 0) {
345 const Eigen::MatrixXd P = buildPrecon(a_r1);
346 sBs = eonc::exact::dot(s0, P * s0);
347 }
348
349 const double gnorm = eonc::exact::norm(a_f1);
350 const double sn = std::sqrt(ss);
351 const double yn = eonc::exact::norm(y0);
352
353 if (curv == "cautious") {
354 // Li and Fukushima, J. Comput. Appl. Math. 129, 15 (2001).
355 const double thresh =
356 cfg.cautious_eps * ss *
357 std::pow(std::max(gnorm, 1.0e-30), cfg.cautious_alpha);
358 if (sy < thresh) {
359 QUILL_LOG_DEBUG(m_log, "[LBFGS] Li-Fukushima skip, s·y={:.4e} < {:.4e}",
360 sy, thresh);
361 return 0;
362 }
363 } else if (std::abs(sy) < kLbfgsEps || (curv != "reset" && sy < 0.2 * sBs)) {
364 if (curv == "skip") {
365 QUILL_LOG_DEBUG(m_log, "[LBFGS] skip pair, s·y={:.4e}", sy);
366 return 0;
367 }
368 if (curv == "damped" && sBs > sy) {
369 const double theta = std::clamp(0.8 * sBs / (sBs - sy), 0.0, 1.0);
370 const Eigen::VectorXd B0s =
371 (usesPrecon() && a_r1.size() >= 6 && a_r1.size() % 3 == 0)
372 ? Eigen::VectorXd(buildPrecon(a_r1) * s0)
373 : Eigen::VectorXd(s0 / H0);
374 y0 = theta * y0 + (1.0 - theta) * B0s;
375 sy = eonc::exact::dot(s0, y0);
376 QUILL_LOG_DEBUG(m_log, "[LBFGS] Powell damp θ={:.3f} s·ŷ={:.4e}", theta,
377 sy);
378 } else if (std::abs(sy) < kLbfgsEps) {
379 QUILL_LOG_WARNING(m_log,
380 "[LBFGS] s0.y0 too small ({:.4e}), resetting memory",
381 eonc::exact::dot(s0, y0));
382 reset();
383 return 0;
384 }
385 }
386
387 if (!std::isfinite(sy)) {
388 QUILL_LOG_WARNING(m_log, "[LBFGS] non-finite s·y, resetting memory");
389 reset();
390 return 0;
391 }
392 // Relative overlap skip belongs to skip/damped/cautious. reset
393 // keeps the ASE rule: only |s·y| < kLbfgsEps drops the pair.
394 if (curv != "reset" && sy <= 1.0e-8 * sn * yn) {
395 QUILL_LOG_DEBUG(m_log, "[LBFGS] overlap skip, s·y={:.4e}", sy);
396 return 0;
397 }
398
399 m_rho.push_back(eonc::safemath::safe_recip(sy, 0.0));
400 m_s.push_back(std::move(s0));
401 m_y.push_back(std::move(y0));
402
403 if (static_cast<int>(m_s.size()) > m_memory) {
404 m_s.pop_front();
405 m_y.pop_front();
406 m_rho.pop_front();
407 }
408 return 0;
409}
double norm(const Eigen::MatrixBase< A > &x)
sqrt of the correctly rounded squared norm; IEEE sqrt is itself correctly rounded,...

◆ usesPrecon()

bool eonc::LBFGS::usesPrecon ( ) const
nodiscardprivate

Definition at line 88 of file LBFGS.cpp.

88 {
89 const std::string &p = m_optConfig.opts.lbfgs.precon;
90 return p == "exp" || p == "c1" || p == "lindh" || p == "lindh_full" ||
91 p == "pair" || p == "pair_abs" || p == "pair_full" || p == "fischer" ||
92 p == "schlegel" || p == "swart";
93}

Member Data Documentation

◆ m_eHist

std::deque<double> eonc::LBFGS::m_eHist
private

Definition at line 66 of file LBFGS.h.

◆ m_ePrev

double eonc::LBFGS::m_ePrev {0.0}
private

Definition at line 70 of file LBFGS.h.

70{0.0};

◆ m_fPrev

Eigen::VectorXd eonc::LBFGS::m_fPrev
private

Definition at line 69 of file LBFGS.h.

◆ m_iteration

int eonc::LBFGS::m_iteration {0}
private

Definition at line 60 of file LBFGS.h.

60{0};

◆ m_log

eonc::log::FileScoped eonc::LBFGS::m_log {"lbfgs", "_lbfgs.log"}
private

Definition at line 71 of file LBFGS.h.

71{"lbfgs", "_lbfgs.log"};

◆ m_memory

int eonc::LBFGS::m_memory {0}
private

Definition at line 61 of file LBFGS.h.

61{0};

◆ m_rho

std::deque<double> eonc::LBFGS::m_rho
private

Definition at line 65 of file LBFGS.h.

◆ m_rPrev

Eigen::VectorXd eonc::LBFGS::m_rPrev
private

Definition at line 68 of file LBFGS.h.

◆ m_s

std::deque<Eigen::VectorXd> eonc::LBFGS::m_s
private

Definition at line 63 of file LBFGS.h.

◆ m_y

std::deque<Eigen::VectorXd> eonc::LBFGS::m_y
private

Definition at line 64 of file LBFGS.h.


The documentation for this class was generated from the following files:
  • /home/runner/work/eOn/eOn/include/eon/LBFGS.h
  • /home/runner/work/eOn/eOn/client/LBFGS.cpp