Development#
Layout#
The repository spans three languages:
Path |
Content |
|---|---|
|
Server, protocol codec, command-line interface |
|
One module per backend, plus the registry |
|
The client module |
|
POSIX socket and process helpers called from Fortran |
|
Example driver with single and parallel instances |
|
pytest suite for the Python side |
|
This documentation |
The server (server.py) contains no ML code.
It binds the socket, prints READY <port>, and dispatches COMPUTE, PING, and QUIT to whichever backend object it was given.
Adding a backend#
For a one-off ASE calculator, the custom backend needs no changes to the package.
A built-in backend takes three steps.
1. Implement the class in src/python/fmlip_relay/backends/mybackend.py.
Subclass BackendBase and load the model in __init__, which runs once at server startup:
import numpy as np
from .base import BackendBase
class MyBackend(BackendBase):
def __init__(self, model_path, device="cpu"):
... # load the model here
def compute(self, atomic_numbers, positions, cell, pbc,
compute_stress, charge, spin):
# atomic_numbers : (N,) int32
# positions : (N, 3) float64, Å
# cell : (3, 3) float64, Å, lattice vectors as rows
# pbc : (3,) bool
# compute_stress : bool
# charge, spin : int (total charge, multiplicity 2S+1)
...
return energy, forces, stress # float, (N, 3), (3, 3)
If the model already has an ASE calculator, subclass _ASEComputeMixin instead and only set self._calc in __init__; compute and the unit conversion are inherited.
backends/_template_ase_backend.py is a starting point to copy.
An optional close() method is called at shutdown and can release temporary directories or GPU contexts.
Per-atom energies need no extra work: BackendBase.compute_atomic() calls compute() and splits the total equally, and the ASE mixin reads the calculator’s energies result when there is one.
Override compute_atomic() only for a model with its own decomposition, and return (energy, forces, stress, atomic_energies) with the atomic energies summing to the total.
The same holds for Hessians: BackendBase.compute_hessian() differentiates the forces numerically, and the ASE mixin calls the calculator’s get_hessian() when it exists.
Override it for a model with analytical second derivatives and return a symmetric (3N, 3N) array in eV/Ų.
2. Register it in src/python/fmlip_relay/backends/__init__.py, inside _register_optional().
The try/except ImportError keeps a missing dependency from breaking the other backends:
try:
from .mybackend import MyBackend
_REGISTRY["mybackend"] = MyBackend
except ImportError:
pass
The existing backends import their heavy dependencies inside __init__ of the class, so the module itself always imports and the missing package is reported when the server starts.
3. Wire up the command line in src/python/fmlip_relay/__main__.py: add an argument group in _build_parser() if the backend has its own options, and a branch in _build_backend() that maps the parsed arguments to constructor keywords.
Then add a row to docs/backends.md and an entry to _BACKENDS in check.py, listing the packages the backend needs so that fmlip-relay-check can report on it.
Tests#
pip install -e ".[dev]"
pytest
pytest tests/test_server.py::TestPing # a single class or test
The suite needs no ML packages.
Module |
Covers |
|---|---|
|
Constants, |
|
|
|
TCP integration: |
Tests that need ase are skipped when it is not installed.
The Fortran client has no unit tests of its own; run_example dummy exercises the full path from Fortran through the socket and back.
Building the documentation#
pip install ".[docs]"
cd docs && make html
The result is in docs/_build/html.
The package itself must be installed, because the option reference and the API page are generated from the imported code.
On every push to main, the docs.yml workflow builds the site and deploys it to GitHub Pages.