compute_grid Subroutine

public subroutine compute_grid(mol, cutoff, grid, det, lat_inv, cell_w, origin)

Computes linked cell grid

Arguments

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

Stucture type

real(kind=wp), intent(in) :: cutoff

Interaction cutoff radius

type(grid_type), intent(out) :: grid

Linked cell grid type

real(kind=wp), intent(out) :: det

Determinant (Volume) of the lattice

real(kind=wp), intent(out), optional :: lat_inv(3,3)

Inverse of the lattice matrix, zero for a molecular system

real(kind=wp), intent(out), optional :: cell_w(3)

Perpendicular width of each grid cell

real(kind=wp), intent(out), optional :: origin(3)

Origin of the grid, fractional coordinates are taken relative to it


Source Code

subroutine compute_grid(mol, cutoff, grid, det, lat_inv, cell_w, origin)

   !> Stucture type
   type(structure_type), intent(in) :: mol

   !> Interaction cutoff radius
   real(wp), intent(in) :: cutoff

   !> Linked cell grid type
   type(grid_type), intent(out) :: grid

   !> Determinant (Volume) of the lattice
   real(wp), intent(out) :: det

   !> Inverse of the lattice matrix, zero for a molecular system
   real(wp), intent(out), optional :: lat_inv(3, 3)

   !> Perpendicular width of each grid cell
   real(wp), intent(out), optional :: cell_w(3)

   !> Origin of the grid, fractional coordinates are taken relative to it
   real(wp), intent(out), optional :: origin(3)

   real(wp) :: height(3), normal(3), grid_basis(3, 3)
   real(wp) :: min_xyz(3), max_xyz(3), grid_origin(3)
   integer :: i, ndim, p_axes(3)

   call get_periodic_axes(mol%periodic, ndim, p_axes)

   grid%pbc(:) = .false.
   grid%pbc(p_axes(:ndim)) = .true.

   grid_origin(:) = 0.0_wp
   if (present(lat_inv)) lat_inv = 0.0_wp

   select case (ndim)
   case (0)
      ! Molecule: construct a Cartesian grid enclosing all atoms
      min_xyz = minval(mol%xyz, dim=2) - buffer
      max_xyz = maxval(mol%xyz, dim=2) + buffer

      grid_origin = min_xyz

      ! Number of cells: must be at least 1, and cell width >= cutoff
      grid%n_xyz = max(1, floor((max_xyz - min_xyz) / (cutoff + tiny(1.0_wp))))

      if (present(cell_w)) then
         cell_w = (max_xyz - min_xyz) / (real(grid%n_xyz, wp) + tiny(1.0_wp)) &
            & + tiny(1.0_wp)
      end if

      det = product(max_xyz - min_xyz)

   case (1, 2)
      ! Wire or slab: complete the missing grid basis vectors
      grid_basis = mol%lattice
      call complete_grid_basis(mol, ndim, p_axes, grid_basis, grid_origin)

   case (3)
      ! Fully periodic: use the physical lattice as the grid basis
      grid_basis = mol%lattice

   case default
      return

   end select

   if (ndim > 0) then
      ! Inverse Lattice Matrix, nonperiodic directions span the structure
      det = matdet_3x3(grid_basis)

      if (present(lat_inv)) lat_inv = matinv_3x3(grid_basis)

      ! Perpendicular height H1 (normal to a2 x a3)
      normal = crossprod(grid_basis(:, 2), grid_basis(:, 3))
      height(1) = abs(det) / norm2(normal)

      ! Perpendicular height H2 (normal to a3 x a1)
      normal = crossprod(grid_basis(:, 3), grid_basis(:, 1))
      height(2) = abs(det) / norm2(normal)

      ! Perpendicular height H3 (normal to a1 x a2)
      normal = crossprod(grid_basis(:, 1), grid_basis(:, 2))
      height(3) = abs(det) / norm2(normal)

      ! Number of cells: must be at least 1, and linked cell height >= cutoff
      do i = 1, 3
         grid%n_xyz(i) = max(1, floor(height(i) / (cutoff + tiny(1.0_wp))))
      end do

      if (present(cell_w)) cell_w = height / real(grid%n_xyz, wp)
   end if

   grid%ncell = product(grid%n_xyz)
   grid%periodic = ndim > 0

   if (present(origin)) origin = grid_origin

end subroutine compute_grid