Architecture ------------ This page explains how the layers of ``rgpot`` fit together and why errors and capabilities cross them the way they do. Why the wire uses three carriers is in :doc:`The RPC Model `. The directory layout and the list of potentials are in :doc:`Architecture reference <../reference/architecture>`. The C entry points are in :doc:`C ABI of rgpot-core <../reference/c_abi>`, and the C++ wrappers in :doc:`C++ API <../reference/cpp_api>`. ``rgpot`` follows a *metatensor*-style layout. A Rust crate, ``rgpot-core``, defines the data exchange types and the RPC client and server. It exports them through a C ABI whose header ``cbindgen`` generates. Hand-written C++ headers wrap that ABI in move-only handles. The C++ potentials in ``CppCore/rgpot/`` plug into the Rust core through a callback. Layer diagram ~~~~~~~~~~~~~ :: +------------------------------------------------+ | C++ wrappers (move-only handles) | | include/rgpot/*.hpp | +------------------------------------------------+ | C ABI (generated by cbindgen) | | rgpot-core/include/rgpot.h | +------------------------------------------------+ | Rust core | | rgpot-core/src/ | | - DLPack input and output structs | | - callback dispatch (rgpot_potential_t) | | - Hessian-vector products | | - status codes, thread-local message | | - capability check (schema feature) | | - profile loader (profile feature) | | - Cap'n Proto RPC client and server (rpc) | +------------------------------------------------+ | callbacks +------------------------------------------------+ | C++ potentials, CppCore/rgpot// | | classes derived from rgpot::Potential| +------------------------------------------------+ Layers ~~~~~~ Rust core (``rgpot-core/``) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ The crate owns the public C types. Every exported struct is ``#[repr(C)]`` and every exported function is ``extern "C"``. ``rgpot_force_input_t`` and ``rgpot_force_out_t`` the data exchange structs. The input holds three borrowed DLPack tensors: positions, atomic numbers and the box matrix. The output holds a forces tensor that the callback allocates and the caller frees with ``rgpot_tensor_free``, plus the energy and the variance as plain doubles. DLPack keeps the structs independent of the device that holds the data. ``rgpot_status_t`` the status codes ``RGPOT_SUCCESS``, ``RGPOT_INVALID_PARAMETER``, ``RGPOT_INTERNAL_ERROR``, ``RGPOT_RPC_ERROR`` and ``RGPOT_BUFFER_SIZE_ERROR``. Calls that can fail return one of them. Constructors such as ``rgpot_potential_new_eindir`` and ``rgpot_rpc_client_new`` return a pointer instead, and NULL on failure. ``rgpot_potential_t`` a ``#[repr(C)]`` struct whose first member is an ``eindir_objective_t``, so a pointer to it is also a pointer to an eindir objective. It carries the callback, its ``void*`` user data and an optional destructor for that data. The core calls the callback without knowing the concrete C++ type behind it. Callers create it with ``rgpot_potential_new`` or ``rgpot_potential_new_eindir`` and release it with ``rgpot_potential_free``. ``rgpot_last_error`` returns the thread-local message of the last failure. The pointer stays valid until the next ``rgpot_*`` call on the same thread. Feature gates ::::::::::::: ``rgpot-core/Cargo.toml`` keeps the default feature set empty. ``schema`` compiles ``Potentials.capnp`` with ``capnpc`` and adds the capability check and ``rgpot_potential_set_peer_capabilities``. ``profile`` enables ``schema`` and adds the profile loader (``ProfileSession``), which opens a backend library with ``libloading``. ``rpc`` enables ``schema`` and adds the Cap'n Proto RPC client and server, with ``capnp-rpc``, ``tokio``, ``tokio-util`` and ``futures``. ``cache`` declared with no code behind it; the Meson option ``-Dwith_cache=true`` passes it to Cargo. ``gen-header`` regenerates ``rgpot-core/include/rgpot.h`` with ``cbindgen``; ``pixi r gen-header`` runs ``cargo build --features gen-header``. The Meson build compiles the crate when ``-Dwith_rust_core=true`` is set. It passes ``rpc`` when RPC is enabled and ``cache`` when ``-Dwith_cache=true`` is set. The schema copy ::::::::::::::: The crate bundles ``rgpot-core/schema/Potentials.capnp``, so a build from crates.io has the schema. ``build.rs`` prefers that copy and falls back to ``CppCore/rgpot/rpc/Potentials.capnp`` when it is missing. The Cap'n Proto wire format does not depend on the language, so a Rust client can call a C++ server and the reverse. C ABI (``rgpot-core/include/rgpot.h``) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The header is the contract between the Rust core and every C or C++ consumer. ``cbindgen`` generates it from the Rust source, so nobody edits it by hand. A change to the public API is made in the Rust source and the header is regenerated. The header holds the ``#[repr(C)]`` structs and enums, the function declarations, and the Rust doc comments as Doxygen comments. ``rgpot_fused_cache_t`` and ``rgpot_rpc_client_t`` are opaque; ``rgpot_potential_t`` is a full struct. Declarations that need the ``rpc`` feature sit inside ``#if defined(RGPOT_HAS_RPC)``. C++ wrappers (``include/rgpot/``) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The headers give C++ callers handles that free themselves and exceptions in place of status codes. ``errors.hpp`` ``rgpot::Error``, a ``std::runtime_error``, and ``rgpot::detail::check_status``. ``check_status`` reads ``rgpot_last_error`` and throws ``rgpot::Error`` with a prefix that names the status category. ``types.hpp`` ``rgpot::InputSpec`` wraps caller buffers in non-owning DLPack tensors, and ``rgpot::CalcResult`` owns the forces tensor that a calculation returns. The names differ from ``rgpot::ForceInput`` and ``rgpot::ForceOut`` in ``ForceStructs.hpp``, so both sets can appear in one translation unit. ``potential.hpp`` ``rgpot::PotentialHandle``, a move-only owner of one ``rgpot_potential_t``. ``from_impl`` wraps any object with a ``forceImpl(const ForceInput&, ForceOut*)`` method through a template trampoline. ``from_callback`` takes a C function pointer, its user data and an optional destructor. ``calculate`` runs the potential and returns a ``CalcResult``. ``rpc_client.hpp`` ``rgpot::RpcClient``, a move-only owner of one ``rgpot_rpc_client_t``, with the same ``calculate`` interface. The constructor does not connect; the Rust client opens a connection for each ``calculate``. The header compiles only when ``RGPOT_HAS_RPC`` is defined. ``rgpot.hpp`` includes the headers above, and ``rpc_client.hpp`` when ``RGPOT_HAS_RPC`` is defined. C++ potentials (``CppCore/rgpot/``) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Each potential derives from the CRTP template ``rgpot::Potential`` and implements ``forceImpl(const ForceInput&, ForceOut*) const``. ``rgpot::PotentialHandle::from_impl`` turns such an object into an ``rgpot_potential_t`` without copying it. The caller keeps the object alive for as long as the handle exists. Error handling ~~~~~~~~~~~~~~ Errors change form at each boundary, because each language has its own convention and unwinding across a C boundary is not allowed. 1. **Inside Rust**, code uses ``Result``, and the ``extern "C"`` entry points turn errors into status codes. 2. **Across the C ABI**, a call returns a status code, or NULL for a constructor, and stores a message that ``rgpot_last_error`` returns. No exception or panic crosses the boundary. ``rgpot_potential_calculate``, ``rgpot_potential_hvp``, ``rgpot_potential_hvp_centre``, ``rgpot_rpc_calculate`` and ``rgpot_rpc_server_start`` run their body inside ``catch_unwind``, which turns a panic into ``RGPOT_INTERNAL_ERROR`` with the panic message. These are the entry points that run a user callback or the RPC runtime. 3. **Inside C++**, the wrappers pass every status to ``rgpot::detail::check_status``, which throws ``rgpot::Error``. In the other direction, the ``from_impl`` trampoline catches any exception from ``forceImpl`` and returns ``RGPOT_INTERNAL_ERROR``. It does not store the exception text, so the message that ``rgpot_last_error`` returns afterwards does not describe that failure. Backend capability handshake ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ A backend can be incompatible with the host in ways a single call does not show: another protocol, another schema, or another eindir bridge layout. The capability handshake refuses such a backend before any work is dispatched. It runs where a backend meets the host: when a library is loaded through the potential profile, when an RPC client connects to a server, and when a callback handle receives its peer's capabilities. The fields it compares, the rules for unset metadata, and the error each path returns are in :doc:`Architecture reference <../reference/architecture>`, under Capability handshake.