torchref.symmetry.cell module
Cell - A dataclass for crystallographic unit cells with cached derived quantities.
Provides a simple container for unit cell parameters with automatic caching of derived quantities (fractional matrix, volume, etc.).
- class torchref.symmetry.cell.Cell(data, *, dtype=torch.float32, device=device(type='cpu'), requires_grad=False)[source]
Bases:
DeviceMixinDataclass for crystallographic unit cells with cached derived quantities.
Stores 6 parameters: [a, b, c, alpha, beta, gamma] - a, b, c: cell lengths in Angstroms - alpha, beta, gamma: cell angles in degrees
Derived quantities (fractional_matrix, volume, etc.) are computed on first access and cached. The cache is cleared when the cell is moved to a different device or dtype.
Examples
>>> cell = Cell([50, 60, 70, 90, 90, 90]) >>> cell.volume # Computed and cached tensor(210000.) >>> cell_gpu = cell.to('cuda') # Move to GPU (returns new Cell) >>> cell_gpu.device.type 'cuda'
- __init__(data, *, dtype=torch.float32, device=device(type='cpu'), requires_grad=False)[source]
Create a new Cell.
- Parameters:
data (array-like) – Unit cell parameters [a, b, c, alpha, beta, gamma]. Can be a list, numpy array, or torch tensor.
dtype (torch.dtype, optional) – Desired data type. Defaults to the configured
dtypes.float.device (torch.device or str, optional) – Desired device. Defaults to the configured
device.current.requires_grad (bool, optional) – Whether to track gradients. Defaults to False.
- Raises:
ValueError – If data does not have exactly 6 elements.
- detach()[source]
Return a new Cell with detached tensor (no gradient tracking).
- Returns:
New Cell with detached data.
- Return type:
- clone()[source]
Return a new Cell with cloned tensor data.
- Returns:
New Cell with cloned data.
- Return type:
- property fractional_matrix: Tensor
Orthogonalization matrix B (fractional -> Cartesian).
Returns the 3x3 matrix B such that: cart = frac @ B.T
- Returns:
Shape (3, 3) orthogonalization matrix.
- Return type:
- property inv_fractional_matrix: Tensor
Fractionalization matrix B^-1 (Cartesian -> fractional).
Returns the 3x3 matrix B^-1 such that: frac = cart @ B^-1.T
- Returns:
Shape (3, 3) fractionalization matrix.
- Return type:
- property volume: Tensor
Unit cell volume in cubic Angstroms.
- Returns:
Scalar tensor with the cell volume.
- Return type:
- property reciprocal_basis_matrix: Tensor
Reciprocal basis matrix with [a*, b*, c*] as rows.
- Returns:
Shape (3, 3) matrix where rows are the reciprocal basis vectors.
- Return type:
- compute_grid_size(max_res, oversampling=3.0)[source]
Compute minimum grid dimensions for a given resolution.
Uses Shannon-Nyquist sampling criterion to determine the minimum number of grid points needed along each axis.
- Parameters:
- Returns:
Minimum grid dimensions (nx, ny, nz).
- Return type:
Examples
>>> cell = Cell([50, 60, 70, 90, 90, 90]) >>> cell.compute_grid_size(2.0) (75, 90, 105)
- tolist()[source]
Convert Cell parameters to a standard Python list.
- Returns:
List of cell parameters [a, b, c, alpha, beta, gamma].
- Return type:
- fractional_to_cartesian(frac_coords)[source]
Convert fractional coordinates to Cartesian coordinates.
- Parameters:
frac_coords (torch.Tensor) – Tensor of fractional coordinates, shape (…, 3).
- Returns:
Tensor of Cartesian coordinates, shape (…, 3).
- Return type:
- cartesian_to_fractional(cart_coords)[source]
Convert Cartesian coordinates to fractional coordinates.
- Parameters:
cart_coords (torch.Tensor) – Tensor of Cartesian coordinates, shape (…, 3).
- Returns:
Tensor of fractional coordinates, shape (…, 3).
- Return type: