/*
 * 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 NONLOCAL_XC_POTENTIAL_COMPUTE_API_H
#define NONLOCAL_XC_POTENTIAL_COMPUTE_API_H

#include <stddef.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 nonlocal RKS XC potential (Vxc) matrix using the supplied XC integral plan and occupied molecular orbitals.
 * @ingroup NonLocalCorrelation
 *
 * This routine computes the XC potential matrix (Vxc) from the occupied molecular orbitals.
 * This is the RKS interface that assumes the alpha and beta spin densities are identical. The user must 
 * provide preallocated occupied molecular orbitals (size: nocc x nao)
 * and a preallocated output buffer (size: nao × nao). The orbital basis and integration grid used are passed
 * via the \c plan . All required temporary workspace must be allocated using the size returned by 
 * \c cuestNonlocalXCPotentialRKSComputeWorkspaceQuery.
 *
 * \b Precision selection. This routine can use reduced-precision "ffloat" compute, selected through the
 * compute parameters:
 *   - \c CUEST_NONLOCALXCPOTENTIALRKSCOMPUTE_PARAMETERS_FFLOAT_USAGE_MODE (\c cuestFfloatUsageMode_t):
 *     \c CUEST_FFLOAT_USAGE_MODE_ON requests ffloat compute, \c CUEST_FFLOAT_USAGE_MODE_OFF requests fp64
 *     compute, and \c CUEST_FFLOAT_USAGE_MODE_DEFAULT requests ffloat compute when it is available and
 *     expected to provide a performance advantage on the present GPU architecture.
 *
 * @param[in]  handle                      cuEST handle. Must not be NULL.
 * @param[in]  plan                        XC integral plan (opaque handle) constructed with \c cuestXCIntPlanCreate. Must not be NULL.
 *                                         The \c cuestXCIntPlanParametersFunctional_t set during plan construction does not affect this compute routine.
 * @param[in]  parameters                  \c cuestNonlocalXCPotentialRKSComputeParameters_t defines the details of the nonlocal XC functional.
 * @param[in]  variableBufferSize          User-provided descriptor for the variable buffer size (host and device). Must not be NULL.
 *                                             2 GB is a good default value for device memory. There are diminishing returns for very
 *                                             large values of the variableBufferSize. Host memory is currently unused.
 *                                             This value MUST remain unchanged between the WorkspaceQuery call and the Compute call.
 * @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] outXCEnergy                 Output (double) for the computed XC energy (size: 1) on CPU. Must not be NULL.
 * @param[out] outXCPotentialMatrix        Output buffer for computed XC potential 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_ARGUMENT if \c numOccupied is zero, or if a parameter value is invalid;
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestNonlocalXCPotentialRKSCompute(
    cuestHandle_t handle,
    const cuestXCIntPlan_t plan,
    const cuestNonlocalXCPotentialRKSComputeParameters_t parameters, 
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspace_t* temporaryWorkspace,
    uint64_t numOccupied,
    const double* coefficientMatrix,
    double* outXCEnergy,
    double* outXCPotentialMatrix);

/**
 * @brief Query the temporary workspace required (in bytes) for a nonlocal XC potential matrix computation.
 * @ingroup NonLocalCorrelation
 *
 * This function returns the workspace needed (host/device) for a single computation,
 * based on the provided plan and pair list. Only sizes are computed; output and orbital 
 * buffers may be NULL.
 *
 * @param[in]  handle                       cuEST handle. Must not be NULL.
 * @param[in]  plan                         XC integral plan (opaque handle) constructed with \c cuestXCIntPlanCreate. Must not be NULL.
 * @param[in]  parameters                   \c cuestNonlocalXCPotentialRKSComputeParameters_t defines the details of the nonlocal XC functional.
 * @param[in]  variableBufferSize           User-provided descriptor for the variable buffer size (host and device). Must not be NULL.
 *                                              2 GB is a good default value for device memory. There are diminishing returns for very
 *                                              large values of the variableBufferSize. Host memory is currently unused.
 *                                              This value MUST remain unchanged between the WorkspaceQuery call and the Compute call.
 * @param[out] temporaryWorkspaceDescriptor Output descriptor for temporary workspace sizes. Must not be NULL.
 * @param[in]  numOccupied                  Number of occupied orbitals (rows in coefficient matrix); must be greater than zero.
 * @param      coefficientMatrix            Input coefficient matrix. May be NULL.
 * @param      outXCEnergy                  Output (double) for the computed XC energy. May be NULL.
 * @param      outXCPotentialMatrix         Output buffer for computed XC potential matrix. 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_ARGUMENT if \c numOccupied is zero, or if a parameter value is invalid;
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestNonlocalXCPotentialRKSComputeWorkspaceQuery(
    cuestHandle_t handle,
    const cuestXCIntPlan_t plan,
    const cuestNonlocalXCPotentialRKSComputeParameters_t parameters, 
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspaceDescriptor_t* temporaryWorkspaceDescriptor,
    uint64_t numOccupied,
    const double* coefficientMatrix,
    double* outXCEnergy,
    double* outXCPotentialMatrix);

/**
 * @brief Compute the nonlocal UKS XC potential (Vxc) matrix using the supplied XC integral plan and occupied molecular orbitals.
 * @ingroup NonLocalCorrelation
 *
 * This routine computes the UKS XC potential matrix (Vxc) from occupied molecular orbitals.
 * This is the UKS interface that allows the alpha and beta spin densities to be different. The user must 
 * provide preallocated occupied molecular orbitals (size: noccA x nao and noccB x nao)
 * and preallocated output buffer (size: nao × nao). The orbital basis and integration grid used are passed
 * via the \c plan. All required temporary workspace must be allocated using the size returned by 
 * \c cuestNonlocalXCPotentialUKSComputeWorkspaceQuery.
 *
 * Alpha and beta coefficient matrices must always be provided with non-zero numbers of occupied orbitals. 
 * Cases without beta (or alpha) electrons (hydrogen atom, for example), can be handled by passing in a 
 * coefficient matrix containing only zeros.
 *
 * \b Precision selection. This routine can use reduced-precision "ffloat" compute, selected through the
 * compute parameters:
 *   - \c CUEST_NONLOCALXCPOTENTIALUKSCOMPUTE_PARAMETERS_FFLOAT_USAGE_MODE (\c cuestFfloatUsageMode_t):
 *     \c CUEST_FFLOAT_USAGE_MODE_ON requests ffloat compute, \c CUEST_FFLOAT_USAGE_MODE_OFF requests fp64
 *     compute, and \c CUEST_FFLOAT_USAGE_MODE_DEFAULT requests ffloat compute when it is available and
 *     expected to provide a performance advantage on the present GPU architecture.
 *
 * @param[in]  handle                     cuEST handle. Must not be NULL.
 * @param[in]  plan                       XC integral plan (opaque handle) constructed with \c cuestXCIntPlanCreate. Must not be NULL.
 *                                            The \c cuestXCIntPlanParametersFunctional_t set during plan construction does not affect this compute routine.
 * @param[in]  parameters                 \c cuestNonlocalXCPotentialUKSComputeParameters_t defines the details of the nonlocal XC functional.
 * @param[in]  variableBufferSize         User-provided descriptor for the variable buffer size (host and device). Must not be NULL.
 *                                            2 GB is a good default value for device memory. There are diminishing returns for very
 *                                            large values of the variableBufferSize. Host memory is currently unused.
 *                                            This value MUST remain unchanged between the WorkspaceQuery call and the Compute call.
 * @param[in]  temporaryWorkspace         Temporary workspace buffer for this operation. Must not be NULL.
 * @param[in]  numOccupiedAlpha           Number of alpha occupied orbitals (rows in alpha coefficient matrix); must be greater than zero.
 * @param[in]  numOccupiedBeta            Number of beta occupied orbitals (rows in beta coefficient matrix); must be greater than zero.
 * @param[in]  coefficientMatrixAlpha     Input alpha coefficient matrix (size: \c numOccupiedAlpha × nao) on GPU. Must not be NULL.
 * @param[in]  coefficientMatrixBeta      Input beta coefficient matrix (size: \c numOccupiedBeta × nao) on GPU. Must not be NULL.
 * @param[out] outXCEnergy                Output (double) for the computed XC energy (size: 1) on CPU. Must not be NULL.
 * @param[out] outXCPotentialMatrix       Output buffer for computed XC potential 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_ARGUMENT if \c numOccupiedAlpha or \c numOccupiedBeta is zero, or if a parameter value is invalid;
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestNonlocalXCPotentialUKSCompute(
    cuestHandle_t handle,
    const cuestXCIntPlan_t plan,
    const cuestNonlocalXCPotentialUKSComputeParameters_t parameters, 
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspace_t* temporaryWorkspace,
    uint64_t numOccupiedAlpha,
    uint64_t numOccupiedBeta,
    const double* coefficientMatrixAlpha,
    const double* coefficientMatrixBeta,
    double* outXCEnergy,
    double* outXCPotentialMatrix);

/**
 * @brief Query the temporary workspace required (in bytes) for a nonlocal XC potential matrix computation.
 * @ingroup NonLocalCorrelation
 *
 * This function returns the workspace needed (host/device) for a single computation,
 * based on the provided plan and pair list. Only sizes are computed; output and orbital 
 * buffers may be NULL.
 *
 * @param[in]  handle                       cuEST handle. Must not be NULL.
 * @param[in]  plan                         XC integral plan (opaque handle) constructed with \c cuestXCIntPlanCreate. Must not be NULL.
 * @param[in]  parameters                   \c cuestNonlocalXCPotentialUKSComputeParameters_t defines the details of the nonlocal XC functional.
 * @param[in]  variableBufferSize           User-provided descriptor for the variable buffer size (host and device). Must not be NULL.
 *                                              2 GB is a good default value for device memory. There are diminishing returns for very
 *                                              large values of the variableBufferSize. Host memory is currently unused.
 *                                              This value MUST remain unchanged between the WorkspaceQuery call and the Compute call.
 * @param[out] temporaryWorkspaceDescriptor Output descriptor for temporary workspace sizes. Must not be NULL.
 * @param[in]  numOccupiedAlpha             Number of alpha occupied orbitals (rows in coefficient matrix); must be greater than zero.
 * @param[in]  numOccupiedBeta              Number of beta occupied orbitals (rows in coefficient matrix); must be greater than zero.
 * @param      coefficientMatrixAlpha       Input alpha coefficient matrix. May be NULL.
 * @param      coefficientMatrixBeta        Input beta coefficient matrix. May be NULL.
 * @param      outXCEnergy                  Output (double) for the computed XC energy. May be NULL.
 * @param      outXCPotentialMatrix         Output buffer for computed XC potential matrix. 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_ARGUMENT if \c numOccupiedAlpha or \c numOccupiedBeta is zero, or if a parameter value is invalid;
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestNonlocalXCPotentialUKSComputeWorkspaceQuery(
    cuestHandle_t handle,
    const cuestXCIntPlan_t plan,
    const cuestNonlocalXCPotentialUKSComputeParameters_t parameters, 
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspaceDescriptor_t* temporaryWorkspaceDescriptor,
    uint64_t numOccupiedAlpha,
    uint64_t numOccupiedBeta,
    const double* coefficientMatrixAlpha,
    const double* coefficientMatrixBeta,
    double* outXCEnergy,
    double* outXCPotentialMatrix);


#ifdef __cplusplus
}
#endif

#endif /* NONLOCAL_XC_POTENTIAL_COMPUTE_API_H */
