LCOV - code coverage report
Current view: top level - src - qs_loc_methods.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 54.0 % 598 323
Test Date: 2026-11-24 01:22:08 Functions: 58.3 % 12 7

            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 Driver for the localization that should be general
      10              : !>      for all the methods available and all the definition of the
      11              : !>      spread functional
      12              : !>      Write centers, spread and cubes only if required and for the
      13              : !>      selected states
      14              : !>      The localized functions are copied in the standard mos array
      15              : !>      for the next use
      16              : !> \par History
      17              : !>      01.2008 Teodoro Laino [tlaino] - University of Zurich
      18              : !>        - Merging the two localization codes and updating to new structures
      19              : !> \author MI (04.2005)
      20              : ! **************************************************************************************************
      21              : MODULE qs_loc_methods
      22              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      23              :                                               deallocate_atomic_kind_set,&
      24              :                                               set_atomic_kind
      25              :    USE cell_types,                      ONLY: cell_type,&
      26              :                                               pbc
      27              :    USE cp_cfm_types,                    ONLY: cp_cfm_create,&
      28              :                                               cp_cfm_get_element,&
      29              :                                               cp_cfm_get_info,&
      30              :                                               cp_cfm_p_type,&
      31              :                                               cp_cfm_release,&
      32              :                                               cp_cfm_to_fm,&
      33              :                                               cp_cfm_type,&
      34              :                                               cp_fm_to_cfm
      35              :    USE cp_control_types,                ONLY: dft_control_type
      36              :    USE cp_dbcsr_api,                    ONLY: dbcsr_copy,&
      37              :                                               dbcsr_deallocate_matrix,&
      38              :                                               dbcsr_p_type,&
      39              :                                               dbcsr_set
      40              :    USE cp_dbcsr_operations,             ONLY: copy_dbcsr_to_fm,&
      41              :                                               cp_dbcsr_sm_fm_multiply
      42              :    USE cp_fm_basic_linalg,              ONLY: cp_fm_rot_cols,&
      43              :                                               cp_fm_rot_rows,&
      44              :                                               cp_fm_schur_product
      45              :    USE cp_fm_pool_types,                ONLY: cp_fm_pool_type,&
      46              :                                               fm_pool_create_fm
      47              :    USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
      48              :                                               cp_fm_struct_release,&
      49              :                                               cp_fm_struct_type
      50              :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      51              :                                               cp_fm_get_element,&
      52              :                                               cp_fm_get_info,&
      53              :                                               cp_fm_maxabsval,&
      54              :                                               cp_fm_release,&
      55              :                                               cp_fm_set_all,&
      56              :                                               cp_fm_to_fm,&
      57              :                                               cp_fm_type
      58              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      59              :                                               cp_logger_type,&
      60              :                                               cp_to_string
      61              :    USE cp_output_handling,              ONLY: cp_iter_string,&
      62              :                                               cp_p_file,&
      63              :                                               cp_print_key_finished_output,&
      64              :                                               cp_print_key_should_output,&
      65              :                                               cp_print_key_unit_nr
      66              :    USE cp_realspace_grid_cube,          ONLY: cp_pw_to_cube
      67              :    USE cp_units,                        ONLY: cp_unit_from_cp2k
      68              :    USE input_constants,                 ONLY: &
      69              :         do_loc_crazy, do_loc_cs, do_loc_direct, do_loc_gapo, do_loc_jacobi, do_loc_l1_norm_sd, &
      70              :         do_loc_none, do_loc_scdm, dump_dcd, dump_dcd_aligned_cell, dump_xmol
      71              :    USE input_section_types,             ONLY: section_get_ival,&
      72              :                                               section_get_ivals,&
      73              :                                               section_vals_get_subs_vals,&
      74              :                                               section_vals_type,&
      75              :                                               section_vals_val_get
      76              :    USE kinds,                           ONLY: default_path_length,&
      77              :                                               default_string_length,&
      78              :                                               dp,&
      79              :                                               sp
      80              :    USE machine,                         ONLY: m_flush
      81              :    USE mathconstants,                   ONLY: pi,&
      82              :                                               twopi
      83              :    USE message_passing,                 ONLY: mp_para_env_type
      84              :    USE molecule_types,                  ONLY: molecule_type
      85              :    USE motion_utils,                    ONLY: get_output_format
      86              :    USE orbital_pointers,                ONLY: ncoset
      87              :    USE parallel_gemm_api,               ONLY: parallel_gemm
      88              :    USE particle_list_types,             ONLY: particle_list_type
      89              :    USE particle_methods,                ONLY: get_particle_set,&
      90              :                                               write_particle_coordinates
      91              :    USE particle_types,                  ONLY: allocate_particle_set,&
      92              :                                               deallocate_particle_set,&
      93              :                                               particle_type
      94              :    USE physcon,                         ONLY: angstrom
      95              :    USE pw_env_types,                    ONLY: pw_env_get,&
      96              :                                               pw_env_type
      97              :    USE pw_pool_types,                   ONLY: pw_pool_type
      98              :    USE pw_types,                        ONLY: pw_c1d_gs_type,&
      99              :                                               pw_r3d_rs_type
     100              :    USE qs_collocate_density,            ONLY: calculate_wavefunction
     101              :    USE qs_environment_types,            ONLY: get_qs_env,&
     102              :                                               qs_environment_type
     103              :    USE qs_kind_types,                   ONLY: qs_kind_type
     104              :    USE qs_loc_types,                    ONLY: get_qs_loc_env,&
     105              :                                               loc_coeff_index,&
     106              :                                               qs_loc_env_type
     107              :    USE qs_localization_methods,         ONLY: &
     108              :         approx_l1_norm_sd, cardoso_souloumiac, cardoso_souloumiac_pipek, crazy_rotations, &
     109              :         direct_mini, jacobi_cg_edf_ls, jacobi_rotations, rotate_orbitals, scdm_qrfact, zij_matrix
     110              :    USE qs_matrix_pools,                 ONLY: mpools_get
     111              :    USE qs_moments,                      ONLY: build_local_moment_matrix
     112              :    USE qs_subsys_types,                 ONLY: qs_subsys_get,&
     113              :                                               qs_subsys_type
     114              :    USE string_utilities,                ONLY: xstring
     115              : #include "./base/base_uses.f90"
     116              : 
     117              :    IMPLICIT NONE
     118              : 
     119              :    PRIVATE
     120              : 
     121              : ! *** Global parameters ***
     122              : 
     123              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_loc_methods'
     124              : 
     125              : ! *** Public ***
     126              :    PUBLIC :: qs_print_cubes, centers_spreads_berry, centers_second_moments_loc, &
     127              :              centers_second_moments_berry, jacobi_rotation_pipek, optimize_loc_berry, &
     128              :              optimize_loc_pipek
     129              : 
     130              : CONTAINS
     131              : 
     132              : ! **************************************************************************************************
     133              : !> \brief Calculate and optimize the spread functional as calculated from
     134              : !>       the selected mos  and by the definition using the berry phase
     135              : !>       as given by silvestrelli et al
     136              : !>       If required the centers and the spreads for each mos selected
     137              : !>       are calculated from z_ij and printed to file.
     138              : !>       The centers files is appended if already exists
     139              : !> \param method indicates localization algorithm
     140              : !> \param qs_loc_env new environment for the localization calculations
     141              : !> \param vectors selected mos to be localized
     142              : !> \param op_sm_set sparse matrices containing the integrals of the kind <mi e{iGr} nu>
     143              : !> \param zij_fm_set set of full matrix of size nmoloc x nmoloc, will contain the z_ij numbers
     144              : !>                    as defined by Silvestrelli et al
     145              : !> \param para_env ...
     146              : !> \param cell ...
     147              : !> \param weights ...
     148              : !> \param ispin ...
     149              : !> \param print_loc_section ...
     150              : !> \param restricted ...
     151              : !> \param nextra ...
     152              : !> \param nmo ...
     153              : !> \param vectors_2 ...
     154              : !> \param guess_mos ...
     155              : !> \par History
     156              : !>      04.2005 created [MI]
     157              : !> \author MI
     158              : !> \note
     159              : !>       This definition need the use of complex numbers, therefore the
     160              : !>       optimization routines are specific for this case
     161              : !>       The file for the centers and the spreads have a xyz format
     162              : ! **************************************************************************************************
     163          514 :    SUBROUTINE optimize_loc_berry(method, qs_loc_env, vectors, op_sm_set, &
     164          514 :                                  zij_fm_set, para_env, cell, weights, ispin, print_loc_section, &
     165              :                                  restricted, nextra, nmo, vectors_2, guess_mos)
     166              : 
     167              :       INTEGER, INTENT(IN)                                :: method
     168              :       TYPE(qs_loc_env_type), POINTER                     :: qs_loc_env
     169              :       TYPE(cp_fm_type), INTENT(IN)                       :: vectors
     170              :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: op_sm_set
     171              :       TYPE(cp_fm_type), DIMENSION(:, :), INTENT(IN)      :: zij_fm_set
     172              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     173              :       TYPE(cell_type), POINTER                           :: cell
     174              :       REAL(dp), DIMENSION(:)                             :: weights
     175              :       INTEGER, INTENT(IN)                                :: ispin
     176              :       TYPE(section_vals_type), POINTER                   :: print_loc_section
     177              :       INTEGER                                            :: restricted
     178              :       INTEGER, INTENT(IN), OPTIONAL                      :: nextra, nmo
     179              :       TYPE(cp_fm_type), INTENT(IN), OPTIONAL             :: vectors_2, guess_mos
     180              : 
     181              :       CHARACTER(len=*), PARAMETER :: routineN = 'optimize_loc_berry'
     182              : 
     183              :       INTEGER                                            :: handle, idim1, idim2, max_iter, nao, &
     184              :                                                             nmoloc, out_each, output_unit, sweeps
     185              :       LOGICAL                                            :: converged, crazy_use_diag, &
     186              :                                                             do_jacobi_refinement, my_do_mixed
     187              :       REAL(dp)                                           :: crazy_scale, eps_localization, &
     188              :                                                             max_crazy_angle, start_time, &
     189              :                                                             target_time
     190          514 :       TYPE(cp_cfm_type), ALLOCATABLE, DIMENSION(:, :)    :: my_zij_cfm_set
     191              :       TYPE(cp_cfm_type), POINTER                         :: complex_vectors
     192              :       TYPE(cp_logger_type), POINTER                      :: logger
     193              : 
     194          514 :       NULLIFY (complex_vectors)
     195              : 
     196          514 :       CALL timeset(routineN, handle)
     197          514 :       logger => cp_get_default_logger()
     198              :       output_unit = cp_print_key_unit_nr(logger, print_loc_section, "PROGRAM_RUN_INFO", &
     199          514 :                                          extension=".locInfo")
     200              : 
     201              :       ! get rows and cols of the input
     202          514 :       CALL cp_fm_get_info(vectors, nrow_global=nao, ncol_global=nmoloc)
     203              : 
     204          514 :       CALL zij_matrix(vectors, op_sm_set, zij_fm_set)
     205              : 
     206          514 :       max_iter = qs_loc_env%localized_wfn_control%max_iter
     207          514 :       max_crazy_angle = qs_loc_env%localized_wfn_control%max_crazy_angle
     208          514 :       crazy_use_diag = qs_loc_env%localized_wfn_control%crazy_use_diag
     209          514 :       crazy_scale = qs_loc_env%localized_wfn_control%crazy_scale
     210          514 :       eps_localization = qs_loc_env%localized_wfn_control%eps_localization
     211          514 :       out_each = qs_loc_env%localized_wfn_control%out_each
     212          514 :       target_time = qs_loc_env%target_time
     213          514 :       start_time = qs_loc_env%start_time
     214          514 :       do_jacobi_refinement = qs_loc_env%localized_wfn_control%jacobi_refinement
     215          514 :       my_do_mixed = qs_loc_env%localized_wfn_control%do_mixed
     216              : 
     217              :       CALL centers_spreads_berry(qs_loc_env, nmoloc, cell, weights, &
     218          514 :                                  ispin, print_loc_section, zij=zij_fm_set, only_initial_out=.TRUE.)
     219              : 
     220          514 :       sweeps = 0
     221              : 
     222          850 :       SELECT CASE (method)
     223              :       CASE (do_loc_jacobi)
     224              :          CALL jacobi_rotations(weights, zij_fm_set, vectors, para_env, max_iter=max_iter, &
     225              :                                eps_localization=eps_localization, sweeps=sweeps, &
     226              :                                out_each=out_each, target_time=target_time, start_time=start_time, &
     227          336 :                                restricted=restricted)
     228              :       CASE (do_loc_gapo)
     229            2 :          IF (my_do_mixed) THEN
     230            2 :             IF (nextra > 0) THEN
     231            2 :                IF (PRESENT(guess_mos)) THEN
     232              :                   CALL jacobi_cg_edf_ls(para_env, weights, zij_fm_set, vectors, max_iter, &
     233              :                                         eps_localization, sweeps, out_each, nextra, &
     234              :                                         qs_loc_env%localized_wfn_control%do_cg_po, &
     235            2 :                                         nmo=nmo, vectors_2=vectors_2, mos_guess=guess_mos)
     236              :                ELSE
     237              :                   CALL jacobi_cg_edf_ls(para_env, weights, zij_fm_set, vectors, max_iter, &
     238              :                                         eps_localization, sweeps, out_each, nextra, &
     239              :                                         qs_loc_env%localized_wfn_control%do_cg_po, &
     240            0 :                                         nmo=nmo, vectors_2=vectors_2)
     241              :                END IF
     242              :             ELSE
     243              :                CALL jacobi_cg_edf_ls(para_env, weights, zij_fm_set, vectors, max_iter, &
     244              :                                      eps_localization, sweeps, out_each, 0, &
     245            0 :                                      qs_loc_env%localized_wfn_control%do_cg_po)
     246              :             END IF
     247              :          ELSE
     248            0 :             CPABORT("GAPO works only with STATES MIXED")
     249              :          END IF
     250              :       CASE (do_loc_scdm)
     251              :          ! Decomposition
     252            2 :          CALL scdm_qrfact(vectors)
     253              :          ! Calculation of Zij
     254            2 :          CALL zij_matrix(vectors, op_sm_set, zij_fm_set)
     255            2 :          IF (do_jacobi_refinement) THEN
     256              :             ! Intermediate spread and centers
     257              :             CALL centers_spreads_berry(qs_loc_env, nmoloc, cell, weights, &
     258            0 :                                        ispin, print_loc_section, zij=zij_fm_set, only_initial_out=.TRUE.)
     259              :             CALL jacobi_rotations(weights, zij_fm_set, vectors, para_env, max_iter=max_iter, &
     260              :                                   eps_localization=eps_localization, sweeps=sweeps, &
     261              :                                   out_each=out_each, target_time=target_time, start_time=start_time, &
     262            0 :                                   restricted=restricted)
     263              :          END IF
     264              :       CASE (do_loc_crazy)
     265              :          CALL crazy_rotations(weights, zij_fm_set, vectors, max_iter=max_iter, max_crazy_angle=max_crazy_angle, &
     266              :                               crazy_scale=crazy_scale, crazy_use_diag=crazy_use_diag, &
     267          136 :                               eps_localization=eps_localization, iterations=sweeps, converged=converged)
     268              :          ! Possibly fallback to jacobi if the crazy rotation fails
     269          136 :          IF (.NOT. converged) THEN
     270           68 :             IF (qs_loc_env%localized_wfn_control%jacobi_fallback) THEN
     271           68 :                IF (output_unit > 0) WRITE (output_unit, "(T4,A,I6,/,T4,A)") &
     272           34 :                   " Crazy Wannier localization not converged after ", sweeps, &
     273           68 :                   " iterations, switching to jacobi rotations"
     274              :                CALL jacobi_rotations(weights, zij_fm_set, vectors, para_env, max_iter=max_iter, &
     275              :                                      eps_localization=eps_localization, sweeps=sweeps, &
     276              :                                      out_each=out_each, target_time=target_time, start_time=start_time, &
     277           68 :                                      restricted=restricted)
     278              :             ELSE
     279            0 :                IF (output_unit > 0) WRITE (output_unit, "(T4,A,I6,/,T4,A)") &
     280            0 :                   " Crazy Wannier localization not converged after ", sweeps, &
     281            0 :                   " iterations, and jacobi_fallback switched off "
     282              :             END IF
     283              :          END IF
     284              :       CASE (do_loc_direct)
     285              :          CALL direct_mini(weights, zij_fm_set, vectors, max_iter=max_iter, &
     286            2 :                           eps_localization=eps_localization, iterations=sweeps)
     287              :       CASE (do_loc_l1_norm_sd)
     288           30 :          IF (.NOT. cell%orthorhombic) THEN
     289            0 :             CPABORT("Non-orthorhombic cell with the selected method NYI")
     290              :          ELSE
     291           30 :             CALL approx_l1_norm_sd(vectors, max_iter, eps_localization, converged, sweeps)
     292              :             ! here we need to set zij for the computation of the centers and spreads
     293           30 :             CALL zij_matrix(vectors, op_sm_set, zij_fm_set)
     294              :          END IF
     295              :       CASE (do_loc_cs)
     296           78 :          ALLOCATE (my_zij_cfm_set(SIZE(zij_fm_set, 1), SIZE(zij_fm_set, 2)))
     297           18 :          DO idim1 = 1, SIZE(zij_fm_set, 1)
     298           54 :             DO idim2 = 1, SIZE(zij_fm_set, 2)
     299           36 :                CALL cp_cfm_create(my_zij_cfm_set(idim1, idim2), zij_fm_set(idim1, idim2)%matrix_struct)
     300           48 :                CALL cp_fm_to_cfm(msourcer=zij_fm_set(idim1, idim2), mtarget=my_zij_cfm_set(idim1, idim2))
     301              :             END DO
     302              :          END DO
     303            6 :          ALLOCATE (complex_vectors)
     304            6 :          CALL cp_cfm_create(complex_vectors, vectors%matrix_struct)
     305            6 :          CALL cp_fm_to_cfm(msourcer=vectors, mtarget=complex_vectors)
     306              : 
     307              :          CALL cardoso_souloumiac(weights, my_zij_cfm_set, max_iter, eps_localization, sweeps, &
     308            6 :                                  out_each, complex_vectors)
     309              : 
     310            6 :          CALL cp_cfm_to_fm(complex_vectors, mtargetr=vectors)
     311            6 :          CALL cp_cfm_release(complex_vectors)
     312            6 :          DEALLOCATE (complex_vectors)
     313           18 :          DO idim1 = 1, SIZE(my_zij_cfm_set, 1)
     314           54 :             DO idim2 = 1, SIZE(my_zij_cfm_set, 2)
     315           48 :                CALL cp_cfm_release(my_zij_cfm_set(idim1, idim2))
     316              :             END DO
     317              :          END DO
     318           12 :          DEALLOCATE (my_zij_cfm_set)
     319              : 
     320              :       CASE (do_loc_none)
     321            0 :          IF (output_unit > 0) THEN
     322            0 :             WRITE (output_unit, '(T4,A,I6,A)') " No MOS localization applied "
     323              :          END IF
     324              :       CASE DEFAULT
     325          514 :          CPABORT("Unknown localization method")
     326              :       END SELECT
     327          514 :       IF (output_unit > 0) THEN
     328          451 :          IF (sweeps <= max_iter) WRITE (output_unit, '(T4,A,I3,A,I6,A)') " Localization  for spin ", ispin, &
     329          388 :             " converged in ", sweeps, " iterations"
     330              :       END IF
     331              : 
     332              :       CALL centers_spreads_berry(qs_loc_env, nmoloc, cell, weights, &
     333          514 :                                  ispin, print_loc_section, zij=zij_fm_set)
     334              : 
     335          514 :       CALL cp_print_key_finished_output(output_unit, logger, print_loc_section, "PROGRAM_RUN_INFO")
     336              : 
     337          514 :       CALL timestop(handle)
     338              : 
     339          514 :    END SUBROUTINE optimize_loc_berry
     340              : 
     341              : ! **************************************************************************************************
     342              : !> \brief ...
     343              : !> \param qs_env ...
     344              : !> \param method ...
     345              : !> \param qs_loc_env ...
     346              : !> \param vectors ...
     347              : !> \param zij_fm_set ...
     348              : !> \param ispin ...
     349              : !> \param print_loc_section ...
     350              : !> \par History
     351              : !>      04.2005 created [MI]
     352              : !> \author MI
     353              : ! **************************************************************************************************
     354            6 :    SUBROUTINE optimize_loc_pipek(qs_env, method, qs_loc_env, vectors, zij_fm_set, &
     355              :                                  ispin, print_loc_section)
     356              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     357              :       INTEGER, INTENT(IN)                                :: method
     358              :       TYPE(qs_loc_env_type), POINTER                     :: qs_loc_env
     359              :       TYPE(cp_fm_type), INTENT(IN)                       :: vectors
     360              :       TYPE(cp_fm_type), DIMENSION(:, :), INTENT(IN)      :: zij_fm_set
     361              :       INTEGER, INTENT(IN)                                :: ispin
     362              :       TYPE(section_vals_type), POINTER                   :: print_loc_section
     363              : 
     364              :       CHARACTER(len=*), PARAMETER :: routineN = 'optimize_loc_pipek'
     365              : 
     366              :       INTEGER                                            :: handle, iatom, idim1, idim2, isgf, ldz, &
     367              :                                                             max_iter, nao, natom, ncol, nmoloc, &
     368              :                                                             out_each, output_unit, sweeps
     369              :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: first_sgf, last_sgf, nsgf
     370              :       REAL(dp)                                           :: eps
     371            6 :       TYPE(cp_cfm_type), POINTER                         :: complex_vectors, my_zij_cfm_set(:, :)
     372              :       TYPE(cp_fm_pool_type), POINTER                     :: ao_ao_fm_pool
     373              :       TYPE(cp_fm_type)                                   :: opvec
     374              :       TYPE(cp_fm_type), POINTER                          :: ov_fm
     375              :       TYPE(cp_logger_type), POINTER                      :: logger
     376            6 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_s
     377            6 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: mols
     378            6 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     379            6 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     380              : 
     381            6 :       NULLIFY (mols, ov_fm)
     382              : 
     383            6 :       CALL timeset(routineN, handle)
     384            6 :       logger => cp_get_default_logger()
     385              :       output_unit = cp_print_key_unit_nr(logger, print_loc_section, "PROGRAM_RUN_INFO", &
     386            6 :                                          extension=".locInfo")
     387              : 
     388            6 :       NULLIFY (particle_set)
     389              :       ! get rows and cols of the input
     390            6 :       CALL cp_fm_get_info(vectors, nrow_global=nao, ncol_global=nmoloc)
     391              : 
     392              :       ! replicate the input kind of matrix
     393            6 :       CALL cp_fm_create(opvec, vectors%matrix_struct)
     394            6 :       CALL cp_fm_set_all(opvec, 0.0_dp)
     395              : 
     396              :       CALL get_qs_env(qs_env=qs_env, matrix_s=matrix_s, &
     397            6 :                       particle_set=particle_set, qs_kind_set=qs_kind_set, molecule_set=mols)
     398              : 
     399              :       ! localization options
     400            6 :       max_iter = qs_loc_env%localized_wfn_control%max_iter
     401            6 :       eps = qs_loc_env%localized_wfn_control%eps_localization
     402            6 :       out_each = qs_loc_env%localized_wfn_control%out_each
     403              : 
     404            6 :       natom = SIZE(particle_set, 1)
     405           18 :       ALLOCATE (first_sgf(natom))
     406           12 :       ALLOCATE (last_sgf(natom))
     407           12 :       ALLOCATE (nsgf(natom))
     408              : 
     409              :       !   construction of
     410              :       CALL get_particle_set(particle_set, qs_kind_set, &
     411            6 :                             first_sgf=first_sgf, last_sgf=last_sgf, nsgf=nsgf)
     412              : 
     413              :       !   Copy the overlap sparse matrix in a full matrix
     414            6 :       CALL mpools_get(qs_env%mpools, ao_ao_fm_pool=ao_ao_fm_pool)
     415            6 :       ALLOCATE (ov_fm)
     416            6 :       CALL fm_pool_create_fm(ao_ao_fm_pool, ov_fm, name=" ")
     417            6 :       CALL copy_dbcsr_to_fm(matrix_s(1)%matrix, ov_fm)
     418              : 
     419              :       !   Compute zij here
     420           78 :       DO iatom = 1, natom
     421           72 :          CALL cp_fm_set_all(zij_fm_set(iatom, 1), 0.0_dp)
     422           72 :          CALL cp_fm_get_info(zij_fm_set(iatom, 1), ncol_global=ldz)
     423           72 :          isgf = first_sgf(iatom)
     424           72 :          ncol = nsgf(iatom)
     425              : 
     426              :          ! multiply fmxfm, using only part of the ao : Ct x S
     427              :          CALL parallel_gemm('N', 'N', nao, nmoloc, nao, 1.0_dp, ov_fm, vectors, 0.0_dp, opvec, &
     428           72 :                             a_first_col=1, a_first_row=1, b_first_col=1, b_first_row=1)
     429              : 
     430              :          CALL parallel_gemm('T', 'N', nmoloc, nmoloc, ncol, 0.5_dp, vectors, opvec, &
     431              :                             0.0_dp, zij_fm_set(iatom, 1), &
     432           72 :                             a_first_col=1, a_first_row=isgf, b_first_col=1, b_first_row=isgf)
     433              : 
     434              :          CALL parallel_gemm('N', 'N', nao, nmoloc, ncol, 1.0_dp, ov_fm, vectors, 0.0_dp, opvec, &
     435           72 :                             a_first_col=isgf, a_first_row=1, b_first_col=1, b_first_row=isgf)
     436              : 
     437              :          CALL parallel_gemm('T', 'N', nmoloc, nmoloc, nao, 0.5_dp, vectors, opvec, &
     438              :                             1.0_dp, zij_fm_set(iatom, 1), &
     439          150 :                             a_first_col=1, a_first_row=1, b_first_col=1, b_first_row=1)
     440              : 
     441              :       END DO ! iatom
     442              : 
     443              :       !   And now perform the optimization and rotate the orbitals
     444            6 :       SELECT CASE (method)
     445              :       CASE (do_loc_jacobi)
     446            0 :          CALL jacobi_rotation_pipek(zij_fm_set, vectors, sweeps)
     447              :       CASE (do_loc_gapo)
     448            0 :          CPABORT("GAPO and Pipek not implemented.")
     449              :       CASE (do_loc_crazy)
     450            0 :          CPABORT("Crazy and Pipek not implemented.")
     451              :       CASE (do_loc_l1_norm_sd)
     452            0 :          CPABORT("L1 norm and Pipek not implemented.")
     453              :       CASE (do_loc_direct)
     454            0 :          CPABORT("Direct and Pipek not implemented.")
     455              :       CASE (do_loc_cs)
     456          102 :          ALLOCATE (my_zij_cfm_set(SIZE(zij_fm_set, 1), SIZE(zij_fm_set, 2)))
     457           78 :          DO idim1 = 1, SIZE(zij_fm_set, 1)
     458          150 :             DO idim2 = 1, SIZE(zij_fm_set, 2)
     459           72 :                CALL cp_cfm_create(my_zij_cfm_set(idim1, idim2), zij_fm_set(idim1, idim2)%matrix_struct)
     460          144 :                CALL cp_fm_to_cfm(msourcer=zij_fm_set(idim1, idim2), mtarget=my_zij_cfm_set(idim1, idim2))
     461              :             END DO
     462              :          END DO
     463            6 :          ALLOCATE (complex_vectors)
     464            6 :          CALL cp_cfm_create(complex_vectors, vectors%matrix_struct)
     465            6 :          CALL cp_fm_to_cfm(msourcer=vectors, mtarget=complex_vectors)
     466              : 
     467            6 :          CALL cardoso_souloumiac_pipek(my_zij_cfm_set, complex_vectors, sweeps, max_iter, eps, out_each)
     468              : 
     469            6 :          CALL cp_cfm_to_fm(complex_vectors, mtargetr=vectors)
     470            6 :          CALL cp_cfm_release(complex_vectors)
     471            6 :          DEALLOCATE (complex_vectors)
     472           78 :          DO idim1 = 1, SIZE(my_zij_cfm_set, 1)
     473          150 :             DO idim2 = 1, SIZE(my_zij_cfm_set, 2)
     474          144 :                CALL cp_cfm_release(my_zij_cfm_set(idim1, idim2))
     475              :             END DO
     476              :          END DO
     477           12 :          DEALLOCATE (my_zij_cfm_set)
     478              : 
     479              :       CASE (do_loc_none)
     480            0 :          IF (output_unit > 0) WRITE (output_unit, '(A,I6,A)') " No MOS localization applied "
     481              :       CASE DEFAULT
     482            6 :          CPABORT("Unknown localization method")
     483              :       END SELECT
     484              : 
     485            9 :       IF (output_unit > 0) WRITE (output_unit, '(A,I3,A,I6,A)') " Localization  for spin ", ispin, &
     486            6 :          " converged in ", sweeps, " iterations"
     487              : 
     488              :       CALL centers_spreads_pipek(qs_loc_env, zij_fm_set, particle_set, ispin, &
     489            6 :                                  print_loc_section)
     490              : 
     491            6 :       DEALLOCATE (first_sgf, last_sgf, nsgf)
     492              : 
     493            6 :       CALL cp_fm_release(opvec)
     494            6 :       CALL cp_fm_release(ov_fm)
     495            6 :       DEALLOCATE (ov_fm)
     496            6 :       CALL cp_print_key_finished_output(output_unit, logger, print_loc_section, "PROGRAM_RUN_INFO")
     497              : 
     498            6 :       CALL timestop(handle)
     499              : 
     500           18 :    END SUBROUTINE optimize_loc_pipek
     501              : 
     502              : ! **************************************************************************************************
     503              : !> \brief 2by2 rotation for the pipek operator
     504              : !>       in this case the z_ij numbers are reals
     505              : !> \param zij_fm_set ...
     506              : !> \param vectors ...
     507              : !> \param sweeps ...
     508              : !> \par History
     509              : !>       05-2005 created
     510              : !> \author MI
     511              : ! **************************************************************************************************
     512            0 :    SUBROUTINE jacobi_rotation_pipek(zij_fm_set, vectors, sweeps)
     513              : 
     514              :       TYPE(cp_fm_type), DIMENSION(:, :), INTENT(IN)      :: zij_fm_set
     515              :       TYPE(cp_fm_type), INTENT(IN)                       :: vectors
     516              :       INTEGER                                            :: sweeps
     517              : 
     518              :       INTEGER                                            :: iatom, istate, jstate, natom, nstate
     519              :       REAL(KIND=dp)                                      :: aij, bij, ct, mii, mij, mjj, ratio, st, &
     520              :                                                             theta, tolerance
     521              :       TYPE(cp_fm_type)                                   :: rmat
     522              : 
     523            0 :       CALL cp_fm_create(rmat, zij_fm_set(1, 1)%matrix_struct)
     524            0 :       CALL cp_fm_set_all(rmat, 0.0_dp, 1.0_dp)
     525              : 
     526            0 :       CALL cp_fm_get_info(rmat, nrow_global=nstate)
     527            0 :       tolerance = 1.0e10_dp
     528            0 :       sweeps = 0
     529            0 :       natom = SIZE(zij_fm_set, 1)
     530              :       ! do jacobi sweeps until converged
     531            0 :       DO WHILE (tolerance >= 1.0e-4_dp)
     532            0 :          sweeps = sweeps + 1
     533            0 :          DO istate = 1, nstate
     534            0 :             DO jstate = istate + 1, nstate
     535              :                aij = 0.0_dp
     536              :                bij = 0.0_dp
     537            0 :                DO iatom = 1, natom
     538            0 :                   CALL cp_fm_get_element(zij_fm_set(iatom, 1), istate, istate, mii)
     539            0 :                   CALL cp_fm_get_element(zij_fm_set(iatom, 1), istate, jstate, mij)
     540            0 :                   CALL cp_fm_get_element(zij_fm_set(iatom, 1), jstate, jstate, mjj)
     541            0 :                   aij = aij + mij*(mii - mjj)
     542            0 :                   bij = bij + mij*mij - 0.25_dp*(mii - mjj)*(mii - mjj)
     543              :                END DO
     544            0 :                IF (ABS(bij) > 1.E-10_dp) THEN
     545            0 :                   ratio = -aij/bij
     546            0 :                   theta = 0.25_dp*ATAN(ratio)
     547              :                ELSE
     548            0 :                   bij = 0.0_dp
     549              :                   theta = 0.0_dp
     550              :                END IF
     551              :                ! Check max or min
     552              :                ! To minimize the spread
     553            0 :                IF (theta > pi*0.5_dp) THEN
     554            0 :                   theta = theta - pi*0.25_dp
     555            0 :                ELSE IF (theta < -pi*0.5_dp) THEN
     556            0 :                   theta = theta + pi*0.25_dp
     557              :                END IF
     558              : 
     559            0 :                ct = COS(theta)
     560            0 :                st = SIN(theta)
     561              : 
     562            0 :                CALL cp_fm_rot_cols(rmat, istate, jstate, ct, st)
     563              : 
     564            0 :                DO iatom = 1, natom
     565            0 :                   CALL cp_fm_rot_cols(zij_fm_set(iatom, 1), istate, jstate, ct, st)
     566            0 :                   CALL cp_fm_rot_rows(zij_fm_set(iatom, 1), istate, jstate, ct, st)
     567              :                END DO
     568              :             END DO
     569              :          END DO
     570            0 :          CALL check_tolerance_real(zij_fm_set, tolerance)
     571              :       END DO
     572              : 
     573            0 :       CALL rotate_orbitals(rmat, vectors)
     574            0 :       CALL cp_fm_release(rmat)
     575              : 
     576            0 :    END SUBROUTINE jacobi_rotation_pipek
     577              : 
     578              : ! **************************************************************************************************
     579              : !> \brief ...
     580              : !> \param zij_fm_set ...
     581              : !> \param tolerance ...
     582              : !> \par History
     583              : !>      04.2005 created [MI]
     584              : !> \author MI
     585              : ! **************************************************************************************************
     586            0 :    SUBROUTINE check_tolerance_real(zij_fm_set, tolerance)
     587              : 
     588              :       TYPE(cp_fm_type), DIMENSION(:, :), INTENT(IN)      :: zij_fm_set
     589              :       REAL(dp), INTENT(OUT)                              :: tolerance
     590              : 
     591              :       INTEGER                                            :: iatom, istate, jstate, natom, &
     592              :                                                             ncol_local, nrow_global, nrow_local
     593            0 :       INTEGER, DIMENSION(:), POINTER                     :: col_indices, row_indices
     594              :       REAL(dp)                                           :: grad_ij, zii, zij, zjj
     595            0 :       REAL(dp), DIMENSION(:, :), POINTER                 :: diag
     596              :       TYPE(cp_fm_type)                                   :: force
     597              : 
     598            0 :       CALL cp_fm_create(force, zij_fm_set(1, 1)%matrix_struct)
     599            0 :       CALL cp_fm_set_all(force, 0._dp)
     600              : 
     601            0 :       NULLIFY (diag, col_indices, row_indices)
     602            0 :       natom = SIZE(zij_fm_set, 1)
     603              :       CALL cp_fm_get_info(zij_fm_set(1, 1), nrow_local=nrow_local, &
     604              :                           ncol_local=ncol_local, nrow_global=nrow_global, &
     605            0 :                           row_indices=row_indices, col_indices=col_indices)
     606            0 :       ALLOCATE (diag(nrow_global, natom))
     607              : 
     608            0 :       DO iatom = 1, natom
     609            0 :          DO istate = 1, nrow_global
     610            0 :             CALL cp_fm_get_element(zij_fm_set(iatom, 1), istate, istate, diag(istate, iatom))
     611              :          END DO
     612              :       END DO
     613              : 
     614            0 :       DO istate = 1, nrow_local
     615            0 :          DO jstate = 1, ncol_local
     616              :             grad_ij = 0.0_dp
     617            0 :             DO iatom = 1, natom
     618            0 :                zii = diag(row_indices(istate), iatom)
     619            0 :                zjj = diag(col_indices(jstate), iatom)
     620            0 :                zij = zij_fm_set(iatom, 1)%local_data(istate, jstate)
     621            0 :                grad_ij = grad_ij + 4.0_dp*zij*(zjj - zii)
     622              :             END DO
     623            0 :             force%local_data(istate, jstate) = grad_ij
     624              :          END DO
     625              :       END DO
     626              : 
     627            0 :       DEALLOCATE (diag)
     628              : 
     629            0 :       CALL cp_fm_maxabsval(force, tolerance)
     630            0 :       CALL cp_fm_release(force)
     631              : 
     632            0 :    END SUBROUTINE check_tolerance_real
     633              : ! **************************************************************************************************
     634              : !> \brief ...
     635              : !> \param qs_loc_env ...
     636              : !> \param nmoloc ...
     637              : !> \param cell ...
     638              : !> \param weights ...
     639              : !> \param ispin ...
     640              : !> \param print_loc_section ...
     641              : !> \param zij ...
     642              : !> \param c_zij ...
     643              : !> \param only_initial_out ...
     644              : !> \par History
     645              : !>      04.2005 created [MI]
     646              : !> \author MI
     647              : ! **************************************************************************************************
     648         1140 :    SUBROUTINE centers_spreads_berry(qs_loc_env, nmoloc, cell, weights, ispin, &
     649         1140 :                                     print_loc_section, zij, c_zij, only_initial_out)
     650              :       TYPE(qs_loc_env_type), POINTER                     :: qs_loc_env
     651              :       INTEGER, INTENT(IN)                                :: nmoloc
     652              :       TYPE(cell_type), POINTER                           :: cell
     653              :       REAL(dp), DIMENSION(:)                             :: weights
     654              :       INTEGER, INTENT(IN)                                :: ispin
     655              :       TYPE(section_vals_type), POINTER                   :: print_loc_section
     656              :       TYPE(cp_fm_type), DIMENSION(:, :), INTENT(IN), &
     657              :          OPTIONAL                                        :: zij
     658              :       TYPE(cp_cfm_p_type), INTENT(INOUT), OPTIONAL       :: c_zij(:, :)
     659              :       LOGICAL, INTENT(IN), OPTIONAL                      :: only_initial_out
     660              : 
     661              :       CHARACTER(len=default_path_length)                 :: file_tmp, iter
     662              :       COMPLEX(KIND=dp)                                   :: z
     663              :       INTEGER                                            :: idir, istate, jdir, nstates, &
     664              :                                                             output_unit, unit_out_s
     665              :       LOGICAL                                            :: my_only_init, zij_is_complex
     666              :       REAL(dp)                                           :: avg_spread_ii, spread_i, spread_ii, &
     667              :                                                             sum_spread_i, sum_spread_ii
     668              :       REAL(dp), DIMENSION(3)                             :: c, c2, cpbc
     669         1140 :       REAL(dp), DIMENSION(:, :), POINTER                 :: centers
     670              :       REAL(KIND=dp)                                      :: imagpart, realpart
     671              :       TYPE(cp_logger_type), POINTER                      :: logger
     672              :       TYPE(section_vals_type), POINTER                   :: print_key
     673              : 
     674         1140 :       NULLIFY (centers, logger, print_key)
     675         2280 :       logger => cp_get_default_logger()
     676         1140 :       my_only_init = .FALSE.
     677         1140 :       IF (PRESENT(only_initial_out)) my_only_init = only_initial_out
     678         1140 :       IF (PRESENT(zij)) THEN
     679         1140 :          zij_is_complex = .FALSE.
     680         1140 :          CPASSERT(.NOT. PRESENT(c_zij))
     681         1140 :          CALL cp_fm_get_info(zij(1, 1), nrow_global=nstates)
     682              :       ELSE
     683            0 :          zij_is_complex = .TRUE.
     684            0 :          CPASSERT(PRESENT(c_zij))
     685            0 :          CALL cp_cfm_get_info(c_zij(1, 1)%matrix, nrow_global=nstates)
     686              :       END IF
     687              : 
     688         1140 :       file_tmp = TRIM(qs_loc_env%tag_mo)//"_spreads_s"//TRIM(ADJUSTL(cp_to_string(ispin)))
     689              :       output_unit = cp_print_key_unit_nr(logger, print_loc_section, "PROGRAM_RUN_INFO", &
     690         1140 :                                          extension=".locInfo")
     691              :       unit_out_s = cp_print_key_unit_nr(logger, print_loc_section, "WANNIER_SPREADS", &
     692         1140 :                                         middle_name=file_tmp, extension=".data")
     693         1140 :       iter = cp_iter_string(logger%iter_info)
     694         1140 :       IF (unit_out_s > 0 .AND. .NOT. my_only_init) WRITE (unit_out_s, '(i6,/,A)') nmoloc, TRIM(iter)
     695              : 
     696         1140 :       CPASSERT(nstates >= nmoloc)
     697              : 
     698         1140 :       centers => qs_loc_env%localized_wfn_control%centers_set(ispin)%array
     699         1140 :       CPASSERT(ASSOCIATED(centers))
     700         1140 :       CPASSERT(SIZE(centers, 2) == nmoloc)
     701         1140 :       sum_spread_i = 0.0_dp
     702         1140 :       sum_spread_ii = 0.0_dp
     703         1140 :       avg_spread_ii = 0.0_dp
     704         9154 :       DO istate = 1, nmoloc
     705         8014 :          c = 0.0_dp
     706              :          c2 = 0.0_dp
     707         8014 :          spread_i = 0.0_dp
     708         8014 :          spread_ii = 0.0_dp
     709         8014 :          IF (.NOT. zij_is_complex) THEN
     710        32584 :             DO jdir = 1, SIZE(zij, 2)
     711        24570 :                CALL cp_fm_get_element(zij(1, jdir), istate, istate, realpart)
     712        24570 :                CALL cp_fm_get_element(zij(2, jdir), istate, istate, imagpart)
     713        24570 :                z = CMPLX(realpart, imagpart, dp)
     714              :                spread_i = spread_i - weights(jdir)* &
     715        24570 :                           LOG(realpart*realpart + imagpart*imagpart)/twopi/twopi
     716              :                spread_ii = spread_ii + weights(jdir)* &
     717        24570 :                            (1.0_dp - (realpart*realpart + imagpart*imagpart))/twopi/twopi
     718        32584 :                IF (jdir < 4) THEN
     719        96168 :                   DO idir = 1, 3
     720              :                      c(idir) = c(idir) + &
     721        96168 :                                (cell%hmat(idir, jdir)/twopi)*AIMAG(LOG(z))
     722              :                   END DO
     723              :                END IF
     724              :             END DO
     725              :          ELSE
     726            0 :             DO jdir = 1, SIZE(c_zij, 2)
     727            0 :                CALL cp_cfm_get_element(c_zij(1, jdir)%matrix, istate, istate, z)
     728            0 :                realpart = REAL(z, dp)
     729            0 :                imagpart = AIMAG(z)
     730              :                spread_i = spread_i - weights(jdir)* &
     731            0 :                           LOG(realpart*realpart + imagpart*imagpart)/twopi/twopi
     732              :                spread_ii = spread_ii + weights(jdir)* &
     733            0 :                            (1.0_dp - (realpart*realpart + imagpart*imagpart))/twopi/twopi
     734            0 :                IF (jdir < 4) THEN
     735            0 :                   DO idir = 1, 3
     736              :                      c(idir) = c(idir) + &
     737            0 :                                (cell%hmat(idir, jdir)/twopi)*AIMAG(LOG(z))
     738              :                   END DO
     739              :                END IF
     740              :             END DO
     741              :          END IF
     742         8014 :          cpbc = pbc(c, cell)
     743        32056 :          centers(1:3, istate) = cpbc(1:3)
     744         8014 :          centers(4, istate) = spread_i
     745         8014 :          centers(5, istate) = spread_ii
     746         8014 :          sum_spread_i = sum_spread_i + centers(4, istate)
     747         8014 :          sum_spread_ii = sum_spread_ii + centers(5, istate)
     748         9210 :          IF (unit_out_s > 0 .AND. .NOT. my_only_init) WRITE (unit_out_s, '(I6,2F16.8)') istate, centers(4:5, istate)
     749              :       END DO
     750         1140 :       avg_spread_ii = sum_spread_ii/REAL(nmoloc, KIND=dp)
     751              : 
     752              :       ! Print of wannier centers
     753         1140 :       print_key => section_vals_get_subs_vals(print_loc_section, "WANNIER_CENTERS")
     754         1140 :       IF (.NOT. my_only_init) CALL print_wannier_centers(qs_loc_env, print_key, centers, logger, ispin)
     755              : 
     756         1140 :       IF (output_unit > 0) THEN
     757          514 :          WRITE (output_unit, '(T4, A, 2x, 2A26,/,T23, A28)') " Spread Functional ", "sum_in -w_i ln(|z_in|^2)", &
     758         1028 :             "sum_in w_i(1-|z_in|^2)", "sum_in w_i(1-|z_in|^2)/n"
     759          514 :          IF (my_only_init) THEN
     760          257 :             WRITE (output_unit, '(T4,A,T38,2F20.10,/,T38,F20.10)') " Initial Spread (Berry) : ", &
     761          514 :                sum_spread_i, sum_spread_ii, avg_spread_ii
     762              :          ELSE
     763          257 :             WRITE (output_unit, '(T4,A,T38,2F20.10,/,T38,F20.10)') " Total Spread (Berry) : ", &
     764          514 :                sum_spread_i, sum_spread_ii, avg_spread_ii
     765              :          END IF
     766              :       END IF
     767              : 
     768         1140 :       IF (unit_out_s > 0 .AND. .NOT. my_only_init) WRITE (unit_out_s, '(A,2F16.10)') " Total ", sum_spread_i, sum_spread_ii
     769              : 
     770         1140 :       CALL cp_print_key_finished_output(unit_out_s, logger, print_loc_section, "WANNIER_SPREADS")
     771         1140 :       CALL cp_print_key_finished_output(output_unit, logger, print_loc_section, "PROGRAM_RUN_INFO")
     772              : 
     773         1140 :    END SUBROUTINE centers_spreads_berry
     774              : 
     775              : ! **************************************************************************************************
     776              : !> \brief define and print the centers and spread
     777              : !>       when the pipek operator is used
     778              : !> \param qs_loc_env ...
     779              : !> \param zij_fm_set matrix elements that define the populations on atoms
     780              : !> \param particle_set ...
     781              : !> \param ispin spin 1 or 2
     782              : !> \param print_loc_section ...
     783              : !> \par History
     784              : !>      05.2005 created [MI]
     785              : ! **************************************************************************************************
     786            6 :    SUBROUTINE centers_spreads_pipek(qs_loc_env, zij_fm_set, particle_set, ispin, &
     787              :                                     print_loc_section)
     788              : 
     789              :       TYPE(qs_loc_env_type), POINTER                     :: qs_loc_env
     790              :       TYPE(cp_fm_type), DIMENSION(:, :), INTENT(IN)      :: zij_fm_set
     791              :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     792              :       INTEGER, INTENT(IN)                                :: ispin
     793              :       TYPE(section_vals_type), POINTER                   :: print_loc_section
     794              : 
     795              :       CHARACTER(len=default_path_length)                 :: file_tmp, iter
     796              :       INTEGER                                            :: iatom, istate, natom, nstate, unit_out_s
     797            6 :       INTEGER, DIMENSION(:), POINTER                     :: atom_of_state
     798              :       REAL(dp)                                           :: r(3)
     799            6 :       REAL(dp), ALLOCATABLE, DIMENSION(:)                :: Qii, ziimax
     800            6 :       REAL(dp), ALLOCATABLE, DIMENSION(:, :)             :: diag
     801            6 :       REAL(dp), DIMENSION(:, :), POINTER                 :: centers
     802              :       TYPE(cp_logger_type), POINTER                      :: logger
     803              :       TYPE(section_vals_type), POINTER                   :: print_key
     804              : 
     805            6 :       NULLIFY (centers, logger, print_key)
     806           12 :       logger => cp_get_default_logger()
     807              : 
     808            6 :       CALL cp_fm_get_info(zij_fm_set(1, 1), nrow_global=nstate)
     809            6 :       natom = SIZE(zij_fm_set, 1)
     810              : 
     811            6 :       centers => qs_loc_env%localized_wfn_control%centers_set(ispin)%array
     812            6 :       CPASSERT(ASSOCIATED(centers))
     813            6 :       CPASSERT(SIZE(centers, 2) == nstate)
     814              : 
     815            6 :       file_tmp = TRIM(qs_loc_env%tag_mo)//"_spreads_s"//TRIM(ADJUSTL(cp_to_string(ispin)))
     816              :       unit_out_s = cp_print_key_unit_nr(logger, print_loc_section, "WANNIER_SPREADS", &
     817            6 :                                         middle_name=file_tmp, extension=".data", log_filename=.FALSE.)
     818            6 :       iter = cp_iter_string(logger%iter_info)
     819            6 :       IF (unit_out_s > 0) WRITE (unit_out_s, '(i6,/,A)') TRIM(iter)
     820              : 
     821           18 :       ALLOCATE (atom_of_state(nstate))
     822          198 :       atom_of_state = 0
     823              : 
     824           24 :       ALLOCATE (diag(nstate, natom))
     825            6 :       diag = 0.0_dp
     826              : 
     827           78 :       DO iatom = 1, natom
     828         2382 :          DO istate = 1, nstate
     829         2376 :             CALL cp_fm_get_element(zij_fm_set(iatom, 1), istate, istate, diag(istate, iatom))
     830              :          END DO
     831              :       END DO
     832              : 
     833           24 :       ALLOCATE (Qii(nstate), ziimax(nstate))
     834            6 :       ziimax = 0.0_dp
     835            6 :       Qii = 0.0_dp
     836              : 
     837           78 :       DO iatom = 1, natom
     838         2382 :          DO istate = 1, nstate
     839         2304 :             Qii(istate) = Qii(istate) + diag(istate, iatom)*diag(istate, iatom)
     840         2376 :             IF (ABS(diag(istate, iatom)) > ziimax(istate)) THEN
     841          706 :                ziimax(istate) = ABS(diag(istate, iatom))
     842          706 :                atom_of_state(istate) = iatom
     843              :             END IF
     844              :          END DO
     845              :       END DO
     846              : 
     847          198 :       DO istate = 1, nstate
     848          192 :          iatom = atom_of_state(istate)
     849          768 :          r(1:3) = particle_set(iatom)%r(1:3)
     850          768 :          centers(1:3, istate) = r(1:3)
     851          192 :          centers(4, istate) = 1.0_dp/Qii(istate)
     852          198 :          IF (unit_out_s > 0) WRITE (unit_out_s, '(I6,F16.8)') istate, angstrom*centers(4, istate)
     853              :       END DO
     854              : 
     855              :       ! Print the wannier centers
     856            6 :       print_key => section_vals_get_subs_vals(print_loc_section, "WANNIER_CENTERS")
     857            6 :       CALL print_wannier_centers(qs_loc_env, print_key, centers, logger, ispin)
     858              : 
     859            6 :       CALL cp_print_key_finished_output(unit_out_s, logger, print_loc_section, "WANNIER_SPREADS")
     860              : 
     861            6 :       DEALLOCATE (Qii, ziimax, atom_of_state, diag)
     862              : 
     863            6 :    END SUBROUTINE centers_spreads_pipek
     864              : 
     865              : ! **************************************************************************************************
     866              : !> \brief write the cube files for a set of selected states
     867              : !> \param qs_env ...
     868              : !> \param mo_coeff set mos from which the states to be printed are extracted
     869              : !> \param nstates number of states to be printed
     870              : !> \param state_list list of the indexes of the states to be printed
     871              : !> \param centers centers and spread, all=0 if they hva not been calculated
     872              : !> \param print_key ...
     873              : !> \param root initial part of the cube file names
     874              : !> \param ispin ...
     875              : !> \param idir ...
     876              : !> \param state0 ...
     877              : !> \param file_position ...
     878              : !> \par History
     879              : !>      08.2005 created [MI]
     880              : !> \author MI
     881              : !> \note
     882              : !>      This routine should not be in this module
     883              : ! **************************************************************************************************
     884           12 :    SUBROUTINE qs_print_cubes(qs_env, mo_coeff, nstates, state_list, centers, &
     885              :                              print_key, root, ispin, idir, state0, file_position)
     886              : 
     887              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     888              :       TYPE(cp_fm_type), INTENT(IN)                       :: mo_coeff
     889              :       INTEGER, INTENT(IN)                                :: nstates
     890              :       INTEGER, DIMENSION(:), POINTER                     :: state_list
     891              :       REAL(dp), DIMENSION(:, :), POINTER                 :: centers
     892              :       TYPE(section_vals_type), POINTER                   :: print_key
     893              :       CHARACTER(LEN=*)                                   :: root
     894              :       INTEGER, INTENT(IN), OPTIONAL                      :: ispin, idir
     895              :       INTEGER, OPTIONAL                                  :: state0
     896              :       CHARACTER(LEN=default_string_length), OPTIONAL     :: file_position
     897              : 
     898              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'qs_print_cubes'
     899              :       CHARACTER, DIMENSION(3), PARAMETER                 :: labels = ['x', 'y', 'z']
     900              : 
     901              :       CHARACTER                                          :: label
     902              :       CHARACTER(LEN=default_path_length)                 :: file_tmp, filename, my_pos
     903              :       CHARACTER(LEN=default_string_length)               :: title
     904              :       INTEGER                                            :: handle, ia, ie, istate, ivector, &
     905              :                                                             my_ispin, my_state0, unit_out_c
     906              :       LOGICAL                                            :: add_idir, add_spin, mpi_io
     907           12 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     908              :       TYPE(cell_type), POINTER                           :: cell
     909              :       TYPE(cp_logger_type), POINTER                      :: logger
     910              :       TYPE(dft_control_type), POINTER                    :: dft_control
     911              :       TYPE(particle_list_type), POINTER                  :: particles
     912           12 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     913              :       TYPE(pw_c1d_gs_type)                               :: wf_g
     914              :       TYPE(pw_env_type), POINTER                         :: pw_env
     915              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
     916              :       TYPE(pw_r3d_rs_type)                               :: wf_r
     917           12 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     918              :       TYPE(qs_subsys_type), POINTER                      :: subsys
     919              : 
     920           12 :       CALL timeset(routineN, handle)
     921           12 :       logger => cp_get_default_logger()
     922              : 
     923           12 :       CALL get_qs_env(qs_env=qs_env, pw_env=pw_env, subsys=subsys)
     924           12 :       CALL qs_subsys_get(subsys, particles=particles)
     925              : 
     926           12 :       CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
     927              : 
     928           12 :       CALL auxbas_pw_pool%create_pw(wf_r)
     929           12 :       CALL auxbas_pw_pool%create_pw(wf_g)
     930              : 
     931           12 :       my_state0 = 0
     932           12 :       IF (PRESENT(state0)) my_state0 = state0
     933              : 
     934           12 :       add_spin = .FALSE.
     935           12 :       my_ispin = 1
     936           12 :       IF (PRESENT(ispin)) THEN
     937            8 :          add_spin = .TRUE.
     938            8 :          my_ispin = ispin
     939              :       END IF
     940           12 :       add_idir = .FALSE.
     941           12 :       IF (PRESENT(idir)) THEN
     942            0 :          add_idir = .TRUE.
     943            0 :          label = labels(idir)
     944              :       END IF
     945              : 
     946           12 :       my_pos = "REWIND"
     947           12 :       IF (PRESENT(file_position)) THEN
     948           12 :          my_pos = file_position
     949              :       END IF
     950              : 
     951           62 :       DO istate = 1, nstates
     952           50 :          ivector = state_list(istate) - my_state0
     953              :          CALL get_qs_env(qs_env=qs_env, atomic_kind_set=atomic_kind_set, qs_kind_set=qs_kind_set, cell=cell, &
     954           50 :                          dft_control=dft_control, particle_set=particle_set, pw_env=pw_env)
     955              : 
     956              :          CALL calculate_wavefunction(mo_coeff, ivector, wf_r, wf_g, atomic_kind_set, &
     957           50 :                                      qs_kind_set, cell, dft_control, particle_set, pw_env)
     958              : 
     959              :          ! Formatting the middle part of the name
     960           50 :          ivector = state_list(istate)
     961           50 :          CALL xstring(root, ia, ie)
     962           50 :          IF (add_idir) THEN
     963            0 :             filename = root(ia:ie)//"_"//label//"_w"//TRIM(ADJUSTL(cp_to_string(ivector)))
     964              :          ELSE
     965           50 :             filename = root(ia:ie)//"_w"//TRIM(ADJUSTL(cp_to_string(ivector)))
     966              :          END IF
     967           50 :          IF (add_spin) THEN
     968           38 :             file_tmp = filename
     969           38 :             CALL xstring(file_tmp, ia, ie)
     970           38 :             filename = file_tmp(ia:ie)//"_s"//TRIM(ADJUSTL(cp_to_string(ispin)))
     971              :          END IF
     972              : 
     973              :          ! Using the print_key tools to open the file with the proper name
     974           50 :          mpi_io = .TRUE.
     975              :          unit_out_c = cp_print_key_unit_nr(logger, print_key, "", middle_name=filename, &
     976              :                                            extension=".cube", file_position=my_pos, log_filename=.FALSE., &
     977           50 :                                            mpi_io=mpi_io)
     978           50 :          IF (SIZE(centers, 1) == 6) THEN
     979           50 :             WRITE (title, '(A7,I5.5,A2,I1.1,A1,6F10.4)') "WFN ", ivector, "_s", my_ispin, " ", &
     980          400 :                centers(1:3, istate)*angstrom, centers(4:6, istate)*angstrom
     981              :          ELSE
     982            0 :             WRITE (title, '(A7,I5.5,A2,I1.1,A1,3F10.4)') "WFN ", ivector, "_s", my_ispin, " ", &
     983            0 :                centers(1:3, istate)*angstrom
     984              :          END IF
     985              :          CALL cp_pw_to_cube(wf_r, unit_out_c, title, &
     986              :                             particles=particles, &
     987           50 :                             stride=section_get_ivals(print_key, "STRIDE"), mpi_io=mpi_io)
     988           62 :          CALL cp_print_key_finished_output(unit_out_c, logger, print_key, "", mpi_io=mpi_io)
     989              :       END DO
     990              : 
     991           12 :       CALL auxbas_pw_pool%give_back_pw(wf_r)
     992           12 :       CALL auxbas_pw_pool%give_back_pw(wf_g)
     993           12 :       CALL timestop(handle)
     994           12 :    END SUBROUTINE qs_print_cubes
     995              : 
     996              : ! **************************************************************************************************
     997              : !> \brief Prints the electronic density
     998              : !> \param qs_env ...
     999              : !> \param nstates ...
    1000              : !> \param state_list ...
    1001              : !> \param centers ...
    1002              : !> \param print_key ...
    1003              : !> \param root ...
    1004              : !> \param mo_coeff ...
    1005              : !> \param complex_mo_coeff ...
    1006              : !> \param ispin ...
    1007              : !> \param file_position ...
    1008              : ! **************************************************************************************************
    1009              : 
    1010            0 :    SUBROUTINE qs_print_density_cubes(qs_env, nstates, state_list, centers, print_key, root, &
    1011              :                                      mo_coeff, complex_mo_coeff, ispin, file_position)
    1012              : 
    1013              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1014              :       INTEGER, INTENT(IN)                                :: nstates
    1015              :       INTEGER, DIMENSION(:), POINTER                     :: state_list
    1016              :       REAL(dp), DIMENSION(:, :), POINTER                 :: centers
    1017              :       TYPE(section_vals_type), POINTER                   :: print_key
    1018              :       CHARACTER(LEN=*)                                   :: root
    1019              :       TYPE(cp_fm_type), OPTIONAL, POINTER                :: mo_coeff
    1020              :       TYPE(cp_cfm_type), OPTIONAL, POINTER               :: complex_mo_coeff
    1021              :       INTEGER, INTENT(IN), OPTIONAL                      :: ispin
    1022              :       CHARACTER(LEN=default_string_length), OPTIONAL     :: file_position
    1023              : 
    1024              :       TYPE(cp_fm_type), POINTER                          :: squaredmat
    1025              : 
    1026            0 :       IF (PRESENT(mo_coeff)) THEN
    1027            0 :          CPASSERT(.NOT. PRESENT(complex_mo_coeff))
    1028            0 :          CALL cp_fm_create(squaredmat, mo_coeff%matrix_struct)
    1029            0 :          squaredmat%local_data = mo_coeff%local_data**2
    1030              :       ELSE
    1031            0 :          CPASSERT(PRESENT(complex_mo_coeff))
    1032            0 :          CALL cp_fm_create(squaredmat, complex_mo_coeff%matrix_struct)
    1033              :          squaredmat%local_data = REAL(CONJG(complex_mo_coeff%local_data) &
    1034            0 :                                       *complex_mo_coeff%local_data, dp)
    1035              :       END IF
    1036              :       CALL qs_print_cubes(qs_env, squaredmat, nstates, state_list, centers, print_key, root, &
    1037            0 :                           ispin=ispin, file_position=file_position)
    1038            0 :       CALL cp_fm_release(squaredmat)
    1039              : 
    1040            0 :    END SUBROUTINE qs_print_density_cubes
    1041              : 
    1042              : ! **************************************************************************************************
    1043              : !> \brief Prints wannier centers
    1044              : !> \param qs_loc_env ...
    1045              : !> \param print_key ...
    1046              : !> \param center ...
    1047              : !> \param logger ...
    1048              : !> \param ispin ...
    1049              : ! **************************************************************************************************
    1050          520 :    SUBROUTINE print_wannier_centers(qs_loc_env, print_key, center, logger, ispin)
    1051              :       TYPE(qs_loc_env_type), POINTER                     :: qs_loc_env
    1052              :       TYPE(section_vals_type), POINTER                   :: print_key
    1053              :       REAL(KIND=dp), INTENT(IN)                          :: center(:, :)
    1054              :       TYPE(cp_logger_type), POINTER                      :: logger
    1055              :       INTEGER, INTENT(IN)                                :: ispin
    1056              : 
    1057              :       CHARACTER(default_string_length)                   :: iter, unit_str
    1058              :       CHARACTER(LEN=default_string_length)               :: my_ext, my_form
    1059              :       INTEGER                                            :: iunit, l, nstates
    1060              :       LOGICAL                                            :: first_time, init_traj
    1061              :       REAL(KIND=dp)                                      :: unit_conv
    1062              : 
    1063          520 :       nstates = SIZE(center, 2)
    1064          520 :       my_form = "formatted"
    1065          520 :       my_ext = ".data"
    1066          520 :       IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key, first_time=first_time) &
    1067              :                 , cp_p_file)) THEN
    1068              :          ! Find out if we want to print IONS+CENTERS or ONLY CENTERS..
    1069          156 :          IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key, "/IONS+CENTERS") &
    1070              :                    , cp_p_file)) THEN
    1071           54 :             CALL get_output_format(print_key, my_form=my_form, my_ext=my_ext)
    1072              :          END IF
    1073          156 :          IF (first_time .OR. (.NOT. qs_loc_env%first_time)) THEN
    1074              :             iunit = cp_print_key_unit_nr(logger, print_key, "", extension=my_ext, file_form=my_form, &
    1075              :                                          middle_name=TRIM(qs_loc_env%tag_mo)//"_centers_s"//TRIM(ADJUSTL(cp_to_string(ispin))), &
    1076          156 :                                          log_filename=.FALSE., on_file=.TRUE., is_new_file=init_traj)
    1077          156 :             IF (iunit > 0) THEN
    1078              :                ! Gather units of measure for output (if available)
    1079           78 :                CALL section_vals_val_get(print_key, "UNIT", c_val=unit_str)
    1080           78 :                unit_conv = cp_unit_from_cp2k(1.0_dp, TRIM(unit_str))
    1081              : 
    1082           78 :                IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key, "/IONS+CENTERS"), cp_p_file)) THEN
    1083              :                   ! Different possible formats
    1084           27 :                   CALL print_wannier_traj(qs_loc_env, print_key, center, iunit, init_traj, unit_conv)
    1085              :                ELSE
    1086              :                   ! Default print format
    1087           51 :                   iter = cp_iter_string(logger%iter_info)
    1088           51 :                   WRITE (iunit, '(i6,/,a)') nstates, TRIM(iter)
    1089          421 :                   DO l = 1, nstates
    1090         1901 :                      WRITE (iunit, '(A,4F16.8)') "X", unit_conv*center(1:4, l)
    1091              :                   END DO
    1092              :                END IF
    1093              :             END IF
    1094          156 :             CALL cp_print_key_finished_output(iunit, logger, print_key, on_file=.TRUE.)
    1095              :          END IF
    1096              :       END IF
    1097          520 :    END SUBROUTINE print_wannier_centers
    1098              : 
    1099              : ! **************************************************************************************************
    1100              : !> \brief computes spread and centers
    1101              : !> \param qs_loc_env ...
    1102              : !> \param print_key ...
    1103              : !> \param center ...
    1104              : !> \param iunit ...
    1105              : !> \param init_traj ...
    1106              : !> \param unit_conv ...
    1107              : ! **************************************************************************************************
    1108           27 :    SUBROUTINE print_wannier_traj(qs_loc_env, print_key, center, iunit, init_traj, unit_conv)
    1109              :       TYPE(qs_loc_env_type), POINTER                     :: qs_loc_env
    1110              :       TYPE(section_vals_type), POINTER                   :: print_key
    1111              :       REAL(KIND=dp), INTENT(IN)                          :: center(:, :)
    1112              :       INTEGER, INTENT(IN)                                :: iunit
    1113              :       LOGICAL, INTENT(IN)                                :: init_traj
    1114              :       REAL(KIND=dp), INTENT(IN)                          :: unit_conv
    1115              : 
    1116              :       CHARACTER(len=default_string_length)               :: iter, remark1, remark2, title
    1117              :       INTEGER                                            :: i, iskip, natom, ntot, outformat
    1118           27 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
    1119              :       TYPE(atomic_kind_type), POINTER                    :: atomic_kind
    1120              :       TYPE(cp_logger_type), POINTER                      :: logger
    1121           27 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1122              : 
    1123           27 :       NULLIFY (particle_set, atomic_kind_set, atomic_kind, logger)
    1124           54 :       logger => cp_get_default_logger()
    1125           27 :       natom = SIZE(qs_loc_env%particle_set)
    1126           27 :       ntot = natom + SIZE(center, 2)
    1127           27 :       CALL allocate_particle_set(particle_set, ntot)
    1128           54 :       ALLOCATE (atomic_kind_set(1))
    1129           27 :       atomic_kind => atomic_kind_set(1)
    1130              :       CALL set_atomic_kind(atomic_kind=atomic_kind, kind_number=0, &
    1131           27 :                            name="X", element_symbol="X", mass=0.0_dp)
    1132              :       ! Particles
    1133          315 :       DO i = 1, natom
    1134          288 :          particle_set(i)%atomic_kind => qs_loc_env%particle_set(i)%atomic_kind
    1135         1179 :          particle_set(i)%r = pbc(qs_loc_env%particle_set(i)%r, qs_loc_env%cell)
    1136              :       END DO
    1137              :       ! Wannier Centers
    1138          488 :       DO i = natom + 1, ntot
    1139          461 :          particle_set(i)%atomic_kind => atomic_kind
    1140         1871 :          particle_set(i)%r = pbc(center(1:3, i - natom), qs_loc_env%cell)
    1141              :       END DO
    1142              :       ! Dump the structure
    1143           27 :       CALL section_vals_val_get(print_key, "FORMAT", i_val=outformat)
    1144              : 
    1145              :       ! Header file
    1146            0 :       SELECT CASE (outformat)
    1147              :       CASE (dump_dcd, dump_dcd_aligned_cell)
    1148            0 :          IF (init_traj) THEN
    1149              :             !Lets write the header for the coordinate dcd
    1150              :             ! Note (TL) : even the new DCD format is unfortunately too poor
    1151              :             !             for our capabilities.. for example here the printing
    1152              :             !             of the geometry could be nested inside several iteration
    1153              :             !             levels.. this cannot be exactly reproduced with DCD.
    1154              :             !             Just as a compromise let's pick-up the value of the MD iteration
    1155              :             !             level. In any case this is not any sensible information for the standard.
    1156            0 :             iskip = section_get_ival(print_key, "EACH%MD")
    1157            0 :             WRITE (iunit) "CORD", 0, -1, iskip, &
    1158            0 :                0, 0, 0, 0, 0, 0, REAL(0, KIND=sp), 1, 0, 0, 0, 0, 0, 0, 0, 0, 24
    1159            0 :             remark1 = "REMARK FILETYPE CORD DCD GENERATED BY CP2K"
    1160            0 :             remark2 = "REMARK Support new DCD format with cell information"
    1161            0 :             WRITE (iunit) 2, remark1, remark2
    1162            0 :             WRITE (iunit) ntot
    1163            0 :             CALL m_flush(iunit)
    1164              :          END IF
    1165              :       CASE (dump_xmol)
    1166           27 :          iter = cp_iter_string(logger%iter_info)
    1167           27 :          WRITE (UNIT=title, FMT="(A)") " Particles+Wannier centers. Iteration:"//TRIM(iter)
    1168              :       CASE DEFAULT
    1169           27 :          title = ""
    1170              :       END SELECT
    1171              :       CALL write_particle_coordinates(particle_set, iunit, outformat, "POS", title, qs_loc_env%cell, &
    1172           27 :                                       unit_conv=unit_conv)
    1173           27 :       CALL m_flush(iunit)
    1174           27 :       CALL deallocate_particle_set(particle_set)
    1175           27 :       CALL deallocate_atomic_kind_set(atomic_kind_set)
    1176           54 :    END SUBROUTINE print_wannier_traj
    1177              : 
    1178              : ! **************************************************************************************************
    1179              : !> \brief Compute the second moments of the centers using the local (non-periodic) pos operators
    1180              : !> \param qs_env ...
    1181              : !> \param qs_loc_env ...
    1182              : !> \param print_loc_section ...
    1183              : !> \param ispin ...
    1184              : !> \par History
    1185              : !>      07.2020 created [H. Elgabarty]
    1186              : !> \author Hossam Elgabarty
    1187              : ! **************************************************************************************************
    1188            0 :    SUBROUTINE centers_second_moments_loc(qs_env, qs_loc_env, print_loc_section, ispin)
    1189              : 
    1190              :       ! I might not actually need the qs_env
    1191              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1192              :       TYPE(qs_loc_env_type), POINTER                     :: qs_loc_env
    1193              :       TYPE(section_vals_type), POINTER                   :: print_loc_section
    1194              :       INTEGER, INTENT(IN)                                :: ispin
    1195              : 
    1196              :       INTEGER, PARAMETER                                 :: norder = 2
    1197              :       LOGICAL, PARAMETER :: debug_this_subroutine = .FALSE.
    1198              : 
    1199              :       CHARACTER(len=default_path_length)                 :: file_tmp
    1200              :       INTEGER                                            :: icoeff, imoment, istate, ncol_global, &
    1201              :                                                             nm, nmoloc, nrow_global, output_unit, &
    1202              :                                                             output_unit_sm
    1203            0 :       REAL(dp), DIMENSION(:, :), POINTER                 :: centers
    1204            0 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: moment_set
    1205              :       REAL(KIND=dp), DIMENSION(3)                        :: rcc
    1206              :       REAL(KIND=dp), DIMENSION(6)                        :: cov
    1207              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct
    1208              :       TYPE(cp_fm_type)                                   :: momv, mvector, omvector
    1209            0 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: moloc_coeff
    1210              :       TYPE(cp_fm_type), POINTER                          :: mo_localized
    1211              :       TYPE(cp_logger_type), POINTER                      :: logger
    1212            0 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_s, moments
    1213              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1214              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1215              : 
    1216            0 :       logger => cp_get_default_logger()
    1217              : 
    1218              :       output_unit = cp_print_key_unit_nr(logger, print_loc_section, "PROGRAM_RUN_INFO", &
    1219            0 :                                          extension=".locInfo")
    1220              : 
    1221            0 :       IF (output_unit > 0) THEN
    1222              :          WRITE (output_unit, '(/,T2,A)') &
    1223            0 :             '!-----------------------------------------------------------------------------!'
    1224            0 :          WRITE (output_unit, '(T12,A)') "Computing second moments of Wannier functions..."
    1225              :          WRITE (output_unit, '(T2,A)') &
    1226            0 :             '!-----------------------------------------------------------------------------!'
    1227              :       END IF
    1228              : 
    1229            0 :       file_tmp = "_r_loc_cov_matrix_"//TRIM(ADJUSTL(cp_to_string(ispin)))
    1230              :       output_unit_sm = cp_print_key_unit_nr(logger, print_loc_section, "WANNIER_SPREADS", &
    1231            0 :                                             middle_name=file_tmp, extension=".data")
    1232              : 
    1233            0 :       CALL get_qs_loc_env(qs_loc_env=qs_loc_env, moloc_coeff=moloc_coeff)
    1234            0 :       centers => qs_loc_env%localized_wfn_control%centers_set(ispin)%array
    1235              : 
    1236            0 :       CALL get_qs_env(qs_env, dft_control=dft_control, matrix_s=matrix_s)
    1237              :       ! in RTP moloc_coeff holds the real and imaginary parts of each spin
    1238            0 :       icoeff = loc_coeff_index(SIZE(moloc_coeff), dft_control%nspins, ispin)
    1239              : 
    1240            0 :       nm = ncoset(norder) - 1
    1241              : 
    1242              :       !CALL get_qs_env(qs_env, qs_kind_set=qs_kind_set, cell=cell)
    1243              :       !particle_set => qs_loc_env%particle_set
    1244            0 :       para_env => qs_loc_env%para_env
    1245              : 
    1246            0 :       CALL cp_fm_get_info(moloc_coeff(icoeff), ncol_global=nmoloc)
    1247            0 :       ALLOCATE (moment_set(nm, nmoloc))
    1248            0 :       moment_set = 0.0_dp
    1249              : 
    1250            0 :       mo_localized => moloc_coeff(icoeff)
    1251              : 
    1252            0 :       DO istate = 1, nmoloc
    1253            0 :          rcc = centers(1:3, istate)
    1254              :          !rcc = 0.0_dp
    1255              : 
    1256            0 :          ALLOCATE (moments(nm))
    1257            0 :          DO imoment = 1, nm
    1258            0 :             ALLOCATE (moments(imoment)%matrix)
    1259            0 :             CALL dbcsr_copy(moments(imoment)%matrix, matrix_s(ispin)%matrix, 'MOM MAT')
    1260            0 :             CALL dbcsr_set(moments(imoment)%matrix, 0.0_dp)
    1261              :          END DO
    1262              :          !
    1263              : 
    1264            0 :          CALL build_local_moment_matrix(qs_env, moments, norder, rcc)
    1265              : 
    1266            0 :          CALL cp_fm_get_info(mo_localized, ncol_global=ncol_global, nrow_global=nrow_global)
    1267              :          CALL cp_fm_struct_create(fm_struct, nrow_global=nrow_global, ncol_global=1, &
    1268              :                                   para_env=mo_localized%matrix_struct%para_env, &
    1269            0 :                                   context=mo_localized%matrix_struct%context)
    1270            0 :          CALL cp_fm_create(mvector, fm_struct, name="mvector")
    1271            0 :          CALL cp_fm_create(omvector, fm_struct, name="omvector")
    1272            0 :          CALL cp_fm_create(momv, fm_struct, name="omvector")
    1273            0 :          CALL cp_fm_struct_release(fm_struct)
    1274              : 
    1275              :          !cp_fm_to_fm_columns(msource, mtarget, ncol, source_start,target_start)
    1276            0 :          CALL cp_fm_to_fm(mo_localized, mvector, 1, istate, 1)
    1277              : 
    1278            0 :          DO imoment = 1, nm
    1279            0 :             CALL cp_dbcsr_sm_fm_multiply(moments(imoment)%matrix, mvector, omvector, 1)
    1280            0 :             CALL cp_fm_schur_product(mvector, omvector, momv)
    1281              :             !moment_set(imoment, istate) = moment_set(imoment, istate) + SUM(momv%local_data)
    1282            0 :             moment_set(imoment, istate) = SUM(momv%local_data)
    1283              :          END DO
    1284              :          !
    1285            0 :          CALL cp_fm_release(mvector)
    1286            0 :          CALL cp_fm_release(omvector)
    1287            0 :          CALL cp_fm_release(momv)
    1288              : 
    1289            0 :          DO imoment = 1, nm
    1290            0 :             CALL dbcsr_deallocate_matrix(moments(imoment)%matrix)
    1291              :          END DO
    1292            0 :          DEALLOCATE (moments)
    1293              :       END DO
    1294              : 
    1295            0 :       CALL para_env%sum(moment_set)
    1296              : 
    1297            0 :       IF (output_unit_sm > 0) THEN
    1298            0 :          WRITE (UNIT=output_unit_sm, FMT='(A,T13,A,I1)') "#", &
    1299            0 :             "SECOND MOMENTS OF WANNIER CENTERS FOR SPIN ", ispin
    1300            0 :          WRITE (UNIT=output_unit_sm, FMT='(A,T29,A2,5(14x,A2))') "#", "XX", "XY", "XZ", "YY", "YZ", "ZZ"
    1301            0 :          DO istate = 1, nmoloc
    1302              :             cov = 0.0_dp
    1303            0 :             cov(1) = moment_set(4, istate) - moment_set(1, istate)*moment_set(1, istate)
    1304            0 :             cov(2) = moment_set(5, istate) - moment_set(1, istate)*moment_set(2, istate)
    1305            0 :             cov(3) = moment_set(6, istate) - moment_set(1, istate)*moment_set(3, istate)
    1306            0 :             cov(4) = moment_set(7, istate) - moment_set(2, istate)*moment_set(2, istate)
    1307            0 :             cov(5) = moment_set(8, istate) - moment_set(2, istate)*moment_set(3, istate)
    1308            0 :             cov(6) = moment_set(9, istate) - moment_set(3, istate)*moment_set(3, istate)
    1309            0 :             WRITE (UNIT=output_unit_sm, FMT='(T4,A,I6,6(T20,6F16.8))') "Center:", istate, cov(1:6)
    1310            0 :             IF (debug_this_subroutine) THEN
    1311              :                WRITE (UNIT=output_unit_sm, FMT='(T4,A,I5,9(T20,9F12.6))') "Center:", istate, moment_set(1:, istate)
    1312              :             END IF
    1313              :          END DO
    1314              :       END IF
    1315            0 :       CALL cp_print_key_finished_output(output_unit_sm, logger, print_loc_section, "WANNIER_SPREADS")
    1316            0 :       CALL cp_print_key_finished_output(output_unit, logger, print_loc_section, "PROGRAM_RUN_INFO")
    1317              : 
    1318            0 :       DEALLOCATE (moment_set)
    1319              : 
    1320            0 :    END SUBROUTINE centers_second_moments_loc
    1321              : 
    1322              : ! **************************************************************************************************
    1323              : !> \brief Compute the second moments of the centers using a periodic quadrupole operator
    1324              : !> \brief c.f. Wheeler et al. PRB 100 245135 2019, and Kang et al. PRB 100 245134 2019
    1325              : !> \brief The calculation is based on a Taylor expansion of the exp(i k_i r_i r_j k_j) operator to
    1326              : !> \brief to first order in the cosine and the sine parts
    1327              : !> \param qs_env ...
    1328              : !> \param qs_loc_env ...
    1329              : !> \param print_loc_section ...
    1330              : !> \param ispin ...
    1331              : !> \par History
    1332              : !>      08.2020 created [H. Elgabarty]
    1333              : !> \author Hossam Elgabarty
    1334              : ! **************************************************************************************************
    1335            0 :    SUBROUTINE centers_second_moments_berry(qs_env, qs_loc_env, print_loc_section, ispin)
    1336              : 
    1337              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1338              :       TYPE(qs_loc_env_type), POINTER                     :: qs_loc_env
    1339              :       TYPE(section_vals_type), POINTER                   :: print_loc_section
    1340              :       INTEGER, INTENT(IN)                                :: ispin
    1341              : 
    1342              :       INTEGER, PARAMETER                                 :: taylor_order = 1
    1343              :       LOGICAL, PARAMETER :: debug_this_subroutine = .FALSE.
    1344              : 
    1345              :       CHARACTER(len=default_path_length)                 :: file_tmp
    1346              :       COMPLEX(KIND=dp)                                   :: z
    1347              :       INTEGER                                            :: icoeff, icov, imoment, istate, &
    1348              :                                                             ncol_global, nm, nmoloc, norder, &
    1349              :                                                             nrow_global, output_unit, &
    1350              :                                                             output_unit_sm
    1351            0 :       REAL(dp), DIMENSION(:, :), POINTER                 :: centers
    1352              :       REAL(KIND=dp)                                      :: imagpart, Lx, Ly, Lz, realpart
    1353            0 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: moment_set
    1354              :       REAL(KIND=dp), DIMENSION(3)                        :: rcc
    1355              :       REAL(KIND=dp), DIMENSION(6)                        :: cov
    1356              :       TYPE(cell_type), POINTER                           :: cell
    1357              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct
    1358              :       TYPE(cp_fm_type)                                   :: momv, mvector, omvector
    1359            0 :       TYPE(cp_fm_type), DIMENSION(:), POINTER            :: moloc_coeff
    1360              :       TYPE(cp_fm_type), POINTER                          :: mo_localized
    1361              :       TYPE(cp_logger_type), POINTER                      :: logger
    1362            0 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_s, moments
    1363              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1364              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1365              : 
    1366              : ! two pointers, one to each spin channel's coeff. matrix (nao x nmoloc)
    1367              : 
    1368            0 :       logger => cp_get_default_logger()
    1369              : 
    1370              :       output_unit = cp_print_key_unit_nr(logger, print_loc_section, "PROGRAM_RUN_INFO", &
    1371            0 :                                          extension=".locInfo")
    1372              : 
    1373            0 :       IF (output_unit > 0) THEN
    1374              :          WRITE (output_unit, '(/,T2,A)') &
    1375            0 :             '!-----------------------------------------------------------------------------!'
    1376            0 :          WRITE (output_unit, '(T12,A)') "Computing second moments of Wannier functions..."
    1377              :          WRITE (output_unit, '(T2,A)') &
    1378            0 :             '!-----------------------------------------------------------------------------!'
    1379              :       END IF
    1380              : 
    1381            0 :       file_tmp = "_r_berry_cov_matrix_"//TRIM(ADJUSTL(cp_to_string(ispin)))
    1382              :       output_unit_sm = cp_print_key_unit_nr(logger, print_loc_section, "WANNIER_SPREADS", &
    1383            0 :                                             middle_name=file_tmp, extension=".data")
    1384              : 
    1385            0 :       norder = 2*(2*taylor_order + 1)
    1386            0 :       nm = (6 + 11*norder + 6*norder**2 + norder**3)/6 - 1
    1387              : 
    1388            0 :       NULLIFY (cell)
    1389            0 :       CALL get_qs_loc_env(qs_loc_env=qs_loc_env, moloc_coeff=moloc_coeff, cell=cell)
    1390            0 :       Lx = cell%hmat(1, 1)
    1391            0 :       Ly = cell%hmat(2, 2)
    1392            0 :       Lz = cell%hmat(3, 3)
    1393              : 
    1394            0 :       centers => qs_loc_env%localized_wfn_control%centers_set(ispin)%array
    1395              : 
    1396            0 :       para_env => qs_loc_env%para_env
    1397              : 
    1398            0 :       CALL get_qs_env(qs_env, dft_control=dft_control, matrix_s=matrix_s)
    1399              :       ! in RTP moloc_coeff holds the real and imaginary parts of each spin
    1400            0 :       icoeff = loc_coeff_index(SIZE(moloc_coeff), dft_control%nspins, ispin)
    1401              : 
    1402            0 :       CALL cp_fm_get_info(moloc_coeff(icoeff), ncol_global=nmoloc)
    1403              : 
    1404            0 :       ALLOCATE (moment_set(nm, nmoloc))
    1405            0 :       moment_set = 0.0_dp
    1406              : 
    1407            0 :       mo_localized => moloc_coeff(icoeff)
    1408              : 
    1409            0 :       DO istate = 1, nmoloc
    1410            0 :          rcc = centers(1:3, istate)
    1411              :          !rcc = 0.0_dp
    1412              : 
    1413            0 :          ALLOCATE (moments(nm))
    1414            0 :          DO imoment = 1, nm
    1415            0 :             ALLOCATE (moments(imoment)%matrix)
    1416            0 :             CALL dbcsr_copy(moments(imoment)%matrix, matrix_s(ispin)%matrix, 'MOM MAT')
    1417            0 :             CALL dbcsr_set(moments(imoment)%matrix, 0.0_dp)
    1418              :          END DO
    1419              :          !
    1420              : 
    1421            0 :          CALL build_local_moment_matrix(qs_env, moments, norder, rcc)
    1422              : 
    1423            0 :          CALL cp_fm_get_info(mo_localized, ncol_global=ncol_global, nrow_global=nrow_global)
    1424              :          CALL cp_fm_struct_create(fm_struct, nrow_global=nrow_global, ncol_global=1, &
    1425              :                                   para_env=mo_localized%matrix_struct%para_env, &
    1426            0 :                                   context=mo_localized%matrix_struct%context)
    1427            0 :          CALL cp_fm_create(mvector, fm_struct, name="mvector")
    1428            0 :          CALL cp_fm_create(omvector, fm_struct, name="omvector")
    1429            0 :          CALL cp_fm_create(momv, fm_struct, name="omvector")
    1430            0 :          CALL cp_fm_struct_release(fm_struct)
    1431              : 
    1432              :          ! cp_fm_to_fm_columns(msource, mtarget, ncol, source_start,target_start)
    1433            0 :          CALL cp_fm_to_fm(mo_localized, mvector, 1, istate, 1)
    1434              : 
    1435            0 :          DO imoment = 1, nm
    1436            0 :             CALL cp_dbcsr_sm_fm_multiply(moments(imoment)%matrix, mvector, omvector, 1)
    1437            0 :             CALL cp_fm_schur_product(mvector, omvector, momv)
    1438            0 :             moment_set(imoment, istate) = SUM(momv%local_data)
    1439              :          END DO
    1440              :          !
    1441            0 :          CALL cp_fm_release(mvector)
    1442            0 :          CALL cp_fm_release(omvector)
    1443            0 :          CALL cp_fm_release(momv)
    1444              : 
    1445            0 :          DO imoment = 1, nm
    1446            0 :             CALL dbcsr_deallocate_matrix(moments(imoment)%matrix)
    1447              :          END DO
    1448            0 :          DEALLOCATE (moments)
    1449              :       END DO
    1450              : 
    1451            0 :       CALL para_env%sum(moment_set)
    1452              : 
    1453            0 :       IF (output_unit_sm > 0) THEN
    1454            0 :          WRITE (UNIT=output_unit_sm, FMT='(A,T13,A,I1)') "#", &
    1455            0 :             "SECOND MOMENTS OF WANNIER CENTERS FOR SPIN ", ispin
    1456            0 :          WRITE (UNIT=output_unit_sm, FMT='(A,T29,A2,5(14x,A2))') "#", "XX", "XY", "XZ", "YY", "YZ", "ZZ"
    1457            0 :          DO istate = 1, nmoloc
    1458            0 :             cov = 0.0_dp
    1459            0 :             DO icov = 1, 6
    1460            0 :                realpart = 0.0_dp
    1461            0 :                imagpart = 0.0_dp
    1462            0 :                z = CMPLX(realpart, imagpart, dp)
    1463            0 :                SELECT CASE (icov)
    1464              : 
    1465              :                  !! XX
    1466              :                CASE (1)
    1467              :                   realpart = 1.0_dp - 0.5_dp*twopi*twopi/Lx/Lx/Lx/Lx &
    1468            0 :                      *moment_set(20, istate)
    1469              :                   imagpart = twopi/Lx/Lx*moment_set(4, istate) &
    1470            0 :                              - twopi*twopi*twopi/Lx/Lx/Lx/Lx/Lx/Lx/6.0_dp*moment_set(56, istate)
    1471              : 
    1472              :                   ! third order
    1473              :                   !realpart = realpart + twopi*twopi*twopi*twopi/Lx/Lx/Lx/Lx/Lx/Lx/Lx/Lx/24.0_dp * moment_set(120, istate)
    1474              :                   !imagpart = imagpart + twopi*twopi*twopi*twopi*twopi/Lx/Lx/Lx/Lx/Lx/Lx/Lx/Lx/Lx/Lx/120 &
    1475              :                   !     * moment_set(220, istate)
    1476              : 
    1477            0 :                   z = CMPLX(realpart, imagpart, dp)
    1478            0 :                   cov(1) = Lx*Lx/twopi*AIMAG(LOG(z)) - Lx*Lx/twopi/twopi*moment_set(1, istate)*moment_set(1, istate)
    1479              : 
    1480              :                  !! XY
    1481              :                CASE (2)
    1482              :                   realpart = 1.0_dp - 0.5_dp*twopi*twopi/Lx/Ly/Lx/Ly &
    1483            0 :                      *moment_set(23, istate)
    1484              :                   imagpart = twopi/Lx/Ly*moment_set(5, istate) &
    1485            0 :                              - twopi*twopi*twopi/Lx/Lx/Lx/Ly/Ly/Ly/6.0_dp*moment_set(62, istate)
    1486              : 
    1487              :                   ! third order
    1488              :                   !realpart = realpart + twopi*twopi*twopi*twopi/Lx/Lx/Lx/Lx/Ly/Ly/Ly/Ly/24.0_dp * moment_set(130, istate)
    1489              :                   !imagpart = imagpart + twopi*twopi*twopi*twopi*twopi/Lx/Lx/Lx/Lx/Lx/Ly/Ly/Ly/Ly/Ly/120 &
    1490              :                   !     * moment_set(235, istate)
    1491              : 
    1492            0 :                   z = CMPLX(realpart, imagpart, dp)
    1493            0 :                   cov(2) = Lx*Ly/twopi*AIMAG(LOG(z)) - Lx*Ly/twopi/twopi*moment_set(1, istate)*moment_set(2, istate)
    1494              : 
    1495              :                  !! XZ
    1496              :                CASE (3)
    1497              :                   realpart = 1.0_dp - 0.5_dp*twopi*twopi/Lx/Lz/Lx/Lz &
    1498            0 :                      *moment_set(25, istate)
    1499              :                   imagpart = twopi/Lx/Lz*moment_set(6, istate) &
    1500            0 :                              - twopi*twopi*twopi/Lx/Lx/Lx/Lz/Lz/Lz/6.0_dp*moment_set(65, istate)
    1501              : 
    1502              :                   ! third order
    1503              :                   !realpart = realpart + twopi*twopi*twopi*twopi/Lx/Lx/Lx/Lx/Lz/Lz/Lz/Lz/24.0_dp * moment_set(134, istate)
    1504              :                   !imagpart = imagpart + twopi*twopi*twopi*twopi*twopi/Lx/Lx/Lx/Lx/Lx/Ly/Ly/Ly/Ly/Ly/120 &
    1505              :                   !     * moment_set(240, istate)
    1506              : 
    1507            0 :                   z = CMPLX(realpart, imagpart, dp)
    1508            0 :                   cov(3) = Lx*Lz/twopi*AIMAG(LOG(z)) - Lx*Lz/twopi/twopi*moment_set(1, istate)*moment_set(3, istate)
    1509              : 
    1510              :                  !! YY
    1511              :                CASE (4)
    1512              :                   realpart = 1.0_dp - 0.5_dp*twopi*twopi/Ly/Ly/Ly/Ly &
    1513            0 :                      *moment_set(30, istate)
    1514              :                   imagpart = twopi/Ly/Ly*moment_set(7, istate) &
    1515            0 :                              - twopi*twopi*twopi/Ly/Ly/Ly/Ly/Ly/Ly/6.0_dp*moment_set(77, istate)
    1516              : 
    1517              :                   ! third order
    1518              :                   !realpart = realpart + twopi*twopi*twopi*twopi/Ly/Ly/Ly/Ly/Ly/Ly/Ly/Ly/24.0_dp * moment_set(156, istate)
    1519              :                   !imagpart = imagpart + twopi*twopi*twopi*twopi*twopi/Ly/Ly/Ly/Ly/Ly/Ly/Ly/Ly/Ly/Ly/120 &
    1520              :                   !     * moment_set(275, istate)
    1521              : 
    1522            0 :                   z = CMPLX(realpart, imagpart, dp)
    1523            0 :                   cov(4) = Ly*Ly/twopi*AIMAG(LOG(z)) - Ly*Ly/twopi/twopi*moment_set(2, istate)*moment_set(2, istate)
    1524              : 
    1525              :                  !! YZ
    1526              :                CASE (5)
    1527              :                   realpart = 1.0_dp - 0.5_dp*twopi*twopi/Ly/Lz/Ly/Lz &
    1528            0 :                      *moment_set(32, istate)
    1529              :                   imagpart = twopi/Ly/Lz*moment_set(8, istate) &
    1530            0 :                              - twopi*twopi*twopi/Ly/Ly/Ly/Lz/Lz/Lz/6.0_dp*moment_set(80, istate)
    1531              : 
    1532              :                   ! third order
    1533              :                   !realpart = realpart + twopi*twopi*twopi*twopi/Lz/Lz/Lz/Lz/Ly/Ly/Ly/Ly/24.0_dp * moment_set(160, istate)
    1534              :                   !imagpart = imagpart + twopi*twopi*twopi*twopi*twopi/Lz/Lz/Lz/Lz/Lz/Ly/Ly/Ly/Ly/Ly/120 &
    1535              :                   !     * moment_set(280, istate)
    1536              : 
    1537            0 :                   z = CMPLX(realpart, imagpart, dp)
    1538            0 :                   cov(5) = Ly*Lz/twopi*AIMAG(LOG(z)) - Ly*Lz/twopi/twopi*moment_set(2, istate)*moment_set(3, istate)
    1539              : 
    1540              :                  !! ZZ
    1541              :                CASE (6)
    1542              :                   realpart = 1.0_dp - 0.5_dp*twopi*twopi/Lz/Lz/Lz/Lz &
    1543            0 :                      *moment_set(34, istate)
    1544              :                   imagpart = twopi/Lz/Lz*moment_set(9, istate) &
    1545            0 :                              - twopi*twopi*twopi/Lz/Lz/Lz/Lz/Lz/Lz/6.0_dp*moment_set(83, istate)
    1546              : 
    1547              :                   ! third order
    1548              :                   !realpart = realpart + twopi*twopi*twopi*twopi/Lz/Lz/Lz/Lz/Lz/Lz/Lz/Lz/24.0_dp * moment_set(164, istate)
    1549              :                   !imagpart = imagpart + twopi*twopi*twopi*twopi*twopi/Lz/Lz/Lz/Lz/Lz/Lz/Lz/Lz/Lz/Lz/120 &
    1550              :                   !     * moment_set(285, istate)
    1551              : 
    1552            0 :                   z = CMPLX(realpart, imagpart, dp)
    1553            0 :                   cov(6) = Lz*Lz/twopi*AIMAG(LOG(z)) - Lz*Lz/twopi/twopi*moment_set(3, istate)*moment_set(3, istate)
    1554              : 
    1555              :                END SELECT
    1556              :             END DO
    1557            0 :             WRITE (UNIT=output_unit_sm, FMT='(T4,A,I6,6(T20,6F16.8))') "Center:", istate, cov(1:6)
    1558            0 :             IF (debug_this_subroutine) THEN
    1559              :                WRITE (UNIT=output_unit_sm, FMT='(T4,A,I5,9(T20,9F12.6))') "Center:", istate, moment_set(1:, istate)
    1560              :             END IF
    1561              :          END DO
    1562              :       END IF
    1563            0 :       CALL cp_print_key_finished_output(output_unit_sm, logger, print_loc_section, "WANNIER_SPREADS")
    1564            0 :       CALL cp_print_key_finished_output(output_unit, logger, print_loc_section, "PROGRAM_RUN_INFO")
    1565              : 
    1566            0 :       DEALLOCATE (moment_set)
    1567              : 
    1568            0 :    END SUBROUTINE centers_second_moments_berry
    1569              : 
    1570              : END MODULE qs_loc_methods
        

Generated by: LCOV version 2.0-1