linkcell 0.3.4

Periodic linked-cell k-nearest neighbour search for molecular simulations
Documentation
/* Generated by cbindgen from src/capi.rs. Do not edit. */

/*
 * linkcell C API.
 *
 * Memory ownership
 *   - lc_knearest writes into caller-owned out_nn (length n * k).
 *   - lc_knearest_d2 also writes out_d2 (length n * k, unused NaN).
 *   - lc_knearest_many is the frame-major batch (n_frames * n * k).
 *   - lc_last_error returns a thread-local pointer. Do not free. The
 *     pointer is invalid after the next lc_knearest on the same thread.
 *     lc_version does not read or write the slot.
 *   - lc_version returns a process-static pointer. Do not free.
 *
 * Thread safety
 *   - Distinct searches may run concurrently.
 *   - lc_last_error is a thread-local slot. Read it on the same thread
 *     that received a nonzero return from lc_knearest.
 */


#ifndef LINKCELL_H
#define LINKCELL_H

#pragma once

/* Generated with cbindgen:0.29.4 */

/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */

#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>

/**
 * Periodic parallelepiped. Lattice vectors are a, b, c (same as vesin rows).
 */
typedef struct lc_cell {
    /**
     * Lattice vector a, x.
     */
    double ax;
    /**
     * Lattice vector a, y.
     */
    double ay;
    /**
     * Lattice vector a, z.
     */
    double az;
    /**
     * Lattice vector b, x.
     */
    double bx;
    /**
     * Lattice vector b, y.
     */
    double by;
    /**
     * Lattice vector b, z.
     */
    double bz;
    /**
     * Lattice vector c, x.
     */
    double cx;
    /**
     * Lattice vector c, y.
     */
    double cy;
    /**
     * Lattice vector c, z.
     */
    double cz;
    /**
     * Origin x.
     */
    double ox;
    /**
     * Origin y.
     */
    double oy;
    /**
     * Origin z.
     */
    double oz;
} lc_cell;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

/**
 * k-nearest neighbours for `n` points.
 *
 * `xyz` is packed row-major `n` triples `(x, y, z)` (`n * 3` doubles).
 * `n` is the point count (`size_t`); `0` is an error.
 * `simbox` is the periodic cell (lattice vectors a, b, c and origin).
 * `k` is neighbours per source (`size_t`); `0` is an error.
 * `mask` of `NULL` includes every point; otherwise `n` ints, nonzero to
 * include that point as both source and candidate.
 * `cell_hint` is the target cell edge. Values `<= 0` select the default
 * (3.0 in the box units).
 * `out_nn` is caller-owned output. Neighbours of source `i` occupy
 * `out_nn[i*k + t]`, nearest first. Missing slots are `-1`. Length is
 * `n * k` ints.
 *
 * `xyz`, `simbox`, and `out_nn` must be non-null when `n > 0` and
 * `k > 0`. `mask` may be `NULL`.
 *
 * Returns `0` on success. Nonzero on failure. Read `lc_last_error()` on
 * the same thread. The last-error string is thread-local: it is not
 * shared across threads, is valid until the next `lc_*` call on this
 * thread, and must not be freed.
 *
 * # Safety
 *
 * `xyz` is aligned for `double` and readable for `n * 3` doubles.
 * `simbox` is aligned and points at one valid `lc_cell`.
 * `out_nn` is aligned for `int` and writable for `n * k` ints.
 * `mask`, if non-null, is aligned for `int` and readable for `n` ints.
 * `n * 3` and `n * k` fit in `size_t`.
 */
int lc_knearest(const double *xyz,
                size_t n,
                const struct lc_cell *simbox,
                size_t k,
                const int *mask,
                double cell_hint,
                int *out_nn);

/**
 * Like [`lc_knearest`], and write squared distances into `out_d2`.
 *
 * `out_d2` is caller-owned, length `n * k`. Unused slots are `NaN`.
 * `out_d2` may be `NULL` to skip distances (same as [`lc_knearest`]).
 *
 * # Safety
 *
 * Same pointer contract as [`lc_knearest`]. `out_d2`, if non-null,
 * is aligned for `double` and writable for `n * k` doubles.
 */
int lc_knearest_d2(const double *xyz,
                   size_t n,
                   const struct lc_cell *simbox,
                   size_t k,
                   const int *mask,
                   double cell_hint,
                   int *out_nn,
                   double *out_d2);

/**
 * Frame-major batch. `xyz` is `n_frames * n` packed triples.
 * `out_nn` / `out_d2` are `n_frames * n * k`. One shared cell.
 * `mask` is length `n` or `NULL`. `out_d2` may be `NULL`.
 *
 * # Safety
 *
 * `xyz` is readable for `n_frames * n * 3` doubles. `out_nn` is
 * writable for `n_frames * n * k` ints. `out_d2`, if non-null, is
 * writable for `n_frames * n * k` doubles. `simbox` and `mask`
 * follow [`lc_knearest`].
 */
int lc_knearest_many(const double *xyz,
                     size_t n,
                     size_t n_frames,
                     const struct lc_cell *simbox,
                     size_t k,
                     const int *mask,
                     double cell_hint,
                     int *out_nn,
                     double *out_d2);

/**
 * Thread-local last-error string from this thread's most recent
 * [`lc_knearest`].
 *
 * Returns a pointer to a NUL-terminated UTF-8 C string, or `NULL` if
 * the last `lc_knearest` on this thread succeeded, or if none has
 * failed yet. [`lc_version`] does not read or write the slot.
 * Distinct threads have independent slots. The pointer is valid until
 * the next `lc_knearest` on this thread. Do not free it.
 */
const char *lc_last_error(void);

/**
 * Library version string. Process-static, NUL-terminated. Do not free.
 */
const char *lc_version(void);

#ifdef __cplusplus
}  // extern "C"
#endif  // __cplusplus

#endif  /* LINKCELL_H */

#ifndef LINKCELL_LC_CELL_ORTHO_H
#define LINKCELL_LC_CELL_ORTHO_H

#ifdef __cplusplus
extern "C" {
#endif

/**
 * Orthorhombic cell with origin at zero.
 */
static inline lc_cell lc_cell_ortho(double lx, double ly, double lz) {
    lc_cell c;
    c.ax = lx;
    c.ay = 0.0;
    c.az = 0.0;
    c.bx = 0.0;
    c.by = ly;
    c.bz = 0.0;
    c.cx = 0.0;
    c.cy = 0.0;
    c.cz = lz;
    c.ox = 0.0;
    c.oy = 0.0;
    c.oz = 0.0;
    return c;
}

#ifdef __cplusplus
}  /* extern "C" */
#endif

#endif  /* LINKCELL_LC_CELL_ORTHO_H */