Metadata-Version: 2.2
Name: gfnff
Version: 0.1.0
Summary: Python bindings for the GFN-FF force-field library
Keywords: force field,GFN-FF,computational chemistry
Author-Email: Philipp Pracht <research@philipp-pracht.de>
License:                    GNU LESSER GENERAL PUBLIC LICENSE
                                Version 3, 29 June 2007
         
          Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
          Everyone is permitted to copy and distribute verbatim copies
          of this license document, but changing it is not allowed.
         
         
           This version of the GNU Lesser General Public License incorporates
         the terms and conditions of version 3 of the GNU General Public
         License, supplemented by the additional permissions listed below.
         
           0. Additional Definitions.
         
           As used herein, "this License" refers to version 3 of the GNU Lesser
         General Public License, and the "GNU GPL" refers to version 3 of the GNU
         General Public License.
         
           "The Library" refers to a covered work governed by this License,
         other than an Application or a Combined Work as defined below.
         
           An "Application" is any work that makes use of an interface provided
         by the Library, but which is not otherwise based on the Library.
         Defining a subclass of a class defined by the Library is deemed a mode
         of using an interface provided by the Library.
         
           A "Combined Work" is a work produced by combining or linking an
         Application with the Library.  The particular version of the Library
         with which the Combined Work was made is also called the "Linked
         Version".
         
           The "Minimal Corresponding Source" for a Combined Work means the
         Corresponding Source for the Combined Work, excluding any source code
         for portions of the Combined Work that, considered in isolation, are
         based on the Application, and not on the Linked Version.
         
           The "Corresponding Application Code" for a Combined Work means the
         object code and/or source code for the Application, including any data
         and utility programs needed for reproducing the Combined Work from the
         Application, but excluding the System Libraries of the Combined Work.
         
           1. Exception to Section 3 of the GNU GPL.
         
           You may convey a covered work under sections 3 and 4 of this License
         without being bound by section 3 of the GNU GPL.
         
           2. Conveying Modified Versions.
         
           If you modify a copy of the Library, and, in your modifications, a
         facility refers to a function or data to be supplied by an Application
         that uses the facility (other than as an argument passed when the
         facility is invoked), then you may convey a copy of the modified
         version:
         
            a) under this License, provided that you make a good faith effort to
            ensure that, in the event an Application does not supply the
            function or data, the facility still operates, and performs
            whatever part of its purpose remains meaningful, or
         
            b) under the GNU GPL, with none of the additional permissions of
            this License applicable to that copy.
         
           3. Object Code Incorporating Material from Library Header Files.
         
           The object code form of an Application may incorporate material from
         a header file that is part of the Library.  You may convey such object
         code under terms of your choice, provided that, if the incorporated
         material is not limited to numerical parameters, data structure
         layouts and accessors, or small macros, inline functions and templates
         (ten or fewer lines in length), you do both of the following:
         
            a) Give prominent notice with each copy of the object code that the
            Library is used in it and that the Library and its use are
            covered by this License.
         
            b) Accompany the object code with a copy of the GNU GPL and this license
            document.
         
           4. Combined Works.
         
           You may convey a Combined Work under terms of your choice that,
         taken together, effectively do not restrict modification of the
         portions of the Library contained in the Combined Work and reverse
         engineering for debugging such modifications, if you also do each of
         the following:
         
            a) Give prominent notice with each copy of the Combined Work that
            the Library is used in it and that the Library and its use are
            covered by this License.
         
            b) Accompany the Combined Work with a copy of the GNU GPL and this license
            document.
         
            c) For a Combined Work that displays copyright notices during
            execution, include the copyright notice for the Library among
            these notices, as well as a reference directing the user to the
            copies of the GNU GPL and this license document.
         
            d) Do one of the following:
         
                0) Convey the Minimal Corresponding Source under the terms of this
                License, and the Corresponding Application Code in a form
                suitable for, and under terms that permit, the user to
                recombine or relink the Application with a modified version of
                the Linked Version to produce a modified Combined Work, in the
                manner specified by section 6 of the GNU GPL for conveying
                Corresponding Source.
         
                1) Use a suitable shared library mechanism for linking with the
                Library.  A suitable mechanism is one that (a) uses at run time
                a copy of the Library already present on the user's computer
                system, and (b) will operate properly with a modified version
                of the Library that is interface-compatible with the Linked
                Version.
         
            e) Provide Installation Information, but only if you would otherwise
            be required to provide such information under section 6 of the
            GNU GPL, and only to the extent that such information is
            necessary to install and execute a modified version of the
            Combined Work produced by recombining or relinking the
            Application with a modified version of the Linked Version. (If
            you use option 4d0, the Installation Information must accompany
            the Minimal Corresponding Source and Corresponding Application
            Code. If you use option 4d1, you must provide the Installation
            Information in the manner specified by section 6 of the GNU GPL
            for conveying Corresponding Source.)
         
           5. Combined Libraries.
         
           You may place library facilities that are a work based on the
         Library side by side in a single library together with other library
         facilities that are not Applications and are not covered by this
         License, and convey such a combined library under terms of your
         choice, if you do both of the following:
         
            a) Accompany the combined library with a copy of the same work based
            on the Library, uncombined with any other library facilities,
            conveyed under the terms of this License.
         
            b) Give prominent notice with the combined library that part of it
            is a work based on the Library, and explaining where to find the
            accompanying uncombined form of the same work.
         
           6. Revised Versions of the GNU Lesser General Public License.
         
           The Free Software Foundation may publish revised and/or new versions
         of the GNU Lesser General Public License from time to time. Such new
         versions will be similar in spirit to the present version, but may
         differ in detail to address new problems or concerns.
         
           Each version is given a distinguishing version number. If the
         Library as you received it specifies that a certain numbered version
         of the GNU Lesser General Public License "or any later version"
         applies to it, you have the option of following the terms and
         conditions either of that published version or of any later version
         published by the Free Software Foundation. If the Library as you
         received it does not specify a version number of the GNU Lesser
         General Public License, you may choose any version of the GNU Lesser
         General Public License ever published by the Free Software Foundation.
         
           If the Library as you received it specifies that a proxy can decide
         whether future versions of the GNU Lesser General Public License shall
         apply, that proxy's public statement of acceptance of any version is
         permanent authorization for you to choose that version for the
         Library.
         
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Fortran
Classifier: Topic :: Scientific/Engineering :: Chemistry
Requires-Python: >=3.9
Requires-Dist: numpy>=1.21
Provides-Extra: ase
Requires-Dist: ase>=3.22; extra == "ase"
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Requires-Dist: ase>=3.22; extra == "test"
Description-Content-Type: text/markdown

<div align="center">

<h1>GFN-FF</h1>
<h3>A general force field for elements <i>Z</i> = 1–103</h3>

![build status](https://github.com/pprcht/gfnff/actions/workflows/build-and-test.yml/badge.svg)
[![License: LGPL v3](https://img.shields.io/badge/license-LGPL_v3-coral.svg)](https://www.gnu.org/licenses/lgpl-3.0)

</div>

This repository provides a standalone library implementation of the **GFN-FF** method by **S.Spicher** and **S.Grimme**,
adapted from the [`xtb`](https://github.com/grimme-lab/xtb) code (most recently at commit [`6d44803`](https://github.com/grimme-lab/xtb/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](https://doi.org/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](https://doi.org/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](https://doi.org/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.

<table>
<tr>
<th>CMake</th>
<th>Meson</th>
</tr>
<tr>
<td>

```bash
cmake -B _build
cmake --build _build
```

To run the test suite:

```bash
cmake -B _build -DWITH_TESTS=ON
cmake --build _build
ctest --test-dir _build
```

</td>
<td>

```bash
meson setup _build
ninja -C _build
```

To run the test suite:

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

</td>
</tr>
</table>

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.

<table>
<tr><td>
<details>
<summary><b>Fortran</b></summary>

```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`](app/main.F90).

</details>
</td></tr>
<tr><td>
<details>
<summary><b>C</b></summary>

```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`](test/main.c).

</details>
</td></tr>
<tr><td>
<details>
<summary><b>C++</b></summary>

```cpp
#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`](test/main.cpp).

</details>
</td></tr>
<tr><td>
<details>
<summary><b>Integrating as a CMake subproject</b></summary>

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

```cmake
add_subdirectory(gfnff)
target_link_libraries(my_target PRIVATE gfnff)
```

</details>
</td></tr>
<tr><td>
<details>
<summary><b>Integrating as a Meson subproject</b></summary>

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

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

</details>
</td></tr>
</table>

---

## 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`](test/main.c) and [`test/main.cpp`](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:

```bash
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:

```bash
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):

```bash
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`):

```bash
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):

```bash
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.

```python
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:

```python
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`](https://wiki.fysik.dtu.dk/ase/ase/calculators/calculators.html).
It handles unit conversion automatically (Å ↔ Bohr, eV ↔ Hartree).
Implemented properties: **energy**, **forces**, **stress**.

```python
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:

```python
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`:

```python
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

```bash
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`](LICENSE) for details.
