Skip to content

VASP Tutorial

This example uses VASP to calculate forces and energies. This is a non-free calculator which needs to be installed/setup separately. This example can be easily adapted to some other DFT calculator (e.g. QuantumEspresso, AbInit etc.)

Source of the results

The code below requires VASP and a site-specific run-calc wrapper. The numerical output and plots shown here come from the archived notebook run of the 1x1x1 cell; the resulting DFSET files are shipped in example/VASP_3C-SiC_calculated/. The figures are the archived images kept in docs/figures/. Regenerating these outputs automatically is planned as a separate step.

Setup

First we need to setup the environment by importing required tools from ASE, tqdm and hecss libraries.

# Import VASP calculator and unit modules
from ase.calculators.vasp import Vasp
from ase import units as un
from os.path import isfile
import os

# The sample generator, monitoring display and dfset writer
from hecss import HECSS
from hecss.util import write_dfset
from hecss.monitor import plot_stats

# Numerical and plotting routines
import numpy as np
from matplotlib import pyplot as plt

System setup

You can run a more realistic test using a 2x2x2 (or even larger) supercell. The results generated by such a calculation are included in the example/VASP_3C-SiC_calculated directory in the source tree. You can run post-processing on these results and use them as a reference. If you want to run your own calculation be prepared for substantial computational resources: a single 2x2x2 job takes 2-5 minutes on a 64-core computing node. Better test first with a single unit cell setup. For this tutorial we will run with a very small system - just a single unit cell of 3C-SiC crystal.

supercell = '1x1x1'  # use '2x2x2' for the more realistic run

# Directory in which our project resides
base_dir = f'example/VASP_3C-SiC/{supercell}/'

Calculator setup

You need to configure your own environment for VASP. The run-calc script located in the root directory is an example from my own system. You need to modify it to fit your system. Running run-calc in a given directory should submit a VASP job to your cluster and wait for it to end. Example of such job script from my system (small cluster with SLURM job menager) is included in example/scripts directory.

# Read the structure (previously calculated unit(super) cell)
# The command argument is specific to the cluster setup
calc = Vasp(label='cryst', directory=f'{base_dir}/sc_{supercell}/', restart=True)

# This just makes a copy of atoms object
# Do not generate supercell here - your atom ordering will be wrong!
cryst = calc.atoms.repeat(1)

If you have magmoms in your system you need to use following temporary fix for a bug in magmom handling in Vasp2 calculator:

if 'magmom' in calc.list_float_params:
    calc.list_float_params['magmom'] = cryst.get_initial_magnetic_moments()
Apply this workaround before attaching the calculator to the structure.

# Setup the calculator - single point energy calculation
# The details will change here from case to case
# We are using run-calc from the current directory!
calc.set(directory=f'TMP/calc_{supercell}/sc')
calc.set(command=f'{os.getcwd()}/run-calc "3C-SiC-{supercell}"')
calc.set(nsw=0)
cryst.calc = calc

System check

You should probably check the calculator setup and the stress tensor of the supercell to make sure it is in equilibrium before running long sequence of DFT calculations. Here is an example:

print('Stress tensor: ', end='')
for ss in calc.get_stress()/un.GPa:
    print(f'{ss:.3f}', end=' ')
print('GPa')

Recorded output from the archived 1x1x1 run:

Stress tensor: 0.017 0.017 0.017 0.000 0.000 0.000 GPa

Running HECSS

There are multiple ways you can use HECSS to generate samples -- depending from the stage your project is, your familiarity with python language and sophistication required by your project. The plots below are the archived run outputs saved in docs/figures/; the progress bars from that run are omitted.

You would probably start with more exploratory setup when you start the project and do not know yet the behaviour of the system. It is best to start with small structure, small initial number of samples and code structure which allows for iterating by returning to the same calculation to generate more samples or breaking the loop which goes wrong. This type of calculation may be well served by the code like the following example:

# Space for results
xsl = []
calc_dir = f'TMP/calc_{supercell}'
# Build the sampler
hecss = HECSS(cryst, calc, 
              directory=f'{calc_dir}',
              w_search = True,
              pbar=True,
              # reuse_base=f'{base_dir}/calc/' # Use if you want to reuse the base calc
              )
m, s, xscl = hecss.estimate_width_scale(10, nwork=0)
wm = np.array(hecss._eta_list).T
y = np.sqrt((3*wm[1]*un.kB)/(2*wm[2]))
plt.plot(wm[1], y, '.');
x = np.linspace(0, 1.05*wm[1].max(), 2)
fit = np.polyfit(wm[1], y, 1)
plt.plot(x, np.polyval(fit, x), ':', label=f'{fit[1]:.4g} {fit[0]:+.4g} T')
plt.axhline(m, ls='--', label=f'{m:.4g}±{s:.4g}')
plt.axhspan(m-s, m+s, alpha=0.3)
plt.ylim(m-4*s, m+4*s)
plt.xlabel('Target temperature (K)')
plt.ylabel('width scale ($\\AA/\\sqrt{K}$)')
plt.legend();

Width-scale estimate from the archived run

For the larger 2x2x2 cell the width-estimation history was also archived, in example/VASP_3C-SiC_calculated/calc_2x2x2/w_est/w_est.dat (30 points, header # w, T, (E-E0)/nat ; Tmax: 2000 K). Its first rows are:

# w, T, (E-E0)/nat ; Tmax: 2000 K
1.803612510138440442e-02 3.253018086727885816e+02 1.223904625000038493e-02
2.673292827554962434e-02 7.146494541856807245e+02 2.674842328125048851e-02
4.164901133681610940e-02 1.734640145334236877e+03 6.588259812500041335e-02
# Temperature (K)
T = 300
# Desired number of samples.
# Here 4 is minimal in 3C-SiC 
# to get somewhat decent phonons 
# with 10 Bohr cutoff.
N = 4
samples = hecss.sample(T, N)
plot_stats(samples, T)
distrib = hecss.generate(samples, T)
plot_stats(distrib, T, sqrN=True)

Sample energy statistics at 300 K

Reshaped distribution at 300 K

After the first run you can increase the limit (N) and return to the same loop (the state of the sampler is preserved) to generate more samples. This cell can be executed multiple times to collect enough samples for further analysis. You can also monitor the progress by providing sentinel argument to the generate method. Here is a simple monitoring function showing an updated histogram and position of the last sample (as dotted line)

def show_stats(s, sl, col=None, Temp=None):
    from matplotlib import pyplot as plt
    from hecss.optimize import make_sampling
    plot_stats(make_sampling(sl if col is None else col + sl, Temp),
               Temp, show=False)
    plt.axvline(s[-1], ls=':', label='Last sample')
    plt.legend()
    plt.show()
    # In a notebook, clear_output(wait=True) here redraws in place.
    # Important! Return False to keep iteration going
    return False
# Need more samples. Increase N and get more samples.
N = 10
samples += hecss.sample(T, N, sentinel=show_stats, 
                              sentinel_args={'col':samples, 'Temp':T})
plot_stats(hecss.generate(samples, T), T, sqrN=True)

Monitoring while adding samples

samples += hecss.sample(T, N, sentinel=show_stats, 
                              sentinel_args={'col':samples, 'Temp':T})

Further sample collection

# Verify if we really got the same picture
plot_stats(hecss.generate(samples, T), T, sqrN=True)

Distribution after collecting more samples

for c in samples:
    write_dfset(f'{calc_dir}/DFSET_T={T}K.dat', c)

When you are at production stage of your project and you know exactly what you want and how to get it the more compact style of code may be better. Though, it is less elastic and forgiving of mistakes. Here is a similar sampling run at T=600 K.

T=600
confs_600 = hecss.sample(T, 50)
plot_stats(hecss.generate(confs_600, T), T, sqrN=True)

Statistics at 600 K

for c in confs_600:
    write_dfset(f'{calc_dir}/DFSET_T={T}K.dat', c)
hecss.generate(confs_600, T, debug=True);

Debug plot of the 600 K generation

Second debug plot of the 600 K generation

c_1000 = hecss.sample(1000, 50)
T_1000 = np.array([s[-1] for s in c_1000]).mean()
s_1000 = hecss.generate(c_1000, 2*T_1000/un.kB/3, border=True, debug=True)

Debug plots for the 1000 K run

Second debug plot for the 1000 K run

for c in s_1000:
    write_dfset(f'{calc_dir}/DFSET_T={1000}K.dat', c)

Precomputed results

The archived calculations produced DFSET files for T=300, 600, 1200 and 3000 K under example/VASP_3C-SiC_calculated/{1x1x1,2x2x2}/T_*K/DFSET.dat, with phonon band files in the corresponding phon/cryst.bands paths. They can be fed directly to the post-processing commands in the CLI tutorial (plot_stats, plot_bands and reshape_sample).