OpenMM Engine¶
openmm
¶
OpenMM molecular mechanics backend.
Provides a full-featured MM engine using OpenMM for energy, minimization,
Hessian, and frequency calculations. Supports both harmonic and MM3
functional forms with runtime parameter updates via :class:OpenMMHandle.
OpenMMHandle
dataclass
¶
OpenMMHandle(molecule: Q2MMMolecule, system: object, integrator: object, context: object, bond_force: object | None, angle_force: object | None, vdw_force: object | None, bond_terms: list[_BondTerm], angle_terms: list[_AngleTerm], vdw_terms: list[_VdwTerm], exceptions_14: list[_Exception14] = list(), functional_form: FunctionalForm = MM3)
Reusable OpenMM system/context pair for fast parameter updates.
Attributes:
| Name | Type | Description |
|---|---|---|
molecule |
Q2MMMolecule
|
Deep copy of the input molecule. |
system |
object
|
The |
integrator |
object
|
The |
context |
object
|
The |
bond_force |
object | None
|
The OpenMM bond force object, or |
angle_force |
object | None
|
The OpenMM angle force object, or |
vdw_force |
object | None
|
The OpenMM vdW force object, or |
bond_terms |
list[_BondTerm]
|
Mapping of molecule bonds to force indices. |
angle_terms |
list[_AngleTerm]
|
Mapping of molecule angles to force indices. |
vdw_terms |
list[_VdwTerm]
|
Mapping of atoms to vdW particle indices. |
exceptions_14 |
list[_Exception14]
|
1-4 nonbonded exceptions (harmonic form only). |
functional_form |
FunctionalForm
|
The functional form used when the handle was created. |
OpenMMEngine
¶
Bases: MMEngine
Molecular mechanics backend powered by OpenMM.
Supports both harmonic (AMBER-style) and MM3 functional forms.
Provides reusable :class:OpenMMHandle objects for fast parameter
updates during optimization loops.
Initialize the OpenMM engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
platform_name
|
str | None
|
OpenMM platform to use (e.g. |
None
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If OpenMM is not installed. |
Source code in q2mm/backends/mm/openmm.py
name
property
¶
Human-readable engine name.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
|
supported_functional_forms
¶
Functional forms this engine can evaluate.
Returns:
| Type | Description |
|---|---|
frozenset[str]
|
frozenset[str]: |
is_available
¶
Check if OpenMM is installed.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
supports_runtime_params
¶
Whether parameters can be updated without rebuilding the system.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
Always |
create_context
¶
create_context(structure, forcefield: ForceField | None = None) -> OpenMMHandle
Build an OpenMM system and context for a molecule.
Creates force objects (bond, angle, vdW) matching the force field's functional form and assigns per-term parameters from forcefield.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Q2MMMolecule | str | Path
|
A
:class: |
required |
forcefield
|
ForceField | None
|
Force field to apply. Auto-generated from the
molecule if |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
OpenMMHandle |
OpenMMHandle
|
Reusable handle for energy evaluation and parameter updates. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If an atom's element has no defined mass. |
ValueError
|
If no OpenMM terms could be created from the force field, or if a vdW parameter is missing for an atom. |
Source code in q2mm/backends/mm/openmm.py
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 480 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 575 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 | |
update_forcefield
¶
update_forcefield(handle: OpenMMHandle, forcefield: ForceField)
Update per-term parameters in an existing OpenMM Context.
Modifies bond, angle, and vdW parameters in-place, then pushes changes to the OpenMM context. Much faster than rebuilding the system from scratch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handle
|
OpenMMHandle
|
An existing :class: |
required |
forcefield
|
ForceField
|
New force field parameters to apply. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the force field's functional form does not match the handle's form, or if a required parameter is missing. |
Source code in q2mm/backends/mm/openmm.py
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 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 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 | |
export_system_xml
¶
export_system_xml(path: str | Path, structure, forcefield: ForceField | None = None) -> Path
Serialize the OpenMM System to XML.
Produces a topology-specific XML file containing the force objects
(HarmonicBondForce/CustomBondForce, etc. depending on the
functional form) with all per-term parameters. The file can be
loaded back with openmm.XmlSerializer.deserialize().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Output file path. |
required |
structure
|
Q2MMMolecule | str | Path | OpenMMHandle
|
A
:class: |
required |
forcefield
|
ForceField | None
|
Force field to apply. When structure is not an
:class: |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
The resolved output path. |
Source code in q2mm/backends/mm/openmm.py
load_system_xml
staticmethod
¶
Deserialize an OpenMM System from XML.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the XML file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
An |
Source code in q2mm/backends/mm/openmm.py
energy
¶
energy(structure, forcefield: ForceField | None = None) -> float
Calculate MM energy in kcal/mol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Q2MMMolecule | str | Path | OpenMMHandle
|
Molecule,
XYZ path, or :class: |
required |
forcefield
|
ForceField | None
|
Force field to apply. Auto-generated if |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Potential energy in kcal/mol. |
Source code in q2mm/backends/mm/openmm.py
minimize
¶
minimize(structure, forcefield: ForceField | None = None, tolerance: float = 1.0, max_iterations: int = 200) -> tuple
Energy-minimize structure using L-BFGS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Q2MMMolecule | str | Path | OpenMMHandle
|
Molecule,
XYZ path, or :class: |
required |
forcefield
|
ForceField | None
|
Force field to apply. Auto-generated if |
None
|
tolerance
|
float
|
Energy convergence tolerance in kJ/mol. |
1.0
|
max_iterations
|
int
|
Maximum minimization steps. |
200
|
Returns:
| Type | Description |
|---|---|
tuple
|
tuple[float, list[str], np.ndarray]: |
Source code in q2mm/backends/mm/openmm.py
hessian
¶
hessian(structure, forcefield: ForceField | None = None, step: float = 0.0001) -> ndarray
Finite-difference Hessian in canonical units (Hartree/Bohr²).
Internally computed in kJ/mol/nm² (OpenMM native) and converted
to Hartree/Bohr² before returning, matching the canonical unit
contract defined in :class:~q2mm.backends.base.MMEngine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Q2MMMolecule | str | Path | OpenMMHandle
|
Molecule,
XYZ path, or :class: |
required |
forcefield
|
ForceField | None
|
Force field to apply. Auto-generated if |
None
|
step
|
float
|
Finite-difference displacement in nm. |
0.0001
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Shape |
Source code in q2mm/backends/mm/openmm.py
frequencies
¶
frequencies(structure, forcefield: ForceField | None = None) -> list[float]
Approximate harmonic frequencies in cm⁻¹ from the numerical Hessian.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Q2MMMolecule | str | Path | OpenMMHandle
|
Molecule,
XYZ path, or :class: |
required |
forcefield
|
ForceField | None
|
Force field to apply. Auto-generated if |
None
|
Returns:
| Type | Description |
|---|---|
list[float]
|
list[float]: Vibrational frequencies in cm⁻¹. |