Positive writing

Here are my notes and thoughts about positive writing.

https://twitter.com/grammarly/status/1457749263904133124

Positive writing helps to communicate better with readers. Naturally, positive writing is more concrete than the negative one. For instance, just removing “not” in  “bananas are not vegetables” or “bananas are not blue” and turning it into positive “bananas are yellow fruits” results in a clear undeniable statement. Another aspect of positive writing is tuning the reader’s attitude towards your ideas. Psychologically, after going through easily agreeable sentences, like “bananas are sweet” and “bananas are colorful”, the reader will be more ready to agree on your conclusion that “a banana is a comfort and nutritious choice for a lunchbox”.

More text with examples are under editing 🙂

External XC libraries for GPAW

There are two libraries of XC functionals that can be used in GPAW. These are libxc and libvdwxc. Conda installation of GPAW automatically picks them. You can check whether your GPAW connects to libxc and libvdwxc like gpaw info.

libvdwxc is useful when you wish to run calculations with vdW-functionals and GPAW. Such as BEEF-vdW. Herewith, libvdwxc implementation of vdW-functionals are better parallelized than the native GPAW implementation. For example, add the following line to your GPAW calculator xc={'name':'BEEF-vdW','backend':'libvdwxc'} to run a calculation with the BEEF-vdW functional. BEEF-vdW calculations with libvdwxc can run as fast as PBE-like calculations if you use the proper grid, like parallel={'augment_grids':True,'sl_auto':True}. Here is a list of libvdwxc functionals: gitlab.com/libvdwxc/libvdwxc

Note that the following GPAW page is somewhat outdated:
wiki.fysik.dtu.dk/gpaw/documentation/xc/vdw.html

libxc is useful when you wish to run calculations with functionals that are not implemented in GPAW. Note that GPAW implementation is more efficient. There are many ways to call for libxc. For example, add the following line to your GPAW calculator xc='MGGA_X_SCAN+MGGA_C_SCAN' to run a calculation with the SCAN functional. Nore that GPAW setups are for LDA, PBE, and RPBE. You can generate setups specifically for your functional if it is GGA or HGGA. Here is a list of libxc functionals: tddft.org/programs/libxc/functionals/

Memory issues in GPAW

Try to use default parameters for the calculator. Simple and often useful.

Below you find a list of suggestions that should be considered when encountering a memory problem – when a calculation does not fit an allocated memory limit.

Note1: You can use –dry-run to make memory estimation and check for parallelization over kpts, domain, and bands as well as use of symmetry.

gpaw python --dry-run=N script.py

Mind that the memory estimation with –dry-run is underestimated. https://gitlab.com/gpaw/gpaw/-/issues/614

Note2: You can use def monkey_patch_timer() to write information about memory usage into mem.* files. Call the function before the actual work is started.

from gpaw.utilities.memory import monkey_patch_timer

monkey_patch_timer()

SUBMISSION OPTIONS

  1. Try increasing the total memory or memory per tasks in the submission script, if you are sure that everything else (see below) is correct.
  2. Try increasing number of tasks (CPUs×threading) and nodes, if only you are sure that everything else (see below) is correct. Note that your calculation accesses all the nodes’ memory independent on the number of allocated tasks, but not not all memory is actually available because some is used by the OS and other running jobs. Also, increasing the number of tasks decreases parallelization efficiency and might decrease the queue priority (depending on the queuing system).

GEOMETRY

  1. Check the model geometry. Perhaps, you can make a more compact model. For example, with orthorhombic=False option.
  2. In slab calculations, use just enough vacuum. Mind that PW mode is egg-box effect free, so, with the dipole-layer correction, you can reduce the vacuum layer significantly. Just check for the energy convergence.
    https://wiki.fysik.dtu.dk/gpaw/tutorialsexercises/electrostatics/dipole_correction/dipole.htm
  3. Ensure that symmetry is used. Sometimes, the calculator uses less symmetry than there is. In that case, recheck the geometry. Remember that you can preserve symmetry during optimization. https://wiki.fysik.dtu.dk/ase/ase/constraints.html#the-fixsymmetry-class

PARALLELIZATION

In general, parallelization over domains requires less memory than parallelization over bands and k-points, but the default order of parallelization is k-points, then domains, then bands. Remember the formula kpts×domain×bands = N, where N is the number of tasks (CPUs).

  1. In most cases, the default parallelization with symmetry is most efficient in terms of memory usage.
  2. Reprioritizing parallelization over domain can reduce memory consumption, but also slow down the calculation as parallelization over k-points is usually more time-efficient.
  3. Parallelization over any type can be suppressed by setting, for example, for domains like parallel = {'domain':1}. In the LCAO mode, you should check whether parallelizing over bands, like parallel = {'bands':2}, helps with the memory issue.

CALCULATOR PARAMETERS

  1. Consider using a different mode. “With LCAO, you have fewer degrees of freedom, so memory usage is low. PW mode uses more memory and FD a lot more.” https://wiki.fysik.dtu.dk/gpaw/documentation/basic.html#manual-mode
  2. Change calculation parameters, like h, cutoff, setups (like setups={'Pt': '10'}), basis (like basis={'H': 'sz', 'C': 'dz'}), etc. Check for convergence of properties, like in this tutorial: wiki.fysik.dtu.dk/gpaw/summerschools/summerschool22/catalysis/catalysis.html#extra-material-convergence-test
  3. It is possible to reduce the memory by changing the mixer options.
    https://wiki.fysik.dtu.dk/gpaw/documentation/convergence.html

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)

Installing GPAW with conda

[Updated on 20.04.2022, 15.04.2023]

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.11
conda activate gpaw
conda install -c conda-forge openmpi
conda install -c conda-forge gpaw=22.8=*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 December 2022, 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.11

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*

Recently, there were problems with openmpi. Try downgrading it to version 4.1.2:

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(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(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.0)

P.S. Note that the TS09 and D4 energies are no outputted to the H2O.txt. They are written to the log-file.

Synchronizing calendars

This post describes ways of pushing MS outlook and Google calendars to Nextcloud.

My main working calendar is the Nextcloud app because I can easily sync it is my Sailfish phone. I also use Google calendar (for sharing family events) and MS outlook calendar (for work). Today I decided to merge all these calendars into a single one that I can sync on all my devices. Here is how.

MS to Nextcloud

Use outlookcaldavsynchronizer as recommended in the Nextcloud blog.

Google to Nextcould

  • Get the iCal link from Google Calendar as follows:
  • In the left calendar list menu of Google Calendar, go to the ⋮ menu of the calendar to be shared
  • Click on “Settings and sharing”
  • On the Calendar settings page, scroll down to “Secret address in iCal format”
  • In Nextcloud Calendar’s left menu, click on “+New Calendar” > “New subscription from link (read-only)”
  • Insert the “Secret address in iCal format”
  • Your new calendar subscription will appear in the list; you can change its name or color in the menu of your calendar