408 {
409 const int size =
static_cast<int>(
atoms.rows()) * 3;
411 auto pot =
matter->getPotential();
412
415 if (!force0.allFinite()) {
416 QUILL_LOG_ERROR(
log,
"[Hessian] non-finite forces at undisplaced geometry; "
417 "aborting FD Hessian");
418 return false;
419 }
420
421
422 std::vector<double> steps{1.0};
424 steps.push_back(-1.0);
425 }
427 steps.push_back(2.0);
428 steps.push_back(-2.0);
429 }
430 const int perColumn = static_cast<int>(steps.size());
431 const long nAtoms =
matter->numberOfAtoms();
432 const VectorXi nrs =
matter->getAtomicNrs();
434 matter->getPeriodic() ?
matter->getCell() : Matrix3d::Zero().eval();
435
436
437 constexpr int kChunkColumns = 32;
438 std::vector<Matter> displaced;
439 for (int c0 = 0; c0 < size; c0 += kChunkColumns) {
440 const int c1 = std::min(size, c0 + kChunkColumns);
441 const long n = static_cast<long>(c1 - c0) * perColumn;
442 displaced.assign(static_cast<size_t>(n), base);
443 std::vector<const double *> posPtr, boxPtr;
444 std::vector<const int *> nrsPtr;
445 std::vector<double *> frcPtr;
446 for (int i = c0; i < c1; ++i) {
447 for (int k = 0; k < perColumn; ++k) {
448 Matter &m = displaced[static_cast<size_t>((i - c0) * perColumn + k)];
450 p(
atoms(i / 3), i % 3) += steps[
static_cast<size_t>(k)] * dr;
452 }
453 }
454 for (auto &m : displaced) {
456 nrsPtr.push_back(nrs.data());
458 boxPtr.push_back(box.data());
459 }
460 std::vector<double> energies(static_cast<size_t>(n)),
461 variances(static_cast<size_t>(n));
462 const std::vector<long> owners(static_cast<size_t>(n),
464 pot->forceBatchOwned(n, nAtoms, posPtr.data(), nrsPtr.data(), frcPtr.data(),
465 energies.data(), variances.data(), boxPtr.data(),
466 owners.data());
467 for (long j = 0; j < n; ++j) {
468 displaced[static_cast<size_t>(j)].setComputedPotential(
469 energies[static_cast<size_t>(j)], variances[static_cast<size_t>(j)]);
470 }
471 for (int i = c0; i < c1; ++i) {
472 auto forces = [&](
int k) ->
const AtomMatrix & {
473 return displaced[static_cast<size_t>((i - c0) * perColumn + k)]
474 .getForces();
475 };
477 const AtomMatrix &fMinus = perColumn > 1 ? forces(1) : force0;
478 const AtomMatrix &fPlus2 = perColumn > 2 ? forces(2) : force0;
479 const AtomMatrix &fMinus2 = perColumn > 3 ? forces(3) : force0;
480 if (!fPlus.allFinite() || !fMinus.allFinite() || !fPlus2.allFinite() ||
481 !fMinus2.allFinite()) {
483 "[Hessian] non-finite forces for FD column {}; "
484 "aborting FD Hessian",
485 i);
486 return false;
487 }
490 for (int j = 0; j < size; j++) {
491 const double effMass = std::sqrt(
matter->getMass(
atoms(j / 3)) *
495 }
496 }
497 }
499}
Eigen::Matrix< double, 3, 3, eOnStorageOrder > Matrix3d
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
const AtomMatrix & getPositions() const
void setPositions(const AtomMatrix &pos)
double * forcesData()
Mutable access to force storage for batched potential evaluation.
bool finalizeHessian(int size)
constexpr long derivative
constexpr double safe_div(double num, double denom, double fallback=0.0)
M fdForceDerivative(FdScheme scheme, double dr, const M &f0, const M &fPlus, const M &fMinus, const M &fPlus2, const M &fMinus2)
Derivative of a sampled force map along one real step of length dr.