LCOV - code coverage report
Current view: top level - src - qs_ot_complex_ref_unittest.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 99.4 % 517 514
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 15 15

            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 Focused regressions for complex k-point OT derivatives.
      10              : ! **************************************************************************************************
      11          192 : PROGRAM qs_ot_complex_ref_unittest
      12            2 :    USE bibliography,                    ONLY: add_all_references
      13              :    USE cp_blacs_env,                    ONLY: cp_blacs_env_create,&
      14              :                                               cp_blacs_env_release,&
      15              :                                               cp_blacs_env_type
      16              :    USE cp_dbcsr_api,                    ONLY: &
      17              :         dbcsr_create, dbcsr_distribution_new, dbcsr_distribution_release, dbcsr_distribution_type, &
      18              :         dbcsr_finalize, dbcsr_finalize_lib, dbcsr_get_block_p, dbcsr_init_lib, dbcsr_put_block, &
      19              :         dbcsr_release, dbcsr_reserve_blocks, dbcsr_type, dbcsr_type_no_symmetry
      20              :    USE cp_log_handling,                 ONLY: cp_add_default_logger,&
      21              :                                               cp_logger_create,&
      22              :                                               cp_logger_release,&
      23              :                                               cp_logger_type,&
      24              :                                               cp_rm_default_logger
      25              :    USE ieee_arithmetic,                 ONLY: ieee_is_finite
      26              :    USE input_constants,                 ONLY: do_dgemm_blas
      27              :    USE kinds,                           ONLY: dp
      28              :    USE local_gemm_api,                  ONLY: local_gemm_set_library
      29              :    USE machine,                         ONLY: default_output_unit
      30              :    USE mathlib,                         ONLY: diag_complex
      31              :    USE message_passing,                 ONLY: mp_comm_self,&
      32              :                                               mp_comm_type,&
      33              :                                               mp_para_env_release,&
      34              :                                               mp_para_env_type,&
      35              :                                               mp_world_finalize,&
      36              :                                               mp_world_init
      37              :    USE qs_ot_kp,                        ONLY: qs_ot_apply_complex_frechet_dbcsr,&
      38              :                                               qs_ot_complex_exp_frechet_kernel,&
      39              :                                               qs_ot_density_tangent,&
      40              :                                               qs_ot_finite_rotation_response,&
      41              :                                               qs_ot_get_derivative_complex,&
      42              :                                               qs_ot_get_derivative_ref_complex,&
      43              :                                               qs_ot_get_orbitals_ref_complex,&
      44              :                                               qs_ot_get_p_complex
      45              :    USE qs_ot_types,                     ONLY: qs_ot_type
      46              :    USE reference_manager,               ONLY: remove_all_references
      47              : 
      48              :    IMPLICIT NONE
      49              : 
      50              :    INTEGER, PARAMETER                                 :: k = 2, n = 3
      51              : 
      52              :    INTEGER                                            :: io_unit, mynode, nfail
      53            2 :    INTEGER, DIMENSION(:), CONTIGUOUS, POINTER                     :: col_dist, col_k, row_dist, row_k, row_n
      54            2 :    INTEGER, DIMENSION(:, :), POINTER                  :: pgrid
      55              :    TYPE(cp_logger_type), POINTER                      :: logger
      56              :    TYPE(dbcsr_distribution_type)                     :: dist
      57              :    TYPE(mp_comm_type)                                 :: mp_comm
      58              :    TYPE(mp_para_env_type), POINTER                    :: para_env
      59              : 
      60            2 :    nfail = 0
      61            2 :    NULLIFY (logger, para_env)
      62            2 :    CALL mp_world_init(mp_comm)
      63            2 :    CALL local_gemm_set_library(do_dgemm_blas)
      64            2 :    mynode = mp_comm%mepos
      65            2 :    io_unit = -1
      66            2 :    IF (mynode == 0) io_unit = default_output_unit
      67            2 :    CALL test_density_tangent(mynode, nfail)
      68            2 :    ALLOCATE (para_env)
      69            2 :    CALL para_env%from_dup(mp_comm)
      70              :    CALL cp_logger_create(logger, para_env=para_env, default_global_unit_nr=io_unit, &
      71            2 :                          close_global_unit_on_dealloc=.FALSE.)
      72            2 :    CALL cp_add_default_logger(logger)
      73            2 :    CALL add_all_references()
      74            2 :    CALL dbcsr_init_lib(mp_comm%get_handle(), io_unit)
      75              :    ! Sparse matrix rotations and derivative pullbacks.
      76            2 :    CALL test_sparse_frechet_patterns(para_env, nfail)
      77              : 
      78            2 :    ALLOCATE (pgrid(0:0, 0:0), row_dist(1), col_dist(1), row_n(1), row_k(1), col_k(1))
      79            6 :    pgrid(:, :) = 0
      80            2 :    row_dist(:) = 0
      81            2 :    col_dist(:) = 0
      82            4 :    row_n(:) = n
      83            4 :    row_k(:) = k
      84            4 :    col_k(:) = k
      85              :    CALL dbcsr_distribution_new(dist, group=mp_comm_self%get_handle(), pgrid=pgrid, &
      86            2 :                                row_dist=row_dist, col_dist=col_dist)
      87              :    ! Both derivative fixtures use one block on MPI_COMM_SELF.
      88            2 :    CALL test_complex_ref_rotation(dist, row_n, row_k, col_k, para_env, nfail)
      89            2 :    CALL test_complex_strict_derivative(dist, row_n, row_k, col_k, para_env, nfail)
      90            2 :    CALL dbcsr_distribution_release(dist)
      91            2 :    DEALLOCATE (pgrid, row_dist, col_dist, row_n, row_k, col_k)
      92              : 
      93            2 :    CALL dbcsr_finalize_lib()
      94            2 :    CALL remove_all_references()
      95            2 :    CALL cp_rm_default_logger()
      96            2 :    CALL cp_logger_release(logger)
      97            2 :    CALL mp_para_env_release(para_env)
      98            2 :    CALL mp_world_finalize()
      99              : 
     100            4 :    IF (nfail > 0) ERROR STOP "qs_ot_complex_ref_unittest failed"
     101              : 
     102              : CONTAINS
     103              : 
     104              : ! **************************************************************************************************
     105              : !> \brief Check the coupled finite-chart density tangent against a central finite difference.
     106              : !> \param mynode MPI rank
     107              : !> \param nfail accumulated number of failures
     108              : ! **************************************************************************************************
     109            2 :    SUBROUTINE test_density_tangent(mynode, nfail)
     110              :       INTEGER, INTENT(IN)                                :: mynode
     111              :       INTEGER, INTENT(INOUT)                             :: nfail
     112              : 
     113              :       INTEGER, PARAMETER                                 :: nbands = 3, nrotation = 6
     114              :       REAL(KIND=dp), PARAMETER                           :: fd_step = 2.0E-5_dp, &
     115              :                                                             kpoint_weight = 0.625_dp
     116              : 
     117              :       COMPLEX(KIND=dp), DIMENSION(nbands, nbands) :: density_minus, density_plus, density_tangent, &
     118              :          direction_generator, generator, rotation, rotation_minus, rotation_plus, weighted_rotation
     119              :       INTEGER                                            :: i, j, r
     120              :       REAL(KIND=dp)                                      :: error, trace_error
     121              :       REAL(KIND=dp), DIMENSION(nbands)                   :: occupation, weighted_occupation_step
     122              :       REAL(KIND=dp), DIMENSION(nrotation)                :: rotation_step
     123              : 
     124            2 :       generator(:, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     125            2 :       generator(1, 2) = CMPLX(0.17_dp, -0.09_dp, KIND=dp)
     126            2 :       generator(2, 1) = -CONJG(generator(1, 2))
     127            2 :       generator(1, 3) = CMPLX(-0.06_dp, 0.11_dp, KIND=dp)
     128            2 :       generator(3, 1) = -CONJG(generator(1, 3))
     129            2 :       generator(2, 3) = CMPLX(0.08_dp, 0.04_dp, KIND=dp)
     130            2 :       generator(3, 2) = -CONJG(generator(2, 3))
     131            2 :       occupation(:) = [1.72_dp, 0.83_dp, 0.31_dp]
     132            2 :       rotation_step(:) = [0.21_dp, -0.14_dp, -0.08_dp, 0.17_dp, 0.11_dp, 0.06_dp]
     133            2 :       weighted_occupation_step(:) = [-0.037_dp, 0.052_dp, -0.015_dp]
     134              : 
     135              :       CALL qs_ot_density_tangent( &
     136              :          generator, occupation, kpoint_weight, rotation_step, weighted_occupation_step, &
     137            2 :          density_tangent, difference_step=fd_step)
     138              : 
     139            2 :       direction_generator(:, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     140            2 :       r = 0
     141            6 :       DO i = 1, nbands - 1
     142           12 :          DO j = i + 1, nbands
     143            6 :             r = r + 1
     144            6 :             direction_generator(i, j) = CMPLX(rotation_step(r), 0.0_dp, KIND=dp)
     145            6 :             direction_generator(j, i) = -direction_generator(i, j)
     146            6 :             r = r + 1
     147              :             direction_generator(i, j) = direction_generator(i, j) + &
     148            6 :                                         CMPLX(0.0_dp, rotation_step(r), KIND=dp)
     149              :             direction_generator(j, i) = direction_generator(j, i) + &
     150           10 :                                         CMPLX(0.0_dp, rotation_step(r), KIND=dp)
     151              :          END DO
     152              :       END DO
     153            2 :       rotation = dense_antihermitian_exp(generator)
     154           26 :       rotation_plus = dense_antihermitian_exp(generator + fd_step*direction_generator)
     155           26 :       rotation_minus = dense_antihermitian_exp(generator - fd_step*direction_generator)
     156            2 :       weighted_rotation(:, :) = rotation_plus
     157            8 :       DO j = 1, nbands
     158              :          weighted_rotation(:, j) = &
     159              :             (kpoint_weight*occupation(j) + fd_step*weighted_occupation_step(j))* &
     160           26 :             weighted_rotation(:, j)
     161              :       END DO
     162           80 :       density_plus(:, :) = MATMUL(weighted_rotation, CONJG(TRANSPOSE(rotation_plus)))
     163            2 :       weighted_rotation(:, :) = rotation_minus
     164            8 :       DO j = 1, nbands
     165              :          weighted_rotation(:, j) = &
     166              :             (kpoint_weight*occupation(j) - fd_step*weighted_occupation_step(j))* &
     167           26 :             weighted_rotation(:, j)
     168              :       END DO
     169           80 :       density_minus(:, :) = MATMUL(weighted_rotation, CONJG(TRANSPOSE(rotation_minus)))
     170              :       density_plus(:, :) = MATMUL(CONJG(TRANSPOSE(rotation)), &
     171          182 :                                   MATMUL((density_plus - density_minus)/(2.0_dp*fd_step), rotation))
     172           26 :       error = MAXVAL(ABS(density_tangent - density_plus))
     173              :       trace_error = ABS(REAL(SUM([(density_tangent(i, i), i=1, nbands)]), KIND=dp) - &
     174            8 :                         SUM(weighted_occupation_step))
     175              : 
     176            2 :       IF (error > 3.0E-9_dp .OR. trace_error > 3.0E-10_dp) nfail = nfail + 1
     177            2 :       IF (mynode == 0) WRITE (io_unit, '(A,2(1X,ES13.6))') &
     178            1 :          "finite complex density tangent errors:", error, trace_error
     179              : 
     180            2 :    END SUBROUTINE test_density_tangent
     181              : 
     182              : ! **************************************************************************************************
     183              : !> \brief Check the combined finite complex REF and rotation derivatives.
     184              : !> \param dist single-block matrix distribution
     185              : !> \param row_n AO block sizes
     186              : !> \param row_k occupied row block sizes
     187              : !> \param col_k occupied column block sizes
     188              : !> \param para_env parallel environment
     189              : !> \param nfail accumulated number of failures
     190              : ! **************************************************************************************************
     191            2 :    SUBROUTINE test_complex_ref_rotation(dist, row_n, row_k, col_k, para_env, nfail)
     192              :       TYPE(dbcsr_distribution_type), INTENT(IN)          :: dist
     193              :       INTEGER, CONTIGUOUS, DIMENSION(:), POINTER         :: row_n, row_k, col_k
     194              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     195              :       INTEGER, INTENT(INOUT)                             :: nfail
     196              : 
     197              :       COMPLEX(KIND=dp), DIMENSION(k, k)                  :: direction_a, generator, gradient_a, &
     198              :                                                             inverse_sqrt, rotation
     199              :       COMPLEX(KIND=dp), DIMENSION(n, k)                  :: c_dense, c_reference, direction_x, &
     200              :                                                             gradient_x, hc_dense, q_dense, x_dense
     201              :       COMPLEX(KIND=dp), DIMENSION(n, n)                  :: hamiltonian, overlap
     202              :       INTEGER                                            :: i, j
     203              :       REAL(KIND=dp)                                      :: error_c, error_ref, error_rot, fd_ref, &
     204              :                                                             fd_rot, hstep, predicted_ref, &
     205              :                                                             predicted_rot
     206              :       REAL(KIND=dp), DIMENSION(2)                        :: response_gradient
     207              :       REAL(KIND=dp), DIMENSION(2, 2)                     :: rayleigh_response, response_hessian
     208              :       REAL(KIND=dp), DIMENSION(k)                        :: occupation
     209              :       REAL(KIND=dp), DIMENSION(k), TARGET                :: rotation_evals
     210              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     211           22 :       TYPE(dbcsr_type), DIMENSION(10), TARGET            :: rot_matrix
     212            6 :       TYPE(dbcsr_type), DIMENSION(2), TARGET             :: nk_buffer
     213            8 :       TYPE(dbcsr_type), DIMENSION(3), TARGET             :: nonsym_buffer
     214           10 :       TYPE(dbcsr_type), DIMENSION(4), TARGET             :: sym_buffer
     215              :       TYPE(dbcsr_type), POINTER                          :: c_im_p, c_re_p, hc_im_p, hc_re_p, &
     216              :                                                             s_im_p, s_re_p
     217              :       TYPE(dbcsr_type), TARGET                           :: c_im, c_re, f_im, f_re, g_im, g_re, &
     218              :                                                             hc_im, hc_re, s_im, s_re, sx_im, &
     219              :                                                             sx_re, x_im, x_re
     220              :       TYPE(mp_para_env_type), POINTER                    :: local_para_env
     221          324 :       TYPE(qs_ot_type)                                   :: rotation_env
     222              : 
     223            2 :       NULLIFY (blacs_env, c_im_p, c_re_p, hc_im_p, hc_re_p, local_para_env, &
     224            2 :                s_im_p, s_re_p)
     225              : 
     226              :       x_dense(:, 1) = [CMPLX(1.05_dp, 0.12_dp, KIND=dp), CMPLX(0.18_dp, -0.27_dp, KIND=dp), &
     227            8 :                        CMPLX(-0.14_dp, 0.16_dp, KIND=dp)]
     228              :       x_dense(:, 2) = [CMPLX(0.26_dp, 0.31_dp, KIND=dp), CMPLX(0.94_dp, -0.17_dp, KIND=dp), &
     229            8 :                        CMPLX(0.21_dp, 0.13_dp, KIND=dp)]
     230              :       direction_x(:, 1) = [CMPLX(0.11_dp, -0.07_dp, KIND=dp), CMPLX(-0.16_dp, 0.09_dp, KIND=dp), &
     231            8 :                            CMPLX(0.05_dp, 0.14_dp, KIND=dp)]
     232              :       direction_x(:, 2) = [CMPLX(-0.08_dp, 0.15_dp, KIND=dp), CMPLX(0.13_dp, 0.04_dp, KIND=dp), &
     233            8 :                            CMPLX(-0.10_dp, -0.06_dp, KIND=dp)]
     234              : 
     235              :       generator(:, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     236            2 :       generator(1, 1) = CMPLX(0.0_dp, 0.19_dp, KIND=dp)
     237            2 :       generator(2, 2) = CMPLX(0.0_dp, -0.08_dp, KIND=dp)
     238            2 :       generator(1, 2) = CMPLX(0.23_dp, -0.11_dp, KIND=dp)
     239            2 :       generator(2, 1) = -CONJG(generator(1, 2))
     240              :       direction_a(:, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     241            2 :       direction_a(1, 1) = CMPLX(0.0_dp, -0.07_dp, KIND=dp)
     242            2 :       direction_a(2, 2) = CMPLX(0.0_dp, 0.05_dp, KIND=dp)
     243            2 :       direction_a(1, 2) = CMPLX(-0.09_dp, 0.06_dp, KIND=dp)
     244            2 :       direction_a(2, 1) = -CONJG(direction_a(1, 2))
     245              : 
     246            2 :       hamiltonian(:, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     247            2 :       hamiltonian(1, 1) = CMPLX(0.65_dp, 0.0_dp, KIND=dp)
     248            2 :       hamiltonian(2, 2) = CMPLX(-0.35_dp, 0.0_dp, KIND=dp)
     249            2 :       hamiltonian(3, 3) = CMPLX(1.15_dp, 0.0_dp, KIND=dp)
     250            2 :       hamiltonian(1, 2) = CMPLX(0.18_dp, 0.12_dp, KIND=dp)
     251            2 :       hamiltonian(1, 3) = CMPLX(-0.09_dp, 0.07_dp, KIND=dp)
     252            2 :       hamiltonian(2, 3) = CMPLX(0.14_dp, -0.17_dp, KIND=dp)
     253            8 :       DO i = 1, n
     254           14 :          DO j = i + 1, n
     255           12 :             hamiltonian(j, i) = CONJG(hamiltonian(i, j))
     256              :          END DO
     257              :       END DO
     258            2 :       overlap(:, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     259            8 :       DO i = 1, n
     260            8 :          overlap(i, i) = CMPLX(1.0_dp, 0.0_dp, KIND=dp)
     261              :       END DO
     262            2 :       occupation = [1.0_dp, 0.37_dp]
     263              : 
     264           38 :       CALL dense_inverse_sqrt(MATMUL(CONJG(TRANSPOSE(x_dense)), x_dense), inverse_sqrt)
     265           38 :       q_dense = MATMUL(x_dense, inverse_sqrt)
     266            2 :       rotation = dense_antihermitian_exp(generator)
     267           38 :       c_reference = MATMUL(q_dense, rotation)
     268              : 
     269            2 :       ALLOCATE (local_para_env)
     270            2 :       CALL local_para_env%from_dup(mp_comm_self)
     271            2 :       CALL cp_blacs_env_create(blacs_env=blacs_env, para_env=local_para_env)
     272              : 
     273            2 :       CALL create_one_block(x_re, 'rotation_ref_x_re', dist, row_n, col_k)
     274            2 :       CALL create_one_block(x_im, 'rotation_ref_x_im', dist, row_n, col_k)
     275            2 :       CALL create_one_block(sx_re, 'rotation_ref_sx_re', dist, row_n, col_k)
     276            2 :       CALL create_one_block(sx_im, 'rotation_ref_sx_im', dist, row_n, col_k)
     277            2 :       CALL create_one_block(c_re, 'rotation_ref_c_re', dist, row_n, col_k)
     278            2 :       CALL create_one_block(c_im, 'rotation_ref_c_im', dist, row_n, col_k)
     279            2 :       CALL create_one_block(hc_re, 'rotation_ref_hc_re', dist, row_n, col_k)
     280            2 :       CALL create_one_block(hc_im, 'rotation_ref_hc_im', dist, row_n, col_k)
     281            2 :       CALL create_one_block(g_re, 'rotation_ref_g_re', dist, row_n, col_k)
     282            2 :       CALL create_one_block(g_im, 'rotation_ref_g_im', dist, row_n, col_k)
     283            2 :       CALL create_one_block(s_re, 'rotation_ref_s_re', dist, row_n, row_n)
     284            2 :       CALL create_one_block(s_im, 'rotation_ref_s_im', dist, row_n, row_n)
     285            2 :       CALL create_one_block(f_re, 'rotation_ref_f_re', dist, row_k, col_k)
     286            2 :       CALL create_one_block(f_im, 'rotation_ref_f_im', dist, row_k, col_k)
     287           10 :       DO i = 1, SIZE(sym_buffer)
     288           10 :          CALL create_one_block(sym_buffer(i), 'rotation_ref_sym', dist, row_k, col_k)
     289              :       END DO
     290            8 :       DO i = 1, SIZE(nonsym_buffer)
     291            8 :          CALL create_one_block(nonsym_buffer(i), 'rotation_ref_nonsym', dist, row_k, col_k)
     292              :       END DO
     293            6 :       DO i = 1, SIZE(nk_buffer)
     294            6 :          CALL create_one_block(nk_buffer(i), 'rotation_ref_nk', dist, row_n, col_k)
     295              :       END DO
     296           22 :       DO i = 1, SIZE(rot_matrix)
     297           22 :          CALL create_one_block(rot_matrix(i), 'rotation_ref_rot', dist, row_k, col_k)
     298              :       END DO
     299              : 
     300            2 :       CALL put_complex_pair(x_re, x_im, x_dense)
     301            2 :       CALL put_complex_pair(s_re, s_im, overlap)
     302            2 :       CALL put_complex_pair(rot_matrix(3), rot_matrix(4), generator)
     303              : 
     304            2 :       rotation_env%has_complex_kpoint_state = .TRUE.
     305            2 :       rotation_env%settings%do_rotation = .TRUE.
     306            2 :       rotation_env%settings%eps_irac_filter_matrix = 0.0_dp
     307            2 :       rotation_env%settings%ot_method = 'LBFG'
     308            2 :       rotation_env%para_env => local_para_env
     309            2 :       rotation_env%blacs_env => blacs_env
     310            2 :       rotation_env%matrix_x => x_re
     311            2 :       rotation_env%matrix_x_im => x_im
     312            2 :       rotation_env%matrix_sx => sx_re
     313            2 :       rotation_env%matrix_sx_im => sx_im
     314            2 :       rotation_env%matrix_gx => g_re
     315            2 :       rotation_env%matrix_gx_im => g_im
     316            2 :       rotation_env%matrix_ref_inv_sqrt => f_re
     317            2 :       rotation_env%matrix_ref_inv_sqrt_im => f_im
     318            2 :       rotation_env%buf1_k_k_sym => sym_buffer(1)
     319            2 :       rotation_env%buf2_k_k_sym => sym_buffer(2)
     320            2 :       rotation_env%buf3_k_k_sym => sym_buffer(3)
     321            2 :       rotation_env%buf4_k_k_sym => sym_buffer(4)
     322            2 :       rotation_env%buf1_k_k_nosym => nonsym_buffer(1)
     323            2 :       rotation_env%buf2_k_k_nosym => nonsym_buffer(2)
     324            2 :       rotation_env%buf3_k_k_nosym => nonsym_buffer(3)
     325            2 :       rotation_env%buf1_n_k => nk_buffer(1)
     326            2 :       rotation_env%buf1_n_k_dp => nk_buffer(2)
     327            2 :       rotation_env%rot_mat_u => rot_matrix(1)
     328            2 :       rotation_env%rot_mat_u_im => rot_matrix(2)
     329            2 :       rotation_env%rot_mat_x => rot_matrix(3)
     330            2 :       rotation_env%rot_mat_x_im => rot_matrix(4)
     331            2 :       rotation_env%rot_mat_dedu => rot_matrix(5)
     332            2 :       rotation_env%rot_mat_dedu_im => rot_matrix(6)
     333            2 :       rotation_env%rot_mat_gx => rot_matrix(7)
     334            2 :       rotation_env%rot_mat_gx_im => rot_matrix(8)
     335            2 :       rotation_env%rot_mat_evec_re => rot_matrix(9)
     336            2 :       rotation_env%rot_mat_evec_im => rot_matrix(10)
     337            2 :       rotation_env%rot_mat_evals => rotation_evals
     338              : 
     339            2 :       c_re_p => c_re
     340            2 :       c_im_p => c_im
     341            2 :       s_re_p => s_re
     342            2 :       s_im_p => s_im
     343            2 :       CALL qs_ot_get_orbitals_ref_complex(c_re_p, c_im_p, s_re_p, s_im_p, rotation_env)
     344            2 :       CALL get_complex_pair(c_re, c_im, c_dense)
     345           18 :       error_c = MAXVAL(ABS(c_dense - c_reference))
     346              : 
     347           70 :       hc_dense = 2.0_dp*MATMUL(hamiltonian, c_dense)
     348            6 :       DO j = 1, k
     349           18 :          hc_dense(:, j) = occupation(j)*hc_dense(:, j)
     350              :       END DO
     351            2 :       CALL put_complex_pair(hc_re, hc_im, hc_dense)
     352            2 :       hc_re_p => hc_re
     353            2 :       hc_im_p => hc_im
     354            2 :       CALL qs_ot_get_derivative_ref_complex(hc_re_p, hc_im_p, rotation_env)
     355            2 :       CALL get_complex_pair(g_re, g_im, gradient_x)
     356            2 :       CALL get_complex_pair(rot_matrix(7), rot_matrix(8), gradient_a)
     357              : 
     358            2 :       hstep = 1.0E-6_dp
     359              :       fd_ref = (rotated_polar_energy(x_dense + hstep*direction_x, generator, &
     360              :                                      hamiltonian, occupation) - &
     361              :                 rotated_polar_energy(x_dense - hstep*direction_x, generator, &
     362           34 :                                      hamiltonian, occupation))/(2.0_dp*hstep)
     363           18 :       predicted_ref = REAL(SUM(CONJG(gradient_x)*direction_x), KIND=dp)
     364            2 :       error_ref = ABS(predicted_ref - fd_ref)
     365              : 
     366              :       fd_rot = (rotated_polar_energy(x_dense, generator + hstep*direction_a, &
     367              :                                      hamiltonian, occupation) - &
     368              :                 rotated_polar_energy(x_dense, generator - hstep*direction_a, &
     369           26 :                                      hamiltonian, occupation))/(2.0_dp*hstep)
     370              :       predicted_rot = 0.5_dp*(SUM(REAL(gradient_a, KIND=dp)*REAL(direction_a, KIND=dp)) + &
     371           26 :                               SUM(AIMAG(gradient_a)*AIMAG(direction_a)))
     372            2 :       error_rot = ABS(predicted_rot - fd_rot)
     373              : 
     374              :       CALL qs_ot_finite_rotation_response(MATMUL(CONJG(TRANSPOSE(c_dense)), MATMUL(hamiltonian, c_dense)), &
     375              :                                           generator, occupation, 0.625_dp, &
     376           90 :                                           response_gradient, response_hessian, rayleigh_response)
     377              :       CALL check_error('local rotation gradient', MAXVAL(ABS(response_gradient - &
     378              :                                                              0.625_dp*[REAL(gradient_a(1, 2), dp), AIMAG(gradient_a(1, 2))])), &
     379            6 :                        5.0E-11_dp, nfail)
     380              : 
     381            2 :       IF (error_c > 5.0E-11_dp .OR. error_ref > 5.0E-8_dp .OR. &
     382            0 :           error_rot > 5.0E-8_dp) nfail = nfail + 1
     383            2 :       IF (para_env%is_source()) THEN
     384            1 :          WRITE (io_unit, '(A,1X,ES13.6)') 'complex finite REF rotation orbital error', error_c
     385            1 :          WRITE (io_unit, '(A,3(1X,ES13.6))') 'complex finite REF slope fd/predicted/error', &
     386            2 :             fd_ref, predicted_ref, error_ref
     387            1 :          WRITE (io_unit, '(A,3(1X,ES13.6))') 'complex finite rotation slope fd/predicted/error', &
     388            2 :             fd_rot, predicted_rot, error_rot
     389              :       END IF
     390              : 
     391           22 :       DO i = 1, SIZE(rot_matrix)
     392           22 :          CALL dbcsr_release(rot_matrix(i))
     393              :       END DO
     394            6 :       DO i = 1, SIZE(nk_buffer)
     395            6 :          CALL dbcsr_release(nk_buffer(i))
     396              :       END DO
     397            8 :       DO i = 1, SIZE(nonsym_buffer)
     398            8 :          CALL dbcsr_release(nonsym_buffer(i))
     399              :       END DO
     400           10 :       DO i = 1, SIZE(sym_buffer)
     401           10 :          CALL dbcsr_release(sym_buffer(i))
     402              :       END DO
     403            2 :       CALL dbcsr_release(f_im)
     404            2 :       CALL dbcsr_release(f_re)
     405            2 :       CALL dbcsr_release(s_im)
     406            2 :       CALL dbcsr_release(s_re)
     407            2 :       CALL dbcsr_release(g_im)
     408            2 :       CALL dbcsr_release(g_re)
     409            2 :       CALL dbcsr_release(hc_im)
     410            2 :       CALL dbcsr_release(hc_re)
     411            2 :       CALL dbcsr_release(c_im)
     412            2 :       CALL dbcsr_release(c_re)
     413            2 :       CALL dbcsr_release(sx_im)
     414            2 :       CALL dbcsr_release(sx_re)
     415            2 :       CALL dbcsr_release(x_im)
     416            2 :       CALL dbcsr_release(x_re)
     417            2 :       CALL cp_blacs_env_release(blacs_env)
     418            2 :       CALL mp_para_env_release(local_para_env)
     419              : 
     420           12 :    END SUBROUTINE test_complex_ref_rotation
     421              : 
     422              : ! **************************************************************************************************
     423              : !> \brief Check union handling for distinct real/imaginary DBCSR block patterns.
     424              : !> \param para_env parallel environment
     425              : !> \param nfail accumulated number of failures
     426              : ! **************************************************************************************************
     427            2 :    SUBROUTINE test_sparse_frechet_patterns(para_env, nfail)
     428              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     429              :       INTEGER, INTENT(INOUT)                             :: nfail
     430              : 
     431              :       COMPLEX(KIND=dp)                                   :: expected, value
     432            2 :       INTEGER, CONTIGUOUS, DIMENSION(:), POINTER         :: col_dist, col_size, row_dist, row_size
     433              :       INTEGER, DIMENSION(:, :), POINTER                  :: pgrid
     434              :       LOGICAL                                            :: found
     435              :       REAL(KIND=dp)                                      :: error
     436              :       REAL(KIND=dp), DIMENSION(2)                        :: evals
     437            2 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: block
     438              :       TYPE(dbcsr_distribution_type)                      :: dist
     439              :       TYPE(dbcsr_type)                                   :: inner_im, inner_re, outer_im, outer_re
     440              : 
     441            2 :       ALLOCATE (pgrid(0:0, 0:0), row_dist(2), col_dist(2), row_size(2), col_size(2))
     442            6 :       pgrid(:, :) = 0
     443            2 :       row_dist(:) = 0
     444            2 :       col_dist(:) = 0
     445            6 :       row_size(:) = 1
     446            6 :       col_size(:) = 1
     447              :       CALL dbcsr_distribution_new(dist, group=mp_comm_self%get_handle(), pgrid=pgrid, &
     448            2 :                                   row_dist=row_dist, col_dist=col_dist)
     449            2 :       CALL dbcsr_create(inner_re, 'frechet_sparse_re', dist, dbcsr_type_no_symmetry, row_size, col_size)
     450            2 :       CALL dbcsr_reserve_blocks(inner_re, rows=[1], cols=[2])
     451            2 :       CALL dbcsr_finalize(inner_re)
     452            2 :       CALL dbcsr_create(inner_im, 'frechet_sparse_im', dist, dbcsr_type_no_symmetry, row_size, col_size)
     453            2 :       CALL dbcsr_reserve_blocks(inner_im, rows=[2], cols=[1])
     454            2 :       CALL dbcsr_finalize(inner_im)
     455            2 :       CALL dbcsr_put_block(inner_re, 1, 2, RESHAPE([0.37_dp], [1, 1]))
     456            2 :       CALL dbcsr_put_block(inner_im, 2, 1, RESHAPE([-0.21_dp], [1, 1]))
     457              : 
     458            2 :       evals = [-0.4_dp, 0.9_dp]
     459            2 :       CALL qs_ot_apply_complex_frechet_dbcsr(evals, inner_re, inner_im, outer_re, outer_im)
     460              :       error = 0.0_dp
     461            2 :       expected = qs_ot_complex_exp_frechet_kernel(evals(1), evals(2))*CMPLX(0.37_dp, 0.0_dp, KIND=dp)
     462            2 :       value = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     463            2 :       CALL dbcsr_get_block_p(outer_re, 1, 2, block, found)
     464            2 :       IF (found) value = value + CMPLX(block(1, 1), 0.0_dp, KIND=dp)
     465            2 :       CALL dbcsr_get_block_p(outer_im, 1, 2, block, found)
     466            2 :       IF (found) value = value + CMPLX(0.0_dp, block(1, 1), KIND=dp)
     467            2 :       error = MAX(error, ABS(value - expected))
     468              : 
     469            2 :       expected = qs_ot_complex_exp_frechet_kernel(evals(2), evals(1))*CMPLX(0.0_dp, -0.21_dp, KIND=dp)
     470            2 :       value = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     471            2 :       CALL dbcsr_get_block_p(outer_re, 2, 1, block, found)
     472            2 :       IF (found) value = value + CMPLX(block(1, 1), 0.0_dp, KIND=dp)
     473            2 :       CALL dbcsr_get_block_p(outer_im, 2, 1, block, found)
     474            2 :       IF (found) value = value + CMPLX(0.0_dp, block(1, 1), KIND=dp)
     475            2 :       error = MAX(error, ABS(value - expected))
     476            2 :       CALL check_error("sparse_frechet_patterns", error, 5.0E-14_dp, nfail)
     477            2 :       IF (para_env%is_source()) THEN
     478            1 :          WRITE (io_unit, '(A,1X,ES13.6)') 'sparse complex Frechet block-union error', error
     479              :       END IF
     480              : 
     481            2 :       CALL dbcsr_release(outer_im)
     482            2 :       CALL dbcsr_release(outer_re)
     483            2 :       CALL dbcsr_release(inner_im)
     484            2 :       CALL dbcsr_release(inner_re)
     485            2 :       CALL dbcsr_distribution_release(dist)
     486            2 :       DEALLOCATE (pgrid, row_dist, col_dist, row_size, col_size)
     487              : 
     488            2 :    END SUBROUTINE test_sparse_frechet_patterns
     489              : 
     490              : ! **************************************************************************************************
     491              : !> \brief Check the finite complex STRICT chart and its analytic derivative.
     492              : !> \param dist single-block matrix distribution
     493              : !> \param row_n AO block sizes
     494              : !> \param row_k occupied-space row block sizes
     495              : !> \param col_k occupied-space column block sizes
     496              : !> \param para_env parallel environment
     497              : !> \param nfail accumulated number of failures
     498              : ! **************************************************************************************************
     499            2 :    SUBROUTINE test_complex_strict_derivative(dist, row_n, row_k, col_k, para_env, nfail)
     500              :       TYPE(dbcsr_distribution_type), INTENT(IN)          :: dist
     501              :       INTEGER, CONTIGUOUS, DIMENSION(:), INTENT(INOUT), &
     502              :          POINTER                                         :: row_n, row_k, col_k
     503              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     504              :       INTEGER, INTENT(INOUT)                             :: nfail
     505              : 
     506              :       CHARACTER(LEN=32)                                  :: name
     507              :       COMPLEX(KIND=dp), DIMENSION(k, k)                  :: direction_a, generator, gradient_a, &
     508              :                                                             inverse_sqrt, rotation
     509              :       COMPLEX(KIND=dp), DIMENSION(n, k)                  :: c0, direction, gradient, hc, orbitals, &
     510              :                                                             orbitals_base, sc0, sx, x
     511              :       COMPLEX(KIND=dp), DIMENSION(n, n)                  :: hamiltonian, overlap
     512              :       INTEGER                                            :: i
     513              :       REAL(KIND=dp)                                      :: error, error_rot, fd_slope, &
     514              :                                                             fd_slope_rot, hstep, slope, slope_rot
     515              :       REAL(KIND=dp), DIMENSION(k)                        :: occupation
     516              :       REAL(KIND=dp), DIMENSION(k), TARGET                :: rotation_evals
     517              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     518           22 :       TYPE(dbcsr_type), DIMENSION(10), TARGET            :: rot_matrix
     519           32 :       TYPE(dbcsr_type), DIMENSION(15), TARGET            :: nk
     520           52 :       TYPE(dbcsr_type), DIMENSION(25), TARGET            :: kk
     521              :       TYPE(dbcsr_type), POINTER                          :: hc_im_p, hc_re_p, sx_im_p, sx_re_p, &
     522              :                                                             x_im_p, x_re_p
     523              :       TYPE(mp_para_env_type), POINTER                    :: strict_para_env
     524          324 :       TYPE(qs_ot_type)                                   :: strict_env
     525              : 
     526            2 :       NULLIFY (blacs_env, hc_im_p, hc_re_p, strict_para_env, x_im_p, x_re_p, sx_im_p, sx_re_p)
     527            2 :       ALLOCATE (strict_para_env)
     528            2 :       CALL strict_para_env%from_dup(mp_comm_self)
     529            2 :       CALL cp_blacs_env_create(blacs_env=blacs_env, para_env=strict_para_env)
     530           32 :       DO i = 1, SIZE(nk)
     531           30 :          WRITE (name, '(A,I0)') 'complex_strict_nk_', i
     532           32 :          CALL create_one_block(nk(i), TRIM(name), dist, row_n, col_k)
     533              :       END DO
     534           52 :       DO i = 1, SIZE(kk)
     535           50 :          WRITE (name, '(A,I0)') 'complex_strict_kk_', i
     536           52 :          CALL create_one_block(kk(i), TRIM(name), dist, row_k, col_k)
     537              :       END DO
     538           22 :       DO i = 1, SIZE(rot_matrix)
     539           20 :          WRITE (name, '(A,I0)') 'complex_strict_rot_', i
     540           22 :          CALL create_one_block(rot_matrix(i), TRIM(name), dist, row_k, col_k)
     541              :       END DO
     542              : 
     543              :       overlap(:, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     544            2 :       overlap(1, 1) = CMPLX(1.0_dp, 0.0_dp, KIND=dp)
     545            2 :       overlap(2, 2) = CMPLX(1.2_dp, 0.0_dp, KIND=dp)
     546            2 :       overlap(3, 3) = CMPLX(1.4_dp, 0.0_dp, KIND=dp)
     547            2 :       overlap(1, 2) = CMPLX(0.06_dp, 0.03_dp, KIND=dp)
     548            2 :       overlap(2, 1) = CONJG(overlap(1, 2))
     549            2 :       overlap(1, 3) = CMPLX(-0.04_dp, 0.02_dp, KIND=dp)
     550            2 :       overlap(3, 1) = CONJG(overlap(1, 3))
     551            2 :       overlap(2, 3) = CMPLX(0.05_dp, -0.01_dp, KIND=dp)
     552            2 :       overlap(3, 2) = CONJG(overlap(2, 3))
     553              : 
     554              :       c0(:, 1) = [CMPLX(1.0_dp, 0.10_dp, KIND=dp), CMPLX(0.12_dp, -0.08_dp, KIND=dp), &
     555            8 :                   CMPLX(-0.07_dp, 0.04_dp, KIND=dp)]
     556              :       c0(:, 2) = [CMPLX(0.09_dp, 0.05_dp, KIND=dp), CMPLX(0.85_dp, -0.12_dp, KIND=dp), &
     557            8 :                   CMPLX(0.11_dp, 0.06_dp, KIND=dp)]
     558           90 :       CALL dense_inverse_sqrt(MATMUL(CONJG(TRANSPOSE(c0)), MATMUL(overlap, c0)), inverse_sqrt)
     559           38 :       c0 = MATMUL(c0, inverse_sqrt)
     560              : 
     561            2 :       x(:, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     562            2 :       x(3, 1) = CMPLX(0.23_dp, 0.17_dp, KIND=dp)
     563            2 :       x(3, 2) = CMPLX(-0.14_dp, 0.21_dp, KIND=dp)
     564          144 :       x = x - MATMUL(c0, MATMUL(CONJG(TRANSPOSE(c0)), MATMUL(overlap, x)))
     565            2 :       direction(:, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     566            2 :       direction(3, 1) = CMPLX(0.11_dp, -0.09_dp, KIND=dp)
     567            2 :       direction(3, 2) = CMPLX(0.07_dp, 0.13_dp, KIND=dp)
     568          144 :       direction = direction - MATMUL(c0, MATMUL(CONJG(TRANSPOSE(c0)), MATMUL(overlap, direction)))
     569              : 
     570              :       hamiltonian(:, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     571            2 :       hamiltonian(1, 1) = CMPLX(-0.70_dp, 0.0_dp, KIND=dp)
     572            2 :       hamiltonian(2, 2) = CMPLX(-0.20_dp, 0.0_dp, KIND=dp)
     573            2 :       hamiltonian(3, 3) = CMPLX(0.90_dp, 0.0_dp, KIND=dp)
     574            2 :       hamiltonian(1, 2) = CMPLX(0.08_dp, 0.04_dp, KIND=dp)
     575            2 :       hamiltonian(2, 1) = CONJG(hamiltonian(1, 2))
     576            2 :       hamiltonian(1, 3) = CMPLX(-0.12_dp, 0.07_dp, KIND=dp)
     577            2 :       hamiltonian(3, 1) = CONJG(hamiltonian(1, 3))
     578            2 :       hamiltonian(2, 3) = CMPLX(0.05_dp, -0.09_dp, KIND=dp)
     579            2 :       hamiltonian(3, 2) = CONJG(hamiltonian(2, 3))
     580              : 
     581              :       generator(:, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     582            2 :       generator(1, 1) = CMPLX(0.0_dp, 0.16_dp, KIND=dp)
     583            2 :       generator(2, 2) = CMPLX(0.0_dp, -0.09_dp, KIND=dp)
     584            2 :       generator(1, 2) = CMPLX(0.18_dp, -0.07_dp, KIND=dp)
     585            2 :       generator(2, 1) = -CONJG(generator(1, 2))
     586              :       direction_a(:, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     587            2 :       direction_a(1, 1) = CMPLX(0.0_dp, -0.05_dp, KIND=dp)
     588            2 :       direction_a(2, 2) = CMPLX(0.0_dp, 0.04_dp, KIND=dp)
     589            2 :       direction_a(1, 2) = CMPLX(-0.08_dp, 0.06_dp, KIND=dp)
     590            2 :       direction_a(2, 1) = -CONJG(direction_a(1, 2))
     591            2 :       occupation = [1.0_dp, 0.34_dp]
     592              : 
     593            2 :       CALL strict_orbitals(c0, x, overlap, orbitals_base)
     594            2 :       rotation = dense_antihermitian_exp(generator)
     595           38 :       orbitals = MATMUL(orbitals_base, rotation)
     596           70 :       hc = 2.0_dp*MATMUL(hamiltonian, orbitals)
     597            6 :       DO i = 1, k
     598           18 :          hc(:, i) = occupation(i)*hc(:, i)
     599              :       END DO
     600           54 :       sx = MATMUL(overlap, x)
     601           54 :       sc0 = MATMUL(overlap, c0)
     602            2 :       CALL put_complex_pair(nk(1), nk(2), x)
     603            2 :       CALL put_complex_pair(nk(3), nk(4), sx)
     604            2 :       CALL put_complex_pair(nk(5), nk(6), hc)
     605            2 :       CALL put_complex_pair(nk(9), nk(10), c0)
     606            2 :       CALL put_complex_pair(nk(11), nk(12), sc0)
     607            2 :       CALL put_complex_pair(rot_matrix(3), rot_matrix(4), generator)
     608              : 
     609            2 :       strict_env%has_complex_kpoint_state = .TRUE.
     610            2 :       strict_env%settings%ot_algorithm = 'TOD'
     611            2 :       strict_env%settings%do_rotation = .TRUE.
     612            2 :       strict_env%para_env => strict_para_env
     613            2 :       strict_env%blacs_env => blacs_env
     614            2 :       strict_env%matrix_x => nk(1)
     615            2 :       strict_env%matrix_x_im => nk(2)
     616            2 :       strict_env%matrix_sx => nk(3)
     617            2 :       strict_env%matrix_sx_im => nk(4)
     618            2 :       strict_env%matrix_gx => nk(7)
     619            2 :       strict_env%matrix_gx_im => nk(8)
     620            2 :       strict_env%matrix_c0 => nk(9)
     621            2 :       strict_env%matrix_c0_im => nk(10)
     622            2 :       strict_env%matrix_sc0 => nk(11)
     623            2 :       strict_env%matrix_sc0_im => nk(12)
     624            2 :       strict_env%matrix_buf_nk => nk(13)
     625            2 :       strict_env%matrix_buf_nk_im => nk(14)
     626            2 :       strict_env%matrix_tmp_nk => nk(15)
     627            2 :       strict_env%matrix_p => kk(1)
     628            2 :       strict_env%matrix_p_im => kk(2)
     629            2 :       strict_env%matrix_r => kk(3)
     630            2 :       strict_env%matrix_r_im => kk(4)
     631            2 :       strict_env%matrix_sinp => kk(5)
     632            2 :       strict_env%matrix_sinp_im => kk(6)
     633            2 :       strict_env%matrix_cosp => kk(7)
     634            2 :       strict_env%matrix_cosp_im => kk(8)
     635            2 :       strict_env%matrix_sinp_b => kk(9)
     636            2 :       strict_env%matrix_cosp_b => kk(10)
     637            2 :       strict_env%matrix_buf1 => kk(11)
     638            2 :       strict_env%matrix_buf1_im => kk(12)
     639            2 :       strict_env%matrix_buf2 => kk(13)
     640            2 :       strict_env%matrix_buf2_im => kk(14)
     641            2 :       strict_env%matrix_buf3 => kk(15)
     642            2 :       strict_env%matrix_buf3_im => kk(16)
     643            2 :       strict_env%matrix_buf4 => kk(17)
     644            2 :       strict_env%matrix_buf4_im => kk(18)
     645            2 :       strict_env%matrix_os => kk(19)
     646            2 :       strict_env%matrix_os_im => kk(20)
     647            2 :       strict_env%matrix_buf1_ortho => kk(21)
     648            2 :       strict_env%matrix_buf1_ortho_im => kk(22)
     649            2 :       strict_env%matrix_buf2_ortho => kk(23)
     650            2 :       strict_env%matrix_buf2_ortho_im => kk(24)
     651            2 :       strict_env%matrix_tmp_ortho => kk(25)
     652            2 :       strict_env%rot_mat_u => rot_matrix(1)
     653            2 :       strict_env%rot_mat_u_im => rot_matrix(2)
     654            2 :       strict_env%rot_mat_x => rot_matrix(3)
     655            2 :       strict_env%rot_mat_x_im => rot_matrix(4)
     656            2 :       strict_env%rot_mat_dedu => rot_matrix(5)
     657            2 :       strict_env%rot_mat_dedu_im => rot_matrix(6)
     658            2 :       strict_env%rot_mat_gx => rot_matrix(7)
     659            2 :       strict_env%rot_mat_gx_im => rot_matrix(8)
     660            2 :       strict_env%rot_mat_evec_re => rot_matrix(9)
     661            2 :       strict_env%rot_mat_evec_im => rot_matrix(10)
     662            2 :       strict_env%rot_mat_evals => rotation_evals
     663            2 :       ALLOCATE (strict_env%evals(k), strict_env%dum(k))
     664              : 
     665            2 :       x_re_p => nk(1)
     666            2 :       x_im_p => nk(2)
     667            2 :       sx_re_p => nk(3)
     668            2 :       sx_im_p => nk(4)
     669            2 :       hc_re_p => nk(5)
     670            2 :       hc_im_p => nk(6)
     671            2 :       CALL qs_ot_get_p_complex(x_re_p, x_im_p, sx_re_p, sx_im_p, strict_env)
     672            2 :       CALL qs_ot_get_derivative_complex(hc_re_p, hc_im_p, strict_env)
     673              : 
     674            2 :       CALL get_complex_pair(nk(7), nk(8), gradient)
     675            2 :       CALL get_complex_pair(rot_matrix(7), rot_matrix(8), gradient_a)
     676              : 
     677            2 :       hstep = 1.0E-6_dp
     678              :       fd_slope = (strict_rotated_energy(c0, x + hstep*direction, generator, overlap, &
     679              :                                         hamiltonian, occupation) - &
     680              :                   strict_rotated_energy(c0, x - hstep*direction, generator, overlap, &
     681           34 :                                         hamiltonian, occupation))/(2.0_dp*hstep)
     682           18 :       slope = REAL(SUM(CONJG(gradient)*direction), KIND=dp)
     683            2 :       error = ABS(slope - fd_slope)
     684              :       fd_slope_rot = (strict_rotated_energy(c0, x, generator + hstep*direction_a, overlap, &
     685              :                                             hamiltonian, occupation) - &
     686              :                       strict_rotated_energy(c0, x, generator - hstep*direction_a, overlap, &
     687           26 :                                             hamiltonian, occupation))/(2.0_dp*hstep)
     688              :       slope_rot = 0.5_dp*(SUM(REAL(gradient_a, KIND=dp)*REAL(direction_a, KIND=dp)) + &
     689           26 :                           SUM(AIMAG(gradient_a)*AIMAG(direction_a)))
     690            2 :       error_rot = ABS(slope_rot - fd_slope_rot)
     691            2 :       IF (error > 5.0E-8_dp .OR. error_rot > 5.0E-8_dp) nfail = nfail + 1
     692            2 :       IF (para_env%is_source()) THEN
     693            1 :          WRITE (io_unit, '(A,3(1X,ES13.6))') 'complex finite-STRICT derivative: fd/predicted/error', &
     694            2 :             fd_slope, slope, error
     695            1 :          WRITE (io_unit, '(A,3(1X,ES13.6))') 'complex finite-STRICT rotation: fd/predicted/error', &
     696            2 :             fd_slope_rot, slope_rot, error_rot
     697              :       END IF
     698              : 
     699            2 :       DEALLOCATE (strict_env%evals, strict_env%dum)
     700           22 :       DO i = SIZE(rot_matrix), 1, -1
     701           22 :          CALL dbcsr_release(rot_matrix(i))
     702              :       END DO
     703           52 :       DO i = SIZE(kk), 1, -1
     704           52 :          CALL dbcsr_release(kk(i))
     705              :       END DO
     706           32 :       DO i = SIZE(nk), 1, -1
     707           32 :          CALL dbcsr_release(nk(i))
     708              :       END DO
     709            2 :       CALL cp_blacs_env_release(blacs_env)
     710            2 :       CALL mp_para_env_release(strict_para_env)
     711              : 
     712            6 :    END SUBROUTINE test_complex_strict_derivative
     713              : 
     714              : ! **************************************************************************************************
     715              : !> \brief Create and reserve a one-block DBCSR matrix.
     716              : !> \param matrix ...
     717              : !> \param name ...
     718              : !> \param dist ...
     719              : !> \param row_size ...
     720              : !> \param col_size ...
     721              : ! **************************************************************************************************
     722          166 :    SUBROUTINE create_one_block(matrix, name, dist, row_size, col_size)
     723              :       TYPE(dbcsr_type), INTENT(INOUT)                    :: matrix
     724              :       CHARACTER(LEN=*), INTENT(IN)                       :: name
     725              :       TYPE(dbcsr_distribution_type), INTENT(IN)          :: dist
     726              :       INTEGER, CONTIGUOUS, DIMENSION(:), INTENT(INOUT), &
     727              :          POINTER                                         :: row_size, col_size
     728              : 
     729          166 :       CALL dbcsr_create(matrix, name, dist, dbcsr_type_no_symmetry, row_size, col_size)
     730          166 :       CALL dbcsr_reserve_blocks(matrix, rows=[1], cols=[1])
     731          166 :       CALL dbcsr_finalize(matrix)
     732              : 
     733          166 :    END SUBROUTINE create_one_block
     734              : 
     735              : ! **************************************************************************************************
     736              : !> \brief Store a dense complex matrix in a real/imaginary DBCSR pair.
     737              : !> \param matrix_re ...
     738              : !> \param matrix_im ...
     739              : !> \param matrix ...
     740              : ! **************************************************************************************************
     741           20 :    SUBROUTINE put_complex_pair(matrix_re, matrix_im, matrix)
     742              :       TYPE(dbcsr_type), INTENT(INOUT)                    :: matrix_re, matrix_im
     743              :       COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(IN)      :: matrix
     744              : 
     745          180 :       CALL dbcsr_put_block(matrix_re, 1, 1, REAL(matrix, KIND=dp))
     746          180 :       CALL dbcsr_put_block(matrix_im, 1, 1, AIMAG(matrix))
     747              : 
     748           20 :    END SUBROUTINE put_complex_pair
     749              : 
     750              : ! **************************************************************************************************
     751              : !> \brief Retrieve a dense complex matrix from a one-block DBCSR pair.
     752              : !> \param matrix_re ...
     753              : !> \param matrix_im ...
     754              : !> \param matrix ...
     755              : ! **************************************************************************************************
     756           10 :    SUBROUTINE get_complex_pair(matrix_re, matrix_im, matrix)
     757              :       TYPE(dbcsr_type), INTENT(INOUT)                    :: matrix_re, matrix_im
     758              :       COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(OUT)     :: matrix
     759              : 
     760              :       LOGICAL                                            :: found
     761           10 :       REAL(KIND=dp), DIMENSION(:, :), POINTER            :: block
     762              : 
     763           82 :       matrix(:, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     764           10 :       CALL dbcsr_get_block_p(matrix_re, 1, 1, block, found)
     765           82 :       IF (found) matrix = matrix + CMPLX(block, 0.0_dp, KIND=dp)
     766           10 :       CALL dbcsr_get_block_p(matrix_im, 1, 1, block, found)
     767           82 :       IF (found) matrix = matrix + CMPLX(0.0_dp, block, KIND=dp)
     768              : 
     769           10 :    END SUBROUTINE get_complex_pair
     770              : 
     771              : ! **************************************************************************************************
     772              : !> \brief Dense fixed-occupation energy after finite REF orthogonalization and rotation.
     773              : !> \param x REF coordinate
     774              : !> \param generator anti-Hermitian rotation generator
     775              : !> \param hamiltonian Hermitian one-particle Hamiltonian
     776              : !> \param occupation orbital occupations
     777              : !> \return energy
     778              : ! **************************************************************************************************
     779            8 :    FUNCTION rotated_polar_energy(x, generator, hamiltonian, occupation) RESULT(energy)
     780              :       COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(IN)      :: x, generator, hamiltonian
     781              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: occupation
     782              :       REAL(KIND=dp)                                      :: energy
     783              : 
     784              :       COMPLEX(KIND=dp), &
     785           16 :          DIMENSION(SIZE(x, 1), SIZE(x, 2))               :: orbitals
     786              :       COMPLEX(KIND=dp), &
     787           16 :          DIMENSION(SIZE(x, 2), SIZE(x, 2))               :: inverse_sqrt, rotation
     788              :       INTEGER                                            :: i
     789              : 
     790          176 :       CALL dense_inverse_sqrt(MATMUL(CONJG(TRANSPOSE(x)), x), inverse_sqrt)
     791            8 :       rotation = dense_antihermitian_exp(generator)
     792          496 :       orbitals = MATMUL(MATMUL(x, inverse_sqrt), rotation)
     793            8 :       energy = 0.0_dp
     794           24 :       DO i = 1, SIZE(occupation)
     795              :          energy = energy + occupation(i)* &
     796          296 :                   REAL(DOT_PRODUCT(orbitals(:, i), MATMUL(hamiltonian, orbitals(:, i))), KIND=dp)
     797              :       END DO
     798              : 
     799            8 :    END FUNCTION rotated_polar_energy
     800              : 
     801              : ! **************************************************************************************************
     802              : !> \brief Dense reference exponential for an anti-Hermitian matrix.
     803              : !> \param generator anti-Hermitian generator
     804              : !> \return its unitary exponential
     805              : ! **************************************************************************************************
     806           26 :    FUNCTION dense_antihermitian_exp(generator) RESULT(rotation)
     807              :       COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(IN)      :: generator
     808              :       COMPLEX(KIND=dp), DIMENSION(SIZE(generator, 1), &
     809              :          SIZE(generator, 2))                             :: rotation
     810              : 
     811              :       COMPLEX(KIND=dp), DIMENSION(SIZE(generator, 1), &
     812           26 :          SIZE(generator, 2))                             :: hermitian, vectors
     813              :       INTEGER                                            :: i
     814           26 :       REAL(KIND=dp), DIMENSION(SIZE(generator, 1))       :: eigenvalues
     815              : 
     816          218 :       hermitian = CMPLX(0.0_dp, 1.0_dp, KIND=dp)*generator
     817           26 :       CALL diag_complex(hermitian, vectors, eigenvalues)
     818          218 :       rotation(:, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     819           84 :       DO i = 1, SIZE(eigenvalues)
     820              :          rotation = rotation + EXP(CMPLX(0.0_dp, -eigenvalues(i), KIND=dp))* &
     821          232 :                     MATMUL(RESHAPE(vectors(:, i), [SIZE(generator, 1), 1]), &
     822         1554 :                            RESHAPE(CONJG(vectors(:, i)), [1, SIZE(generator, 1)]))
     823              :       END DO
     824              : 
     825           26 :    END FUNCTION dense_antihermitian_exp
     826              : 
     827              : ! **************************************************************************************************
     828              : !> \brief Hermitian inverse square root for the independent dense reference.
     829              : !> \param matrix ...
     830              : !> \param inverse ...
     831              : ! **************************************************************************************************
     832           12 :    SUBROUTINE dense_inverse_sqrt(matrix, inverse)
     833              :       COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(IN)      :: matrix
     834              :       COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(OUT)     :: inverse
     835              : 
     836              :       COMPLEX(KIND=dp), &
     837           24 :          DIMENSION(SIZE(matrix, 1), SIZE(matrix, 2))     :: vectors
     838              :       INTEGER                                            :: i
     839           24 :       REAL(KIND=dp), DIMENSION(SIZE(matrix, 1))          :: eigenvalues
     840              : 
     841           12 :       CALL diag_complex(matrix, vectors, eigenvalues)
     842           84 :       inverse(:, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     843           36 :       DO i = 1, SIZE(eigenvalues)
     844           96 :          inverse = inverse + MATMUL(RESHAPE(vectors(:, i), [SIZE(matrix, 1), 1]), &
     845              :                                     RESHAPE(CONJG(vectors(:, i)), [1, SIZE(matrix, 1)]))/ &
     846          540 :                    SQRT(eigenvalues(i))
     847              :       END DO
     848              : 
     849           12 :    END SUBROUTINE dense_inverse_sqrt
     850              : 
     851              : ! **************************************************************************************************
     852              : !> \brief Independent dense finite STRICT transformation for a Hermitian overlap.
     853              : !> \param c0 reference orbitals
     854              : !> \param coordinate tangent coordinate
     855              : !> \param overlap Hermitian overlap matrix
     856              : !> \param orbitals transformed orbitals
     857              : ! **************************************************************************************************
     858           10 :    SUBROUTINE strict_orbitals(c0, coordinate, overlap, orbitals)
     859              :       COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(IN)      :: c0, coordinate, overlap
     860              :       COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(OUT)     :: orbitals
     861              : 
     862              :       COMPLEX(KIND=dp), DIMENSION(SIZE(coordinate, 2), &
     863           20 :          SIZE(coordinate, 2))                            :: cosp, sinp, vectors
     864              :       INTEGER                                            :: i
     865              :       REAL(KIND=dp)                                      :: root
     866           20 :       REAL(KIND=dp), DIMENSION(SIZE(coordinate, 2))      :: eigenvalues
     867              : 
     868           80 :       CALL diag_complex(MATMUL(CONJG(TRANSPOSE(coordinate)), MATMUL(overlap, coordinate)), &
     869          580 :                         vectors, eigenvalues)
     870           70 :       cosp(:, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     871           70 :       sinp(:, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
     872           30 :       DO i = 1, SIZE(eigenvalues)
     873           20 :          root = SQRT(MAX(0.0_dp, eigenvalues(i)))
     874           80 :          cosp = cosp + COS(root)*MATMUL(RESHAPE(vectors(:, i), [SIZE(vectors, 1), 1]), &
     875          440 :                                         RESHAPE(CONJG(vectors(:, i)), [1, SIZE(vectors, 1)]))
     876           30 :          IF (root < 1.0E-12_dp) THEN
     877           16 :             sinp = sinp + MATMUL(RESHAPE(vectors(:, i), [SIZE(vectors, 1), 1]), &
     878           88 :                                  RESHAPE(CONJG(vectors(:, i)), [1, SIZE(vectors, 1)]))
     879              :          ELSE
     880              :             sinp = sinp + SIN(root)/root* &
     881           64 :                    MATMUL(RESHAPE(vectors(:, i), [SIZE(vectors, 1), 1]), &
     882          352 :                           RESHAPE(CONJG(vectors(:, i)), [1, SIZE(vectors, 1)]))
     883              :          END IF
     884              :       END DO
     885          570 :       orbitals = MATMUL(c0, cosp) + MATMUL(coordinate, sinp)
     886              : 
     887           10 :    END SUBROUTINE strict_orbitals
     888              : 
     889              : ! **************************************************************************************************
     890              : !> \brief Energy of finite STRICT orbitals followed by a complex occupied-space rotation.
     891              : !> \param c0 reference orbitals
     892              : !> \param coordinate tangent coordinate
     893              : !> \param generator anti-Hermitian rotation generator
     894              : !> \param overlap Hermitian overlap matrix
     895              : !> \param hamiltonian Hermitian Hamiltonian
     896              : !> \param occupation orbital occupations
     897              : !> \return energy
     898              : ! **************************************************************************************************
     899            8 :    FUNCTION strict_rotated_energy(c0, coordinate, generator, overlap, hamiltonian, &
     900            8 :                                   occupation) RESULT(energy)
     901              :       COMPLEX(KIND=dp), DIMENSION(:, :), INTENT(IN)      :: c0, coordinate, generator, overlap, &
     902              :                                                             hamiltonian
     903              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: occupation
     904              :       REAL(KIND=dp)                                      :: energy
     905              : 
     906              :       COMPLEX(KIND=dp), DIMENSION(SIZE(coordinate, 1), &
     907           16 :          SIZE(coordinate, 2))                            :: orbitals
     908              :       INTEGER                                            :: i
     909              : 
     910            8 :       CALL strict_orbitals(c0, coordinate, overlap, orbitals)
     911          296 :       orbitals = MATMUL(orbitals, dense_antihermitian_exp(generator))
     912            8 :       energy = 0.0_dp
     913           24 :       DO i = 1, SIZE(occupation)
     914              :          energy = energy + occupation(i)* &
     915          296 :                   REAL(DOT_PRODUCT(orbitals(:, i), MATMUL(hamiltonian, orbitals(:, i))), KIND=dp)
     916              :       END DO
     917              : 
     918            8 :    END FUNCTION strict_rotated_energy
     919              : 
     920              : ! **************************************************************************************************
     921              : !> \brief Report a failed numerical check, including non-finite results.
     922              : !> \param label check name
     923              : !> \param error measured error
     924              : !> \param tolerance maximum error
     925              : !> \param nfail accumulated failures
     926              : ! **************************************************************************************************
     927            4 :    SUBROUTINE check_error(label, error, tolerance, nfail)
     928              :       CHARACTER(LEN=*), INTENT(IN)                       :: label
     929              :       REAL(KIND=dp), INTENT(IN)                          :: error, tolerance
     930              :       INTEGER, INTENT(INOUT)                             :: nfail
     931              : 
     932            4 :       IF (ieee_is_finite(error)) THEN
     933            4 :          IF (error <= tolerance) RETURN
     934              :       END IF
     935            0 :       nfail = nfail + 1
     936            0 :       IF (io_unit >= 0) WRITE (io_unit, '(A,2(1X,ES13.6))') TRIM(label)//': error/tolerance', error, tolerance
     937              :    END SUBROUTINE check_error
     938              : 
     939              : END PROGRAM qs_ot_complex_ref_unittest
        

Generated by: LCOV version 2.0-1