35 if (num_atoms == 0 || R.rows() != 1 || R.cols() % 3 != 0) {
36 return Eigen::MatrixXd(R.cols(), 0);
41 for (
Index_t i = 0; i < num_atoms; ++i) {
44 com /=
static_cast<double>(num_atoms);
47 std::vector<Eigen::VectorXd> basis;
51 for (
int i = 0; i < 3; ++i) {
52 Eigen::VectorXd t = Eigen::VectorXd::Zero(dof);
53 for (
Index_t j = 0; j < num_atoms; ++j) {
60 Eigen::VectorXd r_x = Eigen::VectorXd::Zero(dof);
61 Eigen::VectorXd r_y = Eigen::VectorXd::Zero(dof);
62 Eigen::VectorXd r_z = Eigen::VectorXd::Zero(dof);
64 for (
Index_t i = 0; i < num_atoms; ++i) {
67 r_x(3 * i + 1) = -pos(2);
68 r_x(3 * i + 2) = pos(1);
70 r_y(3 * i + 0) = pos(2);
71 r_y(3 * i + 2) = -pos(0);
73 r_z(3 * i + 0) = -pos(1);
74 r_z(3 * i + 1) = pos(0);
90 for (
Index_t i = 0; i < num_atoms; ++i)
92 const double resolution = std::numeric_limits<double>::epsilon() *
93 static_cast<double>(dof) *
94 std::max(std::sqrt(extent), 1.0);
95 std::vector<Eigen::VectorXd> ortho_basis;
96 ortho_basis.reserve(6);
98 for (
auto& v: basis) {
99 Eigen::VectorXd u = v;
100 for (
int pass = 0; pass < 2; ++pass)
101 for (
const auto& ou: ortho_basis) {
104 if (u.norm() > resolution) {
106 ortho_basis.push_back(u);
110 Eigen::MatrixXd result(dof,
static_cast<Eigen::Index
>(ortho_basis.size()));
111 for (Eigen::Index i = 0; i < result.cols(); ++i)
112 result.col(i) = ortho_basis[
static_cast<std::size_t
>(i)];
117 Eigen::VectorXd& step)
120 if (basis.rows() != step.size())
121 throw std::invalid_argument(
122 "rigid projection position/step dimensions");
123 const Eigen::VectorXd removed = basis * (basis.transpose() * step);
125 return removed.norm();
137 translateDimerForNegCurv(orient, F, param_trans, transinfo, R_new);
139 translateDimerForPosCurv(orient, F, param_trans, transinfo, R_new);
143void Dimer::translateDimerForNegCurv(
167 rgmin_trans_ !=
nullptr ? *rgmin_trans_ : transient;
168 const Eigen::Index n = F_trans.size();
169 Eigen::VectorXd
x = Eigen::Map<const Eigen::VectorXd>(R.data(), n);
170 const Eigen::VectorXd x_before =
x;
171 const Eigen::VectorXd g =
172 -Eigen::Map<const Eigen::VectorXd>(F_trans.data(), n);
179 Eigen::VectorXd r_final = x_before -
x;
181 if (!this->is_subsystem) {
185 r_final -= basis * (basis.transpose() * r_final);
187 Eigen::Map<Eigen::VectorXd>(R.data(), R.size()) -= r_final;
190void Dimer::translateDimerForPosCurv(
198 F_trans.resize(1, R.cols());
203 const double f_norm = F_trans.norm();
204 if (rgmin_trans_ !=
nullptr) rgmin_trans_->
forget();
205 if (!(f_norm > 0.0) || !std::isfinite(f_norm))
return;
206 orient_search = F_trans / f_norm;
211 const gpr::Coord& G01,
const uint8_t potential,
212 const double T_anglerot,
const bool estim_Curv,
220 return candidate.G.rows() == 1 && candidate.G.cols() == G01.cols() &&
221 candidate.G.allFinite() && candidate.E.rows() >= 1 &&
222 candidate.E.cols() >= 1 && candidate.E.allFinite();
225 image1_evaluator, rotinfo, orient_new, Curv,
236 orient_new, Curv, image1, hessian_evaluator);
245 orient_new, Curv, image1, {});
254 if (R.rows() != 1 || R.cols() == 0 || orient.rows() != 1 ||
255 G01.rows() < 2 || G01.cols() != R.cols() || orient.cols() != R.cols() ||
256 !R.allFinite() || !(dimer_sep > 0.0) || !std::isfinite(dimer_sep)) {
259 const Eigen::Index n = R.cols();
260 Eigen::VectorXd seed = orient.row(0).transpose();
261 if (rotation_basis_.rows() > 0) {
262 if (rotation_basis_.rows() != n || !rotation_basis_.allFinite())
264 seed = rotation_basis_ * (rotation_basis_.transpose() * seed);
266 const double seed_norm = seed.norm();
267 if (!(seed_norm > 0.0) || !std::isfinite(seed_norm)) {
271 const Eigen::VectorXd g0 = G01.row(0).transpose();
276 const Eigen::VectorXd& gradient;
279 const Eigen::MatrixXd& physical;
283 R, g0, image1_evaluator, hessian_evaluator, rotation_basis_, dimer_sep};
284 const auto action = [](
void*
user,
const DLManagedTensorVersioned*,
285 const DLManagedTensorVersioned* direction,
286 DLManagedTensorVersioned* output)
noexcept {
287 auto& ctx = *
static_cast<Context*
>(
user);
288 if (ctx.failed)
return RGMIN_INTERNAL_ERROR;
290 const Eigen::Index size = ctx.center.cols();
291 const auto* vp =
reinterpret_cast<const double*
>(
292 static_cast<const uint8_t*
>(direction->dl_tensor.data) +
293 direction->dl_tensor.byte_offset);
294 auto* hp =
reinterpret_cast<double*
>(
295 static_cast<uint8_t*
>(output->dl_tensor.data) +
296 output->dl_tensor.byte_offset);
297 const Eigen::Map<const Eigen::VectorXd> v(vp, size);
298 Eigen::Map<Eigen::VectorXd> hv(hp, size);
300 Eigen::VectorXd value;
301 if (!ctx.hessian(v, value) || value.size() != size ||
302 !value.allFinite()) {
304 return RGMIN_INTERNAL_ERROR;
309 trial.
R = ctx.center;
310 trial.
R.row(0) += ctx.separation * v.transpose();
311 if (!ctx.evaluate(trial) || trial.
G.rows() != 1 ||
312 trial.
G.cols() != size || !trial.
G.allFinite()) {
314 return RGMIN_INTERNAL_ERROR;
316 hv = (trial.
G.row(0).transpose() - ctx.gradient) /
319 if (ctx.physical.rows() > 0)
320 hv = (ctx.physical * (ctx.physical.transpose() * hv)).eval();
321 if (!hv.allFinite()) {
323 return RGMIN_INTERNAL_ERROR;
325 return RGMIN_SUCCESS;
328 return RGMIN_INTERNAL_ERROR;
331 Eigen::VectorXd center = R.row(0).transpose();
332 Eigen::VectorXd mode(n);
334 std::unique_ptr<DLManagedTensorVersioned,
decltype(&rgmin_tensor_free)>;
336 Tensor xt(rgmin_tensor_borrow_cpu_f64(center.data(), n), rgmin_tensor_free);
337 Tensor st(rgmin_tensor_borrow_cpu_f64(seed.data(), n), rgmin_tensor_free);
338 Tensor mt(rgmin_tensor_borrow_cpu_f64(mode.data(), n), rgmin_tensor_free);
342 const rgmin_eigen_params_t params{RGMIN_EIGEN_DIMER, 1, 2, 1, 1e-8};
343 rgmin_lowest_mode_t result{};
344 const auto status = rgmin_lowest_eigenpair(
345 action, &context, xt.get(), st.get(), mt.get(), ¶ms, &result);
346 if (
status != RGMIN_SUCCESS || context.failed || !mode.allFinite() ||
347 !(mode.norm() > 0.0)) {
351 if (mode.dot(seed) < 0.0) mode *= -1.0;
357 endpoint.
R.row(0) += dimer_sep * mode.transpose();
358 if (!image1_evaluator(endpoint) || endpoint.
G.rows() != 1 ||
359 endpoint.
G.cols() != n || !endpoint.
G.allFinite() ||
360 endpoint.
E.rows() != 1 || endpoint.
E.cols() != 1 ||
361 !endpoint.
E.allFinite()) {
364 const double curvature =
367 : (endpoint.
G.row(0).transpose() - g0).dot(mode) / dimer_sep;
369 orient_new.resize(1, n);
370 orient_new.row(0) = mode.transpose();
371 Curv = estim_Curv ? curvature : 0.0;
372 image1 = std::move(endpoint);
378 [[maybe_unused]]
const double T_anglerot,
const bool estim_Curv,
383 if (R.rows() != 1 || orient.rows() != 1 || orient.cols() != R.cols() ||
384 G01.rows() < 2 || G01.cols() != R.cols() || !R.allFinite() ||
385 !G01.allFinite() || !(dimer_sep > 0.0) || !std::isfinite(dimer_sep))
387 Eigen::MatrixXd physical = rotation_basis_;
388 if (physical.rows() == 0)
389 physical = Eigen::MatrixXd::Identity(R.cols(), R.cols());
396 if (!image1_evaluator(endpoint))
return false;
397 if (seed_endpoint.
R.size() == 0) seed_endpoint = endpoint;
402 orient_new, Curv, image1);
406 R, G01.row(0).transpose(), dimer_sep, physical,
413 R, orient, G01.row(0).transpose(), dimer_sep, physical,
415 image1_evaluator, rotation_observation_, orient_new, image1))
418 ? (image1.
G.row(0) - G01.row(0)).dot(orient_new.row(0)) /
425 orient_new, Curv, image1);
437 F_rot.resize(1, G01.cols());
452 force_variance_.size() ==
static_cast<Eigen::Index
>(D)) {
453 const Eigen::VectorXd axis = orient.row(0).transpose();
455 F1.row(0).transpose(), axis, force_variance_)
458 F2.row(0).transpose(), axis, force_variance_)
465 F_rot = (F1_rot - F2_rot) / dimer_sep;
466 if (rotation_basis_.rows() > 0) {
467 if (rotation_basis_.rows() != D || !rotation_basis_.allFinite())
468 throw std::invalid_argument(
"invalid dimer rotation basis");
471 F_rot = (rotation_basis_ *
472 (rotation_basis_.transpose() * F_rot.row(0).transpose()))
487 if (G01.rows() < 2 || G01.cols() != orient.cols() || !(dimer_sep > 0.0) ||
488 !std::isfinite(dimer_sep)) {
489 return std::numeric_limits<double>::quiet_NaN();
495 C += (G01(1, n) - G01(0, n)) * orient(0, n);
510 gpr::assertMsg(gpr_model !=
nullptr,
511 "Error! NULL pointer is passed!");
525 image1.
E.resize(1, 1);
526 image1.
G.resize(1, 2 * 3);
528 image1.
E(0, 0) = 140.981934401792e-006;
530 image1.
G(0, 0) = 22.0169292639464e-003;
531 image1.
G(0, 1) = -25.3799869163130e-003;
532 image1.
G(0, 2) = 20.4383773983493e-003;
533 image1.
G(0, 3) = -17.5501833843735e-003;
534 image1.
G(0, 4) = 21.9491835909580e-003;
535 image1.
G(0, 5) = -28.6553826196841e-003;
541 image1.
E.resize(1, 1);
542 image1.
G.resize(1, 2 * 3);
544 image1.
E(0, 0) = 268.422097239501e-006;
546 image1.
G(0, 0) = 21.4263025463240e-003;
547 image1.
G(0, 1) = -30.4813824740716e-003;
548 image1.
G(0, 2) = 26.0833530773849e-003;
549 image1.
G(0, 3) = -16.2806875633080e-003;
550 image1.
G(0, 4) = 21.0852193621926e-003;
551 image1.
G(0, 5) = -35.3650094447835e-003;
556 err <<
"Error! The type of the potential is unknown!";
566 double F_0 = F_rot_oriented.norm();
569 return 0.5 * atan(0.5 * F_0 / fabs(C_0));
RotationStepResult rotateWithHessian(const gpr::Coord &R, const gpr::Coord &orient, const gpr::Coord &G01, const bool estim_Curv, const Image1Evaluator &image1_evaluator, gpr::Coord &orient_new, double &Curv, gpr::Observation &image1, const HessianVectorEvaluator &hessian_evaluator)
bool hasForceVariance() const
void rotateForce(const gpr::Coord &G01, const gpr::Coord &orient, gpr::Coord &F_rot)
Calculate rotational force acting on image 1 of the dimer.
void translate(const gpr::Coord &R, const gpr::Coord &orient, const gpr::Coord &F, const double curv, const gpr::TransitionParameters ¶m_trans, gpr::LBFGSInfo &transinfo, gpr::Coord &R_new)
Translate dimer.
void translateWithForce(const gpr::Coord &R, const gpr::Coord &force, const gpr::TransitionParameters ¶meters, gpr::LBFGSInfo &history, gpr::Coord &output)
RotationStepResult rotateWithImage1Evaluator(const gpr::Coord &R, const gpr::Coord &orient, const gpr::Coord &G01, const double T_anglerot, const bool estim_Curv, const Image1Evaluator &image1_evaluator, gpr::LBFGSInfo &rotinfo, gpr::Coord &orient_new, double &Curv, gpr::Observation &image1)
RotationStepResult rotateWithRgminDimer(const gpr::Coord &R, const gpr::Coord &orient, const gpr::Coord &G01, const bool estim_Curv, const Image1Evaluator &image1_evaluator, gpr::Coord &orient_new, double &Curv, gpr::Observation &image1)
std::function< bool(gpr::Observation &)> Image1Evaluator
void rotate(const gpr::Coord &R, const gpr::Coord &orient, const gpr::Coord &G01, const uint8_t potential, const double T_anglerot, const bool estim_Curv, const gpr::Observation &all_obs, gpr::GaussianProcessRegression *gpr_model, gpr::LBFGSInfo &rotinfo, gpr::Coord &orient_new, double &Curv, gpr::Observation &image1)
Rotate dimer.
void calculatePotential(const uint8_t potential, gpr::GaussianProcessRegression *gpr_model, gpr::Observation &image1)
Calculate potential.
void translateForce(const gpr::Coord &F, const gpr::Coord &orient, gpr::Coord &F_trans)
Calculate translational force acting on image 1 of the dimer.
double calculateCurvature(const gpr::Coord &G01, const gpr::Coord &orient)
Calculate curvature of the energy along the dimer.
std::function< bool(const Eigen::VectorXd &, Eigen::VectorXd &)> HessianVectorEvaluator
double estimateRotationalAngle(const gpr::Coord &orient, const gpr::Coord &G01, const gpr::Coord &F_rot_oriented)
Calculate a rough estimate of the optimal Rotation angle.
bool step(const gpr::Coord ¢er, const gpr::Coord &orientation, const Eigen::VectorXd &gradient, double separation, const Eigen::MatrixXd &physical, std::size_t memory, const Evaluator &evaluate, const gpr::Observation *initial_endpoint, gpr::Coord &rotated, gpr::Observation &endpoint)
bool start(const gpr::Coord ¢er, const Eigen::VectorXd &gradient, double separation, const Eigen::MatrixXd &physical, std::size_t memory, const gpr::Observation &endpoint, const gpr::Observation *previous=nullptr)
void calculatePotentialDispatched(Observation &image1)
Responsible for errors handling.
bool step(Eigen::VectorXd &x, const Eigen::VectorXd &gradient, double max_move, double empty_scale, Objective objective=Objective::Unspecified)
eon_relax_band_t eon_relax_surface_fn void * user
Eigen::MatrixXd rigid_motion_basis(const gpr::Coord &R, bool rotations)
Build an orthonormal basis for rigid-body translations and rotations.
double project_out_rot_trans_with_feedback(const gpr::Coord &R, Eigen::VectorXd &step)
Project rigid-body motion out of a Cartesian optimization step.
Eigen::Vector3d at(const FieldMatrixd &mat, Index_t i)
double flatDot(const Eigen::MatrixBase< DerivedA > &a, const Eigen::MatrixBase< DerivedB > &b)
void requireCompatibleLibrary()
Eigen::VectorXd projectPerpendicularDiagonal(const Eigen::VectorXd &force, const Eigen::VectorXd &tangent, const Eigen::VectorXd &force_variance)
Eigen::Vector3d vector3_reg
Eigen::Matrix< double, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixd
Structure with information for the LBFGS method.
std::shared_ptr< dimer::SphereRotation > sphere_rotation
A copied history starts an independent sphere solve when written.
Observation data: energy, gradient and positions.
Structure of translation parameters.