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

#include <stddef.h>

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

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Compute the PCM potential matrix using preconditioned conjugate gradient (PCG) to solve for the PCM charges.
 * @ingroup PCM
 *
 * Guesses are obtained from the \c inQ matrix, with the pre-allocated \c outQ matrix used to return the converged charges.
 *
 * Convergence properties are controlled by configuring the \c cuestPCMPotentialComputeParameters_t structure passed as the \c parameters parameter.
 *
 * The required temporary workspace must be allocated using the sizes returned by \c cuestPCMPotentialComputeWorkspaceQuery.
 * The number of points, npoint, can be queried from the \c cuestPCMIntPlan_t structure using the \c CUEST_PCMINTPLAN_NUM_POINT query
 * and is the sum of the \c numAngularPointsPerAtom array used to create the plan.
 *
 * \b Kernel and precision selection. This routine can use JIT-compiled kernels and/or reduced-precision
 * "ffloat" compute, selected through the compute parameters:
 *   - \c CUEST_PCMPOTENTIALCOMPUTE_PARAMETERS_JIT_USAGE_MODE (\c cuestJITUsageMode_t): \c CUEST_JIT_USAGE_MODE_ON
 *     uses JIT-compiled kernels, \c CUEST_JIT_USAGE_MODE_OFF uses AOT-compiled kernels. JIT-compiled kernels
 *     are only available in the cuEST CUDA 13 package; the default is \c CUEST_JIT_USAGE_MODE_ON in the
 *     cuEST CUDA 13 package and \c CUEST_JIT_USAGE_MODE_OFF in the cuEST CUDA 12 package. Requesting
 *     \c CUEST_JIT_USAGE_MODE_ON with the cuEST CUDA 12 package returns \c CUEST_STATUS_INVALID_ARGUMENT.
 *   - \c CUEST_PCMPOTENTIALCOMPUTE_PARAMETERS_FFLOAT_USAGE_MODE (\c cuestFfloatUsageMode_t) selects the compute precision:
 *     \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.
 *   - JIT-compiled kernels and ffloat compute may be combined. When JIT is enabled, ffloat compute can be used
 *     throughout the operation. When JIT is disabled, ffloat compute can be used only while solving for the
 *     PCM charges; the remainder of the operation always uses fp64 compute.
 *
 * @param[in]  handle                      cuEST handle. Must not be NULL.
 * @param[in]  plan                        PCM integral plan (opaque handle) constructed with \c cuestPCMIntPlanCreate. Must not be NULL.
 * @param[in]  parameters                  PCM potential compute parameters (opaque handle) constructed with \c cuestParametersCreate. Must not be NULL.
 * @param[in]  temporaryWorkspace          Temporary workspace buffer for this operation. Must not be NULL.
 * @param[in]  densityMatrix               Total (alpha + beta) AO basis density matrix (size: nao × nao) on the GPU. Must not be NULL.
 * @param[in]  inQ                         Input guess Q vector (size: npoint) on the GPU; user should initialize this array to zeros if no guess is available. Must not be NULL.
 * @param[out] outQ                        Output Q matrix (size: npoint) on the GPU. Must not be NULL.
 *                                             Elements of this array are overwritten by the calculation results.
 * @param[out] outPCMResults               PCM results (opaque handle) constructed with \c cuestResultsCreate. Must not be NULL.
 *                                             Elements contained in this handle are overwritten by the calculation results.
 * @param[out] outPCMPotentialMatrix       Output PCM potential matrix (size: nao × nao) on the GPU. Must not be NULL.
 *                                             Elements of this array are overwritten by the calculation results.
 *
 * Note that the return status is \c CUEST_STATUS_SUCCESS even if the PCM charges did not converge within the maximum number of iterations provided by the \c parameters.
 * The caller should check the \c converged member of the \c outPCMResults handle to determine if the PCM charges converged.
 *
 * @return
 *   - \c CUEST_STATUS_SUCCESS on success, including the case where the PCM charges did not converge within the maximum number of iterations provided by the \c parameters;
 *   - \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 the \c parameters are configured to have convergence threshold less than or equal to zero;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if a parameter value is invalid, or if JIT-compiled kernels are requested
 *     with the cuEST CUDA 12 package;
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestPCMPotentialCompute(
    cuestHandle_t handle,
    const cuestPCMIntPlan_t plan,
    const cuestPCMPotentialComputeParameters_t parameters,
    cuestWorkspace_t* temporaryWorkspace,
    const double *densityMatrix,
    const double *inQ,
    double *outQ,
    cuestPCMResults_t outPCMResults,
    double *outPCMPotentialMatrix);

/**
 * @brief Query the temporary workspace required for a PCM potential computation.
 * @ingroup PCM
 *
 * This function calculates the memory required for a PCM potential computation,
 * returning host/device workspace buffer sizes in the output descriptor. The user arrays \c densityMatrix, \c inQ, \c outQ, \c outPCMResults, and \c outPCMPotentialMatrix may be NULL.
 * All parameters and object handles must be valid.
 *
 * \b Kernel and precision selection. This routine can use JIT-compiled kernels and/or reduced-precision
 * "ffloat" compute, selected through the compute parameters:
 *   - \c CUEST_PCMPOTENTIALCOMPUTE_PARAMETERS_JIT_USAGE_MODE (\c cuestJITUsageMode_t): \c CUEST_JIT_USAGE_MODE_ON
 *     uses JIT-compiled kernels, \c CUEST_JIT_USAGE_MODE_OFF uses AOT-compiled kernels. JIT-compiled kernels
 *     are only available in the cuEST CUDA 13 package; the default is \c CUEST_JIT_USAGE_MODE_ON in the
 *     cuEST CUDA 13 package and \c CUEST_JIT_USAGE_MODE_OFF in the cuEST CUDA 12 package. Requesting
 *     \c CUEST_JIT_USAGE_MODE_ON with the cuEST CUDA 12 package returns \c CUEST_STATUS_INVALID_ARGUMENT.
 *   - \c CUEST_PCMPOTENTIALCOMPUTE_PARAMETERS_FFLOAT_USAGE_MODE (\c cuestFfloatUsageMode_t) selects the compute precision:
 *     \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.
 *   - JIT-compiled kernels and ffloat compute may be combined. When JIT is enabled, ffloat compute can be used
 *     throughout the operation. When JIT is disabled, ffloat compute can be used only while solving for the
 *     PCM charges; the remainder of the operation always uses fp64 compute.
 *
 * @param[in]  handle                        cuEST handle. Must not be NULL.
 * @param[in]  plan                          PCM integral plan constructed with \c cuestPCMIntPlanCreate. Must not be NULL.
 * @param[in]  parameters                    PCM potential compute parameters constructed with \c cuestParametersCreate. Must not be NULL.
 * @param[out] temporaryWorkspaceDescriptor  Output descriptor for temporary workspace sizes. Must not be NULL.
 * @param      densityMatrix                 Density matrix. May be NULL.
 * @param      inQ                           Input Q matrix. May be NULL.
 * @param      outQ                          Output Q matrix. May be NULL.
 * @param      outPCMResults                 PCM results constructed with \c cuestResultsCreate. May be NULL.
 * @param      outPCMPotentialMatrix         Output PCM 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 the \c parameters are configured to have convergence threshold less than or equal to zero;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if a parameter value is invalid, or if JIT-compiled kernels are requested
 *     with the cuEST CUDA 12 package;
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestPCMPotentialComputeWorkspaceQuery(
    cuestHandle_t handle,
    const cuestPCMIntPlan_t plan,
    const cuestPCMPotentialComputeParameters_t parameters,
    cuestWorkspaceDescriptor_t* temporaryWorkspaceDescriptor,
    const double *densityMatrix,
    const double *inQ,
    double *outQ,
    cuestPCMResults_t outPCMResults,
    double *outPCMPotentialMatrix);


#ifdef __cplusplus
}
#endif

#endif /* PCM_POTENTIAL_COMPUTE_API_H */
