get_wignerseitz_translations Subroutine

public subroutine get_wignerseitz_translations(mol, trans, error)

Generate lattice translations containing all competing nearest images.

With the periodic lattice vectors A, the metric G = A^T A, and the fractional coordinates s = G^-1 A^T r of a pair vector r, the image translated by A n has the squared distance |r_perp|^2 + (s - n)^T G (s - n). Any admissible candidate image with parallel squared distance d0^2 bounds the nearest image, hence all images within the switching interval satisfy (s - n)^T G (s - n) <= d0^2 + tol. By the Cauchy-Schwarz inequality, this ellipsoid is confined to |n_k - s_k| <= sqrt((d0^2 + tol) G^-1_kk).

The bound holds for any basis of the lattice. A reduced basis keeps it small for skewed cells, the translations are generated in this basis.

Arguments

Type IntentOptional Attributes Name
type(structure_type), intent(in) :: mol

Molecular structure data

real(kind=wp), intent(out), allocatable :: trans(:,:)

Lattice translation vectors

type(error_type), intent(out), allocatable :: error

Error handling


Source Code

subroutine get_wignerseitz_translations(mol, trans, error)

   !> Molecular structure data
   type(structure_type), intent(in) :: mol

   !> Lattice translation vectors
   real(wp), allocatable, intent(out) :: trans(:, :)

   !> Error handling
   type(error_type), allocatable, intent(out) :: error

   integer :: iat, jat, k, kmin
   real(wp) :: basis(3, 3), lat(3, 3), ginv(3, 3), length(3), bound(3)
   real(wp) :: sij(3), diff(3), perp2, par2, step
   real(wp), allocatable :: frac(:, :), perp(:, :)

   if (.not. any(mol%periodic)) then
      allocate(trans(3, 1), source=0.0_wp)
      return
   end if

   call get_inverse_metric(mol%periodic, mol%lattice, basis, ginv, error)
   if (allocated(error)) return

   call reduce_basis(basis)
   call get_inverse_metric(mol%periodic, basis, lat, ginv, error)
   if (allocated(error)) return

   ! Shortest periodic lattice vector, used to step away from self images
   length(:) = norm2(lat, dim=1)
   kmin = minloc(length, dim=1, mask=length > 0.0_wp)

   ! Fractional coordinates in the periodic subspace and perpendicular part
   ! of all atoms, the pair quantities are their differences
   allocate(frac(3, mol%nat), perp(3, mol%nat))
   do iat = 1, mol%nat
      frac(:, iat) = matmul(ginv, matmul(transpose(lat), mol%xyz(:, iat)))
      perp(:, iat) = mol%xyz(:, iat) - matmul(lat, frac(:, iat))
   end do

   bound(:) = 0.0_wp
   !$omp parallel do default(none) schedule(runtime) reduction(max:bound) &
   !$omp& shared(mol, frac, perp, lat, ginv, length, kmin) &
   !$omp& private(iat, jat, k, sij, diff, perp2, par2, step)
   do iat = 1, mol%nat
      do jat = 1, iat
         sij(:) = frac(:, iat) - frac(:, jat)
         perp2 = sum((perp(:, iat) - perp(:, jat))**2)

         ! Rounding gives a nearby lattice point as candidate image
         diff(:) = sij - anint(sij)
         par2 = sum(matmul(lat, diff)**2)

         ! The image search skips the atom itself, in this case step along the
         ! shortest lattice vector until the candidate is clearly not a self image
         if (perp2 + par2 < 4.0_wp * thr) then
            step = aint((2.0_wp * sqrt(thr) + sqrt(par2)) / length(kmin)) + 1.0_wp
            diff(kmin) = diff(kmin) + step
            par2 = sum(matmul(lat, diff)**2)
         end if

         do k = 1, 3
            bound(k) = max(bound(k), abs(sij(k)) + sqrt((par2 + tol) * ginv(k, k)))
         end do
      end do
   end do

   ! Widen the bound for round-off, which otherwise can lose images on the edge
   ! of the switching interval for distant unwrapped atoms
   bound(:) = bound + sqrt(epsilon(1.0_wp)) * (1.0_wp + bound)

   if (.not. (product(2.0_wp * aint(bound) + 1.0_wp) <= real(max_wsc_trans, wp))) then
      call fatal_error(error, &
         & "Too many candidate translations for the Wigner-Seitz cell")
      return
   end if

   call get_lattice_points(lat, int(bound), .true., trans, error)

end subroutine get_wignerseitz_translations