Skip to content

Fix AttributeError when Hessian has no negative eigenvalues#10

Merged
ccastilloo merged 1 commit into
theochem:mainfrom
WHOIM1205:fix-bonded-hess-new-attributeerror
Jun 29, 2026
Merged

Fix AttributeError when Hessian has no negative eigenvalues#10
ccastilloo merged 1 commit into
theochem:mainfrom
WHOIM1205:fix-bonded-hess-new-attributeerror

Conversation

@WHOIM1205

Copy link
Copy Markdown

Fix: Prevent AttributeError in Bonded.__init__ when Hessian has no negative eigenvalues

Problem

self.hess_new was only initialized inside the if np.any(evals < tolerance) branch.
For well-optimized molecules (the normal case), this condition is false and self.hess_new was never created.
However, it was later used unconditionally for unit conversion, causing:

This prevented the Bonded class from being instantiated for typical equilibrium geometries.

Solution

Add an else branch to initialize self.hess_new with a copy of the original Hessian when no eigenvalue correction is required.

Change

if np.any(evals < tolerance):
    ...
    self.hess_new = evecs @ evals_m @ evecs.T
else:
    self.hess_new = self.hess.copy()

self.hess_new = (self.hess_new * 627.509391) / (0.529 ** 2)
self.hess = (self.hess * 627.509391) / (0.529 ** 2)

Signed-off-by: WHOIM1205 <rathourprateek8@gmail.com>
@WHOIM1205

Copy link
Copy Markdown
Author

hey @ccastilloo
This PR fixes a crash in Bonded.__init__ where self.hess_new was only initialized inside the negative-eigenvalue correction branch. For well-optimized molecules (the common case), this branch is skipped and self.hess_new remained undefined, causing an AttributeError during unit conversion.

The fix adds a fallback initialization (self.hess_new = self.hess.copy()) when no correction is needed, ensuring the attribute is always defined while preserving the existing behavior for negative eigenvalues.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Bonded.__init__ to ensure self.hess_new is always initialized, avoiding an AttributeError when the Hessian has no sufficiently negative eigenvalues (the common equilibrium case).

Changes:

  • Add an else branch to set self.hess_new = self.hess.copy() when no eigenvalue correction is needed.
  • Keep the unit-conversion logic operating on both self.hess and self.hess_new after initialization.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread ffprime/bond.py
Comment on lines 27 to +31
if np.allclose(evecs.T, np.linalg.inv(evecs)):
print("Eigenvectors are orthonormal.")
else:
raise ValueError("Eigenvectors are not orthonormal.")
else:
Comment thread ffprime/bond.py
Comment on lines +31 to +32
else:
self.hess_new = self.hess.copy()
@ccastilloo

Copy link
Copy Markdown
Collaborator

@WHOIM1205 Thank you for your contribution! The bond.py module is still a work in progress, so there are several known issues and improvements that are planned.
I don't see any conflicts between your proposed changes and the current implementation, so I'm happy to merge this PR. Thanks again for taking the time to contribute and help improve the project

@ccastilloo ccastilloo merged commit 966e0cd into theochem:main Jun 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants