Dimer Energies

Density Functional Theory

#!/usr/bin/env python
# coding: utf-8

get_ipython().system('pip3 install pyscf')
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

def set_publication_style():
    """Set publication-quality matplotlib style."""
    mpl.rcParams.update({
        'font.family': 'serif',
        'font.size': 12,
        'axes.labelsize': 14,
        'axes.titlesize': 16,
        'axes.linewidth': 1.2,
        'axes.labelpad': 8,
        'axes.titlepad': 10,
        'xtick.labelsize': 12,
        'ytick.labelsize': 12,
        'xtick.direction': 'in',
        'ytick.direction': 'in',
        'xtick.top': True,
        'ytick.right': True,
        'xtick.major.size': 6,
        'ytick.major.size': 6,
        'xtick.major.width': 1.2,
        'ytick.major.width': 1.2,
        'legend.fontsize': 12,
        'legend.frameon': False,
        'lines.linewidth': 2,
        'lines.markersize': 6,
        'figure.dpi': 100,
        'savefig.dpi': 300,
        'savefig.bbox': 'tight'
    })

set_publication_style()
from pyscf import gto, scf, dft

# List of dimers
dimers = [
    ('H', 'H'),
    ('He', 'He'),
    ('Li', 'Li'),
    ('Be', 'Be')
    # Add more dimers as needed
]

# List of functionals
functionals = [
    'hf',       # Hartree-Fock
    'lda',      # Local Density Approximation
    'pbe',      # Perdew-Burke-Ernzerhof
    'b3lyp'     # B3LYP hybrid functional
    # Add more functionals as needed
]

# Define a function to calculate energy
def calculate_energy(dimer, distance, functional):
    atom1, atom2 = dimer
    mol = gto.Mole()
    mol.build(
        atom = f'{atom1} 0 0 0; {atom2} 0 0 {distance}',
        basis = 'sto-3g'
    )

    if functional == 'hf':
        mf = scf.RHF(mol)
    else:
        mf = dft.RKS(mol)
        mf.xc = functional

    energy = mf.kernel()
    return energy

# Define the range of distances
distances = np.linspace(0.5, 3.0, 50)

plt.figure(figsize=(12, 8))

for dimer in dimers:
    atom1, atom2 = dimer
    for functional in functionals:
        energies = []

        for d in distances:
            energy = calculate_energy(dimer, d, functional)
            energies.append(energy)

        # Plot the energy as a function of distance
        plt.plot(distances, energies, label=f'{atom1}-{atom2} ({functional})')

plt.xlabel('Distance (Angstrom)')
plt.ylabel('Energy (Hartree)')
plt.title('Energy of Dimers as a Function of Distance')
plt.legend()
plt.grid(True)
plt.savefig('figures/dimer_energies.png', dpi=300, bbox_inches='tight')
plt.show()


import numpy as np
import plotly.graph_objects as go
from pyscf import gto, scf, dft

# List of dimers
dimers = [
    ('H', 'H'),
    ('He', 'He'),
    ('Li', 'Li'),
    ('Be', 'Be')
    # Add more dimers as needed
]

# List of functionals
functionals = [
    'hf',       # Hartree-Fock
    'lda',      # Local Density Approximation
    'pbe',      # Perdew-Burke-Ernzerhof
    'b3lyp'     # B3LYP hybrid functional
    # Add more functionals as needed
]

# Define a function to calculate energy
def calculate_energy(dimer, distance, functional):
    atom1, atom2 = dimer
    mol = gto.Mole()
    mol.build(
        atom = f'{atom1} 0 0 0; {atom2} 0 0 {distance}',
        basis = 'sto-3g'
    )

    if functional == 'hf':
        mf = scf.RHF(mol)
    else:
        mf = dft.RKS(mol)
        mf.xc = functional

    energy = mf.kernel()
    return energy

# Define the range of distances
distances = np.linspace(0.5, 3.0, 50)

# Iterate over each dimer
for dimer in dimers:
    atom1, atom2 = dimer
    fig = go.Figure()

    # Calculate and plot the energies for each functional
    for functional in functionals:
        energies = []

        for d in distances:
            energy = calculate_energy(dimer, d, functional)
            energies.append(energy)

        fig.add_trace(go.Scatter(
            x=distances, y=energies, mode='lines', name=f'{functional}'
        ))

    fig.update_layout(
        title=f'Energy of {atom1}-{atom2} Dimer as a Function of Distance',
        xaxis_title='Distance (Angstrom)',
        yaxis_title='Energy (Hartree)',
        legend_title='Functional',
        template='plotly_dark'
    )

    # Save the figure as an HTML file
    fig.write_html(f'{atom1}_{atom2}_dimer_energies.html')
    fig.show()

Visualization

The following plot shows dimer energies as a function of distance for different functionals:

Figure pending — running the script above produces figures/dimer_energies.png.