Jump to content

Requests for technical support from the VASP team should be posted in the VASP Forum.

Thermodynamic integration between machine-learned force fields

From VASP Wiki

Thermodynamic integration (TI) can be performed between two machine-learned force fields (MLFFs), significantly speeding up the calculation. Here, the free energy difference [math]\displaystyle{ \Delta A }[/math] between Fe2+/Fe3+ in an electrochemical half-cell is calculated . The accuracy of this MLFF:MLFF approach is confirmed later by performing another TI from the MLFF to a DFT functional.

Input files

Two MD calculations need to be run in parallel between two different systems: Fe3+ in 64 H2O (Fe3P_64H2O) and Fe2+ in 64 H2O (Fe2P_64H2O). I.e., [math]\displaystyle{ [\mathrm{Fe}(\mathrm{H}_2\mathrm{O})_n]^{3+} }[/math] and [math]\displaystyle{ [\mathrm{Fe}(\mathrm{H}_2\mathrm{O})_n]^{2+} }[/math].

POSCARs

The POSCAR files can be found in the calculating the redox potential overview page.

INCAR

The INCAR files are provided in the text and discussed there.

KPOINTS

The Gamma-point only is used for the KPOINTS file:

Gamma-point only
 0
Monkhorst Pack
 1 1 1
 0 0 0

POTCAR

Standard POTCAR files are used throughout:

  • PAW_PBE H 15Jun2001
  • PAW_PBE O 08Apr2002
  • PAW_PBE Fe_sv 23Jul2007

Calculation (MLFF:MLFF)

The first step is thermodynamic integration between two species: Fe3+ and Fe2+ using MLFFs. The procedure is as follows:

Step 0: Obtain initial POSCARs for Fe3+ and Fe2+

A starting structure for the MD simulations in TI should be carefully chosen. In Ref. , a homemade MD simulation program was used to anneal the two systems: [math]\displaystyle{ [\mathrm{Fe}(\mathrm{H}_2\mathrm{O})_n]^{3+} }[/math] and [math]\displaystyle{ [\mathrm{Fe}(\mathrm{H}_2\mathrm{O})_n]^{2+} }[/math] from 1000 K to 400 K in a 1 ns NVT ensemble MD simulation. We begin with the final structure from each of those two simulations.

Step 1: Preparing the directories

To perform TI, you need to use the VCAIMAGES tag. This requires a parent directory from which the TI is run, containing the following INCAR file:

# TI settings
VCAIMAGES = 0.25
NCORE_IN_IMAGE1 = 12

# MD settings
IBRION = 0
ISYM = 0
NSW = 100000
POTIM = 1.0
TEBEG = 298
TEEND = 298

MDALGO = 2
ISIF = 2
SMASS = 0

POMASS = 2.0 16.0 55.847
RANDOM_SEED =         248489752                0                0

# General settings
ML_ESTBLOCK = 100                    # only write to OUTCAR every 100 ionic steps

IMAGE_1 {
#Machine learning
ML_LMLFF = .TRUE.                    # switches on machine learning
ML_MODE = run
}

IMAGE_2 {
#Machine learning
ML_LMLFF = .TRUE.                    # switches on machine learning
ML_MODE = run
}

The VCAIMAGES tag runs calculations in two image directories 01 and 02, which contain the two non-interacting λ=0 and interacting λ=1 systems, respectively. In this case, Fe3+ (the oxidised state Ox, λ=0) and Fe2+ (the reduced state Red, λ=1). Since MLFFs are used, they must contain the ML_FFs trained for the Ox and Red systems, respectively. Make sure to place identical POSCAR, POTCAR, and KPOINTS files in each of these image directories, as well as their respective MLFFs.

Step 2: Molecular dynamics calculation

Set up the TI calculations for different λ values defined by VCAIMAGES (e.g., 0.0, 0.25, 0.5, 0.75, and 1.0). This will require 5 separate directories, e.g.:

lambda_0p0  lambda_0p25  lambda_0p5  lambda_0p75  lambda_1p0

for 0.0, 0.25, 0.5, 0.75, and 1.0, respectively. The calculation should be submitted from the parent directory, similar to for NEB. These will each run two parallel MD calculations for the value of λ defined in VCAIMAGES.

Post-processing

Step 3: Extracting and averaging the energies

Each of these calculations will output the energy for each MD step in the following format:

free  energy ML TOTEN  =      -953.27166392 eV

You should take the values for the 01 and 02 directories separately, e.g., by grepping for the energies and temperatures in each of the sub-directories:

grep "free  energy" 01/OUTCAR | awk '{print $5}' > free_E.dat
grep "free  energy ML TOTEN  =" 02/OUTCAR | awk '{print $6}' >> free_E.dat
grep temperature 01/OUTCAR | awk '{print $6}' > T.dat

then calculate the two ensemble averages, before taking the difference between the two. We exclude the first 20000 MD steps to allow time for equilibration. You can do this with the following script, which will plot the probability vs potential energy, the MD step number vs the potential energy, and the MD step number vs the temperature:

from py4vasp import plot
import plotly.graph_objects as go
import numpy as np
import os 
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde

def delta_A(path, directory, lower, upper, step):
    number_str = directory.split("_")[1] 
    number_str = number_str.replace("p", ".")
    Lambda = float(number_str)
    print(path+directory)
    
    data = np.genfromtxt(path + str(directory) + "/free_E.dat", dtype=None, encoding=None)
    length=len(data)
    print(length)
    
    U_ox, U_red = data[lower:upper:step], data[int(length/2)+lower:int(length/2)+upper:step]
    n_ox, n_red = range(lower, upper+1, step), range(int(length/2)+lower,int(length/2)+upper+1, step)
    print(n_ox, n_red)
    print(len(U_ox), len(U_red))
    U_red_av, U_ox_av = np.average(U_red), np.average(U_ox)
    #U_1_0 =  U_red - U_ox
    U_1_0_av = U_red_av - U_ox_av
    #U_1_0_av = np.average(U_1_0)
    return(Lambda, (U_1_0_av), (U_red-U_ox))

def diff_cutoff(path, lower, upper, step):
    files = [d for d in os.listdir(path) if d.startswith("lambda_")]

    lambdas, A = [], []
    for a in ['lambda_0p0', 'lambda_0p25', 'lambda_0p5', 'lambda_0p75', 'lambda_1p0']:
        print(files)
        temp1, temp2, data = delta_A(path, a, lower, upper, step)
        lambdas.append(temp1)
        A.append(temp2)
        print(lambdas, A)
        U = data
        # Plot first graph
        kde = gaussian_kde(data)
        x = np.linspace(min(data), max(data), 20)
        ax1.plot(x, kde(x), '-', linewidth=2, alpha= 0.5, label=a)
        ax1.set_title('P vs. U')
        ax1.set_xlabel(r'$\Delta U_{ML}$')
        ax1.set_ylabel(r'$P(\Delta U_{ML})$')
        ax1.legend()       

        # Plot second graph
        ax2.set_title('MD step vs. U')
        ax2.set_xlabel(r'$\Delta U_{ML}$')
        ax2.set_ylabel('MD step')
        ax2.plot(U, range(lower,upper,step), '-', linewidth=2, alpha= 0.5, label=a)        

        # Plot T
        data = np.genfromtxt(path + str(a) + "/T.dat", dtype=None, encoding=None)
        ax3.plot(data, range(len(data)), '-', linewidth=2, alpha= 0.5, label=a)
        ax3.set_title('MD step vs. T')
        ax3.set_xlabel('T')
        ax3.set_ylabel('MD step')
        ax3.legend()  
        
    return(lambdas, A)

path = "$PATH_TO_TI_MLFF_MLFF_DIRECTORIES/"

l_cutoff, A_cutoff = [], []

# Create a figure with 1 row and 2 columns
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10, 4))  # 1 row, 2 columns

lambdas, A = diff_cutoff(path, 20000, 100000, 1)
l_cutoff.append(lambdas)
A_cutoff.append(A)

print((A_cutoff))
print((l_cutoff))

ax1.legend()
ax2.legend()

# Adjust layout so titles/labels don't overlap
plt.tight_layout()
plt.savefig("TI_mlff_mlff.png")
Figure 4. Probability vs potential energy (cf. Supplementary Figure 8), the MD step number vs the potential energy, and the MD step number vs the temperature for between Fe2+ (λ = 0) and Fe3+ (λ = 1).

Step 4: Integrate to obtain the free energy

You can then plot the free energy against the lambda values; ideally, it should be almost linear:

Figure 5. Free energy difference ΔA for thermodynamic integration using a parameter λ between Fe3+ (λ = 0) and Fe2+ (λ = 1) (cf. Supplementary Figure 8).

With the free energy for each individual λ, you can integrate over them to obtain the free energy of the TI.

[math]\displaystyle{ \Delta A = \int_0^1 \langle U_1 - U_0 \rangle_{\lambda} d\lambda - ne\Delta \bar{\phi} }[/math]

from scipy.integrate import simpson
print('Simpson: ' + str(simpson(A_cutoff, l_cutoff)[0]))

This gave a value of: -1.272 eV, almost identical to the literature -1.260 eV obtained for Std. POTCARs provided by R. Jinnouchi (Ref. uses GW POTCARs). Adding this to the value of [math]\displaystyle{ e \Delta \bar{\phi} }[/math] from the previous step gives [math]\displaystyle{ \Delta A = -4.98 \: \mathrm{ eV} }[/math]. Considering that the redox potential [math]\displaystyle{ U_{redox} = -\Delta A /e }[/math], [math]\displaystyle{ U_{redox} }[/math] can be calculated as:

[math]\displaystyle{ U_{redox} = -\Delta A/e = -(-1.27 - (3.71))/1 = 4.98 \: \mathrm{ eV} }[/math].

Comparing this to the literature value of 4.95 eV (for Std. POTCAR; cf. Supplementary Table 6 for GW POTCAR), we are in reasonable agreement despite taking additional approximations.

Recommendations and advice

  • Make sure to carefully check that you are using the correct ML_FF files for each directory. If you mix them up, then you can always switch λ in the post-processing.
  • If you see that during the MD simulation (in the TI), U drifts far from the average, this is an indication that a chemical change has happened. Check to see if an Fe-O bond has formed. This should not happen and is an indication that your force field is unstable.

Related tags and articles

Tags
Theory
How-tos

References