Skip to main content

Python bindings for the GFN-FF force-field library

Project description

GFN-FF

A general force field for elements Z = 1–103

build status License: LGPL v3

This repository provides a standalone library implementation of the GFN-FF method by S.Spicher and S.Grimme, adapted from the xtb code (most recently at commit 6d44803 and validated against that version's results). The primary purpose is to serve as a linkable dependency for other Fortran, C, and C++ projects. From this point forward, development may diverge from the upstream xtb implementation.


Method

GFN-FF (Geometries, Frequencies, Non-covalent interactions Force-Field) is a completely automated, topology-based force field for fast structure optimisations and non-covalent interaction energies across the periodic table (Z = 1–103). The topology and parametrisation are derived entirely from the input geometry, without user-defined atom types or connectivity.

The following features are available and documented in the associated publications:

  • Molecular GFN-FF — a generic, partially polarisable force field covering organic, organometallic, and biochemical systems (S. Spicher, S. Grimme, Angew. Chem. Int. Ed. 2020, 59, 15665. doi:10.1002/anie.202004239)

  • Periodic boundary conditions / molecular crystals — adjusted non-covalent interactions for lattice energy predictions and unit-cell optimisations of molecular crystals (S. Grimme, T. Rose, Z. Naturforsch. B 2024, 79, 191. doi:10.1515/znb-2023-0088)

  • Lanthanide and actinide extension — reparametrised f-element treatment enabling MD simulations and geometry optimisations for large lanthanide- and actinide-containing systems (T. Rose, M. Bursch, J.-M. Mewes, S. Grimme, Inorg. Chem. 2024. doi:10.1021/acs.inorgchem.4c03215)


Building from source

The library requires a Fortran and C compiler (e.g. gfortran/gcc), LAPACK/BLAS (e.g. OpenBLAS), and optionally OpenMP. Both CMake (≥ 3.21) and Meson (≥ 0.59) build systems are supported.

CMake Meson
cmake -B _build
cmake --build _build

To run the test suite:

cmake -B _build -DWITH_TESTS=ON
cmake --build _build
ctest --test-dir _build
meson setup _build
ninja -C _build

To run the test suite:

meson setup _build -Dtests=true
ninja -C _build test

The compiled library (libgfnff.a by default) is placed in the build directory and can be linked into any downstream project.


Library usage

The interface is exposed through the gfnff_interface Fortran module and the gfnff_interface_c.h C header (located in include/). Two steps are required: initialise the calculator (topology setup) and call the singlepoint routine. The initialisation is typically the more expensive step; once complete, singlepoint evaluations can be called repeatedly on the same calculator object.

Fortran
use iso_fortran_env, only: real64
use gfnff_interface

type(gfnff_data) :: calc
integer  :: nat, ichrg, io
integer,  allocatable :: at(:)
real(real64), allocatable :: xyz(:,:), gradient(:,:)
real(real64) :: energy, sigma(3,3)

! ... populate nat, at, xyz, ichrg ...

call calc%init(nat, at, xyz, ichrg=ichrg, iostat=io)

call calc%singlepoint(nat, at, xyz, energy, gradient, iostat=io, sigma=sigma)

call calc%deallocate()

All coordinates are in Bohr; the energy is in Hartree, the gradient in Eh/Bohr, and sigma (3×3) is the stress tensor in Hartree (zero for non-periodic systems). Full working example: app/main.F90.

C
#include "gfnff_interface_c.h"

double sigma[3][3];   /* stress tensor (Hartree); zero for non-PBC */

c_gfnff_calculator calc =
    c_gfnff_calculator_init(nat, at, xyz, ichrg, printlevel, solvent);

c_gfnff_calculator_singlepoint(&calc, nat, at, xyz, &energy, gradient,
                               sigma, &iostat);

c_gfnff_calculator_deallocate(&calc);

Full working example: test/main.c.

C++
#include "gfnff_interface_c.h"

double sigma[3][3];   // stress tensor (Hartree); zero for non-PBC

c_gfnff_calculator calc =
    c_gfnff_calculator_init(nat, at, xyz, ichrg, printlevel, solvent);

c_gfnff_calculator_singlepoint(&calc, nat, at, xyz, &energy, gradient,
                               sigma, &iostat);

c_gfnff_calculator_deallocate(&calc);

Full working example: test/main.cpp.

Integrating as a CMake subproject

Add the repository as a subdirectory and link against the exported target:

add_subdirectory(gfnff)
target_link_libraries(my_target PRIVATE gfnff)
Integrating as a Meson subproject

Place the repository under subprojects/gfnff/ and wrap it:

gfnff_dep = dependency('gfnff', fallback: ['gfnff', 'gfnff_dep'])

Periodic boundary conditions

PBC support is available via c_gfnff_calculator_init_pbc on the C/C++ side and via an optional lattice argument to calc%init in Fortran. See the PBC sections in test/main.c and test/main.cpp for worked examples.


Python bindings

Python bindings are provided through a ctypes-based interface. The shared library is bundled into a binary wheel, so no Fortran or C compiler is needed at install time.

Installation

Binary wheels for Linux (x86_64) and macOS (x86_64 / arm64) are published on PyPI:

pip install gfnff          # library only
pip install "gfnff[ase]"   # + ASE (enables the CLI and the ASE calculator)

Building from source

The source build compiles the Fortran library on your machine. The following system packages must be present before running pip:

Dependency Example (Debian/Ubuntu) Example (Fedora/RHEL) Example (macOS)
Fortran compiler apt install gfortran dnf install gcc-gfortran brew install gcc
LAPACK + BLAS apt install libopenblas-dev dnf install openblas-devel brew install openblas
CMake ≥ 3.21 installed by pip automatically

Once those are in place:

pip install ".[ase]"           # from a checkout
pip install "gfnff[ase]" --no-binary gfnff   # force source build from PyPI

Command-line interface

Installing gfnff[ase] places a gfnff executable on your PATH.

gfnff <input> [options]

The input file is read by ASE, so any format it supports works (xyz, extxyz, POSCAR, cif, …).

Singlepoint (default):

gfnff molecule.xyz
gfnff molecule.xyz --chrg -1
gfnff molecule.xyz --alpb h2o        # implicit solvation (--solv is an alias)

Geometry optimisation (L-BFGS via ASE, cell fixed, writes gfnff.log.extxyz):

gfnff molecule.xyz --opt
gfnff molecule.xyz --opt --fmax 0.05          # looser convergence, eV/Å
gfnff molecule.xyz --opt --outfile path.xyz   # custom trajectory file
gfnff molecule.xyz --opt --alpb acetone       # optimise in solvent

Variable-cell optimisation (L-BFGS + ExpCellFilter, periodic systems only):

gfnff crystal.cif --optcell
gfnff crystal.cif --optcell --fmax 0.01

Full option list: gfnff --help

The trajectory file (gfnff.log.extxyz) stores energy and forces in each frame header, compatible with ASE's ase gui.


Low-level API (GFNFFCalculator)

GFNFFCalculator mirrors the C API one-to-one. All quantities use the same units as the library itself: Bohr for coordinates and lattice, Hartree for energy, Eh/Bohr for gradients, and Hartree for the stress tensor.

import numpy as np
from gfnff import GFNFFCalculator

# Atomic numbers and coordinates in Bohr
numbers = np.array([6, 8, 1, 1], dtype=np.int32)   # CO + 2 H
positions = np.array([[0, 0, 0], [2.1, 0, 0],
                      [-1.0, 0, 0], [3.1, 0, 0]], dtype=np.float64)

with GFNFFCalculator(numbers, positions, charge=0, printlevel=0) as calc:
    energy, gradient, sigma = calc.singlepoint(numbers, positions)
    print(f"Energy: {energy:.6f} Eh")
    print(f"Gradient shape: {gradient.shape}")  # (nat, 3)
    print(f"Stress tensor:\n{sigma}")            # (3, 3), Hartree; zero for non-PBC

Periodic systems use a separate initialiser:

calc = GFNFFCalculator(
    numbers, positions,
    lattice=lattice_bohr,   # shape (3, 3), rows are lattice vectors
    npbc=3,
)

ASE Calculator (GFNFF)

GFNFF is a fully compatible ASE Calculator. It handles unit conversion automatically (Å ↔ Bohr, eV ↔ Hartree). Implemented properties: energy, forces, stress.

from ase.build import molecule
from gfnff import GFNFF

atoms = molecule("caffeine")
atoms.calc = GFNFF()

energy = atoms.get_potential_energy()   # eV
forces = atoms.get_forces()             # eV / Å, shape (nat, 3)
stress = atoms.get_stress()             # eV / ų, Voigt [xx,yy,zz,yz,xz,xy]; zero for non-PBC

Periodic systems work the same way — provide an atoms object with cell and pbc set:

from ase.io import read
from gfnff import GFNFF

atoms = read("quartz.cif")
atoms.calc = GFNFF()
print(atoms.get_potential_energy())  # eV / unit cell
print(atoms.get_stress())            # eV / ų, Voigt

Variable-cell relaxation via ASE's ExpCellFilter:

from ase.filters import ExpCellFilter
from ase.optimize import LBFGS

opt = LBFGS(ExpCellFilter(atoms))
opt.run(fmax=0.01)

Additional options:

Parameter Default Description
charge 0 Total charge. Also reads atoms.info["charge"] (takes precedence).
solvent "" Implicit solvent name: "h2o", "acetone", "chcl3", … (molecular systems only)
printlevel 0 Fortran output verbosity (0 = silent, 3 = verbose).

Running the tests

pip install "gfnff[test]"
pytest python/tests/

License

This project is licensed (as the original xtb code) under the GNU Lesser General Public License v3 or later. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

gfnff-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (15.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

gfnff-0.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

gfnff-0.1.0-cp312-cp312-macosx_14_0_arm64.whl (5.0 MB view details)

Uploaded CPython 3.12macOS 14.0+ ARM64

gfnff-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (15.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

gfnff-0.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

gfnff-0.1.0-cp311-cp311-macosx_14_0_arm64.whl (5.0 MB view details)

Uploaded CPython 3.11macOS 14.0+ ARM64

gfnff-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (15.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

gfnff-0.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ ARM64

gfnff-0.1.0-cp310-cp310-macosx_14_0_arm64.whl (5.0 MB view details)

Uploaded CPython 3.10macOS 14.0+ ARM64

gfnff-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (15.4 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

gfnff-0.1.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (7.4 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ ARM64

gfnff-0.1.0-cp39-cp39-macosx_14_0_arm64.whl (5.0 MB view details)

Uploaded CPython 3.9macOS 14.0+ ARM64

File details

Details for the file gfnff-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 141dc62942e97fe5892b65683261faa15d8a0f8c089050f613b57414fdcfc6fc
MD5 31ecb94e1604cb5ecc3e4b9f9d7950b0
BLAKE2b-256 6e97a01b7764617b58b574ed84a4c9de6411e8f11debcc846d3f20ae38c14de4

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: wheels.yml on pprcht/gfnff

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gfnff-0.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 d0ca11460b78bc4afc6f6eb69358b99f3b0f3eb0c62588193fe3be986ee181bb
MD5 1bc7852c4f5bd8eefa702c03eeb4e6ea
BLAKE2b-256 508080d9280af7c19b628dac5c97ff3f344ac9469ca38e02594242660ce08e9f

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: wheels.yml on pprcht/gfnff

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gfnff-0.1.0-cp312-cp312-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.0-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 e24d86f452c5abb16a497026e54703e5c0d3d8443ca53705301d3ed96232ed2d
MD5 6537e4deb9d1320eac73b896d2ebc714
BLAKE2b-256 355486fcf83a0878cacee591c6154678b78c6d84892a3ffd20d0a2f741ebbe71

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.0-cp312-cp312-macosx_14_0_arm64.whl:

Publisher: wheels.yml on pprcht/gfnff

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gfnff-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 da984e653494f373cbd2e36f60c26032e8b146ea74943cae622362d09bf5c752
MD5 4995efb5a7c1d7ea8ad431a46a3b54c2
BLAKE2b-256 4ab90651da3cbe7aa52537353a32922e4d2c679f16e63f08d6e974c5487eeb53

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: wheels.yml on pprcht/gfnff

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gfnff-0.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 fd6022046edc0310f0cbd6ee3ebea02cb4a55c9cb917c08e3298463c32e69853
MD5 0ea368cec9c85280b61a97f5c5247fd9
BLAKE2b-256 6daaeb746ac163209893168d1362a52d251f84fcfb11ebbd13abbf3271626132

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: wheels.yml on pprcht/gfnff

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gfnff-0.1.0-cp311-cp311-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.0-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 af397d0a1bf8d1108c58cbe70a79e9192a6cff345ae7bf29bc0be5e88dd4c670
MD5 fb880dba525b361ba10cb478faa30ba5
BLAKE2b-256 0739de5bcc41d5143f7bce23af33794544a174bd22403f80d8b37de37f3e3eed

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.0-cp311-cp311-macosx_14_0_arm64.whl:

Publisher: wheels.yml on pprcht/gfnff

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gfnff-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 15670e1b89333eca4abe5fa699cda2267cecff97b28b704b05af84fe4ed567ac
MD5 90bb2cffdcd6ca4ab0b6a96e970baecc
BLAKE2b-256 ddb35fa56adc1500001a3024e8632bd8b6606398745dbb3c995c90683ed95251

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: wheels.yml on pprcht/gfnff

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gfnff-0.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 92ce14887725a10ce5d46c8bd5b501ee7b6c8688905c52e3560a668b0f376ea1
MD5 fad018e9f27c701dce02d6c54ecb205e
BLAKE2b-256 5ef790c5dca93605c82fbaa5722195af548c06180c69d9a564e10cb03c320346

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: wheels.yml on pprcht/gfnff

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gfnff-0.1.0-cp310-cp310-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.0-cp310-cp310-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 17053dd16973c267be24d1909c6b9170feffdc996cde6e506b645595a9144675
MD5 f8f6d5b890919dcc5300f8aa433de8a6
BLAKE2b-256 15d2dbfc1af6d663f2c5069d9a4e607d232d73cf1eecca1a7843079fedf213e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.0-cp310-cp310-macosx_14_0_arm64.whl:

Publisher: wheels.yml on pprcht/gfnff

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gfnff-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 583ed2704b53140edf27ef27e695873c90044ce2947bb6bb65ee11fa3867f196
MD5 e16aeaca7fbd09a1c796cf7362ba32bd
BLAKE2b-256 e083345dac946d0f9dccc8021224c1c079da1ee0bd89261034e0774437e547bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: wheels.yml on pprcht/gfnff

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gfnff-0.1.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8fac56daaa133275c7daf31c4c4a759b553d6b45cbb9e22d84afd9e0ae31e420
MD5 549a6c900fda6c779f711c8994750395
BLAKE2b-256 19533801955f7a2eb6f4e27ee9e427fabcfe899db52b8ce305742d1ee4623f82

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: wheels.yml on pprcht/gfnff

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file gfnff-0.1.0-cp39-cp39-macosx_14_0_arm64.whl.

File metadata

  • Download URL: gfnff-0.1.0-cp39-cp39-macosx_14_0_arm64.whl
  • Upload date:
  • Size: 5.0 MB
  • Tags: CPython 3.9, macOS 14.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for gfnff-0.1.0-cp39-cp39-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 009ef4c61de163e87923cdab877a2c69c716af99280cfd0b5d18af274a8028ec
MD5 7c42f14a6770272349f132117b46975c
BLAKE2b-256 3d1839a818006807a204e4948eb43687902141a4e4b389376b73a879f29e767f

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.0-cp39-cp39-macosx_14_0_arm64.whl:

Publisher: wheels.yml on pprcht/gfnff

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page