Kvectors

torchpme.lib.get_ns_mesh(cell: Tensor, mesh_spacing: float)[source]

Computes the mesh size given a target mesh spacing and cell getting the closest powers of 2 to help with FFT.

Parameters:
  • cell (Tensor) – torch.tensor of shape (3, 3), where cell[i] is the i-th basis vector of the unit cell

  • mesh_spacing (float) – float

  • differentiable – boll

Returns:

torch.tensor of length 3 containing the mesh size

torchpme.lib.get_ns_ewald(cell: Tensor, lr_wavelength: float) → Tensor[source]

Compute the number of k-points per reciprocal direction for the Ewald sum.

The grid keeps all reciprocal space vectors with a wavelength \(\geq\) lr_wavelength, i.e. \(|\mathbf{k}| \leq 2\pi / \lambda\).

For \(\mathbf{k} = \sum_j n_j \mathbf{b}_j\) the integer coefficients obey \(|n_j| = |\mathbf{k} \cdot \mathbf{a}_j| / 2\pi \leq |\mathbf{a}_j| / \lambda\), so keeping \(|n_j| \leq m_j = \lceil |\mathbf{a}_j| / \lambda \rceil\) covers the sphere for any cell shape. An odd ns = 2 m + 1 makes torch.fft.fftfreq() return the symmetric range \(-m, \dots, m\).

Parameters:
  • cell (Tensor) – torch.tensor of shape (3, 3), where cell[i] is the i-th basis vector of the unit cell

  • lr_wavelength (float) – smallest wavelength that is kept in the Fourier sum

Returns:

torch.tensor of length 3 containing the number of k-points

Return type:

Tensor

torchpme.lib.generate_kvectors_for_mesh(cell: Tensor, ns: Tensor) → Tensor[source]

Compute all reciprocal space vectors for Fourier space sums.

This variant is used in combination with mesh based calculators using the fast fourier transform (FFT) algorithm.

Parameters:
  • cell (Tensor) – torch.tensor of shape (3, 3), where cell[i] is the i-th basis vector of the unit cell

  • ns (Tensor) – torch.tensor of shape (3,) and dtype int ns = [nx, ny, nz] contains the number of mesh points in the x-, y- and z-direction, respectively. For faster performance during the Fast Fourier Transform (FFT) it is recommended to use values of nx, ny and nz that are powers of 2.

Returns:

torch.tensor of shape (nx, ny, nz, 3) containing all reciprocal space vectors that will be used in the (FFT-based) mesh calculators. Note that k_vectors[0,0,0] = [0,0,0] always is the zero vector.

Return type:

Tensor

See also

generate_kvectors_for_ewald() for a function to be used for Ewald calculators.

torchpme.lib.generate_kvectors_for_ewald(cell: Tensor, ns: Tensor) → Tensor[source]

Compute all reciprocal space vectors for Fourier space sums.

This variant is used with the Ewald calculator, in which the sum over the reciprocal space vectors is performed explicitly rather than using the fast Fourier transform (FFT) algorithm.

The main difference with generate_kvectors_for_mesh() is the shape of the output tensor (see documentation on return) and the fact that the full set of reciprocal space vectors is returned, rather than the FFT-optimized set that roughly contains only half of the vectors.

Parameters:
  • cell (Tensor) – torch.tensor of shape (3, 3), where cell[i] is the i-th basis vector of the unit cell

  • ns (Tensor) – torch.tensor of shape (3,) and dtype int ns = [nx, ny, nz] contains the number of mesh points in the x-, y- and z-direction, respectively.

Returns:

torch.tensor of shape (n, 3) containing all reciprocal space vectors that will be used in the Ewald calculator. Note that k_vectors[0] = [0,0,0] always is the zero vector.

Return type:

Tensor

See also

generate_kvectors_for_mesh() for a function to be used with mesh based calculators like PME.

Examples using Kvectors

Custom models with automatic differentiation

Custom models with automatic differentiation