@@ -139,16 +139,24 @@ def _entropy_components(
139139
140140 @staticmethod
141141 def _matrix_eigenvalues (matrix : np .ndarray ) -> np .ndarray :
142- """Compute eigenvalues of a matrix.
142+ """Compute eigenvalues of a symmetric covariance matrix.
143143
144144 Args:
145- matrix: Input matrix.
145+ matrix: Input covariance/second-moment matrix.
146146
147147 Returns:
148- Eigenvalues as a NumPy array.
148+ Real eigenvalues as a NumPy array.
149149 """
150150 matrix = np .asarray (matrix , dtype = float )
151- return la .eigvals (matrix )
151+
152+ if matrix .ndim != 2 or matrix .shape [0 ] != matrix .shape [1 ]:
153+ raise ValueError (f"Expected square matrix, got shape={ matrix .shape } " )
154+
155+ # Covariance matrices should be symmetric. Explicit symmetrisation avoids
156+ # tiny floating-point asymmetries affecting the eigensolver path.
157+ matrix = 0.5 * (matrix + matrix .T )
158+
159+ return la .eigvalsh (matrix )
152160
153161 def _convert_lambda_units (self , lambdas : np .ndarray ) -> np .ndarray :
154162 """Convert eigenvalues into SI units using run_manager.
@@ -162,54 +170,51 @@ def _convert_lambda_units(self, lambdas: np.ndarray) -> np.ndarray:
162170 return self ._run_manager .change_lambda_units (lambdas )
163171
164172 def _flexible_dihedral (self , lambdas : np .ndarray , flexible : int ) -> np .ndarray :
165- """Force halving for flexible dihedrals.
173+ """Force halving for flexible dihedrals."""
174+ lambdas = np .asarray (lambdas , dtype = float ).copy ()
166175
167- If N flexible dihedrals, halve the forces for the N largest eigenvalues.
168- The matrix has force^2 so use factor of 0.25 for eigenvalues.
176+ if flexible <= 0 or lambdas . size == 0 :
177+ return lambdas
169178
170- Args:
171- lambdas: Eigenvalues
172- flexible: the number of flexible dihedrals in the molecule
179+ n_flexible = min (int (flexible ), lambdas .size )
173180
174- Returns:
175- reduced lambdas
176- """
177- halved = sorted (lambdas , reverse = True )
178- for i in range (flexible ):
179- halved [i ] = 0.25 * halved [i ]
180- lambdas = halved
181+ # Halve the largest eigenvalues deterministically.
182+ idx = np .argsort (lambdas )[::- 1 ][:n_flexible ]
183+ lambdas [idx ] *= 0.25
181184
182185 return lambdas
183186
184187 def _frequencies_from_lambdas (self , lambdas : np .ndarray , temp : float ) -> np .ndarray :
185188 """Convert eigenvalues to frequencies with robust filtering.
186189
187- Filters out eigenvalues that are complex, non-positive, or near-zero to
188- avoid invalid frequencies and unstable entropies.
190+ Filters out non-finite, non-positive, and near-zero eigenvalues. The
191+ near-zero threshold is scale-aware so rigid-body/null modes are removed
192+ consistently across platforms.
193+ """
194+ lambdas = np .asarray (lambdas , dtype = float ).reshape (- 1 )
189195
190- Args:
191- lambdas: Eigenvalues (post unit conversion).
192- temp: Temperature in Kelvin.
196+ finite_mask = np .isfinite (lambdas )
197+ finite_lambdas = lambdas [finite_mask ]
193198
194- Returns:
195- Frequencies in Hz.
196- """
197- lambdas = np .asarray (lambdas )
198- lambdas = np .real_if_close (lambdas , tol = 1000 )
199+ if finite_lambdas .size == 0 :
200+ logger .warning ("No finite eigenvalues available for vibrational entropy." )
201+ return np .array ([], dtype = float )
202+
203+ scale = float (np .max (np .abs (finite_lambdas )))
204+ zero_tol = max (1e-7 , 1e-10 * scale )
199205
200- valid_mask = (
201- np .isreal (lambdas ) & (lambdas > 0 ) & (~ np .isclose (lambdas , 0 , atol = 1e-7 ))
202- )
206+ valid_mask = finite_mask & (lambdas > zero_tol )
203207
204- removed = int (len ( lambdas ) - np .count_nonzero (valid_mask ))
208+ removed = int (lambdas . size - np .count_nonzero (valid_mask ))
205209 if removed :
206210 logger .warning (
207- "%d invalid eigenvalues excluded (complex, non-positive, "
208- "or near-zero )." ,
211+ "%d eigenvalues excluded from vibrational entropy "
212+ "(non-finite, non-positive, or below zero_tol=%g )." ,
209213 removed ,
214+ zero_tol ,
210215 )
211216
212- lambdas = np . asarray ( lambdas [valid_mask ]. real , dtype = float )
217+ lambdas = lambdas [valid_mask ]
213218 if lambdas .size == 0 :
214219 return np .array ([], dtype = float )
215220
0 commit comments