apoCHARMM 1.0.0
High-performance molecular dynamics simulations on GPUs
 
Loading...
Searching...
No Matches
apocharmm.harmonic_restraint_force.HarmonicRestraintForce Class Reference

Configures an owned native absolute harmonic restraint. More...

Inherits _ApoObject.

Public Member Functions

None __init__ (self, int num_atoms)
 Constructs a restraint for a fixed atom count.
 
str default_force_tag (self)
 Returns the default manager energy tag.
 
None setSelection (self, AtomSelection selection)
 Copies the atom selection used by the restraint.
 
None setForceConstant (self, float force_constant)
 Sets one force constant for all selected atoms.
 
None setForceConstants (self, Sequence[float] force_constants)
 Copies one force constant per atom.
 
None setReferenceCoordinates (self, Sequence[Sequence[float]] reference_coordinates)
 Copies absolute Cartesian reference coordinates.
 
None setMasses (self, Sequence[float] masses)
 Copies one mass or restraint weight per atom.
 
None setBoxDimensions (self, Sequence[float] box_dimensions)
 Copies three orthogonal box lengths.
 

Protected Member Functions

None _subscribe_to_force_manager (self, ForceManager force_manager, str|None force_tag=None)
 Implements the ForceManager.subscribe() callback.
 
None _unsubscribe_from_force_manager (self, ForceManager force_manager)
 Implements the ForceManager.unsubscribe() callback.
 

Detailed Description

Configures an owned native absolute harmonic restraint.

The restraint applies one absolute Cartesian reference position, force constant, and mass or weight per atom. See the subsystem page for the energy equation, force-array sign convention, data layout, and units.

This wrapper owns one C handle for the native HarmonicRestraintForce<long long int, float> specialization. Python sequences are converted to float, copied into temporary contiguous ctypes buffers, and copied again into native storage. No caller sequence or buffer is retained.

ForceManager.subscribe() retains this wrapper after native subscription succeeds. The native manager independently retains the restraint and its calculation resources. Unsubscribe before calling close(): closing a subscribed wrapper destroys the C handle but does not remove the native subscription, and the closed wrapper can no longer identify that subscription for normal unsubscription.

close(), destroy(), context-manager exit, and finalization release the owned C handle. Explicit closure is idempotent. Methods that access self.handle after closure raise RuntimeError.

The wrapper and native object provide no internal synchronization. Do not overlap configuration, manager subscription changes, calculation, or closure across threads.

See also
harmonic_restraint_force

Constructor & Destructor Documentation

◆ __init__()

None apocharmm.harmonic_restraint_force.HarmonicRestraintForce.__init__ (   self,
int  num_atoms 
)

Constructs a restraint for a fixed atom count.

num_atoms is checked as a Python int, converted to signed C int, and passed to the native constructor. The wrapper owns the newly returned handle and sets default_force_tag to "harm".

Parameters
[in]num_atomsDimensionless positive atom count representable by signed 32-bit C int. The current isinstance(value, int) check also accepts Python bool; True reaches native construction as one and False is rejected natively as zero.
Exceptions
TypeErrorIf num_atoms is not a Python int.
ValueErrorIf num_atoms is outside the signed 32-bit C int range.
ApoCharmmErrorIf native validation rejects a non-positive value, native CUDA setup fails, or native allocation fails.
RuntimeErrorIf native construction reports success but produces a NULL handle.
MemoryErrorIf Python or ctypes bookkeeping cannot allocate storage.
Postcondition
On success, this wrapper owns a live native handle and default_force_tag == "harm".

Member Function Documentation

◆ _subscribe_to_force_manager()

None apocharmm.harmonic_restraint_force.HarmonicRestraintForce._subscribe_to_force_manager (   self,
ForceManager  force_manager,
str | None   force_tag = None 
)
protected

Implements the ForceManager.subscribe() callback.

None selects default_force_tag; otherwise force_tag must be a Python str. The selected text is encoded as UTF-8 and passed as a borrowed null-terminated C string. Native code copies the tag and retains the restraint and calculation resources on success.

This callback performs the native subscription. The calling ForceManager.subscribe() method retains this Python wrapper only after the callback succeeds.

Parameters
[in]force_managerLive ForceManager receiving the restraint.
[in]force_tagPython str tag, or None to use "harm".
Exceptions
TypeErrorIf force_manager is not a ForceManager or a non-None tag is not a str.
UnicodeEncodeErrorIf force_tag cannot be encoded as UTF-8.
RuntimeErrorIf this wrapper or force_manager has been closed.
ApoCharmmErrorIf the tag is empty, either native handle is invalid, the restraint is already subscribed, initialization detects a mismatch, or native allocation or CUDA setup fails.
Warning
An embedded NUL character terminates the C string and causes native code to observe only the preceding tag prefix.

◆ _unsubscribe_from_force_manager()

None apocharmm.harmonic_restraint_force.HarmonicRestraintForce._unsubscribe_from_force_manager (   self,
ForceManager  force_manager 
)
protected

Implements the ForceManager.unsubscribe() callback.

Native unsubscription releases the manager's retained restraint, stream, force-array, and energy owners. After this callback succeeds, the calling ForceManager.unsubscribe() method releases its retained Python reference.

Parameters
[in]force_managerLive ForceManager currently containing this restraint.
Exceptions
RuntimeErrorIf this wrapper or force_manager has been closed.
ApoCharmmErrorIf either native handle is invalid, this restraint is not subscribed to the manager, or an unexpected native runtime failure occurs.

◆ default_force_tag()

str apocharmm.harmonic_restraint_force.HarmonicRestraintForce.default_force_tag (   self)

Returns the default manager energy tag.

This property reads Python-owned text and does not access the native handle, so it remains available after close().

Returns
The Python string "harm".

◆ setBoxDimensions()

None apocharmm.harmonic_restraint_force.HarmonicRestraintForce.setBoxDimensions (   self,
Sequence[float]  box_dimensions 
)

Copies three orthogonal box lengths.

The method consumes box_dimensions as an iterable, converts every value with float(), and copies the resulting list through a temporary C double buffer. Exactly three finite, positive values are required in [x, y, z] order and angstrom units.

The current restraint kernel stores but does not use these dimensions. Changing them does not wrap displacements or rescale references.

Parameters
[in]box_dimensionsIterable of exactly three box lengths.
Exceptions
TypeErrorIf the object is not iterable or a value cannot be converted with float().
ValueErrorIf value conversion fails.
OverflowErrorIf value conversion overflows.
MemoryErrorIf the temporary list or C buffer cannot be allocated.
RuntimeErrorIf this wrapper has been closed.
ApoCharmmErrorIf native length or value validation fails or a changed box cannot be transferred to the device.

◆ setForceConstant()

None apocharmm.harmonic_restraint_force.HarmonicRestraintForce.setForceConstant (   self,
float  force_constant 
)

Sets one force constant for all selected atoms.

force_constant is converted with float() and then to C double. Unselected atoms receive zero. With masses in atomic mass units, the value has units of kilocalories per mole per square angstrom per atomic mass unit.

Parameters
[in]force_constantAny Python object accepted by float(). The resulting value must be finite and non-negative.
Exceptions
TypeErrorIf float(force_constant) rejects the object.
ValueErrorIf Python conversion rejects the value.
OverflowErrorIf Python conversion overflows.
RuntimeErrorIf this wrapper has been closed.
ApoCharmmErrorIf native validation rejects the converted value or the native CUDA update fails.

◆ setForceConstants()

None apocharmm.harmonic_restraint_force.HarmonicRestraintForce.setForceConstants (   self,
Sequence[float]  force_constants 
)

Copies one force constant per atom.

The method consumes force_constants as an iterable, converts every element with float(), materializes a new Python list, and copies that list into a temporary contiguous C double array. Native code then copies the array again.

The iterable must produce exactly one value per atom. Only currently selected values are validated and used; unselected values are ignored and their native constants are zero.

Parameters
[in]force_constantsIterable of values accepted by float(), in atom order. Selected values must be finite and non-negative.
Exceptions
TypeErrorIf the object is not iterable or an element cannot be converted with float().
ValueErrorIf element conversion fails.
OverflowErrorIf element conversion overflows.
MemoryErrorIf the temporary list or C buffer cannot be allocated.
RuntimeErrorIf this wrapper has been closed.
ApoCharmmErrorIf the native length or selected-value contract is violated or the native CUDA update fails.

◆ setMasses()

None apocharmm.harmonic_restraint_force.HarmonicRestraintForce.setMasses (   self,
Sequence[float]  masses 
)

Copies one mass or restraint weight per atom.

The method consumes masses as an iterable, converts every element with float(), materializes a new Python list, and copies it into a temporary contiguous C double buffer. Native code copies the values again.

Values normally use atomic mass units. Every value must be finite and non-negative. Zero is accepted and suppresses that atom's harmonic contribution.

Parameters
[in]massesIterable producing exactly one mass or weight per atom, in atom order.
Exceptions
TypeErrorIf masses is not iterable or an element cannot be converted with float().
ValueErrorIf element conversion fails.
OverflowErrorIf element conversion overflows.
MemoryErrorIf the temporary list or C buffer cannot be allocated.
RuntimeErrorIf this wrapper has been closed.
ApoCharmmErrorIf the native length or value contract is violated or the native CUDA transfer fails.
Warning
A native invalid-value failure can leave an earlier host prefix updated while the native device mirror remains old.

◆ setReferenceCoordinates()

None apocharmm.harmonic_restraint_force.HarmonicRestraintForce.setReferenceCoordinates (   self,
Sequence[Sequence[float]]   reference_coordinates 
)

Copies absolute Cartesian reference coordinates.

The method consumes an outer iterable of coordinate rows. Every row is consumed as an iterable, every element is converted with float(), and each converted row must contain exactly three values in [x, y, z] order. Rows are flattened into a newly allocated row-major C double buffer.

The outer iterable must produce exactly one row per atom. Coordinates use angstroms and are not minimum-image wrapped or rescaled when the box changes.

Parameters
[in]reference_coordinatesIterable of three-element coordinate iterables in atom order. Every converted value must be finite.
Exceptions
TypeErrorIf an outer or inner value is not iterable or a scalar cannot be converted with float().
ValueErrorIf scalar conversion fails or a converted row does not contain exactly three elements.
OverflowErrorIf scalar conversion overflows.
MemoryErrorIf the flattened list or C buffer cannot be allocated.
RuntimeErrorIf this wrapper has been closed.
ApoCharmmErrorIf the native atom count is wrong, a converted coordinate is non-finite, or the native CUDA transfer fails.
Warning
A native non-finite-value failure can leave an earlier host prefix updated while the native device mirror remains old.

◆ setSelection()

None apocharmm.harmonic_restraint_force.HarmonicRestraintForce.setSelection (   self,
AtomSelection  selection 
)

Copies the atom selection used by the restraint.

The native selection is copied. This wrapper does not retain selection. Unselected atoms have their force constants set to zero; expanding the selection later does not restore constants that were previously zeroed.

Parameters
[in]selectionLive AtomSelection with the same atom count as this restraint.
Exceptions
TypeErrorIf selection is not an AtomSelection.
RuntimeErrorIf this wrapper or selection has been closed.
ApoCharmmErrorIf native handle validation or atom-count validation fails, a native allocation fails, or the native host-to-device update fails.