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.1-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.1-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.1-cp312-cp312-macosx_14_0_arm64.whl (5.0 MB view details)

Uploaded CPython 3.12macOS 14.0+ ARM64

gfnff-0.1.1-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.1-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.1-cp311-cp311-macosx_14_0_arm64.whl (5.0 MB view details)

Uploaded CPython 3.11macOS 14.0+ ARM64

gfnff-0.1.1-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.1-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.1-cp310-cp310-macosx_14_0_arm64.whl (5.0 MB view details)

Uploaded CPython 3.10macOS 14.0+ ARM64

gfnff-0.1.1-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.1-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.1-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.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 56b3ee04eb0879d217d4e6cf7da73995288f1b6045578cd932de2423c537d6df
MD5 a16da5b544e6fdeebb6b84aafc5adbdd
BLAKE2b-256 6f642a6c59edb728e49f1ae075e168705007286310de61b0b331bf510946def9

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.1-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.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 85ab207a2d21550a561211e1a755d1ccc3a631da20d81fbe367d3a3aaf0f1e80
MD5 3691b9f9ff6e5dd8e202c31e5741d59b
BLAKE2b-256 09c23044d977dc8793e8441832308adfd069b53f56b8e1ef58ebe477a9f803df

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.1-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.1-cp312-cp312-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.1-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 e2bcb8296a0d1c9ee43ec9119dc629ba0b7e0b4b9bbabd02770471dad9eaf3cd
MD5 5ab02a55b728dbc78e4f72e2207b8c39
BLAKE2b-256 8de524e87e25cd838603749cdef226607049d9dec3d0fbb9068fd78df1a27086

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.1-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.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1ce561125eb2f1ee5ccc23a82801e967643403e20fd36b1739719c6cfcc51eff
MD5 0946fee02ad1e218676ac188cc11b4d5
BLAKE2b-256 ad76065095b2e600523ca27e50ff5f41852b9c2acc7c0d8adf1ece0e27fe630b

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.1-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.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 708d6d89564f57988cddeeb92bf53d41025d5bcd835b6081f7e6b841a96a37a2
MD5 8c3cfb2aa85ceff9c9f7092a34f489fe
BLAKE2b-256 4bc6b5a0d1e3a79c16e38c51a598e194d6167f398862c0def959eaebe56af2e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.1-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.1-cp311-cp311-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.1-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 19295ff4e579b744442d641b485fcbf1adc7b65170f0a83e60c8e83b7474a274
MD5 16bdfdf0ffcb400408cc615f006f4c30
BLAKE2b-256 ba1b748b35fbe9607e3dd83a7b41d89d70a18958ba4d106bddd1ff99c35bee5b

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.1-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.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2e58c138a1aeb454a0f680061d98fef6fa227880fec5e2a85feb2534c07efad4
MD5 b331d7f12fa0ce2a92693e0463a4f7bb
BLAKE2b-256 b36dbdeed63535fe79991fc1d9d284136e3c19f839854495c3c5af171cff8a2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.1-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.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 afd77893a21df634938526592219fa2080c4bc845466cb9aaa3dac91b4253ee2
MD5 b762bcb24033478696b4a4931ef7c585
BLAKE2b-256 138e3cd9c778980ee0e8d8830d76ca7406a4a92bafcca94be2f18ec2db4f95c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.1-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.1-cp310-cp310-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.1-cp310-cp310-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 3f65848a20ae59e32ef82e2d14fff0d43cba0be7bf92deec2cb990f8f0b9638a
MD5 c50b758cf946c293686fd0cf55479326
BLAKE2b-256 866f9f7068d550a156576b4889a4d190190f560473fd688f1006ffb77ccecdf7

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.1-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.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f5efb89f5847dcac21784a2ac0ea52b01eccba131e823435da15233f91003ece
MD5 44ae6dd0952798b080e1350e92825492
BLAKE2b-256 49fa8d05b8b1dae5fe2ec1b29e6272af485651c4a898214700b56cd568f2db85

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.1-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.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for gfnff-0.1.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 46c3c8c2163fa4c941e09892d4861461192dc284852b1cce4ec1d5e504f6d626
MD5 20cf769fc373350565060703d89ef635
BLAKE2b-256 68c612c7baeebfae102ddf342d662f69e1ad57562393eb124b2871fbc622e691

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.1-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.1-cp39-cp39-macosx_14_0_arm64.whl.

File metadata

  • Download URL: gfnff-0.1.1-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.1-cp39-cp39-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 fcc2f615c73bdea71abc5522587146b907964a01bb9fe0e07d5fdf2d36385a83
MD5 0c189aa6bb535a2d764df0a29082a6d7
BLAKE2b-256 0651433c06eccd590f6972a08b4fa5bf3b78312d0e051fa7ac6e609f08c93f69

See more details on using hashes here.

Provenance

The following attestation bundles were made for gfnff-0.1.1-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