Example 2 — ASE calculator: water dimer

Single-point energy and forces of a water dimer with the ASE calculator.

Prerequisites: a deMonNano installation + global.json (see the DeMonNanoPy Quickstart page).

Run with:

python3 example_ase.py

The code below is the full script, split by part. Runnable version: demonnano/examples/example_ase.py.

Calculator & dimer

Single-point energy and forces of a water dimer with the ASE calculator: the parameters and the dimer geometry. parameters selects the BIO basis and activates SCC-DFTB with the London dispersion correction (DISP=2) for a neutral molecule, and the dimer is an ASE Atoms object holding the two water molecules.

"""Single-point energy and forces of a water dimer with the ASE calculator.

Run with:
    python3 example_ase.py

Requires a deMonNano installation and a global.json file (see the
DeMonNanoPy README).
"""

import numpy as np
from ase.atoms import Atoms

import deMonPy
from deMonPy.ase_calculator import DeMonNano

deMonPy.configure_from_file("global.json")

parameters = {
    "BASIS": {"PTYPE": "BIO", "SKFILE": deMonPy.DEMON_BASIS},
    "DEMON_PARAMETERS": {
        "ACTIVE": {
            "DFTB": {"SCC": True, "DISP": 2},
            "CHARGE": 0.0,
        },
    },
}

image = Atoms(
    ["O", "H", "H", "O", "H", "H"],
    positions=np.array(
        [
            [1.2478, -0.5185, 3.4049],
            [1.5946, -1.4204, 3.3886],
            [0.9008, -0.3341, 2.5062],
            [3.2478, -0.4185, 3.4049],
            [3.5946, -1.5204, 3.3886],
            [2.9008, -0.3341, 2.6062],
        ]
    ),
)

Energy & forces

Run the calculation and print the energy (eV) and the forces. The DeMonNano ASE calculator is created with omp_threads=1 in the working directory .run/example-ase; calculate() runs a deMonNano calculation and populates the ASE results, and the interface reports the energy in eV and the forces on each atom in eV/angstrom.

WORKDIR = ".run/example-ase"


def exemple_run_ase():
    calc = DeMonNano(
        omp_threads=1,
        title="CALCULATION DEMONANO",
        directory=WORKDIR,
        **parameters,
    )

    calc.calculate(atoms=image, properties=["energy", "forces"])

    print(" RESULTS : ")
    print(f" > Energy (eV) : {calc.results['energy']}")
    print(" > Forces :")
    for force in calc.results["forces"]:
        print(f"   {force[0]:10.4f}  {force[1]:10.4f}  {force[2]:10.4f}")


if __name__ == "__main__":
    exemple_run_ase()

Any ASE optimizer works

Any ASE optimizer works on top of the calculator, e.g. BFGS.

    # Any ASE optimizer works on top of the calculator, e.g. BFGS:
    # image.calc = DeMonNano(omp_threads=1, title="CALCULATION DEMONANO", directory=".run/example-bfgs", **parameters)
    # from ase.optimize import BFGS
    # BFGS(image, fmax=0.01).run()

Summary

The script computes the single-point energy and forces of a water dimer with the ASE calculator: SCC-DFTB with the London dispersion correction (DISP=2) and the BIO basis run in .run/example-ase, and the energy (eV) together with the force on each atom are printed. The commented-out block shows the same calculator driving an ASE optimizer, for instance BFGS.