Loading...
Searching...
No Matches
PhysicalCurvatureBasis.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4#pragma once
5
6#include <Eigen/Geometry>
7#include <Eigen/QR>
8#include <cmath>
9#include <limits>
10#include <stdexcept>
11
14#include "../dimer/Dimer.h"
15#include "VibrationalStencil.h"
16
17namespace gpr::optim {
18
20
24inline Eigen::VectorXd movingAtomicMasses(const gpr::AtomsConfiguration& config,
25 Eigen::Index n_moving)
26{
27 if (n_moving < 0)
28 throw std::invalid_argument("moving atom count must be nonnegative");
29 const Eigen::Index atoms = config.is_frozen.size();
30 if (atoms > 0) {
31 if (config.is_frozen.rows() != 1)
32 throw std::invalid_argument("mobility flags require one row");
33 Eigen::Index moving = 0;
34 for (Eigen::Index atom = 0; atom < atoms; ++atom) {
35 const auto flag = config.is_frozen(0, atom);
36 if (flag != MOVING_ATOM && flag != FROZEN_ATOM)
37 throw std::invalid_argument("invalid atom mobility flag");
38 if (flag == MOVING_ATOM) ++moving;
39 }
40 if (moving != n_moving)
41 throw std::invalid_argument(
42 "mobility flags and moving coordinates differ");
43 }
44 const auto& supplied = config.atomic_masses;
45 if (supplied.size() == 0) return Eigen::VectorXd::Ones(n_moving);
46 if (supplied.size() != (atoms > 0 ? atoms : n_moving) ||
47 !supplied.allFinite() || (supplied.array() <= 0.0).any())
48 throw std::invalid_argument(
49 "physical curvature requires one positive finite mass per input "
50 "atom");
51 if (atoms == 0) return supplied;
52 Eigen::VectorXd masses(n_moving);
53 Eigen::Index moving = 0;
54 for (Eigen::Index atom = 0; atom < atoms; ++atom)
55 if (config.is_frozen(0, atom) == MOVING_ATOM)
56 masses(moving++) = supplied(atom);
57 return masses;
58}
59
60namespace detail {
61
64inline Eigen::MatrixXd rigidMotionComplement(const gpr::Coord& positions,
65 const Eigen::VectorXd& masses,
66 bool exclude_rotations)
67{
68 const Eigen::Index dimension = positions.cols();
69 Eigen::MatrixXd excluded =
70 dimer::rigid_motion_basis(positions, exclude_rotations);
71 if (masses.size() > 0) {
72 if (masses.size() != dimension / 3 || !masses.allFinite() ||
73 (masses.array() <= 0.0).any())
74 throw std::invalid_argument(
75 "physical curvature requires one positive finite mass per "
76 "atom");
77 for (Eigen::Index atom = 0; atom < masses.size(); ++atom)
78 excluded.middleRows(3 * atom, 3) *= masses(atom);
79 }
80
81 const Eigen::Index rigid = excluded.cols();
82 // A single atom is spanned by rigid motion, so the vibrational block
83 // has no columns and no storage. bottomRows on that matrix offsets null.
84 if (rigid >= dimension) return Eigen::MatrixXd(dimension, 0);
85 const Eigen::Index physical_dimension = dimension - rigid;
86 Eigen::MatrixXd basis =
87 Eigen::MatrixXd::Zero(dimension, physical_dimension);
88 basis.bottomRows(physical_dimension).setIdentity();
89 if (excluded.cols() > 0) {
90 const Eigen::HouseholderQR<Eigen::MatrixXd> qr(excluded);
91 basis = qr.householderQ() * basis;
92 }
93 return basis;
94}
95
96} // namespace detail
97
102inline Eigen::MatrixXd physicalCurvatureBasis(
103 const gpr::Coord& positions, const gpr::AtomsConfiguration& config,
104 bool exclude_rotations = true,
106{
107 const Eigen::Index dimension = positions.cols();
108 const bool anchored = config.atoms_froz_active.positions.size() > 0 ||
109 config.atoms_froz_inactive.positions.size() > 0 ||
110 (config.is_frozen.array() == FROZEN_ATOM).any();
111 if (anchored || positions.rows() != 1 || dimension % 3 != 0)
112 return Eigen::MatrixXd::Identity(dimension, dimension);
113 return detail::rigidMotionComplement(
114 positions,
116 : Eigen::VectorXd(),
117 exclude_rotations && !gpr::geometry::cellIsMicLattice(config.cell));
118}
119
130inline Eigen::MatrixXd vibrationalStencilDirections(
131 const Eigen::VectorXd& positions, const Eigen::VectorXd& masses,
132 bool periodic, bool anchored, const Eigen::VectorXi* frozen = nullptr)
133{
134 const Eigen::Index dimension = positions.size();
135 if (frozen != nullptr && frozen->size() * 3 == dimension) {
136 const Eigen::Index moving = (frozen->array() == 0).count();
137 Eigen::MatrixXd axes = Eigen::MatrixXd::Zero(dimension, 3 * moving);
138 Eigen::Index column = 0;
139 for (Eigen::Index atom = 0; atom < frozen->size(); ++atom) {
140 if ((*frozen)(atom) != 0) continue;
141 for (int axis = 0; axis < 3; ++axis)
142 axes(3 * atom + axis, column++) = 1.0;
143 }
144 return axes;
145 }
146 if (anchored || dimension == 3 || dimension % 3 != 0)
147 return Eigen::MatrixXd::Identity(dimension, dimension);
148 return detail::rigidMotionComplement(positions.transpose(), masses,
149 !periodic);
150}
151
156inline Eigen::MatrixXd stencilDirectionsForMode(
157 VibrationalStencil mode, const Eigen::VectorXd& positions,
158 const Eigen::VectorXd& masses, bool periodic, bool anchored,
159 const Eigen::VectorXi* frozen = nullptr)
160{
162 return vibrationalStencilDirections(positions, masses, periodic,
163 anchored, frozen);
164 return Eigen::MatrixXd::Identity(positions.size(), positions.size());
165}
166
171inline bool directionsAreCoordinateAxes(const Eigen::MatrixXd& directions)
172{
173 for (Eigen::Index k = 0; k < directions.cols(); ++k) {
174 Eigen::Index ones = 0;
175 for (Eigen::Index i = 0; i < directions.rows(); ++i) {
176 const double value = directions(i, k);
177 if (value == 1.0)
178 ++ones;
179 else if (value != 0.0)
180 return false;
181 }
182 if (ones != 1) return false;
183 }
184 return directions.cols() > 0 &&
185 (directions.transpose() * directions).isIdentity(0.0);
186}
187
191inline Eigen::MatrixXd centralDifferencePoints(
192 const Eigen::VectorXd& center, double step,
193 const Eigen::MatrixXd& directions)
194{
195 Eigen::MatrixXd points(center.size(), 2 * directions.cols());
196 for (Eigen::Index k = 0; k < directions.cols(); ++k) {
197 points.col(2 * k) = center + step * directions.col(k);
198 points.col(2 * k + 1) = center - step * directions.col(k);
199 }
200 return points;
201}
202
208 const gpr::Coord& positions, const gpr::AtomsConfiguration& config,
209 bool exclude_rotations = true)
210{
211 const Eigen::Index dimension = positions.cols();
212 if ((positions.rows() != 1 && !(positions.rows() == 0 && dimension == 0)) ||
213 dimension % 3 != 0 || !positions.allFinite())
214 throw std::invalid_argument(
215 "finite packed Cartesian geometry required");
216 const Eigen::VectorXd masses = movingAtomicMasses(config, dimension / 3);
217 if (dimension == 0) return Eigen::MatrixXd(0, 0);
218 Eigen::MatrixXd basis = physicalCurvatureBasis(
219 positions, config, exclude_rotations, RigidMotionMetric::MassWeighted);
220 if (basis.cols() == 0) return Eigen::MatrixXd(dimension, 0);
221 if (basis.cols() == dimension)
222 return Eigen::MatrixXd::Identity(dimension, dimension);
223 for (Eigen::Index atom = 0; atom < masses.size(); ++atom)
224 basis.middleRows(3 * atom, 3) *= std::sqrt(masses(atom));
225 const Eigen::HouseholderQR<Eigen::MatrixXd> qr(basis);
226 return qr.householderQ() *
227 Eigen::MatrixXd::Identity(dimension, basis.cols());
228}
229
232inline Eigen::MatrixXd transportPhysicalDirections(
233 const Eigen::MatrixXd& directions, const Eigen::MatrixXd& physical)
234{
235 if (directions.rows() != physical.rows() || directions.cols() == 0 ||
236 directions.cols() > physical.cols() || !directions.allFinite())
237 return Eigen::MatrixXd(physical.rows(), 0);
238 const Eigen::MatrixXd projected =
239 physical * (physical.transpose() * directions);
240 Eigen::ColPivHouseholderQR<Eigen::MatrixXd> qr(projected);
241 qr.setThreshold(std::sqrt(std::numeric_limits<double>::epsilon()));
242 if (qr.rank() != directions.cols())
243 return Eigen::MatrixXd(physical.rows(), 0);
244 // Pivoting establishes rank but cannot choose the returned column order:
245 // the leading direction carries the dimer axis and the second its escape.
246 const Eigen::HouseholderQR<Eigen::MatrixXd> ordered(projected);
247 return ordered.householderQ() *
248 Eigen::MatrixXd::Identity(physical.rows(), directions.cols());
249}
250
256inline Eigen::MatrixXd transportMeasuredHessianActions(
257 const gpr::Coord& positions, const gpr::Coord& gradient,
258 const Eigen::MatrixXd& measured_basis,
259 const Eigen::MatrixXd& measured_action, const Eigen::MatrixXd& physical,
260 bool include_rotations = true)
261{
262 const Eigen::Index dimension = positions.cols();
263 const Eigen::Index columns = measured_basis.cols();
264 if (positions.rows() != 1 || gradient.rows() != 1 || dimension == 0 ||
265 dimension % 3 != 0 || gradient.cols() != dimension || columns == 0 ||
266 measured_basis.rows() != dimension ||
267 measured_action.rows() != dimension ||
268 measured_action.cols() != columns || physical.rows() != dimension ||
269 !positions.allFinite() || !gradient.allFinite() ||
270 !measured_basis.allFinite() || !measured_action.allFinite() ||
271 !physical.allFinite())
272 return {};
273 const Eigen::Index rigid =
274 columns == dimension ? 0 : (include_rotations ? 6 : 3);
275 Eigen::MatrixXd directions =
276 Eigen::MatrixXd::Zero(dimension, columns + rigid);
277 Eigen::MatrixXd actions = directions;
278 directions.leftCols(columns) = measured_basis;
279 actions.leftCols(columns) = measured_action;
280 if (rigid > 0) {
281 Eigen::Vector3d center = Eigen::Vector3d::Zero();
282 for (Eigen::Index atom = 0; atom < dimension / 3; ++atom)
283 center += positions.row(0).segment<3>(3 * atom).transpose();
284 center /= dimension / 3;
285 for (Eigen::Index atom = 0; atom < dimension / 3; ++atom) {
286 const Eigen::Vector3d r =
287 positions.row(0).segment<3>(3 * atom).transpose() - center;
288 const Eigen::Vector3d g =
289 gradient.row(0).segment<3>(3 * atom).transpose();
290 for (Eigen::Index axis = 0; axis < 3; ++axis) {
291 directions(3 * atom + axis, columns + axis) = 1.0;
292 if (rigid == 6) {
293 const Eigen::Vector3d generator =
294 Eigen::Vector3d::Unit(axis);
295 directions.block<3, 1>(3 * atom, columns + 3 + axis) =
296 generator.cross(r);
297 actions.block<3, 1>(3 * atom, columns + 3 + axis) =
298 generator.cross(g);
299 }
300 }
301 }
302 for (Eigen::Index j = columns; j < directions.cols(); ++j) {
303 const double norm = directions.col(j).stableNorm();
304 if (norm > 0.0) {
305 directions.col(j) /= norm;
306 actions.col(j) /= norm;
307 }
308 }
309 }
310 const Eigen::ColPivHouseholderQR<Eigen::MatrixXd> decomposition(directions);
311 if (decomposition.rank() != dimension) return {};
312 const Eigen::MatrixXd coefficients = decomposition.solve(physical);
313 const double roundoff =
314 64.0 * std::numeric_limits<double>::epsilon() * dimension;
315 if (!coefficients.allFinite() ||
316 !(directions * coefficients).isApprox(physical, roundoff))
317 return {};
318 return physical * (physical.transpose() * (actions * coefficients));
319}
320
334inline Eigen::MatrixXd hessianColumnsFromDirections(
335 const Eigen::VectorXd& center, const Eigen::VectorXd& center_gradient,
336 const Eigen::MatrixXd& directions, const Eigen::MatrixXd& actions)
337{
338 const Eigen::Index dimension = center.size();
339 if (dimension == 0 || directions.rows() != dimension ||
340 directions.cols() == 0 || directions.cols() > dimension ||
341 actions.rows() != dimension || actions.cols() != directions.cols() ||
342 !actions.allFinite())
343 return {};
344 if (directions.cols() == dimension) {
345 if (directions.isIdentity(0.0)) return actions;
346 return actions * directions.transpose();
347 }
348 if (directionsAreCoordinateAxes(directions))
349 return directions * (directions.transpose() * actions) *
350 directions.transpose();
351 if (center_gradient.size() != dimension) return {};
353 center.transpose(), center_gradient.transpose(), directions, actions,
354 Eigen::MatrixXd::Identity(dimension, dimension),
355 dimension - directions.cols() > 3);
356}
357
358} // namespace gpr::optim
@ FROZEN_ATOM
Definition Enums.h:36
@ MOVING_ATOM
Definition Enums.h:35
Eigen::MatrixXd rigid_motion_basis(const gpr::Coord &R, bool rotations)
Build an orthonormal basis for rigid-body translations and rotations.
Definition Dimer.cpp:30
bool cellIsMicLattice(const double cell[9]) noexcept
Nonzero ASE 3x3 that is not the 25 A vacuum convention.
Definition MinImage.h:56
Eigen::MatrixXd hessianColumnsFromDirections(const Eigen::VectorXd &center, const Eigen::VectorXd &center_gradient, const Eigen::MatrixXd &directions, const Eigen::MatrixXd &actions)
Eigen::MatrixXd physicalCurvatureBasis(const gpr::Coord &positions, const gpr::AtomsConfiguration &config, bool exclude_rotations=true, RigidMotionMetric metric=RigidMotionMetric::MassWeighted)
Eigen::MatrixXd massWeightedPhysicalCurvatureBasis(const gpr::Coord &positions, const gpr::AtomsConfiguration &config, bool exclude_rotations=true)
bool directionsAreCoordinateAxes(const Eigen::MatrixXd &directions)
Eigen::VectorXd movingAtomicMasses(const gpr::AtomsConfiguration &config, Eigen::Index n_moving)
Eigen::MatrixXd transportPhysicalDirections(const Eigen::MatrixXd &directions, const Eigen::MatrixXd &physical)
Eigen::MatrixXd transportMeasuredHessianActions(const gpr::Coord &positions, const gpr::Coord &gradient, const Eigen::MatrixXd &measured_basis, const Eigen::MatrixXd &measured_action, const Eigen::MatrixXd &physical, bool include_rotations=true)
Eigen::MatrixXd stencilDirectionsForMode(VibrationalStencil mode, const Eigen::VectorXd &positions, const Eigen::VectorXd &masses, bool periodic, bool anchored, const Eigen::VectorXi *frozen=nullptr)
Eigen::MatrixXd centralDifferencePoints(const Eigen::VectorXd &center, double step, const Eigen::MatrixXd &directions)
Eigen::MatrixXd vibrationalStencilDirections(const Eigen::VectorXd &positions, const Eigen::VectorXd &masses, bool periodic, bool anchored, const Eigen::VectorXi *frozen=nullptr)
FieldMatrixd Coord
Definition Macros.h:35
Stores configurations of all atoms in the system.
Definition Structures.h:832
AtomsPositionAndType atoms_froz_inactive
Info on inactive frozen atoms.
Definition Structures.h:843
FieldMatrixU8 is_frozen
Indicates whether atom is frozen or not.
Definition Structures.h:834
AtomsPositionAndType atoms_froz_active
Info on active frozen atoms.
Definition Structures.h:841
Eigen::VectorXd atomic_masses
Per-atom input masses; empty selects unit masses.
Definition Structures.h:838