Metadata-Version: 2.5
Name: flmlip
Version: 0.1.0
Summary: Absolute free energies of solids from foundation model interatomic potentials, by Frenkel Ladd nonequilibrium switching
Project-URL: Homepage, https://github.com/Shi-Research-Group/flmlip
Project-URL: Issues, https://github.com/Shi-Research-Group/flmlip/issues
License-Expression: MIT
License-File: LICENSE
Keywords: MOF,free energy,frenkel ladd,machine learning potential,phonons,thermodynamic integration
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Chemistry
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=3.10
Requires-Dist: ase>=3.23
Requires-Dist: numpy>=1.24
Requires-Dist: spglib>=2.0
Provides-Extra: dev
Requires-Dist: build>=1.2; extra == 'dev'
Requires-Dist: pytest>=7; extra == 'dev'
Requires-Dist: ruff>=0.5; extra == 'dev'
Provides-Extra: fairchem
Requires-Dist: fairchem-core; extra == 'fairchem'
Provides-Extra: mace
Requires-Dist: mace-torch; extra == 'mace'
Provides-Extra: mattersim
Requires-Dist: mattersim; extra == 'mattersim'
Provides-Extra: orb
Requires-Dist: orb-models; extra == 'orb'
Provides-Extra: sevennet
Requires-Dist: sevenn; extra == 'sevennet'
Description-Content-Type: text/markdown

# flmlip

[![tests](https://github.com/Shi-Research-Group/flmlip/actions/workflows/tests.yml/badge.svg)](https://github.com/Shi-Research-Group/flmlip/actions/workflows/tests.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Relax crystals with machine learned interatomic potentials, and get their
absolute free energies by Frenkel Ladd nonequilibrium thermodynamic integration.

## Install

```
pip install flmlip                 # core: numpy, ase, spglib
pip install "flmlip[mace]"         # or fairchem, orb, sevennet, mattersim
```

## Relax

```python
from ase.io import read
from flmlip import load_calculator, relax

atoms = read("crystal.cif")
result = relax(atoms, load_calculator("mace", model="medium"), fmax=1e-8)

print(result.report())
atoms.write("relaxed.cif")
```

```
flmlip relax crystal.cif --model mace --fix-symmetry
```

Cell and positions by default; `relax_cell=False` holds the cell, `pressure`
loads it, `fix_symmetry` keeps the space group, and the result reports the space
group before and after so a symmetry break is visible.

## Free energy

```python
from ase.io import read
from flmlip import load_calculator, gibbs_free_energy

atoms = read("crystal.cif")
calc = load_calculator("mace", model="medium")
G, result = gibbs_free_energy(atoms, calc, temperature=298.15, pressure=1.0)

print(G / len(atoms), "eV/atom")
print(result.report())
```

A free energy is a chain of stages that each depend on the last, so the whole
run is written as one input deck rather than as a script you have to get right
by hand. It reads like a LAMMPS input and runs with `flmlip run deck.in`.
Commands execute in order; `#` comments, `&` continues a line, `variable` and
`${name}` substitute.

```
variable        T 298.15
structure       mof.cif
model           mace medium
replicate       2 2 2
temperature     ${T}
thermostat      langevin 100.0
seed            7731

relax           fmax 1e-8 cell free scheme alternating
npt_cell        equil 100000 sample 100000
relax           fmax 1e-8 cell fixed
msd             equil 50000 sample 50000
springs         msd floor 0.4336
springs         write springs.json
frenkel_ladd    switch 400000 equil 30000
gibbs
write           result.json
write_cif
```

Units are eV, Angstrom, femtosecond, Kelvin, bar. Spring constants are eV/A^2.
Single steps are also callable: `relax`, `equilibrium_cell`,
`mean_square_displacement`, `springs_from_msd`, `frenkel_ladd`. `examples/` has
one complete run of each kind with its output and log files kept.

## Method

The solid is connected reversibly to an Einstein crystal, whose free energy is
analytic:

    H(l) = (1 - l) U_solid + l U_Einstein

dH/dl is integrated along one nonequilibrium trajectory each way and the two
works averaged, which cancels the leading dissipative error. Adding the Einstein
term gives an absolute Helmholtz free energy; `gibbs` adds PV.

Stage order follows the reference implementation: relax, NPT for the cell,
relax positions in that cell, mean squared displacement, springs, relax again so
springs anchor on lattice sites, switch.

## Backends

`mace`, `fairchem` (UMA, eSEN), `orb`, `sevennet`, `mattersim` by name; model
libraries import only when requested. Any other ASE calculator can be passed
directly.

```python
calc = load_calculator("mace", model="medium")   # MACE-MP-0 medium
calc = load_calculator("mace", model="medium-omat-0")
calc = load_calculator("mace", model="finetuned.model", head="pbe_d3")
calc = load_calculator("fairchem", model="uma-s-1p1", task_name="omat")
```

The MACE names are `mace_mp`'s own. "medium" is MACE-MP-0 medium, checkpoint
2023-12-03-mace-128-L1_epoch-199.model, fetched to ~/.cache/mace on first use;
"medium-mpa-0" is MACE-MPA-0 and "medium-omat-0" is MACE-OMAT-0.

Named backends default to float64 with TF32 off: a relaxation cannot converge
below the precision of the forces driving it.

## Defaults

    timestep                    1 fs
    thermostat                  Langevin, 100 fs damping, centre of mass held
    mean squared displacement   50 ps after 50 ps of settling
    springs                     k = 3 kT / <dr^2> per element, floor 10 kcal/mol/A^2
    equilibration               30 ps in each end state
    switching                   400 ps each way, tau^5 polynomial with flat ends

Loosen `fmax` and `energy_step_tol` together: convergence requires both.

Two things decide whether a number means anything. Structures being compared
must share one set of springs and hold the same number of atoms of each element,
so the Einstein reference cancels; `springs write` and `springs read` exist for
that. And every perpendicular width of the cell must exceed twice the potential
cutoff.

Long runs can be checkpointed and resumed exactly:

```
frenkel_ladd    switch 400000 equil 30000 checkpoint fl.npz every 10000
```

`max_steps` stops after a given number of steps having saved, for wall clock
limited queues; call again to continue.

## Feedback

Questions, bugs, or a number that looks wrong: [parsakho@buffalo.edu](mailto:parsakho@buffalo.edu), or open an issue.
