Loading...
Searching...
No Matches
FIRE.cpp
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12#include "eon/FIRE.h"
13#include "eon/ExactReduction.h"
14#include "eon/HelperFunctions.h"
15
16#include <algorithm>
17#include <format>
18#include <stdexcept>
19
20namespace eonc {
21
22int FIRE::step(double a_maxMove) {
23 double P = 0;
24 // Check convergence.
25 if (m_objf->isConverged()) {
26 return 1;
27 }
28
29 // Velocity Verlet
30 Eigen::VectorXd f = -m_objf->getGradient();
31 Eigen::VectorXd x = m_objf->getPositions();
32
33 m_vel += f * m_dt;
34 Eigen::VectorXd dx = m_vel * m_dt;
35
36 dx = eonc::geometry::maxAtomMotionAppliedV(dx, a_maxMove);
37 m_objf->setPositions(x + dx);
38
39 // A climbing objective may already be past max_energy. Rotating the
40 // mode here spends a dimer or Lanczos solve the search will discard,
41 // and the rotation can throw before the pull-back runs. The centre
42 // was evaluated by the check, so a point under the limit still shares
43 // a batching solver's first image with the gradient below.
44 if (m_objf->pastClimbCeiling()) {
46 return 0;
47 }
48
49 f = -m_objf->getGradient();
50 const double fnorm = eonc::exact::norm(f);
51 Eigen::VectorXd f_unit = Eigen::VectorXd::Zero(f.size());
52 if (fnorm > 0.0) {
53 f_unit = f / fnorm;
54 }
55
56 // FIRE
57 P = eonc::exact::dot(f, m_vel);
58 m_vel = (1 - m_alpha) * m_vel + m_alpha * f_unit * eonc::exact::norm(m_vel);
59 QUILL_LOG_DEBUG(m_log,
60 "P: {:.4f}, v: {:.4f}, m_dt: {:.4f}, m_alpha: {:.4f}, N: {}",
61 P, m_vel.norm(), m_dt, m_alpha, m_N);
62 if (P >= 0) {
63 m_N++;
64 if (m_N > m_N_min) {
65 m_dt = std::min(m_dt * m_f_inc, m_dt_max);
67 }
68 } else {
69 m_dt = m_dt * m_f_dec;
70 m_vel.setZero();
72 m_N = 0;
73 }
74
75 // add a sanity check on m_dt
76 if (m_dt < 1e-6) {
77 QUILL_LOG_CRITICAL(m_log, "[FIRE] [critical] m_dt is too small: {:.4f}",
78 m_dt);
79 throw std::runtime_error(
80 std::format("[FIRE] m_dt is too small: {:.4f}", m_dt));
81 }
82
84 return m_objf->isConverged() ? 1 : 0;
85}
86
88 // The velocity was kicked toward the step that crossed the limit.
89 m_vel.setZero();
90}
91
92int FIRE::run(size_t a_maxIterations, double a_maxMove) {
93 while (!m_objf->isConverged() && m_iteration < a_maxIterations) {
94 step(a_maxMove);
95 }
96 return m_objf->isConverged() ? 1 : 0;
97}
98
99} // namespace eonc
void onTranslationPulledBack() override
Definition FIRE.cpp:87
Eigen::VectorXd m_vel
Definition FIRE.h:39
double m_dt_max
Definition FIRE.h:37
double m_f_inc
Definition FIRE.h:42
size_t m_N_min
Definition FIRE.h:38
double m_alpha_start
Definition FIRE.h:40
int step(double a_maxMove) override
Definition FIRE.cpp:22
double m_f_a
Definition FIRE.h:44
eonc::log::FileScoped m_log
Definition FIRE.h:46
size_t m_iteration
Definition FIRE.h:45
int run(size_t a_maxIterations, double a_maxMove) override
Definition FIRE.cpp:92
size_t m_N
Definition FIRE.h:38
double m_alpha
Definition FIRE.h:41
double m_f_dec
Definition FIRE.h:43
double m_dt
Definition FIRE.h:37
std::shared_ptr< ObjectiveFunction > m_objf
Definition Optimizer.h:74
double norm(const Eigen::MatrixBase< A > &x)
sqrt of the correctly rounded squared norm; IEEE sqrt is itself correctly rounded,...
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].
VectorXd maxAtomMotionAppliedV(const VectorXd v1, double maxMotion)
RAII resource manager for the ARTn C library with global synchronization.