Loading...
Searching...
No Matches
eonc::Lanczos Class Reference

#include <Lanczos.h>

Inheritance diagram for eonc::Lanczos:

Public Member Functions

 Lanczos (std::shared_ptr< Matter > matter, const Parameters &params, std::shared_ptr< Potential > pot)
 ~Lanczos ()=default
void compute (std::shared_ptr< Matter > matter, AtomMatrix initialDirection) override
void compute (std::shared_ptr< Matter > matter, AtomMatrix initialDirection, const VectorXi &mobileAtoms)
 Same as compute(matter, dir) but with an explicit mobile atom list (intersected with free flags).
double getEigenvalue () override
AtomMatrix getEigenvector () override
void evaluateWithProbe (const std::shared_ptr< Matter > &centre, const AtomMatrix &direction) override
 The probe is the first Krylov product's displaced image, for the one-sided product only.
Public Member Functions inherited from eonc::LowestEigenmode
 LowestEigenmode (std::shared_ptr< Potential > potPassed, const Parameters &parameters)
virtual ~LowestEigenmode ()=default

Private Attributes

std::unique_ptr< Matter > probe_
AtomMatrix lowestEv
double lowestEw {0.0}
eonc::log::Scoped log

Additional Inherited Members

Public Attributes inherited from eonc::LowestEigenmode
long totalForceCalls {0}
double statsTorque {0.0}
double statsCurvature {0.0}
double statsAngle {0.0}
long statsRotations {0}
long totalIterations {0}
Static Public Attributes inherited from eonc::LowestEigenmode
static const char MINMODE_DIMER [] = "dimer"
static const char MINMODE_GPRDIMER [] = "gprdimer"
static const char MINMODE_LANCZOS [] = "lanczos"
static const char MINMODE_DAVIDSON [] = "davidson"
static const char MINMODE_XTSCI [] = "xtsci"
Protected Attributes inherited from eonc::LowestEigenmode
std::shared_ptr< Potential > pot
const Parameters & params

Detailed Description

Definition at line 28 of file Lanczos.h.

Constructor & Destructor Documentation

◆ Lanczos()

eonc::Lanczos::Lanczos ( std::shared_ptr< Matter > matter,
const Parameters & params,
std::shared_ptr< Potential > pot )

Definition at line 37 of file Lanczos.cpp.

40 lowestEw{0.0} {
41 lowestEv.resize(matter->numberOfAtoms(), 3);
42 lowestEv.setZero();
43}
AtomMatrix lowestEv
Definition Lanczos.h:51
double lowestEw
Definition Lanczos.h:52
const Parameters & params
std::shared_ptr< Potential > pot
LowestEigenmode(std::shared_ptr< Potential > potPassed, const Parameters &parameters)

◆ ~Lanczos()

eonc::Lanczos::~Lanczos ( )
default

Member Function Documentation

◆ compute() [1/2]

void eonc::Lanczos::compute ( std::shared_ptr< Matter > matter,
AtomMatrix initialDirection )
overridevirtual

Implements eonc::LowestEigenmode.

Definition at line 45 of file Lanczos.cpp.

45 {
46 const VectorXi mobile =
47 resolveMobileAtoms(matter.get(), params.lanczos_options().phva_atoms);
48 compute(std::move(matter), std::move(direction), mobile);
49}
void compute(std::shared_ptr< Matter > matter, AtomMatrix initialDirection) override
Definition Lanczos.cpp:45
VectorXi resolveMobileAtoms(const Matter *matter, const std::string &atomList)
PHVA-class mobile set for FD Hessian and matrix-free Krylov (Lanczos / Davidson).

◆ compute() [2/2]

void eonc::Lanczos::compute ( std::shared_ptr< Matter > matter,
AtomMatrix initialDirection,
const VectorXi & mobileAtoms )

Same as compute(matter, dir) but with an explicit mobile atom list (intersected with free flags).

Eigenvector rows outside the list are 0.

Definition at line 97 of file Lanczos.cpp.

98 {
100 statsRotations = 0;
101 // A probe from evaluateWithProbe() serves this call's first product only.
102 std::unique_ptr<Matter> probe = std::move(probe_);
103 long probeCalls = 0;
104 lowestEv.resize(matter->numberOfAtoms(), 3);
105 lowestEv.setZero();
106
107 const VectorXi mobile = resolveMobileAtoms(matter.get(), mobileIn);
108 const int size = 3 * static_cast<int>(mobile.size());
109 if (size == 0) {
110 lowestEw = 0.0;
111 return;
112 }
113
114 const long maxIters = std::max(1L, params.lanczos_options().max_iterations);
115 MatrixXd T(size, maxIters), Q(size, maxIters);
116 T.setZero();
117 VectorXd u(size), r = packMobileRows(direction, mobile);
118
119 double alpha, beta = r.norm();
120 if (beta < eonc::safemath::eps) {
121 lowestEw = 0.0;
122 return;
123 }
124 double ew = 0, ewOld = 0, ewAbsRelErr;
125 const double dr = params.main_options().finiteDifference;
126 const FdScheme requested = parseFdScheme(params.hessian_options().fd_scheme);
127 // Fourth-order is the only scheme that changes the matrix-free product.
128 // one_sided and central keep the forward difference used by min-mode.
129 const FdScheme hvpScheme =
131 VectorXd evEst, evT, evOldEst;
132
133 auto tmpMatter = std::make_unique<Matter>(*matter);
134 const long forceCallsStart = tmpMatter->getForceCalls();
135 const AtomMatrix pos0 = matter->getPositions();
136 const VectorXd force0 = mobileForces(tmpMatter.get(), mobile);
137
138 auto applyH = [&](const VectorXd &v) -> VectorXd {
139 auto at = [&](double scale) -> VectorXd {
140 AtomMatrix pos = pos0;
141 unpackMobileRows(packMobileRows(pos0, mobile) + (scale * dr) * v, mobile,
142 pos);
143 tmpMatter->setPositions(pos);
144 // Compared after setPositions, which wraps into the cell as the
145 // probe's own setPositions did.
146 if (probe && !probe->needsForceUpdate() &&
147 probe->getPositions() == tmpMatter->getPositions()) {
148 const VectorXd f = mobileForces(probe.get(), mobile);
149 probe.reset();
150 probeCalls++;
151 return f;
152 }
153 probe.reset();
154 return mobileForces(tmpMatter.get(), mobile);
155 };
156 return fdHessianVector(hvpScheme, dr, force0, at);
157 };
158
159 for (int i = 0; i < size; i++) {
160 statsRotations = i;
161 Q.col(i) = r / beta;
162
163 u = applyH(Q.col(i));
164
165 if (i == 0) {
166 r = u;
167 } else {
168 r = u - beta * Q.col(i - 1);
169 }
170 alpha = Q.col(i).dot(r);
171 r = r - alpha * Q.col(i);
172
173 T(i, i) = alpha;
174 if (i > 0) {
175 T(i - 1, i) = beta;
176 T(i, i - 1) = beta;
177 }
178
179 beta = r.norm();
180 // A vanished residual closes this Krylov block. Take the Ritz pair from
181 // the current T; breaking first keeps the previous subspace.
182 const bool krylovClosed = beta <= 1e-10 * std::fabs(alpha);
183
184 if (i >= 1) {
185 Eigen::SelfAdjointEigenSolver<MatrixXd> es(T.block(0, 0, i + 1, i + 1));
186 ew = es.eigenvalues()(0);
187 evT = es.eigenvectors().col(0);
188 ewAbsRelErr = eonc::safemath::safe_div(std::fabs(ew - ewOld),
189 std::fabs(ewOld), 1.0);
190 ewOld = ew;
191
192 evEst = Q.block(0, 0, size, i + 1) * evT;
193 eonc::safemath::safe_normalize_inplace(evEst);
194 statsAngle = eonc::safemath::safe_acos(std::fabs(evEst.dot(evOldEst))) *
195 (180 / eonc::helpers::pi);
196 statsTorque = ewAbsRelErr;
197 evOldEst = evEst;
198 QUILL_LOG_INFO(log,
199 "[ILanczos] {:9s} {:9s} {:10s} {:14s} {:9.4f} "
200 "{:10.6f} {:7.3f} {:5} n_mobile={}",
201 "----", "----", "----", "----", ew, ewAbsRelErr,
202 statsAngle, i, mobile.size());
203 if (krylovClosed) {
204 QUILL_LOG_ERROR(log, "[ILanczos] ERROR: linear dependence");
205 break;
206 }
207 if (ewAbsRelErr < params.lanczos_options().tolerance) {
208 QUILL_LOG_INFO(log, "[ILanczos] Tolerance reached: {}",
209 params.lanczos_options().tolerance);
210 break;
211 }
212 } else {
213 ew = alpha;
214 ewOld = ew;
215 evEst = Q.col(0);
216 evOldEst = Q.col(0);
217 if (krylovClosed) {
218 QUILL_LOG_ERROR(log, "[ILanczos] ERROR: linear dependence");
219 break;
220 }
221 if (lowestEw != 0.0 && params.lanczos_options().quit_early) {
222 double Cprev = lowestEw;
223 double Cnew = u.dot(Q.col(i));
224 ewAbsRelErr = eonc::safemath::safe_div(std::fabs(Cnew - Cprev),
225 std::fabs(Cprev), 1.0);
226 if (ewAbsRelErr <= params.lanczos_options().tolerance) {
227 statsAngle = 0.0;
228 statsTorque = ewAbsRelErr;
229 QUILL_LOG_INFO(log, "[ILanczos] Tolerance reached: {}",
230 params.lanczos_options().tolerance);
231 break;
232 }
233 }
234 }
235
236 if (i >= params.lanczos_options().max_iterations - 1) {
237 QUILL_LOG_ERROR(log, "[ILanczos] Max iterations");
238 break;
239 }
240 }
241
242 lowestEw = ew;
243 totalForceCalls = tmpMatter->getForceCalls() - forceCallsStart + probeCalls;
244
245 lowestEv.setZero();
246 if (evEst.size() == size) {
247 unpackMobileRows(evEst, mobile, lowestEv);
248 }
249}
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, eOnStorageOrder > MatrixXd
Definition Eigen.h:33
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
Definition Eigen.h:37
std::unique_ptr< Matter > probe_
Definition Lanczos.h:50
eonc::log::Scoped log
Definition Lanczos.h:53
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 eps
Definition SafeMath.h:19
FdScheme
Real finite-difference scheme for the assembled Hessian and for Lanczos/Davidson Hessian-vector produ...
VectorXd fdHessianVector(FdScheme scheme, double dr, const VectorXd &force0, Eval &&eval)
Energy Hessian-vector product -dF.
void unpackMobileRows(const VectorXd &packed, const VectorXi &mobile, AtomMatrix &full)
Write a 3*n_mobile vector into full AtomMatrix rows (other rows unchanged).
FdScheme parseFdScheme(std::string_view scheme)
VectorXd packMobileRows(const AtomMatrix &full, const VectorXi &mobile)
Pack full (n_atoms,3) rows of mobile atoms into a 3*n_mobile vector.
VectorXd mobileForces(Matter *matter, const VectorXi &mobile)
Force components on mobile atoms after Matter has a valid force cache (calls getForces under the hood...

◆ evaluateWithProbe()

void eonc::Lanczos::evaluateWithProbe ( const std::shared_ptr< Matter > & centre,
const AtomMatrix & direction )
overridevirtual

The probe is the first Krylov product's displaced image, for the one-sided product only.

Reimplemented from eonc::LowestEigenmode.

Definition at line 75 of file Lanczos.cpp.

76 {
77 probe_.reset();
78 if (!centre->needsForceUpdate() || !pot->supportsBatchEvaluation() ||
79 !oneSidedProduct(params)) {
80 return;
81 }
82 const VectorXi mobile = resolveMobileAtoms(
83 centre.get(),
84 resolveMobileAtoms(centre.get(), params.lanczos_options().phva_atoms));
85 const auto pos = firstProbe(*centre, direction, mobile,
86 params.main_options().finiteDifference);
87 if (!pos) {
88 return;
89 }
90 auto probe = std::make_unique<Matter>(*centre);
91 probe->setPositions(*pos);
92 Matter *const both[] = {centre.get(), probe.get()};
94 probe_ = std::move(probe);
95}
void evaluateTogether(Potential &pot, std::span< Matter *const > systems, bool threads, std::span< const long > owners)
Evaluates every system that needs a force update.
Definition Matter.cpp:998

◆ getEigenvalue()

double eonc::Lanczos::getEigenvalue ( )
overridevirtual

Implements eonc::LowestEigenmode.

Definition at line 251 of file Lanczos.cpp.

251{ return lowestEw; }

◆ getEigenvector()

AtomMatrix eonc::Lanczos::getEigenvector ( )
overridevirtual

Implements eonc::LowestEigenmode.

Definition at line 253 of file Lanczos.cpp.

253{ return lowestEv; }

Member Data Documentation

◆ log

eonc::log::Scoped eonc::Lanczos::log
private

Definition at line 53 of file Lanczos.h.

◆ lowestEv

AtomMatrix eonc::Lanczos::lowestEv
private

Definition at line 51 of file Lanczos.h.

◆ lowestEw

double eonc::Lanczos::lowestEw {0.0}
private

Definition at line 52 of file Lanczos.h.

52{0.0};

◆ probe_

std::unique_ptr<Matter> eonc::Lanczos::probe_
private

Definition at line 50 of file Lanczos.h.


The documentation for this class was generated from the following files: