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

#include <IDPPObjectiveFunction.hpp>

Inheritance diagram for 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 ()

Private Attributes

std::shared_ptr< Mattermatter
MatrixXd d_target

Additional Inherited Members

Protected Attributes inherited from eonc::ObjectiveFunction
const Parametersparams

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),
32 matter{std::move(matterPassed)},
33 d_target(targetDistances) {
34
35 // Initialize working variables to avoid re-allocation
36 int natoms = matter->numberOfAtoms();
37 }
std::shared_ptr< Matter > matter
ObjectiveFunction(const Parameters &paramsPassed)

Member Function Documentation

◆ degreesOfFreedom()

int eonc::IDPPObjectiveFunction::degreesOfFreedom ( )
inlineoverridevirtual

Implements eonc::ObjectiveFunction.

Definition at line 58 of file IDPPObjectiveFunction.hpp.

58{ return 3 * matter->numberOfAtoms(); }

◆ difference()

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

Implements eonc::ObjectiveFunction.

Definition at line 71 of file IDPPObjectiveFunction.hpp.

71 {
72 return matter->pbcV(a - b);
73 }

◆ getConvergence()

double eonc::IDPPObjectiveFunction::getConvergence ( )
inlineoverridevirtual

Implements eonc::ObjectiveFunction.

Definition at line 64 of file IDPPObjectiveFunction.hpp.

64 {
65 // Return max force component or norm depending on preference
66 // Using norm here for simplicity in path generation
67 return getGradient().norm();
68 }
VectorXd getGradient(bool fdstep=false) override

◆ getEnergy()

double IDPPObjectiveFunction::getEnergy ( )
overridevirtual

Implements eonc::ObjectiveFunction.

Definition at line 18 of file IDPPObjectiveFunction.cpp.

18 {
19 double energy = 0.0;
20 int natoms = matter->numberOfAtoms();
21 AtomMatrix pos = matter->getPositions();
22
23 // Loop over unique pairs
24 for (int i = 0; i < natoms; ++i) {
25 for (int j = i + 1; j < natoms; ++j) {
26 // Respect PBC
27 double r = matter->pbc(pos.row(i) - pos.row(j)).norm();
28
29 // Weight function w = 1 / r^4
30 // Avoid division by zero if atoms overlap perfectly (unlikely in IDPP but
31 // possible)
32 if (r < 1e-4)
33 r = 1e-4;
34
35 double diff = r - d_target(i, j);
36 double r2 = r * r;
37 double weight = 1.0 / (r2 * r2); // 1/r^4
38
39 energy += 0.5 * weight * diff * diff;
40 }
41 }
42 return energy;
43}
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
Definition Eigen.h:37

◆ getGradient()

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

Implements eonc::ObjectiveFunction.

Definition at line 45 of file IDPPObjectiveFunction.cpp.

45 {
46 int natoms = matter->numberOfAtoms();
47 AtomMatrix pos = matter->getPositions();
48 AtomMatrix forces = AtomMatrix::Zero(natoms, 3);
49
50 for (int i = 0; i < natoms; ++i) {
51 for (int j = i + 1; j < natoms; ++j) {
52 // Vector pointing from j to i
53 Eigen::RowVector3d dr_vec = matter->pbc(pos.row(i) - pos.row(j));
54 double r = dr_vec.norm();
55
56 if (r < 1e-4)
57 r = 1e-4;
58
59 double diff = r - d_target(i, j);
60 double r2 = r * r;
61 double r5 = r2 * r2 * r;
62
63 // Derivative of E_pair = 0.5 * (1/r^4) * (r - d_target)^2
64 // dE/dr = (r - d_target)/r^4 - 2(r - d_target)^2 / r^5
65 // Simplified: (r - d_target) * (1 - 2(r - d_target)/r) / r^4
66
67 double r4 = r2 * r2;
68 double dEdr = (diff * (1.0 - 2.0 * diff / r)) / r4;
69
70 // Force contribution: F = -dE/dr * (dr_vec / r)
71 Eigen::RowVector3d f_contribution = -dEdr * (dr_vec / r);
72
73 forces.row(i) += f_contribution;
74 forces.row(j) -= f_contribution; // Newton's 3rd law
75 }
76 }
77
78 // Convert N x 3 matrix to 3N vector and return negative gradient (force)
79 // BUT getGradient expects the Gradient (positive derivative), so we return
80 // -Forces Actually, typical eOn getGradient returns dV/dx.
81 return VectorXd::Map(forces.data(), 3 * natoms) * -1.0;
82}

◆ getPositions()

VectorXd eonc::IDPPObjectiveFunction::getPositions ( )
inlineoverridevirtual

Implements eonc::ObjectiveFunction.

Definition at line 52 of file IDPPObjectiveFunction.hpp.

52 {
53 // Map Matter positions to 3N vector
54 return VectorXd::Map(matter->getPositions().data(),
55 3 * matter->numberOfAtoms());
56 }

◆ isConverged()

bool eonc::IDPPObjectiveFunction::isConverged ( )
inlineoverridevirtual

Implements eonc::ObjectiveFunction.

Definition at line 60 of file IDPPObjectiveFunction.hpp.

60 {
61 return getConvergence() < params.neb_options.initialization.force_tolerance;
62 }
const Parameters & params

◆ setPositions()

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

Implements eonc::ObjectiveFunction.

Definition at line 47 of file IDPPObjectiveFunction.hpp.

47 {
48 // Map 3N vector back to Matter
49 matter->setPositions(AtomMatrix::Map(x.data(), matter->numberOfAtoms(), 3));
50 }

Member Data Documentation

◆ d_target

◆ 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: