1!--------------------------------------------------------------------------------------------------!
2!   CP2K: A general program to perform molecular dynamics simulations                              !
3!   Copyright (C) 2000 - 2019  CP2K developers group                                               !
4!--------------------------------------------------------------------------------------------------!
5
6! **************************************************************************************************
7!> \brief Driver for the localization that should be general
8!>      for all the methods available and all the definition of the
9!>      spread functional
10!>      Write centers, spread and cubes only if required and for the
11!>      selected states
12!>      The localized functions are copied in the standard mos array
13!>      for the next use
14!> \par History
15!>      01.2008 Teodoro Laino [tlaino] - University of Zurich
16!>        - Merging the two localization codes and updating to new structures
17!> \author MI (04.2005)
18! **************************************************************************************************
19MODULE qs_loc_methods
20   USE atomic_kind_types,               ONLY: atomic_kind_type,&
21                                              deallocate_atomic_kind_set,&
22                                              set_atomic_kind
23   USE cell_types,                      ONLY: cell_type,&
24                                              pbc
25   USE cp_control_types,                ONLY: dft_control_type
26   USE cp_dbcsr_operations,             ONLY: copy_dbcsr_to_fm
27   USE cp_fm_pool_types,                ONLY: cp_fm_pool_p_type,&
28                                              fm_pool_create_fm
29   USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
30                                              cp_fm_struct_release,&
31                                              cp_fm_struct_type
32   USE cp_fm_types,                     ONLY: &
33        cp_fm_create, cp_fm_get_element, cp_fm_get_info, cp_fm_get_submatrix, cp_fm_p_type, &
34        cp_fm_release, cp_fm_set_all, cp_fm_set_submatrix, cp_fm_to_fm, cp_fm_type
35   USE cp_gemm_interface,               ONLY: cp_gemm
36   USE cp_log_handling,                 ONLY: cp_get_default_logger,&
37                                              cp_logger_type,&
38                                              cp_to_string
39   USE cp_output_handling,              ONLY: cp_iter_string,&
40                                              cp_p_file,&
41                                              cp_print_key_finished_output,&
42                                              cp_print_key_should_output,&
43                                              cp_print_key_unit_nr
44   USE cp_para_types,                   ONLY: cp_para_env_type
45   USE cp_realspace_grid_cube,          ONLY: cp_pw_to_cube
46   USE cp_units,                        ONLY: cp_unit_from_cp2k
47   USE dbcsr_api,                       ONLY: dbcsr_p_type
48   USE input_constants,                 ONLY: &
49        do_loc_crazy, do_loc_direct, do_loc_jacobi, do_loc_l1_norm_sd, do_loc_none, do_loc_scdm, &
50        dump_dcd, dump_dcd_aligned_cell, dump_xmol, op_loc_berry, op_loc_boys, op_loc_pipek, &
51        state_loc_list
52   USE input_section_types,             ONLY: section_get_ival,&
53                                              section_get_ivals,&
54                                              section_get_lval,&
55                                              section_vals_get_subs_vals,&
56                                              section_vals_type,&
57                                              section_vals_val_get
58   USE kinds,                           ONLY: default_path_length,&
59                                              default_string_length,&
60                                              dp,&
61                                              sp
62   USE machine,                         ONLY: m_flush
63   USE mathconstants,                   ONLY: twopi
64   USE memory_utilities,                ONLY: reallocate
65   USE motion_utils,                    ONLY: get_output_format
66   USE particle_list_types,             ONLY: particle_list_type
67   USE particle_methods,                ONLY: get_particle_set,&
68                                              write_particle_coordinates
69   USE particle_types,                  ONLY: allocate_particle_set,&
70                                              deallocate_particle_set,&
71                                              particle_type
72   USE physcon,                         ONLY: angstrom
73   USE pw_env_types,                    ONLY: pw_env_get,&
74                                              pw_env_type
75   USE pw_pool_types,                   ONLY: pw_pool_create_pw,&
76                                              pw_pool_give_back_pw,&
77                                              pw_pool_type
78   USE pw_types,                        ONLY: COMPLEXDATA1D,&
79                                              REALDATA3D,&
80                                              REALSPACE,&
81                                              RECIPROCALSPACE,&
82                                              pw_p_type
83   USE qs_collocate_density,            ONLY: calculate_wavefunction
84   USE qs_environment_types,            ONLY: get_qs_env,&
85                                              qs_environment_type
86   USE qs_kind_types,                   ONLY: qs_kind_type
87   USE qs_loc_types,                    ONLY: get_qs_loc_env,&
88                                              localized_wfn_control_type,&
89                                              qs_loc_env_new_type
90   USE qs_loc_utils,                    ONLY: jacobi_rotation_pipek
91   USE qs_localization_methods,         ONLY: approx_l1_norm_sd,&
92                                              crazy_rotations,&
93                                              direct_mini,&
94                                              jacobi_rotations,&
95                                              scdm_qrfact,&
96                                              zij_matrix
97   USE qs_matrix_pools,                 ONLY: mpools_get
98   USE qs_mo_types,                     ONLY: get_mo_set,&
99                                              mo_set_p_type
100   USE qs_subsys_types,                 ONLY: qs_subsys_get,&
101                                              qs_subsys_type
102   USE string_utilities,                ONLY: xstring
103#include "./base/base_uses.f90"
104
105   IMPLICIT NONE
106
107   PRIVATE
108
109! *** Global parameters ***
110
111   CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_loc_methods'
112
113! *** Public ***
114   PUBLIC ::  qs_loc_driver, qs_print_cubes, centers_spreads_berry
115
116CONTAINS
117
118! **************************************************************************************************
119!> \brief Calculate and optimize the spread functional as calculated from
120!>       the selected mos  and by the definition using the berry phase
121!>       as given by silvestrelli et al
122!>       If required the centers and the spreads for each mos selected
123!>       are calculated from z_ij and printed to file.
124!>       The centers files is appended if already exists
125!> \param method indicates localization algorithm
126!> \param qs_loc_env new environment for the localization calculations
127!> \param vectors selected mos to be localized
128!> \param op_sm_set sparse matrices containing the integrals of the kind <mi e{iGr} nu>
129!> \param zij_fm_set set of full matrix of size nmoloc x nmoloc, will contain the z_ij numbers
130!>                    as defined by Silvestrelli et al
131!> \param para_env ...
132!> \param cell ...
133!> \param weights ...
134!> \param ispin ...
135!> \param print_loc_section ...
136!> \par History
137!>      04.2005 created [MI]
138!> \author MI
139!> \note
140!>       This definition need the use of complex numbers, therefore the
141!>       optimization routines are specific for this case
142!>       The file for the centers and the spreads have a xyz format
143! **************************************************************************************************
144   SUBROUTINE optimize_loc_berry(method, qs_loc_env, vectors, op_sm_set, &
145                                 zij_fm_set, para_env, cell, weights, ispin, print_loc_section)
146
147      INTEGER, INTENT(IN)                                :: method
148      TYPE(qs_loc_env_new_type), POINTER                 :: qs_loc_env
149      TYPE(cp_fm_type), POINTER                          :: vectors
150      TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: op_sm_set
151      TYPE(cp_fm_p_type), DIMENSION(:, :), POINTER       :: zij_fm_set
152      TYPE(cp_para_env_type), POINTER                    :: para_env
153      TYPE(cell_type), POINTER                           :: cell
154      REAL(dp), DIMENSION(:)                             :: weights
155      INTEGER, INTENT(IN)                                :: ispin
156      TYPE(section_vals_type), POINTER                   :: print_loc_section
157
158      CHARACTER(len=*), PARAMETER :: routineN = 'optimize_loc_berry', &
159         routineP = moduleN//':'//routineN
160
161      INTEGER                                            :: handle, max_iter, nao, nmoloc, out_each, &
162                                                            output_unit, sweeps
163      LOGICAL                                            :: converged, crazy_use_diag, &
164                                                            do_jacobi_refinement
165      REAL(dp)                                           :: crazy_scale, eps_localization, &
166                                                            max_crazy_angle, start_time, &
167                                                            target_time
168      TYPE(cp_logger_type), POINTER                      :: logger
169
170!      INTEGER :: i,j
171
172      CALL timeset(routineN, handle)
173      logger => cp_get_default_logger()
174      output_unit = cp_print_key_unit_nr(logger, print_loc_section, "PROGRAM_RUN_INFO", &
175                                         extension=".locInfo")
176
177      ! get rows and cols of the input
178      CALL cp_fm_get_info(vectors, nrow_global=nao, ncol_global=nmoloc)
179
180      CALL zij_matrix(vectors, op_sm_set, zij_fm_set)
181
182      max_iter = qs_loc_env%localized_wfn_control%max_iter
183      max_crazy_angle = qs_loc_env%localized_wfn_control%max_crazy_angle
184      crazy_use_diag = qs_loc_env%localized_wfn_control%crazy_use_diag
185      crazy_scale = qs_loc_env%localized_wfn_control%crazy_scale
186      eps_localization = qs_loc_env%localized_wfn_control%eps_localization
187      out_each = qs_loc_env%localized_wfn_control%out_each
188      target_time = qs_loc_env%target_time
189      start_time = qs_loc_env%start_time
190      do_jacobi_refinement = qs_loc_env%localized_wfn_control%jacobi_refinement
191      CALL centers_spreads_berry(qs_loc_env, zij_fm_set, nmoloc, cell, weights, &
192                                 ispin, print_loc_section, only_initial_out=.TRUE.)
193      SELECT CASE (method)
194      CASE (do_loc_jacobi)
195         CALL jacobi_rotations(weights, zij_fm_set, vectors, para_env, max_iter=max_iter, &
196                               eps_localization=eps_localization, sweeps=sweeps, &
197                               out_each=out_each, target_time=target_time, start_time=start_time)
198      CASE (do_loc_scdm)
199         ! Decomposition
200         CALL scdm_qrfact(vectors)
201         ! Calculation of Zij
202         CALL zij_matrix(vectors, op_sm_set, zij_fm_set)
203         IF (do_jacobi_refinement) THEN
204            ! Intermediate spread and centers
205            CALL centers_spreads_berry(qs_loc_env, zij_fm_set, nmoloc, cell, weights, &
206                                       ispin, print_loc_section, only_initial_out=.TRUE.)
207            CALL jacobi_rotations(weights, zij_fm_set, vectors, para_env, max_iter=max_iter, &
208                                  eps_localization=eps_localization, sweeps=sweeps, &
209                                  out_each=out_each, target_time=target_time, start_time=start_time)
210         END IF
211      CASE (do_loc_crazy)
212         CALL crazy_rotations(weights, zij_fm_set, vectors, max_iter=max_iter, max_crazy_angle=max_crazy_angle, &
213                              crazy_scale=crazy_scale, crazy_use_diag=crazy_use_diag, &
214                              eps_localization=eps_localization, iterations=sweeps, converged=converged)
215         ! Possibly fallback to jacobi if the crazy rotation fails
216         IF (.NOT. converged) THEN
217            IF (qs_loc_env%localized_wfn_control%jacobi_fallback) THEN
218               IF (output_unit > 0) WRITE (output_unit, "(T4,A,I6,/,T4,A)") &
219                  " Crazy Wannier localization not converged after ", sweeps, &
220                  " iterations, switching to jacobi rotations"
221               CALL jacobi_rotations(weights, zij_fm_set, vectors, para_env, max_iter=max_iter, &
222                                     eps_localization=eps_localization, sweeps=sweeps, &
223                                     out_each=out_each, target_time=target_time, start_time=start_time)
224            ELSE
225               IF (output_unit > 0) WRITE (output_unit, "(T4,A,I6,/,T4,A)") &
226                  " Crazy Wannier localization not converged after ", sweeps, &
227                  " iterations, and jacobi_fallback switched off "
228            ENDIF
229         END IF
230      CASE (do_loc_direct)
231         CALL direct_mini(weights, zij_fm_set, vectors, max_iter=max_iter, &
232                          eps_localization=eps_localization, iterations=sweeps)
233      CASE (do_loc_l1_norm_sd)
234         IF (.NOT. cell%orthorhombic) THEN
235            CPABORT("Non-orthorhombic cell with the selected method NYI")
236         ELSE
237            CALL approx_l1_norm_sd(vectors, max_iter, eps_localization, converged, sweeps)
238            ! here we need to set zij for the computation of the centers and spreads
239            CALL zij_matrix(vectors, op_sm_set, zij_fm_set)
240         END IF
241      CASE (do_loc_none)
242         IF (output_unit > 0) THEN
243            WRITE (output_unit, '(T4,A,I6,A)') " No MOS localization applied "
244         ENDIF
245      CASE DEFAULT
246         CPABORT("Unknown localization method")
247      END SELECT
248      IF (output_unit > 0) THEN
249         IF (sweeps <= max_iter) WRITE (output_unit, '(T4,A,I3,A,I6,A)') " Localization  for spin ", ispin, &
250            " converged in ", sweeps, " iterations"
251      ENDIF
252
253      CALL centers_spreads_berry(qs_loc_env, zij_fm_set, nmoloc, cell, weights, &
254                                 ispin, print_loc_section)
255      CALL cp_print_key_finished_output(output_unit, logger, print_loc_section, "PROGRAM_RUN_INFO")
256
257      CALL timestop(handle)
258
259   END SUBROUTINE optimize_loc_berry
260
261! **************************************************************************************************
262!> \brief ...
263!> \param qs_env ...
264!> \param method ...
265!> \param qs_loc_env ...
266!> \param vectors ...
267!> \param zij_fm_set ...
268!> \param ispin ...
269!> \param print_loc_section ...
270!> \par History
271!>      04.2005 created [MI]
272!> \author MI
273! **************************************************************************************************
274   SUBROUTINE optimize_loc_pipek(qs_env, method, qs_loc_env, vectors, zij_fm_set, &
275                                 ispin, print_loc_section)
276      TYPE(qs_environment_type), POINTER                 :: qs_env
277      INTEGER, INTENT(IN)                                :: method
278      TYPE(qs_loc_env_new_type), POINTER                 :: qs_loc_env
279      TYPE(cp_fm_type), POINTER                          :: vectors
280      TYPE(cp_fm_p_type), DIMENSION(:, :), POINTER       :: zij_fm_set
281      INTEGER, INTENT(IN)                                :: ispin
282      TYPE(section_vals_type), POINTER                   :: print_loc_section
283
284      CHARACTER(len=*), PARAMETER :: routineN = 'optimize_loc_pipek', &
285         routineP = moduleN//':'//routineN
286
287      INTEGER                                            :: handle, iatom, isgf, ldz, nao, natom, &
288                                                            ncol, nmoloc, output_unit, sweeps
289      INTEGER, ALLOCATABLE, DIMENSION(:)                 :: first_sgf, last_sgf, nsgf
290      TYPE(cp_fm_pool_p_type), DIMENSION(:), POINTER     :: ao_ao_fm_pools
291      TYPE(cp_fm_type), POINTER                          :: opvec, ov_fm
292      TYPE(cp_logger_type), POINTER                      :: logger
293      TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matrix_s
294      TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
295      TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
296
297      CALL timeset(routineN, handle)
298      logger => cp_get_default_logger()
299      output_unit = cp_print_key_unit_nr(logger, print_loc_section, "PROGRAM_RUN_INFO", &
300                                         extension=".locInfo")
301
302      NULLIFY (particle_set)
303      ! get rows and cols of the input
304      CALL cp_fm_get_info(vectors, nrow_global=nao, ncol_global=nmoloc)
305      ! replicate the input kind of matrix
306      CALL cp_fm_create(opvec, vectors%matrix_struct)
307      CALL cp_fm_set_all(opvec, 0.0_dp)
308
309      CALL get_qs_env(qs_env=qs_env, matrix_s=matrix_s, &
310                      particle_set=particle_set, qs_kind_set=qs_kind_set)
311
312      natom = SIZE(particle_set, 1)
313      ALLOCATE (first_sgf(natom))
314      ALLOCATE (last_sgf(natom))
315      ALLOCATE (nsgf(natom))
316
317      !   construction of
318      CALL get_particle_set(particle_set, qs_kind_set, &
319                            first_sgf=first_sgf, last_sgf=last_sgf, nsgf=nsgf)
320
321      !   Copy the overlap sparse matrix in a full matrix
322      CALL mpools_get(qs_env%mpools, ao_ao_fm_pools=ao_ao_fm_pools)
323      CALL fm_pool_create_fm(ao_ao_fm_pools(1)%pool, ov_fm, name=" ")
324      CALL copy_dbcsr_to_fm(matrix_s(1)%matrix, ov_fm)
325
326      !   Compute zij here
327      DO iatom = 1, natom
328         CALL cp_fm_set_all(zij_fm_set(iatom, 1)%matrix, 0.0_dp)
329         CALL cp_fm_get_info(zij_fm_set(iatom, 1)%matrix, ncol_global=ldz)
330         isgf = first_sgf(iatom)
331         ncol = nsgf(iatom)
332         ! multiply fmxfm, using only part of the ao : Ct x S
333         CALL cp_gemm('N', 'N', nao, nmoloc, nao, 1.0_dp, ov_fm, vectors, 0.0_dp, opvec, &
334                      a_first_col=1, a_first_row=1, b_first_col=1, b_first_row=1)
335         CALL cp_gemm('T', 'N', nmoloc, nmoloc, ncol, 0.5_dp, vectors, opvec, &
336                      0.0_dp, zij_fm_set(iatom, 1)%matrix, &
337                      a_first_col=1, a_first_row=isgf, b_first_col=1, b_first_row=isgf)
338
339         CALL cp_gemm('N', 'N', nao, nmoloc, ncol, 1.0_dp, ov_fm, vectors, 0.0_dp, opvec, &
340                      a_first_col=1, a_first_row=isgf, b_first_col=1, b_first_row=isgf)
341         CALL cp_gemm('T', 'N', nmoloc, nmoloc, nao, 0.5_dp, vectors, opvec, &
342                      1.0_dp, zij_fm_set(iatom, 1)%matrix, &
343                      a_first_col=1, a_first_row=1, b_first_col=1, b_first_row=1)
344      END DO ! iatom
345
346      !   And now perform the optimization and rotate the orbitals
347      SELECT CASE (method)
348      CASE (do_loc_jacobi)
349         CALL jacobi_rotation_pipek(zij_fm_set, vectors, sweeps)
350      CASE (do_loc_crazy)
351         CPABORT("Crazy and Pipek not implemented.")
352      CASE (do_loc_l1_norm_sd)
353         CPABORT("L1 norm and Pipek not implemented.")
354      CASE (do_loc_direct)
355         CPABORT("Direct and Pipek not implemented.")
356      CASE (do_loc_none)
357         IF (output_unit > 0) WRITE (output_unit, '(A,I6,A)') " No MOS localization applied "
358      CASE DEFAULT
359         CPABORT("Unknown localization method")
360      END SELECT
361
362      IF (output_unit > 0) WRITE (output_unit, '(A,I3,A,I6,A)') " Localization  for spin ", ispin, &
363         " converged in ", sweeps, " iterations"
364
365      CALL centers_spreads_pipek(qs_loc_env, zij_fm_set, particle_set, ispin, &
366                                 print_loc_section)
367
368      DEALLOCATE (first_sgf, last_sgf, nsgf)
369
370      CALL cp_fm_release(opvec)
371      CALL cp_print_key_finished_output(output_unit, logger, print_loc_section, "PROGRAM_RUN_INFO")
372
373      CALL timestop(handle)
374
375   END SUBROUTINE optimize_loc_pipek
376
377! **************************************************************************************************
378!> \brief ...
379!> \param qs_loc_env ...
380!> \param zij ...
381!> \param nmoloc ...
382!> \param cell ...
383!> \param weights ...
384!> \param ispin ...
385!> \param print_loc_section ...
386!> \param only_initial_out ...
387!> \par History
388!>      04.2005 created [MI]
389!> \author MI
390! **************************************************************************************************
391   SUBROUTINE centers_spreads_berry(qs_loc_env, zij, nmoloc, cell, weights, ispin, &
392                                    print_loc_section, only_initial_out)
393      TYPE(qs_loc_env_new_type), POINTER                 :: qs_loc_env
394      TYPE(cp_fm_p_type), INTENT(INOUT)                  :: zij(:, :)
395      INTEGER, INTENT(IN)                                :: nmoloc
396      TYPE(cell_type), POINTER                           :: cell
397      REAL(dp), DIMENSION(:)                             :: weights
398      INTEGER, INTENT(IN)                                :: ispin
399      TYPE(section_vals_type), POINTER                   :: print_loc_section
400      LOGICAL, INTENT(IN), OPTIONAL                      :: only_initial_out
401
402      CHARACTER(len=*), PARAMETER :: routineN = 'centers_spreads_berry', &
403         routineP = moduleN//':'//routineN
404
405      CHARACTER(len=default_path_length)                 :: file_tmp, iter
406      COMPLEX(KIND=dp)                                   :: z
407      INTEGER                                            :: idir, istate, jdir, nstates, &
408                                                            output_unit, unit_out_s
409      LOGICAL                                            :: my_only_init
410      REAL(dp)                                           :: spread_i, spread_ii, sum_spread_i, &
411                                                            sum_spread_ii
412      REAL(dp), DIMENSION(3)                             :: c, c2, cpbc
413      REAL(dp), DIMENSION(:, :), POINTER                 :: centers
414      REAL(KIND=dp)                                      :: imagpart, realpart
415      TYPE(cp_logger_type), POINTER                      :: logger
416      TYPE(section_vals_type), POINTER                   :: print_key
417
418      NULLIFY (centers, logger, print_key)
419      logger => cp_get_default_logger()
420      my_only_init = .FALSE.
421      IF (PRESENT(only_initial_out)) my_only_init = only_initial_out
422
423      file_tmp = TRIM(qs_loc_env%tag_mo)//"_spreads_s"//TRIM(ADJUSTL(cp_to_string(ispin)))
424      output_unit = cp_print_key_unit_nr(logger, print_loc_section, "PROGRAM_RUN_INFO", &
425                                         extension=".locInfo")
426      unit_out_s = cp_print_key_unit_nr(logger, print_loc_section, "WANNIER_SPREADS", &
427                                        middle_name=file_tmp, extension=".data")
428      iter = cp_iter_string(logger%iter_info)
429      IF (unit_out_s > 0 .AND. .NOT. my_only_init) WRITE (unit_out_s, '(i6,/,A)') nmoloc, TRIM(iter)
430
431      CALL cp_fm_get_info(zij(1, 1)%matrix, nrow_global=nstates)
432      CPASSERT(nstates >= nmoloc)
433
434      centers => qs_loc_env%localized_wfn_control%centers_set(ispin)%array
435      CPASSERT(ASSOCIATED(centers))
436      CPASSERT(SIZE(centers, 2) == nmoloc)
437      sum_spread_i = 0.0_dp
438      sum_spread_ii = 0.0_dp
439      DO istate = 1, nmoloc
440         c = 0.0_dp
441         c2 = 0.0_dp
442         spread_i = 0.0_dp
443         spread_ii = 0.0_dp
444         DO jdir = 1, SIZE(zij, 2)
445            CALL cp_fm_get_element(zij(1, jdir)%matrix, istate, istate, realpart)
446            CALL cp_fm_get_element(zij(2, jdir)%matrix, istate, istate, imagpart)
447            z = CMPLX(realpart, imagpart, dp)
448            spread_i = spread_i - weights(jdir)* &
449                       LOG(realpart*realpart + imagpart*imagpart)/twopi/twopi
450            spread_ii = spread_ii + weights(jdir)* &
451                        (1.0_dp - (realpart*realpart + imagpart*imagpart))/twopi/twopi
452            IF (jdir < 4) THEN
453               DO idir = 1, 3
454                  c(idir) = c(idir) + &
455                            (cell%hmat(idir, jdir)/twopi)*AIMAG(LOG(z))
456               ENDDO
457            END IF
458         END DO
459         cpbc = pbc(c, cell)
460         centers(1:3, istate) = cpbc(1:3)
461         centers(4, istate) = spread_i
462         centers(5, istate) = spread_ii
463         sum_spread_i = sum_spread_i + centers(4, istate)
464         sum_spread_ii = sum_spread_ii + centers(5, istate)
465         IF (unit_out_s > 0 .AND. .NOT. my_only_init) WRITE (unit_out_s, '(I6,2F16.8)') istate, centers(4:5, istate)
466      ENDDO
467
468      ! Print of wannier centers
469      print_key => section_vals_get_subs_vals(print_loc_section, "WANNIER_CENTERS")
470      IF (.NOT. my_only_init) CALL print_wannier_centers(qs_loc_env, print_key, centers, logger, ispin)
471
472      IF (output_unit > 0) THEN
473         WRITE (output_unit, '(T4, A, 2x, A26, A26)') " Spread Functional ", "sum_in -w_i ln(|z_in|^2)", &
474            "sum_in w_i(1-|z_in|^2)"
475         IF (my_only_init) THEN
476            WRITE (output_unit, '(T4,A,T38,2F20.10)') " Initial Spread (Berry) : ", sum_spread_i, sum_spread_ii
477         ELSE
478            WRITE (output_unit, '(T4,A,T38,2F20.10)') " Total Spread (Berry) : ", sum_spread_i, sum_spread_ii
479         END IF
480      END IF
481
482      IF (unit_out_s > 0 .AND. .NOT. my_only_init) WRITE (unit_out_s, '(A,2F16.10)') " Total ", sum_spread_i, sum_spread_ii
483
484      CALL cp_print_key_finished_output(unit_out_s, logger, print_loc_section, "WANNIER_SPREADS")
485      CALL cp_print_key_finished_output(output_unit, logger, print_loc_section, "PROGRAM_RUN_INFO")
486
487   END SUBROUTINE centers_spreads_berry
488
489! **************************************************************************************************
490!> \brief define and print the centers and spread
491!>       when the pipek operator is used
492!> \param qs_loc_env ...
493!> \param zij_fm_set matrix elements that define the populations on atoms
494!> \param particle_set ...
495!> \param ispin spin 1 or 2
496!> \param print_loc_section ...
497!> \par History
498!>      05.2005 created [MI]
499! **************************************************************************************************
500   SUBROUTINE centers_spreads_pipek(qs_loc_env, zij_fm_set, particle_set, ispin, &
501                                    print_loc_section)
502
503      TYPE(qs_loc_env_new_type), POINTER                 :: qs_loc_env
504      TYPE(cp_fm_p_type), DIMENSION(:, :), POINTER       :: zij_fm_set
505      TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
506      INTEGER, INTENT(IN)                                :: ispin
507      TYPE(section_vals_type), POINTER                   :: print_loc_section
508
509      CHARACTER(len=*), PARAMETER :: routineN = 'centers_spreads_pipek', &
510         routineP = moduleN//':'//routineN
511
512      CHARACTER(len=default_path_length)                 :: file_tmp, iter
513      INTEGER                                            :: iatom, istate, natom, nstate, unit_out_s
514      INTEGER, DIMENSION(:), POINTER                     :: atom_of_state
515      REAL(dp)                                           :: r(3)
516      REAL(dp), ALLOCATABLE, DIMENSION(:)                :: Qii, ziimax
517      REAL(dp), ALLOCATABLE, DIMENSION(:, :)             :: diag
518      REAL(dp), DIMENSION(:, :), POINTER                 :: centers
519      TYPE(cp_logger_type), POINTER                      :: logger
520      TYPE(section_vals_type), POINTER                   :: print_key
521
522      NULLIFY (centers, logger, print_key)
523      logger => cp_get_default_logger()
524
525      CALL cp_fm_get_info(zij_fm_set(1, 1)%matrix, nrow_global=nstate)
526      natom = SIZE(zij_fm_set, 1)
527
528      centers => qs_loc_env%localized_wfn_control%centers_set(ispin)%array
529      CPASSERT(ASSOCIATED(centers))
530      CPASSERT(SIZE(centers, 2) == nstate)
531
532      file_tmp = TRIM(qs_loc_env%tag_mo)//"_spreads_s"//TRIM(ADJUSTL(cp_to_string(ispin)))
533      unit_out_s = cp_print_key_unit_nr(logger, print_loc_section, "WANNIER_SPREADS", &
534                                        middle_name=file_tmp, extension=".data", log_filename=.FALSE.)
535      iter = cp_iter_string(logger%iter_info)
536      IF (unit_out_s > 0) WRITE (unit_out_s, '(i6,/,A)') TRIM(iter)
537
538      ALLOCATE (atom_of_state(nstate))
539      atom_of_state = 0
540
541      ALLOCATE (diag(nstate, natom))
542      diag = 0.0_dp
543
544      DO iatom = 1, natom
545         DO istate = 1, nstate
546            CALL cp_fm_get_element(zij_fm_set(iatom, 1)%matrix, istate, istate, diag(istate, iatom))
547         END DO
548      END DO
549
550      ALLOCATE (Qii(nstate), ziimax(nstate))
551      ziimax = 0.0_dp
552      Qii = 0.0_dp
553
554      DO iatom = 1, natom
555         DO istate = 1, nstate
556            Qii(istate) = Qii(istate) + diag(istate, iatom)*diag(istate, iatom)
557            IF (ABS(diag(istate, iatom)) > ziimax(istate)) THEN
558               ziimax(istate) = ABS(diag(istate, iatom))
559               atom_of_state(istate) = iatom
560            END IF
561         END DO
562      END DO
563
564      DO istate = 1, nstate
565         iatom = atom_of_state(istate)
566         r(1:3) = particle_set(iatom)%r(1:3)
567         centers(1:3, istate) = r(1:3)
568         centers(4, istate) = 1.0_dp/Qii(istate)
569         IF (unit_out_s > 0) WRITE (unit_out_s, '(I6,F16.8)') istate, angstrom*centers(4, istate)
570      END DO
571
572      ! Print the wannier centers
573      print_key => section_vals_get_subs_vals(print_loc_section, "WANNIER_CENTERS")
574      CALL print_wannier_centers(qs_loc_env, print_key, centers, logger, ispin)
575
576      CALL cp_print_key_finished_output(unit_out_s, logger, print_loc_section, "WANNIER_SPREADS")
577
578      DEALLOCATE (Qii, ziimax, atom_of_state, diag)
579
580   END SUBROUTINE centers_spreads_pipek
581
582! **************************************************************************************************
583!> \brief set up the calculation of localized orbitals
584!> \param qs_env ...
585!> \param qs_loc_env ...
586!> \param print_loc_section ...
587!> \param myspin ...
588!> \param ext_mo_coeff ...
589!> \par History
590!>      04.2005 created [MI]
591!> \author MI
592! **************************************************************************************************
593   SUBROUTINE qs_loc_driver(qs_env, qs_loc_env, print_loc_section, myspin, &
594                            ext_mo_coeff)
595
596      TYPE(qs_environment_type), POINTER                 :: qs_env
597      TYPE(qs_loc_env_new_type), POINTER                 :: qs_loc_env
598      TYPE(section_vals_type), POINTER                   :: print_loc_section
599      INTEGER, INTENT(IN), OPTIONAL                      :: myspin
600      TYPE(cp_fm_type), OPTIONAL, POINTER                :: ext_mo_coeff
601
602      CHARACTER(len=*), PARAMETER :: routineN = 'qs_loc_driver', routineP = moduleN//':'//routineN
603
604      CHARACTER(LEN=default_string_length)               :: my_pos
605      INTEGER                                            :: dim_op, handle, i, imo, imoloc, ir, &
606                                                            ispin, istate, j, jstate, l_spin, lb, &
607                                                            loc_method, n_rep, nao, ncubes, nmo, &
608                                                            nmosub, s_spin, ub
609      INTEGER, DIMENSION(:), POINTER                     :: bounds, list, list_cubes
610      LOGICAL                                            :: append_cube, list_cubes_setup
611      LOGICAL, SAVE                                      :: first_time = .TRUE.
612      REAL(dp), DIMENSION(6)                             :: weights
613      REAL(KIND=dp), DIMENSION(:, :), POINTER            :: centers, vecbuffer
614      TYPE(cell_type), POINTER                           :: cell
615      TYPE(cp_fm_p_type), DIMENSION(:), POINTER          :: moloc_coeff
616      TYPE(cp_fm_p_type), DIMENSION(:, :), POINTER       :: op_fm_set
617      TYPE(cp_fm_struct_type), POINTER                   :: tmp_fm_struct
618      TYPE(cp_fm_type), POINTER                          :: mo_coeff
619      TYPE(cp_para_env_type), POINTER                    :: para_env
620      TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: op_sm_set
621      TYPE(dft_control_type), POINTER                    :: dft_control
622      TYPE(localized_wfn_control_type), POINTER          :: localized_wfn_control
623      TYPE(mo_set_p_type), DIMENSION(:), POINTER         :: mos
624      TYPE(section_vals_type), POINTER                   :: print_key
625
626      CALL timeset(routineN, handle)
627      NULLIFY (para_env, mos, dft_control, list_cubes)
628      NULLIFY (cell, localized_wfn_control, moloc_coeff, op_sm_set, op_fm_set)
629      qs_loc_env%first_time = first_time
630      qs_loc_env%target_time = qs_env%target_time
631      qs_loc_env%start_time = qs_env%start_time
632
633      CALL get_qs_loc_env(qs_loc_env=qs_loc_env, &
634                          localized_wfn_control=localized_wfn_control, &
635                          moloc_coeff=moloc_coeff, op_sm_set=op_sm_set, op_fm_set=op_fm_set, cell=cell, &
636                          weights=weights, dim_op=dim_op)
637
638      CALL get_qs_env(qs_env=qs_env, dft_control=dft_control, &
639                      para_env=para_env, mos=mos)
640
641      s_spin = 1
642      l_spin = dft_control%nspins
643      IF (PRESENT(myspin)) THEN
644         s_spin = myspin
645         l_spin = myspin
646      END IF
647
648      DO ispin = s_spin, l_spin
649         CALL get_mo_set(mo_set=mos(ispin)%mo_set, nao=nao, nmo=nmo)
650         loc_method = localized_wfn_control%localization_method
651         SELECT CASE (localized_wfn_control%operator_type)
652         CASE (op_loc_berry)
653            ! Here we allocate op_fm_set with the RIGHT size for uks
654            NULLIFY (tmp_fm_struct, mo_coeff)
655            nmosub = localized_wfn_control%nloc_states(ispin)
656            IF (PRESENT(ext_mo_coeff)) THEN
657               CALL cp_fm_struct_create(tmp_fm_struct, nrow_global=nmosub, &
658                                        ncol_global=nmosub, para_env=para_env, context=ext_mo_coeff%matrix_struct%context)
659            ELSE
660               CALL get_mo_set(mo_set=mos(ispin)%mo_set, mo_coeff=mo_coeff)
661               CALL cp_fm_struct_create(tmp_fm_struct, nrow_global=nmosub, &
662                                        ncol_global=nmosub, para_env=para_env, context=mo_coeff%matrix_struct%context)
663            END IF
664            !
665            ALLOCATE (op_fm_set(2, dim_op))
666            DO i = 1, dim_op
667               DO j = 1, SIZE(op_fm_set, 1)
668                  NULLIFY (op_fm_set(j, i)%matrix)
669                  CALL cp_fm_create(op_fm_set(j, i)%matrix, tmp_fm_struct)
670                  CALL cp_fm_get_info(op_fm_set(j, i)%matrix, nrow_global=nmosub)
671                  CPASSERT(nmo >= nmosub)
672                  CALL cp_fm_set_all(op_fm_set(j, i)%matrix, 0.0_dp)
673               END DO
674            END DO
675            CALL cp_fm_struct_release(tmp_fm_struct)
676
677            CALL optimize_loc_berry(loc_method, qs_loc_env, moloc_coeff(ispin)%matrix, op_sm_set, &
678                                    op_fm_set, para_env, cell, weights, ispin, print_loc_section)
679
680            ! Here we dealloctate op_fm_set
681            IF (ASSOCIATED(op_fm_set)) THEN
682               DO i = 1, dim_op
683                  DO j = 1, SIZE(op_fm_set, 1)
684                     CALL cp_fm_release(op_fm_set(j, i)%matrix)
685                  END DO
686               END DO
687               DEALLOCATE (op_fm_set)
688            END IF
689
690         CASE (op_loc_boys)
691            CPABORT("Boys localization not implemented")
692
693         CASE (op_loc_pipek)
694            CALL optimize_loc_pipek(qs_env, loc_method, qs_loc_env, moloc_coeff(ispin)%matrix, &
695                                    op_fm_set, ispin, print_loc_section)
696
697         END SELECT
698
699         ! give back the localized orbitals
700         IF (.NOT. PRESENT(ext_mo_coeff)) THEN
701            CALL get_mo_set(mo_set=mos(ispin)%mo_set, mo_coeff=mo_coeff)
702         END IF
703
704         lb = localized_wfn_control%lu_bound_states(1, ispin)
705         ub = localized_wfn_control%lu_bound_states(2, ispin)
706
707         IF (localized_wfn_control%set_of_states == state_loc_list) THEN
708            ALLOCATE (vecbuffer(1, nao))
709            nmosub = SIZE(localized_wfn_control%loc_states, 1)
710            imoloc = 0
711            DO i = lb, ub
712               ! Get the index in the subset
713               imoloc = imoloc + 1
714               ! Get the index in the full set
715               imo = localized_wfn_control%loc_states(i, ispin)
716
717               CALL cp_fm_get_submatrix(moloc_coeff(ispin)%matrix, vecbuffer, 1, imoloc, &
718                                        nao, 1, transpose=.TRUE.)
719               IF (PRESENT(ext_mo_coeff)) THEN
720                  CALL cp_fm_set_submatrix(ext_mo_coeff, vecbuffer, 1, imo, &
721                                           nao, 1, transpose=.TRUE.)
722               ELSE
723                  CALL cp_fm_set_submatrix(mo_coeff, vecbuffer, 1, imo, &
724                                           nao, 1, transpose=.TRUE.)
725               END IF
726            END DO
727            DEALLOCATE (vecbuffer)
728
729         ELSE
730            nmosub = localized_wfn_control%nloc_states(ispin)
731            IF (PRESENT(ext_mo_coeff)) THEN
732               CALL cp_fm_to_fm(moloc_coeff(ispin)%matrix, ext_mo_coeff, nmosub, 1, lb)
733            ELSE
734               CALL cp_fm_to_fm(moloc_coeff(ispin)%matrix, mo_coeff, nmosub, 1, lb)
735            END IF
736         END IF
737
738         ! Write cube files if required
739         IF (localized_wfn_control%print_cubes) THEN
740            list_cubes_setup = .FALSE.
741            NULLIFY (bounds, list, list_cubes)
742
743            ! Provides boundaries of MOs
744            CALL section_vals_val_get(print_loc_section, "WANNIER_CUBES%CUBES_LU_BOUNDS", &
745                                      i_vals=bounds)
746            ncubes = bounds(2) - bounds(1) + 1
747            IF (ncubes > 0) THEN
748               list_cubes_setup = .TRUE.
749               ALLOCATE (list_cubes(ncubes))
750               DO ir = 1, ncubes
751                  list_cubes(ir) = bounds(1) + (ir - 1)
752               END DO
753            END IF
754
755            ! Provides the list of MOs
756            CALL section_vals_val_get(print_loc_section, "WANNIER_CUBES%CUBES_LIST", &
757                                      n_rep_val=n_rep)
758            IF (.NOT. list_cubes_setup) THEN
759               ncubes = 0
760               DO ir = 1, n_rep
761                  CALL section_vals_val_get(print_loc_section, "WANNIER_CUBES%CUBES_LIST", &
762                                            i_rep_val=ir, i_vals=list)
763                  IF (ASSOCIATED(list)) THEN
764                     CALL reallocate(list_cubes, 1, ncubes + SIZE(list))
765                     DO i = 1, SIZE(list)
766                        list_cubes(i + ncubes) = list(i)
767                     END DO
768                     ncubes = ncubes + SIZE(list)
769                  END IF
770               END DO
771               IF (ncubes > 0) list_cubes_setup = .TRUE.
772            END IF
773
774            ! Full list of Mos
775            IF (.NOT. list_cubes_setup) THEN
776               list_cubes_setup = .TRUE.
777               ncubes = localized_wfn_control%nloc_states(1)
778               IF (ncubes > 0) THEN
779                  ALLOCATE (list_cubes(ncubes))
780               END IF
781               DO i = 1, ncubes
782                  list_cubes(i) = i
783               END DO
784            END IF
785
786            ncubes = SIZE(list_cubes)
787            ncubes = MIN(ncubes, nmo)
788            ALLOCATE (centers(6, ncubes))
789            DO i = 1, ncubes
790               istate = list_cubes(i)
791               DO j = 1, localized_wfn_control%nloc_states(ispin)
792                  jstate = localized_wfn_control%loc_states(j, ispin)
793                  IF (istate == jstate) THEN
794                     centers(1:6, i) = localized_wfn_control%centers_set(ispin)%array(1:6, j)
795                     EXIT
796                  ENDIF
797               END DO
798            END DO ! ncubes
799
800            ! Real call for dumping the cube files
801            print_key => section_vals_get_subs_vals(print_loc_section, "WANNIER_CUBES")
802            append_cube = section_get_lval(print_loc_section, "WANNIER_CUBES%APPEND")
803            my_pos = "REWIND"
804            IF (append_cube) THEN
805               my_pos = "APPEND"
806            END IF
807
808            CALL qs_print_cubes(qs_env, moloc_coeff(ispin)%matrix, ncubes, list_cubes, centers, &
809                                print_key, "loc"//TRIM(ADJUSTL(qs_loc_env%tag_mo)), &
810                                ispin=ispin, file_position=my_pos)
811
812            DEALLOCATE (centers)
813            DEALLOCATE (list_cubes)
814         END IF
815      END DO ! ispin
816      first_time = .FALSE.
817      CALL timestop(handle)
818   END SUBROUTINE qs_loc_driver
819
820! **************************************************************************************************
821!> \brief write the cube files for a set of selected states
822!> \param qs_env ...
823!> \param mo_coeff set mos from which the states to be printed are extracted
824!> \param nstates number of states to be printed
825!> \param state_list list of the indexes of the states to be printed
826!> \param centers centers and spread, all=0 if they hva not been calculated
827!> \param print_key ...
828!> \param root initial part of the cube file names
829!> \param ispin ...
830!> \param idir ...
831!> \param state0 ...
832!> \param file_position ...
833!> \par History
834!>      08.2005 created [MI]
835!> \author MI
836!> \note
837!>      This routine shoul not be in this module
838! **************************************************************************************************
839   SUBROUTINE qs_print_cubes(qs_env, mo_coeff, nstates, state_list, centers, &
840                             print_key, root, ispin, idir, state0, file_position)
841
842      TYPE(qs_environment_type), POINTER                 :: qs_env
843      TYPE(cp_fm_type), POINTER                          :: mo_coeff
844      INTEGER, INTENT(IN)                                :: nstates
845      INTEGER, DIMENSION(:), POINTER                     :: state_list
846      REAL(dp), DIMENSION(:, :), POINTER                 :: centers
847      TYPE(section_vals_type), POINTER                   :: print_key
848      CHARACTER(LEN=*)                                   :: root
849      INTEGER, INTENT(IN), OPTIONAL                      :: ispin, idir
850      INTEGER, OPTIONAL                                  :: state0
851      CHARACTER(LEN=default_string_length), OPTIONAL     :: file_position
852
853      CHARACTER(len=*), PARAMETER :: routineN = 'qs_print_cubes', routineP = moduleN//':'//routineN
854      CHARACTER, DIMENSION(3), PARAMETER                 :: labels = (/'x', 'y', 'z'/)
855
856      CHARACTER                                          :: label
857      CHARACTER(LEN=default_path_length)                 :: file_tmp, filename, my_pos
858      CHARACTER(LEN=default_string_length)               :: title
859      INTEGER                                            :: handle, ia, ie, istate, ivector, &
860                                                            my_ispin, my_state0, unit_out_c
861      LOGICAL                                            :: add_idir, add_spin, mpi_io
862      TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
863      TYPE(cell_type), POINTER                           :: cell
864      TYPE(cp_logger_type), POINTER                      :: logger
865      TYPE(dft_control_type), POINTER                    :: dft_control
866      TYPE(particle_list_type), POINTER                  :: particles
867      TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
868      TYPE(pw_env_type), POINTER                         :: pw_env
869      TYPE(pw_p_type)                                    :: wf_g, wf_r
870      TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
871      TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
872      TYPE(qs_subsys_type), POINTER                      :: subsys
873
874      CALL timeset(routineN, handle)
875      NULLIFY (auxbas_pw_pool, pw_env, logger)
876      logger => cp_get_default_logger()
877
878      CALL get_qs_env(qs_env=qs_env, pw_env=pw_env, subsys=subsys)
879      CALL qs_subsys_get(subsys, particles=particles)
880
881      CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
882
883      CALL pw_pool_create_pw(auxbas_pw_pool, wf_r%pw, &
884                             use_data=REALDATA3D, &
885                             in_space=REALSPACE)
886      CALL pw_pool_create_pw(auxbas_pw_pool, wf_g%pw, &
887                             use_data=COMPLEXDATA1D, &
888                             in_space=RECIPROCALSPACE)
889
890      my_state0 = 0
891      IF (PRESENT(state0)) my_state0 = state0
892
893      add_spin = .FALSE.
894      my_ispin = 1
895      IF (PRESENT(ispin)) THEN
896         add_spin = .TRUE.
897         my_ispin = ispin
898      END IF
899      add_idir = .FALSE.
900      IF (PRESENT(idir)) THEN
901         add_idir = .TRUE.
902         label = labels(idir)
903      END IF
904
905      my_pos = "REWIND"
906      IF (PRESENT(file_position)) THEN
907         my_pos = file_position
908      END IF
909
910      DO istate = 1, nstates
911         ivector = state_list(istate) - my_state0
912         CALL get_qs_env(qs_env=qs_env, atomic_kind_set=atomic_kind_set, qs_kind_set=qs_kind_set, cell=cell, &
913                         dft_control=dft_control, particle_set=particle_set, pw_env=pw_env)
914
915         CALL calculate_wavefunction(mo_coeff, ivector, wf_r, wf_g, atomic_kind_set, &
916                                     qs_kind_set, cell, dft_control, particle_set, pw_env)
917
918         ! Formatting the middle part of the name
919         ivector = state_list(istate)
920         CALL xstring(root, ia, ie)
921         IF (add_idir) THEN
922            filename = root(ia:ie)//"_"//label//"_w"//TRIM(ADJUSTL(cp_to_string(ivector)))
923         ELSE
924            filename = root(ia:ie)//"_w"//TRIM(ADJUSTL(cp_to_string(ivector)))
925         END IF
926         IF (add_spin) THEN
927            file_tmp = filename
928            CALL xstring(file_tmp, ia, ie)
929            filename = file_tmp(ia:ie)//"_s"//TRIM(ADJUSTL(cp_to_string(ispin)))
930         END IF
931
932         ! Using the print_key tools to open the file with the proper name
933         mpi_io = .TRUE.
934         unit_out_c = cp_print_key_unit_nr(logger, print_key, "", middle_name=filename, &
935                                           extension=".cube", file_position=my_pos, log_filename=.FALSE., &
936                                           mpi_io=mpi_io)
937         IF (SIZE(centers, 1) == 6) THEN
938            WRITE (title, '(A7,I5.5,A2,I1.1,A1,6F10.4)') "WFN ", ivector, "_s", my_ispin, " ", &
939               centers(1:3, istate)*angstrom, centers(4:6, istate)*angstrom
940         ELSE
941            WRITE (title, '(A7,I5.5,A2,I1.1,A1,3F10.4)') "WFN ", ivector, "_s", my_ispin, " ", &
942               centers(1:3, istate)*angstrom
943         END IF
944         CALL cp_pw_to_cube(wf_r%pw, unit_out_c, title, &
945                            particles=particles, &
946                            stride=section_get_ivals(print_key, "STRIDE"), mpi_io=mpi_io)
947         CALL cp_print_key_finished_output(unit_out_c, logger, print_key, "", mpi_io=mpi_io)
948      END DO
949
950      CALL pw_pool_give_back_pw(auxbas_pw_pool, wf_r%pw)
951      CALL pw_pool_give_back_pw(auxbas_pw_pool, wf_g%pw)
952      CALL timestop(handle)
953   END SUBROUTINE qs_print_cubes
954
955! **************************************************************************************************
956!> \brief Prints wannier centers
957!> \param qs_loc_env ...
958!> \param print_key ...
959!> \param center ...
960!> \param logger ...
961!> \param ispin ...
962! **************************************************************************************************
963   SUBROUTINE print_wannier_centers(qs_loc_env, print_key, center, logger, ispin)
964      TYPE(qs_loc_env_new_type), POINTER                 :: qs_loc_env
965      TYPE(section_vals_type), POINTER                   :: print_key
966      REAL(KIND=dp), INTENT(IN)                          :: center(:, :)
967      TYPE(cp_logger_type), POINTER                      :: logger
968      INTEGER, INTENT(IN)                                :: ispin
969
970      CHARACTER(len=*), PARAMETER :: routineN = 'print_wannier_centers', &
971         routineP = moduleN//':'//routineN
972
973      CHARACTER(default_string_length)                   :: iter, unit_str
974      CHARACTER(LEN=default_string_length)               :: my_ext, my_form
975      INTEGER                                            :: iunit, l, nstates
976      LOGICAL                                            :: first_time, init_traj
977      REAL(KIND=dp)                                      :: unit_conv
978
979      nstates = SIZE(center, 2)
980      my_form = "formatted"
981      my_ext = ".data"
982      IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key, first_time=first_time) &
983                , cp_p_file)) THEN
984         ! Find out if we want to print IONS+CENTERS or ONLY CENTERS..
985         IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key, "/IONS+CENTERS") &
986                   , cp_p_file)) THEN
987            CALL get_output_format(print_key, my_form=my_form, my_ext=my_ext)
988         END IF
989         IF (first_time .OR. (.NOT. qs_loc_env%first_time)) THEN
990            iunit = cp_print_key_unit_nr(logger, print_key, "", extension=my_ext, file_form=my_form, &
991                                         middle_name=TRIM(qs_loc_env%tag_mo)//"_centers_s"//TRIM(ADJUSTL(cp_to_string(ispin))), &
992                                         log_filename=.FALSE., on_file=.TRUE., is_new_file=init_traj)
993            IF (iunit > 0) THEN
994               ! Gather units of measure for output (if available)
995               CALL section_vals_val_get(print_key, "UNIT", c_val=unit_str)
996               unit_conv = cp_unit_from_cp2k(1.0_dp, TRIM(unit_str))
997
998               IF (BTEST(cp_print_key_should_output(logger%iter_info, print_key, "/IONS+CENTERS"), cp_p_file)) THEN
999                  ! Different possible formats
1000                  CALL print_wannier_traj(qs_loc_env, print_key, center, iunit, init_traj, unit_conv)
1001               ELSE
1002                  ! Default print format
1003                  iter = cp_iter_string(logger%iter_info)
1004                  WRITE (iunit, '(i6,/,a)') nstates, TRIM(iter)
1005                  DO l = 1, nstates
1006                     WRITE (iunit, '(A,4F16.8)') "X", unit_conv*center(1:4, l)
1007                  END DO
1008               END IF
1009            END IF
1010            CALL cp_print_key_finished_output(iunit, logger, print_key, on_file=.TRUE.)
1011         END IF
1012      END IF
1013   END SUBROUTINE print_wannier_centers
1014
1015! **************************************************************************************************
1016!> \brief computes spread and centers
1017!> \param qs_loc_env ...
1018!> \param print_key ...
1019!> \param center ...
1020!> \param iunit ...
1021!> \param init_traj ...
1022!> \param unit_conv ...
1023! **************************************************************************************************
1024   SUBROUTINE print_wannier_traj(qs_loc_env, print_key, center, iunit, init_traj, unit_conv)
1025      TYPE(qs_loc_env_new_type), POINTER                 :: qs_loc_env
1026      TYPE(section_vals_type), POINTER                   :: print_key
1027      REAL(KIND=dp), INTENT(IN)                          :: center(:, :)
1028      INTEGER, INTENT(IN)                                :: iunit
1029      LOGICAL, INTENT(IN)                                :: init_traj
1030      REAL(KIND=dp), INTENT(IN)                          :: unit_conv
1031
1032      CHARACTER(len=*), PARAMETER :: routineN = 'print_wannier_traj', &
1033         routineP = moduleN//':'//routineN
1034
1035      CHARACTER(len=default_string_length)               :: iter, remark1, remark2, title
1036      INTEGER                                            :: i, iskip, natom, ntot, outformat
1037      TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
1038      TYPE(atomic_kind_type), POINTER                    :: atomic_kind
1039      TYPE(cp_logger_type), POINTER                      :: logger
1040      TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
1041
1042      NULLIFY (particle_set, atomic_kind_set, atomic_kind, logger)
1043      logger => cp_get_default_logger()
1044      natom = SIZE(qs_loc_env%particle_set)
1045      ntot = natom + SIZE(center, 2)
1046      CALL allocate_particle_set(particle_set, ntot)
1047      ALLOCATE (atomic_kind_set(1))
1048      atomic_kind => atomic_kind_set(1)
1049      CALL set_atomic_kind(atomic_kind=atomic_kind, kind_number=0, &
1050                           name="X", element_symbol="X", mass=0.0_dp)
1051      ! Particles
1052      DO i = 1, natom
1053         particle_set(i)%atomic_kind => qs_loc_env%particle_set(i)%atomic_kind
1054         particle_set(i)%r = pbc(qs_loc_env%particle_set(i)%r, qs_loc_env%cell)
1055      END DO
1056      ! Wannier Centers
1057      DO i = natom + 1, ntot
1058         particle_set(i)%atomic_kind => atomic_kind
1059         particle_set(i)%r = pbc(center(1:3, i - natom), qs_loc_env%cell)
1060      END DO
1061      ! Dump the structure
1062      CALL section_vals_val_get(print_key, "FORMAT", i_val=outformat)
1063
1064      ! Header file
1065      SELECT CASE (outformat)
1066      CASE (dump_dcd, dump_dcd_aligned_cell)
1067         IF (init_traj) THEN
1068            !Lets write the header for the coordinate dcd
1069            ! Note (TL) : even the new DCD format is unfortunately too poor
1070            !             for our capabilities.. for example here the printing
1071            !             of the geometry could be nested inside several iteration
1072            !             levels.. this cannot be exactly reproduce with DCD.
1073            !             Just as a compromise let's pick-up the value of the MD iteration
1074            !             level. In any case this is not any sensible information for the standard..
1075            iskip = section_get_ival(print_key, "EACH%MD")
1076            WRITE (iunit) "CORD", 0, -1, iskip, &
1077               0, 0, 0, 0, 0, 0, REAL(0, KIND=sp), 1, 0, 0, 0, 0, 0, 0, 0, 0, 24
1078            remark1 = "REMARK FILETYPE CORD DCD GENERATED BY CP2K"
1079            remark2 = "REMARK Support new DCD format with cell information"
1080            WRITE (iunit) 2, remark1, remark2
1081            WRITE (iunit) ntot
1082            CALL m_flush(iunit)
1083         END IF
1084      CASE (dump_xmol)
1085         iter = cp_iter_string(logger%iter_info)
1086         WRITE (UNIT=title, FMT="(A)") " Particles+Wannier centers. Iteration:"//TRIM(iter)
1087      CASE DEFAULT
1088         title = ""
1089      END SELECT
1090      CALL write_particle_coordinates(particle_set, iunit, outformat, "POS", title, qs_loc_env%cell, &
1091                                      unit_conv=unit_conv)
1092      CALL m_flush(iunit)
1093      CALL deallocate_particle_set(particle_set)
1094      CALL deallocate_atomic_kind_set(atomic_kind_set)
1095   END SUBROUTINE print_wannier_traj
1096
1097END MODULE qs_loc_methods
1098