Loading...
Searching...
No Matches
ReferenceHessian.h
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3** SPDX-License-Identifier: BSD-3-Clause
4** Copyright (c) 2010--present, eOn Development Team
5*/
6#pragma once
7
8#include "eon/ConFileIO.h"
9#include "eon/Matter.h"
10
11#include <algorithm>
12#include <cmath>
13#include <fstream>
14#include <limits>
15#include <stdexcept>
16#include <string>
17
18namespace eonc::tunneling {
19
22inline MatrixXd readReferenceHessian(const std::string &filename,
23 const Matter &reference, long dimension) {
24 if (dimension <= 0 || reference.numberOfAtoms() <= 0) {
25 throw std::invalid_argument(
26 "instanton: empty reference Hessian coordinates");
27 }
28 Matter recorded(reference);
29 if (!io::io_ok(recorded.con2matter(filename + ".con"))) {
30 throw std::runtime_error("instanton: cannot read Hessian geometry " +
31 filename + ".con");
32 }
33 auto mismatch = [&]() {
34 throw std::invalid_argument(
35 "instanton: Hessian reference geometry or mass metric differs: " +
36 filename);
37 };
38 if (recorded.numberOfAtoms() != reference.numberOfAtoms() ||
39 recorded.getPeriodic() != reference.getPeriodic() ||
40 !recorded.getCell().allFinite() || !reference.getCell().allFinite() ||
41 (recorded.getCell() - reference.getCell()).cwiseAbs().maxCoeff() > 1e-8) {
42 mismatch();
43 }
44 const AtomMatrix delta =
45 reference.pbc(recorded.getPositions() - reference.getPositions());
46 if (!delta.allFinite() || delta.cwiseAbs().maxCoeff() > 1e-8) {
47 mismatch();
48 }
49 long coordinates = 0;
50 for (long i = 0; i < reference.numberOfAtoms(); ++i) {
51 if (recorded.getAtomicNr(i) != reference.getAtomicNr(i) ||
52 recorded.getAtomIndex(i) != reference.getAtomIndex(i) ||
53 recorded.getFixedMask(i) != reference.getFixedMask(i) ||
54 recorded.getMass(i) != reference.getMass(i) ||
55 !(recorded.getMass(i) > 0.0) || !std::isfinite(recorded.getMass(i))) {
56 mismatch();
57 }
58 if (!reference.getFixed(i)) {
59 coordinates += 3;
60 }
61 }
62 if (dimension <= 0 || dimension != coordinates) {
63 throw std::invalid_argument(
64 "instanton: Hessian coordinate dimension differs: " + filename);
65 }
66 std::ifstream stream(filename);
67 MatrixXd matrix(dimension, dimension);
68 for (long i = 0; i < dimension; ++i) {
69 for (long j = 0; j < dimension; ++j) {
70 if (!(stream >> matrix(i, j)) || !std::isfinite(matrix(i, j))) {
71 throw std::runtime_error(
72 "instanton: incomplete or non-finite reference Hessian: " +
73 filename);
74 }
75 }
76 }
77 std::string extra;
78 if (stream >> extra) {
79 throw std::runtime_error("instanton: extra reference Hessian data: " +
80 filename);
81 }
82 const double scale = std::max(1.0, matrix.cwiseAbs().maxCoeff());
83 if ((matrix - matrix.transpose()).cwiseAbs().maxCoeff() >
84 128.0 * std::numeric_limits<double>::epsilon() * scale) {
85 throw std::invalid_argument("instanton: reference Hessian is asymmetric: " +
86 filename);
87 }
88 return (0.5 * (matrix + matrix.transpose())).eval();
89}
90
91} // namespace eonc::tunneling
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
const AtomMatrix & getPositions() const
Definition Matter.cpp:378
bool getPeriodic() const noexcept
Definition Matter.h:338
Matrix3d getCell() const
Definition Matter.cpp:345
long int numberOfAtoms() const
Definition Matter.cpp:343
AtomMatrix pbc(const AtomMatrix &diff) const
Definition Matter.cpp:943
double getMass(long int atom) const
Definition Matter.cpp:548
long getAtomicNr(long int atom) const
Definition Matter.cpp:565
std::array< bool, 3 > getFixedMask(long int atom) const
Per-axis CON column-4 mask (bit0=x, bit1=y, bit2=z).
Definition Matter.cpp:590
std::int64_t getAtomIndex(long int atom) const
.con column-5 index (pre-grouping); public for I/O / bindings.
Definition Matter.cpp:959
int getFixed(long int atom) const
1 if every Cartesian axis of the atom is fixed, else 0.
Definition Matter.cpp:575
io::IoStatus con2matter(std::string filename)
Definition Matter.h:281
constexpr bool io_ok(IoStatus s) noexcept
Definition ConFileIO.h:38
MatrixXd readReferenceHessian(const std::string &filename, const Matter &reference, long dimension)
Read a mass-weighted reference matrix bound to filename + ".con".