LCOV - code coverage report
Current view: top level - src - atom_kind_orbitals.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 90.8 % 714 648
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 6 6

            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              : ! **************************************************************************************************
       9              : !> \brief calculate the orbitals for a given atomic kind type
      10              : ! **************************************************************************************************
      11              : MODULE atom_kind_orbitals
      12              :    USE ai_onecenter,                    ONLY: sg_erfc
      13              :    USE atom_electronic_structure,       ONLY: calculate_atom
      14              :    USE atom_fit,                        ONLY: atom_fit_density
      15              :    USE atom_operators,                  ONLY: atom_int_release,&
      16              :                                               atom_int_setup,&
      17              :                                               atom_ppint_release,&
      18              :                                               atom_ppint_setup,&
      19              :                                               atom_relint_release,&
      20              :                                               atom_relint_setup
      21              :    USE atom_set_basis,                  ONLY: set_kind_basis_atomic
      22              :    USE atom_types,                      ONLY: &
      23              :         CGTO_BASIS, Clementi_geobas, GTO_BASIS, atom_basis_type, atom_ecppot_type, &
      24              :         atom_gthpot_type, atom_integrals, atom_optimization_type, atom_orbitals, &
      25              :         atom_potential_type, atom_sgppot_type, atom_type, create_atom_orbs, create_atom_type, &
      26              :         lmat, release_atom_basis, release_atom_potential, release_atom_type, set_atom
      27              :    USE atom_utils,                      ONLY: atom_density,&
      28              :                                               get_maxl_occ,&
      29              :                                               get_maxn_occ
      30              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      31              :                                               get_atomic_kind
      32              :    USE basis_set_types,                 ONLY: get_gto_basis_set,&
      33              :                                               gto_basis_set_type
      34              :    USE external_potential_types,        ONLY: all_potential_type,&
      35              :                                               get_potential,&
      36              :                                               gth_potential_type,&
      37              :                                               sgp_potential_type
      38              :    USE input_constants,                 ONLY: &
      39              :         barrier_conf, do_analytic, do_dkh0_atom, do_dkh1_atom, do_dkh2_atom, do_dkh3_atom, &
      40              :         do_gapw_log, do_nonrel_atom, do_numeric, do_rks_atom, do_sczoramp_atom, do_uks_atom, &
      41              :         do_zoramp_atom, ecp_pseudo, gth_pseudo, no_pseudo, poly_conf, rel_dkh, rel_none, &
      42              :         rel_sczora_mp, rel_zora, rel_zora_full, rel_zora_mp, sgp_pseudo, upf_pseudo
      43              :    USE input_section_types,             ONLY: section_vals_type
      44              :    USE kinds,                           ONLY: dp
      45              :    USE mathconstants,                   ONLY: dfac,&
      46              :                                               fourpi,&
      47              :                                               pi
      48              :    USE periodic_table,                  ONLY: ptable
      49              :    USE physcon,                         ONLY: bohr
      50              :    USE qs_grid_atom,                    ONLY: allocate_grid_atom,&
      51              :                                               create_grid_atom,&
      52              :                                               grid_atom_type
      53              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      54              :                                               init_atom_electronic_state,&
      55              :                                               qs_kind_type,&
      56              :                                               set_pseudo_state
      57              :    USE rel_control_types,               ONLY: rel_control_type
      58              : #include "./base/base_uses.f90"
      59              : 
      60              :    IMPLICIT NONE
      61              : 
      62              :    PRIVATE
      63              : 
      64              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'atom_kind_orbitals'
      65              : 
      66              :    ! Default values for optimization in an atomic guess
      67              :    ! Commented values are the defaults in create_optimization_section
      68              :    ! of input_cp2k_atom.F for the input section ATOM/OPTIMIZATION
      69              :    TYPE(atom_optimization_type), PARAMETER :: atom_optimization = atom_optimization_type( &
      70              :                                               damping=0.2_dp, &    ! 0.4_dp
      71              :                                               eps_scf=1.0E-6_dp, & ! 1.0E-6_dp
      72              :                                               eps_diis=100.0_dp, & ! 10000.0_dp
      73              :                                               max_iter=200, &      ! 200
      74              :                                               n_diis=5)            ! 5
      75              : 
      76              :    PUBLIC :: calculate_atomic_orbitals, calculate_atomic_density, &
      77              :              calculate_atomic_relkin, gth_potential_conversion, sgp_potential_conversion
      78              : 
      79              : ! **************************************************************************************************
      80              : 
      81              : CONTAINS
      82              : 
      83              : ! **************************************************************************************************
      84              : !> \brief ...
      85              : !> \param atomic_kind ...
      86              : !> \param qs_kind ...
      87              : !> \param agrid ...
      88              : !> \param iunit ...
      89              : !> \param pmat ...
      90              : !> \param fmat ...
      91              : !> \param density ...
      92              : !> \param wavefunction ...
      93              : !> \param wfninfo ...
      94              : !> \param confine ...
      95              : !> \param xc_section ...
      96              : !> \param nocc ...
      97              : !> \param which_l ...
      98              : !> \param which_n ...
      99              : !> \param proj_shell_charge ...
     100              : !> \param ao_coef ...
     101              : ! **************************************************************************************************
     102        33216 :    SUBROUTINE calculate_atomic_orbitals(atomic_kind, qs_kind, agrid, iunit, pmat, fmat, &
     103        11072 :                                         density, wavefunction, wfninfo, confine, xc_section, nocc, &
     104              :                                         which_l, which_n, proj_shell_charge, ao_coef)
     105              :       TYPE(atomic_kind_type), INTENT(IN)                 :: atomic_kind
     106              :       TYPE(qs_kind_type), INTENT(IN)                     :: qs_kind
     107              :       TYPE(grid_atom_type), OPTIONAL                     :: agrid
     108              :       INTEGER, INTENT(IN), OPTIONAL                      :: iunit
     109              :       REAL(KIND=dp), DIMENSION(:, :, :), OPTIONAL, &
     110              :          POINTER                                         :: pmat, fmat
     111              :       REAL(KIND=dp), DIMENSION(:), OPTIONAL, POINTER     :: density
     112              :       REAL(KIND=dp), DIMENSION(:, :), OPTIONAL, POINTER  :: wavefunction, wfninfo
     113              :       LOGICAL, INTENT(IN), OPTIONAL                      :: confine
     114              :       TYPE(section_vals_type), OPTIONAL, POINTER         :: xc_section
     115              :       INTEGER, DIMENSION(:), OPTIONAL                    :: nocc
     116              :       INTEGER, INTENT(IN), OPTIONAL                      :: which_l, which_n
     117              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: proj_shell_charge(4)
     118              :       REAL(KIND=dp), DIMENSION(:), INTENT(INOUT), &
     119              :          OPTIONAL, POINTER                               :: ao_coef
     120              : 
     121              :       INTEGER                                            :: i, ii, j, k, k1, k2, l, ll, m, mb, mo, &
     122              :                                                             nr, nset, nsgf, projector_index, &
     123              :                                                             valence_n, z
     124              :       INTEGER, DIMENSION(0:lmat)                         :: nbb
     125              :       INTEGER, DIMENSION(0:lmat, 10)                     :: ncalc, ncore, nelem
     126              :       INTEGER, DIMENSION(0:lmat, 100)                    :: set_index, shell_index
     127        11072 :       INTEGER, DIMENSION(:), POINTER                     :: nshell
     128        11072 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgf, ls
     129              :       LOGICAL                                            :: ecp_semi_local, ghost, has_pp, uks
     130              :       REAL(KIND=dp)                                      :: ok, qleft, scal, take, zeff
     131              :       REAL(KIND=dp), DIMENSION(0:lmat, 10)               :: occupation_neutral
     132              :       REAL(KIND=dp), DIMENSION(0:lmat, 10, 2)            :: edelta
     133              :       TYPE(all_potential_type), POINTER                  :: all_potential
     134              :       TYPE(atom_basis_type), POINTER                     :: basis
     135              :       TYPE(atom_integrals), POINTER                      :: integrals
     136              :       TYPE(atom_orbitals), POINTER                       :: orbitals
     137              :       TYPE(atom_potential_type), POINTER                 :: potential
     138              :       TYPE(atom_type), POINTER                           :: atom
     139              :       TYPE(gth_potential_type), POINTER                  :: gth_potential
     140              :       TYPE(gto_basis_set_type), POINTER                  :: orb_basis_set
     141              :       TYPE(sgp_potential_type), POINTER                  :: sgp_potential
     142              : 
     143              :       IF (PRESENT(ao_coef) .OR. PRESENT(which_l) .OR. &
     144        11016 :           PRESENT(which_n) .OR. PRESENT(proj_shell_charge)) THEN
     145              :          IF (.NOT. PRESENT(ao_coef) .OR. .NOT. PRESENT(which_l) .OR. &
     146           56 :              .NOT. PRESENT(which_n) .OR. .NOT. PRESENT(proj_shell_charge)) THEN
     147            0 :             CPABORT("ao_coef, which_l, which_n, and proj_shell_charge must be provided together")
     148              :          END IF
     149              :       END IF
     150              : 
     151        11072 :       NULLIFY (atom)
     152        11072 :       CALL create_atom_type(atom)
     153              : 
     154        11072 :       IF (PRESENT(xc_section)) THEN
     155            0 :          atom%xc_section => xc_section
     156              :       ELSE
     157        11072 :          NULLIFY (atom%xc_section)
     158              :       END IF
     159              : 
     160        11072 :       CALL get_atomic_kind(atomic_kind, z=z)
     161        11072 :       NULLIFY (all_potential, gth_potential, sgp_potential, orb_basis_set)
     162              :       CALL get_qs_kind(qs_kind, zeff=zeff, &
     163              :                        basis_set=orb_basis_set, &
     164              :                        ghost=ghost, &
     165              :                        all_potential=all_potential, &
     166              :                        gth_potential=gth_potential, &
     167        11072 :                        sgp_potential=sgp_potential)
     168              : 
     169        11072 :       has_pp = ASSOCIATED(gth_potential) .OR. ASSOCIATED(sgp_potential)
     170              : 
     171        11072 :       atom%z = z
     172              :       CALL set_atom(atom, &
     173              :                     pp_calc=has_pp, &
     174              :                     do_zmp=.FALSE., &
     175              :                     doread=.FALSE., &
     176              :                     read_vxc=.FALSE., &
     177              :                     relativistic=do_nonrel_atom, &
     178              :                     coulomb_integral_type=do_numeric, &
     179        11072 :                     exchange_integral_type=do_numeric)
     180              : 
     181     61903552 :       ALLOCATE (potential, integrals)
     182              : 
     183        11072 :       IF (PRESENT(confine)) THEN
     184            0 :          potential%confinement = confine
     185              :       ELSE
     186        11072 :          IF (ASSOCIATED(gth_potential) .OR. ASSOCIATED(sgp_potential)) THEN
     187         9518 :             potential%confinement = .TRUE.
     188              :          ELSE
     189         1554 :             potential%confinement = .FALSE.
     190              :          END IF
     191              :       END IF
     192        11072 :       potential%conf_type = poly_conf
     193        11072 :       potential%acon = 0.1_dp
     194        11072 :       potential%rcon = 2.0_dp*ptable(z)%vdw_radius*bohr
     195        11072 :       potential%scon = 2.0_dp
     196              : 
     197        11072 :       IF (ASSOCIATED(gth_potential)) THEN
     198         9412 :          potential%ppot_type = gth_pseudo
     199         9412 :          CALL get_potential(gth_potential, zeff=zeff)
     200         9412 :          CALL gth_potential_conversion(gth_potential, potential%gth_pot)
     201         9412 :          CALL set_atom(atom, zcore=NINT(zeff), potential=potential)
     202         1660 :       ELSE IF (ASSOCIATED(sgp_potential)) THEN
     203          106 :          CALL get_potential(sgp_potential, ecp_semi_local=ecp_semi_local)
     204          106 :          IF (ALLOCATED(sgp_potential%upf_data)) THEN
     205           24 :             potential%ppot_type = upf_pseudo
     206           24 :             potential%upf_pot = sgp_potential%upf_data
     207           82 :          ELSE IF (ecp_semi_local) THEN
     208           82 :             potential%ppot_type = ecp_pseudo
     209           82 :             CALL ecp_potential_conversion(sgp_potential, potential%ecp_pot)
     210           82 :             potential%ecp_pot%symbol = ptable(z)%symbol
     211              :          ELSE
     212            0 :             potential%ppot_type = sgp_pseudo
     213            0 :             CALL sgp_potential_conversion(sgp_potential, potential%sgp_pot)
     214            0 :             potential%sgp_pot%symbol = ptable(z)%symbol
     215              :          END IF
     216          106 :          CALL get_potential(sgp_potential, zeff=zeff)
     217          106 :          CALL set_atom(atom, zcore=NINT(zeff), potential=potential)
     218              :       ELSE
     219         1554 :          potential%ppot_type = no_pseudo
     220         1554 :          CALL set_atom(atom, zcore=z, potential=potential)
     221              :       END IF
     222              : 
     223              :       NULLIFY (basis)
     224       210368 :       ALLOCATE (basis)
     225              : 
     226        11072 :       CALL set_kind_basis_atomic(basis, orb_basis_set, has_pp, agrid)
     227              : 
     228        11072 :       CALL set_atom(atom, basis=basis)
     229              : 
     230              :       ! optimization defaults
     231        11072 :       atom%optimization = atom_optimization
     232              : 
     233              :       ! set up the electronic state
     234              :       CALL init_atom_electronic_state(atomic_kind=atomic_kind, &
     235              :                                       qs_kind=qs_kind, &
     236              :                                       ncalc=ncalc, &
     237              :                                       ncore=ncore, &
     238              :                                       nelem=nelem, &
     239              :                                       edelta=edelta, &
     240        11072 :                                       rks=PRESENT(ao_coef))
     241              : 
     242              :       ! restricted or unrestricted?
     243      1583296 :       IF (SUM(ABS(edelta)) > 0.0_dp) THEN
     244           82 :          uks = .TRUE.
     245           82 :          CALL set_atom(atom, method_type=do_uks_atom)
     246              :       ELSE
     247        10990 :          uks = .FALSE.
     248        10990 :          CALL set_atom(atom, method_type=do_rks_atom)
     249              :       END IF
     250              : 
     251      4019136 :       ALLOCATE (atom%state)
     252              : 
     253       786112 :       atom%state%core = 0._dp
     254       553600 :       atom%state%core(0:lmat, 1:7) = REAL(ncore(0:lmat, 1:7), dp)
     255       786112 :       atom%state%occ = 0._dp
     256        11072 :       IF (uks) THEN
     257              :          atom%state%occ(0:lmat, 1:7) = REAL(ncalc(0:lmat, 1:7), dp) + &
     258         4100 :                                        edelta(0:lmat, 1:7, 1) + edelta(0:lmat, 1:7, 2)
     259              :       ELSE
     260       549500 :          atom%state%occ(0:lmat, 1:7) = REAL(ncalc(0:lmat, 1:7), dp)
     261              :       END IF
     262       786112 :       atom%state%occupation = 0._dp
     263        77504 :       DO l = 0, lmat
     264              :          k = 0
     265       531456 :          DO i = 1, 7
     266       531456 :             IF (ncalc(l, i) > 0) THEN
     267        17551 :                k = k + 1
     268        17551 :                IF (uks) THEN
     269              :                   atom%state%occupation(l, k) = REAL(ncalc(l, i), dp) + &
     270          170 :                                                 edelta(l, i, 1) + edelta(l, i, 2)
     271          170 :                   atom%state%occa(l, k) = 0.5_dp*REAL(ncalc(l, i), dp) + edelta(l, i, 1)
     272          170 :                   atom%state%occb(l, k) = 0.5_dp*REAL(ncalc(l, i), dp) + edelta(l, i, 2)
     273              :                ELSE
     274        17381 :                   atom%state%occupation(l, k) = REAL(ncalc(l, i), dp)
     275              :                END IF
     276              :             END IF
     277              :          END DO
     278        66432 :          ok = REAL(2*l + 1, KIND=dp)
     279        77504 :          IF (uks) THEN
     280         3936 :             DO i = 1, 7
     281         3444 :                atom%state%occ(l, i) = MIN(atom%state%occ(l, i), 2.0_dp*ok)
     282         3444 :                atom%state%occa(l, i) = MIN(atom%state%occa(l, i), ok)
     283         3444 :                atom%state%occb(l, i) = MIN(atom%state%occb(l, i), ok)
     284         3936 :                atom%state%occupation(l, i) = atom%state%occa(l, i) + atom%state%occb(l, i)
     285              :             END DO
     286              :          ELSE
     287       527520 :             DO i = 1, 7
     288       461580 :                atom%state%occ(l, i) = MIN(atom%state%occ(l, i), 2.0_dp*ok)
     289       527520 :                atom%state%occupation(l, i) = MIN(atom%state%occupation(l, i), 2.0_dp*ok)
     290              :             END DO
     291              :          END IF
     292              :       END DO
     293              : 
     294        11072 :       IF (PRESENT(proj_shell_charge)) THEN
     295              :          ! Save the neutral/original calculated shell list.
     296         3976 :          occupation_neutral(0:lmat, 1:10) = atom%state%occupation(0:lmat, 1:10)
     297           56 :          IF (uks) THEN
     298              :             CALL cp_abort(__LOCATION__, "proj_shell_charge in"// &
     299            0 :                           " calculate_atomic_orbitals is implemented only for RKS")
     300              :          END IF
     301          280 :          DO l = 0, MIN(3, lmat)
     302          224 :             qleft = proj_shell_charge(l + 1)
     303          280 :             IF (qleft > 0.0_dp) THEN
     304              :                ! Remove electrons from the highest shell in this angular momentum channel.
     305          392 :                DO k = 7, 1, -1
     306          448 :                   IF (atom%state%occupation(l, k) > 0.0_dp) THEN
     307           56 :                      take = MIN(qleft, atom%state%occupation(l, k))
     308           56 :                      atom%state%occupation(l, k) = atom%state%occupation(l, k) - take
     309           56 :                      qleft = qleft - take
     310           56 :                      IF (qleft <= 1.0e-12_dp) EXIT
     311              :                   END IF
     312              :                END DO
     313           56 :                IF (qleft > 1.0e-10_dp) THEN
     314              :                   CALL cp_abort(__LOCATION__, "proj_shell_charge removes"// &
     315            0 :                                 " more electrons than available in this angular channel")
     316              :                END IF
     317              :                ! Keep atom%state%occ consistent with atom%state%occupation.
     318              :                qleft = proj_shell_charge(l + 1)
     319          392 :                DO k = 7, 1, -1
     320          448 :                   IF (atom%state%occ(l, k) > 0.0_dp) THEN
     321           56 :                      take = MIN(qleft, atom%state%occ(l, k))
     322           56 :                      atom%state%occ(l, k) = atom%state%occ(l, k) - take
     323           56 :                      qleft = qleft - take
     324           56 :                      IF (qleft <= 1.0e-12_dp) EXIT
     325              :                   END IF
     326              :                END DO
     327          168 :             ELSE IF (qleft < 0.0_dp) THEN
     328              :                CALL cp_abort(__LOCATION__, "Negative proj_shell_charge"// &
     329            0 :                              " is not supported in this implementation")
     330              :             END IF
     331              :          END DO
     332              :       END IF
     333              : 
     334        11072 :       IF (uks) THEN
     335         5822 :          atom%state%multiplicity = NINT(ABS(SUM(atom%state%occa - atom%state%occb)) + 1)
     336              :       ELSE
     337        10990 :          atom%state%multiplicity = -1
     338              :       END IF
     339              : 
     340        11072 :       atom%state%maxl_occ = get_maxl_occ(atom%state%occupation)
     341        77504 :       atom%state%maxn_occ = get_maxn_occ(atom%state%occupation)
     342        11072 :       IF (PRESENT(proj_shell_charge)) THEN
     343           56 :          atom%state%maxl_calc = get_maxl_occ(occupation_neutral)
     344          392 :          atom%state%maxn_calc = get_maxn_occ(occupation_neutral)
     345              :       ELSE
     346        11016 :          atom%state%maxl_calc = atom%state%maxl_occ
     347        77112 :          atom%state%maxn_calc = atom%state%maxn_occ
     348              :       END IF
     349              : 
     350              :       ! total number of occupied orbitals
     351        11072 :       IF (PRESENT(nocc) .AND. ghost) THEN
     352          450 :          nocc = 0
     353              :       ELSE IF (PRESENT(nocc)) THEN
     354        32364 :          nocc = 0
     355        75516 :          DO l = 0, lmat
     356       528612 :             DO k = 1, 7
     357       517824 :                IF (uks) THEN
     358         3444 :                   IF (atom%state%occa(l, k) > 0.0_dp) THEN
     359          140 :                      nocc(1) = nocc(1) + 2*l + 1
     360              :                   END IF
     361         3444 :                   IF (atom%state%occb(l, k) > 0.0_dp) THEN
     362          126 :                      nocc(2) = nocc(2) + 2*l + 1
     363              :                   END IF
     364              :                ELSE
     365       449652 :                   IF (atom%state%occupation(l, k) > 0.0_dp) THEN
     366        17225 :                      nocc(1) = nocc(1) + 2*l + 1
     367        17225 :                      nocc(2) = nocc(2) + 2*l + 1
     368              :                   END IF
     369              :                END IF
     370              :             END DO
     371              :          END DO
     372              :       END IF
     373              : 
     374              :       ! calculate integrals
     375              :       ! general integrals
     376              :       CALL atom_int_setup(integrals, basis, potential=atom%potential, &
     377              :                           eri_coulomb=(atom%coulomb_integral_type == do_analytic), &
     378        11072 :                           eri_exchange=(atom%exchange_integral_type == do_analytic))
     379              :       ! potential
     380        11072 :       CALL atom_ppint_setup(integrals, basis, potential=atom%potential)
     381              :       ! relativistic correction terms
     382        11072 :       NULLIFY (integrals%tzora, integrals%hdkh)
     383        11072 :       CALL atom_relint_setup(integrals, basis, atom%relativistic, zcore=REAL(atom%zcore, dp))
     384        11072 :       CALL set_atom(atom, integrals=integrals)
     385              : 
     386        11072 :       NULLIFY (orbitals)
     387        77504 :       mo = MAXVAL(atom%state%maxn_calc)
     388        77504 :       mb = MAXVAL(atom%basis%nbas)
     389        11072 :       CALL create_atom_orbs(orbitals, mb, mo)
     390        11072 :       CALL set_atom(atom, orbitals=orbitals)
     391              : 
     392        11072 :       IF (.NOT. ghost) THEN
     393        10922 :          IF (PRESENT(iunit)) THEN
     394        10858 :             CALL calculate_atom(atom, iunit)
     395              :          ELSE
     396           64 :             CALL calculate_atom(atom, -1)
     397              :          END IF
     398              :       END IF
     399        11072 :       IF (PRESENT(pmat)) THEN
     400              :          ! recover density matrix in CP2K/GPW order and normalization
     401              :          CALL get_gto_basis_set(orb_basis_set, &
     402        10938 :                                 nset=nset, nshell=nshell, l=ls, nsgf=nsgf, first_sgf=first_sgf)
     403        10938 :          set_index = 0
     404        10938 :          shell_index = 0
     405        10938 :          nbb = 0
     406        32960 :          DO i = 1, nset
     407        75392 :             DO j = 1, nshell(i)
     408        42432 :                l = ls(j, i)
     409        64454 :                IF (l <= lmat) THEN
     410        42432 :                   nbb(l) = nbb(l) + 1
     411        42432 :                   k = nbb(l)
     412        42432 :                   CPASSERT(k <= 100)
     413        42432 :                   set_index(l, k) = i
     414        42432 :                   shell_index(l, k) = j
     415              :                END IF
     416              :             END DO
     417              :          END DO
     418              : 
     419        10938 :          IF (ASSOCIATED(pmat)) THEN
     420            0 :             DEALLOCATE (pmat)
     421              :          END IF
     422        54678 :          ALLOCATE (pmat(nsgf, nsgf, 2))
     423      2937118 :          pmat = 0._dp
     424        21876 :          IF (.NOT. ghost) THEN
     425        75516 :             DO l = 0, lmat
     426        64728 :                ll = 2*l
     427       117390 :                DO k1 = 1, atom%basis%nbas(l)
     428       192894 :                   DO k2 = 1, atom%basis%nbas(l)
     429        86292 :                      scal = SQRT(atom%integrals%ovlp(k1, k1, l)*atom%integrals%ovlp(k2, k2, l))/REAL(2*l + 1, KIND=dp)
     430        86292 :                      i = first_sgf(shell_index(l, k1), set_index(l, k1))
     431        86292 :                      j = first_sgf(shell_index(l, k2), set_index(l, k2))
     432       128166 :                      IF (uks) THEN
     433         1912 :                         DO m = 0, ll
     434         1338 :                            pmat(i + m, j + m, 1) = atom%orbitals%pmata(k1, k2, l)*scal
     435         1912 :                            pmat(i + m, j + m, 2) = atom%orbitals%pmatb(k1, k2, l)*scal
     436              :                         END DO
     437              :                      ELSE
     438       258522 :                         DO m = 0, ll
     439       258522 :                            pmat(i + m, j + m, 1) = atom%orbitals%pmat(k1, k2, l)*scal
     440              :                         END DO
     441              :                      END IF
     442              :                   END DO
     443              :                END DO
     444              :             END DO
     445        10788 :             IF (uks) THEN
     446        14278 :                pmat(:, :, 1) = pmat(:, :, 1) + pmat(:, :, 2)
     447        14278 :                pmat(:, :, 2) = pmat(:, :, 1) - 2.0_dp*pmat(:, :, 2)
     448              :             END IF
     449              :          END IF
     450              :       END IF
     451              : 
     452        11072 :       IF (PRESENT(fmat)) THEN
     453              :          ! recover fock matrix in CP2K/GPW order.
     454              :          ! Caution: Normalization is not take care of, so it's probably weird.
     455              :          CALL get_gto_basis_set(orb_basis_set, &
     456           70 :                                 nset=nset, nshell=nshell, l=ls, nsgf=nsgf, first_sgf=first_sgf)
     457           70 :          set_index = 0
     458           70 :          shell_index = 0
     459           70 :          nbb = 0
     460          142 :          DO i = 1, nset
     461          366 :             DO j = 1, nshell(i)
     462          224 :                l = ls(j, i)
     463          296 :                IF (l <= lmat) THEN
     464          224 :                   nbb(l) = nbb(l) + 1
     465          224 :                   k = nbb(l)
     466          224 :                   CPASSERT(k <= 100)
     467          224 :                   set_index(l, k) = i
     468          224 :                   shell_index(l, k) = j
     469              :                END IF
     470              :             END DO
     471              :          END DO
     472           70 :          IF (uks) CPABORT("calculate_atomic_orbitals: only RKS is implemented")
     473           70 :          IF (ASSOCIATED(fmat)) CPABORT("fmat already associated")
     474           70 :          IF (.NOT. ASSOCIATED(atom%fmat)) CPABORT("atom%fmat not associated")
     475          280 :          ALLOCATE (fmat(nsgf, nsgf, 1))
     476         3176 :          fmat = 0.0_dp
     477          140 :          IF (.NOT. ghost) THEN
     478          490 :             DO l = 0, lmat
     479          420 :                ll = 2*l
     480          714 :                DO k1 = 1, atom%basis%nbas(l)
     481         1028 :                DO k2 = 1, atom%basis%nbas(l)
     482          384 :                   scal = SQRT(atom%integrals%ovlp(k1, k1, l)*atom%integrals%ovlp(k2, k2, l))
     483          384 :                   i = first_sgf(shell_index(l, k1), set_index(l, k1))
     484          384 :                   j = first_sgf(shell_index(l, k2), set_index(l, k2))
     485         1192 :                   DO m = 0, ll
     486          968 :                      fmat(i + m, j + m, 1) = atom%fmat%op(k1, k2, l)/scal
     487              :                   END DO
     488              :                END DO
     489              :                END DO
     490              :             END DO
     491              :          END IF
     492              :       END IF
     493              : 
     494        11072 :       nr = basis%grid%nr
     495              : 
     496        11072 :       IF (PRESENT(density)) THEN
     497            8 :          IF (ASSOCIATED(density)) DEALLOCATE (density)
     498           24 :          ALLOCATE (density(nr))
     499            8 :          IF (ghost) THEN
     500            0 :             density = 0.0_dp
     501              :          ELSE
     502            8 :             CALL atom_density(density, atom%orbitals%pmat, atom%basis, atom%state%maxl_occ)
     503              :          END IF
     504              :       END IF
     505              : 
     506        11072 :       IF (PRESENT(wavefunction)) THEN
     507            8 :          CPASSERT(PRESENT(wfninfo))
     508            8 :          IF (ASSOCIATED(wavefunction)) DEALLOCATE (wavefunction)
     509            8 :          IF (ASSOCIATED(wfninfo)) DEALLOCATE (wfninfo)
     510           56 :          mo = SUM(atom%state%maxn_occ)
     511           48 :          ALLOCATE (wavefunction(nr, mo), wfninfo(2, mo))
     512        18608 :          wavefunction = 0.0_dp
     513            8 :          IF (.NOT. ghost) THEN
     514              :             ii = 0
     515           56 :             DO l = 0, lmat
     516           76 :                DO i = 1, atom%state%maxn_occ(l)
     517           68 :                   IF (atom%state%occupation(l, i) > 0.0_dp) THEN
     518           20 :                      ii = ii + 1
     519           20 :                      wfninfo(1, ii) = atom%state%occupation(l, i)
     520           20 :                      wfninfo(2, ii) = REAL(l, dp)
     521          420 :                      DO j = 1, atom%basis%nbas(l)
     522              :                         wavefunction(:, ii) = wavefunction(:, ii) + &
     523       743620 :                                               atom%orbitals%wfn(j, i, l)*basis%bf(:, j, l)
     524              :                      END DO
     525              :                   END IF
     526              :                END DO
     527              :             END DO
     528            8 :             CPASSERT(mo == ii)
     529              :          END IF
     530              :       END IF
     531              : 
     532        11072 :       IF (PRESENT(ao_coef)) THEN
     533           56 :          CPASSERT(PRESENT(which_l))
     534           56 :          CPASSERT(PRESENT(which_n))
     535           56 :          CPASSERT(which_l >= 0 .AND. which_l <= lmat)
     536          616 :          valence_n = COUNT(ncore(which_l, :) > 0) + which_l + 1
     537           56 :          projector_index = which_n - valence_n + 1
     538           56 :          IF (projector_index < 1 .OR. projector_index > SIZE(atom%orbitals%wfn, 2)) THEN
     539              :             CALL cp_abort(__LOCATION__, &
     540              :                           "The requested tensorial atomic projector N is outside "// &
     541            0 :                           "the available orbital range for the selected L.")
     542              :          END IF
     543           56 :          IF (ASSOCIATED(ao_coef)) DEALLOCATE (ao_coef)
     544          168 :          ALLOCATE (ao_coef(SIZE(atom%orbitals%wfn(:, 1, which_l))))
     545          280 :          ao_coef(:) = atom%orbitals%wfn(:, projector_index, which_l)
     546          168 :          IF (MAXVAL(ABS(ao_coef)) <= 100.0_dp*EPSILON(1.0_dp)) THEN
     547              :             CALL cp_abort(__LOCATION__, &
     548              :                           "The requested atomic projector is zero. Check the "// &
     549            0 :                           "specified N and L quantum numbers.")
     550              :          END IF
     551          168 :          DO j = 1, SIZE(ao_coef(:))
     552          168 :             ao_coef(j) = ao_coef(j)*SQRT(atom%integrals%ovlp(j, j, which_l))
     553              :          END DO
     554              :       END IF
     555              : 
     556              :       ! clean up
     557        11072 :       CALL atom_int_release(integrals)
     558        11072 :       CALL atom_ppint_release(integrals)
     559        11072 :       CALL atom_relint_release(integrals)
     560        11072 :       CALL release_atom_basis(basis)
     561        11072 :       CALL release_atom_potential(potential)
     562        11072 :       CALL release_atom_type(atom)
     563              : 
     564        11072 :       DEALLOCATE (potential, basis, integrals)
     565              : 
     566        11072 :    END SUBROUTINE calculate_atomic_orbitals
     567              : 
     568              : ! **************************************************************************************************
     569              : !> \brief ...
     570              : !> \param density ...
     571              : !> \param atomic_kind ...
     572              : !> \param qs_kind ...
     573              : !> \param ngto ...
     574              : !> \param iunit ...
     575              : !> \param optbasis ... Default=T, if basis should be optimized, if not basis is given in input (density)
     576              : !> \param allelectron ...
     577              : !> \param confine ...
     578              : ! **************************************************************************************************
     579           66 :    SUBROUTINE calculate_atomic_density(density, atomic_kind, qs_kind, ngto, iunit, &
     580              :                                        optbasis, allelectron, confine)
     581              :       REAL(KIND=dp), DIMENSION(:, :), INTENT(INOUT)      :: density
     582              :       TYPE(atomic_kind_type), POINTER                    :: atomic_kind
     583              :       TYPE(qs_kind_type), POINTER                        :: qs_kind
     584              :       INTEGER, INTENT(IN)                                :: ngto
     585              :       INTEGER, INTENT(IN), OPTIONAL                      :: iunit
     586              :       LOGICAL, INTENT(IN), OPTIONAL                      :: optbasis, allelectron, confine
     587              : 
     588              :       INTEGER, PARAMETER                                 :: num_gto = 40
     589              : 
     590              :       INTEGER                                            :: i, ii, iw, k, l, ll, m, mb, mo, ngp, nn, &
     591              :                                                             nr, quadtype, relativistic, z
     592              :       INTEGER, DIMENSION(0:lmat)                         :: starti
     593              :       INTEGER, DIMENSION(0:lmat, 10)                     :: ncalc, ncore, nelem
     594           66 :       INTEGER, DIMENSION(:), POINTER                     :: econf
     595              :       LOGICAL                                            :: do_basopt, ecp_semi_local, monovalent
     596              :       REAL(KIND=dp)                                      :: al, aval, cc, cval, ear, rk, xx, zeff
     597              :       REAL(KIND=dp), DIMENSION(num_gto+2)                :: results
     598              :       TYPE(all_potential_type), POINTER                  :: all_potential
     599              :       TYPE(atom_basis_type), POINTER                     :: basis
     600              :       TYPE(atom_integrals), POINTER                      :: integrals
     601              :       TYPE(atom_orbitals), POINTER                       :: orbitals
     602              :       TYPE(atom_potential_type), POINTER                 :: potential
     603              :       TYPE(atom_type), POINTER                           :: atom
     604              :       TYPE(grid_atom_type), POINTER                      :: grid
     605              :       TYPE(gth_potential_type), POINTER                  :: gth_potential
     606              :       TYPE(sgp_potential_type), POINTER                  :: sgp_potential
     607              : 
     608           66 :       NULLIFY (atom)
     609           66 :       CALL create_atom_type(atom)
     610              : 
     611           66 :       CALL get_atomic_kind(atomic_kind, z=z)
     612           66 :       NULLIFY (all_potential, gth_potential)
     613              :       CALL get_qs_kind(qs_kind, zeff=zeff, &
     614              :                        all_potential=all_potential, &
     615              :                        gth_potential=gth_potential, &
     616              :                        sgp_potential=sgp_potential, &
     617           66 :                        monovalent=monovalent)
     618              : 
     619           66 :       IF (PRESENT(iunit)) THEN
     620            8 :          iw = iunit
     621              :       ELSE
     622           58 :          iw = -1
     623              :       END IF
     624              : 
     625           66 :       IF (PRESENT(allelectron)) THEN
     626            4 :          IF (allelectron) THEN
     627            4 :             NULLIFY (gth_potential)
     628            4 :             zeff = z
     629              :          END IF
     630              :       END IF
     631              : 
     632           66 :       do_basopt = .TRUE.
     633           66 :       IF (PRESENT(optbasis)) THEN
     634           22 :          do_basopt = optbasis
     635              :       END IF
     636              : 
     637           66 :       CPASSERT(ngto <= num_gto)
     638              : 
     639           66 :       IF (ASSOCIATED(gth_potential) .OR. ASSOCIATED(sgp_potential)) THEN
     640              :          ! PP calculation are non-relativistic
     641           60 :          relativistic = do_nonrel_atom
     642              :       ELSE
     643              :          ! AE calculations use DKH2
     644            6 :          relativistic = do_dkh2_atom
     645              :       END IF
     646              : 
     647           66 :       atom%z = z
     648              :       CALL set_atom(atom, &
     649              :                     pp_calc=(ASSOCIATED(gth_potential) .OR. ASSOCIATED(sgp_potential)), &
     650              :                     method_type=do_rks_atom, &
     651              :                     relativistic=relativistic, &
     652              :                     coulomb_integral_type=do_numeric, &
     653           72 :                     exchange_integral_type=do_numeric)
     654              : 
     655       370194 :       ALLOCATE (potential, basis, integrals)
     656              : 
     657           66 :       IF (PRESENT(confine)) THEN
     658           66 :          potential%confinement = confine
     659              :       ELSE
     660            0 :          IF (ASSOCIATED(gth_potential) .OR. ASSOCIATED(sgp_potential)) THEN
     661            0 :             potential%confinement = .TRUE.
     662              :          ELSE
     663            0 :             potential%confinement = .FALSE.
     664              :          END IF
     665              :       END IF
     666           66 :       potential%conf_type = barrier_conf
     667           66 :       potential%acon = 200._dp
     668           66 :       potential%rcon = 4.0_dp
     669           66 :       potential%scon = 8.0_dp
     670              : 
     671           66 :       IF (ASSOCIATED(gth_potential)) THEN
     672           60 :          potential%ppot_type = gth_pseudo
     673           60 :          CALL get_potential(gth_potential, zeff=zeff)
     674           60 :          CALL gth_potential_conversion(gth_potential, potential%gth_pot)
     675           60 :          CALL set_atom(atom, zcore=NINT(zeff), potential=potential)
     676            6 :       ELSE IF (ASSOCIATED(sgp_potential)) THEN
     677            0 :          CALL get_potential(sgp_potential, ecp_semi_local=ecp_semi_local)
     678            0 :          IF (ALLOCATED(sgp_potential%upf_data)) THEN
     679            0 :             potential%ppot_type = upf_pseudo
     680            0 :             potential%upf_pot = sgp_potential%upf_data
     681            0 :          ELSE IF (ecp_semi_local) THEN
     682            0 :             potential%ppot_type = ecp_pseudo
     683            0 :             CALL ecp_potential_conversion(sgp_potential, potential%ecp_pot)
     684            0 :             potential%ecp_pot%symbol = ptable(z)%symbol
     685              :          ELSE
     686            0 :             potential%ppot_type = sgp_pseudo
     687            0 :             CALL sgp_potential_conversion(sgp_potential, potential%sgp_pot)
     688            0 :             potential%sgp_pot%symbol = ptable(z)%symbol
     689              :          END IF
     690            0 :          CALL get_potential(sgp_potential, zeff=zeff)
     691            0 :          CALL set_atom(atom, zcore=NINT(zeff), potential=potential)
     692              :       ELSE
     693            6 :          potential%ppot_type = no_pseudo
     694            6 :          CALL set_atom(atom, zcore=z, potential=potential)
     695              :       END IF
     696              : 
     697              :       ! atomic grid
     698           66 :       NULLIFY (grid)
     699           66 :       ngp = 400
     700           66 :       quadtype = do_gapw_log
     701           66 :       CALL allocate_grid_atom(grid)
     702           66 :       CALL create_grid_atom(grid, ngp, 1, 1, 0, quadtype)
     703           66 :       grid%nr = ngp
     704           66 :       basis%grid => grid
     705              : 
     706           66 :       NULLIFY (basis%am, basis%cm, basis%as, basis%ns, basis%bf, basis%dbf, basis%ddbf)
     707              : 
     708              :       ! fill in the basis data structures
     709           66 :       basis%eps_eig = 1.e-12_dp
     710           66 :       basis%basis_type = GTO_BASIS
     711           66 :       CALL Clementi_geobas(z, cval, aval, basis%nbas, starti)
     712          462 :       basis%nprim = basis%nbas
     713          462 :       m = MAXVAL(basis%nbas)
     714          198 :       ALLOCATE (basis%am(m, 0:lmat))
     715         8886 :       basis%am = 0._dp
     716          462 :       DO l = 0, lmat
     717         2314 :          DO i = 1, basis%nbas(l)
     718         1852 :             ll = i - 1 + starti(l)
     719         2248 :             basis%am(i, l) = aval*cval**(ll)
     720              :          END DO
     721              :       END DO
     722              : 
     723           66 :       basis%geometrical = .TRUE.
     724           66 :       basis%aval = aval
     725           66 :       basis%cval = cval
     726          462 :       basis%start = starti
     727              : 
     728              :       ! initialize basis function on a radial grid
     729           66 :       nr = basis%grid%nr
     730          462 :       m = MAXVAL(basis%nbas)
     731          330 :       ALLOCATE (basis%bf(nr, m, 0:lmat))
     732          198 :       ALLOCATE (basis%dbf(nr, m, 0:lmat))
     733          198 :       ALLOCATE (basis%ddbf(nr, m, 0:lmat))
     734      3378486 :       basis%bf = 0._dp
     735      3378486 :       basis%dbf = 0._dp
     736      3378486 :       basis%ddbf = 0._dp
     737          462 :       DO l = 0, lmat
     738         2314 :          DO i = 1, basis%nbas(l)
     739         1852 :             al = basis%am(i, l)
     740       743048 :             DO k = 1, nr
     741       740800 :                rk = basis%grid%rad(k)
     742       740800 :                ear = EXP(-al*basis%grid%rad(k)**2)
     743       740800 :                basis%bf(k, i, l) = rk**l*ear
     744       740800 :                basis%dbf(k, i, l) = (REAL(l, dp)*rk**(l - 1) - 2._dp*al*rk**(l + 1))*ear
     745              :                basis%ddbf(k, i, l) = (REAL(l*(l - 1), dp)*rk**(l - 2) - &
     746       742652 :                                       2._dp*al*REAL(2*l + 1, dp)*rk**(l) + 4._dp*al**2*rk**(l + 2))*ear
     747              :             END DO
     748              :          END DO
     749              :       END DO
     750              : 
     751           66 :       CALL set_atom(atom, basis=basis)
     752              : 
     753              :       ! optimization defaults
     754           66 :       atom%optimization = atom_optimization
     755              : 
     756           66 :       nelem = 0
     757           66 :       ncore = 0
     758           66 :       ncalc = 0
     759           66 :       IF (monovalent) THEN
     760            0 :          ncalc(0, 1) = 1
     761            0 :          nelem(0, 1) = 1
     762           66 :       ELSE IF (ASSOCIATED(gth_potential)) THEN
     763           60 :          CALL get_potential(gth_potential, elec_conf=econf)
     764           60 :          CALL set_pseudo_state(econf, z, ncalc, ncore, nelem)
     765            6 :       ELSE IF (ASSOCIATED(sgp_potential)) THEN
     766            0 :          CALL get_potential(sgp_potential, elec_conf=econf)
     767            0 :          CALL set_pseudo_state(econf, z, ncalc, ncore, nelem)
     768              :       ELSE
     769           30 :          DO l = 0, MIN(lmat, UBOUND(ptable(z)%e_conv, 1))
     770           24 :             ll = 2*(2*l + 1)
     771           24 :             nn = ptable(z)%e_conv(l)
     772           24 :             ii = 0
     773            6 :             DO
     774           24 :                ii = ii + 1
     775           24 :                IF (nn <= ll) THEN
     776           24 :                   nelem(l, ii) = nn
     777              :                   EXIT
     778              :                ELSE
     779            0 :                   nelem(l, ii) = ll
     780            0 :                   nn = nn - ll
     781              :                END IF
     782              :             END DO
     783              :          END DO
     784          426 :          ncalc = nelem - ncore
     785              :       END IF
     786              : 
     787           66 :       IF (qs_kind%ghost .OR. qs_kind%floating) THEN
     788            0 :          nelem = 0
     789            0 :          ncore = 0
     790            0 :          ncalc = 0
     791              :       END IF
     792              : 
     793        23958 :       ALLOCATE (atom%state)
     794              : 
     795         4686 :       atom%state%core = 0._dp
     796         3300 :       atom%state%core(0:lmat, 1:7) = REAL(ncore(0:lmat, 1:7), dp)
     797         4686 :       atom%state%occ = 0._dp
     798         3300 :       atom%state%occ(0:lmat, 1:7) = REAL(ncalc(0:lmat, 1:7), dp)
     799         4686 :       atom%state%occupation = 0._dp
     800           66 :       atom%state%multiplicity = -1
     801          462 :       DO l = 0, lmat
     802              :          k = 0
     803         3234 :          DO i = 1, 7
     804         3168 :             IF (ncalc(l, i) > 0) THEN
     805           94 :                k = k + 1
     806           94 :                atom%state%occupation(l, k) = REAL(ncalc(l, i), dp)
     807              :             END IF
     808              :          END DO
     809              :       END DO
     810              : 
     811           66 :       atom%state%maxl_occ = get_maxl_occ(atom%state%occupation)
     812          462 :       atom%state%maxn_occ = get_maxn_occ(atom%state%occupation)
     813           66 :       atom%state%maxl_calc = atom%state%maxl_occ
     814          462 :       atom%state%maxn_calc = atom%state%maxn_occ
     815              : 
     816              :       ! calculate integrals
     817              :       ! general integrals
     818              :       CALL atom_int_setup(integrals, basis, potential=atom%potential, &
     819              :                           eri_coulomb=(atom%coulomb_integral_type == do_analytic), &
     820           66 :                           eri_exchange=(atom%exchange_integral_type == do_analytic))
     821              :       ! potential
     822           66 :       CALL atom_ppint_setup(integrals, basis, potential=atom%potential)
     823              :       ! relativistic correction terms
     824           66 :       NULLIFY (integrals%tzora, integrals%hdkh)
     825           66 :       CALL atom_relint_setup(integrals, basis, atom%relativistic, zcore=REAL(atom%zcore, dp))
     826           66 :       CALL set_atom(atom, integrals=integrals)
     827              : 
     828           66 :       NULLIFY (orbitals)
     829          462 :       mo = MAXVAL(atom%state%maxn_calc)
     830          462 :       mb = MAXVAL(atom%basis%nbas)
     831           66 :       CALL create_atom_orbs(orbitals, mb, mo)
     832           66 :       CALL set_atom(atom, orbitals=orbitals)
     833              : 
     834           66 :       CALL calculate_atom(atom, iw)
     835              : 
     836           66 :       IF (do_basopt) THEN
     837           44 :          CALL atom_fit_density(atom, ngto, 0, iw, results=results)
     838           44 :          xx = results(1)
     839           44 :          cc = results(2)
     840          428 :          DO i = 1, ngto
     841          384 :             density(i, 1) = xx*cc**i
     842          428 :             density(i, 2) = results(2 + i)
     843              :          END DO
     844              :       ELSE
     845           22 :          CALL atom_fit_density(atom, ngto, 0, iw, agto=density(:, 1), results=results)
     846          168 :          density(1:ngto, 2) = results(1:ngto)
     847              :       END IF
     848              : 
     849              :       ! clean up
     850           66 :       CALL atom_int_release(integrals)
     851           66 :       CALL atom_ppint_release(integrals)
     852           66 :       CALL atom_relint_release(integrals)
     853           66 :       CALL release_atom_basis(basis)
     854           66 :       CALL release_atom_potential(potential)
     855           66 :       CALL release_atom_type(atom)
     856              : 
     857           66 :       DEALLOCATE (potential, basis, integrals)
     858              : 
     859           66 :    END SUBROUTINE calculate_atomic_density
     860              : 
     861              : ! **************************************************************************************************
     862              : !> \brief ...
     863              : !> \param atomic_kind ...
     864              : !> \param qs_kind ...
     865              : !> \param rel_control ...
     866              : !> \param rtmat ...
     867              : ! **************************************************************************************************
     868           28 :    SUBROUTINE calculate_atomic_relkin(atomic_kind, qs_kind, rel_control, rtmat)
     869              :       TYPE(atomic_kind_type), INTENT(IN)                 :: atomic_kind
     870              :       TYPE(qs_kind_type), INTENT(IN)                     :: qs_kind
     871              :       TYPE(rel_control_type), POINTER                    :: rel_control
     872              :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: rtmat
     873              : 
     874              :       INTEGER                                            :: i, ii, ipgf, j, k, k1, k2, l, ll, m, n, &
     875              :                                                             ngp, nj, nn, nr, ns, nset, nsgf, &
     876              :                                                             quadtype, relativistic, z
     877              :       INTEGER, DIMENSION(0:lmat, 10)                     :: ncalc, ncore, nelem
     878              :       INTEGER, DIMENSION(0:lmat, 100)                    :: set_index, shell_index
     879           28 :       INTEGER, DIMENSION(:), POINTER                     :: lmax, lmin, npgf, nshell
     880           28 :       INTEGER, DIMENSION(:, :), POINTER                  :: first_sgf, last_sgf, ls
     881              :       REAL(KIND=dp)                                      :: al, alpha, ear, prefac, rk, zeff
     882           28 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: omat
     883           28 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: zet
     884           28 :       REAL(KIND=dp), DIMENSION(:, :, :), POINTER         :: gcc
     885              :       TYPE(all_potential_type), POINTER                  :: all_potential
     886              :       TYPE(atom_basis_type), POINTER                     :: basis
     887              :       TYPE(atom_integrals), POINTER                      :: integrals
     888              :       TYPE(atom_potential_type), POINTER                 :: potential
     889              :       TYPE(atom_type), POINTER                           :: atom
     890              :       TYPE(grid_atom_type), POINTER                      :: grid
     891              :       TYPE(gto_basis_set_type), POINTER                  :: orb_basis_set
     892              : 
     893           28 :       IF (rel_control%rel_method == rel_none) RETURN
     894              : 
     895           28 :       NULLIFY (all_potential, orb_basis_set)
     896           28 :       CALL get_qs_kind(qs_kind, basis_set=orb_basis_set, all_potential=all_potential)
     897              : 
     898           28 :       CPASSERT(ASSOCIATED(orb_basis_set))
     899              : 
     900           28 :       IF (ASSOCIATED(all_potential)) THEN
     901              :          ! only all electron atoms will get the relativistic correction
     902              : 
     903           28 :          CALL get_atomic_kind(atomic_kind, z=z)
     904           28 :          CALL get_qs_kind(qs_kind, zeff=zeff)
     905           28 :          NULLIFY (atom)
     906           28 :          CALL create_atom_type(atom)
     907           28 :          NULLIFY (atom%xc_section)
     908           28 :          NULLIFY (atom%orbitals)
     909           28 :          atom%z = z
     910           28 :          alpha = SQRT(all_potential%alpha_core_charge)
     911              : 
     912              :          ! set the method flag
     913           28 :          SELECT CASE (rel_control%rel_method)
     914              :          CASE DEFAULT
     915            0 :             CPABORT("Unknown relativistic method for calculate_atomic_relkin")
     916              :          CASE (rel_dkh)
     917           28 :             SELECT CASE (rel_control%rel_DKH_order)
     918              :             CASE DEFAULT
     919            0 :                CPABORT("Unknown DKH order for calculate_atomic_relkin")
     920              :             CASE (0)
     921            0 :                relativistic = do_dkh0_atom
     922              :             CASE (1)
     923            0 :                relativistic = do_dkh1_atom
     924              :             CASE (2)
     925            8 :                relativistic = do_dkh2_atom
     926              :             CASE (3)
     927           16 :                relativistic = do_dkh3_atom
     928              :             END SELECT
     929              :          CASE (rel_zora)
     930           28 :             SELECT CASE (rel_control%rel_zora_type)
     931              :             CASE DEFAULT
     932            0 :                CPABORT("Unknown ZORA type for calculate_atomic_relkin")
     933              :             CASE (rel_zora_full)
     934            0 :                CPABORT("ZORA full not yet implemented in calculate_atomic_relkin")
     935              :             CASE (rel_zora_mp)
     936            0 :                relativistic = do_zoramp_atom
     937              :             CASE (rel_sczora_mp)
     938           12 :                relativistic = do_sczoramp_atom
     939              :             END SELECT
     940              :          END SELECT
     941              : 
     942              :          CALL set_atom(atom, &
     943              :                        pp_calc=.FALSE., &
     944              :                        method_type=do_rks_atom, &
     945              :                        relativistic=relativistic, &
     946              :                        coulomb_integral_type=do_numeric, &
     947           28 :                        exchange_integral_type=do_numeric)
     948              : 
     949       157052 :          ALLOCATE (potential, basis, integrals)
     950              : 
     951           28 :          potential%ppot_type = no_pseudo
     952           28 :          CALL set_atom(atom, zcore=z, potential=potential)
     953              : 
     954              :          CALL get_gto_basis_set(orb_basis_set, &
     955              :                                 nset=nset, nshell=nshell, npgf=npgf, lmin=lmin, lmax=lmax, l=ls, nsgf=nsgf, zet=zet, gcc=gcc, &
     956           28 :                                 first_sgf=first_sgf, last_sgf=last_sgf)
     957              : 
     958           28 :          NULLIFY (grid)
     959           28 :          ngp = 400
     960           28 :          quadtype = do_gapw_log
     961           28 :          CALL allocate_grid_atom(grid)
     962           28 :          CALL create_grid_atom(grid, ngp, 1, 1, 0, quadtype)
     963           28 :          grid%nr = ngp
     964           28 :          basis%grid => grid
     965              : 
     966           28 :          NULLIFY (basis%am, basis%cm, basis%as, basis%ns, basis%bf, basis%dbf, basis%ddbf)
     967           28 :          basis%basis_type = CGTO_BASIS
     968           28 :          basis%eps_eig = 1.e-12_dp
     969              : 
     970              :          ! fill in the basis data structures
     971           28 :          set_index = 0
     972           28 :          shell_index = 0
     973          196 :          basis%nprim = 0
     974          196 :          basis%nbas = 0
     975          138 :          DO i = 1, nset
     976          220 :             DO j = lmin(i), MIN(lmax(i), lmat)
     977          220 :                basis%nprim(j) = basis%nprim(j) + npgf(i)
     978              :             END DO
     979          492 :             DO j = 1, nshell(i)
     980          354 :                l = ls(j, i)
     981          464 :                IF (l <= lmat) THEN
     982          354 :                   basis%nbas(l) = basis%nbas(l) + 1
     983          354 :                   k = basis%nbas(l)
     984          354 :                   CPASSERT(k <= 100)
     985          354 :                   set_index(l, k) = i
     986          354 :                   shell_index(l, k) = j
     987              :                END IF
     988              :             END DO
     989              :          END DO
     990              : 
     991          196 :          nj = MAXVAL(basis%nprim)
     992          196 :          ns = MAXVAL(basis%nbas)
     993           84 :          ALLOCATE (basis%am(nj, 0:lmat))
     994         2308 :          basis%am = 0._dp
     995          140 :          ALLOCATE (basis%cm(nj, ns, 0:lmat))
     996        18448 :          basis%cm = 0._dp
     997          196 :          DO j = 0, lmat
     998              :             nj = 0
     999              :             ns = 0
    1000          856 :             DO i = 1, nset
    1001          828 :                IF (j >= lmin(i) .AND. j <= lmax(i)) THEN
    1002          792 :                   DO ipgf = 1, npgf(i)
    1003          792 :                      basis%am(nj + ipgf, j) = zet(ipgf, i)
    1004              :                   END DO
    1005          464 :                   DO ii = 1, nshell(i)
    1006          464 :                      IF (ls(ii, i) == j) THEN
    1007          354 :                         ns = ns + 1
    1008         4204 :                         DO ipgf = 1, npgf(i)
    1009         4204 :                            basis%cm(nj + ipgf, ns, j) = gcc(ipgf, ii, i)
    1010              :                         END DO
    1011              :                      END IF
    1012              :                   END DO
    1013          110 :                   nj = nj + npgf(i)
    1014              :                END IF
    1015              :             END DO
    1016              :          END DO
    1017              : 
    1018              :          ! Normalization as used in the atomic code
    1019              :          ! We have to undo the Quickstep normalization
    1020          196 :          DO j = 0, lmat
    1021          168 :             prefac = 2.0_dp*SQRT(pi/dfac(2*j + 1))
    1022          878 :             DO ipgf = 1, basis%nprim(j)
    1023         6292 :                DO ii = 1, basis%nbas(j)
    1024         6124 :                   basis%cm(ipgf, ii, j) = prefac*basis%cm(ipgf, ii, j)
    1025              :                END DO
    1026              :             END DO
    1027              :          END DO
    1028              : 
    1029              :          ! initialize basis function on a radial grid
    1030           28 :          nr = basis%grid%nr
    1031          196 :          m = MAXVAL(basis%nbas)
    1032          140 :          ALLOCATE (basis%bf(nr, m, 0:lmat))
    1033           84 :          ALLOCATE (basis%dbf(nr, m, 0:lmat))
    1034           84 :          ALLOCATE (basis%ddbf(nr, m, 0:lmat))
    1035              : 
    1036       370720 :          basis%bf = 0._dp
    1037       370720 :          basis%dbf = 0._dp
    1038       370720 :          basis%ddbf = 0._dp
    1039          196 :          DO l = 0, lmat
    1040          878 :             DO i = 1, basis%nprim(l)
    1041          682 :                al = basis%am(i, l)
    1042       273650 :                DO k = 1, nr
    1043       272800 :                   rk = basis%grid%rad(k)
    1044       272800 :                   ear = EXP(-al*basis%grid%rad(k)**2)
    1045      2450282 :                   DO j = 1, basis%nbas(l)
    1046      2176800 :                      basis%bf(k, j, l) = basis%bf(k, j, l) + rk**l*ear*basis%cm(i, j, l)
    1047              :                      basis%dbf(k, j, l) = basis%dbf(k, j, l) &
    1048      2176800 :                                           + (REAL(l, dp)*rk**(l - 1) - 2._dp*al*rk**(l + 1))*ear*basis%cm(i, j, l)
    1049              :                      basis%ddbf(k, j, l) = basis%ddbf(k, j, l) + &
    1050              :                                            (REAL(l*(l - 1), dp)*rk**(l - 2) - 2._dp*al*REAL(2*l + 1, dp)* &
    1051      2449600 :                                             rk**(l) + 4._dp*al**2*rk**(l + 2))*ear*basis%cm(i, j, l)
    1052              :                   END DO
    1053              :                END DO
    1054              :             END DO
    1055              :          END DO
    1056              : 
    1057           28 :          CALL set_atom(atom, basis=basis)
    1058              : 
    1059              :          ! optimization defaults
    1060           28 :          atom%optimization = atom_optimization
    1061              : 
    1062              :          ! electronic state
    1063           28 :          nelem = 0
    1064           28 :          ncore = 0
    1065           28 :          ncalc = 0
    1066          140 :          DO l = 0, MIN(lmat, UBOUND(ptable(z)%e_conv, 1))
    1067          112 :             ll = 2*(2*l + 1)
    1068          112 :             nn = ptable(z)%e_conv(l)
    1069          112 :             ii = 0
    1070           28 :             DO
    1071          160 :                ii = ii + 1
    1072          160 :                IF (nn <= ll) THEN
    1073          112 :                   nelem(l, ii) = nn
    1074              :                   EXIT
    1075              :                ELSE
    1076           48 :                   nelem(l, ii) = ll
    1077           48 :                   nn = nn - ll
    1078              :                END IF
    1079              :             END DO
    1080              :          END DO
    1081         1988 :          ncalc = nelem - ncore
    1082              : 
    1083           28 :          IF (qs_kind%ghost .OR. qs_kind%floating) THEN
    1084              :             nelem = 0
    1085            0 :             ncore = 0
    1086            0 :             ncalc = 0
    1087              :          END IF
    1088              : 
    1089        10164 :          ALLOCATE (atom%state)
    1090              : 
    1091         1988 :          atom%state%core = 0._dp
    1092         1400 :          atom%state%core(0:lmat, 1:7) = REAL(ncore(0:lmat, 1:7), dp)
    1093         1988 :          atom%state%occ = 0._dp
    1094         1400 :          atom%state%occ(0:lmat, 1:7) = REAL(ncalc(0:lmat, 1:7), dp)
    1095         1988 :          atom%state%occupation = 0._dp
    1096           28 :          atom%state%multiplicity = -1
    1097          196 :          DO l = 0, lmat
    1098              :             k = 0
    1099         1372 :             DO i = 1, 7
    1100         1344 :                IF (ncalc(l, i) > 0) THEN
    1101           98 :                   k = k + 1
    1102           98 :                   atom%state%occupation(l, k) = REAL(ncalc(l, i), dp)
    1103              :                END IF
    1104              :             END DO
    1105              :          END DO
    1106              : 
    1107           28 :          atom%state%maxl_occ = get_maxl_occ(atom%state%occupation)
    1108          196 :          atom%state%maxn_occ = get_maxn_occ(atom%state%occupation)
    1109           28 :          atom%state%maxl_calc = atom%state%maxl_occ
    1110          196 :          atom%state%maxn_calc = atom%state%maxn_occ
    1111              : 
    1112              :          ! calculate integrals
    1113              :          ! general integrals
    1114           28 :          CALL atom_int_setup(integrals, basis)
    1115              :          ! potential
    1116           28 :          CALL atom_ppint_setup(integrals, basis, potential=atom%potential)
    1117              :          ! relativistic correction terms
    1118           28 :          NULLIFY (integrals%tzora, integrals%hdkh)
    1119              :          CALL atom_relint_setup(integrals, basis, atom%relativistic, zcore=REAL(atom%zcore, dp), &
    1120           28 :                                 alpha=alpha)
    1121           28 :          CALL set_atom(atom, integrals=integrals)
    1122              : 
    1123              :          ! for DKH we need erfc integrals to correct non-relativistic
    1124        13996 :          integrals%core = 0.0_dp
    1125          196 :          DO l = 0, lmat
    1126          168 :             n = integrals%n(l)
    1127          168 :             m = basis%nprim(l)
    1128          488 :             ALLOCATE (omat(m, m))
    1129              : 
    1130          168 :             CALL sg_erfc(omat(1:m, 1:m), l, alpha, basis%am(1:m, l), basis%am(1:m, l))
    1131          168 :             integrals%core(1:n, 1:n, l) = MATMUL(TRANSPOSE(basis%cm(1:m, 1:n, l)), &
    1132       432156 :                                                  MATMUL(omat(1:m, 1:m), basis%cm(1:m, 1:n, l)))
    1133              : 
    1134          196 :             DEALLOCATE (omat)
    1135              :          END DO
    1136              : 
    1137              :          ! recover relativistic kinetic matrix in CP2K/GPW order and normalization
    1138           28 :          IF (ASSOCIATED(rtmat)) THEN
    1139            0 :             DEALLOCATE (rtmat)
    1140              :          END IF
    1141          112 :          ALLOCATE (rtmat(nsgf, nsgf))
    1142       160300 :          rtmat = 0._dp
    1143          196 :          DO l = 0, lmat
    1144          168 :             ll = 2*l
    1145          550 :             DO k1 = 1, basis%nbas(l)
    1146         4872 :                DO k2 = 1, basis%nbas(l)
    1147         4350 :                   i = first_sgf(shell_index(l, k1), set_index(l, k1))
    1148         4350 :                   j = first_sgf(shell_index(l, k2), set_index(l, k2))
    1149          354 :                   SELECT CASE (atom%relativistic)
    1150              :                   CASE DEFAULT
    1151            0 :                      CPABORT("Unknown relativistic type for calculate_atomic_relkin")
    1152              :                   CASE (do_zoramp_atom, do_sczoramp_atom)
    1153        14656 :                      DO m = 0, ll
    1154        14656 :                         rtmat(i + m, j + m) = integrals%tzora(k1, k2, l)
    1155              :                      END DO
    1156              :                   CASE (do_dkh0_atom, do_dkh1_atom, do_dkh2_atom, do_dkh3_atom)
    1157         5046 :                      DO m = 0, ll
    1158              :                         rtmat(i + m, j + m) = integrals%hdkh(k1, k2, l) - integrals%kin(k1, k2, l) + &
    1159         1052 :                                               atom%zcore*integrals%core(k1, k2, l)
    1160              :                      END DO
    1161              :                   END SELECT
    1162              :                END DO
    1163              :             END DO
    1164              :          END DO
    1165         1006 :          DO k1 = 1, nsgf
    1166        81142 :             DO k2 = k1, nsgf
    1167        80136 :                rtmat(k1, k2) = 0.5_dp*(rtmat(k1, k2) + rtmat(k2, k1))
    1168        81114 :                rtmat(k2, k1) = rtmat(k1, k2)
    1169              :             END DO
    1170              :          END DO
    1171              : 
    1172              :          ! clean up
    1173           28 :          CALL atom_int_release(integrals)
    1174           28 :          CALL atom_ppint_release(integrals)
    1175           28 :          CALL atom_relint_release(integrals)
    1176           28 :          CALL release_atom_basis(basis)
    1177           28 :          CALL release_atom_potential(potential)
    1178           28 :          CALL release_atom_type(atom)
    1179              : 
    1180           84 :          DEALLOCATE (potential, basis, integrals)
    1181              : 
    1182              :       ELSE
    1183              : 
    1184            0 :          IF (ASSOCIATED(rtmat)) THEN
    1185            0 :             DEALLOCATE (rtmat)
    1186              :          END IF
    1187            0 :          NULLIFY (rtmat)
    1188              : 
    1189              :       END IF
    1190              : 
    1191           56 :    END SUBROUTINE calculate_atomic_relkin
    1192              : 
    1193              : ! **************************************************************************************************
    1194              : !> \brief ...
    1195              : !> \param gth_potential ...
    1196              : !> \param gth_atompot ...
    1197              : ! **************************************************************************************************
    1198        28440 :    SUBROUTINE gth_potential_conversion(gth_potential, gth_atompot)
    1199              :       TYPE(gth_potential_type), POINTER                  :: gth_potential
    1200              :       TYPE(atom_gthpot_type)                             :: gth_atompot
    1201              : 
    1202              :       INTEGER                                            :: i, j, l, lm, n, ne, nexp_lpot, nexp_lsd, &
    1203              :                                                             nexp_nlcc
    1204         9480 :       INTEGER, DIMENSION(:), POINTER                     :: nct_lpot, nct_lsd, nct_nlcc, nppnl, &
    1205         9480 :                                                             ppeconf
    1206              :       LOGICAL                                            :: lpot_present, lsd_present, nlcc_present, &
    1207              :                                                             soc_present
    1208              :       REAL(KIND=dp)                                      :: ac, zeff
    1209         9480 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: alpha_lpot, alpha_lsd, alpha_nlcc, ap, ce
    1210         9480 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: cval_lpot, cval_lsd, cval_nlcc
    1211         9480 :       REAL(KIND=dp), DIMENSION(:, :, :), POINTER         :: hp, kp
    1212              : 
    1213              :       CALL get_potential(gth_potential, &
    1214              :                          zeff=zeff, &
    1215              :                          elec_conf=ppeconf, &
    1216              :                          alpha_core_charge=ac, &
    1217              :                          nexp_ppl=ne, &
    1218              :                          cexp_ppl=ce, &
    1219              :                          lppnl=lm, &
    1220              :                          nprj_ppnl=nppnl, &
    1221              :                          alpha_ppnl=ap, &
    1222              :                          kprj_ppnl=kp, &
    1223         9480 :                          hprj_ppnl=hp)
    1224              : 
    1225         9480 :       gth_atompot%zion = zeff
    1226         9480 :       gth_atompot%rc = SQRT(0.5_dp/ac)
    1227         9480 :       gth_atompot%ncl = ne
    1228        56880 :       gth_atompot%cl(:) = 0._dp
    1229         9480 :       IF (ac > 0._dp) THEN
    1230        27916 :          DO i = 1, ne
    1231        27916 :             gth_atompot%cl(i) = ce(i)/(2._dp*ac)**(i - 1)
    1232              :          END DO
    1233              :       END IF
    1234              :       !extended type
    1235         9480 :       gth_atompot%lpotextended = .FALSE.
    1236         9480 :       gth_atompot%lsdpot = .FALSE.
    1237         9480 :       gth_atompot%nlcc = .FALSE.
    1238         9480 :       gth_atompot%nexp_lpot = 0
    1239         9480 :       gth_atompot%nexp_lsd = 0
    1240         9480 :       gth_atompot%nexp_nlcc = 0
    1241              :       CALL get_potential(gth_potential, &
    1242              :                          lpot_present=lpot_present, &
    1243              :                          lsd_present=lsd_present, &
    1244         9480 :                          nlcc_present=nlcc_present)
    1245         9480 :       IF (lpot_present) THEN
    1246              :          CALL get_potential(gth_potential, &
    1247              :                             nexp_lpot=nexp_lpot, &
    1248              :                             alpha_lpot=alpha_lpot, &
    1249              :                             nct_lpot=nct_lpot, &
    1250            8 :                             cval_lpot=cval_lpot)
    1251            8 :          gth_atompot%lpotextended = .TRUE.
    1252            8 :          gth_atompot%nexp_lpot = nexp_lpot
    1253           20 :          gth_atompot%alpha_lpot(1:nexp_lpot) = SQRT(0.5_dp/alpha_lpot(1:nexp_lpot))
    1254           20 :          gth_atompot%nct_lpot(1:nexp_lpot) = nct_lpot(1:nexp_lpot)
    1255           20 :          DO j = 1, nexp_lpot
    1256           12 :             ac = alpha_lpot(j)
    1257           68 :             DO i = 1, 4
    1258           60 :                gth_atompot%cval_lpot(i, j) = cval_lpot(i, j)/(2._dp*ac)**(i - 1)
    1259              :             END DO
    1260              :          END DO
    1261              :       END IF
    1262         9480 :       IF (lsd_present) THEN
    1263              :          CALL get_potential(gth_potential, &
    1264              :                             nexp_lsd=nexp_lsd, &
    1265              :                             alpha_lsd=alpha_lsd, &
    1266              :                             nct_lsd=nct_lsd, &
    1267            0 :                             cval_lsd=cval_lsd)
    1268            0 :          gth_atompot%lsdpot = .TRUE.
    1269            0 :          gth_atompot%nexp_lsd = nexp_lsd
    1270            0 :          gth_atompot%alpha_lsd(1:nexp_lsd) = SQRT(0.5_dp/alpha_lsd(1:nexp_lsd))
    1271            0 :          gth_atompot%nct_lsd(1:nexp_lsd) = nct_lsd(1:nexp_lsd)
    1272            0 :          DO j = 1, nexp_lpot
    1273            0 :             ac = alpha_lsd(j)
    1274            0 :             DO i = 1, 4
    1275            0 :                gth_atompot%cval_lsd(i, j) = cval_lsd(i, j)/(2._dp*ac)**(i - 1)
    1276              :             END DO
    1277              :          END DO
    1278              :       END IF
    1279              : 
    1280              :       ! nonlocal part
    1281        66360 :       gth_atompot%nl(:) = 0
    1282        66360 :       gth_atompot%rcnl(:) = 0._dp
    1283      1203960 :       gth_atompot%hnl(:, :, :) = 0._dp
    1284        18958 :       DO l = 0, lm
    1285         9478 :          n = nppnl(l)
    1286         9478 :          gth_atompot%nl(l) = n
    1287         9478 :          gth_atompot%rcnl(l) = SQRT(0.5_dp/ap(l))
    1288        35472 :          gth_atompot%hnl(1:n, 1:n, l) = hp(1:n, 1:n, l)
    1289              :       END DO
    1290              : 
    1291              :       ! SOC
    1292         9480 :       CALL get_potential(gth_potential, soc_present=soc_present)
    1293         9480 :       gth_atompot%soc = soc_present
    1294      1203960 :       gth_atompot%knl = 0.0_dp
    1295         9480 :       IF (soc_present) THEN
    1296          310 :          DO l = 1, lm
    1297          168 :             n = nppnl(l)
    1298          902 :             gth_atompot%knl(1:n, 1:n, l) = kp(1:n, 1:n, l)
    1299              :          END DO
    1300              :       END IF
    1301              : 
    1302         9480 :       IF (nlcc_present) THEN
    1303              :          CALL get_potential(gth_potential, &
    1304              :                             nexp_nlcc=nexp_nlcc, &
    1305              :                             alpha_nlcc=alpha_nlcc, &
    1306              :                             nct_nlcc=nct_nlcc, &
    1307           68 :                             cval_nlcc=cval_nlcc)
    1308           68 :          gth_atompot%nlcc = .TRUE.
    1309           68 :          gth_atompot%nexp_nlcc = nexp_nlcc
    1310          136 :          gth_atompot%alpha_nlcc(1:nexp_nlcc) = alpha_nlcc(1:nexp_nlcc)
    1311          136 :          gth_atompot%nct_nlcc(1:nexp_nlcc) = nct_nlcc(1:nexp_nlcc)
    1312          408 :          gth_atompot%cval_nlcc(1:4, 1:nexp_nlcc) = cval_nlcc(1:4, 1:nexp_nlcc)
    1313              :       END IF
    1314              : 
    1315         9480 :    END SUBROUTINE gth_potential_conversion
    1316              : 
    1317              : ! **************************************************************************************************
    1318              : !> \brief ...
    1319              : !> \param sgp_potential ...
    1320              : !> \param sgp_atompot ...
    1321              : ! **************************************************************************************************
    1322            6 :    SUBROUTINE sgp_potential_conversion(sgp_potential, sgp_atompot)
    1323              :       TYPE(sgp_potential_type), POINTER                  :: sgp_potential
    1324              :       TYPE(atom_sgppot_type)                             :: sgp_atompot
    1325              : 
    1326              :       INTEGER                                            :: lm, n
    1327            2 :       INTEGER, DIMENSION(:), POINTER                     :: ppeconf
    1328              :       LOGICAL                                            :: nlcc_present
    1329              :       REAL(KIND=dp)                                      :: ac, zeff
    1330            2 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: ap, ce
    1331            2 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: hhp
    1332            2 :       REAL(KIND=dp), DIMENSION(:, :, :), POINTER         :: ccp
    1333              : 
    1334              :       CALL get_potential(sgp_potential, &
    1335              :                          name=sgp_atompot%pname, &
    1336              :                          zeff=zeff, &
    1337              :                          elec_conf=ppeconf, &
    1338            2 :                          core_charge_radius=ac)
    1339            2 :       sgp_atompot%zion = zeff
    1340            2 :       sgp_atompot%ac_local = ac
    1341           10 :       sgp_atompot%econf(0:3) = ppeconf(0:3)
    1342              :       CALL get_potential(sgp_potential, lmax=lm, &
    1343              :                          is_nonlocal=sgp_atompot%is_nonlocal, &
    1344            2 :                          n_nonlocal=n, a_nonlocal=ap, h_nonlocal=hhp, c_nonlocal=ccp)
    1345              :       ! nonlocal
    1346            2 :       sgp_atompot%has_nonlocal = ANY(sgp_atompot%is_nonlocal)
    1347            2 :       sgp_atompot%lmax = lm
    1348            2 :       IF (sgp_atompot%has_nonlocal) THEN
    1349            2 :          CPASSERT(n <= SIZE(sgp_atompot%a_nonlocal))
    1350            2 :          sgp_atompot%n_nonlocal = n
    1351            4 :          sgp_atompot%a_nonlocal(1:n) = ap(1:n)
    1352           14 :          sgp_atompot%h_nonlocal(1:n, 0:lm) = hhp(1:n, 0:lm)
    1353              :          ! The atomic solver uses unit-normalized spherical harmonics and
    1354              :          ! radial integrals; undo the SGP storage factor 1/sqrt(4*pi).
    1355           20 :          sgp_atompot%c_nonlocal(1:n, 1:n, 0:lm) = SQRT(fourpi)*ccp(1:n, 1:n, 0:lm)
    1356              :       END IF
    1357              :       ! local
    1358            2 :       CALL get_potential(sgp_potential, n_local=n, a_local=ap, c_local=ce)
    1359            2 :       CPASSERT(n <= SIZE(sgp_atompot%a_local))
    1360            2 :       sgp_atompot%n_local = n
    1361            4 :       sgp_atompot%a_local(1:n) = ap(1:n)
    1362            4 :       sgp_atompot%c_local(1:n) = ce(1:n)
    1363              :       ! NLCC
    1364              :       CALL get_potential(sgp_potential, has_nlcc=nlcc_present, &
    1365            2 :                          n_nlcc=n, a_nlcc=ap, c_nlcc=ce)
    1366            2 :       IF (nlcc_present) THEN
    1367            0 :          sgp_atompot%has_nlcc = .TRUE.
    1368            0 :          CPASSERT(n <= SIZE(sgp_atompot%a_nlcc))
    1369            0 :          sgp_atompot%n_nlcc = n
    1370            0 :          sgp_atompot%a_nlcc(1:n) = ap(1:n)
    1371            0 :          sgp_atompot%c_nlcc(1:n) = ce(1:n)
    1372              :       ELSE
    1373            2 :          sgp_atompot%has_nlcc = .FALSE.
    1374              :       END IF
    1375              : 
    1376            2 :    END SUBROUTINE sgp_potential_conversion
    1377              : 
    1378              : ! **************************************************************************************************
    1379              : !> \brief ...
    1380              : !> \param sgp_potential ...
    1381              : !> \param ecp_atompot ...
    1382              : ! **************************************************************************************************
    1383          164 :    SUBROUTINE ecp_potential_conversion(sgp_potential, ecp_atompot)
    1384              :       TYPE(sgp_potential_type), POINTER                  :: sgp_potential
    1385              :       TYPE(atom_ecppot_type)                             :: ecp_atompot
    1386              : 
    1387           82 :       INTEGER, DIMENSION(:), POINTER                     :: ppeconf
    1388              :       LOGICAL                                            :: ecp_local, ecp_semi_local
    1389              :       REAL(KIND=dp)                                      :: zeff
    1390              : 
    1391           82 :       CALL get_potential(sgp_potential, ecp_local=ecp_local, ecp_semi_local=ecp_semi_local)
    1392           82 :       CPASSERT(ecp_semi_local .AND. ecp_local)
    1393              :       CALL get_potential(sgp_potential, &
    1394              :                          name=ecp_atompot%pname, &
    1395              :                          zeff=zeff, &
    1396           82 :                          elec_conf=ppeconf)
    1397           82 :       ecp_atompot%zion = zeff
    1398          410 :       ecp_atompot%econf(0:3) = ppeconf(0:3)
    1399           82 :       CALL get_potential(sgp_potential, sl_lmax=ecp_atompot%lmax)
    1400              :       ! local
    1401              :       CALL get_potential(sgp_potential, nloc=ecp_atompot%nloc, nrloc=ecp_atompot%nrloc, &
    1402           82 :                          aloc=ecp_atompot%aloc, bloc=ecp_atompot%bloc)
    1403              :       ! nonlocal
    1404              :       CALL get_potential(sgp_potential, npot=ecp_atompot%npot, nrpot=ecp_atompot%nrpot, &
    1405           82 :                          apot=ecp_atompot%apot, bpot=ecp_atompot%bpot)
    1406              : 
    1407           82 :    END SUBROUTINE ecp_potential_conversion
    1408              : ! **************************************************************************************************
    1409              : 
    1410          168 : END MODULE atom_kind_orbitals
        

Generated by: LCOV version 2.0-1