C API
=====

Include ``<skalaxc/skalaxc.h>``. The C ABI uses opaque handles and plain data;
no C++, GauXC, or LibTorch types cross it. A C-only consumer can use:

.. code-block:: cmake

   project(my_solver LANGUAGES C)
   find_package(SkalaXC REQUIRED CONFIG COMPONENTS C)
   add_executable(my_solver main.c)
   target_link_libraries(my_solver PRIVATE SkalaXC::skalaxc)

If SkalaXC is installed outside a default prefix, add it to
``CMAKE_PREFIX_PATH`` or set ``SkalaXC_DIR``. The ``*_from_hdf5`` functions and
the :doc:`c` example exist only when SkalaXC is built with HDF5 support.

The pipeline is runtime, molecule/basis, grid, load balancer, molecular
weights, functional, and integrator. Check each status against
``SKALAXC_SUCCESS`` and report ``skalaxc_last_error_message()`` before cleanup.
Initialize handles to ``NULL`` and destroy each owned handle exactly once,
in reverse construction order. The :doc:`c` demonstrates cleanup on every
failure path.

Atomic numbers, shell angular momentum and ``pure`` flags are checked when atoms
and shells are added. Coordinates, exponents, coefficients and shell centers are
checked when a later stage consumes them (for example
``skalaxc_molgrid_create_default`` or ``skalaxc_load_balancer_create``), and the
selected backend's own angular momentum limit applies when the pipeline is
built. Diagnostics snapshots must be prepared with
``skalaxc_diagnostics_snapshot_init``, which records the struct size so the
library and the caller may be built against different header versions.
Status values and every enumeration-valued argument are fixed-width
``int32_t``, so they do not depend on compiler options such as
``-fshort-enums``. Likewise ``skalaxc_functional_create`` does not load the model; a
missing or misspelled model name, which is case-sensitive, is reported by
``skalaxc_xc_integrator_create``. Named models are looked up in
``SKALAXC_MODEL_PATH`` or, if unset, in the model directory recorded when the
library was built, so set ``SKALAXC_MODEL_PATH`` if the installation is moved.

Evaluation accepts caller-owned ``double`` arrays in column-major order,
with ``nbf * nbf`` elements per density or potential channel. Nuclear
gradients need ``3 * natoms`` elements in atom-major xyz order. Inputs are
the scalar and z-spin density channels, not separate alpha/beta matrices.
The supported calculation is UKS; RKS is not implemented.

MPI builds take the caller's native communicator and require collective
evaluation over that communicator. Keep it alive until dependent handles
are destroyed, and destroy those handles before finalizing MPI. A NULL
matrix or gradient pointer on any rank makes every rank return an error, but a
NULL integrator handle or ``exc_out`` is rejected before the collective, so on
only some ranks it leaves the others waiting.

Status and error reporting
------------------------------------------------------------

.. literalinclude:: ../../SkalaXC/include/skalaxc/c/status.h
   :language: c

Evaluation and diagnostics
------------------------------------------------------------

.. literalinclude:: ../../SkalaXC/include/skalaxc/c/integrator.h
   :language: c

.. literalinclude:: ../../SkalaXC/include/skalaxc/c/diagnostics.h
   :language: c

The umbrella header lists the remaining construction and configuration APIs:

.. literalinclude:: ../../SkalaXC/include/skalaxc/skalaxc.h
   :language: c
