An overview on the Libxc library of density functional approximations

Susi Lehtola

Molecular Sciences Institute at Virginia Tech

2 June 2021 Outline

Why Libxc?

Recap on DFT

What is Libxc?

Using Libxc

A look under the hood

Wrapup

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola Why Libxc? 2/28 Why Libxc?

There are many approximations for the exchange-correlation functional.

But, most programs I ... only implement a handful (sometimes 5, typically 10-15) I ... and the implementations may be buggy / non-standard

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola Why Libxc? 3/28 Why Libxc, cont’d

This leads to issues with reproducibility I chemists and physicists do not traditionally use the same functionals! Outdated(?) stereotype: B3LYP vs PBE I how to reproduce a calculation performed with another code?

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola Why Libxc? 4/28 Why Libxc, cont’d

The issue is compounded by the need for backwards and forwards compatibility: how can one I reproduce old calculations from the literature done with a now-obsolete functional (possibly with a program that is proprietary / no longer available)? I use a newly developed functional in an old program?

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola Why Libxc? 5/28 Why Libxc, cont’d

A standard implementation is beneficial! I no need to keep reinventing (and rebuilding) the wheel I use same collection of density functionals in all programs I new functionals only need to be implemented in one place I broken/buggy functionals only need to be fixed in one place I same implementation can be used across numerical approaches, e.g. vs finite elements =⇒ functionals in Libxc can be validated against data in original publications, regardless of which numerical method was used!

E.g. Gaussian-basis calculations afford extremely sensitive verification

I < 1µEh level agreement for total energies typically achievable between different codes when density functional implementations are equivalent

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola Why Libxc? 6/28 Outline

Why Libxc?

Recap on DFT

What is Libxc?

Using Libxc

A look under the hood

Wrapup

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola Recap on DFT 7/28 Brief recap on DFT

Density functionals can be classified on Jacob’s ladder.

The first rung: local density approximation (LDA), for which the energy density per particle xc depends only on the local electron spin-density nσ(r) or Z Z 3 3 Exc = fxc(n↑(r), n↓(r))d r = n(r)xc(n↑(r), n↓(r))d r

LDAs can be derived from the homogeneous electron gas.

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola Recap on DFT 8/28 Brief recap on DFT, cont’d

Meta-local density approximation (meta-LDA) functionals depend on nσ(r) and the local kinetic energy density τσ, and are still (almost) fully derivable from the homogeneous electron gas Z 3 Exc = n(r)xc(n↑(r), n↓(r), τ↑(r), τ↓(r))d r

occupied 1 X τ = [∇ψ (r)]2 σ 2 iσ i [J. Chem. Theory Comput. 17, 943 (2021)]

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola Recap on DFT 9/28 Brief recap on DFT, cont’d

The next rungs are I generalized gradient approximation (GGA): dependence on nσ(r) and its gradient ∇nσ(r) I meta-GGA approximation: nσ(r), ∇nσ(r) and τσ(r) and/or 2 ∇ nσ(r) which are still ”pure” density functionals. Higher rungs contain non-density-functional contributions I global hybrids: include fraction of Hartree–Fock exchange I range-separated hybrids: include variable fraction of short-range vs long-range exact exchange (various kernels) I local hybrids: local exchange energy enters functional I double hybrids: include post-Hartree–Fock correlation, e.g. Møller–Plesset but still contain a density functional part.

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola Recap on DFT 10/28 Outline

Why Libxc?

Recap on DFT

What is Libxc?

Using Libxc

A look under the hood

Wrapup

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola What is Libxc? 11/28 What is Libxc?

Libxc is a library that I evaluates the density functional contributions I provides the parameters to be used for non-local contributions e.g. exact exchange, VV10 non-local correlation, MP2 correlation energy etc that need to be evaluated by the calling program

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola What is Libxc? 12/28 What is Libxc, cont’d

Libxc currently implements 600+ density functionals xc (currently!). I last presentation on Libxc by M.A.L. Marques in 2012: ”There are many approximations for the xc (probably of the order of 250–300)”

Libxc supports I exchange, correlation, exchange-correlation, and kinetic energy functionals (for orbital-free DFT) I pure functionals, global hybrids, range-separated hybrids (various kernels: error function, Yukawa, etc) I up to 4th derivatives for (almost) all functionals I double hybrids in the upcoming major release and uses a permissible Mozilla Public License that allows its inclusion also in non-free/non-open-source programs.

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola What is Libxc? 13/28 What is Libxc, cont’d

Libxc is currently used in over 30 electronic structure programs differing in numerical approaches, size of developer community, as well as development model (free software vs open source vs proprietary/closed-source/commercial).

I Abinit I entos I MRCC I ACE-Molecule I ERKALE I I ADF I exciting I ORCA APE FHI-AIMS I I I PROFESS I AtomPAW I GAMESS (US) I Psi4 I BAGEL I GPAW I PySCF I BigDFT I HelFEM I QuantumATK I CP2K I Horton Quantum Espresso I DFT-FE I JDFTx I I DP I MADNESS I Turbomole I Chronus Quantum I MOLGW I WIEN2k I Elk I Molpro I Yambo

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola What is Libxc? 14/28 Side note: Libxc and GPAW

GPAW was one of the early adopters of Libxc I Libxc started in 2004 I GPAW had an interface in 2007; from GPAW git log commit 2dc204a9aeb0adb5de21a22a714705e9881793f2 Author: Jens J?rgen Mortensen Date: Mon Jul 16 12:31:53 2007 +0000

Remember libxc files when compiling gpaw−python

My history is more recent I 2012: I started using (and patching) Libxc as a grad student I wrote ERKALE, the first(?) Gaussian-basis code to fully interface Libxc I 2014: I got commit rights to Libxc and became one of the main authors

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola What is Libxc? 15/28 Outline

Why Libxc?

Recap on DFT

What is Libxc?

Using Libxc

A look under the hood

Wrapup

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola Using Libxc 16/28 Libxc interface

I Libxc has native interfaces to C, Fortran 2003, and Python; I will showcase the C interface given in xc.h I Before use, functional instance is constructed with xc func init; after use, it needs to be destroyed with xc func end /∗∗ Initializes a functional by id with nspin spin channels ∗/ i n t x c f u n c i n i t ( x c f u n c t y p e ∗p , i n t functional , i n t n s p i n ) ; /∗∗ Destructor for an initialized functional ∗/ v o i d x c f u n c e n d ( x c f u n c t y p e ∗p ) ;

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola Using Libxc 17/28 External parameters

Parameters need to be adjustable to e.g. tune the range-separation parameter for ionization potential tuning [Annu. Rev. Phys. Chem. 61, 85 (2010)] or triplet tuning [J. Chem. Theory Comput. 15, 1226 (2019)] I Parameters like µ and κ for PBE exchange can be set for many functionals with

/∗∗ Sets all external parameters for a functional ∗/ v o i d x c f u n c s e t e x t p a r a m s ( x c f u n c t y p e ∗p , const double ∗ e x t p a r a m s ) ; /∗∗ Sets an external parameter by name for a functional ∗/ v o i d x c f u n c s e t e x t p a r a m s n a m e ( x c f u n c t y p e ∗p , const char ∗name , double par ) ;

I N.B. in libxc <6.0.0 xc func set ext params name resets all other parameters to default values

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola Using Libxc 18/28 Evaluation of functionals

I Many routines exist for evaluating functionals; e.g. these for the energy and its 1st derivative

/∗∗ Evaluates the energy density and its first derivative for an LDA functional ∗/ v o i d x c l d a e x c v x c ( const x c f u n c t y p e ∗p , s i z e t np , const double ∗rho , double ∗zk , double ∗ vrho ) ; /∗∗ Evaluates the energy density and its first derivative for a GGA functional ∗/ v o i d x c g g a e x c v x c ( const x c f u n c t y p e ∗p , s i z e t np , const double ∗rho , const double ∗sigma , double ∗zk , double ∗vrho , double ∗ vsigma ) ; /∗∗ Evaluates the energy density and its first derivative for a meta−GGA functional ∗/ v o i d x c m g g a e x c v x c ( const x c f u n c t y p e ∗p , s i z e t np , const double ∗rho , const double ∗sigma , const double ∗ l a p l , const double ∗tau , double ∗zk , double ∗vrho , double ∗vsigma , double ∗ v l a p l , double ∗ vtau ) ;

I p is pointer to previously initialized instance I np is number of grid points I rho is electron density np−1 I spin-restricted i.e. spin-unpolarized: {n(ri )}i=0 np−1 I spin-unrestricted i.e. spin-polarized: {n↑(ri ), n↓(ri )}i=0 I zk is energy density per particle I vrho is derivative ∂fxc/∂nσ (same ordering as for rho)

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola Using Libxc 19/28 Outline

Why Libxc?

Recap on DFT

What is Libxc?

Using Libxc

A look under the hood

Wrapup

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola A look under the hood 20/28 Libxc before

Libxc was originally based on hand-written C code I Functional itself i.e. energy: pretty simple I First derivatives: compute derivatives wrt all parameters by hand, figure out optimal evaluation, implement I Second derivatives: already hairy for many functionals

But, higher-order derivatives are sometimes needed for e.g. response properties I DFT self-consistent field: 1st derivative (employing variational algorithm) I DFT nuclear gradients: 2nd derivatives I ... I TD-DFT vibrational frequencies: 4th derivatives needed!

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola A look under the hood 21/28 Libxc before, cont’d

The number of input parameters may already be non-trivial.

I LDAs have two input parameters: n↑ and n↓ I GGAs have five input parameters: n↑, n↓, ∇n↑ · ∇n↑, ∇n↑ · ∇n↓, ∇n↓ · ∇n↓

I meta-GGAs have seven / nine input parameters: n↑, n↓, 2 2 ∇n↑ · ∇n↑, ∇n↑ · ∇n↓, ∇n↓ · ∇n↓, τ↑, τ↓, ∇ n↑, ∇ n↓

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola A look under the hood 22/28 Libxc before, cont’d

2, 5, 7/9 parameters for LDAs, GGAs, and meta-GGAs yield the following number of derivatives I 1 zeroth derivative: the energy density I n first derivatives: 2, 5, 7/9 I n × (n + 1)/2 second derivatives: 3, 15, 28/45 I n × (n + 1)/2 × (n + 2)/3 third derivatives: 4, 35, 84/165 I n × (n + 1)/2 × (n + 2)/3 × (n + 3)/4 fourth derivatives: 5, 70, 210/495 ∴ including higher derivatives requires automated algorithms

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola A look under the hood 23/28 Current Libxc: Maple

Libxc started switching over to Maple some years ago in the version 4.x.x series; currently all functionals are implemented via Maple.

Using a computer algebra system greatly facilitates bug-free and fast implementation of functionals. For instance, this is what PBE exchange looks like

(∗ The enhancement factor in terms of s ∗) p b e f 0 := s −> 1 + params a kappa ∗ (1 − params a kappa/(params a kappa + params a mu∗ s ˆ 2 ) ) : (∗ Transform from libxc ’s x variable into the s variable ∗) p b e f := x −> p b e f 0 ( X2S∗x ) : (∗ Multiplies the enhancement with LDA exchange ∗) f := (rs, z, xt, xs0, xs1) −> gga exchange(pbe f, rs, z, xs0, xs1): Maple is especially useful for complicated functionals; e.g. the implementation of r2SCAN on top of SCAN, rSCAN and PW92 is about 100 lines of Maple code; the original published equations are 4 pages long.

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola A look under the hood 24/28 Current Libxc: Maple, cont’d

We have also been able to eliminate several issues with numerical stability in recent releases I several floating point errors, e.g. FE DIVBYZERO, FE INEXACT, FE OVERFLOW have been removed I in regions where double precision arithmetic is not accurate we switch over to automatically computed asymptotic polynomial expansions I expansions always occur for range-separated functionals: evaluating short-range exchange at large r tends to be numerically challenging (CAM-B3LYP, HSE06, etc) The new code is included in Libxc 5.1.x enabling good numerical stability =⇒ tight convergence criteria can be used

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola A look under the hood 25/28 Outline

Why Libxc?

Recap on DFT

What is Libxc?

Using Libxc

A look under the hood

Wrapup

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola Wrapup 26/28 Summary

The Libxc library of density functional approximations I offers fast and standard implementations of 600+ density functionals I supports up to 4th derivatives for most functionals I is licensed with a permissible open-source license I is interfaced to > 30 electronic structure programs based on various numerical approaches

Found a bug? Want / want us to implement a new functional? I Contact us at https://gitlab.com/libxc/libxc/

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola Wrapup 27/28 Acknowledgments

I original author / BDFL of Libxc: Miguel A. L. Marques I Molecular Sciences Software Institute [NSF grant OAC-1547580]: 5+ months of full-time work on Libxc since October 2020

GPAW 2021: Users’ and Developers’ Meeting Susi Lehtola Wrapup 28/28