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.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (15.5 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

gfnff-0.2.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.2.0-cp312-cp312-macosx_14_0_arm64.whl (5.1 MB view details)

Uploaded CPython 3.12macOS 14.0+ ARM64

gfnff-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (15.5 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

gfnff-0.2.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.2.0-cp311-cp311-macosx_14_0_arm64.whl (5.1 MB view details)

Uploaded CPython 3.11macOS 14.0+ ARM64

gfnff-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (15.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

gfnff-0.2.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.2.0-cp310-cp310-macosx_14_0_arm64.whl (5.1 MB view details)

Uploaded CPython 3.10macOS 14.0+ ARM64

gfnff-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (15.5 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

gfnff-0.2.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.2.0-cp39-cp39-macosx_14_0_arm64.whl (5.1 MB view details)

Uploaded CPython 3.9macOS 14.0+ ARM64

File details

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

File metadata

File hashes

Hashes for gfnff-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d9593f180c96d1f30edaf853a54a6cdaff31c7fdc180b3cc9955856137c156bb
MD5 446989a0d67f9f63e5dc31f137a58222
BLAKE2b-256 5efea1fe91cbf897c7cbf9c94c64ec80f4f3f93cab99e827fcc84637f282a02e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gfnff-0.2.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 f38157adee07fe36945757f0f198a9ec879382b0032596f7ad9dc33843a03405
MD5 cd0b41b243188499953f6f63d3f79037
BLAKE2b-256 09b8a5fb67897e4c360d6ace29ee7c5669040df83e4a32e606ce8a87e23c894f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gfnff-0.2.0-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 4d887c2af8ceecdf959820bd9ec7b7287b40022ac5e1ac8c0229ca34332dd352
MD5 af22e9da190ef50cbe01ae3c518a45e7
BLAKE2b-256 c48cfb99fd2e844b79390e84baa410fdc24275b15004acd00dc509bd1a39850b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gfnff-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 90473eec7e2ea4e2e9bd7372142ba99f36085132c2c2c073f098338f7e663688
MD5 706d03c537d388c925e3f24ff1048479
BLAKE2b-256 7ac08d7c26f84341906da6a8beeafe3fc7d960e5a8cee1bf9ae52453ac84420f

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gfnff-0.2.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2d5451bde7b59313800d026bd866ebdeb7ed74c4590c848c3ed3a1a6fb967f53
MD5 65c12c82688146e77ec3c41a1c90a99c
BLAKE2b-256 5a085dbbf5b4a683b54dac9d82b92d7ee94026b3ba2ca99159b4200b6a10447a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gfnff-0.2.0-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 3dbde43c84dffdcf26baf272dc2ef5e19af3a98d7e063c80f23202064448de7d
MD5 6cf5527daef6440d922bcdd2cbe5f8bb
BLAKE2b-256 6442a531970e18535889654ac54e167f3bfb163a05fd7dfc0a6b9b6a38617844

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gfnff-0.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0388fa8dc766aa1e1a2c2e034a1eae70b22bd9d8aac3d83e55cb2b57dfbbd40c
MD5 33f0e8002880d675aa24734cac674ed5
BLAKE2b-256 5e6334243c20dcb79fa17e170afa8f0acf9654bf73a9b7aa705da6477a34d8d7

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gfnff-0.2.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 698211ca579b8127930ecf86070c7387d3c6e7e8b2f84c078feb5b49662325fc
MD5 0bfadaf607d60823c20335d8f7ea4ded
BLAKE2b-256 4da67f533c3541eef3a0ee0f406290509753655a8ca63a68c07f398a96977f46

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gfnff-0.2.0-cp310-cp310-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 98ed11ff4507ebd4c2da6498ede49768d1ca8daa831980fe51a1168c57a4c2d9
MD5 9224f366a78420aa8c5e9acb4eef391d
BLAKE2b-256 e356d3828ca4ca5a7fb9c05bc317aebedb46d4b4a2b825868e0f6a52506e75d6

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gfnff-0.2.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0d834f84e367173c9ba8693730249ead1c01c4753e574ba30b699594ac2ddbd4
MD5 c4b37c8dece8fbbb3891117c59958d63
BLAKE2b-256 000ec97b526d07be8c16ca0be80ac9e819a58477542971e86a4697f38d7e40af

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for gfnff-0.2.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 89207674c109b852247046901587efa38556e037fd1c3c8ee49b357cb5592b4c
MD5 1356dd30bba109489751270a7a4c9c7c
BLAKE2b-256 5cbedb1a01c9faff2707c2384f512f31470dea07a032b4688af198bfeb966196

See more details on using hashes here.

Provenance

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

File metadata

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

File hashes

Hashes for gfnff-0.2.0-cp39-cp39-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 ae14107dfaf75b55a83b5d6b9800f0dd45dd92cda262fcd4f63039a54a7df150
MD5 3db3133adf5b2290b898170ca4e25489
BLAKE2b-256 2ec85cfac45ec2d7b33fd00807fbcac5d93e29d336bc9e418cec0583b30bb271

See more details on using hashes here.

Provenance

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