28bool batchForcesFinite(
long nAtoms,
const double *forces,
double energy) {
29 if (forces ==
nullptr || !std::isfinite(energy)) {
32 return Eigen::Map<const AtomMatrix>(forces, nAtoms, 3).allFinite();
35void rejectNonFiniteBatch(
long nAtoms,
const double *forces,
double energy) {
36 if (!batchForcesFinite(nAtoms, forces, energy)) {
37 throw std::runtime_error(
"Dimer::calcRotationalForceReturnCurvature: "
38 "non-finite batch forces");
42double atanRatioOrHalfPi(
double num,
double denom) {
43 if (!std::isfinite(num) || !std::isfinite(denom)) {
52 return std::atan(num / denom);
58 std::shared_ptr<Potential>
pot)
62 (
pot->needsPerImageInstance() &&
params.main_options().parallel)
69 nAtoms = matter->numberOfAtoms();
82 eonc::safemath::safe_normalize_inplace(initialDirection);
89 static_cast<quill::Logger *
>(
log))) {
94 eonc::safemath::safe_normalize_inplace(
direction);
95 for (
long i = 0; i <
nAtoms; ++i) {
106 long forceCallsDimer =
matterDimer->getForceCalls();
107 double curvature = 0.0;
108 double rotationAngle = 0.0;
114 rotationalForce.setZero();
115 rotationalForceOld.setZero();
116 rotationalPlaneOld.setZero();
119 double lengthRotationalForceOld = 0.0;
122 bool doneRotating =
false;
123 while (!doneRotating) {
127 rotationalPlaneOld, lengthRotationalForceOld);
129 torque = rotationalForce.norm();
131 assert(std::isfinite(torque));
134 if ((torque >
params.dimer_options().torque_max &&
135 rotations >=
params.dimer_options().rotations_max) ||
136 (torque <
params.dimer_options().torque_max &&
137 torque >=
params.dimer_options().torque_min &&
138 rotations >=
params.dimer_options().rotations_min) ||
139 (torque <
params.dimer_options().torque_min)) {
152 double rotForceChange =
153 (rotForce1 - rotForce2) /
params.dimer_options().rotation_angle;
154 double forceDimer = (rotForce1 + rotForce2) / 2.0;
157 atanRatioOrHalfPi(2.0 * forceDimer, rotForceChange) / 2.0 -
158 params.dimer_options().rotation_angle / 2.0;
160 if (rotForceChange < 0) {
169 "[DimerRot] ----- --------- ---------------- "
170 "--------- {:9.3e} {:9.3e} {:9.3e} ---------\n",
177 eonc::safemath::safe_normalize_inplace(
direction);
178 for (
long i = 0; i <
nAtoms; ++i) {
188 forceCallsCenter =
matterCenter->getForceCalls() - forceCallsCenter;
189 forceCallsDimer =
matterDimer->getForceCalls() - forceCallsDimer;
205 if (
params.dimer_options().remove_rotation) {
210 eonc::safemath::safe_normalize_inplace(
direction);
211 posDimer = posCenter +
direction *
params.main_options().finiteDifference;
217 if (
pot->supportsBatchEvaluation()) {
222 if (centerDirty && dimerDirty) {
228 const double *posVec[] = {
matterCenter->getPositions().data(),
230 const int *nrsVec[] = {nrs0.data(), nrs1.data()};
233 double energies[2], vars[2];
234 const double *boxVec[] = {box0.data(), box1.data()};
235 pot->forceBatch(2,
nAtoms, posVec, nrsVec, frcVec, energies, vars,
237 rejectNonFiniteBatch(
nAtoms, frcVec[0], energies[0]);
238 rejectNonFiniteBatch(
nAtoms, frcVec[1], energies[1]);
239 matterCenter->setComputedPotential(energies[0], vars[0]);
240 matterDimer->setComputedPotential(energies[1], vars[1]);
241 }
else if (dimerDirty) {
245 const double *posVec[] = {posDimer.data()};
246 const int *nrsVec[] = {nrs.data()};
248 double energies[1], vars[1];
249 const double *boxVec[] = {box.data()};
250 pot->forceBatch(1,
nAtoms, posVec, nrsVec, frcVec, energies, vars,
252 rejectNonFiniteBatch(
nAtoms, frcVec[0], energies[0]);
253 matterDimer->setComputedPotential(energies[0], vars[0]);
254 }
else if (centerDirty) {
265 AtomMatrix forceB = 2.0 * forceCenter - forceA;
276 (forceA - forceB) / (2.0 *
params.main_options().finiteDifference);
279 return (projB - projA) / (2.0 *
params.main_options().finiteDifference);
285 double &lengthRotationalForceOld) {
287 double a = std::abs(
matDot(rotationalForce, rotationalForceOld));
288 double b = rotationalForceOld.squaredNorm();
291 gamma = (rotationalForce.array() *
292 (rotationalForce - rotationalForceOld).array())
299 rotationalForce + rotationalPlaneOld * lengthRotationalForceOld * gamma;
306 rotationalForceOld = rotationalForce;
312 double cosA = std::cos(rotationAngle);
313 double sinA = std::sin(rotationAngle);
320 eonc::safemath::safe_normalize_inplace(
direction);
double matDot(const AtomMatrix &a, const AtomMatrix &b)
SIMD-optimized dot product for contiguous Eigen matrices.
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
AtomMatrix rotationalPlane
std::shared_ptr< Matter > matterDimer
double getEigenvalue() override
AtomMatrix getEigenvector() override
std::shared_ptr< Matter > matterCenter
void determineRotationalPlane(const AtomMatrix &rotationalForce, AtomMatrix &rotationalForceOld, const AtomMatrix &rotationalPlaneOld, double &lengthRotationalForceOld)
Determine rotational plane via conjugate gradient.
double calcRotationalForceReturnCurvature(AtomMatrix &rotationalForce)
Compute rotational force and return curvature along the dimer.
void rotate(double rotationAngle)
Rotate the dimer by the given angle (radians).
eonc::log::FileScoped log
void compute(std::shared_ptr< Matter > matter, AtomMatrix initialDirection) override
Dimer(std::shared_ptr< Matter > matter, const Parameters ¶ms, std::shared_ptr< Potential > pot)
const Parameters & params
std::shared_ptr< Potential > pot
LowestEigenmode(std::shared_ptr< Potential > potPassed, const Parameters ¶meters)
void rotationRemove(const AtomMatrix r1, std::shared_ptr< Matter > m2)
std::shared_ptr< Potential > makeImagePotential(const Potential &pot, const Parameters ¶ms)
A separate instance of pot for one image of a band, dimer or pair of minimizations: a clone when the ...
AtomMatrix makeOrthogonal(const AtomMatrix v1, const AtomMatrix v2)
double safe_acos(double x)
RAII resource manager for the ARTn C library with global synchronization.
std::optional< DimerRotationResult > runAlternativeRotation(DimerRotationBackend backend, const std::shared_ptr< Matter > &matter, const Parameters ¶ms, const std::shared_ptr< Potential > &pot, const AtomMatrix &initialDirection, quill::Logger *log=nullptr)
Run Lanczos, Davidson, or LOR.