Systems¶
- class metatomic.torch.System(types: Tensor, positions: Tensor, cell: Tensor, pbc: Tensor)[source]¶
A System contains all the information about an atomistic system; and should be used as the input of atomistic models.
You can create a
Systemwithtypes,positionsandcelltensors, or convert data from other libraries.- Parameters:
types (Tensor) – 1D tensor of integer representing the particles identity. For atoms, this is typically their atomic numbers.
positions (Tensor) – 2D tensor of shape (len(types), 3) containing the Cartesian positions of all particles in the system.
cell (Tensor) – 2D tensor of shape (3, 3), describing the bounding box/unit cell of the system. Each row should be one of the bounding box vector; and columns should contain the x, y, and z components of these vectors (i.e. the cell should be given in row-major order). Systems that are not periodic along one or more directions should set the corresponding cell vectors to 0.
pbc (Tensor) – tensor containing 3 boolean values, indicating which dimensions are periodic along each axis, in the same order as the cell vectors.
- property positions: Tensor¶
Tensor of floating point values containing the particles cartesian coordinates
- property dtype: dtype¶
get the dtype of all the arrays stored inside this
SystemWarning
Due to limitations in TorchScript C++ extensions, the dtype is returned as an integer, which can not be compared with
torch.dtypeinstances. SeeTensorBlock.dtypefor more information.
- to(dtype: dtype | None = None, device: device | None = None, non_blocking: bool = False) System[source]¶
Move all the arrays in this system to the given
dtypeanddevice.- Parameters:
dtype (dtype | None) – new dtype to use for all arrays. The dtype stays the same if this is set to
None.device (device | None) – new device to use for all arrays. The device stays the same if this is set to
None.non_blocking (bool) – If this is
True, the function tries to move the data asynchronously. Seetorch.Tensor.to()for more information.
- Return type:
- add_neighbor_list(options: NeighborListOptions, neighbors: TensorBlock)[source]¶
Add a new neighbors list in this system corresponding to the given
options.The neighbors list should have the following samples:
"first_atom","second_atom","cell_shift_a","cell_shift_b","cell_shift_c", containing the index of the first and second atoms (matching the “atom” sample in the positions); and the number of cell vector a/b/c to add to the positions difference to get the pair vector.The neighbors should also have a single component
"xyz"with values[0, 1, 2]; and a single property"distance"with value 0.The neighbors values must contain the distance vector from the first to the second atom, i.e.
positions[second_atom] - positions[first_atom] + cell_shift_a * cell_a + cell_shift_b * cell_b + cell_shift_c * cell_c.- Parameters:
options (NeighborListOptions) – options of the neighbors list
neighbors (TensorBlock) – list of neighbors stored in a
TensorBlock
- get_neighbor_list(options: NeighborListOptions) TensorBlock[source]¶
Retrieve a previously stored neighbors list with the given
options, or throw an error if no such neighbors list exists.- Parameters:
options (NeighborListOptions) – options of the neighbors list to retrieve
- Return type:
- known_neighbor_lists() List[NeighborListOptions][source]¶
Get all the neighbors lists options registered with this
System- Return type:
- add_data(name: str, tensor: TensorMap, override: bool = False)[source]¶
Add custom data to this system, stored as
TensorBlock.This is intended for experimentation with models that need more data as input, and moved into a field of
Systemlater.
- class metatomic.torch.NeighborListOptions(cutoff: float, full_list: bool, strict: bool, requestor: str = '')[source]¶
Options for the calculation of a neighbors list
- Parameters:
cutoff (float) – spherical cutoff radius for the neighbors list, in the model units
full_list (bool) – should the list be a full or half neighbors list
strict (bool) – whether the list guarantee to have no pairs farther than cutoff
requestor (str) – who requested this neighbors list, you can add additional requestors later using
add_requestor()
- property length_unit: str¶
The unit of length used for the cutoff.
This is typically set by
AtomisticModelwhen collecting all neighbors list requests.The list of possible units is available here.
- engine_cutoff(engine_length_unit: str) float[source]¶
Spherical cutoff radius for this neighbors list in engine units.
The engine must provide the unit it uses for lengths, and the cutoff will automatically be converted.
- property full_list: bool¶
Should the list be a full neighbors list (contains both the pair
i->jandj->i) or a half neighbors list (contains only the pairi->j)
- property strict: bool¶
Does the list guarantee to have no pairs beyond the cutoff (strict) or can it also have pairs that are farther apart (non strict)
- add_requestor(requestor: str)[source]¶
Add another
requestorto the list of modules requesting this neighbors list- Parameters:
requestor (str)
- __eq__(other: NeighborListOptions) bool[source]¶
Return self==value.
- Parameters:
other (NeighborListOptions)
- Return type:
- __ne__(other: NeighborListOptions) bool[source]¶
Return self!=value.
- Parameters:
other (NeighborListOptions)
- Return type:
- metatomic.torch.systems_to_torch(systems: IntoSystem | List[IntoSystem], dtype: dtype | None = None, device: device | None = None, positions_requires_grad: bool = False, cell_requires_grad: bool = False) System | List[System][source]¶
Converts a system or a list of systems into a
metatomic.torch.Systemor a list of such objects.- Param:
systems: The system or list of systems to convert.
- Param:
dtype: The dtype of the output tensors. If
None, the default dtype is used.- Param:
device: The device of the output tensors. If
None, the default device is used.- Param:
positions_requires_grad: Whether the positions tensors of the outputs should require gradients.
- Param:
cell_requires_grad: Whether the cell tensors of the outputs should require gradients.
- Returns:
The converted system or list of systems.
- Parameters:
systems (IntoSystem | List[IntoSystem])
dtype (dtype | None)
device (device | None)
positions_requires_grad (bool)
cell_requires_grad (bool)
- Return type:
- class metatomic.torch.systems_to_torch.IntoSystem[source]¶
A type that can be converted into a
metatomic.torch.System.This is an abstract class that is used to indicate a class whose objects can be converted into a
System. For the moment, the only supported type isase.Atoms.
- metatomic.torch.register_autograd_neighbors(system: System, neighbors: TensorBlock, check_consistency: bool)[source]¶
Register a new torch autograd node going from (
system.positions,system.cell) to theneighborsdistance vectors.This does not recompute the distance vectors, but work as-if all the data in
neighbors.valueswas computed directly fromsystem.positionsandsystem.cell, allowing downstream models to use it directly with full autograd integration.- Parameters:
system (System) – system containing the positions and cell used to compute the neighbors list
neighbors (TensorBlock) – neighbors list, following the same format as
System.add_neighbor_list()check_consistency (bool) – can be set to
Trueto run additional checks in case the data in neighbors does not follow what’s expected.
Serialization¶
Below are functions to load and save metatomic systems to disk. The
serialization format is based on numpy’s .npz.
- metatomic.torch.save(file: str | Path | BinaryIO, system: System) None[source]¶
Save a System object to a file.
The provided System must contain float64 data and be on the CPU device.
The saved file will be a zip archive containing the following files:
types.npy, containing the atomic types in numpy’s NPY format;positions.npy, containing the systems’ positions in numpy’s NPY format;cell.npy, containing the systems’ cell in numpy’s NPY format;pbc.npy, containing the periodic boundary conditions in numpy’s NPY format;
For each neighbor list in the System object, the following files will be saved (where
{nl_idx}is the index of the neighbor list):pairs/{nl_idx}/options.json: theNeighborListOptionsobject converted to a JSON string.pairs/{nl_idx}/data.mts: the neighbor listTensorBlockobject
For each extra data in the System object, the following file will be saved (where
{name}is the name of the extra data):data/{name}.mts: The extra dataTensorMap
- metatomic.torch.load_system(file: str | Path | BinaryIO) System[source]¶
Load a System object from a file.
The loaded System object will be on the CPU device and contain float64 data.
- metatomic.torch.save_buffer(system: System) Tensor[source]¶
Save the given
systemto an in-memory buffer, represented as a 1-dimensionaltorch.Tensorwithuint8dtype.