dxtb.components.base.Classical#
- class dxtb.components.base.Classical(device=None, dtype=None)[source]#
Bases:
ClassicalABC,ComponentBase class for calculation of classical contributions.
Methods
Disable the cache.
Enable the cache.
Invalidate the cache to require renewed setup.
Check if the driver is set up and updated.
Returns a copy of the
TensorLikeinstance on the CPU.Store variables for energy calculation.
Obtain energy of the contribution.
Calculates nuclear gradient of a classical energy contribution via PyTorch's autograd engine.
Override the device of the class object.
Override the dtype of the class object.
Reset the tensor attributes of the
dxtb.components.base.Componentinstance to their original states or to specified values.Returns a copy of the
TensorLikeinstance on the specified device.Returns a copy of the
TensorLikeinstance with specified floating point type.Update the attributes of the
Componentinstance.Attributes
Specification of dtypes that the
TensorLikeobject can take.Cache for the interaction.
Whether the cache has been set up.
Shortcut for device and dtype.
The device on which the class object resides.
Floating point dtype used by class object.
Label for the classical contribution.
- Parameters:
device (torch.device | None)
dtype (torch.dtype | None)
- cache_is_latest(cvars, tol=None)#
Check if the driver is set up and updated.
- cpu()#
Returns a copy of the
TensorLikeinstance on the CPU.This method creates and returns a new copy of the
TensorLikeinstance on the CPU.- Return type:
Self- Returns:
A copy of the
TensorLikeinstance placed on the CPU.- Return type:
- abstract get_cache(numbers, ihelp=None, **kwargs)#
Store variables for energy calculation.
- Return type:
- Parameters:
numbers (Tensor) – Atomic numbers for all atoms in the system (shape:
(..., nat)).ihelp (IndexHelper) – Helper class for indexing.
- Returns:
Cache class for storage of variables.
- Return type:
Cache
- Parameters:
numbers (Tensor)
ihelp (IndexHelper | None)
kwargs (Any)
Note
The cache of a classical contribution does not require
positionsas it only becomes useful ifnumbersremain unchanged andpositionsvary, i.e., during geometry optimization.
- abstract get_energy(positions, cache, **kwargs)#
Obtain energy of the contribution.
- Return type:
- Parameters:
positions (Tensor) – Cartesian coordinates of all atoms (shape:
(..., nat, 3)).cache (Cache) – Cache for the parameters.
- Returns:
Atomwise energy contributions.
- Return type:
Tensor
- Parameters:
positions (Tensor)
cache (ComponentCache)
kwargs (Any)
- get_gradient(energy, positions, grad_outputs=None)[source]#
Calculates nuclear gradient of a classical energy contribution via PyTorch’s autograd engine.
- Return type:
- Parameters:
energy (Tensor) – Energy that will be differentiated.
positions (Tensor) – Nuclear positions. Needs
requires_grad=True.grad_outputs (Tensor | None, optional) – Vector in the vector-Jacobian product. If
None, the vector is initialized to ones.
- Returns:
Nuclear gradient of
energy.- Return type:
Tensor
- Raises:
RuntimeError –
positionstensor does not haverequires_grad=True.- Parameters:
- override_device(device)#
Override the device of the class object. :rtype:
NoneWarning
This does not change the device of the underlying tensors. It only changes the device of the class object. Use with caution.
- Parameters:
device (
torch.device) – Device to override the current device.- Parameters:
device (device)
- Return type:
None
- override_dtype(dtype)#
Override the dtype of the class object. :rtype:
NoneWarning
This does not change the dtype of the underlying tensors. It only changes the dtype of the class object. Use with caution.
- Parameters:
dtype (
torch.dtype) – Floating point dtype to override the current dtype.- Parameters:
dtype (dtype)
- Return type:
None
- reset()#
Reset the tensor attributes of the
dxtb.components.base.Componentinstance to their original states or to specified values.This method iterates through the attributes defined in
__slots__and resets any tensor attributes to a detached clone of their original state. The requires_grad status of each tensor is preserved.- Return type:
Examples
import torch from dxtb.components.base.field import ElectricField ef = ElectricField(field=torch.tensor([0.0, 0.0, 0.0])) ef.reset()
Notes
Only tensor attributes defined in
__slots__are reset. Non-tensor attributes are ignored. Attempting to reset an attribute not defined in__slots__or providing a non-tensor value in kwargs will not raise an error; the method will simply ignore these cases and proceed with the reset operation for valid tensor attributes.
- to(device=None, dtype=None)#
Returns a copy of the
TensorLikeinstance on the specified device.This method creates and returns a new copy of the
TensorLikeinstance on the specified device “device”.- Return type:
Self- Parameters:
device (
torch.device) – Device to which all associated tensors should be moved.- Returns:
A copy of the
TensorLikeinstance placed on the specified device.- Return type:
- Parameters:
Notes
If the
TensorLikeinstance is already on the desired deviceselfwill be returned.
- type(dtype)#
Returns a copy of the
TensorLikeinstance with specified floating point type. This method creates and returns a new copy of theTensorLikeinstance with the specified dtype.- Return type:
Self- Parameters:
dtype (
torch.dtype) – Floating point type.- Returns:
A copy of the
TensorLikeinstance with the specified dtype.- Return type:
- Parameters:
dtype (dtype)
Notes
If the
TensorLikeinstance has already the desired dtypeSelfwill be returned.
- update(**kwargs)#
Update the attributes of the
Componentinstance.This method updates the attributes of the
Componentinstance based on the provided keyword arguments. Only the attributes defined in__slots__can be updated.- Return type:
- Parameters:
kwargs (dict[str, Any]) – Keyword arguments where keys are attribute names and values are the new values for those attributes. Valid keys are those defined in
__slots__of this class.- Raises:
AttributeError – If any key in kwargs is not an attribute defined in
__slots__.- Parameters:
kwargs (Any)
Examples
import torch from dxtb.components.field import ElectricField ef = ElectricField(field=torch.tensor([0.0, 0.0, 0.0])) ef.update(field=torch.tensor([1.0, 0.0, 0.0]))
- property allowed_dtypes: tuple[dtype, ...]#
Specification of dtypes that the
TensorLikeobject can take. Defaults to float types and must be overridden by subclass if float are not allowed. The IndexHelper is an example that should only allow integers.- Returns:
Collection of allowed dtypes the
TensorLikeobject can take.- Return type:
tuple[torch.dtype, …]
- property cache: ComponentCache | None#
Cache for the interaction.