Loading...
Searching...
No Matches
SteepestDescent.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
13// Based on the SteepestDescent minimizer written in ASE.
14
15#include "eon/SteepestDescent.h"
16#include "eon/SafeMath.h"
17
18int SteepestDescent::step(double a_maxMove) {
19 Eigen::VectorXd r = m_objf->getPositions();
20 Eigen::VectorXd f = -m_objf->getGradient();
21
22 Eigen::VectorXd dr;
23 double alpha = m_optConfig.opts.sd.alpha;
24 if (m_optConfig.opts.sd.two_point && iteration > 0) {
25 Eigen::VectorXd dx = r - m_rPrev;
26 Eigen::VectorXd dg = -f + m_fPrev;
27 alpha = eonc::safemath::safe_div(dx.dot(dx), dx.dot(dg), 0.0);
28 if (alpha < 0) {
29 alpha = m_optConfig.opts.sd.alpha;
30 }
31 QUILL_LOG_DEBUG(m_log, "[SD] alpha: {:.4e}", alpha);
32 }
33
34 dr = alpha * f;
35 dr = eonc::helpers::maxAtomMotionAppliedV(dr, a_maxMove);
36
37 m_objf->setPositions(r + dr);
38
39 m_rPrev = r;
40 m_fPrev = f;
41
42 iteration++;
43
44 return m_objf->isConverged() ? 1 : 0;
45}
46
47int SteepestDescent::run(size_t a_maxIteration, double a_maxMove) {
48 while (!m_objf->isConverged() && iteration < a_maxIteration) {
49 step(a_maxMove);
50 }
51 return m_objf->isConverged() ? 1 : 0;
52}
int step(double a_maxMove) override
const OptimizerConfig m_optConfig
Definition Optimizer.h:67
std::shared_ptr< ObjectiveFunction > m_objf
Definition Optimizer.h:68
int run(size_t a_maxIterations, double a_maxMove) override
int step(double a_maxMove) override
Eigen::VectorXd m_fPrev
eonc::log::FileScoped m_log
Eigen::VectorXd m_rPrev
VectorXd maxAtomMotionAppliedV(const VectorXd v1, double maxMotion)
constexpr double safe_div(double num, double denom, double fallback=0.0)
Definition SafeMath.h:21