/*
 * 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 XC_POTENTIAL_COMPUTE_API_H
#define 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 RKS XC potential (Vxc) matrix using the supplied XC integral plan and occupied molecular orbitals.
 * @ingroup ExchangeCorrelation
 *
 * This routine computes the XC potential matrix (Vxc) from occupied molecular orbitals.
 * This is the RKS interface that assumes the alpha and beta spin orbitals are identical. The user must 
 * provide 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 cuestXCPotentialRKSComputeWorkspaceQuery.
 *
 * @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 cuestXCIntPlanParametersFunctional_t set during plan construction determines what XC potential is evaluated.
 * @param[in]  parameters                  Compute parameters (\c cuestXCPotentialRKSComputeParameters_t) Must not be NULL
 * @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. Some static allocations are made outside of what is
 *                                             controlled by 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 the GPU. Must not be NULL.
 * @param[out] outXCEnergy                 Output (double) for the computed XC energy (size: 1) on the CPU. Must not be NULL.
 *                                             This element is overwritten by the calculation results.
 * @param[out] outXCPotentialMatrix        Output buffer for computed XC potential matrix (size: nao × nao) on the 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;
 *   - \c CUEST_STATUS_EXCEPTION if \c cuestXCIntPlanParametersFunctional_t is \c CUEST_XCINTPLAN_PARAMETERS_FUNCTIONAL_HF (set in the \c cuestXCIntPlan_t);
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestXCPotentialRKSCompute(
    cuestHandle_t handle,
    const cuestXCIntPlan_t plan,
    const cuestXCPotentialRKSComputeParameters_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 an XC potential matrix computation.
 * @ingroup ExchangeCorrelation
 *
 * 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                   Compute parameters (\c cuestXCPotentialRKSComputeParameters_t) Must not be NULL
 * @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. Some static allocations are made outside of what is
 *                                              controlled by 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;
 *   - \c CUEST_STATUS_EXCEPTION if \c cuestXCIntPlanParametersFunctional_t is \c CUEST_XCINTPLAN_PARAMETERS_FUNCTIONAL_HF (set in the \c cuestXCIntPlan_t);
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestXCPotentialRKSComputeWorkspaceQuery(
    cuestHandle_t handle,
    const cuestXCIntPlan_t plan,
    const cuestXCPotentialRKSComputeParameters_t parameters,
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspaceDescriptor_t* temporaryWorkspaceDescriptor,
    uint64_t numOccupied,
    const double* coefficientMatrix,
    double* outXCEnergy,
    double* outXCPotentialMatrix);

/**
 * @brief Compute the UKS XC potential (Vxc) matrix using the supplied XC integral plan and occupied molecular orbitals.
 * @ingroup ExchangeCorrelation
 *
 * This routine computes the UKS XC potential matrices (Vxc) from occupied molecular orbitals.
 * This is the UKS interface that allows the alpha and beta spin orbitals to be different. The user must 
 * provide occupied molecular orbitals (size nocc x nao) and preallocated output buffers (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 cuestXCPotentialUKSComputeWorkspaceQuery.
 *
 * 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.
 *
 * @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 determines what XC potential is evaluated.
 * @param[in]  parameters                      Compute parameters (\c cuestXCPotentialUKSComputeParameters_t) Must not be NULL
 * @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. Some static allocations are made outside of what is
 *                                                 controlled by 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). Must not be NULL.
 * @param[in]  coefficientMatrixBeta           Input beta coefficient matrix (size: \c numOccupiedBeta × nao). Must not be NULL.
 * @param[out] outXCEnergy                     Output (double) for the computed XC energy (size: 1) on the CPU. Must not be NULL.
 * @param[out] outXCPotentialMatrixAlpha       Output buffer for computed XC potential matrix (size: nao × nao) on the GPU. Must not be NULL.
 *                                                 Elements of this array are overwritten by the calculation results.
 * @param[out] outXCPotentialMatrixBeta        Output buffer for computed XC potential matrix (size: nao × nao) on the 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;
 *   - \c CUEST_STATUS_EXCEPTION if \c cuestXCIntPlanParametersFunctional_t is \c CUEST_XCINTPLAN_PARAMETERS_FUNCTIONAL_HF (set in the \c cuestXCIntPlan_t);
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestXCPotentialUKSCompute(
    cuestHandle_t handle,
    const cuestXCIntPlan_t plan,
    const cuestXCPotentialUKSComputeParameters_t parameters,
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspace_t* temporaryWorkspace,
    uint64_t numOccupiedAlpha,
    uint64_t numOccupiedBeta,
    const double* coefficientMatrixAlpha,
    const double* coefficientMatrixBeta,
    double* outXCEnergy,
    double* outXCPotentialMatrixAlpha,
    double* outXCPotentialMatrixBeta);

/**
 * @brief Query the temporary workspace required (in bytes) for an XC potential matrix computation.
 * @ingroup ExchangeCorrelation
 *
 * 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                   Compute parameters (\c cuestXCPotentialUKSComputeParameters_t) Must not be NULL
 * @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. Some static allocations are made outside of what is
 *                                              controlled by 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      outXCPotentialMatrixAlpha    Output buffer for computed alpha XC potential matrix. May be NULL.
 * @param      outXCPotentialMatrixBeta     Output buffer for computed beta 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;
 *   - \c CUEST_STATUS_EXCEPTION if \c cuestXCIntPlanParametersFunctional_t is \c CUEST_XCINTPLAN_PARAMETERS_FUNCTIONAL_HF (set in the \c cuestXCIntPlan_t);
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestXCPotentialUKSComputeWorkspaceQuery(
    cuestHandle_t handle,
    const cuestXCIntPlan_t plan,
    const cuestXCPotentialUKSComputeParameters_t parameters,
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspaceDescriptor_t* temporaryWorkspaceDescriptor,
    uint64_t numOccupiedAlpha,
    uint64_t numOccupiedBeta,
    const double* coefficientMatrixAlpha,
    const double* coefficientMatrixBeta,
    double* outXCEnergy,
    double* outXCPotentialMatrixAlpha,
    double* outXCPotentialMatrixBeta);

/**
 * @brief Compute the electron density on the integration grid using the provided XC integral plan and molecular orbitals.
 * @ingroup ExchangeCorrelationAdvanced
 *
 * This routine evaluates the electronic density (and its derivatives, when applicable) on the molecular integration grid.
 * The computation depends on the chosen approximation level (LDA, GGA, or meta-GGA), which determines the number of
 * quantities calculated per grid point. The density is computed from the occupied molecular orbitals.
 * 
 * Each approximation produces an output array of shape (\c npoint × ndim), where:
 *   - LDA: ndim = 1 (\f$ \rho \f$);
 *   - GGA: ndim = 4 (\f$ \rho, \nabla_{x}\rho, \nabla_{y}\rho, \nabla_{z}\rho \f$);
 *   - meta-GGA: ndim = 5 (\f$ \rho, \nabla_{x}\rho, \nabla_{y}\rho, \nabla_{z}\rho, \tau \f$).
 *
 * The returned density does not include integration weights. To obtain the total number of electrons, evaluate:
 * \f[ N_{elec} = \sum_{P}^{\rm npoint} w_{P} \rho_{P} \f]
 * where, \f$ w_{P} \f$ is the total integration weight that can be obtained by calling \c cuestXCIntegrationWeightCompute.
 *
 * All required workspace must first be allocated according to the size returned by 
 * \c cuestXCDensityComputeWorkspaceQuery, which must be called before invoking this function.
 *
 * @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]  approximation               Specifies the XC approximation: LDA, GGA, or meta-GGA.
 * @param[in]  parameters                  Compute parameters (\c cuestXCDensityComputeParameters_t) Must not be NULL
 * @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. Some static allocations are made outside of what is
 *                                             controlled by the variableBufferSize. Host memory is currently unused.
 *                                             This value MUST remain unchanged between the WorkspaceQuery call and the Compute call.
 * @param[in]  temporaryWorkspace          Preallocated temporary workspace buffer for this operation. Must not be NULL.
 * @param[in]  numOccupied                 Number of occupied orbitals (rows in \c coefficientMatrix). Must be greater than zero.
 * @param[in]  coefficientMatrix           Input orbital coefficient matrix (size: \c numOccupied × nao) on the GPU. Must not be NULL.
 * @param[out] outGridDensity              Output buffer for computed density or density derivatives on the grid (size: \c npoint × ndim) on the GPU. Must not 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 of incorrect type;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if \c numOccupied is zero or \c approximation is invalid;
 *   - \c CUEST_STATUS_EXCEPTION on device or internal error;
 *   - \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestXCDensityCompute(
    cuestHandle_t handle,
    const cuestXCIntPlan_t plan,
    cuestXCAdvancedComputeParametersApproximation_t approximation,
    const cuestXCDensityComputeParameters_t parameters,
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspace_t* temporaryWorkspace,
    uint64_t numOccupied,
    const double* coefficientMatrix,
    double* outGridDensity);

/**
 * @brief Query the temporary workspace required (in bytes) for an XC density computation.
 * @ingroup ExchangeCorrelationAdvanced
 *
 * Determines the GPU memory requirements for computing the electron density (and its derivatives)
 * on the molecular grid using the provided XC integral plan and approximation type. Only memory
 * requirements are estimated; no actual computation is performed. Input and output pointers may be NULL.
 *
 * This call must precede \c cuestXCDensityCompute to ensure that sufficient temporary memory is available.
 *
 * @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]  approximation                     Specifies the XC approximation: LDA, GGA, or meta-GGA.
 * @param[in]  parameters                        Compute parameters (\c cuestXCDensityComputeParameters_t) Must not be NULL
 * @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. Some static allocations are made outside of what is
 *                                                   controlled by 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 with required workspace sizes (in bytes). Must not be NULL.
 * @param[in]  numOccupied                       Number of occupied orbitals (rows in \c coefficientMatrix). Must be greater than zero.
 * @param      coefficientMatrix                 Input orbital coefficient matrix. May be NULL.
 * @param      outGridDensity                    Output buffer for computed density values. 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 of incorrect type;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if \c numOccupied is zero or \c approximation is invalid;
 *   - \c CUEST_STATUS_EXCEPTION on device or internal error;
 *   - \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestXCDensityComputeWorkspaceQuery(
    cuestHandle_t handle,
    const cuestXCIntPlan_t plan,
    cuestXCAdvancedComputeParametersApproximation_t approximation,
    const cuestXCDensityComputeParameters_t parameters,
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspaceDescriptor_t* temporaryWorkspaceDescriptor,
    uint64_t numOccupied,
    const double* coefficientMatrix,
    double* outGridDensity);

/**
 * @brief Compute the XC potential matrix (Vxc) from a precomputed XC potential on the integration grid.
 * @ingroup ExchangeCorrelationAdvanced
 *
 * This routine constructs the exchange-correlation potential matrix in the AO basis from a grid representation
 * of the XC potential. The input grid potential should correspond to the same grid and basis information
 * specified by the provided XC integral plan. The dimensionality of the grid potential depends on the selected
 * approximation (LDA, GGA, meta-GGA).
 *  
 * Each approximation requires an input array of shape (\c npoint × ndim) containing the XC potential
 * components (derivatives of the XC energy density \f$ f \f$) ordered as:
 *   - LDA: ndim = 1 (\f$ \partial f / \partial\rho \f$);
 *   - GGA: ndim = 4 (\f$ \partial f / \partial\rho,\; 4 (\partial f / \partial\gamma) \nabla_{x}\rho,\; 4 (\partial f / \partial\gamma) \nabla_{y}\rho,\; 4 (\partial f / \partial\gamma) \nabla_{z}\rho \f$, where \f$ \gamma = |\nabla\rho|^{2} \f$);
 *   - meta-GGA: ndim = 5 (the GGA components followed by \f$ \partial f / \partial\tau \f$).
 *
 * Important: This function does not apply the integration weights to the user-supplied XC potential. The user must
 * apply the total integration weights, which can be obtained by calling \c cuestXCIntegrationWeightCompute.
 *
 * All required temporary workspace must first be allocated using the size returned by 
 * \c cuestXCPotentialComputeWorkspaceQuery.
 *
 * @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]  approximation               Specifies the XC approximation: LDA, GGA, or meta-GGA.
 * @param[in]  parameters                  Compute parameters (\c cuestXCPotentialComputeParameters_t). Must not be NULL
 * @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. Some static allocations are made outside of what is
 *                                             controlled by the variableBufferSize. Host memory is currently unused.
 *                                             This value MUST remain unchanged between the WorkspaceQuery call and the Compute call.
 * @param[in]  temporaryWorkspace          Preallocated temporary workspace buffer for this operation. Must not be NULL.
 * @param[in]  gridXCPotential             Input XC potential on the grid (size: \c npoint × ndim) on GPU. Must not be NULL.
 * @param[out] outXCPotentialMatrix        Output buffer for computed XC potential matrix (size: nao × nao) on GPU. Must not 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 of incorrect type;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if \c approximation is invalid;
 *   - \c CUEST_STATUS_EXCEPTION on device or internal error;
 *   - \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestXCPotentialCompute(
    cuestHandle_t handle,
    const cuestXCIntPlan_t plan,
    cuestXCAdvancedComputeParametersApproximation_t approximation,
    const cuestXCPotentialComputeParameters_t parameters,
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspace_t* temporaryWorkspace,
    const double* gridXCPotential,
    double* outXCPotentialMatrix);

/**
 * @brief Query the temporary workspace required (in bytes) for an XC potential matrix computation.
 * @ingroup ExchangeCorrelationAdvanced
 *
 * This function returns the GPU workspace memory requirements for constructing
 * the exchange-correlation (XC) potential matrix in the AO basis from a given XC potential on the grid.
 * Only workspace sizes are computed; the input and output pointers may be NULL.
 *
 * This query must be executed prior to calling \c cuestXCPotentialCompute.
 *
 * @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]  approximation                Specifies the XC approximation: LDA, GGA, or meta-GGA.
 * @param[in]  parameters                   Compute parameters (\c cuestXCPotentialComputeParameters_t). Must not be NULL
 * @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. Some static allocations are made outside of what is
 *                                              controlled by 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 required temporary workspace sizes (in bytes). Must not be NULL.
 * @param      gridXCPotential              Input XC potential on the grid. 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 of incorrect type;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if \c approximation is invalid;
 *   - \c CUEST_STATUS_EXCEPTION on device or internal error;
 *   - \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestXCPotentialComputeWorkspaceQuery(
    cuestHandle_t handle,
    const cuestXCIntPlan_t plan,
    cuestXCAdvancedComputeParametersApproximation_t approximation,
    const cuestXCPotentialComputeParameters_t parameters,
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspaceDescriptor_t* temporaryWorkspaceDescriptor,
    const double* gridXCPotential,
    double* outXCPotentialMatrix);

/**
 * @brief Compute the nonsymmetric electron density on the integration grid using the provided XC integral plan and two sets of molecular orbitals.
 * @ingroup ExchangeCorrelationAdvanced
 *
 * This routine evaluates the nonsymmetric electronic density (and its derivatives, when applicable) on the molecular
 * integration grid. The nonsymmetric density is formed from a left and a right set of occupied molecular orbitals,
 * both of size \c numOccupied × nao. This is the generalization of \c cuestXCDensityCompute, which uses the same
 * coefficients for both the left and right.
 *
 * The output array has the same shape as \c cuestXCDensityCompute for the chosen approximation (size: \c npoint × ndim):
 *   - LDA: ndim = 1 (\f$ \rho \f$);
 *   - GGA: ndim = 4 (\f$ \rho, \nabla_{x}\rho, \nabla_{y}\rho, \nabla_{z}\rho \f$);
 *   - meta-GGA: ndim = 5 (\f$ \rho, \nabla_{x}\rho, \nabla_{y}\rho, \nabla_{z}\rho, \tau \f$).
 *
 * All required workspace must first be allocated according to the size returned by
 * \c cuestXCNonsymmetricDensityComputeWorkspaceQuery, which must be called before invoking this function.
 *
 * @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]  approximation               Specifies the XC approximation: LDA, GGA, or meta-GGA.
 * @param[in]  parameters                  Compute parameters (\c cuestXCNonsymmetricDensityComputeParameters_t). Must not be NULL.
 * @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          Preallocated temporary workspace buffer for this operation. Must not be NULL.
 * @param[in]  numOccupied                 Number of occupied orbitals (rows in each coefficient matrix). Must be greater than zero.
 * @param[in]  leftCoefficientMatrix       Input left orbital coefficient matrix (size: \c numOccupied × nao) on the GPU. Must not be NULL.
 * @param[in]  rightCoefficientMatrix      Input right orbital coefficient matrix (size: \c numOccupied × nao) on the GPU. Must not be NULL.
 * @param[out] outGridDensity              Output buffer for computed nonsymmetric density or density derivatives on the grid (size: \c npoint × ndim) on the GPU. Must not 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 of incorrect type;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if \c numOccupied is zero or \c approximation is invalid;
 *   - \c CUEST_STATUS_EXCEPTION on device or internal error;
 *   - \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestXCNonsymmetricDensityCompute(
    cuestHandle_t handle,
    const cuestXCIntPlan_t plan,
    cuestXCAdvancedComputeParametersApproximation_t approximation,
    const cuestXCNonsymmetricDensityComputeParameters_t parameters,
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspace_t* temporaryWorkspace,
    uint64_t numOccupied,
    const double* leftCoefficientMatrix,
    const double* rightCoefficientMatrix,
    double* outGridDensity);

/**
 * @brief Query the temporary workspace required (in bytes) for an XC nonsymmetric density computation.
 * @ingroup ExchangeCorrelationAdvanced
 *
 * Determines the GPU memory requirements for computing the nonsymmetric electron density (and its derivatives)
 * on the molecular grid using the provided XC integral plan and approximation type. Only memory
 * requirements are estimated; no actual computation is performed. Input and output pointers may be NULL.
 *
 * This call must precede \c cuestXCNonsymmetricDensityCompute to ensure that sufficient temporary memory is available.
 *
 * @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]  approximation                     Specifies the XC approximation: LDA, GGA, or meta-GGA.
 * @param[in]  parameters                        Compute parameters (\c cuestXCNonsymmetricDensityComputeParameters_t). Must not be NULL.
 * @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 with required workspace sizes (in bytes). Must not be NULL.
 * @param[in]  numOccupied                       Number of occupied orbitals (rows in each coefficient matrix). Must be greater than zero.
 * @param      leftCoefficientMatrix             Input left orbital coefficient matrix. May be NULL.
 * @param      rightCoefficientMatrix            Input right orbital coefficient matrix. May be NULL.
 * @param      outGridDensity                    Output buffer for computed nonsymmetric density values. 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 of incorrect type;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if \c numOccupied is zero or \c approximation is invalid;
 *   - \c CUEST_STATUS_EXCEPTION on device or internal error;
 *   - \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestXCNonsymmetricDensityComputeWorkspaceQuery(
    cuestHandle_t handle,
    const cuestXCIntPlan_t plan,
    cuestXCAdvancedComputeParametersApproximation_t approximation,
    const cuestXCNonsymmetricDensityComputeParameters_t parameters,
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspaceDescriptor_t* temporaryWorkspaceDescriptor,
    uint64_t numOccupied,
    const double* leftCoefficientMatrix,
    const double* rightCoefficientMatrix,
    double* outGridDensity);

#ifdef __cplusplus
}
#endif

#endif /* XC_POTENTIAL_COMPUTE_API_H */
