Python API#

The Python package is mainly used through the fmlip-relay-server command, but the pieces below are importable, e.g. for writing a backend or a test client in Python. The reference is generated from the docstrings.

Backends#

class fmlip_relay.backends.base.BackendBase[source]#

Minimal interface every fmlip_relay backend must satisfy.

Subclasses should load their model in __init__ so that loading happens exactly once at server startup.

close() → None[source]#

Release resources (temp dirs, GPU contexts) on server shutdown.

Called once when the server exits, also after SIGTERM/SIGHUP/SIGINT. The default does nothing.

abstractmethod compute(atomic_numbers: ndarray, positions: ndarray, cell: ndarray, pbc: ndarray, compute_stress: bool, charge: int, spin: int) → tuple[float, ndarray, ndarray][source]#

Evaluate the potential.

Parameters:
  • atomic_numbers ((N,) int32 – atomic numbers (Z))

  • positions ((N, 3) float64 – Cartesian coordinates, Angstrom)

  • cell ((3, 3) float64 – lattice row vectors, Angstrom)

  • pbc ((3,) bool – periodic boundary flags)

  • compute_stress (bool – whether to compute the stress tensor)

  • charge (int32 - total charge (neutral = 0))

  • spin (int32 - spin multiplicity 2S+1) – (ground-state singlet = 1)

Returns:

  • energy (float – potential energy, eV)

  • forces ((N, 3) float64 – forces, eV / Angstrom)

  • stress ((3, 3) float64 – stress tensor, eV / Angstrom^3) – (zero matrix when compute_stress is False)

compute_atomic(atomic_numbers: ndarray, positions: ndarray, cell: ndarray, pbc: ndarray, compute_stress: bool, charge: int, spin: int) → tuple[float, ndarray, ndarray, ndarray][source]#

Evaluate the potential and also return per-atom energies.

Takes the arguments of compute() and returns (energy, forces, stress, atomic_energies), where atomic_energies is an (N,) float64 array in eV that sums to energy.

The default splits the total energy equally over the atoms. Backends whose model has a real per-atom decomposition override this.

compute_hessian(atomic_numbers: ndarray, positions: ndarray, cell: ndarray, pbc: ndarray, charge: int, spin: int) → ndarray[source]#

Return the Hessian, the second derivatives of the energy with respect to the atomic positions, as a symmetric (3N, 3N) float64 array in eV/Ų. Row and column 3*i + a belong to Cartesian component a of atom i. The cell is held fixed.

The default takes central finite differences of the forces, which costs 6N calls of compute(). Backends that can differentiate their model analytically override this.

property device: str | None#

Compute device (e.g. "cpu", "cuda:0"), or None if N/A.

hessian_step = 0.005#

displacement in Å for the finite-difference Hessian

property name: str#

Human-readable backend name for log messages.

property precision: str | None#

Floating-point precision (e.g. "float32"), or None if N/A.

fmlip_relay.backends.base.partition_energy(energy: float, natoms: int, atomic_energies=None) → ndarray[source]#

Return (natoms,) per-atom energies that sum to energy.

Without atomic_energies (or if their shape does not fit) the total is split equally. Otherwise the model’s values are used, and whatever they leave out of the total (e.g. a dispersion correction added by a wrapper calculator) is spread equally over the atoms.

fmlip_relay.backends.get_backend_class(name: str) → type[BackendBase][source]#

Return the backend class for name, raising KeyError if unknown.

class fmlip_relay.backends._ase_base._ASEComputeMixin[source]#

Mixin that implements compute() on top of a stored ASE calculator self._calc. Subclasses only need to construct self._calc in __init__.

_e_to_ev and _l_to_ang convert a model that works in other units: positions and cell are passed to the calculator in model length units, and energy, forces and stress are converted back to eV and Å.

Server#

fmlip_relay.server.run(port: int, backend: BackendBase, exit_on_disconnect: bool = False, detach_stdout: bool = False) → None[source]#

Start the server, block until a QUIT message is received.

Parameters:
  • port (TCP port to bind (loopback only); 0 lets the OS pick one.)

  • backend (Initialised backend instance (model already loaded).)

  • exit_on_disconnect (also stop when the client goes away without QUIT.)

  • detach_stdout (after the READY line, point stdout at stderr. The client) – reads the stdout pipe only until READY, so anything a model library printed there later would fill the pipe and block the server.

Protocol#

fmlip_relay.protocol.MSG_COMPUTE = 1#

int([x]) -> integer int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating-point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by ‘+’ or ‘-’ and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int(‘0b100’, base=0) 4

fmlip_relay.protocol.MSG_COMPUTE_ATOMIC = 4#

int([x]) -> integer int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating-point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by ‘+’ or ‘-’ and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int(‘0b100’, base=0) 4

fmlip_relay.protocol.MSG_HESSIAN = 5#

int([x]) -> integer int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating-point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by ‘+’ or ‘-’ and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int(‘0b100’, base=0) 4

fmlip_relay.protocol.MSG_QUIT = 2#

int([x]) -> integer int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating-point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by ‘+’ or ‘-’ and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int(‘0b100’, base=0) 4

fmlip_relay.protocol.MSG_PING = 3#

int([x]) -> integer int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating-point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by ‘+’ or ‘-’ and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int(‘0b100’, base=0) 4

fmlip_relay.protocol.STATUS_OK = 0#

int([x]) -> integer int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating-point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by ‘+’ or ‘-’ and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int(‘0b100’, base=0) 4

fmlip_relay.protocol.STATUS_ERROR = 1#

int([x]) -> integer int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating-point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by ‘+’ or ‘-’ and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int(‘0b100’, base=0) 4

class fmlip_relay.protocol.ComputeRequest(natoms, atomic_numbers, positions, cell, pbc, compute_stress, charge, spin)[source]#

Decoded COMPUTE request.

fmlip_relay.protocol.read_compute_request(conn: socket) → ComputeRequest[source]#

Read and decode one COMPUTE payload (msg_type already consumed).

fmlip_relay.protocol.write_ok_response(conn: socket, energy: float, forces: ndarray, stress: ndarray, atomic_energies: ndarray | None = None) → None[source]#

Send an OK response; atomic_energies is appended for COMPUTE_ATOMIC.

fmlip_relay.protocol.write_error_response(conn: socket, natoms: int, atomic: bool = False) → None[source]#

Send a zero-filled ERROR response of the same length as an OK one.

fmlip_relay.protocol.write_hessian_response(conn: socket, hessian: ndarray) → None[source]#

Send an OK status followed by the (3N, 3N) Hessian.

Unlike COMPUTE, a failed HESSIAN request is answered with the status alone (see write_status()), since a zero-filled matrix of this size would be wasteful.

fmlip_relay.protocol.write_status(conn: socket, status: int) → None[source]#

Send a bare status word.

fmlip_relay.protocol.recv_exactly(conn: socket, n: int) → bytes[source]#

Block until exactly n bytes have been read from conn.

fmlip_relay.protocol.send_all(conn: socket, data: bytes) → None[source]#

Send all of data, retrying on short writes.

Geometry input#

fmlip_relay.test_mode.read_geometry(path: str) → tuple[ndarray, ndarray, ndarray, ndarray, int | None, int | None][source]#

Read a molecular geometry file and return arrays suitable for BackendBase.compute().

Tries ase.io.read first (broad format support); falls back to a minimal plain-XYZ parser when ASE is not installed.

Parameters:

path (str) – Path to the geometry file.

Returns:

  • atomic_numbers (np.ndarray shape (N,) int32)

  • positions (np.ndarray shape (N, 3) float64 [Angstrom])

  • cell (np.ndarray shape (3, 3) float64 [Angstrom])

  • pbc (np.ndarray shape (3,) bool)

  • charge (int | None total charge if present in the file, else None)

  • spin (int | None spin multiplicity (2S+1) if present, else None)