Fortran client#
The module fmlip_relay_client manages the server processes and the socket connections.
Each instance is one Python process plus one TCP connection, addressed by an integer id between 1 and MLIP_MAX_INSTANCES (16).
use fmlip_relay_client
Minimal usage#
use fmlip_relay_client
integer :: ierr, charge, spin
integer :: pbc(3), atomic_numbers(64)
real(8) :: pos(3,64), cell(3,3), energy, forces(3,64), stress(3,3)
!>-- spawn the server, blocks until Python prints "READY"
call mlip_init(1, 54321, &
"fmlip-relay-server --port 54321 --backend mace --model /path/model.model", &
60, ierr)
if (ierr /= MLIP_OK) stop "mlip_init failed"
!>-- evaluate
pbc = [1, 1, 1]
atomic_numbers = 13 ! aluminium
charge = 0
spin = 1
call mlip_compute(1, 64, atomic_numbers, pos, cell, pbc, 1, charge, spin, &
energy, forces, stress, ierr)
!>-- teardown
call mlip_finalize(1, ierr)
For molecules and clusters, pass pbc = [0, 0, 0] and compute_stress = 0.
The cell is unused in that case but should still be a non-degenerate matrix (e.g. a large cubic box), since some backends invert it regardless.
Conventions#
- Units
Positions and cell in Å, energy in eV, forces in eV/Å, stress in eV/ų.
- Array layout
positionsandforcesare dimensioned(3, natoms), i.e. the first index runs over x, y, z. The rows ofcellare the lattice vectors.- Integers
All integer arguments are default 32-bit
INTEGER.- Error handling
Every routine returns
ierr, which isMLIP_OK(0) on success andMLIP_ERR(1) otherwise. Diagnostics are written to standard output.
API reference#
mlip_init#
call mlip_init(iid, port, server_cmd, timeout_sec, ierr)
Spawns the server process, waits for its READY line, and connects.
On failure the half-started instance is cleaned up.
Argument |
Type |
Intent |
Description |
|---|---|---|---|
|
|
in |
Instance id, |
|
|
in |
TCP port to connect to; ignored if the server announces its own (see below) |
|
|
in |
Full shell command that launches the server, at most 1023 characters |
|
|
in |
Seconds to wait for |
|
|
out |
Status |
The command is free-form, so any server option can be passed through it.
Loading a large model on a cold filesystem can take a minute or more; set timeout_sec accordingly.
If the command contains --port 0, the operating system assigns a free port, the server announces it as READY <port>, and mlip_init connects to the announced port.
The port argument is then ignored.
This avoids port collisions between independent jobs on the same node.
mlip_compute#
call mlip_compute(iid, natoms, atomic_numbers, positions, cell, pbc, compute_stress, &
charge, spin, energy, forces, stress, ierr [, atomic_energies])
Sends one structure and receives energy, forces, and stress.
Argument |
Type |
Intent |
Description |
|---|---|---|---|
|
|
in |
Instance id |
|
|
in |
Number of atoms |
|
|
in |
Atomic number Z of each atom |
|
|
in |
Cartesian coordinates in Å |
|
|
in |
Lattice vectors as rows, in Å |
|
|
in |
1 for a periodic axis, 0 otherwise |
|
|
in |
1 to request the stress tensor |
|
|
in |
Total charge, default 0 |
|
|
in |
Spin multiplicity 2S+1, default 1 |
|
|
out |
Potential energy in eV |
|
|
out |
Forces in eV/Å |
|
|
out |
Stress tensor in eV/ų, zero if not requested |
|
|
out |
Status |
|
|
out |
Per-atom energies in eV |
charge and spin sit in the middle of the argument list.
To omit them, pass the output arguments by keyword:
call mlip_compute(1, natoms, atomic_numbers, pos, cell, pbc, 1, &
energy=energy, forces=forces, stress=stress, ierr=ierr)
Charge and spin only affect backends that model them (UMA with the omol task, MACE-POLAR); all others ignore the values.
Per-atom energies#
Passing the optional atomic_energies array requests a decomposition of the energy over the atoms:
real(8) :: e_atom(natoms)
call mlip_compute(1, natoms, atomic_numbers, pos, cell, pbc, 0, &
energy=energy, forces=forces, stress=stress, ierr=ierr, &
atomic_energies=e_atom)
The values always sum to energy.
Models with a per-atom decomposition (the MACE family, lj, and custom calculators that provide ASE’s energies) return it; all other backends return energy/natoms for every atom, see Backends.
The request uses a separate message type, so calls without the argument are unchanged, and a server too old to know it makes the call return MLIP_ERR.
Errors#
A backend exception on the Python side does not kill the server.
The call returns ierr = MLIP_ERR with zeroed outputs, and the instance stays usable for the next structure.
mlip_hessian#
call mlip_hessian(iid, natoms, atomic_numbers, positions, cell, pbc, hessian, ierr [, charge, spin])
Returns the second derivatives of the energy with respect to the atomic positions.
Argument |
Type |
Intent |
Description |
|---|---|---|---|
|
|
in |
Instance id |
|
|
in |
Number of atoms |
|
|
in |
Atomic number Z of each atom |
|
|
in |
Cartesian coordinates in Å |
|
|
in |
Lattice vectors as rows, in Å |
|
|
in |
1 for a periodic axis, 0 otherwise |
|
|
out |
Hessian in eV/Ų, symmetric |
|
|
out |
Status |
|
|
in |
Total charge, default 0 |
|
|
in |
Spin multiplicity 2S+1, default 1 |
Row and column 3*(i-1)+a of hessian belong to Cartesian component a of atom i.
For periodic systems the cell is held fixed.
real(8) :: hess(3*natoms,3*natoms)
call mlip_hessian(1, natoms, atomic_numbers, pos, cell, pbc, hess, ierr)
Backends that can differentiate their model return an analytical Hessian.
All others compute it on the server from central finite differences of the forces, which takes 6·natoms force evaluations, so a call can run for a long time; see Backends.
On failure hessian is zero and ierr = MLIP_ERR, and the instance stays usable.
A server too old to know the request also gives MLIP_ERR.
mlip_ping#
call mlip_ping(iid, ierr)
Round trip without a computation, to check that the server is alive.
mlip_finalize and mlip_finalize_all#
call mlip_finalize(iid, ierr)
call mlip_finalize_all(ierr)
mlip_finalize sends QUIT, closes the socket, terminates the server’s process group, and returns once the server has exited.
A server that is still alive after 10 s is killed.
Calling it on an inactive instance is a no-op that returns MLIP_OK.
mlip_finalize_all does the same for every active instance; it signals all servers first and then waits, so they shut down side by side.
Constants and helpers#
Symbol |
Value |
Meaning |
|---|---|---|
|
16 |
Largest valid instance id |
|
0 |
Success |
|
1 |
Failure |
|
PID of the calling process, handy for deriving unique port numbers |
Parallel instances#
Each instance owns its socket and only instance-local state, so different instances can be called from different OpenMP threads without locking. A single instance must not be shared between threads.
!>-- start all servers serially, before the parallel region
do i = 1, N
write(cmd, '("fmlip-relay-server --port ",I0," --backend mace --model ",A)') &
BASE_PORT+i, trim(model_path)
call mlip_init(i, BASE_PORT+i, trim(cmd), 120, ierr)
end do
!$omp parallel do num_threads(N) private(iid, E, F, S, ierr) schedule(static,1)
do i = 1, N
iid = omp_get_thread_num() + 1
call mlip_compute(iid, natoms, atomic_numbers, pos(:,:,i), cell, pbc, 0, &
energy=E, forces=F, stress=S, ierr=ierr)
end do
!$omp end parallel do
call mlip_finalize_all(ierr)
Starting the servers serially matters: several processes loading the same checkpoint from a shared filesystem at once can race on download and cache files.
Choosing ports#
With fixed ports, every instance on a node needs its own number.
Under MPI, a scheme such as port = base_port + mpi_rank * 100 + local_instance keeps the ranks apart.
The simpler route is --port 0 in every server command, which leaves the choice to the operating system (see mlip_init).
Orphaned servers#
If the Fortran program crashes, mlip_finalize never runs and the servers would keep waiting for a new connection.
Add --exit-on-disconnect to the server command to make a server exit as soon as its client goes away without sending QUIT.