Loading...
Searching...
No Matches
AtomicDimer.inl
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5/*
6 * AtomicDimer.inl
7 *
8 * Created on: 18 Nov 2020
9 * Author: Maxim Masterov
10 * Company: SURFSara
11 */
12
13#ifndef GPR_ATOMICDIMER_INL_
14#define GPR_ATOMICDIMER_INL_
15
16namespace atmd {
17
18inline double AtomicDimer::calculateCurvature(
19 const gpr::Coord& orient, const gpr::Observation& approx_obs,
20 const bool use_hssvd_analytic, const double cached_hssvd_curvature)
21{
22 // Minimum-mode curvature is v^T H_mu v (doi:10.1063/1.480097). The
23 // cached value is the Ritz value of the mode just installed from the
24 // model's Hessian action and still aligned with the orientation; it is
25 // the curvature of that mode for every model, from the operator that
26 // selected it. After the orientation leaves the mode, a kernel whose
27 // meanCurvature is Exact is contracted. A kernel that does not report
28 // Exact uses the posterior-mean Hessian action and says so once. The
29 // forward difference of the predicted gradient is not a stand-in.
30 if (use_hssvd_analytic && std::isfinite(cached_hssvd_curvature))
31 return cached_hssvd_curvature;
32
33 if (use_hssvd_analytic) {
34 double analytic_curvature = std::numeric_limits<double>::quiet_NaN();
35 if (gpr_model == nullptr || orient.rows() != 1 ||
36 approx_obs.R.rows() < 1 || approx_obs.R.cols() != orient.cols() ||
37 !orient.allFinite() || !approx_obs.R.row(0).allFinite())
38 return analytic_curvature;
39 const gpr::Coord midpoint = approx_obs.R.topRows(1);
40 const Eigen::VectorXd direction = orient.row(0).transpose();
41 if (gpr_model->calculateExactDirectionalCurvature(midpoint, direction,
42 analytic_curvature))
43 return analytic_curvature;
44 const auto hessian = gpr_model->prepareMeanHessian(midpoint);
45 Eigen::VectorXd hessian_vector;
46 if (!hessian || !hessian(direction, hessian_vector) ||
47 hessian_vector.size() != direction.size())
48 return std::numeric_limits<double>::quiet_NaN();
49 analytic_curvature = direction.dot(hessian_vector);
50 if (!std::isfinite(analytic_curvature))
51 return std::numeric_limits<double>::quiet_NaN();
52 // The relaxation phase asks for this curvature on every inner
53 // iteration. The note does not.
54 if (!inexact_curvature_logged_) {
55 inexact_curvature_logged_ = true;
56 log_man << "Dimer curvature is v^T H v of the posterior-mean "
57 "Hessian action. The kernel does not report an exact "
58 "mean curvature.\n";
59 }
60 return analytic_curvature;
61 }
62
63 if (approx_obs.G.rows() < 2 || approx_obs.G.cols() != orient.cols() ||
64 !(dimer_sep > 0.0) || !std::isfinite(dimer_sep)) {
65 return std::numeric_limits<double>::quiet_NaN();
66 }
67
68 double curvature = 0.;
69
70 for (gpr::Index_t j = 0; j < approx_obs.G.cols(); ++j) {
71 curvature += (approx_obs.G(1, j) - approx_obs.G(0, j)) * orient(0, j);
72 }
73 curvature /= dimer_sep;
74
75 return curvature;
76}
77
78inline Eigen::VectorXd AtomicDimer::getFinalGradientAtMidPoint() const
79{
80 Eigen::VectorXd gradient(middle_point_final.G.cols());
81
82 for (gpr::Index_t n = 0; n < gradient.rows(); ++n)
83 gradient(n) = middle_point_final.G(0, n);
84
85 return gradient;
86}
87
88inline Eigen::VectorXd AtomicDimer::getFinalForceAtMidPoint() const
89{
91}
92
94{
95 return forceNormForConvergence(middle_point_final);
96}
97
98inline Eigen::VectorXd AtomicDimer::getFinalCoordOfMidPoint() const
99{
100 Eigen::VectorXd coord(middle_point_final.R.cols());
101
102 for (gpr::Index_t n = 0; n < coord.rows(); ++n)
103 coord(n) = middle_point_final.R(0, n);
104
105 return coord;
106}
107
109{
110 return num_of_gen_potential_calls;
111}
112
114{
115 if (gpr_model != nullptr) {
116 return gpr_model->getNumberOfPotentialCalls();
117 }
118 return 0;
119}
120
122{
123 return totalIterations;
124}
125
126inline double AtomicDimer::getFinalEnergy() const
127{
128 return middle_point_final.E(0, 0);
129}
130
132{
133 return curvature_final;
134}
135
136inline bool AtomicDimer::getConverged() const
137{
138 return final_converged_;
139}
140
142{
143 return orient_final;
144}
145
147{
148 markObservedDataChanged();
149 return all_obs.R;
150}
151
156{
157 markObservedDataChanged();
158 return all_obs.E;
159}
160
165{
166 markObservedDataChanged();
167 return all_obs.G;
168}
169
174{
175 return gpr_model;
176}
177
179 const gpr::AtomsConfiguration& _atoms_config)
180{
181 atoms_config = _atoms_config;
182}
183
184inline void AtomicDimer::assignFinalResults(
185 const gpr::Observation& middle_point,
186 const std::pair<double, gpr::Coord>& eigen_data)
187{
188 middle_point_final.R = middle_point.R;
189 middle_point_final.E = middle_point.E;
190 middle_point_final.G = middle_point.G;
191 curvature_final = eigen_data.first;
192 latest_mode_curvature_ = eigen_data.first;
193 orient_final = eigen_data.second;
194}
195
196} /* namespace atmd */
197
198#endif /* GPR_ATOMICDIMER_INL_ */
gpr::GaussianProcessRegression * getGPRModel()
Get GPR model.
Eigen::VectorXd getFinalCoordOfMidPoint() const
Return final coordinates of the middle point.
void setAtomsConfiguration(const gpr::AtomsConfiguration &_atoms_config)
Set configuration of the atoms.
gpr::Coord & getGradientForAllObservationPointsRef()
Return gradients for all observed points.
gpr::Index_t getTotalGPRForceCalls() const
Return the total number of force calls.
gpr::AtomsConfiguration atoms_config
gpr::FieldMatrixd & getEnergyForAllObservationPointsRef()
Return energies for all observed points.
Eigen::VectorXd getFinalGradientAtMidPoint() const
Return the final gradient dE/dR at the middle point.
const gpr::Coord & getFinalOrientation() const
Return final orientation of the dimer.
gpr::Index_t getTotalForceCalls() const
Return the total number of force calls.
double getFinalForceNormForConvergence() const
Return final force in the configured convergence norm.
double getFinalEnergy() const
Return final energy.
Eigen::VectorXd getFinalForceAtMidPoint() const
Deprecated alias of getFinalGradientAtMidPoint().
double getFinalCurvature() const
Return final curvature.
bool getConverged() const
Return true when the final force convergence criterion is met.
gpr::Coord & getMidPointCoordForAllObservationPointsRef()
Return gpr::Coordinates of all observed points.
gpr::Index_t getIterations() const
Return the total number of cycles.
bool calculateExactDirectionalCurvature(const Coord &R, const Eigen::VectorXd &direction, double &curvature)
MeanHessianAction prepareMeanHessian(const Coord &R)
Linear action of the physical posterior mean at one geometry.
The atomic dimer search (class AtomicDimer) and its status types.
FieldMatrixd Coord
Definition Macros.h:35
uint32_t Index_t
Definition Macros.h:17
Eigen::Matrix< double, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixd
Definition Macros.h:25
Stores configurations of all atoms in the system.
Definition Structures.h:832
Observation data: energy, gradient and positions.
FieldMatrixd E