LCOV - code coverage report
Current view: top level - src/xc - xc_gauxc_interface.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 80.3 % 304 244
Test Date: 2026-11-24 01:22:08 Functions: 58.6 % 29 17

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : #ifdef __GAUXC
       9              : #include "gauxc/gauxc_config.f"
      10              : #endif
      11              : 
      12              : #define GAUXC_RETURN_IF_ERROR(status) IF (status%status%code /= 0) RETURN
      13              : 
      14              : MODULE xc_gauxc_interface
      15              : 
      16              :    USE iso_fortran_env, ONLY: &
      17              :       error_unit
      18              : #if defined (__HAS_IEEE_EXCEPTIONS)
      19              :    USE ieee_exceptions, ONLY: &
      20              :       ieee_all, &
      21              :       ieee_get_halting_mode, &
      22              :       ieee_set_halting_mode
      23              : #endif
      24              :    USE iso_c_binding, ONLY: &
      25              :       c_associated, &
      26              :       c_bool, &
      27              :       c_char, &
      28              :       c_double, &
      29              :       c_f_pointer, &
      30              :       c_int, &
      31              :       c_int32_t, &
      32              :       c_int64_t, &
      33              :       c_null_char, &
      34              :       c_null_ptr, &
      35              :       c_ptr, &
      36              :       c_size_t
      37              :    USE particle_types, ONLY: &
      38              :       particle_type
      39              :    USE qs_kind_types, ONLY: &
      40              :       get_qs_kind, &
      41              :       qs_kind_type
      42              :    USE cp_dbcsr_api, ONLY: &
      43              :       dbcsr_p_type
      44              :    USE cp_log_handling, ONLY: &
      45              :       cp_logger_get_default_io_unit
      46              : 
      47              : #ifdef __GAUXC
      48              : 
      49              :    USE kinds, ONLY: &
      50              :       default_path_length, &
      51              :       default_string_length, &
      52              :       dp
      53              :    USE physcon, ONLY: &
      54              :       bohr
      55              :    USE atomic_kind_types, ONLY: &
      56              :       atomic_kind_type, &
      57              :       get_atomic_kind, &
      58              :       get_atomic_kind_set
      59              :    USE qs_integral_utils, ONLY: &
      60              :       basis_set_list_setup
      61              :    USE basis_set_types, ONLY: &
      62              :       gto_basis_set_p_type, &
      63              :       gto_basis_set_type, &
      64              :       write_gto_basis_set
      65              :    USE periodic_table, ONLY: &
      66              :       get_ptable_info
      67              :    USE gauxc_status, ONLY: &
      68              :       gauxc_status_message, &
      69              :       gauxc_status_type
      70              :    USE gauxc_enums, ONLY: &
      71              :       gauxc_atomicgridsizedefault, &
      72              :       gauxc_executionspace, &
      73              :       gauxc_pruningscheme, &
      74              :       gauxc_radialquad
      75              :    USE gauxc_runtime_environment, ONLY: &
      76              :       gauxc_runtime_environment_delete, &
      77              :       gauxc_runtime_environment_new, &
      78              :       gauxc_runtime_environment_type
      79              : #ifdef GAUXC_HAS_DEVICE
      80              :    USE gauxc_runtime_environment, ONLY: &
      81              :       gauxc_device_runtime_environment_new
      82              : #endif
      83              : #if defined(GAUXC_HAS_DEVICE) && defined(__LIBTORCH)
      84              :    USE torch_api, ONLY: torch_cuda_device_count, torch_cuda_is_available
      85              : #endif
      86              :    USE gauxc_molecule, ONLY: &
      87              :       gauxc_delete, &
      88              :       gauxc_molecule_new_from_atoms, &
      89              :       gauxc_molecule_type
      90              :    USE gauxc_atom, ONLY: &
      91              :       gauxc_atom_type
      92              :    USE gauxc_basisset, ONLY: &
      93              :       gauxc_basisset_new, &
      94              :       gauxc_basisset_new_from_shells, &
      95              :       gauxc_basisset_type, &
      96              :       gauxc_delete
      97              :    USE gauxc_shell, ONLY: &
      98              :       gauxc_shell_type
      99              :    USE gauxc_molgrid, ONLY: &
     100              :       gauxc_delete, &
     101              :       gauxc_molgrid_new_default, &
     102              :       gauxc_molgrid_type
     103              :    USE gauxc_load_balancer, ONLY: &
     104              :       gauxc_delete, &
     105              :       gauxc_load_balancer_factory_get_instance, &
     106              :       gauxc_load_balancer_factory_new, &
     107              :       gauxc_load_balancer_factory_type, &
     108              :       gauxc_load_balancer_type
     109              :    USE gauxc_molecular_weights, ONLY: &
     110              :       gauxc_delete, &
     111              :       gauxc_get_instance, &
     112              :       gauxc_molecular_weights_factory_new, &
     113              :       gauxc_molecular_weights_factory_type, &
     114              :       gauxc_molecular_weights_modify_weights, &
     115              :       gauxc_molecular_weights_settings, &
     116              :       gauxc_molecular_weights_type
     117              :    USE gauxc_xc_functional, ONLY: &
     118              :       gauxc_delete, &
     119              :       gauxc_functional_from_string, &
     120              :       gauxc_functional_type
     121              :    USE gauxc_integrator, ONLY: &
     122              :       gauxc_delete, &
     123              :       gauxc_integrator_eval_exc_grad_rks, &
     124              :       gauxc_integrator_eval_exc_grad_uks, &
     125              :       gauxc_integrator_eval_exc_vxc_rks, &
     126              :       gauxc_integrator_eval_exc_vxc_uks, &
     127              :       gauxc_integrator_new, &
     128              :       gauxc_integrator_type
     129              : #ifdef GAUXC_HAS_ONEDFT
     130              :    USE gauxc_integrator, ONLY: &
     131              :       gauxc_integrator_eval_exc_grad_onedft_uks, &
     132              :       gauxc_integrator_eval_exc_vxc_onedft_uks
     133              :    USE OMP_LIB, ONLY: &
     134              :       omp_get_max_threads, &
     135              :       omp_set_num_threads
     136              : #endif
     137              : #ifdef GAUXC_HAS_HDF5
     138              :    USE gauxc_external_hdf5_write, ONLY: &
     139              :       gauxc_write_hdf5_record
     140              : #endif
     141              :    USE string_utilities, ONLY: &
     142              :       uppercase
     143              : #endif
     144              : 
     145              : #include "../base/base_uses.f90"
     146              : 
     147              :    IMPLICIT NONE
     148              :    PRIVATE
     149              : 
     150              : #ifndef __GAUXC
     151              : 
     152              :    ! The module still exists as an empty shell when compiling without GauXC.
     153              : 
     154              :    TYPE cp_gauxc_molecule_type
     155              :    END TYPE cp_gauxc_molecule_type
     156              : 
     157              :    TYPE cp_gauxc_basisset_type
     158              :       INTEGER :: max_l = -1
     159              :    END TYPE cp_gauxc_basisset_type
     160              : 
     161              :    TYPE cp_gauxc_grid_type
     162              :    END TYPE cp_gauxc_grid_type
     163              : 
     164              :    TYPE cp_gauxc_integrator_type
     165              :    END TYPE cp_gauxc_integrator_type
     166              : 
     167              :    TYPE cp_gauxc_status_type
     168              :    END TYPE cp_gauxc_status_type
     169              : 
     170              : #else
     171              : 
     172              :    ! TODO can we make the single fields private somehow?
     173              : 
     174              :    TYPE cp_gauxc_molecule_type
     175              :       TYPE(gauxc_molecule_type) :: molecule
     176              :    END TYPE cp_gauxc_molecule_type
     177              : 
     178              :    TYPE cp_gauxc_basisset_type
     179              :       TYPE(gauxc_basisset_type) :: basis
     180              :       INTEGER :: max_l = -1
     181              :    END TYPE cp_gauxc_basisset_type
     182              : 
     183              :    TYPE cp_gauxc_grid_type
     184              :       TYPE(gauxc_molgrid_type) :: grid
     185              :       TYPE(gauxc_load_balancer_type) :: lb
     186              :       TYPE(gauxc_load_balancer_factory_type) :: lbf
     187              :       TYPE(gauxc_molecular_weights_type) :: mw
     188              :       TYPE(gauxc_molecular_weights_factory_type) :: mwf
     189              :       TYPE(gauxc_runtime_environment_type) :: rt
     190              :       LOGICAL :: owns_rt = .FALSE.
     191              :       LOGICAL :: uses_device_runtime = .FALSE.
     192              :    END TYPE cp_gauxc_grid_type
     193              : 
     194              :    TYPE cp_gauxc_integrator_type
     195              :       TYPE(gauxc_functional_type) :: func
     196              :       TYPE(gauxc_integrator_type) :: integrator
     197              :    END TYPE cp_gauxc_integrator_type
     198              : 
     199              :    TYPE cp_gauxc_status_type
     200              :       TYPE(gauxc_status_type) :: status
     201              :    END TYPE cp_gauxc_status_type
     202              : 
     203              :    TYPE(gauxc_runtime_environment_type) :: rt
     204              :    INTEGER :: rt_mpi_comm = -1
     205              :    LOGICAL :: rt_has_mpi_comm = .FALSE.
     206              : 
     207              : #endif
     208              : 
     209              :    TYPE cp_gauxc_xc_type
     210              :       REAL(c_double) :: exc = 0.0_c_double
     211              :       REAL(c_double), DIMENSION(:, :), ALLOCATABLE :: vxc_scalar, vxc_zeta
     212              :    END TYPE cp_gauxc_xc_type
     213              : 
     214              :    TYPE cp_gauxc_xc_gradient_type
     215              :       REAL(c_double), ALLOCATABLE, DIMENSION(:) :: exc_grad
     216              :    END TYPE cp_gauxc_xc_gradient_type
     217              : 
     218              :    CHARACTER(len=*), PARAMETER :: no_gauxc_message = "Compile CP2K with GauXC to use this functionality!"
     219              : 
     220              :    PUBLIC :: &
     221              :       cp_gauxc_basisset_type, &
     222              :       cp_gauxc_grid_type, &
     223              :       cp_gauxc_integrator_type, &
     224              :       cp_gauxc_molecule_type, &
     225              :       cp_gauxc_status_type, &
     226              :       cp_gauxc_xc_gradient_type, &
     227              :       cp_gauxc_xc_type, &
     228              :       gauxc_check_status, &
     229              :       gauxc_compute_xc_gradient, &
     230              :       gauxc_compute_xc, &
     231              :       gauxc_create_basisset, &
     232              :       gauxc_create_grid, &
     233              :       gauxc_create_integrator, &
     234              :       gauxc_create_molecule, &
     235              :       gauxc_destroy_basisset, &
     236              :       gauxc_destroy_grid, &
     237              :       gauxc_destroy_integrator, &
     238              :       gauxc_destroy_molecule, &
     239              :       gauxc_finalize, &
     240              :       gauxc_init, &
     241              :       gauxc_write_basisset_hdf5, &
     242              :       gauxc_write_molecule_hdf5
     243              : CONTAINS
     244              : 
     245              : ! **************************************************************************************************
     246              : !> \brief ...
     247              : !> \param status ...
     248              : ! **************************************************************************************************
     249            0 :    SUBROUTINE print_gauxc_status_message(status)
     250              :       ! IMPORT :: c_ptr
     251              :       TYPE(cp_gauxc_status_type)                         :: status
     252              : 
     253              : #ifdef __GAUXC
     254            0 :       CHARACTER(kind=c_char), POINTER                    :: s(:)
     255              :       CHARACTER(len=32)                                  :: stderr_env
     256              :       INTEGER                                            :: i, ierr, iw
     257              :       LOGICAL                                            :: print_to_stderr
     258              :       INTEGER, PARAMETER                                 :: status_message_length = 4096
     259              : 
     260            0 :       iw = cp_logger_get_default_io_unit()
     261            0 :       ierr = error_unit
     262            0 :       CALL GET_ENVIRONMENT_VARIABLE("CP2K_GAUXC_STATUS_STDERR", stderr_env)
     263            0 :       CALL uppercase(stderr_env)
     264            0 :       SELECT CASE (TRIM(stderr_env))
     265              :       CASE ("", "0", "FALSE", "F", "OFF", "NO")
     266            0 :          print_to_stderr = .FALSE.
     267              :       CASE ("1", "TRUE", "T", "ON", "YES")
     268            0 :          print_to_stderr = .TRUE.
     269              :       CASE DEFAULT
     270            0 :          print_to_stderr = .TRUE.
     271              :       END SELECT
     272            0 :       IF (iw > 0) THEN
     273            0 :          WRITE (UNIT=iw, FMT='(a,1x,i0)') "GauXC returned with status code", status%status%code
     274            0 :          IF (c_associated(status%status%message)) THEN
     275            0 :             WRITE (UNIT=iw, FMT='(a)', ADVANCE='no') "GauXC status message: ["
     276              : 
     277            0 :             CALL c_f_pointer(status%status%message, s, [status_message_length])
     278            0 :             DO i = 1, SIZE(s)
     279            0 :                IF (s(i) == c_null_char) EXIT
     280            0 :                WRITE (UNIT=iw, FMT='(A)', ADVANCE='no') s(i)
     281              :             END DO
     282              : 
     283            0 :             WRITE (UNIT=iw, FMT='(a)') "]"
     284              :          ELSE
     285            0 :             WRITE (UNIT=iw, FMT='(a)') "GauXC status message: [null]"
     286              :          END IF
     287              :       END IF
     288            0 :       IF (print_to_stderr) THEN
     289            0 :          WRITE (UNIT=ierr, FMT='(a,1x,i0)') "GauXC returned with status code", status%status%code
     290            0 :          IF (c_associated(status%status%message)) THEN
     291            0 :             WRITE (UNIT=ierr, FMT='(a)', ADVANCE='no') "GauXC status message: ["
     292              : 
     293            0 :             CALL c_f_pointer(status%status%message, s, [status_message_length])
     294            0 :             DO i = 1, SIZE(s)
     295            0 :                IF (s(i) == c_null_char) EXIT
     296            0 :                WRITE (UNIT=ierr, FMT='(A)', ADVANCE='no') s(i)
     297              :             END DO
     298              : 
     299            0 :             WRITE (UNIT=ierr, FMT='(a)') "]"
     300              :          ELSE
     301            0 :             WRITE (UNIT=ierr, FMT='(a)') "GauXC status message: [null]"
     302              :          END IF
     303              :       END IF
     304              : #else
     305              :       MARK_USED(status)
     306              : #endif
     307            0 :    END SUBROUTINE print_gauxc_status_message
     308              : 
     309              : ! **************************************************************************************************
     310              : !> \brief ...
     311              : !> \param mpi_comm ...
     312              : !> \param status ...
     313              : ! **************************************************************************************************
     314         1492 :    SUBROUTINE gauxc_init(mpi_comm, status)
     315              :       INTEGER, INTENT(IN), OPTIONAL                      :: mpi_comm
     316              :       TYPE(cp_gauxc_status_type), INTENT(OUT)            :: status
     317              : 
     318              : #ifdef __GAUXC
     319              : #if defined(GAUXC_HAS_MPI) && defined(__parallel)
     320         1492 :       IF (PRESENT(mpi_comm)) THEN
     321         1492 :          rt = gauxc_runtime_environment_new(status%status, mpi_comm)
     322         1492 :          rt_mpi_comm = mpi_comm
     323         1492 :          rt_has_mpi_comm = .TRUE.
     324              :       ELSE
     325            0 :          rt = gauxc_runtime_environment_new(status%status)
     326            0 :          rt_mpi_comm = -1
     327            0 :          rt_has_mpi_comm = .FALSE.
     328              :       END IF
     329              : #else
     330              :       MARK_USED(mpi_comm)
     331              :       rt = gauxc_runtime_environment_new(status%status)
     332              :       rt_mpi_comm = -1
     333              :       rt_has_mpi_comm = .FALSE.
     334              : #endif
     335         1492 :       GAUXC_RETURN_IF_ERROR(status)
     336              : #else
     337              :       MARK_USED(mpi_comm)
     338              :       MARK_USED(status)
     339              : #endif
     340              :    END SUBROUTINE gauxc_init
     341              : 
     342              : ! **************************************************************************************************
     343              : !> \brief ...
     344              : !> \param status ...
     345              : ! **************************************************************************************************
     346         1492 :    SUBROUTINE gauxc_finalize(status)
     347              :       TYPE(cp_gauxc_status_type), INTENT(OUT)            :: status
     348              : 
     349              : #ifdef __GAUXC
     350         1492 :       CALL gauxc_runtime_environment_delete(status%status, rt)
     351         1492 :       GAUXC_RETURN_IF_ERROR(status)
     352         1492 :       rt_mpi_comm = -1
     353         1492 :       rt_has_mpi_comm = .FALSE.
     354              : #else
     355              :       MARK_USED(status)
     356              : #endif
     357              :    END SUBROUTINE gauxc_finalize
     358              : 
     359              : ! **************************************************************************************************
     360              : !> \brief ...
     361              : !> \param particle_set ...
     362              : !> \param status ...
     363              : !> \return ...
     364              : ! **************************************************************************************************
     365          268 :    FUNCTION gauxc_create_molecule(particle_set, status) RESULT(res)
     366              :       TYPE(particle_type), DIMENSION(:), INTENT(IN)      :: particle_set
     367              :       TYPE(cp_gauxc_status_type), INTENT(OUT)            :: status
     368              :       TYPE(cp_gauxc_molecule_type)                       :: res
     369              : 
     370              : #ifdef __GAUXC
     371              :       CHARACTER(LEN=2)                                   :: element_symbol
     372              :       INTEGER                                            :: atomic_number, i, natoms
     373              :       TYPE(atomic_kind_type), POINTER                    :: atomic_kind
     374          134 :       TYPE(gauxc_atom_type), ALLOCATABLE, DIMENSION(:)   :: atoms
     375              : 
     376          134 :       natoms = SIZE(particle_set)
     377          402 :       ALLOCATE (atoms(natoms))
     378              : 
     379          434 :       DO i = 1, natoms
     380          300 :          atomic_kind => particle_set(i)%atomic_kind
     381          300 :          CALL get_atomic_kind(atomic_kind, element_symbol=element_symbol)
     382          300 :          CALL get_ptable_info(element_symbol, number=atomic_number)
     383          300 :          atoms(i)%atomic_number = INT(atomic_number, c_int64_t)
     384          300 :          atoms(i)%x = REAL(particle_set(i)%r(1), c_double)
     385          300 :          atoms(i)%y = REAL(particle_set(i)%r(2), c_double)
     386          434 :          atoms(i)%z = REAL(particle_set(i)%r(3), c_double)
     387              :       END DO
     388              : 
     389          134 :       res%molecule = gauxc_molecule_new_from_atoms(status%status, atoms, INT(natoms, c_size_t))
     390          134 :       GAUXC_RETURN_IF_ERROR(status)
     391              : 
     392          134 :       DEALLOCATE (atoms)
     393              : #else
     394              :       MARK_USED(particle_set)
     395              :       MARK_USED(res)
     396              :       MARK_USED(status)
     397              :       CPABORT(no_gauxc_message)
     398              : #endif
     399          268 :    END FUNCTION gauxc_create_molecule
     400              : 
     401              : ! **************************************************************************************************
     402              : !> \brief ...
     403              : !> \param qs_kind_set ...
     404              : !> \param particle_set ...
     405              : !> \param status ...
     406              : !> \return ...
     407              : ! **************************************************************************************************
     408          268 :    FUNCTION gauxc_create_basisset(qs_kind_set, particle_set, status) RESULT(res)
     409              :       TYPE(qs_kind_type), DIMENSION(:), INTENT(IN), &
     410              :          POINTER                                         :: qs_kind_set
     411              :       TYPE(particle_type), DIMENSION(:), INTENT(IN)      :: particle_set
     412              :       TYPE(cp_gauxc_status_type), INTENT(OUT)            :: status
     413              :       TYPE(cp_gauxc_basisset_type)                       :: res
     414              : 
     415              : #ifdef __GAUXC
     416              :       INTEGER                                            :: iatom, ikind, iprim, iset, ishell, lval, &
     417              :                                                             nkind, npgf, nset, nshell, &
     418              :                                                             nshell_total, shell_index, natoms
     419              :       REAL(c_double), DIMENSION(3)                       :: shell_origin
     420              :       TYPE(atomic_kind_type), POINTER                    :: atomic_kind
     421          134 :       TYPE(gauxc_shell_type), ALLOCATABLE, DIMENSION(:)  :: shells
     422              :       TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER  :: basis_set_list
     423              :       TYPE(gto_basis_set_type), POINTER                  :: gto_basis
     424              : 
     425          134 :       nkind = SIZE(qs_kind_set)
     426          134 :       natoms = SIZE(particle_set)
     427              : 
     428          556 :       ALLOCATE (basis_set_list(nkind))
     429          134 :       CALL basis_set_list_setup(basis_set_list, "ORB", qs_kind_set)
     430              : 
     431          134 :       nshell_total = 0
     432          434 :       DO iatom = 1, natoms
     433          300 :          atomic_kind => particle_set(iatom)%atomic_kind
     434          300 :          CALL get_atomic_kind(atomic_kind, kind_number=ikind)
     435          300 :          gto_basis => basis_set_list(ikind)%gto_basis_set
     436          300 :          CPASSERT(ASSOCIATED(gto_basis))
     437              :          ! CALL write_gto_basis_set(gto_basis, 6, "–––––GAUXC-CREATE-BASISSET–––––")
     438         1330 :          nshell_total = nshell_total + SUM(gto_basis%nshell)
     439              :       END DO
     440              : 
     441         1362 :       ALLOCATE (shells(nshell_total))
     442              : 
     443          434 :       shell_index = 0
     444          434 :       res%max_l = -1
     445          434 :       DO iatom = 1, natoms ! for each atom
     446          300 :          atomic_kind => particle_set(iatom)%atomic_kind
     447          300 :          CALL get_atomic_kind(atomic_kind, kind_number=ikind)
     448          300 :          gto_basis => basis_set_list(ikind)%gto_basis_set
     449          300 :          CPASSERT(ASSOCIATED(gto_basis))
     450              : 
     451          300 :          shell_origin(1) = REAL(particle_set(iatom)%r(1), c_double)
     452          300 :          shell_origin(2) = REAL(particle_set(iatom)%r(2), c_double)
     453          300 :          shell_origin(3) = REAL(particle_set(iatom)%r(3), c_double)
     454              : 
     455          300 :          nset = gto_basis%nset
     456          300 :          CPASSERT(nset == SIZE(gto_basis%nshell))
     457         1330 :          DO iset = 1, nset ! for each shell group
     458          596 :             nshell = gto_basis%nshell(iset)
     459          596 :             npgf = gto_basis%npgf(iset) ! corresponds with nprim of gauxc
     460              : 
     461         1856 :             DO ishell = 1, gto_basis%nshell(iset) ! for each shell within the shell group
     462          960 :                shell_index = shell_index + 1 ! global shell index, flattened over atoms and groups
     463          960 :                lval = gto_basis%l(ishell, iset)
     464          960 :                res%max_l = MAX(res%max_l, lval)
     465          960 :                shells(shell_index)%l = INT(lval, c_int32_t)
     466              :                ! FIXME hardcoded true param
     467              :                ! pure=1: spherical Gaussians; pure=0: cartesian Gaussians
     468          960 :                shells(shell_index)%pure = .TRUE._c_bool
     469          960 :                shells(shell_index)%nprim = INT(npgf, c_int32_t)
     470         3840 :                shells(shell_index)%origin = shell_origin
     471              : 
     472         4572 :                DO iprim = 1, npgf
     473              :                   shells(shell_index)%exponents(iprim) = &
     474         3016 :                      REAL(gto_basis%zet(iprim, iset), c_double)
     475              :                   shells(shell_index)%coefficients(iprim) = &
     476              :                      REAL(gto_basis%norm_cgf(gto_basis%first_cgf(ishell, iset))* &
     477         3976 :                           gto_basis%gcc(iprim, ishell, iset), c_double)
     478              :                END DO
     479              :             END DO
     480              :          END DO
     481              :       END DO
     482              : 
     483              :       res%basis = gauxc_basisset_new_from_shells( &
     484              :                   status%status, &
     485              :                   shells, &
     486          134 :                   normalize=.FALSE.)
     487          134 :       GAUXC_RETURN_IF_ERROR(status)
     488              : 
     489          134 :       DEALLOCATE (shells)
     490          134 :       DEALLOCATE (basis_set_list)
     491              : 
     492              : #else
     493              :       MARK_USED(particle_set)
     494              :       MARK_USED(qs_kind_set)
     495              :       MARK_USED(res)
     496              :       MARK_USED(status)
     497              :       CPABORT(no_gauxc_message)
     498              : #endif
     499          268 :    END FUNCTION gauxc_create_basisset
     500              : 
     501              : ! **************************************************************************************************
     502              : !> \brief ...
     503              : !> \param molecule ...
     504              : !> \param basis ...
     505              : !> \param grid_type ...
     506              : !> \param radial_quadrature ...
     507              : !> \param pruning_scheme ...
     508              : !> \param lb_exec_space ...
     509              : !> \param batch_size ...
     510              : !> \param device_runtime_fill_fraction ...
     511              : !> \param status ...
     512              : !> \param mpi_comm optional communicator for a grid-local GauXC runtime
     513              : !> \param force_new_runtime force creation of a grid-local GauXC runtime
     514              : !> \param int_exec_space optional integrator execution space; a DEVICE
     515              : !>        integrator also requires a device runtime even when the load
     516              : !>        balancer stays on the host
     517              : !> \return ...
     518              : ! **************************************************************************************************
     519          140 :    FUNCTION gauxc_create_grid( &
     520              :       molecule, &
     521              :       basis, &
     522              :       grid_type, &
     523              :       radial_quadrature, &
     524              :       pruning_scheme, &
     525              :       lb_exec_space, &
     526              :       batch_size, &
     527              :       device_runtime_fill_fraction, &
     528              :       status, &
     529              :       mpi_comm, &
     530              :       force_new_runtime, &
     531          140 :       int_exec_space) RESULT(res)
     532              : 
     533              :       TYPE(cp_gauxc_molecule_type), INTENT(IN)           :: molecule
     534              :       TYPE(cp_gauxc_basisset_type), INTENT(in)           :: basis
     535              :       CHARACTER(len=*)               :: grid_type, lb_exec_space, &
     536              :                                         pruning_scheme, radial_quadrature
     537              :       INTEGER                                            :: batch_size
     538              :       REAL(c_double), INTENT(IN)                         :: device_runtime_fill_fraction
     539              :       TYPE(cp_gauxc_status_type), INTENT(OUT)            :: status
     540              :       INTEGER, INTENT(IN), OPTIONAL                      :: mpi_comm
     541              :       LOGICAL, INTENT(IN), OPTIONAL                      :: force_new_runtime
     542              :       CHARACTER(len=*), INTENT(IN), OPTIONAL             :: int_exec_space
     543              :       TYPE(cp_gauxc_grid_type)                           :: res
     544              : 
     545              : #ifdef __GAUXC
     546              :       INTEGER(c_int)                                     :: grid_type_local, int_exec_space_local, &
     547              :                                                             int_exec_requested, &
     548              :                                                             lb_exec_space_local, &
     549              :                                                             pruning_scheme_local, radial_quad_local
     550              :       LOGICAL                                            :: force_new_runtime_local, use_device_runtime
     551              : 
     552          140 :       grid_type_local = read_atomic_grid_size(grid_type)
     553          140 :       radial_quad_local = read_radial_quad(radial_quadrature)
     554          140 :       pruning_scheme_local = read_pruning_scheme(pruning_scheme)
     555          140 :       lb_exec_space_local = read_execution_space(lb_exec_space)
     556          140 :       int_exec_requested = read_execution_space("host")
     557          140 :       IF (PRESENT(int_exec_space)) int_exec_requested = read_execution_space(int_exec_space)
     558              :       use_device_runtime = (lb_exec_space_local == gauxc_executionspace%device) .OR. &
     559          140 :                            (int_exec_requested == gauxc_executionspace%device)
     560          140 :       IF (use_device_runtime) THEN
     561              :          int_exec_space_local = gauxc_executionspace%device
     562              :       ELSE
     563          140 :          int_exec_space_local = gauxc_executionspace%host
     564              :       END IF
     565              : #if defined(GAUXC_HAS_DEVICE) && defined(__LIBTORCH)
     566              :       IF (use_device_runtime) THEN
     567              :          IF (.NOT. torch_cuda_is_available() .OR. torch_cuda_device_count() <= 0) THEN
     568              :             CALL cp_abort(__LOCATION__, &
     569              :                           "GPU execution was requested for GauXC, but no usable CUDA device was found. "// &
     570              :                           "Set GAUXC%USE_GPU HOST to force host-only execution.")
     571              :          END IF
     572              :       END IF
     573              : #endif
     574          140 :       force_new_runtime_local = .FALSE.
     575          140 :       IF (PRESENT(force_new_runtime)) force_new_runtime_local = force_new_runtime
     576          140 :       res%owns_rt = .FALSE.
     577          140 :       res%uses_device_runtime = .FALSE.
     578              : 
     579          140 :       IF (use_device_runtime) THEN
     580              : #ifdef GAUXC_HAS_DEVICE
     581              : #if defined(GAUXC_HAS_MPI) && defined(__parallel)
     582              :          IF (PRESENT(mpi_comm)) THEN
     583              :             res%rt = gauxc_device_runtime_environment_new( &
     584              :                      status%status, mpi_comm, device_runtime_fill_fraction)
     585              :          ELSE
     586              :             res%rt = gauxc_device_runtime_environment_new( &
     587              :                      status%status, device_runtime_fill_fraction)
     588              :          END IF
     589              : #else
     590              :          MARK_USED(mpi_comm)
     591              :          res%rt = gauxc_device_runtime_environment_new( &
     592              :                   status%status, device_runtime_fill_fraction)
     593              : #endif
     594              :          GAUXC_RETURN_IF_ERROR(status)
     595              :          res%owns_rt = .TRUE.
     596              :          res%uses_device_runtime = .TRUE.
     597              : #else
     598              :          MARK_USED(device_runtime_fill_fraction)
     599            0 :          CPABORT("GauXC was built without device runtime support")
     600              : #endif
     601              :       ELSE
     602              : #if defined(GAUXC_HAS_MPI) && defined(__parallel)
     603          140 :          IF (PRESENT(mpi_comm)) THEN
     604              :             ! Reuse the global runtime when the requested communicator matches
     605              :             ! the communicator used during gauxc_init.
     606          140 :             IF (force_new_runtime_local .OR. .NOT. rt_has_mpi_comm .OR. &
     607              :                 mpi_comm /= rt_mpi_comm) THEN
     608          116 :                res%rt = gauxc_runtime_environment_new(status%status, mpi_comm)
     609          116 :                GAUXC_RETURN_IF_ERROR(status)
     610              :                res%owns_rt = .TRUE.
     611              :             END IF
     612              :          END IF
     613              : #else
     614              :          MARK_USED(mpi_comm)
     615              :          MARK_USED(force_new_runtime)
     616              : #endif
     617              :       END IF
     618              : 
     619              :       res%grid = gauxc_molgrid_new_default( &
     620              :                  status%status, &
     621              :                  molecule%molecule, &
     622              :                  pruning_scheme_local, &
     623              :                  INT(batch_size, c_int64_t), &
     624              :                  radial_quad_local, &
     625          140 :                  grid_type_local)
     626          140 :       GAUXC_RETURN_IF_ERROR(status)
     627              : 
     628              :       res%lbf = gauxc_load_balancer_factory_new( &
     629              :                 status%status, &
     630          140 :                 lb_exec_space_local)
     631          140 :       GAUXC_RETURN_IF_ERROR(status)
     632              : 
     633          140 :       IF (res%owns_rt) THEN
     634              :          res%lb = gauxc_load_balancer_factory_get_instance( &
     635              :                   status%status, &
     636              :                   res%lbf, &
     637              :                   res%rt, &
     638              :                   molecule%molecule, &
     639              :                   res%grid, &
     640          116 :                   basis%basis)
     641              :       ELSE
     642              :          res%lb = gauxc_load_balancer_factory_get_instance( &
     643              :                   status%status, &
     644              :                   res%lbf, &
     645              :                   rt, &
     646              :                   molecule%molecule, &
     647              :                   res%grid, &
     648           24 :                   basis%basis)
     649              :       END IF
     650          140 :       GAUXC_RETURN_IF_ERROR(status)
     651              : 
     652              :       res%mwf = gauxc_molecular_weights_factory_new( &
     653              :                 status%status, &
     654          140 :                 int_exec_space_local)
     655          140 :       GAUXC_RETURN_IF_ERROR(status)
     656              : 
     657              :       res%mw = gauxc_get_instance( &
     658              :                status%status, &
     659          140 :                res%mwf)
     660          140 :       GAUXC_RETURN_IF_ERROR(status)
     661              : 
     662              :       CALL gauxc_molecular_weights_modify_weights( &
     663              :          status%status, &
     664              :          res%mw, &
     665          140 :          res%lb)
     666          140 :       GAUXC_RETURN_IF_ERROR(status)
     667              : 
     668              : #else
     669              :       MARK_USED(basis)
     670              :       MARK_USED(batch_size)
     671              :       MARK_USED(device_runtime_fill_fraction)
     672              :       MARK_USED(grid_type)
     673              :       MARK_USED(int_exec_space)
     674              :       MARK_USED(lb_exec_space)
     675              :       MARK_USED(mpi_comm)
     676              :       MARK_USED(force_new_runtime)
     677              :       MARK_USED(int_exec_space)
     678              :       MARK_USED(molecule)
     679              :       MARK_USED(pruning_scheme)
     680              :       MARK_USED(radial_quadrature)
     681              :       MARK_USED(res)
     682              :       MARK_USED(status)
     683              :       CPABORT(no_gauxc_message)
     684              : #endif
     685          280 :    END FUNCTION gauxc_create_grid
     686              : 
     687              : ! **************************************************************************************************
     688              : !> \brief ...
     689              : !> \param xc_functional_name ...
     690              : !> \param grid ...
     691              : !> \param int_exec_space ...
     692              : !> \param lwd_kernel ...
     693              : !> \param nspins ...
     694              : !> \param status ...
     695              : !> \return ...
     696              : ! **************************************************************************************************
     697          140 :    FUNCTION gauxc_create_integrator( &
     698              :       xc_functional_name, &
     699              :       grid, &
     700              :       int_exec_space, &
     701              :       lwd_kernel, &
     702              :       nspins, &
     703          140 :       status) RESULT(res)
     704              : 
     705              :       CHARACTER(len=*), INTENT(IN)                       :: xc_functional_name, int_exec_space, &
     706              :                                                             lwd_kernel
     707              :       TYPE(cp_gauxc_grid_type), INTENT(IN)               :: grid
     708              :       INTEGER, INTENT(IN)                                :: nspins
     709              :       TYPE(cp_gauxc_status_type), INTENT(OUT)            :: status
     710              :       TYPE(cp_gauxc_integrator_type)                     :: res
     711              : 
     712              : #ifdef __GAUXC
     713              :       INTEGER(c_int)                                     :: int_exec_space_local
     714              :       LOGICAL(c_bool)                                    :: polarized
     715              : 
     716          140 :       polarized = (nspins == 2)
     717              :       res%func = gauxc_functional_from_string( &
     718              :                  status%status, &
     719              :                  xc_functional_name, &
     720          280 :                  polarized)
     721          140 :       GAUXC_RETURN_IF_ERROR(status)
     722              : 
     723          140 :       int_exec_space_local = read_execution_space(int_exec_space)
     724              :       res%integrator = gauxc_integrator_new( &
     725              :                        status%status, &
     726              :                        res%func, &
     727              :                        grid%lb, &
     728              :                        int_exec_space_local, &
     729          140 :                        local_work_kernel_name=TRIM(lwd_kernel))
     730          140 :       GAUXC_RETURN_IF_ERROR(status)
     731              : 
     732              : #else
     733              :       MARK_USED(grid)
     734              :       MARK_USED(int_exec_space)
     735              :       MARK_USED(lwd_kernel)
     736              :       MARK_USED(nspins)
     737              :       MARK_USED(res)
     738              :       MARK_USED(status)
     739              :       MARK_USED(xc_functional_name)
     740              :       CPABORT(no_gauxc_message)
     741              : #endif
     742          280 :    END FUNCTION gauxc_create_integrator
     743              : 
     744              : ! **************************************************************************************************
     745              : !> \brief ...
     746              : !> \param integrator ...
     747              : !> \param density_scalar ...
     748              : !> \param density_zeta ...
     749              : !> \param nspins ...
     750              : !> \param status ...
     751              : !> \param model ...
     752              : !> \return ...
     753              : ! **************************************************************************************************
     754         1460 :    FUNCTION gauxc_compute_xc( &
     755              :       integrator, &
     756         1460 :       density_scalar, &
     757          730 :       density_zeta, &
     758              :       nspins, &
     759              :       status, &
     760          730 :       model) RESULT(res)
     761              : 
     762              :       TYPE(cp_gauxc_integrator_type), INTENT(IN)         :: integrator
     763              :       REAL(c_double), DIMENSION(:, :), INTENT(IN)        :: density_scalar
     764              :       REAL(c_double), DIMENSION(:, :), INTENT(IN), &
     765              :          OPTIONAL                                        :: density_zeta
     766              :       INTEGER, INTENT(IN)                                :: nspins
     767              :       TYPE(cp_gauxc_status_type), INTENT(OUT)            :: status
     768              :       CHARACTER(len=*), INTENT(IN), OPTIONAL             :: model
     769              :       TYPE(cp_gauxc_xc_type)                             :: res
     770              : 
     771              : #ifdef __GAUXC
     772              :       CHARACTER(len=default_path_length)                 :: model_key
     773              :       LOGICAL                                            :: use_gauxc_model
     774          730 :       REAL(c_double), ALLOCATABLE, DIMENSION(:, :)       :: density_scalar_rks
     775              : #ifdef GAUXC_HAS_ONEDFT
     776          730 :       REAL(c_double), ALLOCATABLE, DIMENSION(:, :)       :: density_zeta_zero
     777              : #if defined (__HAS_IEEE_EXCEPTIONS)
     778              :       LOGICAL, DIMENSION(5)                              :: ieee_halt
     779              : #endif
     780              :       INTEGER                                            :: omp_max_threads_restore
     781              : #endif
     782              : 
     783          730 :       use_gauxc_model = .FALSE.
     784          730 :       IF (PRESENT(model)) THEN
     785          730 :          model_key = ADJUSTL(model)
     786          730 :          CALL uppercase(model_key)
     787          730 :          use_gauxc_model = (TRIM(model_key) /= "" .AND. TRIM(model_key) /= "NONE")
     788              :       END IF
     789              : 
     790          730 :       IF (.NOT. ALLOCATED(res%vxc_scalar)) THEN
     791         2920 :          ALLOCATE (res%vxc_scalar, mold=density_scalar)
     792              :       ELSE
     793            0 :          CPASSERT(ALL(SHAPE(res%vxc_scalar) == SHAPE(density_scalar)))
     794              :       END IF
     795       237462 :       res%vxc_scalar = 0._dp
     796              : 
     797          730 :       IF (use_gauxc_model) THEN
     798              : #ifndef GAUXC_HAS_ONEDFT
     799              :          CPABORT("GauXC lacks Skala support")
     800              : #else
     801              :          ! Skala may change the OpenMP team size for later parallel regions.
     802              :          ! Restore max threads only; omp_get_num_threads() is 1 here.
     803          220 :          omp_max_threads_restore = omp_get_max_threads()
     804              : #if defined (__HAS_IEEE_EXCEPTIONS)
     805              :          CALL ieee_get_halting_mode(IEEE_ALL, ieee_halt)
     806              :          CALL ieee_set_halting_mode(IEEE_ALL, .FALSE.)
     807              : #endif
     808          220 :          IF (.NOT. ALLOCATED(res%vxc_zeta)) THEN
     809          880 :             ALLOCATE (res%vxc_zeta, mold=density_scalar)
     810              :          ELSE
     811            0 :             CPASSERT(ALL(SHAPE(res%vxc_zeta) == SHAPE(density_scalar)))
     812              :          END IF
     813        49412 :          res%vxc_zeta = 0._dp
     814              : 
     815          220 :          IF (nspins == 1) THEN
     816          704 :             ALLOCATE (density_zeta_zero, mold=density_scalar)
     817          176 :             density_zeta_zero = 0._dp
     818              :             CALL gauxc_integrator_eval_exc_vxc_onedft_uks( &
     819              :                status%status, &
     820              :                integrator%integrator, &
     821              :                density_scalar, &
     822              :                density_zeta_zero, &
     823              :                TRIM(model), &
     824              :                res%exc, &
     825              :                res%vxc_scalar, &
     826        19616 :                res%vxc_zeta)
     827          176 :             DEALLOCATE (density_zeta_zero)
     828              :          ELSE
     829           44 :             CPASSERT(PRESENT(density_zeta))
     830              :             CALL gauxc_integrator_eval_exc_vxc_onedft_uks( &
     831              :                status%status, &
     832              :                integrator%integrator, &
     833              :                density_scalar, &
     834              :                density_zeta, &
     835              :                TRIM(model), &
     836              :                res%exc, &
     837              :                res%vxc_scalar, &
     838        59548 :                res%vxc_zeta)
     839              :          END IF
     840              : #if defined (__HAS_IEEE_EXCEPTIONS)
     841              :          CALL ieee_set_halting_mode(IEEE_ALL, ieee_halt)
     842              : #endif
     843          220 :          CALL omp_set_num_threads(omp_max_threads_restore)
     844          220 :          GAUXC_RETURN_IF_ERROR(status)
     845          220 :          RETURN
     846              : #endif
     847              :       END IF
     848              : 
     849          510 :       IF (nspins == 1) THEN
     850              :          ! xmat factor 2 is applied by both CP2K and GauXC
     851              :          ! "unapply" it here to even things back out.
     852              :          ! This is NOT necessary in the Skala branch.
     853         1744 :          ALLOCATE (density_scalar_rks, mold=density_scalar)
     854       134468 :          density_scalar_rks(:, :) = 0.5_dp*density_scalar
     855              :          CALL gauxc_integrator_eval_exc_vxc_rks( &
     856              :             status%status, &
     857              :             integrator%integrator, &
     858              :             density_scalar_rks, &
     859              :             res%exc, &
     860          436 :             res%vxc_scalar)
     861          436 :          DEALLOCATE (density_scalar_rks)
     862              :       ELSE
     863           74 :          CPASSERT(PRESENT(density_zeta))
     864              : 
     865           74 :          IF (.NOT. ALLOCATED(res%vxc_zeta)) THEN
     866          296 :             ALLOCATE (res%vxc_zeta, mold=density_zeta)
     867              :          ELSE
     868            0 :             CPASSERT(ALL(SHAPE(res%vxc_zeta) == SHAPE(density_scalar)))
     869              :          END IF
     870        53582 :          res%vxc_zeta = 0._dp
     871              : 
     872              :          CALL gauxc_integrator_eval_exc_vxc_uks( &
     873              :             status%status, &
     874              :             integrator%integrator, &
     875              :             density_scalar, &
     876              :             density_zeta, &
     877              :             res%exc, &
     878              :             res%vxc_scalar, &
     879       107090 :             res%vxc_zeta)
     880              :       END IF
     881          510 :       GAUXC_RETURN_IF_ERROR(status)
     882              : 
     883              : #else
     884              :       MARK_USED(integrator)
     885              :       MARK_USED(density_scalar)
     886              :       MARK_USED(density_zeta)
     887              :       MARK_USED(nspins)
     888              :       MARK_USED(status)
     889              :       MARK_USED(model)
     890              :       CPABORT(no_gauxc_message)
     891              : #endif
     892         1460 :    END FUNCTION gauxc_compute_xc
     893              : 
     894              : ! **************************************************************************************************
     895              : !> \brief ...
     896              : !> \param integrator ...
     897              : !> \param density_scalar ...
     898              : !> \param density_zeta ...
     899              : !> \param nspins ...
     900              : !> \param natom ...
     901              : !> \param status ...
     902              : !> \param model ...
     903              : !> \return ...
     904              : ! **************************************************************************************************
     905          108 :    FUNCTION gauxc_compute_xc_gradient( &
     906              :       integrator, &
     907          108 :       density_scalar, &
     908           54 :       density_zeta, &
     909              :       nspins, &
     910              :       natom, &
     911              :       status, &
     912           54 :       model) RESULT(res)
     913              : 
     914              :       TYPE(cp_gauxc_integrator_type), INTENT(IN)         :: integrator
     915              :       REAL(c_double), DIMENSION(:, :), INTENT(IN)        :: density_scalar
     916              :       REAL(c_double), DIMENSION(:, :), INTENT(IN), &
     917              :          OPTIONAL                                        :: density_zeta
     918              :       INTEGER, INTENT(IN)                                :: nspins, natom
     919              :       TYPE(cp_gauxc_status_type), INTENT(OUT)            :: status
     920              :       CHARACTER(len=*), INTENT(IN), OPTIONAL             :: model
     921              :       TYPE(cp_gauxc_xc_gradient_type)                    :: res
     922              : 
     923              : #ifdef __GAUXC
     924              :       CHARACTER(len=default_path_length)                 :: model_key
     925              :       LOGICAL                                            :: use_gauxc_model
     926           54 :       REAL(c_double), ALLOCATABLE, DIMENSION(:, :)       :: density_scalar_rks
     927              : #ifdef GAUXC_HAS_ONEDFT
     928           54 :       REAL(c_double), ALLOCATABLE, DIMENSION(:, :)       :: density_zeta_zero
     929              : #if defined (__HAS_IEEE_EXCEPTIONS)
     930              :       LOGICAL, DIMENSION(5)                              :: ieee_halt
     931              : #endif
     932              :       INTEGER                                            :: omp_max_threads_restore
     933              : #endif
     934              : 
     935          162 :       ALLOCATE (res%exc_grad(3*natom))
     936          390 :       res%exc_grad = 0._dp
     937              : 
     938           54 :       use_gauxc_model = .FALSE.
     939           54 :       IF (PRESENT(model)) THEN
     940           54 :          model_key = ADJUSTL(model)
     941           54 :          CALL uppercase(model_key)
     942           54 :          use_gauxc_model = (TRIM(model_key) /= "" .AND. TRIM(model_key) /= "NONE")
     943              :       END IF
     944              : 
     945              :       IF (use_gauxc_model) THEN
     946              : #ifndef GAUXC_HAS_ONEDFT
     947              :          CPABORT("GauXC lacks Skala support")
     948              : #else
     949              :          ! Skala may change the OpenMP team size for later parallel regions.
     950              :          ! Restore max threads only; omp_get_num_threads() is 1 here.
     951           48 :          omp_max_threads_restore = omp_get_max_threads()
     952              : #if defined (__HAS_IEEE_EXCEPTIONS)
     953              :          CALL ieee_get_halting_mode(IEEE_ALL, ieee_halt)
     954              :          CALL ieee_set_halting_mode(IEEE_ALL, .FALSE.)
     955              : #endif
     956           48 :          IF (nspins == 1) THEN
     957          192 :             ALLOCATE (density_zeta_zero, mold=density_scalar)
     958           48 :             density_zeta_zero = 0._dp
     959              :             CALL gauxc_integrator_eval_exc_grad_onedft_uks( &
     960              :                status%status, &
     961              :                integrator%integrator, &
     962              :                density_scalar, &
     963              :                density_zeta_zero, &
     964              :                TRIM(model), &
     965         5328 :                res%exc_grad)
     966           48 :             DEALLOCATE (density_zeta_zero)
     967              :          ELSE
     968            0 :             CPASSERT(PRESENT(density_zeta))
     969              :             CALL gauxc_integrator_eval_exc_grad_onedft_uks( &
     970              :                status%status, &
     971              :                integrator%integrator, &
     972              :                density_scalar, &
     973              :                density_zeta, &
     974              :                TRIM(model), &
     975            0 :                res%exc_grad)
     976              :          END IF
     977              : #if defined (__HAS_IEEE_EXCEPTIONS)
     978              :          CALL ieee_set_halting_mode(IEEE_ALL, ieee_halt)
     979              : #endif
     980           48 :          CALL omp_set_num_threads(omp_max_threads_restore)
     981           48 :          GAUXC_RETURN_IF_ERROR(status)
     982           48 :          RETURN
     983              : #endif
     984              :       END IF
     985              : 
     986            6 :       IF (nspins == 1) THEN
     987           24 :          ALLOCATE (density_scalar_rks, mold=density_scalar)
     988         2330 :          density_scalar_rks(:, :) = 0.5_dp*density_scalar
     989              :          CALL gauxc_integrator_eval_exc_grad_rks( &
     990              :             status%status, &
     991              :             integrator%integrator, &
     992              :             density_scalar_rks, &
     993            6 :             res%exc_grad)
     994            6 :          DEALLOCATE (density_scalar_rks)
     995              :       ELSE
     996            0 :          CPASSERT(PRESENT(density_zeta))
     997              :          CALL gauxc_integrator_eval_exc_grad_uks( &
     998              :             status%status, &
     999              :             integrator%integrator, &
    1000              :             density_scalar, &
    1001              :             density_zeta, &
    1002            0 :             res%exc_grad)
    1003              :       END IF
    1004            6 :       GAUXC_RETURN_IF_ERROR(status)
    1005              : 
    1006              : #else
    1007              :       MARK_USED(density_scalar)
    1008              :       MARK_USED(density_zeta)
    1009              :       MARK_USED(res)
    1010              :       MARK_USED(integrator)
    1011              :       MARK_USED(model)
    1012              :       MARK_USED(natom)
    1013              :       MARK_USED(nspins)
    1014              :       MARK_USED(status)
    1015              :       CPABORT(no_gauxc_message)
    1016              : #endif
    1017          108 :    END FUNCTION gauxc_compute_xc_gradient
    1018              : 
    1019              : ! **************************************************************************************************
    1020              : !> \brief ...
    1021              : !> \param molecule ...
    1022              : !> \param status ...
    1023              : ! **************************************************************************************************
    1024          134 :    SUBROUTINE gauxc_destroy_molecule(molecule, status)
    1025              :       TYPE(cp_gauxc_molecule_type), INTENT(INOUT)        :: molecule
    1026              :       TYPE(cp_gauxc_status_type), INTENT(OUT)            :: status
    1027              : 
    1028              : #ifdef __GAUXC
    1029          134 :       CALL gauxc_delete(status%status, molecule%molecule)
    1030          134 :       GAUXC_RETURN_IF_ERROR(status)
    1031              : #else
    1032              :       MARK_USED(molecule)
    1033              :       MARK_USED(status)
    1034              :       CPABORT(no_gauxc_message)
    1035              : #endif
    1036              :    END SUBROUTINE gauxc_destroy_molecule
    1037              : 
    1038              : ! **************************************************************************************************
    1039              : !> \brief ...
    1040              : !> \param basis ...
    1041              : !> \param status ...
    1042              : ! **************************************************************************************************
    1043          134 :    SUBROUTINE gauxc_destroy_basisset(basis, status)
    1044              :       TYPE(cp_gauxc_basisset_type), INTENT(INOUT)        :: basis
    1045              :       TYPE(cp_gauxc_status_type), INTENT(OUT)            :: status
    1046              : 
    1047              : #ifdef __GAUXC
    1048          134 :       CALL gauxc_delete(status%status, basis%basis)
    1049          134 :       GAUXC_RETURN_IF_ERROR(status)
    1050              : #else
    1051              :       MARK_USED(basis)
    1052              :       MARK_USED(status)
    1053              :       CPABORT(no_gauxc_message)
    1054              : #endif
    1055              :    END SUBROUTINE gauxc_destroy_basisset
    1056              : 
    1057              : ! **************************************************************************************************
    1058              : !> \brief ...
    1059              : !> \param grid_result ...
    1060              : !> \param status ...
    1061              : ! **************************************************************************************************
    1062          140 :    SUBROUTINE gauxc_destroy_grid(grid_result, status)
    1063              :       TYPE(cp_gauxc_grid_type), INTENT(INOUT)            :: grid_result
    1064              :       TYPE(cp_gauxc_status_type), INTENT(OUT)            :: status
    1065              : 
    1066              : #ifdef __GAUXC
    1067          140 :       CALL gauxc_delete(status%status, grid_result%mw)
    1068          140 :       GAUXC_RETURN_IF_ERROR(status)
    1069          140 :       CALL gauxc_delete(status%status, grid_result%mwf)
    1070          140 :       GAUXC_RETURN_IF_ERROR(status)
    1071          140 :       CALL gauxc_delete(status%status, grid_result%lb)
    1072          140 :       GAUXC_RETURN_IF_ERROR(status)
    1073          140 :       CALL gauxc_delete(status%status, grid_result%lbf)
    1074          140 :       GAUXC_RETURN_IF_ERROR(status)
    1075          140 :       CALL gauxc_delete(status%status, grid_result%grid)
    1076          140 :       GAUXC_RETURN_IF_ERROR(status)
    1077          140 :       IF (grid_result%owns_rt) THEN
    1078          116 :          CALL gauxc_runtime_environment_delete(status%status, grid_result%rt)
    1079          116 :          GAUXC_RETURN_IF_ERROR(status)
    1080          116 :          grid_result%owns_rt = .FALSE.
    1081              :       END IF
    1082              : #else
    1083              :       MARK_USED(grid_result)
    1084              :       MARK_USED(status)
    1085              :       CPABORT(no_gauxc_message)
    1086              : #endif
    1087              :    END SUBROUTINE gauxc_destroy_grid
    1088              : 
    1089              : ! **************************************************************************************************
    1090              : !> \brief ...
    1091              : !> \param integrator_result ...
    1092              : !> \param status ...
    1093              : ! **************************************************************************************************
    1094          140 :    SUBROUTINE gauxc_destroy_integrator(integrator_result, status)
    1095              :       TYPE(cp_gauxc_integrator_type), INTENT(INOUT)      :: integrator_result
    1096              :       TYPE(cp_gauxc_status_type), INTENT(OUT)            :: status
    1097              : 
    1098              : #ifdef __GAUXC
    1099          140 :       CALL gauxc_delete(status%status, integrator_result%integrator)
    1100          140 :       GAUXC_RETURN_IF_ERROR(status)
    1101          140 :       CALL gauxc_delete(status%status, integrator_result%func)
    1102          140 :       GAUXC_RETURN_IF_ERROR(status)
    1103              : #else
    1104              :       MARK_USED(integrator_result)
    1105              :       MARK_USED(status)
    1106              :       CPABORT(no_gauxc_message)
    1107              : #endif
    1108              :    END SUBROUTINE gauxc_destroy_integrator
    1109              : 
    1110              : ! **************************************************************************************************
    1111              : !> \brief Checks gauxc status and prints error message before aborting
    1112              : !> \param status the gauxc status to check
    1113              : ! **************************************************************************************************
    1114         5228 :    SUBROUTINE gauxc_check_status(status)
    1115              :       TYPE(cp_gauxc_status_type), INTENT(IN)                :: status
    1116              : 
    1117              : #ifdef __GAUXC
    1118         5228 :       IF (status%status%code /= 0) THEN
    1119            0 :          CALL print_gauxc_status_message(status)
    1120            0 :          CPABORT("GauXC returned with non-zero status code")
    1121              :       END IF
    1122              : #else
    1123              :       MARK_USED(status)
    1124              : #endif
    1125         5228 :    END SUBROUTINE gauxc_check_status
    1126              : 
    1127              : !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    1128              : ! From here on, it's private helpers !
    1129              : !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    1130              : 
    1131              : #ifdef __GAUXC
    1132              : 
    1133              : ! **************************************************************************************************
    1134              : !> \brief ...
    1135              : !> \param spec ...
    1136              : !> \return ...
    1137              : ! **************************************************************************************************
    1138          560 :    PURE FUNCTION read_execution_space(spec) RESULT(val)
    1139              :       CHARACTER(len=*), INTENT(IN)                       :: spec
    1140              :       INTEGER(c_int)                                     :: val
    1141              : 
    1142          560 :       CHARACTER(len=LEN(spec))                           :: spec_upper
    1143              : 
    1144          560 :       spec_upper = spec
    1145          560 :       CALL uppercase(spec_upper)
    1146              : 
    1147              :       SELECT CASE (spec_upper)
    1148              :       CASE ("HOST")
    1149            0 :          val = gauxc_executionspace%host
    1150              :       CASE ("DEVICE")
    1151            0 :          val = gauxc_executionspace%device
    1152              :       CASE DEFAULT
    1153          560 :          val = gauxc_executionspace%host
    1154              :       END SELECT
    1155          560 :    END FUNCTION read_execution_space
    1156              : 
    1157              : ! **************************************************************************************************
    1158              : !> \brief ...
    1159              : !> \param spec ...
    1160              : !> \return ...
    1161              : ! **************************************************************************************************
    1162          140 :    PURE FUNCTION read_atomic_grid_size(spec) RESULT(val)
    1163              :       CHARACTER(len=*), INTENT(IN)                       :: spec
    1164              :       INTEGER(c_int)                                     :: val
    1165              : 
    1166          140 :       CHARACTER(len=LEN(spec))                           :: spec_upper
    1167              : 
    1168          140 :       spec_upper = spec
    1169          140 :       CALL uppercase(spec_upper)
    1170              : 
    1171              :       SELECT CASE (spec_upper)
    1172              :       CASE ("FINE")
    1173            0 :          val = gauxc_atomicgridsizedefault%finegrid
    1174              :       CASE ("ULTRAFINE")
    1175            0 :          val = gauxc_atomicgridsizedefault%ultrafinegrid
    1176              :       CASE ("SUPERFINE")
    1177            0 :          val = gauxc_atomicgridsizedefault%superfinegrid
    1178              :       CASE ("GM3")
    1179            4 :          val = gauxc_atomicgridsizedefault%gm3
    1180              :       CASE ("GM5")
    1181            0 :          val = gauxc_atomicgridsizedefault%gm5
    1182              :       CASE DEFAULT
    1183          140 :          val = gauxc_atomicgridsizedefault%finegrid
    1184              :       END SELECT
    1185          140 :    END FUNCTION read_atomic_grid_size
    1186              : 
    1187              : ! **************************************************************************************************
    1188              : !> \brief ...
    1189              : !> \param spec ...
    1190              : !> \return ...
    1191              : ! **************************************************************************************************
    1192          140 :    PURE FUNCTION read_radial_quad(spec) RESULT(val)
    1193              :       CHARACTER(len=*), INTENT(IN)                       :: spec
    1194              :       INTEGER(c_int)                                     :: val
    1195              : 
    1196          140 :       CHARACTER(len=LEN(spec))                           :: spec_upper
    1197              : 
    1198          140 :       spec_upper = spec
    1199          140 :       CALL uppercase(spec_upper)
    1200              : 
    1201              :       SELECT CASE (spec_upper)
    1202              :       CASE ("BECKE")
    1203          140 :          val = gauxc_radialquad%becke
    1204              :       CASE ("MURAKNOWLES")
    1205          140 :          val = gauxc_radialquad%mura_knowles
    1206              :       CASE ("TREUTLERAHLRICHS")
    1207            0 :          val = gauxc_radialquad%treutler_ahlrichs
    1208              :       CASE ("MURRAYHANDYLAMING")
    1209            0 :          val = gauxc_radialquad%murray_handy_laming
    1210              :       CASE DEFAULT
    1211          140 :          val = gauxc_radialquad%mura_knowles
    1212              :       END SELECT
    1213          140 :    END FUNCTION read_radial_quad
    1214              : 
    1215              : ! **************************************************************************************************
    1216              : !> \brief ...
    1217              : !> \param spec ...
    1218              : !> \return ...
    1219              : ! **************************************************************************************************
    1220          140 :    PURE FUNCTION read_pruning_scheme(spec) RESULT(val)
    1221              :       CHARACTER(len=*), INTENT(IN)                       :: spec
    1222              :       INTEGER(c_int)                                     :: val
    1223              : 
    1224          140 :       CHARACTER(len=LEN(spec))                           :: spec_upper
    1225              : 
    1226          140 :       spec_upper = spec
    1227          140 :       CALL uppercase(spec_upper)
    1228              : 
    1229              :       SELECT CASE (spec_upper)
    1230              :       CASE ("UNPRUNED")
    1231          136 :          val = gauxc_pruningscheme%unpruned
    1232              :       CASE ("ROBUST")
    1233          136 :          val = gauxc_pruningscheme%robust
    1234              :       CASE ("TREUTLER")
    1235            0 :          val = gauxc_pruningscheme%treutler
    1236              :       CASE DEFAULT
    1237          140 :          val = gauxc_pruningscheme%robust
    1238              :       END SELECT
    1239          140 :    END FUNCTION read_pruning_scheme
    1240              : 
    1241              : #endif
    1242              : 
    1243              : ! **************************************************************************************************
    1244              : !> \brief Write molecule data to HDF5 file for debugging
    1245              : !> \param molecule ...
    1246              : !> \param output_path ...
    1247              : !> \param filename ...
    1248              : !> \param dataset ...
    1249              : !> \param status ...
    1250              : ! **************************************************************************************************
    1251            0 :    SUBROUTINE gauxc_write_molecule_hdf5(molecule, output_path, filename, dataset, status)
    1252              :       TYPE(cp_gauxc_molecule_type), INTENT(IN)           :: molecule
    1253              :       CHARACTER(len=*), INTENT(IN)                       :: output_path, filename, dataset
    1254              :       TYPE(cp_gauxc_status_type), INTENT(INOUT)          :: status
    1255              : 
    1256              : #if defined(__GAUXC) && defined(GAUXC_HAS_HDF5)
    1257              :       CHARACTER(len=default_path_length)                 :: full_path
    1258              : 
    1259              :       full_path = TRIM(output_path)//"/"//TRIM(filename)
    1260              :       CALL gauxc_write_hdf5_record(status%status, molecule%molecule, full_path, dataset)
    1261              : #else
    1262              :       MARK_USED(molecule)
    1263              :       MARK_USED(output_path)
    1264              :       MARK_USED(filename)
    1265              :       MARK_USED(dataset)
    1266              :       MARK_USED(status)
    1267            0 :       CPABORT("GauXC HDF5 output requires GauXC to be built with HDF5 support.")
    1268              : #endif
    1269            0 :    END SUBROUTINE gauxc_write_molecule_hdf5
    1270              : 
    1271              : ! **************************************************************************************************
    1272              : !> \brief Write basis set data to HDF5 file for debugging
    1273              : !> \param basis ...
    1274              : !> \param output_path ...
    1275              : !> \param filename ...
    1276              : !> \param dataset ...
    1277              : !> \param status ...
    1278              : ! **************************************************************************************************
    1279            0 :    SUBROUTINE gauxc_write_basisset_hdf5(basis, output_path, filename, dataset, status)
    1280              :       TYPE(cp_gauxc_basisset_type), INTENT(IN)           :: basis
    1281              :       CHARACTER(len=*), INTENT(IN)                       :: output_path, filename, dataset
    1282              :       TYPE(cp_gauxc_status_type), INTENT(INOUT)          :: status
    1283              : 
    1284              : #if defined(__GAUXC) && defined(GAUXC_HAS_HDF5)
    1285              :       CHARACTER(len=default_path_length)                 :: full_path
    1286              : 
    1287              :       full_path = TRIM(output_path)//"/"//TRIM(filename)
    1288              :       CALL gauxc_write_hdf5_record(status%status, basis%basis, full_path, dataset)
    1289              : #else
    1290              :       MARK_USED(basis)
    1291              :       MARK_USED(output_path)
    1292              :       MARK_USED(filename)
    1293              :       MARK_USED(dataset)
    1294              :       MARK_USED(status)
    1295            0 :       CPABORT("GauXC HDF5 output requires GauXC to be built with HDF5 support.")
    1296              : #endif
    1297            0 :    END SUBROUTINE gauxc_write_basisset_hdf5
    1298              : 
    1299            0 : END MODULE xc_gauxc_interface
        

Generated by: LCOV version 2.0-1