=========== ``mod hvp`` =========== .. rust:module:: rgpot_core::c_api::hvp :index: 0 :vis: pub C API for Hessian-vector products of a potential handle. .. code-block:: c // A Lanczos loop at one centre: k products, k + 1 force calls. rgpot_force_out_t centre = rgpot_force_out_create(); rgpot_potential_hvp_centre(pot, &input, ¢re); // 1 force call for (int i = 0; i < k; ++i) { rgpot_potential_hvp(pot, &input, q[i], 0.0, RGPOT_HVP_AUTO, hq); // 1 each // ... Lanczos recurrence on hq ... } rgpot_tensor_free(centre.forces); See ``crate::hvp`` for the schemes and the centre cache. .. rust:use:: rgpot_core::c_api::hvp :used_name: self .. rust:use:: rgpot_core :used_name: crate .. rust:use:: std::os::raw::c_void :used_name: c_void .. rust:use:: rgpot_core::eindir::rgpot_potential_t :used_name: rgpot_potential_t .. rust:use:: rgpot_core::hvp::HessianVectorCallback :used_name: HessianVectorCallback .. rust:use:: rgpot_core::hvp::HvpError :used_name: HvpError .. rust:use:: rgpot_core::hvp::rgpot_hvp_counts_t :used_name: rgpot_hvp_counts_t .. rust:use:: rgpot_core::hvp::rgpot_hvp_scheme_t :used_name: rgpot_hvp_scheme_t .. rust:use:: rgpot_core::status::catch_unwind :used_name: catch_unwind .. rust:use:: rgpot_core::status::rgpot_status_t :used_name: rgpot_status_t .. rust:use:: rgpot_core::status::set_last_error :used_name: set_last_error .. rust:use:: rgpot_core::tensor::create_owned_f64_tensor :used_name: create_owned_f64_tensor .. rust:use:: rgpot_core::types::rgpot_force_input_t :used_name: rgpot_force_input_t .. rust:use:: rgpot_core::types::rgpot_force_out_t :used_name: rgpot_force_out_t .. rubric:: Functions .. rust:function:: rgpot_core::c_api::hvp::rgpot_potential_hvp :index: 0 :vis: pub :layout: [{"type":"keyword","value":"unsafe"},{"type":"space"},{"type":"keyword","value":"extern"},{"type":"space"},{"type":"literal","value":"C"},{"type":"space"},{"type":"keyword","value":"fn"},{"type":"space"},{"type":"name","value":"rgpot_potential_hvp"},{"type":"punctuation","value":"("},{"type":"name","value":"pot"},{"type":"punctuation","value":": "},{"type":"operator","value":"*"},{"type":"keyword","value":"const"},{"type":"space"},{"type":"link","value":"rgpot_potential_t","target":"rgpot_potential_t"},{"type":"punctuation","value":", "},{"type":"name","value":"input"},{"type":"punctuation","value":": "},{"type":"operator","value":"*"},{"type":"keyword","value":"const"},{"type":"space"},{"type":"link","value":"rgpot_force_input_t","target":"rgpot_force_input_t"},{"type":"punctuation","value":", "},{"type":"name","value":"direction"},{"type":"punctuation","value":": "},{"type":"operator","value":"*"},{"type":"keyword","value":"const"},{"type":"space"},{"type":"link","value":"f64","target":"f64"},{"type":"punctuation","value":", "},{"type":"name","value":"step"},{"type":"punctuation","value":": "},{"type":"link","value":"f64","target":"f64"},{"type":"punctuation","value":", "},{"type":"name","value":"scheme"},{"type":"punctuation","value":": "},{"type":"link","value":"i32","target":"i32"},{"type":"punctuation","value":", "},{"type":"name","value":"hv_out"},{"type":"punctuation","value":": "},{"type":"operator","value":"*"},{"type":"keyword","value":"mut"},{"type":"space"},{"type":"link","value":"f64","target":"f64"},{"type":"punctuation","value":")"},{"type":"space"},{"type":"returns"},{"type":"space"},{"type":"link","value":"rgpot_status_t","target":"rgpot_status_t"}] Write the Hessian-vector product ``H direction`` at the geometry of ``input`` to ``hv_out``. ``H`` is the Hessian of the energy with respect to the ``3 * n_atoms`` positions. ``direction`` and ``hv_out`` hold ``3 * n_atoms`` doubles laid out like the positions and must not overlap. ``scheme`` is a ``rgpot_hvp_scheme_t`` value: - ``RGPOT_HVP_AUTO``: the analytic callback when one is registered, otherwise ``RGPOT_HVP_FORWARD``. - ``RGPOT_HVP_FORWARD``: ``(F(x) - F(x + h u)) |v| / h`` with ``u = v / |v|``. ``F(x)`` comes from the handle's cached centre when the positions, atomic numbers and cell carry the same bits as the last centre, so ``k`` products at one geometry make ``k + 1`` force calls. Error ``O(h)``. - ``RGPOT_HVP_CENTRAL``: ``(F(x - h u) - F(x + h u)) |v| / (2 h)``, two force calls, error ``O(h^2)``. - ``RGPOT_HVP_ANALYTIC``: the registered callback; ``RGPOT_INVALID_PARAMETER`` when none is registered. ``step`` is ``h``, a length in the units of the positions; 0 selects ``RGPOT_HVP_DEFAULT_STEP``. A zero direction writes zeros without a call. A failing force callback's status is returned with its own ``rgpot_last_error`` message. **Safety** NULL pointers, an unknown scheme, a non-positive step and overlapping buffers return ``RGPOT_INVALID_PARAMETER``. Otherwise ``pot`` must be a live handle, ``input`` must point to a valid ``rgpot_force_input_t`` whose tensors stay alive for the call, and ``direction`` and ``hv_out`` must point to ``3 * n_atoms`` readable and writable doubles. .. rust:function:: rgpot_core::c_api::hvp::rgpot_potential_hvp_centre :index: 0 :vis: pub :layout: [{"type":"keyword","value":"unsafe"},{"type":"space"},{"type":"keyword","value":"extern"},{"type":"space"},{"type":"literal","value":"C"},{"type":"space"},{"type":"keyword","value":"fn"},{"type":"space"},{"type":"name","value":"rgpot_potential_hvp_centre"},{"type":"punctuation","value":"("},{"type":"name","value":"pot"},{"type":"punctuation","value":": "},{"type":"operator","value":"*"},{"type":"keyword","value":"const"},{"type":"space"},{"type":"link","value":"rgpot_potential_t","target":"rgpot_potential_t"},{"type":"punctuation","value":", "},{"type":"name","value":"input"},{"type":"punctuation","value":": "},{"type":"operator","value":"*"},{"type":"keyword","value":"const"},{"type":"space"},{"type":"link","value":"rgpot_force_input_t","target":"rgpot_force_input_t"},{"type":"punctuation","value":", "},{"type":"name","value":"output"},{"type":"punctuation","value":": "},{"type":"operator","value":"*"},{"type":"keyword","value":"mut"},{"type":"space"},{"type":"link","value":"rgpot_force_out_t","target":"rgpot_force_out_t"},{"type":"punctuation","value":")"},{"type":"space"},{"type":"returns"},{"type":"space"},{"type":"link","value":"rgpot_status_t","target":"rgpot_status_t"}] Energy and forces at the geometry of ``input``, from the cached centre of ``pot`` when it matches, otherwise from one force call that becomes the cached centre. `output->forces`` is set to a new owning ``[n_atoms, 3]` tensor; free it with ``rgpot_tensor_free``. A search that needs the centre forces calls this instead of ``rgpot_potential_calculate``, so the forward products at that centre share the evaluation. **Safety** A NULL ``pot``, ``input`` or ``output`` returns ``RGPOT_INVALID_PARAMETER``. Otherwise the pointers follow ``rgpot_potential_calculate``. .. rust:function:: rgpot_core::c_api::hvp::rgpot_potential_hvp_counts :index: 0 :vis: pub :layout: [{"type":"keyword","value":"unsafe"},{"type":"space"},{"type":"keyword","value":"extern"},{"type":"space"},{"type":"literal","value":"C"},{"type":"space"},{"type":"keyword","value":"fn"},{"type":"space"},{"type":"name","value":"rgpot_potential_hvp_counts"},{"type":"punctuation","value":"("},{"type":"name","value":"pot"},{"type":"punctuation","value":": "},{"type":"operator","value":"*"},{"type":"keyword","value":"const"},{"type":"space"},{"type":"link","value":"rgpot_potential_t","target":"rgpot_potential_t"},{"type":"punctuation","value":", "},{"type":"name","value":"out"},{"type":"punctuation","value":": "},{"type":"operator","value":"*"},{"type":"keyword","value":"mut"},{"type":"space"},{"type":"link","value":"rgpot_hvp_counts_t","target":"rgpot_hvp_counts_t"},{"type":"punctuation","value":")"},{"type":"space"},{"type":"returns"},{"type":"space"},{"type":"link","value":"rgpot_status_t","target":"rgpot_status_t"}] Read the Hessian-vector counters of ``pot`` into ``out``. **Safety** ``pot`` must be NULL or a live handle, and ``out`` NULL or writable. NULL for either returns ``RGPOT_INVALID_PARAMETER``. .. rust:function:: rgpot_core::c_api::hvp::rgpot_potential_hvp_invalidate :index: 0 :vis: pub :layout: [{"type":"keyword","value":"unsafe"},{"type":"space"},{"type":"keyword","value":"extern"},{"type":"space"},{"type":"literal","value":"C"},{"type":"space"},{"type":"keyword","value":"fn"},{"type":"space"},{"type":"name","value":"rgpot_potential_hvp_invalidate"},{"type":"punctuation","value":"("},{"type":"name","value":"pot"},{"type":"punctuation","value":": "},{"type":"operator","value":"*"},{"type":"keyword","value":"const"},{"type":"space"},{"type":"link","value":"rgpot_potential_t","target":"rgpot_potential_t"},{"type":"punctuation","value":")"}] Drop the cached centre of ``pot``. A centre is keyed on positions, atomic numbers and cell, not on the state behind the callback's ``user_data``. Call this after changing that state at a fixed geometry. NULL is a no-op. **Safety** ``pot`` must be NULL or a live handle. .. rust:function:: rgpot_core::c_api::hvp::rgpot_potential_reset_hvp_counts :index: 0 :vis: pub :layout: [{"type":"keyword","value":"unsafe"},{"type":"space"},{"type":"keyword","value":"extern"},{"type":"space"},{"type":"literal","value":"C"},{"type":"space"},{"type":"keyword","value":"fn"},{"type":"space"},{"type":"name","value":"rgpot_potential_reset_hvp_counts"},{"type":"punctuation","value":"("},{"type":"name","value":"pot"},{"type":"punctuation","value":": "},{"type":"operator","value":"*"},{"type":"keyword","value":"const"},{"type":"space"},{"type":"link","value":"rgpot_potential_t","target":"rgpot_potential_t"},{"type":"punctuation","value":")"}] Zero the Hessian-vector counters of ``pot``. The cached centre stays. **Safety** ``pot`` must be NULL or a live handle. NULL is a no-op. .. rust:function:: rgpot_core::c_api::hvp::rgpot_potential_set_hvp_callback :index: 0 :vis: pub :layout: [{"type":"keyword","value":"unsafe"},{"type":"space"},{"type":"keyword","value":"extern"},{"type":"space"},{"type":"literal","value":"C"},{"type":"space"},{"type":"keyword","value":"fn"},{"type":"space"},{"type":"name","value":"rgpot_potential_set_hvp_callback"},{"type":"punctuation","value":"("},{"type":"name","value":"pot"},{"type":"punctuation","value":": "},{"type":"operator","value":"*"},{"type":"keyword","value":"const"},{"type":"space"},{"type":"link","value":"rgpot_potential_t","target":"rgpot_potential_t"},{"type":"punctuation","value":", "},{"type":"name","value":"callback"},{"type":"punctuation","value":": "},{"type":"link","value":"Option","target":"Option"},{"type":"punctuation","value":"<"},{"type":"keyword","value":"unsafe"},{"type":"space"},{"type":"keyword","value":"extern"},{"type":"space"},{"type":"literal","value":"C"},{"type":"space"},{"type":"keyword","value":"fn"},{"type":"space"},{"type":"punctuation","value":"("},{"type":"name","value":"user_data"},{"type":"punctuation","value":":"},{"type":"operator","value":"*"},{"type":"keyword","value":"mut"},{"type":"space"},{"type":"link","value":"c_void","target":"c_void"},{"type":"punctuation","value":", "},{"type":"name","value":"input"},{"type":"punctuation","value":":"},{"type":"operator","value":"*"},{"type":"keyword","value":"const"},{"type":"space"},{"type":"link","value":"rgpot_force_input_t","target":"rgpot_force_input_t"},{"type":"punctuation","value":", "},{"type":"name","value":"direction"},{"type":"punctuation","value":":"},{"type":"operator","value":"*"},{"type":"keyword","value":"const"},{"type":"space"},{"type":"link","value":"f64","target":"f64"},{"type":"punctuation","value":", "},{"type":"name","value":"hv_out"},{"type":"punctuation","value":":"},{"type":"operator","value":"*"},{"type":"keyword","value":"mut"},{"type":"space"},{"type":"link","value":"f64","target":"f64"},{"type":"punctuation","value":")"},{"type":"space"},{"type":"returns"},{"type":"space"},{"type":"link","value":"rgpot_status_t","target":"rgpot_status_t"},{"type":"punctuation","value":">"},{"type":"punctuation","value":")"},{"type":"space"},{"type":"returns"},{"type":"space"},{"type":"link","value":"rgpot_status_t","target":"rgpot_status_t"}] Register ``callback`` as the analytic Hessian-vector product of ``pot``, or clear it with NULL. The callback receives the ``user_data`` the handle was created with, so a potential object supplies its forces and its Hessian through one pointer. With a callback registered, ``RGPOT_HVP_AUTO`` products call it and make no force call. **Safety** ``pot`` must be NULL or a live handle. A NULL ``pot`` returns ``RGPOT_INVALID_PARAMETER``. ``callback`` must be safe to call with the handle's ``user_data`` for as long as it stays registered.