utils.py API reference
Core utility functions for GADES.
This module provides essential utilities for GADES computations: - Hessian matrix computation via finite differences - Force magnitude clamping
Recommended Hessian Function
For most use cases, use compute_hessian_force_fd_richardson. It applies
Richardson extrapolation to reduce sensitivity to step size and improve
numerical accuracy. Example::
from GADES.utils import compute_hessian_force_fd_richardson as hessian
hess = hessian(backend, atom_indices=[0, 1, 2])
Alternative functions are provided for specific scenarios:
- compute_hessian_force_fd_block_serial: Simpler, no extrapolation
get_hessian_fdiff
get_hessian_fdiff(func, x0, epsilon=1e-06)
Compute the Hessian matrix of a scalar function using finite differences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
The scalar function f(x) whose Hessian is to be computed. |
required |
x0
|
ndarray
|
The point at which the Hessian is evaluated. |
required |
epsilon
|
float
|
Small step size for finite difference approximation. |
1e-06
|
Returns:
| Name | Type | Description |
|---|---|---|
ndarray |
ndarray
|
The Hessian matrix of f at x0. |
Source code in GADES/utils.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
central_diff_ij
central_diff_ij(func, x0, i, j, epsilon)
Compute Hessian element H[i,j] using central differences.
Source code in GADES/utils.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
get_hessian_cdiff_parallel
get_hessian_cdiff_parallel(func, x0, epsilon=1e-05, n_jobs=-1)
Compute the Hessian matrix using central differences in parallel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
The scalar function f(x) whose Hessian is to be computed. |
required |
x0
|
ndarray
|
The point at which the Hessian is evaluated. |
required |
epsilon
|
float
|
Small step size for finite difference approximation. |
1e-05
|
n_jobs
|
int
|
Number of parallel workers (-1 for all cores). |
-1
|
Returns:
| Name | Type | Description |
|---|---|---|
ndarray |
ndarray
|
The symmetric Hessian matrix of f at x0. |
Source code in GADES/utils.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
compute_hessian_force_fd_block_parallel
compute_hessian_force_fd_block_parallel(backend, atom_indices, epsilon=0.0001, n_jobs=-1, platform_name='CPU')
Compute the Hessian block for a subset of atoms via finite-difference forces.
.. deprecated::
This function is deprecated due to thread-safety issues. The threading
backend cannot safely parallelize calls to backend.get_forces()
because OpenMM/ASE backends mutate shared state (positions) without
synchronization. This can produce corrupted Hessians.
Use ``compute_hessian_force_fd_richardson`` (recommended) or
``compute_hessian_force_fd_block_serial`` instead.
This function builds the Hessian matrix (second derivatives of the potential
energy with respect to Cartesian coordinates) for a selected set of atoms.
The calculation perturbs each coordinate by a small displacement and computes
the corresponding force differences in parallel. This is the parallel version
of compute_hessian_force_fd_block_serial. The performance gain of the parallel
version is minimal for systems with <10000 biased particles. Because of joblib
overhead, this method is in fact slower than the serial version for small
systems.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
Backend
|
The backend object providing system state and force calculations. |
required |
atom_indices
|
Sequence[int] or None
|
Indices of atoms to include in the Hessian block. If None, all atoms are included. |
required |
epsilon
|
float
|
Finite-difference displacement step size (in nanometers).
Default is |
0.0001
|
n_jobs
|
int
|
Number of parallel workers for finite-difference force evaluations.
Default is |
-1
|
platform_name
|
str
|
OpenMM platform to use for evaluations (e.g., |
'CPU'
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray:
A symmetric Hessian block matrix of shape |
Notes
- The Hessian is computed column by column using finite-difference forces:
H_ij = d²V / (dx_i dx_j) - Parallelization uses
joblib.Parallelwith the'threading'backend. The'loky'(multiprocessing) backend cannot be used because OpenMM/ASE backends contain non-picklable objects (contexts, file handles, etc.). - Due to Python's GIL, threading provides limited speedup for CPU-bound
workloads. For most systems,
compute_hessian_force_fd_block_serialorcompute_hessian_force_fd_richardsonare recommended instead. - The final matrix is symmetrized to mitigate finite-difference noise.
Examples:
>>> hess_block = compute_hessian_force_fd_block_parallel(
... backend, atom_indices=[0, 1, 2], epsilon=1e-4, n_jobs=4
... )
>>> hess_block.shape
(9, 9)
Source code in GADES/utils.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
compute_hessian_force_fd_block_serial
compute_hessian_force_fd_block_serial(backend, atom_indices, epsilon=0.0001, platform_name='CPU')
Compute the Hessian block for a subset of atoms via finite-difference forces (serial version).
This function constructs the Hessian matrix (second derivatives of the potential energy with respect to Cartesian coordinates) for a selected set of atoms. The calculation perturbs each coordinate one at a time and computes the corresponding force differences, without parallelization. Use this version for system with <10000 biased atoms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
Backend
|
The backend object providing system state and force calculations. |
required |
atom_indices
|
Sequence[int] or None
|
Indices of atoms to include in the Hessian block. If None, all atoms are included. |
required |
epsilon
|
float
|
Finite-difference displacement step size (in nanometers).
Default is |
0.0001
|
platform_name
|
str
|
OpenMM platform to use for evaluations (e.g., |
'CPU'
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray:
A symmetric Hessian block matrix of shape |
Notes
- The Hessian is computed column by column using finite-difference forces:
H_ij = d²V / (dx_i dx_j) - This serial implementation is simpler but slower than the parallel
version (
compute_hessian_force_fd_block_parallel) for large systems. - The final matrix is symmetrized to mitigate finite-difference noise.
Examples:
>>> hess_block = compute_hessian_force_fd_block_serial(
... backend, atom_indices=[0, 1], epsilon=1e-4
... )
>>> hess_block.shape
(6, 6)
Source code in GADES/utils.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | |
compute_hessian_force_fd_richardson
compute_hessian_force_fd_richardson(backend, atom_indices, step_size=0.0001, platform_name='CPU', factors=None)
Compute the Hessian block for a subset of atoms using Richardson-extrapolated finite differences.
This method estimates second derivatives of the potential energy by recursively applying Richardson extrapolation to finite-difference force calculations at multiple step sizes. This improves accuracy compared to a single-step finite-difference scheme. This is the go-to method for calculating numerical Hessian for GADES. Using the Richardson extrapolation drastically reduces the depency of accuracy on step size and prevents numerical error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
Backend
|
The backend object providing system state and force calculations. |
required |
atom_indices
|
Sequence[int] or None
|
Indices of atoms to include in the Hessian block. If None, all atoms are included. |
required |
step_size
|
float
|
Base finite-difference displacement step size (in nanometers).
Default is |
0.0001
|
platform_name
|
str
|
OpenMM platform to use for evaluations (e.g., |
'CPU'
|
factors
|
Sequence[float]
|
Decreasing list of scaling factors for step sizes, applied to |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray:
A symmetric Hessian block matrix of shape |
Notes
- The Hessian is computed column by column. For each perturbed coordinate,
force differences are evaluated at multiple step sizes and combined via
Richardson extrapolation:
where
R(k, i) = (r * R(k-1, i+1) - R(k-1, i)) / (r - 1)r = h_i / h_{i+k}is the ratio of step sizes. - Using more factors generally improves accuracy, but increases cost.
- The final Hessian is symmetrized to reduce numerical noise.
Examples:
>>> hess_block = compute_hessian_force_fd_richardson(
... backend, atom_indices=[0, 1],
... step_size=1e-4, factors=[1.0, 0.5, 0.25]
... )
>>> hess_block.shape
(6, 6)
Source code in GADES/utils.py
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | |
clamp_force_magnitudes
clamp_force_magnitudes(forces_flat, max_force)
Clamp the magnitudes of 3D force vectors in a flattened array.
This function rescales each 3D force vector so that the magnitude of the bias
force on each particle does not exceed max_force. The input is a flattened
array where each consecutive triplet of values corresponds to one (fx, fy, fz) vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
forces_flat
|
ndarray
|
Flattened array of shape |
required |
max_force
|
float
|
Maximum allowed magnitude for each force vector. Forces with smaller magnitudes are unchanged. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray:
Flattened array of the same shape as |
Notes
- Zero-length vectors remain unchanged.
- The scaling is applied independently to each force vector.
Examples:
>>> import numpy as np
>>> forces = np.array([3.0, 4.0, 0.0, 0.0, 0.0, 10.0]) # two vectors
>>> clamped = clamp_force_magnitudes(forces, max_force=5.0)
>>> clamped
array([3., 4., 0., 0., 0., 5.])
Source code in GADES/utils.py
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | |