42bool loadColumnCheckpoint(
const std::string &path,
int size,
int &nextCol,
43 MatrixXd &H,
const std::string &context) {
44 eonc::log::Scoped
log;
45 std::ifstream in(path);
51 in >> tag >> fileSize >> nextCol;
52 if (!in || tag !=
"eon_hess_ckpt" || fileSize != size || nextCol < 0 ||
57 for (
int i = 0; i < size; ++i) {
58 for (
int j = 0; j < size; ++j) {
68 if (!(in >> marker)) {
69 QUILL_LOG_WARNING(
log,
"[Hessian] legacy checkpoint has no geometry or "
70 "displacement identity; resume is unverified");
74 if (marker !=
"eon_hess_context_v1" || !(in >> std::quoted(stored)) ||
76 QUILL_LOG_WARNING(
log,
"[Hessian] checkpoint context mismatch; "
77 "recomputing all finite-difference columns");
85std::string columnCheckpointContext(
const Matter &matter,
const VectorXi &atoms,
88 std::ostringstream out;
89 out.imbue(std::locale::classic());
90 out << std::setprecision(std::numeric_limits<double>::max_digits10);
91 out << dr << ' ' << static_cast<int>(scheme) <<
' '
98 out <<
' ' << numbers(i) <<
' ' << matter.
getMass(i);
99 for (
int j = 0; j < 3; ++j) {
100 out <<
' ' << positions(i, j) <<
' ' << matter.
getFixed(i, j);
103 const auto cell = matter.
getCell();
104 for (
int i = 0; i < 3; ++i) {
105 for (
int j = 0; j < 3; ++j) {
106 out <<
' ' << cell(i, j);
109 out <<
' ' << atoms.size();
110 for (
int i = 0; i < atoms.size(); ++i) {
111 out <<
' ' << atoms(i);
116bool saveColumnCheckpoint(
const std::string &path,
int size,
int nextCol,
117 const MatrixXd &H,
const std::string &context) {
119 if (std::fprintf(out,
"eon_hess_ckpt %d %d\n", size, nextCol) < 0) {
122 for (
int i = 0; i < size; ++i) {
123 for (
int j = 0; j < size; ++j) {
124 if (std::fprintf(out,
"%.*g%c",
125 std::numeric_limits<double>::max_digits10, H(i, j),
126 j + 1 == size ?
'\n' :
' ') < 0) {
131 std::ostringstream footer;
132 footer <<
"eon_hess_context_v1 " << std::quoted(context) <<
'\n';
133 return std::fputs(footer.str().c_str(), out) >= 0;
147 if ((
matter != matterIn) || (
atoms.size() != atomsIn.size()) ||
161 if ((
matter != matterIn) || (
atoms.size() != atomsIn.size()) ||
177struct MobileColoring {
178 std::vector<int> color;
180 std::vector<std::vector<int>> closed;
183MobileColoring buildMobileColoring(
const Matter &matter,
const VectorXi &atoms,
187 const int nMobile =
static_cast<int>(atoms.rows());
188 if (nAtoms <= 0 || nMobile <= 0 || !(cutoff > 0.0) ||
189 !std::isfinite(cutoff)) {
193 if (!(std::abs(cell.determinant()) > 1e-18)) {
196 for (
int a = 0; a < nMobile; ++a) {
197 const long idx = atoms(a);
198 if (idx < 0 || idx >= nAtoms) {
204 std::vector<std::vector<int>> nbs(
static_cast<std::size_t
>(nAtoms));
205 auto add = [&](
int i,
int j) {
206 if (i != j && i >= 0 && j >= 0 && i < nAtoms && j < nAtoms)
207 nbs[
static_cast<std::size_t
>(i)].push_back(j);
211 for (
const auto &pair :
213 cell.data(), cutoff))
214 add(pair.first, pair.second);
220 opt.return_distances =
false;
221 opt.return_vectors =
false;
222 opt.periodic = {
false,
false,
false};
223 nl.compute(pos.data(),
static_cast<std::size_t
>(nAtoms), cell.data(),
225 for (
size_t p = 0; p < nl.size(); ++p)
226 add(
static_cast<int>(nl.i(p)),
static_cast<int>(nl.j(p)));
228 }
catch (
const std::exception &) {
231 for (
auto &row : nbs) {
232 std::sort(row.begin(), row.end());
233 row.erase(std::unique(row.begin(), row.end()), row.end());
236 std::vector<int> local(
static_cast<std::size_t
>(nAtoms), -1);
237 for (
int a = 0; a < nMobile; ++a) {
238 local[
static_cast<std::size_t
>(atoms(a))] = a;
241 out.closed.assign(
static_cast<std::size_t
>(nMobile), {});
242 for (
int ia = 0; ia < nMobile; ++ia) {
243 auto &nbhd = out.closed[
static_cast<std::size_t
>(ia)];
245 const int g =
static_cast<int>(atoms(ia));
246 for (
int nb : nbs[
static_cast<std::size_t
>(g)]) {
247 const int lb = local[
static_cast<std::size_t
>(nb)];
252 std::sort(nbhd.begin(), nbhd.end());
253 nbhd.erase(std::unique(nbhd.begin(), nbhd.end()), nbhd.end());
261 std::vector<std::vector<int>> touchers(
static_cast<std::size_t
>(nMobile));
262 for (
int ia = 0; ia < nMobile; ++ia) {
263 for (
int k : out.closed[
static_cast<std::size_t
>(ia)]) {
264 touchers[
static_cast<std::size_t
>(k)].push_back(ia);
267 std::vector<std::vector<int>> adj(
static_cast<std::size_t
>(nMobile));
268 std::vector<int> seen(
static_cast<std::size_t
>(nMobile), 0);
270 for (
int ia = 0; ia < nMobile; ++ia) {
272 auto &row = adj[
static_cast<std::size_t
>(ia)];
273 for (
int k : out.closed[
static_cast<std::size_t
>(ia)]) {
274 for (
int j : touchers[
static_cast<std::size_t
>(k)]) {
278 if (seen[
static_cast<std::size_t
>(j)] == epoch) {
281 seen[
static_cast<std::size_t
>(j)] = epoch;
291 const VectorXi &atoms,
int col,
int atomI,
293 double denom,
const std::vector<int> &owner,
int ia) {
294 const int size =
static_cast<int>(atoms.rows()) * 3;
295 const double massI = matter.
getMass(atomI);
296 for (
int j = 0; j < size; ++j) {
297 const long atomJ = atoms(j / 3);
299 if (owner[
static_cast<std::size_t
>(j / 3)] == ia) {
300 dF = forceA(atomJ, j % 3) - forceB(atomJ, j % 3);
302 hessian(col, j) = -dF / denom;
303 const double effMass = std::sqrt(matter.
getMass(atomJ) * massI);
312 const int n =
static_cast<int>(adj.size());
313 std::vector<int> color(
static_cast<std::size_t
>(n), -1);
314 std::vector<int> used(
static_cast<std::size_t
>(std::max(n, 0)), 0);
316 for (
int v = 0; v < n; ++v) {
318 for (
int u : adj[
static_cast<std::size_t
>(v)]) {
319 if (u < 0 || u >= n || u == v) {
322 const int cu = color[
static_cast<std::size_t
>(u)];
323 if (cu >= 0 && cu < n) {
324 used[
static_cast<std::size_t
>(cu)] = epoch;
328 while (c < n && used[
static_cast<std::size_t
>(c)] == epoch) {
331 color[
static_cast<std::size_t
>(v)] = c;
337 const VectorXi &atoms,
double cutoff) {
338 return buildMobileColoring(matter, atoms, cutoff).color;
342 int nAtoms =
matter->numberOfAtoms();
344 int size =
static_cast<int>(
atoms.rows()) * 3;
345 QUILL_LOG_DEBUG(
log,
"[Hessian] Hessian size: {}\n", size);
351 for (
int a = 0; a <
atoms.rows(); ++a) {
352 const long idx =
atoms(a);
353 if (idx < 0 || idx >= nAtoms) {
355 "[Hessian] atom index {} out of range [0, {}) at list "
356 "entry {}; aborting FD Hessian",
362 double dr =
parameters.main_options().finiteDifference;
363 if (!(dr > 0.0) || !std::isfinite(dr)) {
364 QUILL_LOG_ERROR(
log,
"[Hessian] invalid finiteDifference dr={}\n", dr);
369 const std::string &ckptPath =
parameters.hessian_options().checkpoint_path;
377 bool anyFixed =
false;
378 for (
int i = 0; i < nAtoms; ++i) {
379 if (
matter->getFixed(i)) {
384 const bool netCoupled =
385 parameters.main_options().removeNetForce && nAtoms > 1 && !anyFixed;
389 if (
matter->getPotential()) {
390 range =
matter->getPotential()->hessianRange();
392 if (!netCoupled && ckptPath.empty() && range > 0.0 && std::isfinite(range)) {
401 if (ckptPath.empty() &&
matter->getPotential() &&
402 matter->getPotential()->supportsBatchEvaluation() && size > 1) {
409 const int size =
static_cast<int>(
atoms.rows()) * 3;
415 if (!force0.allFinite()) {
416 QUILL_LOG_ERROR(
log,
"[Hessian] non-finite forces at undisplaced geometry; "
417 "aborting FD Hessian");
422 std::vector<double> steps{1.0};
424 steps.push_back(-1.0);
427 steps.push_back(2.0);
428 steps.push_back(-2.0);
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();
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;
454 for (
auto &m : displaced) {
455 posPtr.push_back(m.getPositions().data());
456 nrsPtr.push_back(nrs.data());
457 frcPtr.push_back(m.forcesData());
458 boxPtr.push_back(box.data());
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(),
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)]);
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)]
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",
490 for (
int j = 0; j < size; j++) {
491 const double effMass = std::sqrt(
matter->getMass(
atoms(j / 3)) *
502 const int nAtoms =
static_cast<int>(
matter->numberOfAtoms());
503 const int nMobile =
static_cast<int>(
atoms.rows());
504 const int size = nMobile * 3;
505 const MobileColoring coloring = buildMobileColoring(*
matter,
atoms, range);
506 if (
static_cast<int>(coloring.color.size()) != nMobile ||
507 static_cast<int>(coloring.closed.size()) != nMobile) {
512 for (
int c : coloring.color) {
516 nColors = std::max(nColors, c + 1);
523 if (nColors >= nMobile) {
527 "[Hessian] hessian-range coloring: {} colors for {} mobile "
531 std::vector<std::vector<int>> members(
static_cast<std::size_t
>(nColors));
532 for (
int ia = 0; ia < nMobile; ++ia) {
533 members[
static_cast<std::size_t
>(
534 coloring.color[
static_cast<std::size_t
>(ia)])]
537 std::vector<std::vector<int>> owner(
538 static_cast<std::size_t
>(nColors),
539 std::vector<int>(
static_cast<std::size_t
>(nMobile), -1));
540 for (
int c = 0; c < nColors; ++c) {
541 for (
int ia : members[
static_cast<std::size_t
>(c)]) {
542 for (
int k : coloring.closed[
static_cast<std::size_t
>(ia)]) {
544 owner[
static_cast<std::size_t
>(c)][
static_cast<std::size_t
>(k)];
545 if (slot >= 0 && slot != ia) {
557 if (!force0.allFinite()) {
558 QUILL_LOG_ERROR(
log,
"[Hessian] non-finite forces at undisplaced geometry; "
559 "aborting FD Hessian");
563 for (
int dir = 0; dir < 3; ++dir) {
564 for (
int c = 0; c < nColors; ++c) {
565 const auto &group = members[
static_cast<std::size_t
>(c)];
569 auto shifted = [&](
double scale,
const char *side) ->
AtomMatrix {
570 posDisplace.setZero();
571 for (
int ia : group) {
572 posDisplace(
atoms(ia), dir) = scale * dr;
576 if (!force.allFinite()) {
578 "[Hessian] non-finite forces for color {} dir {} "
579 "({}); aborting FD Hessian",
585 const AtomMatrix forcePlus = shifted(1.0,
"+");
586 if (forcePlus.size() == 0) {
593 forceMinus = shifted(-1.0,
"-");
594 if (forceMinus.size() == 0) {
599 forcePlus2 = shifted(2.0,
"+2");
600 if (forcePlus2.size() == 0) {
603 forceMinus2 = shifted(-2.0,
"-2");
604 if (forceMinus2.size() == 0) {
609 scheme, dr, force0, forcePlus, forceMinus, forcePlus2, forceMinus2);
610 const AtomMatrix zero = AtomMatrix::Zero(nAtoms, 3);
611 for (
int ia : group) {
612 const int col = ia * 3 + dir;
614 static_cast<int>(
atoms(ia)), slope, zero, 1.0,
615 owner[
static_cast<std::size_t
>(c)], ia);
623 const int nAtoms =
static_cast<int>(
matter->numberOfAtoms());
624 const int size =
static_cast<int>(
atoms.rows()) * 3;
625 const std::string &ckptPath =
parameters.hessian_options().checkpoint_path;
626 const bool wantResume =
627 parameters.hessian_options().resume && !ckptPath.empty();
635 const std::string context =
639 loadColumnCheckpoint(ckptPath, size, startCol,
hessian, context)) {
640 QUILL_LOG_DEBUG(
log,
"[Hessian] resume from column {} / {}\n", startCol,
649 if (!force0.allFinite()) {
650 QUILL_LOG_ERROR(
log,
"[Hessian] non-finite forces at undisplaced geometry; "
651 "aborting FD Hessian");
655 for (
int i = startCol; i < size; i++) {
656 posDisplace.setZero();
657 posDisplace(
atoms(i / 3), i % 3) = dr;
659 posTemp = pos + posDisplace;
662 if (!forcePlus.allFinite()) {
664 "[Hessian] non-finite forces for FD column {} (+); "
665 "aborting FD Hessian",
671 posTemp = pos - posDisplace;
674 if (!forceMinus.allFinite()) {
676 "[Hessian] non-finite forces for FD column {} (-); "
677 "aborting FD Hessian",
685 posDisplace(
atoms(i / 3), i % 3) = 2.0 * dr;
688 if (!forcePlus2.allFinite()) {
690 "[Hessian] non-finite forces for FD column {} (+2); "
691 "aborting FD Hessian",
697 if (!forceMinus2.allFinite()) {
699 "[Hessian] non-finite forces for FD column {} (-2); "
700 "aborting FD Hessian",
707 scheme, dr, force0, forcePlus, forceMinus, forcePlus2, forceMinus2);
708 for (
int j = 0; j < size; j++) {
710 const double effMass = std::sqrt(
matter->getMass(
atoms(j / 3)) *
715 if (!ckptPath.empty()) {
717 if (!saveColumnCheckpoint(ckptPath, size, i + 1,
hessian, context)) {
718 QUILL_LOG_WARNING(
log,
719 "[Hessian] failed to replace checkpoint {} at "
720 "column {}; retaining the last complete file",
729 const std::string &ckptPath =
parameters.hessian_options().checkpoint_path;
732 for (
int i = 0; i < size; i++) {
733 for (
int j = 0; j < i; j++) {
740 QUILL_LOG_ERROR(
log,
"[Hessian] non-finite entries after FD assembly; "
741 "aborting eigen solve");
747 QUILL_LOG_DEBUG(
log,
"[Hessian] writing hessian\n");
751 std::ofstream hessfile(
"hessian.dat", std::ios::out | std::ios::trunc);
753 std::remove(
"hessian.dat");
755 hessfile.open(
"hessian.dat", std::ios::out | std::ios::trunc);
758 QUILL_LOG_ERROR(
log,
"[Hessian] failed to open hessian.dat");
761 hessfile << std::setprecision(std::numeric_limits<double>::max_digits10)
765 QUILL_LOG_ERROR(
log,
"[Hessian] failed to write hessian.dat");
771 if (!ckptPath.empty()) {
772 std::remove(ckptPath.c_str());
777 QUILL_LOG_DEBUG(
log,
"[Hessian] calculating eigen values of the hessian\n");
780 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>;
781 ColMajorXd hessianCol =
hessian;
782 const bool withModes =
parameters.hessian_options().write_modes;
783 Eigen::SelfAdjointEigenSolver<ColMajorXd> es(
785 withModes ? Eigen::ComputeEigenvectors : Eigen::EigenvaluesOnly);
787 QUILL_LOG_DEBUG(
log,
"[Hessian] eigenvalue problem took {:.4e} seconds\n",
789 if (es.info() != Eigen::Success) {
791 "[Hessian] SelfAdjointEigenSolver failed (info={}); "
793 static_cast<int>(es.info()));
796 freqs = es.eigenvalues();
797 if (!
freqs.allFinite()) {
798 QUILL_LOG_ERROR(
log,
"[Hessian] non-finite eigenvalues; aborting");
802 modes = es.eigenvectors();
811 QUILL_LOG_DEBUG(
log,
"[Hessian] removing zero frequency modes");
812 int size =
freqs.size();
813 if (size != 3 *
matter->numberOfAtoms()) {
817 newfreqs.resize(size);
819 for (
int i = 0; i < size; i++) {
820 if (std::abs(
freqs(i)) >
parameters.hessian_options().zero_freq_value) {
821 newfreqs(i - nremoved) =
freqs(i);
828 QUILL_LOG_WARNING(
log,
829 "[Hessian] found {} trivial eigenmodes; a free cluster "
830 "has 6 (5 if linear), a periodic cell 3, and a "
831 "structure with fixed atoms none",
834 return newfreqs.head(size - nremoved);
838 const Eigen::Ref<const VectorXd> &mode) {
840 std::vector<double> out(
static_cast<size_t>(3 * n), 0.0);
841 if (mode.size() != 3 * atoms.size()) {
842 throw std::invalid_argument(
"cartesianMode: mode length is not 3 x atoms");
845 for (
long j = 0; j < mode.size(); ++j) {
846 const long atom = atoms(j / 3);
847 const double mass = matter.
getMass(atom);
849 throw std::invalid_argument(
"cartesianMode: atom without a mass");
851 const double x = mode(j) / std::sqrt(mass);
852 out[
static_cast<size_t>(3 * atom + j % 3)] = x;
856 const double scale = 1.0 / std::sqrt(norm2);
857 for (
double &x : out) {
865 const VectorXd &eigenvalues,
const MatrixXd &modes,
866 const std::string &path) {
867 if (modes.cols() != eigenvalues.size() || modes.rows() != 3 * atoms.size()) {
870 for (
long k = 0; k < eigenvalues.size(); ++k) {
871 const double lambda = eigenvalues(k);
876 meta.
scalars = {{
"mode_eigenvalue", lambda},
888 if (fixedAtoms > 0) {
891 return removed == 3 || removed == 5 || removed == 6;
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, eOnStorageOrder > MatrixXd
Eigen::Matrix< double, 3, 3, eOnStorageOrder > Matrix3d
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
VectorXi getAtomicNrs() const
const AtomMatrix & getPositions() const
bool getPeriodic() const noexcept
long int numberOfAtoms() const
double getMass(long int atom) const
int getFixed(long int atom) const
1 if every Cartesian axis of the atom is fixed, else 0.
const potential_options_t & potential_options() const
const main_options_t & main_options() const
Hessian(const Parameters ¶ms, Matter *matter)
MatrixXd getHessian(Matter *matterIn, const VectorXi &atomsIn)
bool calculateSerial(double dr, FdScheme scheme)
VectorXd removeZeroFreqs(const VectorXd &freqs)
bool calculateBatched(double dr, FdScheme scheme)
const Parameters & parameters
bool finalizeHessian(int size)
bool calculateColored(double range, double dr, FdScheme scheme)
VectorXd getFreqs(Matter *matterIn, const VectorXi &atomsIn)
void setPositions(const AtomMatrix &pos)
long int numberOfAtoms() const
double getMass(long int atom) const
const AtomMatrix & getForces() const
io::IoStatus matter2con(std::string filename, bool append=false, const io::ConFrameMetadata *metadata=nullptr)
RAII wrapper around vesin_neighbors for Matter-style boxes (double[9] row-major 3×3 cell,...
bool atomicWriteFile(const std::string &path, Writer writer)
void getTime(double *real, double *user, double *sys)
constexpr bool io_ok(IoStatus s) noexcept
constexpr long derivative
constexpr double safe_div(double num, double denom, double fallback=0.0)
constexpr double kHbar
hbar in eV^0.5 amu^0.5 Angstrom, correctly rounded from the exact SI values (h = 6....
RAII resource manager for the ARTn C library with global synchronization.
FdScheme
Real finite-difference scheme for the assembled Hessian and for Lanczos/Davidson Hessian-vector produ...
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.
constexpr double kEvToWavenumber
1 eV in cm^-1, e / (h c) from the exact SI values.
FdScheme parseFdScheme(std::string_view scheme)
bool writeNormalModes(Matter &matter, const VectorXi &atoms, const VectorXd &eigenvalues, const MatrixXd &modes, const std::string &path)
Writes one frame of matter per mode to path, the mode as the displacements section and mode_eigenvalu...
std::vector< CutoffPair > periodicCutoffNeighbors(const double *positions, size_t atoms, const double *cell, double cutoff, bool full)
bool trivialModeCountIsPhysical(long removed, long fixedAtoms)
Whether removed zero-frequency modes are what the structure's symmetries give: 6 for a free cluster (...
std::vector< int > greedyColorCutoffGraph(const std::vector< std::vector< int > > &adj)
Greedy coloring of an undirected graph.
std::vector< int > colorMobileCutoffGraph(const Matter &matter, const VectorXi &atoms, double cutoff)
Conflict graph on atoms: two mobile atoms share an edge when their closed cutoff neighborhoods inters...
std::vector< double > cartesianMode(const Matter &matter, const VectorXi &atoms, const Eigen::Ref< const VectorXd > &mode)
Cartesian displacement of every atom along one mass-weighted mode over the mobile degrees of freedom ...