Using the library
libvdwxc evaluates the non-local correlation part of vdW functionals. This complements libxc which provides implementations of the local parts.
To use libvdwxc, initialize a libvdwxc data handle which is essentially a pointer to a struct containing buffers and metadata. This pointer is then passed to other libvdwxc functions. When initializing a data handle, the caller must choose:
Whether to use one or two spin channels
Which van der Waals functional to use
Which parallelization to use
The unit cell (i.e., simulation box)
The specific steps are described below.
To call functions from libvdwxc, include this header:
# include "vdwxc.h"
To use libvdwxc with MPI, also include:
# include "vdwxc_mpi.h"
You can query whether libvdwxc is compiled with MPI or PFFT support by calling:
int vdwxc_has_mpi(void);
int vdwxc_has_pfft(void);
They return 1 if available and 0 if not.
To perform a computation with libvdwxc, first create a libvdwxc handle.
Use vdwxc_new() for spin-paired computations (one spin-channel)
or vdwxc_new_spin() for spin-polarized computations (two spin-channels):
vdwxc_data vdwxc_new(int functional)
Or:
vdwxc_data vdwxc_new_spin(int functional)
The call returns a libvdwxc data handle, which must be passed when calling other libvdwxc functions. There is no limit to how many handles can be allocated, but they should be freed eventually using:
void vdwxc_finalize(vdwxc_data* data)
functional is an int which specifies the functional.
It can be one of:
VDWXC_DF1 # vdW-DF1
VDWXC_DF2 # vdW-DF2
VDWXC_DFCX # vdW-DF-cx
VDWXC_RVV10 # rVV10
After initializing the libvdwxc handle, specify the unit cell. Non-periodic structures still need a unit cell since arrays must be allocated within it. Pass the libvdwxc handle as first input:
void vdwxc_set_unit_cell(
vdwxc_data data,
int Nx, int Ny, int Nz,
double C00, double C10, double C20,
double C01, double C11, double C21,
double C02, double C12, double C22)
The number of grid points along each direction is Nx, Ny, Nz, and
the three lattice vectors are C00, C10, C20, C01, C11, C21,
and C02, C12, C22 and are measured in Bohr radii. The unit cell
can be set any number of times reusing the same data handle and grid
dimensions Nx, Ny, Nz. If the grid changes, a new vdwxc_data
handle must be created.
Initialize the parallelization by calling one of:
void vdwxc_init_serial(vdwxc_data data)
void vdwxc_init_mpi(vdwxc_data data, MPI_Comm mpi_comm)
void vdwxc_init_pfft(vdwxc_data data, MPI_Comm mpi_comm, int nproc1, int nproc2)
Finally a calculation is performed by calling vdwxc_calculate() or
a similar function. There are currently four such functions, although we
expect to simplify this in the future. These functions take several arrays as
input in the form of double *.
Some arrays are inputs and others are outputs as detailed below.
Arrays must always be contiguous in memory, and for real-space arrays,
z is the contiguous direction. Arrays have no FFT padding. libvdwxc
treats the simulation cell as 3D periodic, so the last element along any
given direction is a direct neighbour of the first element.
For a spin-paired calculation without stress tensor, use:
double vdwxc_calculate(
vdwxc_data data,
double *rho_g, double *sigma_g,
double *dedn_g, double *dedsigma_g)
This computes the non-local correlation energy and the corresponding
potential. The two arrays rho_g and sigma_g are inputs
and will not be written to. dedn_g and dedsigma_g
are outputs are written to but not read from.
They must all have the same length as the number of points in the
grid, except when using MPI parallelization (see below). Generally,
the suffix _g denotes “grid” arrays.
rho_g is the density \(\rho (\mathbf r)\) on the grid
specified by the latest call to vdwxc_set_unit_cell, and
sigma_g is \(|\nabla \rho (\mathbf r)|^2\). They are the same
quantities used in libxc. All physical quantities are in atomic units
with Bohr radii for distances and Hartree for energy, also like libxc.
The call returns the nonlocal van der Waals correlation energy
as a double and writes the derivative of the energy with
respect to the density at each point into dedn_g and the
derivative of the energy with respect to the squared gradient
into dedsigma_g. They are derivatives of the total energy taken with
respect to the values on the grid, divided by the volume per grid point.
To perform the same computation and additionally write a stress tensor, call:
double vdwxc_stress(
vdwxc_data data, double *stress_vv,
double *rho_g, double *sigma_g,
double *dedn_g, double *dedsigma_g)
The nine stress tensor elements, i.e., strain derivatives at fixed
\(\rho\) and \(\sigma\), are written to the output array
stress_vv. The caller adds the density and gradient terms and
divides by the cell volume using its own stress sign convention.
For spin-polarized van der Waals functionals, call:
double vdwxc_calculate_spin(vdwxc_data data,
double *rho_up_g, double *rho_dn_g,
double *sigma_up_g, double *sigma_dn_g,
double *dedn_up_g, double *dedn_dn_g,
double *dedsigma_up_g, double *dedsigma_dn_g)
where all density arrays have two inputs or corresponding outputs for spin “up” and spin “down” respectively.
To additionally compute a stress tensor, use:
double vdwxc_stress_spin(
vdwxc_data data, double *stress_vv,
double *rho_up_g, double *rho_dn_g,
double *sigma_up_g, double *sigma_dn_g,
double *dedn_up_g, double *dedn_dn_g,
double *dedsigma_up_g, double *dedsigma_dn_g)
with the same conventions as above.
To print metadata about a vdwxc_data object,
use:
void vdwxc_print(vdwxc_data data)
To return the above printout as a string, use:
int vdwxc_tostring(vdwxc_data data, int maxsize, char *str);
str is an input string buffer with maxsize bytes.
Call it as follows:
int bufferlength = vdwxc_tostring(data, 0, NULL);
char *buffer[bufferlength + 1];
vdwxc_tostring(data, bufferlength, buffer);
printf("%s", buffer);
This example first calls it with maxsize 0 and a NULL buffer
which computes the string length in bytes without writing anything.
Then allocate one more byte than that for the NULL terminator
(cf. sprintf from the C string library) and call again
to actually write to the buffer.
The spin and stress features were not present in the original
implementation of libvdwxc. You can check for the presence of these
features by the precompiler variables VDWXC_HAS_SPIN and
VDWXC_HAS_STRESS, respectively.
Parallelization
libvdwxc can work on distributed densities. All real-space arrays
then use the same distribution. With FFTW-MPI, parallelization takes
place over the first axis following the conventions of FFTW-MPI.
FFTW-MPI takes a blocksize, so a grid of [nx, ny, nz] is divided
in chunks of [blocksize, ny, nz], where some cores may have
fewer than blocksize elements along the first axis.
libvdwxc specifies a blocksize of ceil(nx / ncores) to FFTW-MPI,
so callers of libvdwxc must supply inputs in the same shapes.
The last core (or cores) may have fewer than blocksize elements,
or no data.
See also the documentation of FFTW-MPI for details on array distribution.
It may be challenging for a caller to achieve the correct layout of the density to call libvdwxc in parallel, but any code which already calls FFTW-MPI necessarily contains this functionality in some form.
When running in parallel, libvdwxc returns local energy and (when applicable) stress contributions from each core. The caller must take a sum over all ranks of the energy or stress contributions to get the total energy or total stress tensor.
It is possible to additionally parallelize libvdwxc with PFFT. However PFFT is not as widely used and may not currently be maintained, so we do not document this.