utils.py API reference
muller_brown_potential_base
muller_brown_potential_base(x)
2D Muller-Brown potential.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
(2, ) vector of x0 and x1 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Muller-Brown potential value at |
Source code in GADES/utils.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | |
muller_brown_potential
muller_brown_potential(X)
vmap version of the Muller-Brown potential.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
(N, 2) input x0 and x1 values |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jax.ndarray: (N, 1) Muller-Brown potential values |
Source code in GADES/utils.py
37 38 39 40 41 42 43 44 45 46 47 48 | |
muller_brown_force_base
muller_brown_force_base(x)
Muller-Brown forces at x calculated using AD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
(2, ) position |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jax.ndarray: (2, ) forces vector [-dU/dx0, -dU/dx1] |
Source code in GADES/utils.py
50 51 52 53 54 55 56 57 58 59 60 | |
muller_brown_force
muller_brown_force(X)
vmap version of Muller-Brown forces at X calculated using AD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
(N, 2) position |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jax.ndarray: (N, 2) forces vectors [-dU/dx0, -dU/dx1] |
Source code in GADES/utils.py
62 63 64 65 66 67 68 69 70 71 72 | |
muller_brown_hess_base
muller_brown_hess_base(x)
Muller-Brown Hessian at x calculated using AD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
(2, ) position |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jax.ndarray: (2, 2) Hessian matrix [[ddU/ddx0, ddU/dx0dx1], [ddU/dx1dx0, ddU/ddx1]] |
Source code in GADES/utils.py
74 75 76 77 78 79 80 81 82 83 84 | |
muller_brown_hess
muller_brown_hess(X)
vmap version of Muller-Brown Hessian at x calculated using AD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
(N, 2) position |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
jax.ndarray: (N, 2, 2) Hessian matrix [[ddU/ddx0, ddU/dx0dx1], [ddU/dx1dx0, ddU/ddx1]] |
Source code in GADES/utils.py
86 87 88 89 90 91 92 93 94 95 96 | |
muller_brown_gad_force_base
muller_brown_gad_force_base(position, kappa=0.9)
GADES forces for the Muller-Brown potential at position calculated using AD.
Calculates the total forces, then finds the most-negative eigenvalue and
the corresponding eigenvector of the Hessian and returns negative kappa
times the force projected in the eigenvector direction as the biasing force.
Args:
position (jax.ndarray): (2, ) position
kappa (float): GAD intensity parameter. Determines how much of the GAD
force is used for biasing. kappa=1 is 100% and kappa=0 is none.
Returns: jax.ndarray: (2, ) GAD bias forces vector
Source code in GADES/utils.py
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 123 124 125 | |
null_force
null_force(X)
Helper function for return Null forces. Used for unbiased runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
(d, ) array of position |
required |
Returns:
| Type | Description |
|---|---|
(d,)
|
Forces vector of all zeros |
Source code in GADES/utils.py
144 145 146 147 148 149 150 151 152 153 154 155 | |
inverse_power_iteration
inverse_power_iteration(A, mu=0.0, num_iters=100, tol=1e-06)
Computes the smallest eigenvalue and eigenvector of a matrix using inverse power iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
ndarray
|
The square matrix (n x n) for which to find the eigenvalue/vector pair with eval closest to mu. |
required |
mu
|
float
|
Shift value applied to A to condition search on eigenvalue/vector pair with eval closest to mu. |
0.0
|
num_iters
|
int
|
Maximum number of iterations. |
100
|
tol
|
float
|
Convergence tolerance for the eigenvector. |
1e-06
|
Returns:
| Name | Type | Description |
|---|---|---|
eigenvalue |
float
|
Smallest eigenvalue of the matrix. |
eigenvector |
ndarray
|
Corresponding eigenvector (normalized). |
Source code in GADES/utils.py
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 | |
baoab_langevin_integrator
baoab_langevin_integrator(positions, velocities, forces_u, forces_b, mass, gamma, dt, kBT, force_function_u, force_function_b, n_steps=1)
BAOAB Langevin integrator based on Leimkuhler and Matthews (2013). https://dx.doi.org/10.1093/amrx/abs010
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positions
|
ndarray
|
Initial positions (shape: [D, ], where D is dimensionality). |
required |
velocities
|
ndarray
|
Initial velocities (shape: [D, ]). |
required |
forces_u
|
ndarray
|
Initial unbiased forces (shape: [D, ]). |
required |
forces_b
|
ndarray
|
Initial biased forces (shape: [D, ]). |
required |
mass
|
float
|
Mass of the particles (scalar). |
required |
gamma
|
float
|
Friction coefficient. (scalar). |
required |
dt
|
float
|
Time step. (scalar). |
required |
n_steps
|
int
|
Number of simulation steps. (scalar). |
1
|
kBT
|
float
|
Thermal energy (\(k_B T\)). (scalar). |
required |
force_function_u
|
callable
|
Function to compute unbiased forces given positions (returns forces of shape [D, ]). |
required |
force_function_b
|
callable
|
Function to compute biased forces given positions (returns forces of shape [D, ]). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
positions |
ndarray
|
New positions (shape: [D, ]). |
velocities |
ndarray
|
New velocities (shape: [D, ]). |
forces_u |
ndarray
|
Unbiased forces at new position (shape: [D, ]). |
forces_b |
ndarray
|
Biased forces at new position (shape: [D, ]). |
Source code in GADES/utils.py
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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
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
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
compute_hessian_force_fd_block_parallel
compute_hessian_force_fd_block_parallel(system, positions, atom_indices, epsilon=0.0001, n_jobs=-1, platform_name='CPU')
Compute the Hessian block for a subset of atoms via finite-difference forces.
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 |
|---|---|---|---|
system
|
System
|
The OpenMM system object defining particles, interactions, and forces. |
required |
positions
|
Quantity
|
Atomic positions with shape |
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 = ∂²V / (∂x_i ∂x_j) - Parallelization is handled with
joblib.Parallel. - The final matrix is symmetrized to mitigate finite-difference noise.
Examples:
>>> hess_block = compute_hessian_force_fd_block_parallel(
... system, positions, atom_indices=[0, 1, 2], epsilon=1e-4, n_jobs=4
... )
>>> hess_block.shape
(9, 9)
Source code in GADES/utils.py
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 453 454 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 | |
compute_hessian_force_fd_block_serial
compute_hessian_force_fd_block_serial(system, positions, 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 |
|---|---|---|---|
system
|
System
|
The OpenMM system object defining particles, interactions, and forces. |
required |
positions
|
Quantity
|
Atomic positions with shape |
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 = ∂²V / (∂x_i ∂x_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(
... system, positions, atom_indices=[0, 1], epsilon=1e-4
... )
>>> hess_block.shape
(6, 6)
Source code in GADES/utils.py
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 | |
compute_hessian_force_fd_richardson
compute_hessian_force_fd_richardson(system, positions, atom_indices, epsilon=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 |
|---|---|---|---|
system
|
System
|
The OpenMM system object defining particles, interactions, and forces. |
required |
positions
|
Quantity
|
Atomic positions with shape |
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
|
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(
... system, positions, atom_indices=[0, 1],
... epsilon=1e-4, factors=[1.0, 0.5, 0.25]
... )
>>> hess_block.shape
(6, 6)
Source code in GADES/utils.py
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 | |
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
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 | |