LCOV - code coverage report
Current view: top level - src - atom_fit.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 86.9 % 1130 982
Test Date: 2026-11-24 01:22:08 Functions: 94.1 % 17 16

            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 routines that fit parameters for /from atomic calculations
      10              : ! **************************************************************************************************
      11              : MODULE atom_fit
      12              :    USE atom_electronic_structure,       ONLY: calculate_atom
      13              :    USE atom_operators,                  ONLY: atom_int_release,&
      14              :                                               atom_int_setup,&
      15              :                                               atom_ppint_release,&
      16              :                                               atom_ppint_setup,&
      17              :                                               atom_relint_release,&
      18              :                                               atom_relint_setup
      19              :    USE atom_output,                     ONLY: atom_print_basis,&
      20              :                                               atom_print_basis_file,&
      21              :                                               atom_write_pseudo_param
      22              :    USE atom_types,                      ONLY: &
      23              :         GTO_BASIS, STO_BASIS, atom_basis_type, atom_gthpot_type, atom_integrals, atom_p_type, &
      24              :         atom_potential_type, atom_type, create_opgrid, create_opmat, lmat, opgrid_type, &
      25              :         opmat_type, release_opgrid, release_opmat, set_atom
      26              :    USE atom_utils,                      ONLY: &
      27              :         atom_completeness, atom_condnumber, atom_consistent_method, atom_core_density, &
      28              :         atom_denmat, atom_density, atom_orbital_charge, atom_orbital_max, atom_orbital_nodes, &
      29              :         atom_wfnr0, get_maxn_occ, integrate_grid
      30              :    USE cp_files,                        ONLY: close_file,&
      31              :                                               open_file
      32              :    USE input_constants,                 ONLY: do_analytic,&
      33              :                                               do_rhf_atom,&
      34              :                                               do_rks_atom,&
      35              :                                               do_rohf_atom,&
      36              :                                               do_uhf_atom,&
      37              :                                               do_uks_atom
      38              :    USE input_section_types,             ONLY: section_vals_type,&
      39              :                                               section_vals_val_get
      40              :    USE kinds,                           ONLY: dp
      41              :    USE mathconstants,                   ONLY: fac,&
      42              :                                               fourpi,&
      43              :                                               pi
      44              :    USE periodic_table,                  ONLY: ptable
      45              :    USE physcon,                         ONLY: bohr,&
      46              :                                               evolt
      47              :    USE powell,                          ONLY: opt_state_type,&
      48              :                                               powell_optimize
      49              :    USE qs_grid_atom,                    ONLY: grid_atom_type
      50              : #include "./base/base_uses.f90"
      51              : 
      52              :    IMPLICIT NONE
      53              : 
      54              :    PRIVATE
      55              : 
      56              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'atom_fit'
      57              : 
      58              :    PUBLIC :: atom_fit_density, atom_fit_basis, atom_fit_pseudo, atom_fit_kgpot
      59              : 
      60              :    TYPE wfn_init
      61              :       REAL(KIND=dp), DIMENSION(:, :, :), POINTER       :: wfn => NULL()
      62              :    END TYPE wfn_init
      63              : 
      64              : CONTAINS
      65              : 
      66              : ! **************************************************************************************************
      67              : !> \brief Fit the atomic electron density using a geometrical Gaussian basis set.
      68              : !> \param atom            information about the atomic kind
      69              : !> \param num_gto         number of Gaussian basis functions
      70              : !> \param norder ...
      71              : !> \param iunit           output file unit
      72              : !> \param agto            Gaussian exponents
      73              : !> \param powell_section  POWELL input section
      74              : !> \param results         (output) array of num_gto+2 real numbers in the following format:
      75              : !>                starting exponent, factor of geometrical series, expansion coefficients(1:num_gto)
      76              : ! **************************************************************************************************
      77           66 :    SUBROUTINE atom_fit_density(atom, num_gto, norder, iunit, agto, powell_section, results)
      78              :       TYPE(atom_type), POINTER                           :: atom
      79              :       INTEGER, INTENT(IN)                                :: num_gto, norder, iunit
      80              :       REAL(KIND=dp), DIMENSION(:), OPTIONAL              :: agto
      81              :       TYPE(section_vals_type), OPTIONAL, POINTER         :: powell_section
      82              :       REAL(KIND=dp), DIMENSION(:), OPTIONAL              :: results
      83              : 
      84              :       INTEGER                                            :: i, n10
      85              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: co, pe
      86              :       REAL(KIND=dp), DIMENSION(2)                        :: x
      87              :       TYPE(opgrid_type), POINTER                         :: density
      88              :       TYPE(opt_state_type)                               :: ostate
      89              : 
      90          264 :       ALLOCATE (co(num_gto), pe(num_gto))
      91           66 :       co = 0._dp
      92           66 :       pe = 0._dp
      93           66 :       NULLIFY (density)
      94           66 :       CALL create_opgrid(density, atom%basis%grid)
      95              :       CALL atom_denmat(atom%orbitals%pmat, atom%orbitals%wfn, atom%basis%nbas, atom%state%occupation, &
      96           66 :                        atom%state%maxl_occ, atom%state%maxn_occ)
      97              :       CALL atom_density(density%op, atom%orbitals%pmat, atom%basis, atom%state%maxl_occ, &
      98           66 :                         typ="RHO")
      99        26466 :       density%op = fourpi*density%op
     100           66 :       IF (norder /= 0) THEN
     101            0 :          density%op = density%op*atom%basis%grid%rad**norder
     102              :       END IF
     103              : 
     104           66 :       IF (PRESENT(agto)) THEN
     105           22 :          CPASSERT(num_gto <= SIZE(agto))
     106          168 :          pe(1:num_gto) = agto(1:num_gto)
     107              :       ELSE
     108           44 :          ostate%nf = 0
     109           44 :          ostate%nvar = 2
     110           44 :          x(1) = 0.10_dp !starting point of geometric series
     111           44 :          x(2) = 2.00_dp !factor of series
     112           44 :          IF (PRESENT(powell_section)) THEN
     113            0 :             CALL section_vals_val_get(powell_section, "ACCURACY", r_val=ostate%rhoend)
     114            0 :             CALL section_vals_val_get(powell_section, "STEP_SIZE", r_val=ostate%rhobeg)
     115            0 :             CALL section_vals_val_get(powell_section, "MAX_FUN", i_val=ostate%maxfun)
     116              :          ELSE
     117           44 :             ostate%rhoend = 1.e-8_dp
     118           44 :             ostate%rhobeg = 5.e-2_dp
     119           44 :             ostate%maxfun = 1000
     120              :          END IF
     121           44 :          ostate%iprint = 1
     122           44 :          ostate%unit = iunit
     123              : 
     124           44 :          ostate%state = 0
     125           44 :          IF (iunit > 0) THEN
     126            4 :             WRITE (iunit, '(/," POWELL| Start optimization procedure")')
     127              :          END IF
     128           44 :          n10 = ostate%maxfun/10
     129              : 
     130              :          DO
     131              : 
     132         2954 :             IF (ostate%state == 2) THEN
     133        51286 :                pe(1:num_gto) = [(x(1)*x(2)**i, i=1, num_gto)]
     134         2822 :                CALL density_fit(density%op, density%grid, num_gto, pe, co, ostate%f)
     135              :             END IF
     136              : 
     137         2954 :             IF (ostate%state == -1) EXIT
     138              : 
     139         2910 :             CALL powell_optimize(ostate%nvar, x, ostate)
     140              : 
     141         2954 :             IF (MOD(ostate%nf, n10) == 0 .AND. iunit > 0) THEN
     142              :                WRITE (iunit, '(" POWELL| Reached",i4,"% of maximal function calls")') &
     143            0 :                   INT(REAL(ostate%nf, dp)/REAL(ostate%maxfun, dp)*100._dp)
     144              :             END IF
     145              : 
     146              :          END DO
     147              : 
     148           44 :          ostate%state = 8
     149           44 :          CALL powell_optimize(ostate%nvar, x, ostate)
     150          812 :          pe(1:num_gto) = [(x(1)*x(2)**i, i=1, num_gto)]
     151              :       END IF
     152              : 
     153           66 :       CALL density_fit(density%op, density%grid, num_gto, pe, co, ostate%f)
     154              : 
     155           66 :       CALL release_opgrid(density)
     156              : 
     157           66 :       IF (iunit > 0 .AND. .NOT. PRESENT(agto)) THEN
     158            4 :          WRITE (iunit, '(" POWELL| Number of function evaluations",T71,I10)') ostate%nf
     159            4 :          WRITE (iunit, '(" POWELL| Final value of function",T61,G20.10)') ostate%fopt
     160            4 :          WRITE (iunit, '(" Optimized representation of density using a Geometrical Gaussian basis")')
     161            4 :          WRITE (iunit, '(A,I3,/,T10,A,F10.6,T48,A,F10.6)') " Number of Gaussians:", num_gto, &
     162            8 :             "Starting exponent:", x(1), "Proportionality factor:", x(2)
     163            4 :          WRITE (iunit, '(A)') " Expansion coefficients"
     164            4 :          WRITE (iunit, '(4F20.10)') co(1:num_gto)
     165              :       END IF
     166              : 
     167           66 :       IF (PRESENT(results)) THEN
     168           66 :          IF (PRESENT(agto)) THEN
     169           22 :             CPASSERT(SIZE(results) >= num_gto)
     170          168 :             results(1:num_gto) = co(1:num_gto)
     171              :          ELSE
     172           44 :             CPASSERT(SIZE(results) >= num_gto + 2)
     173           44 :             results(1) = x(1)
     174           44 :             results(2) = x(2)
     175          428 :             results(3:2 + num_gto) = co(1:num_gto)
     176              :          END IF
     177              :       END IF
     178              : 
     179           66 :       DEALLOCATE (co, pe)
     180              : 
     181           66 :    END SUBROUTINE atom_fit_density
     182              : ! **************************************************************************************************
     183              : !> \brief ...
     184              : !> \param atom_info ...
     185              : !> \param basis ...
     186              : !> \param pptype ...
     187              : !> \param iunit           output file unit
     188              : !> \param powell_section  POWELL input section
     189              : ! **************************************************************************************************
     190           12 :    SUBROUTINE atom_fit_basis(atom_info, basis, pptype, iunit, powell_section)
     191              :       TYPE(atom_p_type), DIMENSION(:, :), POINTER        :: atom_info
     192              :       TYPE(atom_basis_type), POINTER                     :: basis
     193              :       LOGICAL, INTENT(IN)                                :: pptype
     194              :       INTEGER, INTENT(IN)                                :: iunit
     195              :       TYPE(section_vals_type), POINTER                   :: powell_section
     196              : 
     197              :       INTEGER                                            :: i, j, k, l, ll, m, n, n10, zval
     198           12 :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: xtob
     199              :       LOGICAL                                            :: explicit, mult, penalty
     200              :       REAL(KIND=dp)                                      :: crad, fopt
     201           12 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: x
     202           12 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: wem
     203           12 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: w
     204              :       TYPE(opt_state_type)                               :: ostate
     205              : 
     206           12 :       penalty = .FALSE.
     207           12 :       SELECT CASE (basis%basis_type)
     208              :       CASE DEFAULT
     209            0 :          CPABORT("Unknown basis type for atom_fit_basis")
     210              :       CASE (GTO_BASIS)
     211            6 :          IF (basis%geometrical) THEN
     212            0 :             ostate%nvar = 2
     213            0 :             ALLOCATE (x(2))
     214            0 :             x(1) = SQRT(basis%aval)
     215            0 :             x(2) = SQRT(basis%cval)
     216              :          ELSE
     217           42 :             ll = MAXVAL(basis%nprim(:))
     218           18 :             ALLOCATE (xtob(ll, 0:lmat))
     219            6 :             xtob = 0
     220           42 :             ll = SUM(basis%nprim(:))
     221           18 :             ALLOCATE (x(ll))
     222            6 :             x = 0._dp
     223            6 :             ll = 0
     224           42 :             DO l = 0, lmat
     225          118 :                DO i = 1, basis%nprim(l)
     226              :                   mult = .FALSE.
     227          426 :                   DO k = 1, ll
     228          426 :                      IF (ABS(basis%am(i, l) - x(k)) < 1.e-6_dp) THEN
     229           48 :                         mult = .TRUE.
     230           48 :                         xtob(i, l) = k
     231              :                      END IF
     232              :                   END DO
     233          112 :                   IF (.NOT. mult) THEN
     234           28 :                      ll = ll + 1
     235           28 :                      x(ll) = basis%am(i, l)
     236           28 :                      xtob(i, l) = ll
     237              :                   END IF
     238              :                END DO
     239              :             END DO
     240            6 :             ostate%nvar = ll
     241           34 :             DO i = 1, ostate%nvar
     242           34 :                x(i) = SQRT(LOG(1._dp + x(i)))
     243              :             END DO
     244            6 :             penalty = .TRUE.
     245              :          END IF
     246              :       CASE (STO_BASIS)
     247           42 :          ll = MAXVAL(basis%nbas(:))
     248           18 :          ALLOCATE (xtob(ll, 0:lmat))
     249            6 :          xtob = 0
     250           42 :          ll = SUM(basis%nbas(:))
     251           18 :          ALLOCATE (x(ll))
     252            6 :          x = 0._dp
     253            6 :          ll = 0
     254           42 :          DO l = 0, lmat
     255           60 :             DO i = 1, basis%nbas(l)
     256           18 :                ll = ll + 1
     257           18 :                x(ll) = basis%as(i, l)
     258           54 :                xtob(i, l) = ll
     259              :             END DO
     260              :          END DO
     261            6 :          ostate%nvar = ll
     262           24 :          DO i = 1, ostate%nvar
     263           24 :             x(i) = SQRT(LOG(1._dp + x(i)))
     264              :          END DO
     265              :       END SELECT
     266              : 
     267           12 :       CALL section_vals_val_get(powell_section, "ACCURACY", r_val=ostate%rhoend)
     268           12 :       CALL section_vals_val_get(powell_section, "STEP_SIZE", r_val=ostate%rhobeg)
     269           12 :       CALL section_vals_val_get(powell_section, "MAX_FUN", i_val=ostate%maxfun)
     270              : 
     271           12 :       n = SIZE(atom_info, 1)
     272           12 :       m = SIZE(atom_info, 2)
     273           48 :       ALLOCATE (wem(n, m))
     274           36 :       wem = 1._dp
     275           12 :       CALL section_vals_val_get(powell_section, "WEIGHT_ELECTRON_CONFIGURATION", explicit=explicit)
     276           12 :       IF (explicit) THEN
     277            0 :          CALL section_vals_val_get(powell_section, "WEIGHT_ELECTRON_CONFIGURATION", r_vals=w)
     278            0 :          DO i = 1, MIN(SIZE(w), n)
     279            0 :             wem(i, :) = w(i)*wem(i, :)
     280              :          END DO
     281              :       END IF
     282           12 :       CALL section_vals_val_get(powell_section, "WEIGHT_METHOD", explicit=explicit)
     283           12 :       IF (explicit) THEN
     284            0 :          CALL section_vals_val_get(powell_section, "WEIGHT_METHOD", r_vals=w)
     285            0 :          DO i = 1, MIN(SIZE(w), m)
     286            0 :             wem(:, i) = w(i)*wem(:, i)
     287              :          END DO
     288              :       END IF
     289              : 
     290           24 :       DO i = 1, SIZE(atom_info, 1)
     291           36 :          DO j = 1, SIZE(atom_info, 2)
     292           24 :             atom_info(i, j)%atom%weight = wem(i, j)
     293              :          END DO
     294              :       END DO
     295           12 :       DEALLOCATE (wem)
     296              : 
     297           12 :       ostate%nf = 0
     298           12 :       ostate%iprint = 1
     299           12 :       ostate%unit = iunit
     300              : 
     301           12 :       ostate%state = 0
     302           12 :       IF (iunit > 0) THEN
     303            6 :          WRITE (iunit, '(/," POWELL| Start optimization procedure")')
     304            6 :          WRITE (iunit, '(/," POWELL| Total number of parameters in optimization",T71,I10)') ostate%nvar
     305              :       END IF
     306           12 :       n10 = MAX(ostate%maxfun/100, 1)
     307              : 
     308           12 :       fopt = HUGE(0._dp)
     309              : 
     310              :       DO
     311              : 
     312         2158 :          IF (ostate%state == 2) THEN
     313         2122 :             CALL restore_basis()
     314         2122 :             CALL basis_fit(atom_info, basis, pptype, ostate%f, 0, penalty)
     315         2122 :             fopt = MIN(fopt, ostate%f)
     316              :          END IF
     317              : 
     318         2158 :          IF (ostate%state == -1) EXIT
     319              : 
     320         2146 :          CALL powell_optimize(ostate%nvar, x, ostate)
     321              : 
     322         2146 :          IF (ostate%nf == 2 .AND. iunit > 0) THEN
     323            6 :             WRITE (iunit, '(" POWELL| Initial value of function",T61,F20.10)') ostate%f
     324              :          END IF
     325         2158 :          IF (MOD(ostate%nf, n10) == 0 .AND. iunit > 0) THEN
     326              :             WRITE (iunit, '(" POWELL| Reached",i4,"% of maximal function calls",T61,F20.10)') &
     327           25 :                INT(REAL(ostate%nf, dp)/REAL(ostate%maxfun, dp)*100._dp), fopt
     328              :          END IF
     329              : 
     330              :       END DO
     331              : 
     332           12 :       ostate%state = 8
     333           12 :       CALL powell_optimize(ostate%nvar, x, ostate)
     334              : 
     335           12 :       IF (iunit > 0) THEN
     336            6 :          WRITE (iunit, '(" POWELL| Number of function evaluations",T71,I10)') ostate%nf
     337            6 :          WRITE (iunit, '(" POWELL| Final value of function",T61,F20.10)') ostate%fopt
     338              :       END IF
     339              :       ! Powell may return an earlier best point. Restore its basis on every rank
     340              :       ! and recompute the orbitals before exporting contraction coefficients.
     341           12 :       CALL restore_basis()
     342           12 :       CALL basis_fit(atom_info, basis, pptype, fopt, 0, penalty)
     343           12 :       IF (iunit > 0) THEN
     344            6 :          CALL atom_print_basis(basis, iunit, " Optimized Basis")
     345            6 :          print_basis: DO i = 1, SIZE(atom_info, 1)
     346            6 :             DO j = 1, SIZE(atom_info, 2)
     347            6 :                IF (pptype .EQV. atom_info(i, j)%atom%pp_calc) THEN
     348            6 :                   CALL atom_print_basis_file(basis, atom_info(i, j)%atom%orbitals%wfn)
     349            6 :                   EXIT print_basis
     350              :                END IF
     351              :             END DO
     352              :          END DO print_basis
     353              :       END IF
     354              : 
     355           12 :       DEALLOCATE (x)
     356              : 
     357           12 :       IF (ALLOCATED(xtob)) THEN
     358           12 :          DEALLOCATE (xtob)
     359              :       END IF
     360              : 
     361           48 :       SELECT CASE (basis%basis_type)
     362              :       CASE DEFAULT
     363            0 :          CPABORT("Unknown basis type for atom_fit_basis")
     364              :       CASE (GTO_BASIS)
     365            6 :          zval = atom_info(1, 1)%atom%z
     366            6 :          crad = ptable(zval)%covalent_radius*bohr
     367           12 :          IF (iunit > 0) THEN
     368            3 :             CALL atom_condnumber(basis, crad, iunit)
     369            3 :             CALL atom_completeness(basis, zval, iunit)
     370              :          END IF
     371              :       CASE (STO_BASIS)
     372              :          ! no basis test available
     373              :       END SELECT
     374              : 
     375              :    CONTAINS
     376              : 
     377              : ! **************************************************************************************************
     378              : !> \brief Restore the radial basis and its derivatives from the Powell parameters.
     379              : ! **************************************************************************************************
     380         2134 :       SUBROUTINE restore_basis()
     381              :       INTEGER                                            :: i, k, l, ll, nl, nr
     382              :       REAL(KIND=dp)                                      :: al, ear, pf, rk
     383              : 
     384         2134 :          SELECT CASE (basis%basis_type)
     385              :          CASE DEFAULT
     386            0 :             CPABORT("Unknown basis type for atom_fit_basis")
     387              :          CASE (GTO_BASIS)
     388          970 :             IF (basis%geometrical) THEN
     389            0 :                basis%am = 0._dp
     390            0 :                DO l = 0, lmat
     391            0 :                   DO i = 1, basis%nbas(l)
     392            0 :                      ll = i - 1 + basis%start(l)
     393            0 :                      basis%am(i, l) = x(1)*x(1)*(x(2)*x(2))**(ll)
     394              :                   END DO
     395              :                END DO
     396            0 :                basis%aval = x(1)*x(1)
     397            0 :                basis%cval = x(2)*x(2)
     398              :             ELSE
     399         6790 :                DO l = 0, lmat
     400        24058 :                   DO i = 1, basis%nprim(l)
     401        17268 :                      al = x(xtob(i, l))**2
     402        23088 :                      basis%am(i, l) = EXP(al) - 1._dp
     403              :                   END DO
     404              :                END DO
     405              :             END IF
     406     13894222 :             basis%bf = 0._dp
     407     13894222 :             basis%dbf = 0._dp
     408     13894222 :             basis%ddbf = 0._dp
     409          970 :             nr = basis%grid%nr
     410         6790 :             DO l = 0, lmat
     411        24058 :                DO i = 1, basis%nbas(l)
     412        17268 :                   al = basis%am(i, l)
     413      6930288 :                   DO k = 1, nr
     414      6907200 :                      rk = basis%grid%rad(k)
     415      6907200 :                      ear = EXP(-al*basis%grid%rad(k)**2)
     416      6907200 :                      basis%bf(k, i, l) = rk**l*ear
     417      6907200 :                      basis%dbf(k, i, l) = (REAL(l, dp)*rk**(l - 1) - 2._dp*al*rk**(l + 1))*ear
     418              :                      basis%ddbf(k, i, l) = (REAL(l*(l - 1), dp)*rk**(l - 2) - &
     419      6924468 :                                             2._dp*al*REAL(2*l + 1, dp)*rk**(l) + 4._dp*al**2*rk**(l + 2))*ear
     420              :                   END DO
     421              :                END DO
     422              :             END DO
     423              :          CASE (STO_BASIS)
     424              :             ! The Slater parameter map is indexed by basis function.
     425         8148 :             DO l = 0, lmat
     426        12268 :                DO i = 1, basis%nbas(l)
     427         4120 :                   al = x(xtob(i, l))**2
     428        11104 :                   basis%as(i, l) = EXP(al) - 1._dp
     429              :                END DO
     430              :             END DO
     431      8063436 :             basis%bf = 0._dp
     432      8063436 :             basis%dbf = 0._dp
     433      8063436 :             basis%ddbf = 0._dp
     434         1164 :             nr = basis%grid%nr
     435        10282 :             DO l = 0, lmat
     436        12268 :                DO i = 1, basis%nbas(l)
     437         4120 :                   al = basis%as(i, l)
     438         4120 :                   nl = basis%ns(i, l)
     439         4120 :                   pf = (2._dp*al)**nl*SQRT(2._dp*al/fac(2*nl))
     440      1659104 :                   DO k = 1, nr
     441      1648000 :                      rk = basis%grid%rad(k)
     442      1648000 :                      ear = rk**(nl - 1)*EXP(-al*rk)
     443      1648000 :                      basis%bf(k, i, l) = pf*ear
     444      1648000 :                      basis%dbf(k, i, l) = pf*(REAL(nl - 1, dp)/rk - al)*ear
     445              :                      basis%ddbf(k, i, l) = pf*(REAL((nl - 2)*(nl - 1), dp)/rk/rk &
     446      1652120 :                                                - al*REAL(2*(nl - 1), dp)/rk + al*al)*ear
     447              :                   END DO
     448              :                END DO
     449              :             END DO
     450              :          END SELECT
     451         2134 :       END SUBROUTINE restore_basis
     452              : 
     453              :    END SUBROUTINE atom_fit_basis
     454              : ! **************************************************************************************************
     455              : !> \brief ...
     456              : !> \param atom_info ...
     457              : !> \param atom_refs ...
     458              : !> \param ppot ...
     459              : !> \param iunit           output file unit
     460              : !> \param powell_section  POWELL input section
     461              : ! **************************************************************************************************
     462           14 :    SUBROUTINE atom_fit_pseudo(atom_info, atom_refs, ppot, iunit, powell_section)
     463              :       TYPE(atom_p_type), DIMENSION(:, :), POINTER        :: atom_info, atom_refs
     464              :       TYPE(atom_potential_type), POINTER                 :: ppot
     465              :       INTEGER, INTENT(IN)                                :: iunit
     466              :       TYPE(section_vals_type), POINTER                   :: powell_section
     467              : 
     468              :       LOGICAL, PARAMETER                                 :: debug = .FALSE.
     469              : 
     470              :       CHARACTER(len=2)                                   :: pc1, pc2, pct
     471              :       INTEGER                                            :: i, i1, i2, iw, j, j1, j2, k, l, m, n, &
     472              :                                                             n10, np, nre, nreinit, ntarget
     473           14 :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: xtob
     474              :       INTEGER, DIMENSION(0:lmat)                         :: ncore
     475              :       LOGICAL                                            :: explicit, noopt_nlcc, preopt_nlcc
     476              :       REAL(KIND=dp) :: afun, charge, de, deig, drho, dx, eig, fopt, oc, pchg, peig, pv, rcm, rcov, &
     477              :                        rmax, semicore_level, step_size_scaling, t_ener, t_psir0, t_semi, t_valence, t_virt, &
     478              :                        w_ener, w_node, w_psir0, w_semi, w_valence, w_virt, wtot
     479           14 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: x, xi
     480           14 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: wem
     481              :       REAL(KIND=dp), ALLOCATABLE, &
     482              :          DIMENSION(:, :, :, :, :)                        :: dener, pval
     483           14 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: w
     484              :       TYPE(atom_type), POINTER                           :: atom
     485              :       TYPE(opt_state_type)                               :: ostate
     486              :       TYPE(wfn_init), DIMENSION(:, :), POINTER           :: wfn_guess
     487              : 
     488              :       ! weights for the optimization
     489           14 :       CALL section_vals_val_get(powell_section, "ACCURACY", r_val=ostate%rhoend)
     490           14 :       CALL section_vals_val_get(powell_section, "STEP_SIZE", r_val=ostate%rhobeg)
     491           14 :       CALL section_vals_val_get(powell_section, "MAX_FUN", i_val=ostate%maxfun)
     492           14 :       CALL section_vals_val_get(powell_section, "MAX_INIT", i_val=nreinit)
     493           14 :       CALL section_vals_val_get(powell_section, "STEP_SIZE_SCALING", r_val=step_size_scaling)
     494              : 
     495           14 :       CALL section_vals_val_get(powell_section, "WEIGHT_POT_VALENCE", r_val=w_valence)
     496           14 :       CALL section_vals_val_get(powell_section, "WEIGHT_POT_VIRTUAL", r_val=w_virt)
     497           14 :       CALL section_vals_val_get(powell_section, "WEIGHT_POT_SEMICORE", r_val=w_semi)
     498           14 :       CALL section_vals_val_get(powell_section, "WEIGHT_POT_NODE", r_val=w_node)
     499           14 :       CALL section_vals_val_get(powell_section, "WEIGHT_DELTA_ENERGY", r_val=w_ener)
     500              : 
     501           14 :       CALL section_vals_val_get(powell_section, "TARGET_PSIR0", r_val=t_psir0)
     502           14 :       CALL section_vals_val_get(powell_section, "WEIGHT_PSIR0", r_val=w_psir0)
     503           14 :       CALL section_vals_val_get(powell_section, "RCOV_MULTIPLICATION", r_val=rcm)
     504              : 
     505           14 :       CALL section_vals_val_get(powell_section, "TARGET_POT_VALENCE", r_val=t_valence)
     506           14 :       CALL section_vals_val_get(powell_section, "TARGET_POT_VIRTUAL", r_val=t_virt)
     507           14 :       CALL section_vals_val_get(powell_section, "TARGET_POT_SEMICORE", r_val=t_semi)
     508           14 :       CALL section_vals_val_get(powell_section, "TARGET_DELTA_ENERGY", r_val=t_ener)
     509              : 
     510           14 :       CALL section_vals_val_get(powell_section, "SEMICORE_LEVEL", r_val=semicore_level)
     511              : 
     512           14 :       CALL section_vals_val_get(powell_section, "NOOPT_NLCC", l_val=noopt_nlcc)
     513           14 :       CALL section_vals_val_get(powell_section, "PREOPT_NLCC", l_val=preopt_nlcc)
     514              : 
     515           14 :       n = SIZE(atom_info, 1)
     516           14 :       m = SIZE(atom_info, 2)
     517           56 :       ALLOCATE (wem(n, m))
     518           42 :       wem = 1._dp
     519           56 :       ALLOCATE (pval(8, 10, 0:lmat, m, n))
     520           84 :       ALLOCATE (dener(2, m, m, n, n))
     521           14 :       dener = 0.0_dp
     522              : 
     523           84 :       ALLOCATE (wfn_guess(n, m))
     524           28 :       DO i = 1, n
     525           42 :          DO j = 1, m
     526           14 :             atom => atom_info(i, j)%atom
     527           14 :             NULLIFY (wfn_guess(i, j)%wfn)
     528           28 :             IF (atom_consistent_method(atom%method_type, atom%state%multiplicity)) THEN
     529           14 :                i1 = SIZE(atom%orbitals%wfn, 1)
     530           14 :                i2 = SIZE(atom%orbitals%wfn, 2)
     531           70 :                ALLOCATE (wfn_guess(i, j)%wfn(i1, i2, 0:lmat))
     532              :             END IF
     533              :          END DO
     534              :       END DO
     535              : 
     536           14 :       CALL section_vals_val_get(powell_section, "WEIGHT_ELECTRON_CONFIGURATION", explicit=explicit)
     537           14 :       IF (explicit) THEN
     538            0 :          CALL section_vals_val_get(powell_section, "WEIGHT_ELECTRON_CONFIGURATION", r_vals=w)
     539            0 :          DO i = 1, MIN(SIZE(w), n)
     540            0 :             wem(i, :) = w(i)*wem(i, :)
     541              :          END DO
     542              :       END IF
     543           14 :       CALL section_vals_val_get(powell_section, "WEIGHT_METHOD", explicit=explicit)
     544           14 :       IF (explicit) THEN
     545            0 :          CALL section_vals_val_get(powell_section, "WEIGHT_METHOD", r_vals=w)
     546            0 :          DO i = 1, MIN(SIZE(w), m)
     547            0 :             wem(:, i) = w(i)*wem(:, i)
     548              :          END DO
     549              :       END IF
     550              : 
     551              :       IF (debug) THEN
     552              :          CALL open_file(file_name="POWELL_RESULT", file_status="UNKNOWN", file_action="WRITE", unit_number=iw)
     553              :       END IF
     554              : 
     555           14 :       IF (ppot%gth_pot%nlcc) THEN
     556            3 :          CALL opt_nlcc_param(atom_info, atom_refs, ppot%gth_pot, iunit, preopt_nlcc)
     557              :       ELSE
     558           11 :          noopt_nlcc = .TRUE.
     559           11 :          preopt_nlcc = .FALSE.
     560              :       END IF
     561              : 
     562           14 :       ALLOCATE (xi(200))
     563              :       !decide here what to optimize
     564           14 :       CALL get_pseudo_param(xi, ostate%nvar, ppot%gth_pot, noopt_nlcc)
     565           42 :       ALLOCATE (x(ostate%nvar))
     566           88 :       x(1:ostate%nvar) = xi(1:ostate%nvar)
     567           14 :       DEALLOCATE (xi)
     568              : 
     569           14 :       ostate%nf = 0
     570           14 :       ostate%iprint = 1
     571           14 :       ostate%unit = iunit
     572              : 
     573           14 :       ostate%state = 0
     574           14 :       IF (iunit > 0) THEN
     575           14 :          WRITE (iunit, '(/," POWELL| Start optimization procedure")')
     576           14 :          WRITE (iunit, '(/," POWELL| Total number of parameters in optimization",T71,I10)') ostate%nvar
     577              :       END IF
     578           14 :       n10 = MAX(ostate%maxfun/100, 1)
     579              : 
     580           14 :       rcov = ptable(atom_refs(1, 1)%atom%z)%covalent_radius*bohr*rcm
     581              :       !set all reference values
     582           14 :       ntarget = 0
     583           14 :       wtot = 0.0_dp
     584           28 :       DO i = 1, SIZE(atom_info, 1)
     585           42 :          DO j = 1, SIZE(atom_info, 2)
     586           14 :             atom => atom_info(i, j)%atom
     587           28 :             IF (atom_consistent_method(atom%method_type, atom%state%multiplicity)) THEN
     588           14 :                dener(2, j, j, i, i) = atom_refs(i, j)%atom%energy%etot
     589           14 :                IF (atom%state%multiplicity == -1) THEN
     590              :                   ! no spin polarization
     591           13 :                   atom%weight = wem(i, j)
     592           91 :                   ncore = get_maxn_occ(atom%state%core) - get_maxn_occ(atom_refs(i, j)%atom%state%core)
     593           91 :                   DO l = 0, lmat
     594           78 :                      np = atom%state%maxn_calc(l)
     595          150 :                      DO k = 1, np
     596              :                         CALL atom_orbital_max(rmax, atom_refs(i, j)%atom%orbitals%wfn(:, ncore(l) + k, l), &
     597           72 :                                               rcov, l, atom_refs(i, j)%atom%basis)
     598           72 :                         atom%orbitals%rcmax(k, l, 1) = MAX(rcov, rmax)
     599              :                         CALL atom_orbital_charge(charge, atom_refs(i, j)%atom%orbitals%wfn(:, ncore(l) + k, l), &
     600           72 :                                                  atom%orbitals%rcmax(k, l, 1), l, atom_refs(i, j)%atom%basis)
     601           72 :                         atom%orbitals%refene(k, l, 1) = atom_refs(i, j)%atom%orbitals%ener(ncore(l) + k, l)
     602           72 :                         atom%orbitals%refchg(k, l, 1) = charge
     603          150 :                         IF (k > atom%state%maxn_occ(l)) THEN
     604           50 :                            IF (k <= atom%state%maxn_occ(l) + 1) THEN
     605           31 :                               atom%orbitals%wrefene(k, l, 1) = w_virt
     606           31 :                               atom%orbitals%wrefchg(k, l, 1) = w_virt/100._dp
     607           31 :                               atom%orbitals%crefene(k, l, 1) = t_virt
     608           31 :                               atom%orbitals%reftype(k, l, 1) = "U1"
     609           31 :                               ntarget = ntarget + 2
     610           31 :                               wtot = wtot + atom%weight*(w_virt + w_virt/100._dp)
     611              :                            ELSE
     612           19 :                               atom%orbitals%wrefene(k, l, 1) = w_virt/100._dp
     613           19 :                               atom%orbitals%wrefchg(k, l, 1) = 0._dp
     614           19 :                               atom%orbitals%crefene(k, l, 1) = t_virt*10._dp
     615           19 :                               atom%orbitals%reftype(k, l, 1) = "U2"
     616           19 :                               ntarget = ntarget + 1
     617           19 :                               wtot = wtot + atom%weight*w_virt/100._dp
     618              :                            END IF
     619           22 :                         ELSE IF (k < atom%state%maxn_occ(l)) THEN
     620            0 :                            atom%orbitals%wrefene(k, l, 1) = w_semi
     621            0 :                            atom%orbitals%wrefchg(k, l, 1) = w_semi/100._dp
     622            0 :                            atom%orbitals%crefene(k, l, 1) = t_semi
     623            0 :                            atom%orbitals%reftype(k, l, 1) = "SC"
     624            0 :                            ntarget = ntarget + 2
     625            0 :                            wtot = wtot + atom%weight*(w_semi + w_semi/100._dp)
     626              :                         ELSE
     627           22 :                            IF (ABS(atom%state%occupation(l, k) - REAL(4*l + 2, KIND=dp)) < 0.01_dp .AND. &
     628              :                                ABS(atom%orbitals%refene(k, l, 1)) > semicore_level) THEN
     629              :                               ! full shell semicore
     630            0 :                               atom%orbitals%wrefene(k, l, 1) = w_semi
     631            0 :                               atom%orbitals%wrefchg(k, l, 1) = w_semi/100._dp
     632            0 :                               atom%orbitals%crefene(k, l, 1) = t_semi
     633            0 :                               atom%orbitals%reftype(k, l, 1) = "SC"
     634            0 :                               wtot = wtot + atom%weight*(w_semi + w_semi/100._dp)
     635              :                            ELSE
     636           22 :                               atom%orbitals%wrefene(k, l, 1) = w_valence
     637           22 :                               atom%orbitals%wrefchg(k, l, 1) = w_valence/100._dp
     638           22 :                               atom%orbitals%crefene(k, l, 1) = t_valence
     639           22 :                               atom%orbitals%reftype(k, l, 1) = "VA"
     640           22 :                               wtot = wtot + atom%weight*(w_valence + w_valence/100._dp)
     641              :                            END IF
     642           22 :                            IF (l == 0) THEN
     643           13 :                               atom%orbitals%tpsir0(k, 1) = t_psir0
     644           13 :                               atom%orbitals%wpsir0(k, 1) = w_psir0
     645           13 :                               wtot = wtot + atom%weight*w_psir0
     646              :                            END IF
     647           22 :                            ntarget = ntarget + 2
     648              :                         END IF
     649              :                      END DO
     650          163 :                      DO k = 1, np
     651           72 :                         atom%orbitals%refnod(k, l, 1) = REAL(k - 1, KIND=dp)
     652              :                         ! we only enforce 0-nodes for the first state
     653          150 :                         IF (k == 1 .AND. atom%state%occupation(l, k) /= 0._dp) THEN
     654           22 :                            atom%orbitals%wrefnod(k, l, 1) = w_node
     655           22 :                            wtot = wtot + atom%weight*w_node
     656              :                         END IF
     657              :                      END DO
     658              :                   END DO
     659              :                ELSE
     660              :                   ! spin polarization
     661            1 :                   atom%weight = wem(i, j)
     662            1 :                   ncore = get_maxn_occ(atom_info(i, j)%atom%state%core)
     663            7 :                   DO l = 0, lmat
     664            6 :                      np = atom%state%maxn_calc(l)
     665           12 :                      DO k = 1, np
     666              :                         CALL atom_orbital_max(rmax, atom_refs(i, j)%atom%orbitals%wfna(:, ncore(l) + k, l), &
     667            6 :                                               rcov, l, atom_refs(i, j)%atom%basis)
     668            6 :                         atom%orbitals%rcmax(k, l, 1) = MAX(rcov, rmax)
     669              :                         CALL atom_orbital_max(rmax, atom_refs(i, j)%atom%orbitals%wfnb(:, ncore(l) + k, l), &
     670            6 :                                               rcov, l, atom_refs(i, j)%atom%basis)
     671            6 :                         atom%orbitals%rcmax(k, l, 2) = MAX(rcov, rmax)
     672              :                         CALL atom_orbital_charge(charge, atom_refs(i, j)%atom%orbitals%wfna(:, ncore(l) + k, l), &
     673            6 :                                                  atom%orbitals%rcmax(k, l, 1), l, atom_refs(i, j)%atom%basis)
     674            6 :                         atom%orbitals%refene(k, l, 1) = atom_refs(i, j)%atom%orbitals%enera(ncore(l) + k, l)
     675            6 :                         atom%orbitals%refchg(k, l, 1) = charge
     676              :                         CALL atom_orbital_charge(charge, atom_refs(i, j)%atom%orbitals%wfnb(:, ncore(l) + k, l), &
     677            6 :                                                  atom%orbitals%rcmax(k, l, 1), l, atom_refs(i, j)%atom%basis)
     678            6 :                         atom%orbitals%refene(k, l, 2) = atom_refs(i, j)%atom%orbitals%enerb(ncore(l) + k, l)
     679            6 :                         atom%orbitals%refchg(k, l, 2) = charge
     680              :                         ! the following assignments could be further specialized
     681           12 :                         IF (k > atom%state%maxn_occ(l)) THEN
     682            4 :                            IF (k <= atom%state%maxn_occ(l) + 1) THEN
     683            6 :                               atom%orbitals%wrefene(k, l, 1:2) = w_virt
     684            6 :                               atom%orbitals%wrefchg(k, l, 1:2) = w_virt/100._dp
     685            6 :                               atom%orbitals%crefene(k, l, 1:2) = t_virt
     686            6 :                               atom%orbitals%reftype(k, l, 1:2) = "U1"
     687            2 :                               ntarget = ntarget + 4
     688            2 :                               wtot = wtot + atom%weight*2._dp*(w_virt + w_virt/100._dp)
     689              :                            ELSE
     690            6 :                               atom%orbitals%wrefene(k, l, 1:2) = w_virt/100._dp
     691            6 :                               atom%orbitals%wrefchg(k, l, 1:2) = 0._dp
     692            6 :                               atom%orbitals%crefene(k, l, 1:2) = t_virt*10.0_dp
     693            6 :                               atom%orbitals%reftype(k, l, 1:2) = "U2"
     694            2 :                               wtot = wtot + atom%weight*2._dp*w_virt/100._dp
     695            2 :                               ntarget = ntarget + 2
     696              :                            END IF
     697            2 :                         ELSE IF (k < atom%state%maxn_occ(l)) THEN
     698            0 :                            atom%orbitals%wrefene(k, l, 1:2) = w_semi
     699            0 :                            atom%orbitals%wrefchg(k, l, 1:2) = w_semi/100._dp
     700            0 :                            atom%orbitals%crefene(k, l, 1:2) = t_semi
     701            0 :                            atom%orbitals%reftype(k, l, 1:2) = "SC"
     702            0 :                            ntarget = ntarget + 4
     703            0 :                            wtot = wtot + atom%weight*2._dp*(w_semi + w_semi/100._dp)
     704              :                         ELSE
     705            2 :                            IF (ABS(atom%state%occupation(l, k) - REAL(2*l + 1, KIND=dp)) < 0.01_dp .AND. &
     706              :                                ABS(atom%orbitals%refene(k, l, 1)) > semicore_level) THEN
     707            0 :                               atom%orbitals%wrefene(k, l, 1:2) = w_semi
     708            0 :                               atom%orbitals%wrefchg(k, l, 1:2) = w_semi/100._dp
     709            0 :                               atom%orbitals%crefene(k, l, 1:2) = t_semi
     710            0 :                               atom%orbitals%reftype(k, l, 1:2) = "SC"
     711            0 :                               wtot = wtot + atom%weight*2._dp*(w_semi + w_semi/100._dp)
     712              :                            ELSE
     713            6 :                               atom%orbitals%wrefene(k, l, 1:2) = w_valence
     714            6 :                               atom%orbitals%wrefchg(k, l, 1:2) = w_valence/100._dp
     715            6 :                               atom%orbitals%crefene(k, l, 1:2) = t_valence
     716            6 :                               atom%orbitals%reftype(k, l, 1:2) = "VA"
     717            2 :                               wtot = wtot + atom%weight*2._dp*(w_valence + w_valence/100._dp)
     718              :                            END IF
     719            2 :                            ntarget = ntarget + 4
     720            2 :                            IF (l == 0) THEN
     721            3 :                               atom%orbitals%tpsir0(k, 1:2) = t_psir0
     722            3 :                               atom%orbitals%wpsir0(k, 1:2) = w_psir0
     723            1 :                               wtot = wtot + atom%weight*2._dp*w_psir0
     724              :                            END IF
     725              :                         END IF
     726              :                      END DO
     727           13 :                      DO k = 1, np
     728           18 :                         atom%orbitals%refnod(k, l, 1:2) = REAL(k - 1, KIND=dp)
     729              :                         ! we only enforce 0-nodes for the first state
     730            6 :                         IF (k == 1 .AND. atom%state%occa(l, k) /= 0._dp) THEN
     731            2 :                            atom%orbitals%wrefnod(k, l, 1) = w_node
     732            2 :                            wtot = wtot + atom%weight*w_node
     733              :                         END IF
     734           12 :                         IF (k == 1 .AND. atom%state%occb(l, k) /= 0._dp) THEN
     735            1 :                            atom%orbitals%wrefnod(k, l, 2) = w_node
     736            1 :                            wtot = wtot + atom%weight*w_node
     737              :                         END IF
     738              :                      END DO
     739              :                   END DO
     740              :                END IF
     741           14 :                CALL calculate_atom(atom, 0)
     742              :             END IF
     743              :          END DO
     744              :       END DO
     745              :       ! energy differences
     746           28 :       DO j1 = 1, SIZE(atom_info, 2)
     747           42 :          DO j2 = 1, SIZE(atom_info, 2)
     748           42 :             DO i1 = 1, SIZE(atom_info, 1)
     749           42 :                DO i2 = 1, SIZE(atom_info, 1)
     750           14 :                   IF ((j1 > j2) .OR. (j1 == j2 .AND. i1 >= i2)) CYCLE
     751            0 :                   dener(2, j1, j2, i1, i2) = dener(2, j1, j1, i1, i1) - dener(2, j2, j2, i2, i2)
     752           28 :                   wtot = wtot + w_ener
     753              :                END DO
     754              :             END DO
     755              :          END DO
     756              :       END DO
     757              : 
     758           14 :       DEALLOCATE (wem)
     759              : 
     760           42 :       ALLOCATE (xi(ostate%nvar))
     761           15 :       DO nre = 1, nreinit
     762           88 :          xi(:) = x(:)
     763           14 :          CALL put_pseudo_param(x, ppot%gth_pot, noopt_nlcc)
     764           14 :          CALL pseudo_fit(atom_info, wfn_guess, ppot, ostate%f, wtot, pval, dener, w_ener, t_ener, .TRUE.)
     765           14 :          IF (nre == 1) THEN
     766           14 :             WRITE (iunit, '(/," POWELL| Initial errors of target values")')
     767           14 :             afun = ostate%f*wtot + 1.0E-8_dp
     768           28 :             DO i = 1, SIZE(atom_info, 1)
     769           28 :                DO j = 1, SIZE(atom_info, 2)
     770           14 :                   atom => atom_info(i, j)%atom
     771           28 :                   IF (atom_consistent_method(atom%method_type, atom%state%multiplicity)) THEN
     772              :                      ! start orbitals
     773        11354 :                      wfn_guess(i, j)%wfn = atom%orbitals%wfn
     774              :                      !
     775           14 :                      WRITE (iunit, '(/," Reference configuration  ",T31,i5,T50," Method number ",T76,i5)') i, j
     776           14 :                      IF (atom%state%multiplicity == -1) THEN
     777              :                         ! no spin polarization
     778           13 :                         WRITE (iunit, '("    L    N    Occupation      Eigenvalue [eV]           dE [eV]          dCharge ")')
     779           91 :                         DO l = 0, lmat
     780           78 :                            np = atom%state%maxn_calc(l)
     781           91 :                            IF (np > 0) THEN
     782          106 :                               DO k = 1, np
     783           72 :                                  oc = atom%state%occupation(l, k)
     784           72 :                                  eig = atom%orbitals%ener(k, l)
     785           72 :                                  deig = eig - atom%orbitals%refene(k, l, 1)
     786           72 :                                  peig = pval(1, k, l, j, i)/afun*100._dp
     787           72 :                                  IF (pval(5, k, l, j, i) > 0.5_dp) THEN
     788           63 :                                     pc1 = " X"
     789              :                                  ELSE
     790            9 :                                     WRITE (pc1, "(I2)") NINT(peig)
     791              :                                  END IF
     792              :                                  CALL atom_orbital_charge(charge, atom%orbitals%wfn(:, k, l), &
     793           72 :                                                           atom%orbitals%rcmax(k, l, 1), l, atom%basis)
     794           72 :                                  drho = charge - atom%orbitals%refchg(k, l, 1)
     795           72 :                                  pchg = pval(2, k, l, j, i)/afun*100._dp
     796           72 :                                  IF (pval(6, k, l, j, i) > 0.5_dp) THEN
     797           36 :                                     pc2 = " X"
     798              :                                  ELSE
     799           36 :                                     WRITE (pc2, "(I2)") MIN(NINT(pchg), 99)
     800              :                                  END IF
     801           72 :                                  pct = atom%orbitals%reftype(k, l, 1)
     802              :                                  WRITE (iunit, '(I5,I5,F14.2,F21.10,A3,F11.6,"[",A2,"]",F13.6,"[",A2,"]")') &
     803          106 :                                     l, k, oc, eig*evolt, pct, deig*evolt, pc1, drho, pc2
     804              :                               END DO
     805              :                            END IF
     806              :                         END DO
     807              :                      ELSE
     808              :                         ! spin polarization
     809            1 :                         WRITE (iunit, '("    L    N   Spin Occupation    Eigenvalue [eV]          dE [eV]         dCharge ")')
     810            7 :                         DO l = 0, lmat
     811            6 :                            np = atom%state%maxn_calc(l)
     812            7 :                            IF (np > 0) THEN
     813            8 :                               DO k = 1, np
     814            6 :                                  oc = atom%state%occa(l, k)
     815            6 :                                  eig = atom%orbitals%enera(k, l)
     816            6 :                                  deig = eig - atom%orbitals%refene(k, l, 1)
     817            6 :                                  peig = pval(1, k, l, j, i)/afun*100._dp
     818            6 :                                  IF (pval(5, k, l, j, i) > 0.5_dp) THEN
     819            5 :                                     pc1 = " X"
     820              :                                  ELSE
     821            1 :                                     WRITE (pc1, "(I2)") NINT(peig)
     822              :                                  END IF
     823              :                                  CALL atom_orbital_charge( &
     824            6 :                                     charge, atom%orbitals%wfna(:, k, l), atom%orbitals%rcmax(k, l, 1), l, atom%basis)
     825            6 :                                  drho = charge - atom%orbitals%refchg(k, l, 1)
     826            6 :                                  pchg = pval(2, k, l, j, i)/afun*100._dp
     827            6 :                                  IF (pval(6, k, l, j, i) > 0.5_dp) THEN
     828            2 :                                     pc2 = " X"
     829              :                                  ELSE
     830            4 :                                     WRITE (pc2, "(I2)") MIN(NINT(pchg), 99)
     831              :                                  END IF
     832            6 :                                  pct = atom%orbitals%reftype(k, l, 1)
     833              :                                  WRITE (iunit, '(I5,I5,2X,A5,F11.2,F19.10,A3,F10.6,"[",A2,"]",F12.6,"[",A2,"]")') &
     834            6 :                                     l, k, "alpha", oc, eig*evolt, pct, deig*evolt, pc1, drho, pc2
     835            6 :                                  oc = atom%state%occb(l, k)
     836            6 :                                  eig = atom%orbitals%enerb(k, l)
     837            6 :                                  deig = eig - atom%orbitals%refene(k, l, 2)
     838            6 :                                  peig = pval(3, k, l, j, i)/afun*100._dp
     839            6 :                                  IF (pval(7, k, l, j, i) > 0.5_dp) THEN
     840            4 :                                     pc1 = " X"
     841              :                                  ELSE
     842            2 :                                     WRITE (pc1, "(I2)") NINT(peig)
     843              :                                  END IF
     844              :                                  CALL atom_orbital_charge( &
     845            6 :                                     charge, atom%orbitals%wfnb(:, k, l), atom%orbitals%rcmax(k, l, 2), l, atom%basis)
     846            6 :                                  drho = charge - atom%orbitals%refchg(k, l, 2)
     847            6 :                                  pchg = pval(4, k, l, j, i)/afun*100._dp
     848            6 :                                  IF (pval(8, k, l, j, i) > 0.5_dp) THEN
     849            2 :                                     pc2 = " X"
     850              :                                  ELSE
     851            4 :                                     WRITE (pc2, "(I2)") MIN(NINT(pchg), 99)
     852              :                                  END IF
     853            6 :                                  pct = atom%orbitals%reftype(k, l, 2)
     854              :                                  WRITE (iunit, '(I5,I5,2X,A5,F11.2,F19.10,A3,F10.6,"[",A2,"]",F12.6,"[",A2,"]")') &
     855            8 :                                     l, k, " beta", oc, eig*evolt, pct, deig*evolt, pc1, drho, pc2
     856              :                               END DO
     857              :                            END IF
     858              :                         END DO
     859              :                      END IF
     860              :                   END IF
     861              :                END DO
     862           28 :                WRITE (iunit, *)
     863              :             END DO
     864              :             ! energy differences
     865           14 :             IF (n*m > 1) THEN
     866            0 :                WRITE (iunit, '(" Method/Econf 1 "," Method/Econf 2","      Delta Energy ","      Error Energy ","     Target")')
     867            0 :                DO j1 = 1, SIZE(atom_info, 2)
     868            0 :                   DO j2 = 1, SIZE(atom_info, 2)
     869            0 :                      DO i1 = 1, SIZE(atom_info, 1)
     870            0 :                         DO i2 = 1, SIZE(atom_info, 1)
     871            0 :                            IF ((j1 > j2) .OR. (j1 == j2 .AND. i1 >= i2)) CYCLE
     872            0 :                            IF (ABS(dener(1, j1, j1, i1, i1)) < 0.000001_dp) CYCLE
     873            0 :                            IF (ABS(dener(2, j1, j1, i1, i1)) < 0.000001_dp) CYCLE
     874            0 :                            IF (ABS(dener(1, j2, j2, i2, i2)) < 0.000001_dp) CYCLE
     875            0 :                            IF (ABS(dener(2, j2, j2, i2, i2)) < 0.000001_dp) CYCLE
     876            0 :                            de = dener(2, j1, j2, i1, i2) - dener(1, j1, j2, i1, i2)
     877              :                            WRITE (iunit, '(i6,i6,i10,i6,5X,F16.6,F19.6,F12.6)') &
     878            0 :                               j1, i1, j2, i2, dener(2, j1, j2, i1, i2), de, t_ener
     879              :                         END DO
     880              :                      END DO
     881              :                   END DO
     882              :                END DO
     883            0 :                WRITE (iunit, *)
     884              :             END IF
     885              : 
     886              :             WRITE (iunit, '(" Number of target values reached: ",T69,i5," of ",i3)') &
     887         4326 :                INT(SUM(pval(5:8, :, :, :, :))), ntarget
     888           14 :             WRITE (iunit, *)
     889              : 
     890              :          END IF
     891              : 
     892              :          WRITE (iunit, '(" POWELL| Start optimization",I4," of total",I4,T60,"rhobeg = ",F12.8)') &
     893           14 :             nre, nreinit, ostate%rhobeg
     894           14 :          fopt = HUGE(0._dp)
     895           14 :          ostate%state = 0
     896           14 :          CALL powell_optimize(ostate%nvar, x, ostate)
     897              :          DO
     898              : 
     899          333 :             IF (ostate%state == 2) THEN
     900          307 :                CALL put_pseudo_param(x, ppot%gth_pot, noopt_nlcc)
     901          307 :                CALL pseudo_fit(atom_info, wfn_guess, ppot, ostate%f, wtot, pval, dener, w_ener, t_ener, .FALSE.)
     902          307 :                fopt = MIN(fopt, ostate%f)
     903              :             END IF
     904              : 
     905          333 :             IF (ostate%state == -1) EXIT
     906              : 
     907          320 :             CALL powell_optimize(ostate%nvar, x, ostate)
     908              : 
     909          320 :             IF (ostate%nf == 2 .AND. iunit > 0) THEN
     910           14 :                WRITE (iunit, '(" POWELL| Initial value of function",T61,F20.10)') ostate%f
     911              :             END IF
     912          320 :             IF (MOD(ostate%nf, n10) == 0 .AND. iunit > 0 .AND. ostate%nf > 2) THEN
     913              :                WRITE (iunit, '(" POWELL| Reached",i4,"% of maximal function calls",T61,F20.10)') &
     914           22 :                   INT(REAL(ostate%nf, dp)/REAL(ostate%maxfun, dp)*100._dp), fopt
     915           22 :                CALL put_pseudo_param(ostate%xopt, ppot%gth_pot, noopt_nlcc)
     916           22 :                CALL atom_write_pseudo_param(ppot%gth_pot, fopt=fopt)
     917              :             END IF
     918              : 
     919          320 :             IF (fopt < 1.e-12_dp) EXIT
     920              : 
     921           14 :             IF (debug) THEN
     922              :                WRITE (iw, *) ostate%nf, ostate%f, x(1:ostate%nvar)
     923              :             END IF
     924              : 
     925              :          END DO
     926              : 
     927           88 :          dx = SQRT(SUM((ostate%xopt(:) - xi(:))**2)/REAL(ostate%nvar, KIND=dp))
     928           14 :          IF (iunit > 0) THEN
     929           14 :             WRITE (iunit, '(" POWELL| RMS average of variables",T69,F12.10)') dx
     930              :          END IF
     931           14 :          ostate%state = 8
     932           14 :          CALL powell_optimize(ostate%nvar, x, ostate)
     933           14 :          CALL put_pseudo_param(x, ppot%gth_pot, noopt_nlcc)
     934           14 :          CALL atom_write_pseudo_param(ppot%gth_pot, fopt=fopt)
     935              :          ! dx < SQRT(ostate%rhoend)
     936           14 :          IF ((dx*dx) < ostate%rhoend) EXIT
     937           15 :          ostate%rhobeg = step_size_scaling*ostate%rhobeg
     938              :       END DO
     939           14 :       DEALLOCATE (xi)
     940              : 
     941           14 :       IF (iunit > 0) THEN
     942           14 :          WRITE (iunit, '(" POWELL| Number of function evaluations",T71,I10)') ostate%nf
     943           14 :          WRITE (iunit, '(" POWELL| Final value of function",T61,F20.10)') ostate%fopt
     944              : 
     945           14 :          CALL put_pseudo_param(x, ppot%gth_pot, noopt_nlcc)
     946           14 :          CALL pseudo_fit(atom_info, wfn_guess, ppot, ostate%f, wtot, pval, dener, w_ener, t_ener, .FALSE.)
     947           14 :          afun = wtot*ostate%f + 1.0E-8_dp
     948              : 
     949           14 :          WRITE (iunit, '(/," POWELL| Final errors of target values")')
     950           28 :          DO i = 1, SIZE(atom_info, 1)
     951           42 :             DO j = 1, SIZE(atom_info, 2)
     952           14 :                atom => atom_info(i, j)%atom
     953           28 :                IF (atom_consistent_method(atom%method_type, atom%state%multiplicity)) THEN
     954           14 :                   WRITE (iunit, '(/," Reference configuration  ",T31,i5,T50," Method number ",T76,i5)') i, j
     955           14 :                   IF (atom%state%multiplicity == -1) THEN
     956              :                      !no spin polarization
     957           13 :                      WRITE (iunit, '("    L    N    Occupation      Eigenvalue [eV]           dE [eV]          dCharge ")')
     958           91 :                      DO l = 0, lmat
     959           78 :                         np = atom%state%maxn_calc(l)
     960           91 :                         IF (np > 0) THEN
     961          106 :                            DO k = 1, np
     962           72 :                               oc = atom%state%occupation(l, k)
     963           72 :                               eig = atom%orbitals%ener(k, l)
     964           72 :                               deig = eig - atom%orbitals%refene(k, l, 1)
     965           72 :                               peig = pval(1, k, l, j, i)/afun*100._dp
     966           72 :                               IF (pval(5, k, l, j, i) > 0.5_dp) THEN
     967           29 :                                  pc1 = " X"
     968              :                               ELSE
     969           43 :                                  WRITE (pc1, "(I2)") NINT(peig)
     970              :                               END IF
     971              :                               CALL atom_orbital_charge( &
     972           72 :                                  charge, atom%orbitals%wfn(:, k, l), atom%orbitals%rcmax(k, l, 1), l, atom%basis)
     973           72 :                               drho = charge - atom%orbitals%refchg(k, l, 1)
     974           72 :                               pchg = pval(2, k, l, j, i)/afun*100._dp
     975           72 :                               IF (pval(6, k, l, j, i) > 0.5_dp) THEN
     976           35 :                                  pc2 = " X"
     977              :                               ELSE
     978           37 :                                  WRITE (pc2, "(I2)") MIN(NINT(pchg), 99)
     979              :                               END IF
     980           72 :                               pct = atom%orbitals%reftype(k, l, 1)
     981              :                               WRITE (iunit, '(I5,I5,F14.2,F21.10,A3,F11.6,"[",A2,"]",F13.6,"[",A2,"]")') &
     982          106 :                                  l, k, oc, eig*evolt, pct, deig*evolt, pc1, drho, pc2
     983              :                            END DO
     984              :                         END IF
     985              :                      END DO
     986           13 :                      np = atom%state%maxn_calc(0)
     987           48 :                      DO k = 1, np
     988           35 :                         CALL atom_wfnr0(pv, atom%orbitals%wfn(:, k, 0), atom%basis)
     989           35 :                         IF (ABS(atom%orbitals%tpsir0(k, 1)) > 1.e-14_dp) THEN
     990            0 :                            IF (ABS(pv) > ABS(atom%orbitals%tpsir0(k, 1))) THEN
     991            0 :                               pv = 0.0_dp
     992              :                            ELSE
     993            0 :                               pv = 10._dp*(ABS(pv) - ABS(atom%orbitals%tpsir0(k, 1)))
     994              :                            END IF
     995            0 :                            pchg = atom%weight*atom%orbitals%wpsir0(k, 1)*pv*pv/afun
     996              :                         ELSE
     997           35 :                            pchg = atom%weight*atom%orbitals%wpsir0(k, 1)*pv*pv/afun*100._dp
     998              :                         END IF
     999              :                         WRITE (iunit, '("    s-states"," N=",I5,T40,"Wavefunction at r=0:",T64,F13.6,"[",I2,"]")') &
    1000           48 :                            k, pv, NINT(pchg)
    1001              :                      END DO
    1002              :                   ELSE
    1003              :                      !spin polarization
    1004            1 :                      WRITE (iunit, '("    L    N   Spin Occupation     Eigenvalue [eV]          dE [eV]        dCharge ")')
    1005            7 :                      DO l = 0, lmat
    1006            6 :                         np = atom%state%maxn_calc(l)
    1007            7 :                         IF (np > 0) THEN
    1008            8 :                            DO k = 1, np
    1009            6 :                               oc = atom%state%occa(l, k)
    1010            6 :                               eig = atom%orbitals%enera(k, l)
    1011            6 :                               deig = eig - atom%orbitals%refene(k, l, 1)
    1012            6 :                               peig = pval(1, k, l, j, i)/afun*100._dp
    1013            6 :                               IF (pval(5, k, l, j, i) > 0.5_dp) THEN
    1014            5 :                                  pc1 = " X"
    1015              :                               ELSE
    1016            1 :                                  WRITE (pc1, "(I2)") NINT(peig)
    1017              :                               END IF
    1018              :                               CALL atom_orbital_charge( &
    1019            6 :                                  charge, atom%orbitals%wfna(:, k, l), atom%orbitals%rcmax(k, l, 1), l, atom%basis)
    1020            6 :                               drho = charge - atom%orbitals%refchg(k, l, 1)
    1021            6 :                               pchg = pval(2, k, l, j, i)/afun*100._dp
    1022            6 :                               IF (pval(6, k, l, j, i) > 0.5_dp) THEN
    1023            2 :                                  pc2 = " X"
    1024              :                               ELSE
    1025            4 :                                  WRITE (pc2, "(I2)") MIN(NINT(pchg), 99)
    1026              :                               END IF
    1027            6 :                               pct = atom%orbitals%reftype(k, l, 1)
    1028              :                               WRITE (iunit, '(I5,I5,A,F11.2,F20.10,A3,F10.6,"[",A2,"]",F11.6,"[",A2,"]")') &
    1029            6 :                                  l, k, "  alpha", oc, eig*evolt, pct, deig*evolt, pc1, drho, pc2
    1030            6 :                               oc = atom%state%occb(l, k)
    1031            6 :                               eig = atom%orbitals%enerb(k, l)
    1032            6 :                               deig = eig - atom%orbitals%refene(k, l, 2)
    1033            6 :                               peig = pval(3, k, l, j, i)/afun*100._dp
    1034            6 :                               IF (pval(7, k, l, j, i) > 0.5_dp) THEN
    1035            4 :                                  pc1 = " X"
    1036              :                               ELSE
    1037            2 :                                  WRITE (pc1, "(I2)") NINT(peig)
    1038              :                               END IF
    1039              :                               CALL atom_orbital_charge( &
    1040            6 :                                  charge, atom%orbitals%wfnb(:, k, l), atom%orbitals%rcmax(k, l, 2), l, atom%basis)
    1041            6 :                               drho = charge - atom%orbitals%refchg(k, l, 2)
    1042            6 :                               pchg = pval(4, k, l, j, i)/afun*100._dp
    1043            6 :                               IF (pval(8, k, l, j, i) > 0.5_dp) THEN
    1044            2 :                                  pc2 = " X"
    1045              :                               ELSE
    1046            4 :                                  WRITE (pc2, "(I2)") MIN(NINT(pchg), 99)
    1047              :                               END IF
    1048            6 :                               pct = atom%orbitals%reftype(k, l, 2)
    1049              :                               WRITE (iunit, '(I5,I5,A,F11.2,F20.10,A3,F10.6,"[",A2,"]",F11.6,"[",A2,"]")') &
    1050            8 :                                  l, k, "   beta", oc, eig*evolt, pct, deig*evolt, pc1, drho, pc2
    1051              :                            END DO
    1052              :                         END IF
    1053              :                      END DO
    1054            1 :                      np = atom%state%maxn_calc(0)
    1055            4 :                      DO k = 1, np
    1056            3 :                         CALL atom_wfnr0(pv, atom%orbitals%wfna(:, k, 0), atom%basis)
    1057            3 :                         IF (ABS(atom%orbitals%tpsir0(k, 1)) > 1.e-14_dp) THEN
    1058            0 :                            IF (ABS(pv) > ABS(atom%orbitals%tpsir0(k, 1))) THEN
    1059            0 :                               pv = 0.0_dp
    1060              :                            ELSE
    1061            0 :                               pv = 10._dp*(ABS(pv) - ABS(atom%orbitals%tpsir0(k, 1)))
    1062              :                            END IF
    1063            0 :                            pchg = atom%weight*atom%orbitals%wpsir0(k, 1)*pv*pv/afun
    1064              :                         ELSE
    1065            3 :                            pchg = atom%weight*atom%orbitals%wpsir0(k, 1)*pv*pv/afun*100._dp
    1066              :                         END IF
    1067              :                         WRITE (iunit, '("    s-states"," N=",I5,T35,"Alpha Wavefunction at r=0:",T64,F13.6,"[",I2,"]")') &
    1068            3 :                            k, pv, NINT(pchg)
    1069              :                         !
    1070            3 :                         CALL atom_wfnr0(pv, atom%orbitals%wfnb(:, k, 0), atom%basis)
    1071            3 :                         IF (ABS(atom%orbitals%tpsir0(k, 2)) > 1.e-14_dp) THEN
    1072            0 :                            IF (ABS(pv) > ABS(atom%orbitals%tpsir0(k, 2))) THEN
    1073            0 :                               pv = 0.0_dp
    1074              :                            ELSE
    1075            0 :                               pv = 10._dp*(ABS(pv) - ABS(atom%orbitals%tpsir0(k, 2)))
    1076              :                            END IF
    1077            0 :                            pchg = atom%weight*atom%orbitals%wpsir0(k, 2)*pv*pv/afun
    1078              :                         ELSE
    1079            3 :                            pchg = atom%weight*atom%orbitals%wpsir0(k, 2)*pv*pv/afun*100._dp
    1080              :                         END IF
    1081              :                         WRITE (iunit, '("    s-states"," N=",I5,T36,"Beta Wavefunction at r=0:",T64,F13.6,"[",I2,"]")') &
    1082            4 :                            k, pv, NINT(pchg)
    1083              :                      END DO
    1084              :                   END IF
    1085              :                END IF
    1086              :             END DO
    1087              :          END DO
    1088              :          ! energy differences
    1089           14 :          IF (n*m > 1) THEN
    1090            0 :             WRITE (iunit, *)
    1091            0 :             WRITE (iunit, '(" Method/Econf 1 "," Method/Econf 2","      Delta Energy ","      Error Energy ","     Target")')
    1092            0 :             DO j1 = 1, SIZE(atom_info, 2)
    1093            0 :                DO j2 = 1, SIZE(atom_info, 2)
    1094            0 :                   DO i1 = 1, SIZE(atom_info, 1)
    1095            0 :                      DO i2 = 1, SIZE(atom_info, 1)
    1096            0 :                         IF ((j1 > j2) .OR. (j1 == j2 .AND. i1 >= i2)) CYCLE
    1097            0 :                         IF (ABS(dener(1, j1, j1, i1, i1)) < 0.000001_dp) CYCLE
    1098            0 :                         IF (ABS(dener(2, j1, j1, i1, i1)) < 0.000001_dp) CYCLE
    1099            0 :                         IF (ABS(dener(1, j2, j2, i2, i2)) < 0.000001_dp) CYCLE
    1100            0 :                         IF (ABS(dener(2, j2, j2, i2, i2)) < 0.000001_dp) CYCLE
    1101            0 :                         de = dener(2, j1, j2, i1, i2) - dener(1, j1, j2, i1, i2)
    1102            0 :                         WRITE (iunit, '(i6,i6,i10,i6,5X,F16.6,F19.6,F12.6)') j1, i1, j2, i2, dener(2, j1, j2, i1, i2), de, t_ener
    1103              :                      END DO
    1104              :                   END DO
    1105              :                END DO
    1106              :             END DO
    1107            0 :             WRITE (iunit, *)
    1108              :          END IF
    1109              : 
    1110         4326 :          WRITE (iunit, '(/," Number of target values reached: ",T69,i5," of ",i3)') INT(SUM(pval(5:8, :, :, :, :))), ntarget
    1111           14 :          WRITE (iunit, *)
    1112              :       END IF
    1113              : 
    1114           14 :       DEALLOCATE (x, pval, dener)
    1115              : 
    1116           28 :       DO i = 1, SIZE(wfn_guess, 1)
    1117           42 :          DO j = 1, SIZE(wfn_guess, 2)
    1118           28 :             IF (ASSOCIATED(wfn_guess(i, j)%wfn)) THEN
    1119           14 :                DEALLOCATE (wfn_guess(i, j)%wfn)
    1120              :             END IF
    1121              :          END DO
    1122              :       END DO
    1123           14 :       DEALLOCATE (wfn_guess)
    1124              : 
    1125              :       IF (ALLOCATED(xtob)) THEN
    1126              :          DEALLOCATE (xtob)
    1127              :       END IF
    1128              : 
    1129              :       IF (debug) THEN
    1130              :          CALL close_file(unit_number=iw)
    1131              :       END IF
    1132              : 
    1133           56 :    END SUBROUTINE atom_fit_pseudo
    1134              : 
    1135              : ! **************************************************************************************************
    1136              : !> \brief Fit NLCC density on core densities
    1137              : !> \param atom_info ...
    1138              : !> \param atom_refs ...
    1139              : !> \param gthpot ...
    1140              : !> \param iunit ...
    1141              : !> \param preopt_nlcc ...
    1142              : ! **************************************************************************************************
    1143            3 :    SUBROUTINE opt_nlcc_param(atom_info, atom_refs, gthpot, iunit, preopt_nlcc)
    1144              :       TYPE(atom_p_type), DIMENSION(:, :), POINTER        :: atom_info, atom_refs
    1145              :       TYPE(atom_gthpot_type), INTENT(inout)              :: gthpot
    1146              :       INTEGER, INTENT(in)                                :: iunit
    1147              :       LOGICAL, INTENT(in)                                :: preopt_nlcc
    1148              : 
    1149              :       INTEGER                                            :: i, im, j, k, method
    1150              :       REAL(KIND=dp)                                      :: rcov, zcore, zrc, zrch
    1151              :       TYPE(atom_type), POINTER                           :: aref, atom
    1152              :       TYPE(opgrid_type), POINTER                         :: corden, den, den1, den2, density
    1153              :       TYPE(opmat_type), POINTER                          :: denmat, dma, dmb
    1154              : 
    1155            3 :       CPASSERT(gthpot%nlcc)
    1156              : 
    1157            3 :       IF (iunit > 0) THEN
    1158            3 :          WRITE (iunit, '(/," Core density information")')
    1159            3 :          WRITE (iunit, '(A,T37,A,T55,A,T75,A)') "     State       Density:", "Full", "Rcov/2", "Rcov/4"
    1160              :       END IF
    1161              : 
    1162            3 :       rcov = ptable(atom_refs(1, 1)%atom%z)%covalent_radius*bohr
    1163            3 :       atom => atom_info(1, 1)%atom
    1164            3 :       NULLIFY (density)
    1165            3 :       CALL create_opgrid(density, atom%basis%grid)
    1166         1203 :       density%op = 0.0_dp
    1167            3 :       im = 0
    1168            6 :       DO i = 1, SIZE(atom_info, 1)
    1169            9 :          DO j = 1, SIZE(atom_info, 2)
    1170            3 :             atom => atom_info(i, j)%atom
    1171            3 :             aref => atom_refs(i, j)%atom
    1172            6 :             IF (atom_consistent_method(atom%method_type, atom%state%multiplicity)) THEN
    1173            3 :                NULLIFY (den, denmat)
    1174            3 :                CALL create_opgrid(den, atom%basis%grid)
    1175            3 :                CALL create_opmat(denmat, atom%basis%nbas)
    1176              : 
    1177            3 :                method = atom%method_type
    1178            2 :                SELECT CASE (method)
    1179              :                CASE (do_rks_atom, do_rhf_atom)
    1180              :                   CALL atom_denmat(denmat%op, aref%orbitals%wfn, &
    1181              :                                    atom%basis%nbas, atom%state%core, &
    1182            2 :                                    aref%state%maxl_occ, aref%state%maxn_occ)
    1183              :                CASE (do_uks_atom, do_uhf_atom)
    1184            1 :                   NULLIFY (dma, dmb)
    1185            1 :                   CALL create_opmat(dma, atom%basis%nbas)
    1186            1 :                   CALL create_opmat(dmb, atom%basis%nbas)
    1187              :                   CALL atom_denmat(dma%op, aref%orbitals%wfna, &
    1188              :                                    atom%basis%nbas, atom%state%core, &
    1189            1 :                                    aref%state%maxl_occ, aref%state%maxn_occ)
    1190              :                   CALL atom_denmat(dmb%op, aref%orbitals%wfnb, &
    1191              :                                    atom%basis%nbas, atom%state%core, &
    1192            1 :                                    aref%state%maxl_occ, aref%state%maxn_occ)
    1193        19693 :                   denmat%op = 0.5_dp*(dma%op + dmb%op)
    1194            1 :                   CALL release_opmat(dma)
    1195            1 :                   CALL release_opmat(dmb)
    1196              :                CASE (do_rohf_atom)
    1197            0 :                   CPABORT("ROHF not yet implemented for opt_nlcc_param")
    1198              :                CASE DEFAULT
    1199            3 :                   CPABORT("Unknown method for opt_nlcc_param")
    1200              :                END SELECT
    1201              : 
    1202            3 :                im = im + 1
    1203            3 :                CALL atom_density(den%op, denmat%op, atom%basis, aref%state%maxl_occ, typ="RHO")
    1204         2403 :                density%op = density%op + den%op
    1205            3 :                zcore = integrate_grid(den%op, atom%basis%grid)
    1206            3 :                zcore = fourpi*zcore
    1207            3 :                NULLIFY (den1, den2)
    1208            3 :                CALL create_opgrid(den1, atom%basis%grid)
    1209            3 :                CALL create_opgrid(den2, atom%basis%grid)
    1210         1203 :                den1%op = 0.0_dp
    1211         1203 :                den2%op = 0.0_dp
    1212         1203 :                DO k = 1, atom%basis%grid%nr
    1213         1200 :                   IF (atom%basis%grid%rad(k) < 0.50_dp*rcov) THEN
    1214          576 :                      den1%op(k) = den%op(k)
    1215              :                   END IF
    1216         1203 :                   IF (atom%basis%grid%rad(k) < 0.25_dp*rcov) THEN
    1217          484 :                      den2%op(k) = den%op(k)
    1218              :                   END IF
    1219              :                END DO
    1220            3 :                zrc = integrate_grid(den1%op, atom%basis%grid)
    1221            3 :                zrc = fourpi*zrc
    1222            3 :                zrch = integrate_grid(den2%op, atom%basis%grid)
    1223            3 :                zrch = fourpi*zrch
    1224            3 :                CALL release_opgrid(den1)
    1225            3 :                CALL release_opgrid(den2)
    1226            3 :                CALL release_opgrid(den)
    1227            3 :                CALL release_opmat(denmat)
    1228            3 :                IF (iunit > 0) THEN
    1229            3 :                   WRITE (iunit, '(2I5,T31,F10.4,T51,F10.4,T71,F10.4)') i, j, zcore, zrc, zrch
    1230              :                END IF
    1231              :             END IF
    1232              :          END DO
    1233              :       END DO
    1234         1203 :       density%op = density%op/REAL(im, KIND=dp)
    1235              :       !
    1236            3 :       IF (preopt_nlcc) THEN
    1237            1 :          gthpot%nexp_nlcc = 1
    1238           11 :          gthpot%nct_nlcc = 0
    1239           51 :          gthpot%cval_nlcc = 0._dp
    1240           11 :          gthpot%alpha_nlcc = 0._dp
    1241            1 :          gthpot%nct_nlcc(1) = 1
    1242            1 :          gthpot%cval_nlcc(1, 1) = 1.0_dp
    1243            1 :          gthpot%alpha_nlcc(1) = gthpot%rc
    1244            1 :          NULLIFY (corden)
    1245            1 :          CALL create_opgrid(corden, atom%basis%grid)
    1246            1 :          CALL atom_core_density(corden%op, atom%potential, typ="RHO", rr=atom%basis%grid%rad)
    1247          401 :          DO k = 1, atom%basis%grid%nr
    1248          401 :             IF (atom%basis%grid%rad(k) > 0.25_dp*rcov) THEN
    1249          226 :                corden%op(k) = 0.0_dp
    1250              :             END IF
    1251              :          END DO
    1252            1 :          zrc = integrate_grid(corden%op, atom%basis%grid)
    1253            1 :          zrc = fourpi*zrc
    1254            1 :          gthpot%cval_nlcc(1, 1) = zrch/zrc
    1255            1 :          CALL release_opgrid(corden)
    1256              :       END IF
    1257            3 :       CALL release_opgrid(density)
    1258              : 
    1259            3 :    END SUBROUTINE opt_nlcc_param
    1260              : 
    1261              : ! **************************************************************************************************
    1262              : !> \brief Low level routine to fit an atomic electron density.
    1263              : !> \param density  electron density on an atomic radial grid 'atom%basis%grid'
    1264              : !> \param grid     information about the atomic grid
    1265              : !> \param n        number of Gaussian basis functions
    1266              : !> \param pe       exponents of Gaussian basis functions
    1267              : !> \param co       computed expansion coefficients
    1268              : !> \param aerr     error in fitted density \f[\sqrt{\sum_{i}^{nr} (density_fitted(i)-density(i))^2 }\f]
    1269              : ! **************************************************************************************************
    1270         2888 :    SUBROUTINE density_fit(density, grid, n, pe, co, aerr)
    1271              :       REAL(dp), DIMENSION(:), INTENT(IN)                 :: density
    1272              :       TYPE(grid_atom_type)                               :: grid
    1273              :       INTEGER, INTENT(IN)                                :: n
    1274              :       REAL(dp), DIMENSION(:), INTENT(IN)                 :: pe
    1275              :       REAL(dp), DIMENSION(:), INTENT(INOUT)              :: co
    1276              :       REAL(dp), INTENT(OUT)                              :: aerr
    1277              : 
    1278              :       INTEGER                                            :: i, info, ip, iq, k, nr
    1279         2888 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: ipiv
    1280              :       REAL(dp)                                           :: a, rk, zval
    1281         2888 :       REAL(dp), ALLOCATABLE, DIMENSION(:)                :: den, uval
    1282         2888 :       REAL(dp), ALLOCATABLE, DIMENSION(:, :)             :: bf, smat, tval
    1283              : 
    1284         2888 :       nr = grid%nr
    1285        17328 :       ALLOCATE (bf(nr, n), den(nr))
    1286         2888 :       bf = 0._dp
    1287              : 
    1288        27650 :       DO i = 1, n
    1289        24762 :          a = pe(i)
    1290      9932450 :          DO k = 1, nr
    1291      9904800 :             rk = grid%rad(k)
    1292      9929562 :             bf(k, i) = EXP(-a*rk**2)
    1293              :          END DO
    1294              :       END DO
    1295              : 
    1296              :       ! total charge
    1297         2888 :       zval = integrate_grid(density, grid)
    1298              : 
    1299              :       ! allocate vectors and matrices for overlaps
    1300        23104 :       ALLOCATE (tval(n + 1, 1), uval(n), smat(n + 1, n + 1))
    1301        27650 :       DO i = 1, n
    1302        24762 :          uval(i) = (pi/pe(i))**1.5_dp
    1303        27650 :          tval(i, 1) = integrate_grid(density, bf(:, i), grid)
    1304              :       END DO
    1305         2888 :       tval(n + 1, 1) = zval
    1306              : 
    1307        27650 :       DO iq = 1, n
    1308       245808 :          DO ip = 1, n
    1309       242920 :             smat(ip, iq) = (pi/(pe(ip) + pe(iq)))**1.5_dp
    1310              :          END DO
    1311              :       END DO
    1312        27650 :       smat(1:n, n + 1) = uval(1:n)
    1313        27650 :       smat(n + 1, 1:n) = uval(1:n)
    1314         2888 :       smat(n + 1, n + 1) = 0._dp
    1315              : 
    1316         8664 :       ALLOCATE (ipiv(n + 1))
    1317         2888 :       CALL dgesv(n + 1, 1, smat, n + 1, ipiv, tval, n + 1, info)
    1318         2888 :       DEALLOCATE (ipiv)
    1319         2888 :       CPASSERT(info == 0)
    1320        27650 :       co(1:n) = tval(1:n, 1)
    1321              : 
    1322              :       ! calculate density
    1323         2888 :       den(:) = 0._dp
    1324        27650 :       DO i = 1, n
    1325      9932450 :          den(:) = den(:) + co(i)*bf(:, i)
    1326              :       END DO
    1327      1158088 :       den(:) = den(:)*fourpi
    1328      1158088 :       den(:) = (den(:) - density(:))**2
    1329         2888 :       aerr = SQRT(integrate_grid(den, grid))
    1330              : 
    1331         2888 :       DEALLOCATE (bf, den)
    1332              : 
    1333         2888 :       DEALLOCATE (tval, uval, smat)
    1334              : 
    1335         2888 :    END SUBROUTINE density_fit
    1336              : ! **************************************************************************************************
    1337              : !> \brief Low level routine to fit a basis set.
    1338              : !> \param atom_info ...
    1339              : !> \param basis ...
    1340              : !> \param pptype ...
    1341              : !> \param afun ...
    1342              : !> \param iw ...
    1343              : !> \param penalty ...
    1344              : ! **************************************************************************************************
    1345         2134 :    SUBROUTINE basis_fit(atom_info, basis, pptype, afun, iw, penalty)
    1346              :       TYPE(atom_p_type), DIMENSION(:, :), POINTER        :: atom_info
    1347              :       TYPE(atom_basis_type), POINTER                     :: basis
    1348              :       LOGICAL, INTENT(IN)                                :: pptype
    1349              :       REAL(dp), INTENT(OUT)                              :: afun
    1350              :       INTEGER, INTENT(IN)                                :: iw
    1351              :       LOGICAL, INTENT(IN)                                :: penalty
    1352              : 
    1353              :       INTEGER                                            :: do_eric, do_erie, i, im, in, l, nm, nn, &
    1354              :                                                             reltyp, zval
    1355              :       LOGICAL                                            :: eri_c, eri_e
    1356              :       REAL(KIND=dp)                                      :: amin
    1357              :       TYPE(atom_integrals), POINTER                      :: atint
    1358              :       TYPE(atom_potential_type), POINTER                 :: pot
    1359              :       TYPE(atom_type), POINTER                           :: atom
    1360              : 
    1361       454542 :       ALLOCATE (atint)
    1362              : 
    1363         2134 :       nn = SIZE(atom_info, 1)
    1364         2134 :       nm = SIZE(atom_info, 2)
    1365              : 
    1366              :       ! calculate integrals
    1367         2134 :       NULLIFY (pot)
    1368         2134 :       zval = 0
    1369         2134 :       eri_c = .FALSE.
    1370         2134 :       eri_e = .FALSE.
    1371         2134 :       DO in = 1, nn
    1372         2134 :          DO im = 1, nm
    1373         2134 :             atom => atom_info(in, im)%atom
    1374         2134 :             IF (pptype .EQV. atom%pp_calc) THEN
    1375         2134 :                pot => atom%potential
    1376         2134 :                zval = atom_info(in, im)%atom%z
    1377         2134 :                reltyp = atom_info(in, im)%atom%relativistic
    1378         2134 :                do_eric = atom_info(in, im)%atom%coulomb_integral_type
    1379         2134 :                do_erie = atom_info(in, im)%atom%exchange_integral_type
    1380         2134 :                IF (do_eric == do_analytic) eri_c = .TRUE.
    1381         2134 :                IF (do_erie == do_analytic) eri_e = .TRUE.
    1382              :                EXIT
    1383              :             END IF
    1384              :          END DO
    1385         2134 :          IF (ASSOCIATED(pot)) EXIT
    1386              :       END DO
    1387              :       ! general integrals
    1388         2134 :       CALL atom_int_setup(atint, basis, potential=pot, eri_coulomb=eri_c, eri_exchange=eri_e)
    1389              :       ! potential
    1390         2134 :       CALL atom_ppint_setup(atint, basis, potential=pot)
    1391         2134 :       IF (pptype) THEN
    1392         1030 :          NULLIFY (atint%tzora, atint%hdkh)
    1393              :       ELSE
    1394              :          ! relativistic correction terms
    1395         1104 :          CALL atom_relint_setup(atint, basis, reltyp, zcore=REAL(zval, dp))
    1396              :       END IF
    1397              : 
    1398         2134 :       afun = 0._dp
    1399              : 
    1400         4268 :       DO in = 1, nn
    1401         6402 :          DO im = 1, nm
    1402         2134 :             atom => atom_info(in, im)%atom
    1403         4268 :             IF (atom_consistent_method(atom%method_type, atom%state%multiplicity)) THEN
    1404         2134 :                IF (pptype .EQV. atom%pp_calc) THEN
    1405         2134 :                   CALL set_atom(atom, basis=basis)
    1406         2134 :                   CALL set_atom(atom, integrals=atint)
    1407         2134 :                   CALL calculate_atom(atom, iw)
    1408         2134 :                   afun = afun + atom%energy%etot*atom%weight
    1409              :                END IF
    1410              :             END IF
    1411              :          END DO
    1412              :       END DO
    1413              : 
    1414              :       ! penalty
    1415         2134 :       IF (penalty) THEN
    1416         6790 :          DO l = 0, lmat
    1417        21172 :             DO i = 1, basis%nbas(l) - 1
    1418        57504 :                amin = MINVAL(ABS(basis%am(i, l) - basis%am(i + 1:basis%nbas(l), l)))
    1419        14382 :                amin = amin/basis%am(i, l)
    1420        20202 :                afun = afun + 10._dp*EXP(-(20._dp*amin)**4)
    1421              :             END DO
    1422              :          END DO
    1423              :       END IF
    1424              : 
    1425         2134 :       CALL atom_int_release(atint)
    1426         2134 :       CALL atom_ppint_release(atint)
    1427         2134 :       CALL atom_relint_release(atint)
    1428              : 
    1429         2134 :       DEALLOCATE (atint)
    1430              : 
    1431         2134 :    END SUBROUTINE basis_fit
    1432              : ! **************************************************************************************************
    1433              : !> \brief Low level routine to fit a pseudo-potential.
    1434              : !> \param atom_info ...
    1435              : !> \param wfn_guess ...
    1436              : !> \param ppot ...
    1437              : !> \param afun ...
    1438              : !> \param wtot ...
    1439              : !> \param pval ...
    1440              : !> \param dener ...
    1441              : !> \param wen ...
    1442              : !> \param ten ...
    1443              : !> \param init ...
    1444              : ! **************************************************************************************************
    1445          335 :    SUBROUTINE pseudo_fit(atom_info, wfn_guess, ppot, afun, wtot, pval, dener, wen, ten, init)
    1446              :       TYPE(atom_p_type), DIMENSION(:, :), POINTER        :: atom_info
    1447              :       TYPE(wfn_init), DIMENSION(:, :), POINTER           :: wfn_guess
    1448              :       TYPE(atom_potential_type), POINTER                 :: ppot
    1449              :       REAL(dp), INTENT(OUT)                              :: afun
    1450              :       REAL(dp), INTENT(IN)                               :: wtot
    1451              :       REAL(dp), DIMENSION(:, :, 0:, :, :), INTENT(INOUT) :: pval
    1452              :       REAL(dp), DIMENSION(:, :, :, :, :), INTENT(INOUT)  :: dener
    1453              :       REAL(dp), INTENT(IN)                               :: wen, ten
    1454              :       LOGICAL, INTENT(IN)                                :: init
    1455              : 
    1456              :       REAL(KIND=dp), PARAMETER                           :: fde_thre = 1.0E-8_dp
    1457              : 
    1458              :       INTEGER                                            :: i, i1, i2, j, j1, j2, k, l, n, node
    1459              :       LOGICAL                                            :: converged, noguess, shift
    1460              :       REAL(KIND=dp)                                      :: charge, de, fde, pv, rcov, rcov1, rcov2, &
    1461              :                                                             tv
    1462              :       TYPE(atom_integrals), POINTER                      :: pp_int
    1463              :       TYPE(atom_type), POINTER                           :: atom
    1464              : 
    1465          335 :       afun = 0._dp
    1466       183915 :       pval = 0._dp
    1467         1675 :       dener(1, :, :, :, :) = 0._dp
    1468              : 
    1469          335 :       noguess = .NOT. init
    1470              : 
    1471          335 :       pp_int => atom_info(1, 1)%atom%integrals
    1472          335 :       CALL atom_ppint_release(pp_int)
    1473          335 :       CALL atom_ppint_setup(pp_int, atom_info(1, 1)%atom%basis, potential=ppot)
    1474              : 
    1475          670 :       DO i = 1, SIZE(atom_info, 1)
    1476         1005 :          DO j = 1, SIZE(atom_info, 2)
    1477          335 :             atom => atom_info(i, j)%atom
    1478          670 :             IF (atom_consistent_method(atom%method_type, atom%state%multiplicity)) THEN
    1479          335 :                IF (noguess) THEN
    1480       163221 :                   atom%orbitals%wfn = wfn_guess(i, j)%wfn
    1481              :                END IF
    1482          335 :                CALL set_atom(atom, integrals=pp_int, potential=ppot)
    1483          335 :                CALL calculate_atom(atom, 0, noguess=noguess, converged=converged)
    1484          335 :                IF (.NOT. converged) THEN
    1485            0 :                   CALL calculate_atom(atom, 0, noguess=.FALSE., converged=shift)
    1486            0 :                   IF (.NOT. shift) THEN
    1487            0 :                      atom%orbitals%ener(:, :) = 1.5_dp*atom%orbitals%ener(:, :) + 0.5_dp
    1488              :                   END IF
    1489              :                END IF
    1490          335 :                dener(1, j, j, i, i) = atom%energy%etot
    1491         1165 :                DO l = 0, atom%state%maxl_calc
    1492          830 :                   n = atom%state%maxn_calc(l)
    1493         2539 :                   DO k = 1, n
    1494         2204 :                      IF (atom%state%multiplicity == -1) THEN
    1495              :                         !no spin polarization
    1496         1242 :                         rcov = atom%orbitals%rcmax(k, l, 1)
    1497         1242 :                         tv = atom%orbitals%crefene(k, l, 1)
    1498         1242 :                         de = ABS(atom%orbitals%ener(k, l) - atom%orbitals%refene(k, l, 1))
    1499         1242 :                         fde = get_error_value(de, tv)
    1500         1242 :                         IF (fde < fde_thre) pval(5, k, l, j, i) = 1._dp
    1501         1242 :                         pv = atom%weight*atom%orbitals%wrefene(k, l, 1)*fde
    1502         1242 :                         afun = afun + pv
    1503         1242 :                         pval(1, k, l, j, i) = pv
    1504         1242 :                         IF (atom%orbitals%wrefchg(k, l, 1) > 0._dp) THEN
    1505          967 :                            CALL atom_orbital_charge(charge, atom%orbitals%wfn(:, k, l), rcov, l, atom%basis)
    1506          967 :                            de = ABS(charge - atom%orbitals%refchg(k, l, 1))
    1507          967 :                            fde = get_error_value(de, 25._dp*tv)
    1508          967 :                            IF (fde < fde_thre) pval(6, k, l, j, i) = 1._dp
    1509          967 :                            pv = atom%weight*atom%orbitals%wrefchg(k, l, 1)*fde
    1510          967 :                            afun = afun + pv
    1511          967 :                            pval(2, k, l, j, i) = pv
    1512              :                         END IF
    1513         1242 :                         IF (atom%orbitals%wrefnod(k, l, 1) > 0._dp) THEN
    1514          527 :                            CALL atom_orbital_nodes(node, atom%orbitals%wfn(:, k, l), 2._dp*rcov, l, atom%basis)
    1515              :                            afun = afun + atom%weight*atom%orbitals%wrefnod(k, l, 1)* &
    1516          527 :                                   ABS(REAL(node, dp) - atom%orbitals%refnod(k, l, 1))
    1517              :                         END IF
    1518         1242 :                         IF (l == 0) THEN
    1519          623 :                            IF (atom%orbitals%wpsir0(k, 1) > 0._dp) THEN
    1520          214 :                               CALL atom_wfnr0(pv, atom%orbitals%wfn(:, k, l), atom%basis)
    1521          214 :                               IF (ABS(atom%orbitals%tpsir0(k, 1)) > 1.e-14_dp) THEN
    1522            0 :                                  IF (ABS(pv) > ABS(atom%orbitals%tpsir0(k, 1))) THEN
    1523            0 :                                     pv = 0.0_dp
    1524              :                                  ELSE
    1525            0 :                                     pv = 10._dp*(ABS(pv) - ABS(atom%orbitals%tpsir0(k, 1)))
    1526              :                                  END IF
    1527            0 :                                  pv = atom%weight*atom%orbitals%wpsir0(k, 1)*pv*pv
    1528              :                               ELSE
    1529          214 :                                  pv = atom%weight*atom%orbitals%wpsir0(k, 1)*pv*pv*100._dp
    1530              :                               END IF
    1531          214 :                               afun = afun + pv
    1532              :                            END IF
    1533              :                         END IF
    1534              :                      ELSE
    1535              :                         !spin polarization
    1536          132 :                         rcov1 = atom%orbitals%rcmax(k, l, 1)
    1537          132 :                         rcov2 = atom%orbitals%rcmax(k, l, 2)
    1538          132 :                         tv = atom%orbitals%crefene(k, l, 1)
    1539          132 :                         de = ABS(atom%orbitals%enera(k, l) - atom%orbitals%refene(k, l, 1))
    1540          132 :                         fde = get_error_value(de, tv)
    1541          132 :                         IF (fde < fde_thre) pval(5, k, l, j, i) = 1._dp
    1542          132 :                         pv = atom%weight*atom%orbitals%wrefene(k, l, 1)*fde
    1543          132 :                         afun = afun + pv
    1544          132 :                         pval(1, k, l, j, i) = pv
    1545          132 :                         tv = atom%orbitals%crefene(k, l, 2)
    1546          132 :                         de = ABS(atom%orbitals%enerb(k, l) - atom%orbitals%refene(k, l, 2))
    1547          132 :                         fde = get_error_value(de, tv)
    1548          132 :                         IF (fde < fde_thre) pval(7, k, l, j, i) = 1._dp
    1549          132 :                         pv = atom%weight*atom%orbitals%wrefene(k, l, 2)*fde
    1550          132 :                         afun = afun + pv
    1551          132 :                         pval(3, k, l, j, i) = pv
    1552          132 :                         IF (atom%orbitals%wrefchg(k, l, 1) > 0._dp) THEN
    1553           88 :                            CALL atom_orbital_charge(charge, atom%orbitals%wfna(:, k, l), rcov1, l, atom%basis)
    1554           88 :                            de = ABS(charge - atom%orbitals%refchg(k, l, 1))
    1555           88 :                            fde = get_error_value(de, 25._dp*tv)
    1556           88 :                            IF (fde < fde_thre) pval(6, k, l, j, i) = 1._dp
    1557           88 :                            pv = atom%weight*atom%orbitals%wrefchg(k, l, 1)*fde
    1558           88 :                            afun = afun + pv
    1559           88 :                            pval(2, k, l, j, i) = pv
    1560              :                         END IF
    1561          132 :                         IF (atom%orbitals%wrefchg(k, l, 2) > 0._dp) THEN
    1562           88 :                            CALL atom_orbital_charge(charge, atom%orbitals%wfnb(:, k, l), rcov2, l, atom%basis)
    1563           88 :                            de = ABS(charge - atom%orbitals%refchg(k, l, 2))
    1564           88 :                            fde = get_error_value(de, 25._dp*tv)
    1565           88 :                            IF (fde < fde_thre) pval(8, k, l, j, i) = 1._dp
    1566           88 :                            pv = atom%weight*atom%orbitals%wrefchg(k, l, 2)*fde
    1567           88 :                            afun = afun + pv
    1568           88 :                            pval(4, k, l, j, i) = pv
    1569              :                         END IF
    1570          132 :                         IF (atom%orbitals%wrefnod(k, l, 1) > 0._dp) THEN
    1571           44 :                            CALL atom_orbital_nodes(node, atom%orbitals%wfna(:, k, l), 2._dp*rcov1, l, atom%basis)
    1572              :                            afun = afun + atom%weight*atom%orbitals%wrefnod(k, l, 1)* &
    1573           44 :                                   ABS(REAL(node, dp) - atom%orbitals%refnod(k, l, 1))
    1574              :                         END IF
    1575          132 :                         IF (atom%orbitals%wrefnod(k, l, 2) > 0._dp) THEN
    1576           22 :                            CALL atom_orbital_nodes(node, atom%orbitals%wfnb(:, k, l), 2._dp*rcov2, l, atom%basis)
    1577              :                            afun = afun + atom%weight*atom%orbitals%wrefnod(k, l, 2)* &
    1578           22 :                                   ABS(REAL(node, dp) - atom%orbitals%refnod(k, l, 2))
    1579              :                         END IF
    1580          132 :                         IF (l == 0) THEN
    1581           66 :                            IF (atom%orbitals%wpsir0(k, 1) > 0._dp) THEN
    1582           22 :                               CALL atom_wfnr0(pv, atom%orbitals%wfna(:, k, l), atom%basis)
    1583           22 :                               IF (ABS(atom%orbitals%tpsir0(k, 1)) > 1.e-14_dp) THEN
    1584            0 :                                  IF (ABS(pv) > ABS(atom%orbitals%tpsir0(k, 1))) THEN
    1585            0 :                                     pv = 0.0_dp
    1586              :                                  ELSE
    1587            0 :                                     pv = 10._dp*(ABS(pv) - ABS(atom%orbitals%tpsir0(k, 1)))
    1588              :                                  END IF
    1589            0 :                                  pv = atom%weight*atom%orbitals%wpsir0(k, 1)*pv*pv
    1590              :                               ELSE
    1591           22 :                                  pv = atom%weight*atom%orbitals%wpsir0(k, 1)*pv*pv*100._dp
    1592              :                               END IF
    1593           22 :                               afun = afun + pv
    1594              :                            END IF
    1595           66 :                            IF (atom%orbitals%wpsir0(k, 2) > 0._dp) THEN
    1596           22 :                               CALL atom_wfnr0(pv, atom%orbitals%wfnb(:, k, l), atom%basis)
    1597           22 :                               IF (ABS(atom%orbitals%tpsir0(k, 2)) > 1.e-14_dp) THEN
    1598            0 :                                  IF (ABS(pv) > ABS(atom%orbitals%tpsir0(k, 2))) THEN
    1599            0 :                                     pv = 0.0_dp
    1600              :                                  ELSE
    1601            0 :                                     pv = 10._dp*(ABS(pv) - ABS(atom%orbitals%tpsir0(k, 2)))
    1602              :                                  END IF
    1603            0 :                                  pv = atom%weight*atom%orbitals%wpsir0(k, 2)*pv*pv
    1604              :                               ELSE
    1605           22 :                                  pv = atom%weight*atom%orbitals%wpsir0(k, 2)*pv*pv*100._dp
    1606              :                               END IF
    1607           22 :                               afun = afun + pv
    1608              :                            END IF
    1609              :                         END IF
    1610              :                      END IF
    1611              :                   END DO
    1612              :                END DO
    1613              :             END IF
    1614              :          END DO
    1615              :       END DO
    1616              : 
    1617              :       ! energy differences
    1618          670 :       DO j1 = 1, SIZE(atom_info, 2)
    1619         1005 :          DO j2 = 1, SIZE(atom_info, 2)
    1620         1005 :             DO i1 = 1, SIZE(atom_info, 1)
    1621          670 :                DO i2 = i1 + 1, SIZE(atom_info, 1)
    1622            0 :                   IF ((j1 > j2) .OR. (j1 == j2 .AND. i1 >= i2)) CYCLE
    1623            0 :                   dener(1, j1, j2, i1, i2) = dener(1, j1, j1, i1, i1) - dener(1, j2, j2, i2, i2)
    1624            0 :                   de = ABS(dener(2, j1, j2, i1, i2) - dener(1, j1, j2, i1, i2))
    1625            0 :                   fde = get_error_value(de, ten)
    1626          335 :                   afun = afun + wen*fde
    1627              :                END DO
    1628              :             END DO
    1629              :          END DO
    1630              :       END DO
    1631              : 
    1632              :       ! scaling
    1633          335 :       afun = afun/wtot
    1634              : 
    1635          335 :    END SUBROUTINE pseudo_fit
    1636              : ! **************************************************************************************************
    1637              : !> \brief Compute the squared relative error.
    1638              : !> \param fval     actual value
    1639              : !> \param ftarget  target value
    1640              : !> \return squared relative error
    1641              : ! **************************************************************************************************
    1642         2649 :    FUNCTION get_error_value(fval, ftarget) RESULT(errval)
    1643              :       REAL(KIND=dp), INTENT(in)                          :: fval, ftarget
    1644              :       REAL(KIND=dp)                                      :: errval
    1645              : 
    1646         2649 :       CPASSERT(fval >= 0.0_dp)
    1647              : 
    1648         2649 :       IF (fval <= ftarget) THEN
    1649              :          errval = 0.0_dp
    1650              :       ELSE
    1651         1474 :          errval = (fval - ftarget)/MAX(ftarget, 1.e-10_dp)
    1652         1474 :          errval = errval*errval
    1653              :       END IF
    1654              : 
    1655         2649 :    END FUNCTION get_error_value
    1656              : ! **************************************************************************************************
    1657              : !> \brief ...
    1658              : !> \param pvec ...
    1659              : !> \param nval ...
    1660              : !> \param gthpot ...
    1661              : !> \param noopt_nlcc ...
    1662              : ! **************************************************************************************************
    1663           14 :    SUBROUTINE get_pseudo_param(pvec, nval, gthpot, noopt_nlcc)
    1664              :       REAL(KIND=dp), DIMENSION(:), INTENT(out)           :: pvec
    1665              :       INTEGER, INTENT(out)                               :: nval
    1666              :       TYPE(atom_gthpot_type), INTENT(in)                 :: gthpot
    1667              :       LOGICAL, INTENT(in)                                :: noopt_nlcc
    1668              : 
    1669              :       INTEGER                                            :: i, ival, j, l, n
    1670              : 
    1671           14 :       IF (gthpot%lsdpot) THEN
    1672            0 :          pvec = 0
    1673            0 :          ival = 0
    1674            0 :          DO j = 1, gthpot%nexp_lsd
    1675            0 :             ival = ival + 1
    1676            0 :             pvec(ival) = rcpro(-1, gthpot%alpha_lsd(j))
    1677            0 :             DO i = 1, gthpot%nct_lsd(j)
    1678            0 :                ival = ival + 1
    1679            0 :                pvec(ival) = gthpot%cval_lsd(i, j)
    1680              :             END DO
    1681              :          END DO
    1682              :       ELSE
    1683         2814 :          pvec = 0
    1684           14 :          ival = 1
    1685           14 :          pvec(ival) = rcpro(-1, gthpot%rc)
    1686           42 :          DO i = 1, gthpot%ncl
    1687           28 :             ival = ival + 1
    1688           42 :             pvec(ival) = gthpot%cl(i)
    1689              :          END DO
    1690           14 :          IF (gthpot%lpotextended) THEN
    1691            0 :             DO j = 1, gthpot%nexp_lpot
    1692            0 :                ival = ival + 1
    1693            0 :                pvec(ival) = rcpro(-1, gthpot%alpha_lpot(j))
    1694            0 :                DO i = 1, gthpot%nct_lpot(j)
    1695            0 :                   ival = ival + 1
    1696            0 :                   pvec(ival) = gthpot%cval_lpot(i, j)
    1697              :                END DO
    1698              :             END DO
    1699              :          END IF
    1700           14 :          IF (gthpot%nlcc .AND. (.NOT. noopt_nlcc)) THEN
    1701            4 :             DO j = 1, gthpot%nexp_nlcc
    1702            2 :                ival = ival + 1
    1703            2 :                pvec(ival) = rcpro(-1, gthpot%alpha_nlcc(j))
    1704            6 :                DO i = 1, gthpot%nct_nlcc(j)
    1705            2 :                   ival = ival + 1
    1706            4 :                   pvec(ival) = gthpot%cval_nlcc(i, j)
    1707              :                END DO
    1708              :             END DO
    1709              :          END IF
    1710           98 :          DO l = 0, lmat
    1711           98 :             IF (gthpot%nl(l) > 0) THEN
    1712           14 :                ival = ival + 1
    1713           14 :                pvec(ival) = rcpro(-1, gthpot%rcnl(l))
    1714              :             END IF
    1715              :          END DO
    1716           98 :          DO l = 0, lmat
    1717           98 :             IF (gthpot%nl(l) > 0) THEN
    1718              :                n = gthpot%nl(l)
    1719           28 :                DO i = 1, n
    1720           42 :                   DO j = i, n
    1721           14 :                      ival = ival + 1
    1722           28 :                      pvec(ival) = gthpot%hnl(i, j, l)
    1723              :                   END DO
    1724              :                END DO
    1725              :             END IF
    1726              :          END DO
    1727              :       END IF
    1728           14 :       nval = ival
    1729              : 
    1730           14 :    END SUBROUTINE get_pseudo_param
    1731              : 
    1732              : ! **************************************************************************************************
    1733              : !> \brief ...
    1734              : !> \param pvec ...
    1735              : !> \param gthpot ...
    1736              : !> \param noopt_nlcc ...
    1737              : ! **************************************************************************************************
    1738          371 :    SUBROUTINE put_pseudo_param(pvec, gthpot, noopt_nlcc)
    1739              :       REAL(KIND=dp), DIMENSION(:), INTENT(in)            :: pvec
    1740              :       TYPE(atom_gthpot_type), INTENT(inout)              :: gthpot
    1741              :       LOGICAL, INTENT(in)                                :: noopt_nlcc
    1742              : 
    1743              :       INTEGER                                            :: i, ival, j, l, n
    1744              : 
    1745          371 :       IF (gthpot%lsdpot) THEN
    1746            0 :          ival = 0
    1747            0 :          DO j = 1, gthpot%nexp_lsd
    1748            0 :             ival = ival + 1
    1749            0 :             gthpot%alpha_lsd(j) = rcpro(1, pvec(ival))
    1750            0 :             DO i = 1, gthpot%nct_lsd(j)
    1751            0 :                ival = ival + 1
    1752            0 :                gthpot%cval_lsd(i, j) = pvec(ival)
    1753              :             END DO
    1754              :          END DO
    1755              :       ELSE
    1756          371 :          ival = 1
    1757          371 :          gthpot%rc = rcpro(1, pvec(ival))
    1758         1113 :          DO i = 1, gthpot%ncl
    1759          742 :             ival = ival + 1
    1760         1113 :             gthpot%cl(i) = pvec(ival)
    1761              :          END DO
    1762          371 :          IF (gthpot%lpotextended) THEN
    1763            0 :             DO j = 1, gthpot%nexp_lpot
    1764            0 :                ival = ival + 1
    1765            0 :                gthpot%alpha_lpot(j) = rcpro(1, pvec(ival))
    1766            0 :                DO i = 1, gthpot%nct_lpot(j)
    1767            0 :                   ival = ival + 1
    1768            0 :                   gthpot%cval_lpot(i, j) = pvec(ival)
    1769              :                END DO
    1770              :             END DO
    1771              :          END IF
    1772          371 :          IF (gthpot%nlcc .AND. (.NOT. noopt_nlcc)) THEN
    1773           92 :             DO j = 1, gthpot%nexp_nlcc
    1774           46 :                ival = ival + 1
    1775           46 :                gthpot%alpha_nlcc(j) = rcpro(1, pvec(ival))
    1776          138 :                DO i = 1, gthpot%nct_nlcc(j)
    1777           46 :                   ival = ival + 1
    1778           92 :                   gthpot%cval_nlcc(i, j) = pvec(ival)
    1779              :                END DO
    1780              :             END DO
    1781              :          END IF
    1782         2597 :          DO l = 0, lmat
    1783         2597 :             IF (gthpot%nl(l) > 0) THEN
    1784          379 :                ival = ival + 1
    1785          379 :                gthpot%rcnl(l) = rcpro(1, pvec(ival))
    1786              :             END IF
    1787              :          END DO
    1788         2597 :          DO l = 0, lmat
    1789         2597 :             IF (gthpot%nl(l) > 0) THEN
    1790              :                n = gthpot%nl(l)
    1791          758 :                DO i = 1, n
    1792         1137 :                   DO j = i, n
    1793          379 :                      ival = ival + 1
    1794          758 :                      gthpot%hnl(i, j, l) = pvec(ival)
    1795              :                   END DO
    1796              :                END DO
    1797              :             END IF
    1798              :          END DO
    1799              :       END IF
    1800              : 
    1801          371 :    END SUBROUTINE put_pseudo_param
    1802              : 
    1803              : ! **************************************************************************************************
    1804              : !> \brief Transform xval according to the following rules:
    1805              : !>        direct  (id == +1): yval = 2 tanh^{2}(xval / 10)
    1806              : !>        inverse (id == -1): yval = 10 arctanh(\sqrt{xval/2})
    1807              : !> \param id    direction of the transformation
    1808              : !> \param xval  value to transform
    1809              : !> \return      transformed value
    1810              : ! **************************************************************************************************
    1811          826 :    FUNCTION rcpro(id, xval) RESULT(yval)
    1812              :       INTEGER, INTENT(IN)                                :: id
    1813              :       REAL(dp), INTENT(IN)                               :: xval
    1814              :       REAL(dp)                                           :: yval
    1815              : 
    1816              :       REAL(dp)                                           :: x1, x2
    1817              : 
    1818          826 :       IF (id == 1) THEN
    1819          796 :          yval = 2.0_dp*TANH(0.1_dp*xval)**2
    1820           30 :       ELSE IF (id == -1) THEN
    1821           30 :          x1 = SQRT(xval/2.0_dp)
    1822           30 :          CPASSERT(x1 <= 1._dp)
    1823           30 :          x2 = 0.5_dp*LOG((1._dp + x1)/(1._dp - x1))
    1824           30 :          yval = x2/0.1_dp
    1825              :       ELSE
    1826            0 :          CPABORT("wrong id")
    1827              :       END IF
    1828          826 :    END FUNCTION rcpro
    1829              : 
    1830              : ! **************************************************************************************************
    1831              : !> \brief ...
    1832              : !> \param atom ...
    1833              : !> \param num_gau ...
    1834              : !> \param num_pol ...
    1835              : !> \param iunit ...
    1836              : !> \param powell_section ...
    1837              : !> \param results ...
    1838              : ! **************************************************************************************************
    1839            1 :    SUBROUTINE atom_fit_kgpot(atom, num_gau, num_pol, iunit, powell_section, results)
    1840              :       TYPE(atom_type), POINTER                           :: atom
    1841              :       INTEGER, INTENT(IN)                                :: num_gau, num_pol, iunit
    1842              :       TYPE(section_vals_type), OPTIONAL, POINTER         :: powell_section
    1843              :       REAL(KIND=dp), DIMENSION(:), OPTIONAL              :: results
    1844              : 
    1845              :       REAL(KIND=dp), PARAMETER                           :: t23 = 2._dp/3._dp
    1846              : 
    1847              :       INTEGER                                            :: i, ig, ip, iw, j, n10
    1848              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: x
    1849              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: co
    1850              :       TYPE(opgrid_type), POINTER                         :: density
    1851              :       TYPE(opt_state_type)                               :: ostate
    1852              : 
    1853              : ! at least one parameter to be optimized
    1854              : 
    1855            0 :       CPASSERT(num_pol*num_gau > 0)
    1856              : 
    1857            6 :       ALLOCATE (co(num_pol + 1, num_gau), x(num_pol*num_gau + num_gau))
    1858            1 :       co = 0._dp
    1859              : 
    1860              :       ! calculate density
    1861            1 :       NULLIFY (density)
    1862            1 :       CALL create_opgrid(density, atom%basis%grid)
    1863              :       CALL atom_denmat(atom%orbitals%pmat, atom%orbitals%wfn, atom%basis%nbas, atom%state%occupation, &
    1864            1 :                        atom%state%maxl_occ, atom%state%maxn_occ)
    1865            1 :       CALL atom_density(density%op, atom%orbitals%pmat, atom%basis, atom%state%maxl_occ, typ="RHO")
    1866              :       ! target functional
    1867          401 :       density%op = t23*(0.3_dp*(3.0_dp*pi*pi)**t23)*density%op**t23
    1868              : 
    1869              :       ! initiallize parameter
    1870            1 :       ostate%nf = 0
    1871            1 :       ostate%nvar = num_pol*num_gau + num_gau
    1872            3 :       DO i = 1, num_gau
    1873            2 :          co(1, i) = 0.5_dp + REAL(i - 1, KIND=dp)
    1874            2 :          co(2, i) = 1.0_dp
    1875            3 :          DO j = 3, num_pol + 1
    1876            2 :             co(j, i) = 0.1_dp
    1877              :          END DO
    1878              :       END DO
    1879            1 :       CALL putvar(x, co, num_pol, num_gau)
    1880              : 
    1881            1 :       IF (PRESENT(powell_section)) THEN
    1882            1 :          CALL section_vals_val_get(powell_section, "ACCURACY", r_val=ostate%rhoend)
    1883            1 :          CALL section_vals_val_get(powell_section, "STEP_SIZE", r_val=ostate%rhobeg)
    1884            1 :          CALL section_vals_val_get(powell_section, "MAX_FUN", i_val=ostate%maxfun)
    1885              :       ELSE
    1886            0 :          ostate%rhoend = 1.e-8_dp
    1887            0 :          ostate%rhobeg = 5.e-2_dp
    1888            0 :          ostate%maxfun = 1000
    1889              :       END IF
    1890            1 :       ostate%iprint = 1
    1891            1 :       ostate%unit = iunit
    1892              : 
    1893            1 :       ostate%state = 0
    1894            1 :       IF (iunit > 0) THEN
    1895            1 :          WRITE (iunit, '(/," ",13("*")," Approximated Nonadditive Kinetic Energy Functional ",14("*"))')
    1896            1 :          WRITE (iunit, '(" POWELL| Start optimization procedure")')
    1897              :       END IF
    1898              :       n10 = 50
    1899              : 
    1900              :       DO
    1901              : 
    1902          302 :          IF (ostate%state == 2) THEN
    1903          299 :             CALL getvar(x, co, num_pol, num_gau)
    1904          299 :             CALL kgpot_fit(density, num_gau, num_pol, co, ostate%f)
    1905              :          END IF
    1906              : 
    1907          302 :          IF (ostate%state == -1) EXIT
    1908              : 
    1909          301 :          CALL powell_optimize(ostate%nvar, x, ostate)
    1910              : 
    1911          302 :          IF (MOD(ostate%nf, n10) == 0 .AND. iunit > 0) THEN
    1912              :             WRITE (iunit, '(" POWELL| Reached",i4,"% of maximal function calls",T66,G15.7)') &
    1913            5 :                INT(REAL(ostate%nf, dp)/REAL(ostate%maxfun, dp)*100._dp), ostate%fopt
    1914              :          END IF
    1915              : 
    1916              :       END DO
    1917              : 
    1918            1 :       ostate%state = 8
    1919            1 :       CALL powell_optimize(ostate%nvar, x, ostate)
    1920            1 :       CALL getvar(x, co, num_pol, num_gau)
    1921              : 
    1922            1 :       CALL release_opgrid(density)
    1923              : 
    1924            1 :       IF (iunit > 0) THEN
    1925            1 :          WRITE (iunit, '(" POWELL| Number of function evaluations",T71,I10)') ostate%nf
    1926            1 :          WRITE (iunit, '(" POWELL| Final value of function",T61,G20.10)') ostate%fopt
    1927            1 :          WRITE (iunit, '(" Optimized local potential of approximated nonadditive kinetic energy functional")')
    1928            3 :          DO ig = 1, num_gau
    1929            2 :             WRITE (iunit, '(I2,T15,"Gaussian polynomial expansion",T66,"Rc=",F12.4)') ig, co(1, ig)
    1930            3 :             WRITE (iunit, '(T15,"Coefficients",T33,4F12.4)') (co(1 + ip, ig), ip=1, num_pol)
    1931              :          END DO
    1932              :       END IF
    1933              : 
    1934            1 :       CALL open_file(file_name="FIT_RESULT", file_status="UNKNOWN", file_action="WRITE", unit_number=iw)
    1935            1 :       WRITE (iw, *) ptable(atom%z)%symbol
    1936            1 :       WRITE (iw, *) num_gau, num_pol
    1937            3 :       DO ig = 1, num_gau
    1938            3 :          WRITE (iw, '(T10,F12.4,6X,4F12.4)') (co(ip, ig), ip=1, num_pol + 1)
    1939              :       END DO
    1940            1 :       CALL close_file(unit_number=iw)
    1941              : 
    1942            1 :       IF (PRESENT(results)) THEN
    1943            0 :          CPASSERT(SIZE(results) >= SIZE(x))
    1944            0 :          results = x
    1945              :       END IF
    1946              : 
    1947            1 :       DEALLOCATE (co, x)
    1948              : 
    1949            2 :    END SUBROUTINE atom_fit_kgpot
    1950              : 
    1951              : ! **************************************************************************************************
    1952              : !> \brief ...
    1953              : !> \param kgpot ...
    1954              : !> \param ng ...
    1955              : !> \param np ...
    1956              : !> \param cval ...
    1957              : !> \param aerr ...
    1958              : ! **************************************************************************************************
    1959          299 :    SUBROUTINE kgpot_fit(kgpot, ng, np, cval, aerr)
    1960              :       TYPE(opgrid_type), POINTER                         :: kgpot
    1961              :       INTEGER, INTENT(IN)                                :: ng, np
    1962              :       REAL(dp), DIMENSION(:, :), INTENT(INOUT)           :: cval
    1963              :       REAL(dp), INTENT(OUT)                              :: aerr
    1964              : 
    1965              :       INTEGER                                            :: i, ig, ip, n
    1966              :       REAL(KIND=dp)                                      :: pc, rc
    1967          299 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: pval
    1968              : 
    1969          299 :       n = kgpot%grid%nr
    1970          897 :       ALLOCATE (pval(n))
    1971          299 :       pval = 0.0_dp
    1972       119899 :       DO i = 1, n
    1973       359099 :          DO ig = 1, ng
    1974       239200 :             rc = kgpot%grid%rad(i)/cval(1, ig)
    1975       239200 :             pc = 0.0_dp
    1976       478400 :             DO ip = 1, np
    1977       478400 :                pc = pc + cval(ip + 1, ig)*rc**(2*ip - 2)
    1978              :             END DO
    1979       358800 :             pval(i) = pval(i) + pc*EXP(-0.5_dp*rc*rc)
    1980              :          END DO
    1981              :       END DO
    1982       119899 :       pval(1:n) = (pval(1:n) - kgpot%op(1:n))**2
    1983       119899 :       aerr = fourpi*SUM(pval(1:n)*kgpot%grid%wr(1:n))
    1984              : 
    1985          299 :       DEALLOCATE (pval)
    1986              : 
    1987          299 :    END SUBROUTINE kgpot_fit
    1988              : 
    1989              : ! **************************************************************************************************
    1990              : !> \brief ...
    1991              : !> \param xvar ...
    1992              : !> \param cvar ...
    1993              : !> \param np ...
    1994              : !> \param ng ...
    1995              : ! **************************************************************************************************
    1996          300 :    PURE SUBROUTINE getvar(xvar, cvar, np, ng)
    1997              :       REAL(KIND=dp), DIMENSION(:), INTENT(in)            :: xvar
    1998              :       REAL(KIND=dp), DIMENSION(:, :), INTENT(inout)      :: cvar
    1999              :       INTEGER, INTENT(IN)                                :: np, ng
    2000              : 
    2001              :       INTEGER                                            :: ig, ii, ip
    2002              : 
    2003          300 :       ii = 0
    2004          900 :       DO ig = 1, ng
    2005          600 :          ii = ii + 1
    2006          600 :          cvar(1, ig) = xvar(ii)
    2007         1500 :          DO ip = 1, np
    2008          600 :             ii = ii + 1
    2009         1200 :             cvar(ip + 1, ig) = xvar(ii)**2
    2010              :          END DO
    2011              :       END DO
    2012              : 
    2013          300 :    END SUBROUTINE getvar
    2014              : 
    2015              : ! **************************************************************************************************
    2016              : !> \brief ...
    2017              : !> \param xvar ...
    2018              : !> \param cvar ...
    2019              : !> \param np ...
    2020              : !> \param ng ...
    2021              : ! **************************************************************************************************
    2022            1 :    PURE SUBROUTINE putvar(xvar, cvar, np, ng)
    2023              :       REAL(KIND=dp), DIMENSION(:), INTENT(inout)         :: xvar
    2024              :       REAL(KIND=dp), DIMENSION(:, :), INTENT(in)         :: cvar
    2025              :       INTEGER, INTENT(IN)                                :: np, ng
    2026              : 
    2027              :       INTEGER                                            :: ig, ii, ip
    2028              : 
    2029            1 :       ii = 0
    2030            3 :       DO ig = 1, ng
    2031            2 :          ii = ii + 1
    2032            2 :          xvar(ii) = cvar(1, ig)
    2033            5 :          DO ip = 1, np
    2034            2 :             ii = ii + 1
    2035            4 :             xvar(ii) = SQRT(ABS(cvar(ip + 1, ig)))
    2036              :          END DO
    2037              :       END DO
    2038              : 
    2039            1 :    END SUBROUTINE putvar
    2040              : 
    2041            0 : END MODULE atom_fit
        

Generated by: LCOV version 2.0-1