Loading...
Searching...
No Matches
BandStall.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#ifndef GPR_NEB_BAND_STALL_H_
6#define GPR_NEB_BAND_STALL_H_
7
8#include <algorithm>
9#include <limits>
10#include <vector>
11
12#include "NEBPath.h"
13
14namespace gpr::neb {
15
18
24 double best_force = std::numeric_limits<double>::infinity();
25 int quiet = 0;
26 int restores = 0;
27 bool have = false;
28 int image = -1;
29 double energy = std::numeric_limits<double>::infinity();
30 std::vector<Image> band;
31};
32
39inline int bandStallWindow(int n_images, int polished_dofs) noexcept
40{
41 const int images = n_images > 1 ? n_images : 1;
42 const int pairs = images * (images - 1);
43 const int polished = polished_dofs > 0 ? polished_dofs : 0;
44 return std::max(pairs, std::max(polished, 1));
45}
46
50inline void noteLowestCertifiedClimb(BandForceStall& stall, const NEBPath& path,
51 int image)
52{
53 if (image <= 0 || image >= path.numImages() - 1) return;
54 const Image& img = path.image(image);
55 const Image& prev = path.image(image - 1);
56 const Image& next = path.image(image + 1);
57 if (!img.evaluated || !prev.evaluated || !next.evaluated) return;
58 if (!std::isfinite(img.energy) || !std::isfinite(prev.energy) ||
59 !std::isfinite(next.energy))
60 return;
61 // A certified climb is a measured local maximum, not a placeholder zero
62 // left on an image the oracle has not touched.
63 if (!(img.energy > prev.energy && img.energy > next.energy)) return;
64 if (stall.have && !(img.energy < stall.energy)) return;
65 stall.have = true;
66 stall.image = image;
67 stall.energy = img.energy;
68 stall.band.clear();
69 stall.band.reserve(static_cast<std::size_t>(path.numImages()));
70 for (int i = 0; i < path.numImages(); ++i)
71 stall.band.push_back(path.image(i));
72}
73
81 double max_force,
82 double force_tolerance, int window)
83{
84 if (!(force_tolerance > 0.0) || window < 1 || !std::isfinite(max_force)) {
86 }
87 if (max_force < stall.best_force - force_tolerance) {
88 stall.best_force = max_force;
89 stall.quiet = 0;
91 }
92 if (++stall.quiet < window || !stall.have) return BandStallAction::Continue;
93 stall.quiet = 0;
94 if (stall.restores >= 1) return BandStallAction::Stop;
95 ++stall.restores;
97}
98
100inline void restoreCertifiedClimb(NEBPath& path, const BandForceStall& stall)
101{
102 if (!stall.have) return;
103 const int n =
104 std::min(path.numImages(), static_cast<int>(stall.band.size()));
105 for (int i = 0; i < n; ++i)
106 path.image(i) = stall.band[static_cast<std::size_t>(i)];
107 path.computeNEBForces();
108 path.enableClimbingImage();
109}
110
111} // namespace gpr::neb
112
113#endif // GPR_NEB_BAND_STALL_H_
NEB path management: image chain, spring forces, climbing image.
NEB path: chain of images between two endpoints.
Definition NEBPath.h:57
int numImages() const
Definition NEBPath.h:338
void enableClimbingImage()
Enable climbing image on the highest-energy intermediate image.
Definition NEBPath.cpp:1121
void computeNEBForces()
Compute NEB forces for all intermediate images.
Definition NEBPath.cpp:880
Image & image(int i)
Access image i.
Definition NEBPath.h:329
The nudged elastic band: path, GP-NEB driver, configuration and result, acquisition,...
void restoreCertifiedClimb(NEBPath &path, const BandForceStall &stall)
Put the certified climb back and make it the climbing image.
Definition BandStall.h:100
BandStallAction observeMaxBandForce(BandForceStall &stall, double max_force, double force_tolerance, int window)
Definition BandStall.h:80
BandStallAction
What to do when the max band force stops falling.
Definition BandStall.h:17
void noteLowestCertifiedClimb(BandForceStall &stall, const NEBPath &path, int image)
Definition BandStall.h:50
int bandStallWindow(int n_images, int polished_dofs) noexcept
Definition BandStall.h:39
std::vector< Image > band
Definition BandStall.h:30
A single image on the NEB path.
Definition NEBPath.h:39
double energy
Definition NEBPath.h:41