/*
 * Copyright 2025-2026 NVIDIA CORPORATION & AFFILIATES.  All rights reserved.
 *
 * NOTICE TO LICENSEE:
 *
 * This source code and/or documentation ("Licensed Deliverables") are
 * subject to NVIDIA intellectual property rights under U.S. and
 * international Copyright laws.
 *
 * These Licensed Deliverables contained herein is PROPRIETARY and
 * CONFIDENTIAL to NVIDIA and is being provided under the terms and
 * conditions of a form of NVIDIA software license agreement by and
 * between NVIDIA and Licensee ("License Agreement") or electronically
 * accepted by Licensee.  Notwithstanding any terms or conditions to
 * the contrary in the License Agreement, reproduction or disclosure
 * of the Licensed Deliverables to any third party without the express
 * written consent of NVIDIA is prohibited.
 *
 * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
 * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE
 * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE.  IT IS
 * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND.
 * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED
 * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY,
 * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
 * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
 * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY
 * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY
 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THESE LICENSED DELIVERABLES.
 *
 * U.S. Government End Users.  These Licensed Deliverables are a
 * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT
 * 1995), consisting of "commercial computer software" and "commercial
 * computer software documentation" as such terms are used in 48
 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government
 * only as a commercial end item.  Consistent with 48 C.F.R.12.212 and
 * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all
 * U.S. Government End Users acquire the Licensed Deliverables with
 * only those rights set forth herein.
 *
 * Any use of the Licensed Deliverables in individual and commercial
 * software must include, in the user documentation and internal
 * comments to the code, the above Disclaimer and U.S. Government End
 * Users Notice.
 */

#ifndef DENSITY_FITTED_EXCHANGE_COMPUTE_API_H
#define DENSITY_FITTED_EXCHANGE_COMPUTE_API_H

#include <stddef.h>
#include <stdint.h>

#include "../macros.h"
#include "../types/cuest_types.h"
#include "../types/cuest_parameter_types.h"
#include "../types/workspace_api.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Compute the AO symmetric exchange (K) matrix using density fitting (DF) with the supplied DF integral plan and coefficient matrix.
 * @ingroup Exchange
 *
 * - The input coefficient matrix must be preallocated to size (\c numOccupied × nao) (row-major).
 * - The output buffer \c outExchangeMatrix must be preallocated for (nao × nao) doubles.
 * - All handles, pointers, and buffers must be valid and non-NULL.
 * - Temporary workspace must be preallocated using size returned by \c cuestDFSymmetricExchangeComputeWorkspaceQuery.
 *
 * @param[in]  handle                      cuEST handle. Must not be NULL.
 * @param[in]  plan                        Density-fitted integral plan (opaque handle). Must not be NULL.
 * @param[in]  parameters                  Compute parameters (\c cuestDFSymmetricExchangeComputeParameters_t). Must not be NULL
 * @param[in]  variableBufferSize          User-provided descriptor for an internal scratch buffer (host and device). Must not be NULL.
 *                                             For most problem sizes, 8 GB is a good buffer size. On older devices, such as A100,
 *                                             smaller buffer sizes down to 2 GB can be used without degrading performance. Host memory 
 *                                             is currently unused. Note that the variableBufferSize does not limit total memory usage.
 *                                             There may be static allocations required outside of the scope of what is limited by the
 *                                             variableBufferSize. This value must remain unchanged between WorkspaceQuery and Compute calls.
 * @param[in]  temporaryWorkspace          Temporary workspace buffer for this operation. Must not be NULL.
 * @param[in]  numOccupied                 Number of occupied orbitals (rows in coefficient matrix); must be greater than zero.
 * @param[in]  coefficientMatrix           Input coefficient matrix (size: \c numOccupied × nao) on GPU. Must not be NULL.
 * @param[out] outExchangeMatrix           Output buffer for the AO exchange matrix (size: nao × nao) on GPU. Must not be NULL.
 *                                             Elements of this array are overwritten by the calculation results.
 *
 * @return 
 *   - \c CUEST_STATUS_SUCCESS on success;
 *   - \c CUEST_STATUS_INVALID_HANDLE if the cuEST handle is NULL;
 *   - \c CUEST_STATUS_NULL_POINTER if any required pointer is NULL;
 *   - \c CUEST_STATUS_INVALID_TYPE if opaque handles are not the correct type;
 *   - \c CUEST_STATUS_INVALID_PLAN if the \c cuestDFIntPlan_t is not correctly configured or in a dirty state;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if \c numOccupied is zero;
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 *
 * The exchange matrix is computed as:
 *     \f[ K_{\mu\nu} = \sum^{N_{ao}}_{\lambda\sigma} \sum^{N_{occ}}_{i}
 *                      \left[ f_{\rm HF} (\mu\lambda|\frac{1}{r}|\nu\sigma) + f_{\rm LRC} (\mu\lambda|\frac{{\rm erf}(\omega r)}{r}|\nu\sigma) \right]
 *                      C_{i\lambda} C_{i\sigma} \f]
 *
 * where \f$ f_{\rm HF} \f$ is the fraction of Hartree-Fock exchange to add  (\c CUEST_DFINTPLAN_PARAMETERS_EXCHANGE_FRACTION),
 * \f$ f_{\rm LRC} \f$ is the fraction of the long-range component of range-separated exchange to add (\c CUEST_DFINTPLAN_PARAMETERS_LRC_EXCHANGE_FRACTION),
 * and \f$ \omega \f$ is the range-separation parameter.
 *
 * When the operator definition leads to zero exchange, the exchange matrix is still calculated and scaled by zero. The user should
 * avoid calling this function in that case to avoid unnecessary compute.
 *
 */
CUEST_API
cuestStatus_t cuestDFSymmetricExchangeCompute(
    cuestHandle_t handle,
    const cuestDFIntPlan_t plan,
    const cuestDFSymmetricExchangeComputeParameters_t parameters,
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspace_t* temporaryWorkspace,
    uint64_t numOccupied,
    const double* coefficientMatrix,
    double* outExchangeMatrix);

/**
 * @brief Query the temporary workspace required (in bytes) for a DF exchange matrix calculation.
 * @ingroup Exchange
 *
 * This function returns (via output parameters) the host and device workspace buffer sizes needed to perform
 * a symmetric exchange computation, given the plan and matrix dimensions. The coefficient matrix and output
 * buffer may be NULL (only sizes are computed).
 *
 * @param[in]  handle                              cuEST handle. Must not be NULL.
 * @param[in]  plan                                Density-fitted integral plan (opaque handle). Must not be NULL.
 * @param[in]  parameters                          Compute parameters (\c cuestDFSymmetricExchangeComputeParameters_t). Must not be NULL
 * @param[in]  variableBufferSize                  User-provided descriptor for an internal scratch buffer (host and device). Must not be NULL.
 *                                                     For most problem sizes, 8 GB is a good buffer size. On older devices, such as A100,
 *                                                     smaller buffer sizes down to 2 GB can be used without degrading performance. Host memory 
 *                                                     is currently unused. Note that the variableBufferSize does not limit total memory usage.
 *                                                     There may be static allocations required outside of the scope of what is limited by the
 *                                                     variableBufferSize. This value must remain unchanged between WorkspaceQuery and Compute calls.
 * @param[out] temporaryWorkspaceDescriptor        Output descriptor for workspace sizes (host and device). Must not be NULL.
 * @param[in]  numOccupied                         Number of occupied orbitals (rows in coefficient matrix); must be greater than zero.
 * @param      coefficientMatrix                   Optional pointer to coefficient matrix buffer (may be NULL).
 * @param      outExchangeMatrix                   Optional pointer to output exchange matrix buffer (may be NULL).
 *
 * @return
 *   - \c CUEST_STATUS_SUCCESS on success;
 *   - \c CUEST_STATUS_INVALID_HANDLE if the cuEST handle is NULL;
 *   - \c CUEST_STATUS_NULL_POINTER if any required pointer is NULL;
 *   - \c CUEST_STATUS_INVALID_TYPE if opaque handles are not the correct type;
 *   - \c CUEST_STATUS_INVALID_PLAN if the \c cuestDFIntPlan_t is not correctly configured or in a dirty state;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if \c numOccupied is zero;
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestDFSymmetricExchangeComputeWorkspaceQuery(
    cuestHandle_t handle,
    const cuestDFIntPlan_t plan,
    const cuestDFSymmetricExchangeComputeParameters_t parameters,
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspaceDescriptor_t* temporaryWorkspaceDescriptor,
    uint64_t numOccupied,
    const double* coefficientMatrix,
    double* outExchangeMatrix);

/**
 * @brief Compute AO exchange (K) matrices with nonsymmetric densities using density fitting (DF) with the supplied DF integral plan and coefficient matrices.
 * @ingroup Exchange
 * @since v0.2.0
 *
 * - The input left coefficient matrix must be preallocated to size (\c numOccupied × nao) (row-major).
 * - The input right coefficient matrices must be preallocated to size (\c numCoefficientMatrices × numOccupied × nao) (row-major).
 * - The output buffer \c outExchangeMatrices must be preallocated for (\c numCoefficientMatrices × nao × nao) doubles.
 * - All handles, pointers, and buffers must be valid and non-NULL.
 * - Temporary workspace must be preallocated using size returned by \c cuestDFNonsymmetricExchangeComputeWorkspaceQuery.
 *
 * @param[in]  handle                      cuEST handle. Must not be NULL.
 * @param[in]  plan                        Density-fitted integral plan (opaque handle). Must not be NULL.
 * @param[in]  parameters                  Compute parameters (\c cuestDFNonsymmetricExchangeComputeParameters_t). Must not be NULL
 * @param[in]  variableBufferSize          User-provided descriptor for an internal scratch buffer (host and device). Must not be NULL.
 *                                             For most problem sizes, 8 GB is a good buffer size. On older devices, such as A100,
 *                                             smaller buffer sizes down to 2 GB can be used without degrading performance. Host memory 
 *                                             is currently unused. Note that the variableBufferSize does not limit total memory usage.
 *                                             There may be static allocations required outside of the scope of what is limited by the
 *                                             variableBufferSize. This value must remain unchanged between WorkspaceQuery and Compute calls.
 * @param[in]  temporaryWorkspace          Temporary workspace buffer for this operation. Must not be NULL.
 * @param[in]  numCoefficientMatrices      Number of right coefficient matrices provided and output exchange matrices; must be greater than zero.
 * @param[in]  numOccupied                 Number of occupied orbitals (rows in left and right coefficient matrices); must be greater than zero.
 * @param[in]  leftCoefficientMatrix       Input left coefficient matrix (size: \c numOccupied × nao) on GPU. Must not be NULL.
 * @param[in]  rightCoefficientMatrices    Input right coefficient matrices (size: \c numCoefficientMatrices × numOccupied × nao) on GPU. Must not be NULL.
 * @param[out] outExchangeMatrices         Output buffer for the AO exchange matrices (size: numCoefficientMatrices × nao × nao) on GPU. Must not be NULL.
 *                                             Elements of this array are overwritten by the calculation results.
 *
 * @return 
 *   - \c CUEST_STATUS_SUCCESS on success;
 *   - \c CUEST_STATUS_INVALID_HANDLE if the cuEST handle is NULL;
 *   - \c CUEST_STATUS_NULL_POINTER if any required pointer is NULL;
 *   - \c CUEST_STATUS_INVALID_TYPE if opaque handles are not the correct type;
 *   - \c CUEST_STATUS_INVALID_PLAN if the \c cuestDFIntPlan_t is not correctly configured or in a dirty state;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if \c numOccupied or \c numCoefficientMatrices is zero;
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 *
 * The exchange matrix is computed as:
 *     \f[ K^{n}_{\mu\nu} = \sum^{N_{ao}}_{\lambda\sigma} \sum^{N_{occ}}_{i} 
 *                      \left[ f_{\rm HF} (\mu\lambda|\frac{1}{r}|\nu\sigma) + f_{\rm LRC} (\mu\lambda|\frac{{\rm erf}(\omega r)}{r}|\nu\sigma) \right]
 *                      L_{i\lambda} R^{n}_{i\sigma} \f]
 *
 * where \f$ f_{\rm HF} \f$ is the fraction of Hartree-Fock exchange to add  (\c CUEST_DFINTPLAN_PARAMETERS_EXCHANGE_FRACTION),
 * \f$ f_{\rm LRC} \f$ is the fraction of the long-range component of range-separated exchange to add (\c CUEST_DFINTPLAN_PARAMETERS_LRC_EXCHANGE_FRACTION),
 * and \f$ \omega \f$ is the range-separation parameter.
 *
 * When the operator definition leads to zero exchange, the exchange matrix is still calculated and scaled by zero. The user should
 * avoid calling this function in that case to avoid unnecessary compute.
 */
CUEST_API
cuestStatus_t cuestDFNonsymmetricExchangeCompute(
    cuestHandle_t handle,
    const cuestDFIntPlan_t plan,
    const cuestDFNonsymmetricExchangeComputeParameters_t parameters,
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspace_t* temporaryWorkspace,
    uint64_t numCoefficientMatrices,
    uint64_t numOccupied,
    const double* leftCoefficientMatrix,
    const double* rightCoefficientMatrices,
    double* outExchangeMatrices);

/**
 * @brief Query the temporary workspace required (in bytes) for the calculation of nonsymmetric DF exchange matrices.
 * @ingroup Exchange
 * @since v0.2.0
 *
 * This function returns (via output parameters) the host and device workspace buffer sizes needed to perform
 * a nonsymmetric exchange computation, given the plan and matrix dimensions. The coefficient matrices and output
 * buffer may be NULL (only sizes are computed).
 *
 * @param[in]  handle                         cuEST handle. Must not be NULL.
 * @param[in]  plan                           Density-fitted integral plan (opaque handle). Must not be NULL.
 * @param[in]  parameters                     Compute parameters (\c cuestDFNonsymmetricExchangeComputeParameters_t). Must not be NULL
 * @param[in]  variableBufferSize             User-provided descriptor for an internal scratch buffer (host and device). Must not be NULL.
 *                                                For most problem sizes, 8 GB is a good buffer size. On older devices, such as A100,
 *                                                smaller buffer sizes down to 2 GB can be used without degrading performance. Host memory 
 *                                                is currently unused. Note that the variableBufferSize does not limit total memory usage.
 *                                                There may be static allocations required outside of the scope of what is limited by the
 *                                                variableBufferSize. This value must remain unchanged between WorkspaceQuery and Compute calls.
 * @param[out] temporaryWorkspaceDescriptor   Output descriptor for workspace sizes (host and device). Must not be NULL.
 * @param[in]  numCoefficientMatrices         Number of right coefficient matrices provided and output exchange matrices; must be greater than zero.
 * @param[in]  numOccupied                    Number of occupied orbitals (rows in left and right coefficient matrices); must be greater than zero.
 * @param      leftCoefficientMatrix          Input left coefficient matrix (size: \c numOccupied × nao) on GPU. May be NULL.
 * @param      rightCoefficientMatrices       Input right coefficient matrices (size: \c numCoefficientMatrices × numOccupied × nao) on GPU. May be NULL.
 * @param      outExchangeMatrices            Output buffer for the AO exchange matrices (size: numCoefficientMatrices × nao × nao) on GPU. May be NULL.
 *
 * @return 
 *   - \c CUEST_STATUS_SUCCESS on success;
 *   - \c CUEST_STATUS_INVALID_HANDLE if the cuEST handle is NULL;
 *   - \c CUEST_STATUS_NULL_POINTER if any required pointer is NULL;
 *   - \c CUEST_STATUS_INVALID_TYPE if opaque handles are not the correct type;
 *   - \c CUEST_STATUS_INVALID_PLAN if the \c cuestDFIntPlan_t is not correctly configured or in a dirty state;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if \c numOccupied or \c numCoefficientMatrices is zero;
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestDFNonsymmetricExchangeComputeWorkspaceQuery(
    cuestHandle_t handle,
    const cuestDFIntPlan_t plan,
    const cuestDFNonsymmetricExchangeComputeParameters_t parameters,
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspaceDescriptor_t* temporaryWorkspaceDescriptor,
    uint64_t numCoefficientMatrices,
    uint64_t numOccupied,
    const double* leftCoefficientMatrix,
    const double* rightCoefficientMatrices,
    double* outExchangeMatrices);

#ifdef __cplusplus
}
#endif

#endif /* DENSITY_FITTED_EXCHANGE_COMPUTE_API_H */
