Matching positions to grid in GPAW

Slab (2D) geometry.

from ase.build import fcc111
from gpaw import GPAW, PW
from gpaw.utilities import h2gpts
import numpy as np

# Set variables
div   = 4.0    # number of grid points is divisible by div
grid  = 0.16   # desired grid spacing
left  = 6.0    # vacuum at the left border of the slab
vacuum= 8.0    # vacuum at the right border above the slab/adsorbate
cutoff= 400    # PW cut-off
name  ='slab'  # output file name
func  ='RPBE'  # functional name; with libvdwxc you can use:
               # {'name':'BEEF-vdW', 'backend':'libvdwxc'}
kpts  = 4      # number of k-points

# Define a slab with fixed grid and atoms positions
atoms = fcc111('Pt', size=(4,4,4), vacuum=left)
# add adsorbate here
zmax  = np.max([i[2] for i in atoms.get_positions()])
cell  = atoms.get_cell()
cell[2][2]= grid*div*round((zmax+vacuum)/grid/div)
atoms.set_cell(cell)

# Set the default calculator
calc = GPAW(poissonsolver={'dipolelayer':'xy'},
            mode=PW(cutoff),
            xc=func,
            gpts=h2gpts(grid, atoms.get_cell(), idiv=div),
            kpts=(kpt,kpt,1),
            parallel={'augment_grids':True,'sl_auto':True},
            txt=f'{name}.txt',
           )

# Run calculation
atoms.calc = calc
atoms.get_potential_energy()

When choosing the number of k-points, consider using

kpts = {'density': 2.5,'gamma':True,'even':True}

Read
wiki.fysik.dtu.dk/gpaw/documentation/basic.html#manual-kpts
and
wiki.fysik.dtu.dk/gpaw/tutorialsexercises/structureoptimization/surface/surface.html

Molecular (0D) geometry

from ase.io import read, write
from ase.build import molecule
from ase.optimize import QuasiNewton
from gpaw import GPAW, PW
from gpaw.cluster import *
from gpaw.utilities import h2gpts

# Set variables
div   = 4.0    # number of grid points is divisible by div
grid  = 0.16   # desired grid spacing
vacuum= 8.0    # vacuum around the molecule
cutoff= 400    # PW cut-off
name  ='H2O'   # molecule name from ase.build
func  ='RPBE'  # functional name; with libvdwxc you can use:
               # {'name':'BEEF-vdW', 'backend':'libvdwxc'}
fmax  = 0.05   # maximum force in optimization
smax  = 11     # maximum steps in optimization

# Define a box with fixed grid and atoms positions
atoms = Cluster(molecule(name))
atoms.minimal_box(border=vacuum, h=grid, multiple=div)
# atoms.translate([0.01,0.02,0.03]) # probably not needed

# Set calculator
calc = GPAW(mode=PW(cutoff),
            xc = func,
            gpts=h2gpts(grid, atoms.get_cell(), idiv=div),
            parallel={'augment_grids':True,'sl_auto':True},
            txt=f'{name}.txt',
           )

# Run optimization
atoms.calc = calc
opt = QuasiNewton(atoms, trajectory=f'{name}.traj', logfile=f'{name}.log')
opt.run(fmax=fmax, steps=smax)