# 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
from collections import defaultdict
from tempfile import TemporaryDirectory
from glob import glob
VASP workflow
VASP workflow demo.
# Quick test using conventional unit cell
= '1x1x1' supercell
# Slow more realistic test
= '2x2x2' supercell
# Directory in which our project resides
= f'example/VASP_3C-SiC/{supercell}/'
base_dir = TemporaryDirectory(dir='TMP') calc_dir
# Read the structure (previously calculated unit(super) cell)
# The command argument is specific to the cluster setup
= Vasp(label='cryst', directory=f'{base_dir}/sc_{supercell}/', restart=True)
calc
# This just makes a copy of atoms object
# Do not generate supercell here - your atom ordering will be wrong!
= calc.atoms.repeat(1) cryst
# Setup the calculator - single point energy calculation
# The details will change here from case to case
# We are using run-vasp from the current directory!
set(directory=f'{calc_dir.name}/sc')
calc.set(command=f'{os.getcwd()}/run-calc.sh "vasp_test"')
calc.set(nsw=0)
calc.= calc cryst.calc
print('Stress tensor: ', end='')
for ss in calc.get_stress()/un.GPa:
print(f'{ss:.3f}', end=' ')
print('GPa')
Stress tensor: 0.017 0.017 0.017 0.000 0.000 0.000 GPa
# Prepare space for the results.
# We use defaultdict to automatically
# initialize the items to empty list.
= defaultdict(lambda : [])
samples
# Space for amplitude correction data
= [] xsl
# Build the sampler
= HECSS(cryst, calc,
hecss =calc_dir.name,
directory= True,
w_search =True,
pbar )
= 5
N = hecss.estimate_width_scale(5, nwork=0)
m, s, xscl = np.array(hecss._eta_list).T
wm = np.sqrt((3*wm[1]*un.kB)/(2*wm[2]))
y 1], y, '.');
plt.plot(wm[= np.linspace(0, 1.05*wm[1].max(), 2)
x = np.polyfit(wm[1], y, 1)
fit ':', label=f'{fit[1]:.4g} {fit[0]:+.4g} T')
plt.plot(x, np.polyval(fit, x), ='--', label=f'{m:.4g}±{s:.4g}')
plt.axhline(m, ls-s, m+s, alpha=0.3)
plt.axhspan(m-4*s, m+4*s)
plt.ylim(m'Target temperature (K)')
plt.xlabel('width scale ($\\AA/\\sqrt{K}$)')
plt.ylabel(; plt.legend()
# Desired number of samples and T list.
= 30
N = (100, 200, 300)
T_list for T in T_list:
+= hecss.sample(T, N) samples[T]
# Plot the resulting distributions
for T in T_list:
=True) plot_stats(hecss.generate(samples[T]), sqrN