Loading...
Searching...
No Matches
eonc::IDPPObjectiveFunction Class Reference

#include <IDPPObjectiveFunction.hpp>

Inheritance diagram for eonc::IDPPObjectiveFunction:

Public Member Functions

 IDPPObjectiveFunction (std::shared_ptr< Matter > matterPassed, const Parameters &paramsPassed, const MatrixXd &targetDistances)
double getEnergy () override
VectorXd getGradient (bool fdstep=false) override
void setPositions (const VectorXd &x) override
VectorXd getPositions () override
int degreesOfFreedom () override
bool isConverged () override
double getConvergence () override
VectorXd difference (const VectorXd &a, const VectorXd &b) override
Public Member Functions inherited from eonc::ObjectiveFunction
 ObjectiveFunction (const Parameters &paramsPassed)
virtual ~ObjectiveFunction ()
virtual bool supportsFiniteDifferenceCurvature () const
virtual std::optional< double > knownCurvature () const
virtual void minimumImage (Eigen::Ref< Eigen::Vector3d >) const
virtual VectorXd getMasses () const
virtual bool getPeriodic () const

Private Attributes

std::shared_ptr< Matter > matter
MatrixXd d_target

Additional Inherited Members

Protected Attributes inherited from eonc::ObjectiveFunction
const Parameters & params

Detailed Description

Definition at line 24 of file IDPPObjectiveFunction.hpp.

Constructor & Destructor Documentation

◆ IDPPObjectiveFunction()

eonc::IDPPObjectiveFunction::IDPPObjectiveFunction ( std::shared_ptr< Matter > matterPassed,
const Parameters & paramsPassed,
const MatrixXd & targetDistances )
inline

Definition at line 28 of file IDPPObjectiveFunction.hpp.

31 : ObjectiveFunction(paramsPassed), matter{std::move(matterPassed)},
32 d_target(targetDistances) {}
std::shared_ptr< Matter > matter
ObjectiveFunction(const Parameters &paramsPassed)

Member Function Documentation

◆ degreesOfFreedom()

int eonc::IDPPObjectiveFunction::degreesOfFreedom ( )
inlineoverridevirtual

Implements eonc::ObjectiveFunction.

Definition at line 49 of file IDPPObjectiveFunction.hpp.

49 {
50 return 3 * static_cast<int>(matter->numberOfFreeAtoms());
51 }

◆ difference()

VectorXd eonc::IDPPObjectiveFunction::difference ( const VectorXd & a,
const VectorXd & b )
inlineoverridevirtual

Implements eonc::ObjectiveFunction.

Definition at line 65 of file IDPPObjectiveFunction.hpp.

65 {
66 return matter->pbcV(a - b);
67 }

◆ getConvergence()

double eonc::IDPPObjectiveFunction::getConvergence ( )
inlineoverridevirtual

Implements eonc::ObjectiveFunction.

Definition at line 58 of file IDPPObjectiveFunction.hpp.

58 {
59 // Return max force component or norm depending on preference
60 // Using norm here for simplicity in path generation
61 return getGradient().norm();
62 }
VectorXd getGradient(bool fdstep=false) override

◆ getEnergy()

double eonc::IDPPObjectiveFunction::getEnergy ( )
overridevirtual

Implements eonc::ObjectiveFunction.

Definition at line 34 of file IDPPObjectiveFunction.cpp.

34 {
35 double energy = 0.0;
36 int natoms = matter->numberOfAtoms();
37 AtomMatrix pos = matter->getPositions();
38
39 // Loop over unique pairs
40 for (int i = 0; i < natoms; ++i) {
41 for (int j = i + 1; j < natoms; ++j) {
42 // Respect PBC
43 double r = matter->pbc(pos.row(i) - pos.row(j)).norm();
44
45 // Weight function w = 1 / r^4
46 // Avoid division by zero if atoms overlap perfectly (unlikely in IDPP but
47 // possible)
48 if (r < 1e-4)
49 r = 1e-4;
50
51 double diff = r - d_target(i, j);
52 double r2 = r * r;
53 double weight = 1.0 / (r2 * r2); // 1/r^4
54
55 energy += 0.5 * weight * diff * diff;
56 }
57 }
58 return energy;
59}
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
Definition Eigen.h:37

◆ getGradient()

VectorXd eonc::IDPPObjectiveFunction::getGradient ( bool fdstep = false)
overridevirtual

Implements eonc::ObjectiveFunction.

Definition at line 61 of file IDPPObjectiveFunction.cpp.

61 {
62 int natoms = matter->numberOfAtoms();
63 AtomMatrix pos = matter->getPositions();
64 AtomMatrix forces = AtomMatrix::Zero(natoms, 3);
65
66 for (int i = 0; i < natoms; ++i) {
67 for (int j = i + 1; j < natoms; ++j) {
68 // Vector pointing from j to i
69 Eigen::RowVector3d dr_vec = matter->pbc(pos.row(i) - pos.row(j));
70 double r = dr_vec.norm();
71
72 if (r < 1e-4)
73 r = 1e-4;
74
75 double diff = r - d_target(i, j);
76 double r2 = r * r;
77
78 // Derivative of E_pair = 0.5 * (1/r^4) * (r - d_target)^2
79 // dE/dr = (r - d_target)/r^4 - 2(r - d_target)^2 / r^5
80 // Simplified: (r - d_target) * (1 - 2(r - d_target)/r) / r^4
81
82 double r4 = r2 * r2;
83 double dEdr = (diff * (1.0 - 2.0 * diff / r)) / r4;
84
85 // Force contribution: F = -dE/dr * (dr_vec / r)
86 Eigen::RowVector3d f_contribution = -dEdr * (dr_vec / r);
87
88 forces.row(i) += f_contribution;
89 forces.row(j) -= f_contribution; // Newton's 3rd law
90 }
91 }
92
93 // dV/dx on free atoms only. Frozen rows stay in `forces` for Newton's
94 // third law during the pair loop, then are dropped.
95 return packFree(*matter, forces) * -1.0;
96}

◆ getPositions()

VectorXd eonc::IDPPObjectiveFunction::getPositions ( )
inlineoverridevirtual

Implements eonc::ObjectiveFunction.

Definition at line 47 of file IDPPObjectiveFunction.hpp.

47{ return matter->getPositionsFreeV(); }

◆ isConverged()

bool eonc::IDPPObjectiveFunction::isConverged ( )
inlineoverridevirtual

Implements eonc::ObjectiveFunction.

Definition at line 53 of file IDPPObjectiveFunction.hpp.

53 {
54 return getConvergence() <
55 params.neb_options().initialization.force_tolerance;
56 }
const Parameters & params

◆ setPositions()

void eonc::IDPPObjectiveFunction::setPositions ( const VectorXd & x)
inlineoverridevirtual

Implements eonc::ObjectiveFunction.

Definition at line 43 of file IDPPObjectiveFunction.hpp.

43 {
44 matter->setPositionsFreeV(x);
45 }

Member Data Documentation

◆ d_target

MatrixXd eonc::IDPPObjectiveFunction::d_target
private

Definition at line 70 of file IDPPObjectiveFunction.hpp.

◆ matter

std::shared_ptr<Matter> eonc::IDPPObjectiveFunction::matter
private

Definition at line 25 of file IDPPObjectiveFunction.hpp.


The documentation for this class was generated from the following files: