k-points with kplib and gpaw

Choosing optimal k-points is a tricky task. In GPAW, one can set them manually, using size or density and following a rule of thumb:

calc = GPAW(kpts={'size': (4, 4, 4), 'gamma': True})
# or
calc = GPAW(kpts={'density': 2.5, 'gamma': True})

A rule of thumb for choosing the initial k-point sampling is, that the product, ka, between the number of k-points, k, in any direction, and the length of the basis vector in this direction, a, should be:

  • ka ~ 30 Å, for d band metals
  • ka ~ 25 Å, for simple metals
  • ka ~ 20 Å, for semiconductors
  • ka ~ 15 Å, for insulators

Remember that convergence in this parameter should always be checked.

https://wiki.fysik.dtu.dk/gpaw/tutorialsexercises/structureoptimization/surface/surface.html

The corresponding densities (ka/2π) are:

  • ka/2π ~ 4.8 Å, for d band metals
  • ka/2π ~ 4.0 Å, for simple metals
  • ka/2π ~ 3.2 Å, for semiconductors
  • ka/2π ~ 2.4 Å, for insulators

With the recent update, I can start using kplib (see paper) to choose the optimal generalized k-point grids. The main variable in kplib is min_distance, which is analogous to the density×2π. Read more about the min_distance at muellergroup.jhu.edu/K-Points.html.

Here is an example of my conda environment

conda create -n gpaw23 python=3.9
conda activate gpaw23
conda install -c conda-forge cxx-compiler
pip install kplib # from pypi.org/project/kpLib
conda install -c conda-forge gpaw

Here is a working example:

from ase import Atoms
from ase.parallel import parprint
from gpaw import GPAW, PW
from kpLib import get_kpoints
from pymatgen.io.ase import AseAtomsAdaptor

atoms = Atoms(cell=[[1.608145, -2.785389, 0.0], [1.608145, 2.785389, 0.0], [0.0, 0.0, 5.239962]],
              symbols=['Ga', 'Ga', 'N', 'N'],
              positions=[[ 1.608145  , -0.92846486,  2.61536983],
                         [ 1.608145  ,  0.92846486,  5.23535083],
                         [ 1.608145  , -0.92846486,  4.58957792],
                         [ 1.608145  ,  0.92846486,  1.96959692]],
              pbc=True)
structure = AseAtomsAdaptor.get_structure(atoms)
kpts_data = get_kpoints(structure, minDistance=30, include_gamma=False)
    
parprint("Found lattice with kplib: ")
parprint(f"Nominal kpts: {kpts_data['num_total_kpts']}")
parprint(f"Distinct kpts: {kpts_data['num_distinct_kpts']}")

atoms.calc = GPAW(xc='PBE',
                  mode=PW(400),
                  kpts=kpts_data['coords'],
                  symmetry={'point_group': True,
                            'time_reversal': True,
                            'symmorphic': False,
                            'tolerance': 1e-4},
                  txt='gpaw-out.txt')
energy = atoms.get_total_energy()

parprint(f"Total energy: {energy}")
parprint(f"kpts passed to GPAW: {len(atoms.calc.get_bz_k_points())}")
parprint(f"kpts in GPAW IBZ: {len(atoms.calc.get_ibz_k_points())}")

Installing GPAW with conda

[Updated on 20.04.2022, 15.04.2023, 10.06.2023, 03.10.2023, 04.06.2024]

In short, in a clean environment, everything should work with just five lines:

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

Initialize conda. If it is in the .bashch, source it. If not, source “PATHTOCONDA/miniconda3/etc/profile.d/conda.sh”.

conda create --name gpaw -c conda-forge python=3.12
conda activate gpaw
conda install -c conda-forge openmpi ucx
conda install -c conda-forge gpaw=24.1.0=*openmpi*

For details, see the description below.

1. Install conda – software and environment management system.

Here is the official instruction: docs.conda.io/projects/conda/en/latest/user-guide/install/linux.html

On June 2024, run these:

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

If you wish to autostart conda, allow it to write to your .bashrc.

P.S. Here are good intros to conda:

N.B! If the locale is not set, add it to your .bashrc export

LC_ALL=en_US.UTF-8

Without it python might give a segmentation fault (core dumped) error.

2. Create a conda virtual environment:

conda create --name gpaw -c conda-forge python=3.12

If needed, remove the environment as:

conda remove --name gpaw --all

You can check the available environments as:

conda env list

3. Activate the virtual environment.

conda activate gpaw

4. Install gpaw:

Ensure that no interfering modules and environments are loaded.

Purge modules by executing:

module purge

To check whether some code (like mpirun) has an alternative path, try:

which codename

or

codename --version

There should be no mpirun, ase, libxc, numpy, scipy, etc. Otherwise, the installation with conda will most probably fail due to conflicting paths.

4.1. It is safer to install using gpaw*.yml file from vliv/conda directory on FEND:

conda env create -f gpaw.yml

Note that there are many yml files with different versions of GPAW.

4.2. Pure installation is simple but might not work:

conda install -c conda-forge openmpi

conda install -c conda-forge gpaw=*=*openmpi*

In 2022, there were problems with openmpi. Downgrading to version 4.1.2 helped:

conda install -c conda-forge openmpi=4.1.2

You might wish to install ucx but be aware that there are many problems with it, e. g. depending on mlx version:

conda install -c conda-forge ucx

If you get an error about GLIBCXX, try upgrading gcc:

conda install -c conda-forge gcc=12.1.0

4.3. To quickly check the installation, run “gpaw -P 2 test” or “gpaw info”.

The installation might fail. In case you succeed, save the yml file as:

conda env export | grep -v "^prefix: " > gpaw.yml

Now you can use it to install gpaw as:

conda env create -f gpaw.yml

To properly test the installation install pytest and follow wiki.fysik.dtu.dk/gpaw/devel/testing.html. That might take hours.

conda install -c conda-forge pytest pytest-xdist 

5. If needed, install extra packages within your specific conda environment (gpaw).

To apply D4 dispersion correction:

conda install -c conda-forge dftd4 dftd4-python

To analyze trajectories:

conda install -c conda-forge mdanalysis

To analyze electronic density (some might not work):

pip install git+https://github.com/funkymunkycool/Cube-Toolz.git
pip install git+https://github.com/theochem/grid.git
pip install git+https://github.com/theochem/denspart.git
pip install pybader
pip install cpmd-cube-tools
conda install -c conda-forge chargemol

To use catlearn:

pip install catlearn

To work with crystal symmetries:

conda install -c conda-forge spglib

Extra for visualization (matplotlib comes with ASE):

conda install -c conda-forge pandas seaborn bokeh jmol

To use notebooks (you might need to install firefox as well):

conda install -c conda-forge jupyterlab nodejs jupyter_contrib_nbextensions 

6. Run calculations by adding these lines to the submission script:

Note1: Check the path and change the USERNAME

Note2: Turn off ucx.

Note3: You may play with the number of openmp threads.

module purge
source "/groups/kemi/USERNAME/miniconda3/etc/profile.d/conda.sh"
conda activate gpaw
export OMP_NUM_THREADS=1
export OMPI_MCA_pml="^ucx"
export OMPI_MCA_osc="^ucx"
mpirun gpaw python script.py

Note4: Check an example in vliv/conda/sub directory.

7. Speeding-up calculations.

Add the “parallel” keyword to GPAW calculator:

parallel = {'augment_grids':True,'sl_auto':True},

For more options see wiki.fysik.dtu.dk/gpaw/documentation/parallel_runs/parallel_runs.html#manual-parallel. For LCAO mode, try ELPA. See wiki.fysik.dtu.dk/gpaw/documentation/lcao/lcao.html#notes-on-performance.

parallel = {'augment_grids':True,'sl_auto':True,'use_elpa':True},

For calculations with vdW-functionals, use libvdwxc:

xc = {'name':'BEEF-vdW', 'backend':'libvdwxc'},

8. If needed, add fixes.

To do Bayesian error estimation (BEE) see doublelayer.eu/vilab/2022/03/30/bayesian-error-estimation-for-rpbe/.

To use MLMin/NEB apply corrections from github.com/SUNCAT-Center/CatLearn/pulls

9. Something worth trying:

Atomic Simulation Recipes:

asr.readthedocs.io/en/latest/

gpaw-tools:

github.com/lrgresearch/gpaw-tools/

www.sciencedirect.com/science/article/pii/S0927025622000155

ase-notebook (won’t install at FEND because of glibc 2.17):

github.com/chrisjsewell/ase-notebook

ase-notebook.readthedocs.io/en/latest/

Optimizers:

gitlab.com/gpatom/ase-gpatom

gitlab.com/egarijo/bondmin/

gpaw benchmarking:

github.com/OleHolmNielsen/GPAW-benchmark-2021

github.com/mlouhivu/gpaw-benchmarks

members.cecam.org/storage/presentation/Ask_Hjorth_Larsen-1622631504.pdf

d4 parameters fitting:

github.com/dftd4/dftd4-fit

k-point grid choosing:

gitlab.com/muellergroup/kplib

Useful tips

Regex
^.*(A|B).*(A|B).*$
Nano
see https://www.nano-editor.org/dist/latest/cheatsheet.html
alt+U to undo
alt+a to start a selection
alt+shift+} to indent the selection

Bayesian Error Estimation (BEE) for RPBE

The Beyesian Error Estimation (BEE) is implemented in GPAW only for PBE, BEEF-vdW, and mBEEF-vdW.

Here is a trick for making the BEE with the RPBE functional. Just edit the lines in ASE and GPAW codes by adding RPBE as an exception.

To find the needed files, run

find ./ -name "bee.py"

In ase/dft/bee.py change one line:

class BEEFEnsemble:



            if self.xc in ['BEEF-vdW', 'BEEF', 'PBE', 'RPBE']: # add RPBE
                self.beef_type = 'beefvdw'

In gpaw/xc/bee.py add two lines:

class BEEFEnsemble:
    """BEEF ensemble error estimation."""
    def __init__(self, calc):



        # determine functional and read parameters
        self.xc = self.calc.get_xc_functional()
        if self.xc == 'BEEF-vdW':
            self.bee_type = 1
        elif self.xc == 'RPBE': # catch the RPBE exchange functional
            self.bee_type = 1   # assign BEEF coefficients the RBPE

Below we use BEEF-vdW, RPBE, and PBE dimensionless density (n) with gradient (s) and apply BEEF coefficients (E₀, ΔEᵢ) to evaluate the BEE as the standard deviation for the ensemble total energies with the variable enhancement factor (F(s,θᵢ)).


from ase import Atoms
from ase.dft.bee import BEEFEnsemble
from ase.parallel import parprint
from gpaw import GPAW
import time

for xc in ['BEEF-vdW','RPBE','PBE']:
    start_time = time.time()

    h2 = Atoms('H2',[[0.,0.,0.],[0.,0.,0.741]]) #exp. bond length
    h2.center(vacuum=3)
    cell = h2.get_cell()

    calc = GPAW(xc=xc,txt='H2_{0}.txt'.format(xc))
    h2.calc = calc
    e_h2 = h2.get_potential_energy()
    ens = BEEFEnsemble(calc)
    de_h2 = ens.get_ensemble_energies()
    del h2, calc, ens

    h = Atoms('H')
    h.set_cell(cell)
    h.center()
    calc = GPAW(xc=xc,txt='H_{0}.txt'.format(xc), hund=True)
    h.calc = calc
    e_h = h.get_potential_energy()
    ens = BEEFEnsemble(calc)
    de_h = ens.get_ensemble_energies()
    del h, calc, ens

    E_bind = 2*e_h - e_h2
    dE_bind = 2*de_h[:] - de_h2[:]
    dE_bind = dE_bind.std()
    
    parpting('{0} functional'.format(xc))
    parprint('Time: {0} s'.format(round(time.time()-start_time,0)))
    parprint('E_bind: {0} eV'.format(round(E_bind,4)))
    parprint('Error bar {0} eV'.format(round(dE_bind,4)))

TS09 and D4 corrections with ASE

TS09 and D4 are atomic-charge dependent dispersion corrections (see TS09 PRL paper and D4 homepage for the refs). The D4 code is available at github. According to GPAW documentation, TS09 and D4 show for the S26 test set smaller mean deviation than vdW-DF. Herewith, D4 correction does not depend on the actual calculation as it is added to the calculated energy.

Here is how D4 correction can be added with ASE (see Readme) after installing it (for example, as conda install -c conda-forge dftd4 dftd4-python):

from ase.build import molecule 
from ase.calculators.mixing import SumCalculator 
from ase.optimize import BFGS
from dftd4.ase import DFTD4 
from gpaw import GPAW 

atoms = molecule('H2O') 
atoms.center(vacuum=4)

gpaw = GPAW(mode='fd',txt='H2O_D4.txt',xc='PBE') 
atoms.calc = SumCalculator([DFTD4(method='PBE'), gpaw])

#atoms.get_potential_energy()
opt = BFGS(atoms,trajectory='H2O_D4.traj', logfile='H2O_D4.log')
opt.run(fmax=0.05)

Let me stress that before choosing TS09 or D4 one should consider all pro and contra. TS09 method used Hirshfeld charges while D4 uses the electronegativity equilibration method to obtain charges. The former naturally accounts for the interfacial charge transfer while the latter does not. The TS09 correction requires vdW radii and is implemented for a limited set on functionals (see ASE code), like PBE, RPBE, and BLYP. The D4 correction supports much more functionals (see parameters). Regarding the vdW radii values for TS09 bare in mind that there are four data sources – one in GPAW, two in ASE and one more in ASE.

Here is how TS09 correction can be added with ASE and GPAW:

from ase.build import molecule
from ase.calculators.vdwcorrection import vdWTkatchenko09prl
from ase.data.vdw_alvarez import vdw_radii
from ase.optimize import BFGS
from gpaw.analyse.hirshfeld import HirshfeldPartitioning
from gpaw.analyse.vdwradii import vdWradii
from gpaw import GPAW

atoms = molecule('H2O')
atoms.center(vacuum=4)

gpaw = GPAW(mode='fd',txt='H2O_TS.txt',xc='PBE')
atoms.calc = vdWTkatchenko09prl(HirshfeldPartitioning(gpaw), vdWradii(atoms.get_chemical_symbols(), 'PBE'))

#atoms.get_potential_energy()
opt = BFGS(atoms,trajectory='H2O_TS.traj', logfile='H2O_TS.log')
opt.run(fmax=0.05)

N.B! Note that the TS09 and D4 energies are no outputted to the H2O.txt. They are written to the log-file.

Installation of LibXC 4.0.0 trunk + GPAW1.3.0 + ASE

Assume that all the requirements are fulfilled:

  • Python 2.7-3.5
  • NumPy 1.6.1 or later (base N-dimensional array package)
  • ASE 3.15.0 or later (atomic simulation environment)
  • a C-compiler
  • LibXC 2.0.1 or later
  • BLAS and LAPACK libraries

Optional, but highly recommended:

  • SciPy 0.7 or later (library for scientific computing, requirered for some features)
  • an MPI library (required for parallel calculations)
  • FFTW (for increased performance)
  • BLACS and ScaLAPACK

LibXC compilation:

svn co http://www.tddft.org/svn/libxc/trunk/ libxc
cd libxc
autoreconf -i
./configure --enable-shared --prefix=/home/USER/xc
make -j N
make install

The LibXC compilation might not work, and GPAW would complain, so configure as follows:

./configure CFLAGS="-O2 -fPIC" --prefix=/home/USER/xc

After compiling LibXC add these lines to your .bashrc:

export C_INCLUDE_PATH=/home/USER/xc/include
export LIBRARY_PATH=/home/USER/xc/lib
export LD_LIBRARY_PATH=/home/USER/xc/lib

Let’s install ASE using pip, because it is easy.

pip install --upgrade --user ase

Get the GPAW source code and remove in libxc.c in c/xc/ line xc_mgga_x_tb09_set_params(self->functional[0], c);. Them compile GPAW with python setup.py install --user. You might want to add the .local/bin to the path.

Use either Python or Python3, and be consistent with that.

The official guideline also recommends adding these lines to your .bashrc:

export PYTHONPATH=/home/USER/gpaw:$PYTHONPATH
export PATH=/home/USER/tools:$PATH

Don’t forget to get setups. E.g. execute gpaw install-data DIR. After that run the tests.

Transpose paste

Despite python etc we still heavily rely on the tables. Sometimes it is need to transpose a vertical data-set to a horizontal representation. That is easy. In LibreOffice Calc use special paste (Shift+Ctrl+v) and tick transpose and numbers. One can do similar trick in Excel. That is it.

Installation of LibXC 3.0.0 trunk + GPAW1.1.0 + ASE

For a long time we wanted to try SCAN functional implemented in LibXC using GPAW. However, at first, fresh LibXC 3.0.0 did not work. Then we could not compile GPAW. Unit today. Here is a recipe that works for Fedora 25.

First, let’s prepare clean Fedora 25:

sudo dnf groupinstall "Development Tools"
sudo dnf groupinstall 'C Development Tools and Libraries'
sudo dnf install gcc-gfortran python-devel zlib-devel
sudo dnf install python-pip blas-devel lapack-devel atlas-devel openblas-devel rpm-build
sudo dnf install openmpi-devel scalapack-openmpi-devel blacs-openmpi-devel
sudo pip install --upgrade pip
pip install --upgrade --user numpy scipy matplotlib
sudo dnf install nano

Nano is installed in case you don’t like vi or emacs. Some packages might not be needed, but we installed them anyway.

LibXC compilation:

svn co http://www.tddft.org/svn/libxc/trunk/ libxc
cd libxc
autoreconf -i
./configure --enable-shared --prefix=/home/USER/xc
make -j N
make install

After compiling LibXC add these lines to your .bashrc:

export C_INCLUDE_PATH=/home/USER/xc/include
export LIBRARY_PATH=/home/USER/xc/lib
export LD_LIBRARY_PATH=/home/USER/xc/lib

Let’s install ASE using pip, because it is easy.

pip install --upgrade --user ase

Get the GPAW source code and remove in libxc.c in c/xc/ line “xc_mgga_x_tb09_set_params(self->functional[0], c);”. Them compile GPAW with python setup.py install --user.  YOu might want to add the .local/bin to the path.

Don’t forget to get setups. E.g. execute gpaw install-data DIR. After that try this example:


from ase import Atom, Atoms
from gpaw import GPAW
xc = 'MGGA_X_SCAN+MGGA_C_SCAN'
bulk = Atoms([Atom('Li')], pbc=True)
k = 4
g = 8
calc = GPAW(gpts=(g, g, g), kpts=(k, k, k),
xc=xc)#, txt=None)
bulk.set_calculator(calc)
bulk.get_potential_energy()

pdf optimisation

While preparing an online report for the PUT1107 project, I encountered a limit for uploaded pdf-files as low as 3 Mb. Thus, I was forced to reduce the pdf-file size to this limit as follows:

1. I merged a set of articles into one files: pdftk 1.pdf 2.pdf 3.pdf output set.pdf

2. Then I reduced the size of the resulting file: gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=out.pdf set.pdf

The size was reduced by more than 50% with almost the same visual quality.

Taming the equations in Libreoffice

Working on large documents with many equations in a word processor is a torture. In my case, booklets of chemistry problems require a lot of work. For certain reason I prefer to use LibreOffice. When is needed to reformat all equations in a document the following macro is very useful:

Sub FormulaFontSizeChanger

o = ThisComponent.getEmbeddedObjects()

fontSize = 12

fontFamily = “Arial”

For i = 0 to o.count-1

if (not IsNull(o(i))) and (not IsNull(o(i).Model)) then

o(i).Model.TopMargin = 0

o(i).Model.BottomMargin = 0

o(i).Model.LeftMargin = 0

o(i).Model.RightMargin = 0

o(i).Model.BaseFontHeight = fontSize

o(i).Model.FontNameVariables = fontFamily

o(i).Model.FontVariablesIsItalic = 1

o(i).Model.FontNameFunctions = fontFamily

o(i).Model.FontNameNumbers = fontFamily

o(i).Model.FontNameText = fontFamily

o(i).Component.BaseFontHeight = fontSize

o(i).ExtendedControlOverEmbeddedObject.update()

endif

Next i

End Sub

 

P.S. The script might be useful also when writing a thesis with a lot of chemistry inside and many Zotero references. LaTeX might not be so comfortable, and in Word one is still limited with few math fonts.