(git:27c966b)
Loading...
Searching...
No Matches
qs_wannier90.F
Go to the documentation of this file.
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 Interface to Wannier90 code
10!> \par History
11!> 06.2016 created [JGH]
12!> \author JGH
13! **************************************************************************************************
18 USE bibliography, ONLY: gresch2017,&
20 cite_reference
21 USE cell_types, ONLY: cell_type,&
24 USE cp_cfm_types, ONLY: &
29 USE cp_dbcsr_api, ONLY: &
31 dbcsr_p_type, dbcsr_scale, dbcsr_set, dbcsr_type, dbcsr_type_antisymmetric, &
32 dbcsr_type_no_symmetry, dbcsr_type_symmetric
36 USE cp_files, ONLY: close_file,&
41 USE cp_fm_types, ONLY: cp_fm_create,&
50 USE kinds, ONLY: default_path_length,&
52 dp
61 USE kpoint_types, ONLY: get_kpoint_info,&
67 USE machine, ONLY: m_timestamp,&
69 USE mathconstants, ONLY: twopi
70 USE mathlib, ONLY: diag_complex
74 USE physcon, ONLY: angstrom,&
75 evolt
80 USE qs_kind_types, ONLY: get_qs_kind,&
82 USE qs_mo_types, ONLY: get_mo_set,&
108 USE wannier90, ONLY: wannier_setup
113#include "./base/base_uses.f90"
114
115 IMPLICIT NONE
116 PRIVATE
117
118 CHARACTER(LEN=12), PARAMETER :: window_keywords(4) = &
119 [character(len=12) :: 'DIS_WIN_MIN', 'DIS_WIN_MAX', 'DIS_FROZ_MIN', 'DIS_FROZ_MAX']
120 CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_wannier90'
121 INTEGER, PARAMETER, PRIVATE :: w90_kpoints_mp_grid = 0, &
122 w90_kpoints_scf = 1, w90_kpoints_nnkp = 2, w90_kpoints_wilson = 3, &
123 w90_kpoints_trim = 4
124
125 TYPE berry_matrix_type
126 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: sinmat => null(), cosmat => null()
127 END TYPE berry_matrix_type
128
130
131! **************************************************************************************************
132
133CONTAINS
134
135! **************************************************************************************************
136!> \brief ...
137!> \param input ...
138!> \param logger ...
139!> \param qs_env ...
140! **************************************************************************************************
141 SUBROUTINE wannier90_interface(input, logger, qs_env)
142 TYPE(section_vals_type), POINTER :: input
143 TYPE(cp_logger_type), POINTER :: logger
144 TYPE(qs_environment_type), POINTER :: qs_env
145
146 CHARACTER(len=*), PARAMETER :: routinen = 'wannier90_interface'
147
148 INTEGER :: first_spin, handle, i, ichern, ispin, &
149 iw, iz2, last_spin, max_refinement, &
150 old_chern, old_z2, refinement, &
151 selection, source
152 LOGICAL :: converged, explicit, require_chern, &
153 require_z2
154 REAL(kind=dp) :: error, movement, tolerance
155 REAL(kind=dp), ALLOCATABLE :: centres(:, :), previous(:, :)
156 TYPE(dft_control_type), POINTER :: dft_control
157 TYPE(section_vals_type), POINTER :: w_input
158
159 !--------------------------------------------------------------------------------------------!
160
161 CALL timeset(routinen, handle)
162 w_input => section_vals_get_subs_vals(section_vals=input, &
163 subsection_name="DFT%PRINT%WANNIER90")
164 CALL section_vals_get(w_input, explicit=explicit)
165 IF (explicit) THEN
166
168
169 IF (iw > 0) THEN
170 WRITE (iw, '(/,T2,A)') &
171 '!-----------------------------------------------------------------------------!'
172 WRITE (iw, '(T32,A)') "Interface to Wannier90"
173 WRITE (iw, '(T2,A)') &
174 '!-----------------------------------------------------------------------------!'
175 END IF
176
177 CALL get_qs_env(qs_env, dft_control=dft_control)
178 first_spin = 1
179 last_spin = dft_control%nspins
180 IF (last_spin /= 1 .AND. last_spin /= 2) cpabort('Wannier90 requires collinear spin channels.')
181 CALL section_vals_val_get(w_input, 'SPIN_CHANNEL', i_val=selection)
182 IF (selection /= 0) THEN
183 IF (selection > last_spin) cpabort('The requested Wannier90 spin channel is not present.')
184 first_spin = selection
185 last_spin = selection
186 END IF
187 DO ispin = first_spin, last_spin
188 CALL section_vals_val_get(w_input, "KPOINTS_SOURCE", i_val=source)
189 CALL section_vals_val_get(w_input, "WILSON_MAX_REFINEMENT", i_val=max_refinement)
190 CALL section_vals_val_get(w_input, "WILSON_TOL", r_val=tolerance)
191 CALL section_vals_val_get(w_input, "Z2", l_val=require_z2)
192 CALL section_vals_val_get(w_input, "CHERN", l_val=require_chern)
193 IF (tolerance <= 0.0_dp) cpabort("WILSON_TOL must be positive.")
194 IF (source == w90_kpoints_wilson) THEN
195 IF (max_refinement < 1 .OR. max_refinement > 10) THEN
196 cpabort("WILSON_MAX_REFINEMENT must be between 1 and 10.")
197 END IF
198 converged = .false.
199 old_z2 = -1
200 old_chern = huge(0)
201 DO refinement = 0, max_refinement
202 CALL wannier90_files(qs_env, w_input, iw, ispin, refinement, centres, iz2, ichern)
203 IF (refinement > 0) THEN
204 error = 0.0_dp
205 DO i = 1, SIZE(previous, 2)
206 error = max(error, wcc_distance(previous(:, i), centres(:, 2*i - 1)))
207 END DO
208 movement = 0.0_dp
209 DO i = 2, SIZE(centres, 2)
210 movement = max(movement, wcc_distance(centres(:, i - 1), centres(:, i)))
211 END DO
212 IF (iw > 0) WRITE (iw, '(T2,A,I0,A,ES12.4,A,ES12.4)') &
213 "TOPOLOGY| Refinement ", refinement, ": WCC change ", error, ", transverse step ", movement
214 converged = error < tolerance .AND. movement < 0.1_dp .AND. iz2 == old_z2
215 IF (require_z2) converged = converged .AND. iz2 >= 0 .AND. surface_resolved(centres)
216 IF (require_chern) converged = converged .AND. ichern /= huge(0) .AND. ichern == old_chern
217 IF (converged) EXIT
218 END IF
219 CALL move_alloc(centres, previous)
220 old_z2 = iz2
221 old_chern = ichern
222 END DO
223 IF (.NOT. converged) THEN
224 cpabort("Wilson surface unconverged; increase WILSON_MAX_REFINEMENT or mesh.")
225 END IF
226 IF (iw > 0) THEN
227 WRITE (iw, '(T2,A)') "TOPOLOGY| Wilson surface sampling converged."
228 IF (iz2 >= 0) WRITE (iw, '(T2,A,I0)') "TOPOLOGY| Converged Z2 invariant: ", iz2
229 IF (require_chern) WRITE (iw, '(T2,A,I0)') "TOPOLOGY| Converged first Chern number: ", ichern
230 END IF
231 ELSE
232 CALL wannier90_files(qs_env, w_input, iw, ispin, 0, centres, iz2, ichern)
233 END IF
234 IF (ALLOCATED(previous)) DEALLOCATE (previous)
235 END DO
236
237 IF (iw > 0) THEN
238 WRITE (iw, '(/,T2,A)') &
239 '!--------------------------------End of Wannier90-----------------------------!'
240 END IF
241 END IF
242 CALL timestop(handle)
243
244 END SUBROUTINE wannier90_interface
245
246! **************************************************************************************************
247!> \brief Copies a k-point CFM into the all-k-point Wannier work distribution.
248!> \param source source CFM; it may be unassociated on ranks outside its k-point group
249!> \param destination destination CFM
250!> \param para_env communicator spanning the source and destination distributions
251! **************************************************************************************************
252 SUBROUTINE wannier90_copy_cfm_general(source, destination, para_env)
253 TYPE(cp_cfm_type), INTENT(IN), POINTER :: source
254 TYPE(cp_cfm_type), INTENT(IN) :: destination
255 TYPE(mp_para_env_type), INTENT(IN), POINTER :: para_env
256
257 TYPE(cp_cfm_type) :: empty_source
258
259 IF (ASSOCIATED(source)) THEN
260 CALL cp_cfm_copy_general(source, destination, para_env)
261 ELSE
262 CALL cp_cfm_copy_general(empty_source, destination, para_env)
263 END IF
264
265 END SUBROUTINE wannier90_copy_cfm_general
266
267! **************************************************************************************************
268!> \brief ...
269!> \param qs_env ...
270!> \param input ...
271!> \param iw ...
272!> \param ispin selected spin channel
273!> \param refinement number of joint mesh doublings
274!> \param wcc_out Wilson centres for each closed loop
275!> \param z2_value candidate Z2 parity, or -1 when not requested
276!> \param chern_value candidate first Chern number, or HUGE(0) when unavailable
277! **************************************************************************************************
278 SUBROUTINE wannier90_files(qs_env, input, iw, ispin, refinement, wcc_out, z2_value, chern_value)
279 TYPE(qs_environment_type), POINTER :: qs_env
280 TYPE(section_vals_type), POINTER :: input
281 INTEGER, INTENT(IN) :: iw, ispin, refinement
282 REAL(kind=dp), ALLOCATABLE, INTENT(OUT) :: wcc_out(:, :)
283 INTEGER, INTENT(OUT) :: z2_value, chern_value
284
285 INTEGER, PARAMETER :: num_nnmax = 12
286
287 CHARACTER(len=2) :: asym
288 CHARACTER(len=20), ALLOCATABLE, DIMENSION(:) :: atom_symbols
289 CHARACTER(len=default_path_length) :: nnkp_file
290 CHARACTER(len=default_string_length) :: filename, input_kp_scheme, reuse_reason, &
291 seed_name
292 CHARACTER(LEN=timestamp_length) :: timestamp
293 COMPLEX(KIND=dp) :: zmmn
294 COMPLEX(KIND=dp), ALLOCATABLE :: ao_projections(:, :, :), export_coeff(:, :), &
295 export_scalar(:, :), hybrid_projections(:, :, :), initial_amn(:, :, :), &
296 link_matrix(:, :), parity_metric(:, :), parity_phase(:), reference_mo(:, :, :), &
297 scalar_overlap(:, :), soc_h(:, :), soc_u(:, :), soc_xyz(:, :, :), spinor_coeff(:, :, :), &
298 wilson_product(:, :, :)
299 INTEGER :: aligned_degenerate_blocks, aligned_degenerate_max_size, axis, base_mesh(2), &
300 counts(2), first_point, i, i_rep, ib, ib1, ib2, ibs, ik, ik2, ikk, ikpgr, iloop, &
301 initial_projections, ipoint, iunit, ix, iy, iz, jpar, k, kept_index, kpoints_source, &
302 loop_direction(3), n_rep, nadd, nao, nberry_images, nbs, nelectron, nexcl, nkp, nloop, &
303 nmo, nntot, npoint, nscalar, num_atoms, num_bands, num_bands_tot, num_kpts, num_wann, &
304 projection_candidates, spin_channel, state_components, state_unit, status, strong, &
305 tqc_dimension, trim_id, weak(3), z4
306 INTEGER, ALLOCATABLE :: ebr_coefficients(:, :), hybrid_groups(:, :), loop_index(:), &
307 parity_map(:), parity_odd(:), projection_pivots(:)
308 INTEGER, ALLOCATABLE, DIMENSION(:) :: band_indices, exclude_bands
309 INTEGER, ALLOCATABLE, DIMENSION(:, :) :: nblist, nnlist
310 INTEGER, ALLOCATABLE, DIMENSION(:, :, :) :: nncell
311 INTEGER, DIMENSION(2) :: kp_range
312 INTEGER, DIMENSION(3) :: input_nkp_grid, mp_grid
313 INTEGER, DIMENSION(:), POINTER :: invals
314 INTEGER, DIMENSION(:, :, :), POINTER :: berry_cell_index, cell_to_index
315 LOGICAL :: diis_step, do_chern, do_kpoints, do_parity, do_soc, do_tqc, do_wilson, do_z2, &
316 export_ao, export_hybrids, export_state, full_mesh_diagonalized, input_full_grid, &
317 input_gamma_centered, input_kpoint_symmetry, lowest_bands, mp_grid_explicit, &
318 mp_grid_valid, my_kpgrp, mygrp, nonnegative_atomic, ordered_berry, require_global_gap, &
319 reuse_scf_mos, reused_scf_mos, signed_atomic, time_reversal, use_bloch_phases, &
320 validate_reuse_ok, validate_reuse_scf_mos, window_set(4)
321 LOGICAL, ALLOCATABLE, DIMENSION(:) :: allowed_band, keep_band
322 REAL(kind=dp) :: aligned_degenerate_min_svalue, berry_phase, chern_winding, cmmn, &
323 conduction_min, direct_gap, energy_window(4), gap_tol, gauge_arg, gauge_imag, gauge_real, &
324 gauge_tmp, ksign, link_sv, loop_origin(3), pair_gap, parity_checks(4), &
325 parity_energy_error, parity_energy_tolerance, parity_error, parity_gap, parity_origin(3), &
326 parity_tolerance, projection_min_svalue, reuse_candidate_deviation, &
327 reuse_candidate_metric_deviation, reuse_candidate_min_svalue, reuse_candidate_residual, &
328 rmmn, transverse(3), valence_max, validation_eigenvalue_deviation, validation_min_svalue
329 REAL(kind=dp) :: validation_subspace_deviation, wkp_ref
330 REAL(kind=dp), ALLOCATABLE :: loop_sv(:), parity_energies(:), &
331 projection_eigenvalues(:, :), &
332 scalar_values(:), spinor_values(:, :)
333 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eigval
334 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: atoms_cart, b_latt, kpt_latt, &
335 reference_eigenvalues
336 REAL(kind=dp), DIMENSION(3) :: bvec, input_kp_shift, phase_center
337 REAL(kind=dp), DIMENSION(3, 3) :: h_inv, real_lattice, recip_lattice
338 REAL(kind=dp), DIMENSION(:), POINTER :: eigenvalues, wkp, wkp_source
339 REAL(kind=dp), DIMENSION(:, :), POINTER :: xkp, xkp_source
340 REAL(kind=dp), POINTER :: rvals(:)
341 TYPE(berry_matrix_type), DIMENSION(:), POINTER :: berry_matrix
342 TYPE(cell_type), POINTER :: cell
343 TYPE(cp_blacs_env_type), POINTER :: blacs_env
344 TYPE(cp_cfm_type) :: fmk1_cfm, fmk2_cfm, mmn_cfm, omat_cfm, &
345 tmp_cfm
346 TYPE(cp_cfm_type), POINTER :: cmo_coeff_source
347 TYPE(cp_fm_struct_type), POINTER :: matrix_struct_ao, matrix_struct_mmn, &
348 matrix_struct_work
349 TYPE(cp_fm_type) :: mat_imag, mat_real
350 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks, matrix_s, soc_matrices
351 TYPE(dbcsr_type), POINTER :: cmatrix, cmatrix_full, loop_imag, &
352 loop_real, rmatrix, rmatrix_full
353 TYPE(dft_control_type), POINTER :: dft_control
354 TYPE(kpoint_env_type), POINTER :: kp
355 TYPE(kpoint_type), POINTER :: berry_kpoint, kpoint, qs_kpoint
356 TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
357 TYPE(mp_para_env_type), POINTER :: para_env
358 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
359 POINTER :: overlap_nl, sab_nl
360 TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
361 TYPE(qs_environment_type), POINTER :: qs_env_kp
362 TYPE(qs_scf_env_type), POINTER :: scf_env
363 TYPE(scf_control_type), POINTER :: scf_control
364
365 !--------------------------------------------------------------------------------------------!
366
367 ! generate all arrays needed for the setup call
368 CALL section_vals_val_get(input, "SEED_NAME", c_val=seed_name)
369 CALL get_qs_env(qs_env, dft_control=dft_control)
370 IF (dft_control%nspins == 2) THEN
371 IF (len_trim(seed_name) + 5 > len(seed_name)) THEN
372 cpabort('Wannier90 seed is too long for the spin suffix.')
373 END IF
374 IF (ispin == 1) seed_name = trim(seed_name)//'_up'
375 IF (ispin == 2) seed_name = trim(seed_name)//'_down'
376 IF (iw > 0) WRITE (iw, '(T2,A,I0,2A)') 'WANNIER90| Spin channel ', ispin, ' file prefix ', trim(seed_name)
377 END IF
378 CALL section_vals_val_get(input, "MP_GRID", i_vals=invals, explicit=mp_grid_explicit)
379 CALL section_vals_val_get(input, "KPOINTS_SOURCE", i_val=kpoints_source)
380 ordered_berry = kpoints_source >= w90_kpoints_nnkp
381 CALL section_vals_val_get(input, "NNKP_FILE", c_val=nnkp_file)
382 spin_channel = ispin
383 CALL section_vals_val_get(input, "WILSON_LOOP", l_val=do_wilson)
384 CALL section_vals_val_get(input, "Z2", l_val=do_z2)
385 CALL section_vals_val_get(input, "CHERN", l_val=do_chern)
386 CALL section_vals_val_get(input, "REQUIRE_GLOBAL_GAP", l_val=require_global_gap)
387 CALL section_vals_val_get(input, "STATE_EXPORT", l_val=export_state)
388 CALL section_vals_val_get(input, "PARITY", l_val=do_parity)
389 CALL section_vals_val_get(input, "INVERSION_TQC", l_val=do_tqc)
390 CALL section_vals_val_get(input, "TQC_DIMENSION", i_val=tqc_dimension)
391 CALL section_vals_val_get(input, "PARITY_ORIGIN", r_vals=rvals)
392 parity_origin = rvals
393 CALL section_vals_val_get(input, "PARITY_TOLERANCE", r_val=parity_tolerance)
394 CALL section_vals_val_get(input, "PARITY_ENERGY_TOL", r_val=parity_energy_tolerance)
395 do_parity = do_parity .OR. do_tqc .OR. kpoints_source == w90_kpoints_trim
396 IF (do_parity .AND. .NOT. ordered_berry) THEN
397 cpabort("PARITY requires explicit NNKP, WILSON or TRIM points.")
398 END IF
399 IF (tqc_dimension < 2 .OR. tqc_dimension > 3) cpabort("TQC_DIMENSION must be 2 or 3.")
400 IF (parity_tolerance <= 0.0_dp .OR. parity_energy_tolerance <= 0.0_dp) THEN
401 cpabort("Invalid parity tolerance.")
402 END IF
403 IF (export_state .AND. .NOT. ordered_berry) THEN
404 cpabort("STATE_EXPORT requires explicit NNKP, WILSON or TRIM points.")
405 END IF
406 CALL section_vals_val_get(input, "TIME_REVERSAL", l_val=time_reversal)
407 CALL section_vals_val_get(input, "SOC", l_val=do_soc)
408 IF (do_tqc .AND. (.NOT. do_soc .OR. .NOT. time_reversal)) THEN
409 cpabort("INVERSION_TQC requires SOC T and TIME_REVERSAL T.")
410 END IF
411 CALL section_vals_val_get(input, "WILSON_GAP_TOL", r_val=gap_tol)
412 IF (gap_tol <= 0.0_dp) cpabort("WILSON_GAP_TOL must be positive.")
413 z2_value = -1
414 chern_value = huge(0)
415 do_wilson = do_wilson .OR. kpoints_source == w90_kpoints_wilson
416 IF (do_wilson .AND. kpoints_source == w90_kpoints_trim) THEN
417 cpabort("TRIM points are not Wilson loops; use KPOINTS_SOURCE WILSON.")
418 END IF
419 IF (do_wilson) CALL cite_reference(gresch2017)
420 IF (do_z2) CALL cite_reference(soluyanov2011)
421 IF ((do_wilson .OR. do_soc) .AND. kpoints_source < w90_kpoints_nnkp) THEN
422 cpabort("Native Wilson/SOC requires KPOINTS_SOURCE NNKP or WILSON.")
423 END IF
424 IF (do_z2 .AND. (kpoints_source /= w90_kpoints_wilson .OR. .NOT. do_soc .OR. .NOT. time_reversal)) THEN
425 cpabort("Z2 requires KPOINTS_SOURCE WILSON, SOC T, and TIME_REVERSAL T.")
426 END IF
427 IF (do_chern .AND. (kpoints_source /= w90_kpoints_wilson .OR. do_z2)) THEN
428 cpabort("CHERN requires a full WILSON surface and cannot be combined with Z2.")
429 END IF
430 IF (require_global_gap .AND. .NOT. do_wilson) THEN
431 cpabort("REQUIRE_GLOBAL_GAP requires Wilson analysis.")
432 END IF
433 CALL get_qs_env(qs_env, dft_control=dft_control)
434 IF (spin_channel < 1 .OR. spin_channel > dft_control%nspins) THEN
435 cpabort("WANNIER90%SPIN_CHANNEL is not available in this calculation.")
436 END IF
437 IF (do_soc .AND. dft_control%nspins /= 1) THEN
438 cpabort("WANNIER90 SOC currently requires a restricted SCF.")
439 END IF
440 CALL section_vals_val_get(input, "WANNIER_FUNCTIONS", n_rep_val=n_rep)
441 IF (n_rep > 1 .AND. n_rep /= dft_control%nspins) THEN
442 cpabort('Specify WANNIER_FUNCTIONS once, or once per spin channel.')
443 END IF
444 i_rep = 1
445 IF (n_rep > 1) i_rep = ispin
446 CALL section_vals_val_get(input, "WANNIER_FUNCTIONS", i_rep_val=i_rep, i_val=num_wann)
447 CALL section_vals_val_get(input, "ADDED_MOS", i_val=nadd)
448 CALL section_vals_val_get(input, "REUSE_SCF_MOS", l_val=reuse_scf_mos)
449 CALL section_vals_val_get(input, "VALIDATE_REUSE_SCF_MOS", l_val=validate_reuse_scf_mos)
450 CALL section_vals_val_get(input, "USE_BLOCH_PHASES", l_val=use_bloch_phases)
451 CALL section_vals_val_get(input, "INITIAL_PROJECTIONS", i_val=initial_projections)
452 CALL section_vals_val_get(input, "EXPORT_PROJECTION_CANDIDATES", i_val=projection_candidates)
453 export_ao = projection_candidates == 1 .OR. projection_candidates == 3
454 export_hybrids = projection_candidates == 2 .OR. projection_candidates == 3
455 IF (initial_projections == -1 .AND. use_bloch_phases) initial_projections = 0
456 energy_window = [-huge(0.0_dp)/100, huge(0.0_dp)/100, -huge(0.0_dp)/100, huge(0.0_dp)/100]
457 DO i = 1, 4
458 CALL section_vals_val_get(input, trim(window_keywords(i)), explicit=window_set(i))
459 IF (window_set(i)) CALL section_vals_val_get(input, trim(window_keywords(i)), r_val=energy_window(i))
460 END DO
461 IF (ordered_berry .AND. (initial_projections > 0 .OR. projection_candidates > 0 .OR. any(window_set))) THEN
462 cpabort("AO projections and energy windows require KPOINTS_SOURCE MP_GRID or SCF.")
463 END IF
464 IF (window_set(3) .AND. .NOT. window_set(4)) cpabort("DIS_FROZ_MIN requires DIS_FROZ_MAX.")
465 IF (energy_window(1) > energy_window(2) .OR. energy_window(3) > energy_window(4)) THEN
466 cpabort("Invalid Wannier90 energy window.")
467 END IF
468 reuse_scf_mos = reuse_scf_mos .AND. kpoints_source == w90_kpoints_scf
469 validate_reuse_scf_mos = validate_reuse_scf_mos .AND. reuse_scf_mos
470 mp_grid(1:3) = invals(1:3)
471 ! excluded bands
472 CALL section_vals_val_get(input, "EXCLUDE_BANDS", n_rep_val=n_rep)
473 nexcl = 0
474 DO i_rep = 1, n_rep
475 CALL section_vals_val_get(input, "EXCLUDE_BANDS", i_rep_val=i_rep, i_vals=invals)
476 nexcl = nexcl + SIZE(invals)
477 END DO
478 IF (nexcl > 0) THEN
479 ALLOCATE (exclude_bands(nexcl))
480 nexcl = 0
481 DO i_rep = 1, n_rep
482 CALL section_vals_val_get(input, "EXCLUDE_BANDS", i_rep_val=i_rep, i_vals=invals)
483 exclude_bands(nexcl + 1:nexcl + SIZE(invals)) = invals(:)
484 nexcl = nexcl + SIZE(invals)
485 END DO
486 END IF
487 !
488 ! lattice -> Angstrom
489 CALL get_qs_env(qs_env, cell=cell)
490 CALL get_cell(cell, h=real_lattice, h_inv=h_inv)
491 ! k-points
492 CALL get_qs_env(qs_env, particle_set=particle_set)
493 CALL get_qs_env(qs_env, para_env=para_env)
494 phase_center = 0.0_dp
495 DO i = 1, SIZE(particle_set)
496 phase_center(1:3) = phase_center(1:3) + matmul(h_inv, particle_set(i)%r)
497 END DO
498 phase_center(1:3) = phase_center(1:3)/real(SIZE(particle_set), kind=dp)
499 phase_center(1:3) = phase_center(1:3) - floor(phase_center(1:3))
500 recip_lattice(1:3, 1:3) = h_inv(1:3, 1:3)
501 real_lattice(1:3, 1:3) = angstrom*real_lattice(1:3, 1:3)
502 recip_lattice(1:3, 1:3) = (twopi/angstrom)*transpose(recip_lattice(1:3, 1:3))
503 NULLIFY (kpoint, qs_kpoint, xkp, wkp, xkp_source, wkp_source)
504 CALL get_qs_env(qs_env, do_kpoints=do_kpoints, kpoints=qs_kpoint)
505 input_kpoint_symmetry = .false.
506 input_full_grid = .false.
507 input_kp_scheme = ""
508 IF (do_kpoints .AND. ASSOCIATED(qs_kpoint)) THEN
509 CALL get_kpoint_info(qs_kpoint, kp_scheme=input_kp_scheme, nkp_grid=input_nkp_grid, &
510 kp_shift=input_kp_shift, symmetry=input_kpoint_symmetry, &
511 full_grid=input_full_grid, gamma_centered=input_gamma_centered, &
512 nkp=nkp, xkp=xkp, wkp=wkp)
513 END IF
514 CALL kpoint_create(kpoint)
515
516 SELECT CASE (kpoints_source)
517 CASE (w90_kpoints_nnkp, w90_kpoints_wilson, w90_kpoints_trim)
518 IF (kpoints_source == w90_kpoints_nnkp) THEN
519 CALL read_wannier90_nnkpts(nnkp_file, real_lattice, recip_lattice, kpt_latt, nnlist, nncell)
520 ELSE IF (kpoints_source == w90_kpoints_trim) THEN
521 num_kpts = 2**tqc_dimension
522 ALLOCATE (kpt_latt(3, num_kpts), nnlist(num_kpts, 1), nncell(3, num_kpts, 1))
523 kpt_latt = 0.0_dp
524 nncell = 0
525 DO i = 1, num_kpts
526 DO axis = 1, tqc_dimension
527 IF (btest(i - 1, axis - 1)) kpt_latt(axis, i) = 0.5_dp
528 END DO
529 nnlist(i, 1) = i
530 END DO
531 ELSE
532 CALL section_vals_val_get(input, "WILSON_MESH", i_vals=invals)
533 base_mesh = invals
534 IF (base_mesh(1) < 2 .OR. base_mesh(2) < 2) THEN
535 cpabort("WILSON_MESH entries must be at least two.")
536 END IF
537 npoint = base_mesh(1)*2**refinement
538 nloop = (base_mesh(2) - 1)*2**refinement + 1
539 CALL section_vals_val_get(input, "WILSON_DIRECTION", i_vals=invals)
540 loop_direction = invals
541 IF (all(loop_direction == 0)) cpabort("WILSON_DIRECTION must be nonzero.")
542 CALL section_vals_val_get(input, "WILSON_ORIGIN", r_vals=rvals)
543 loop_origin = rvals
544 CALL section_vals_val_get(input, "WILSON_TRANSVERSE", r_vals=rvals)
545 transverse = rvals
546 bvec = [loop_direction(2)*transverse(3) - loop_direction(3)*transverse(2), &
547 loop_direction(3)*transverse(1) - loop_direction(1)*transverse(3), &
548 loop_direction(1)*transverse(2) - loop_direction(2)*transverse(1)]
549 IF (sum(bvec**2) < 1.e-20_dp) THEN
550 cpabort("Wilson loop and transverse vectors must be linearly independent.")
551 END IF
552 IF (do_chern .AND. maxval(abs(transverse - nint(transverse))) > 1.e-10_dp) THEN
553 cpabort("CHERN requires integer WILSON_TRANSVERSE: a closed full surface, not a half-plane.")
554 END IF
555 IF (do_z2) THEN
556 IF (maxval(abs(2*loop_origin - nint(2*loop_origin))) > 1.e-10_dp .OR. &
557 maxval(abs(2*transverse - nint(2*transverse))) > 1.e-10_dp) THEN
558 cpabort("Z2 surface boundaries must pass through time-reversal-invariant momenta.")
559 END IF
560 ! Initially restrict native Z2 to standard half-planes, avoiding multiple coverings.
561 IF (sum(abs(loop_direction)) /= 1 .OR. &
562 abs(sum(abs(transverse)) - 0.5_dp) > 1.e-10_dp .OR. &
563 abs(dot_product(real(loop_direction, dp), transverse)) > 1.e-10_dp) THEN
564 cpabort("Native Z2 requires distinct coordinate axes with windings one and one half.")
565 END IF
566 END IF
567 num_kpts = npoint*nloop
568 ALLOCATE (kpt_latt(3, num_kpts), nnlist(num_kpts, 1), nncell(3, num_kpts, 1))
569 nncell = 0
570 DO iloop = 1, nloop
571 first_point = (iloop - 1)*npoint + 1
572 DO ipoint = 1, npoint
573 i = first_point + ipoint - 1
574 kpt_latt(:, i) = loop_origin + real(iloop - 1, dp)/real(nloop - 1, dp)*transverse + &
575 REAL(ipoint - 1, dp)/REAL(npoint, dp)*loop_direction
576 nnlist(i, 1) = i + 1
577 END DO
578 nnlist(i, 1) = first_point
579 nncell(:, i, 1) = loop_direction
580 END DO
581 END IF
582 num_kpts = SIZE(kpt_latt, 2)
583 nntot = SIZE(nnlist, 2)
584 kpoint%kp_scheme = "GENERAL"
585 kpoint%symmetry = .false.
586 kpoint%verbose = .false.
587 kpoint%full_grid = .true.
588 kpoint%eps_geo = 1.0e-6_dp
589 kpoint%use_real_wfn = .false.
590 kpoint%parallel_group_size = para_env%num_pe
591 kpoint%nkp = num_kpts
592 ALLOCATE (kpoint%xkp(3, num_kpts), kpoint%wkp(num_kpts))
593 kpoint%xkp = kpt_latt
594 ! Export weights do not enter the converged SCF density.
595 kpoint%wkp = 1.0_dp/real(num_kpts, kind=dp)
596 IF (iw > 0) WRITE (iw, '(T2,A,I0,A,I0)') &
597 "WANNIER90| Explicit points: ", num_kpts, ", neighbours per point: ", nntot
598 CASE (w90_kpoints_mp_grid)
599 num_kpts = mp_grid(1)*mp_grid(2)*mp_grid(3)
600 ALLOCATE (kpt_latt(3, num_kpts))
601 kpoint%kp_scheme = "MONKHORST-PACK"
602 kpoint%symmetry = .false.
603 kpoint%nkp_grid(1:3) = mp_grid(1:3)
604 kpoint%verbose = .false.
605 kpoint%full_grid = .true.
606 kpoint%eps_geo = 1.0e-6_dp
607 kpoint%use_real_wfn = .false.
608 kpoint%parallel_group_size = para_env%num_pe
609 i = 0
610 DO ix = 0, mp_grid(1) - 1
611 DO iy = 0, mp_grid(2) - 1
612 DO iz = 0, mp_grid(3) - 1
613 i = i + 1
614 kpt_latt(1, i) = real(ix, kind=dp)/real(mp_grid(1), kind=dp)
615 kpt_latt(2, i) = real(iy, kind=dp)/real(mp_grid(2), kind=dp)
616 kpt_latt(3, i) = real(iz, kind=dp)/real(mp_grid(3), kind=dp)
617 END DO
618 END DO
619 END DO
620 kpoint%nkp = num_kpts
621 ALLOCATE (kpoint%xkp(3, num_kpts), kpoint%wkp(num_kpts))
622 kpoint%wkp(:) = 1._dp/real(num_kpts, kind=dp)
623 ! rskp_transform uses reduced coordinates in exp(2*pi*i*k.R), with integer cell shifts R.
624 kpoint%xkp(:, :) = kpt_latt(:, :)
625
626 CASE (w90_kpoints_scf)
627 IF (.NOT. do_kpoints .OR. .NOT. ASSOCIATED(qs_kpoint)) THEN
628 cpabort("WANNIER90%KPOINTS_SOURCE SCF requires an active DFT%KPOINTS section.")
629 END IF
630 SELECT CASE (trim(input_kp_scheme))
631 CASE ("GAMMA")
632 mp_grid(:) = 1
633 num_kpts = 1
634 ALLOCATE (kpt_latt(3, num_kpts))
635 kpt_latt(1:3, 1) = 0.0_dp
636 kpoint%kp_scheme = "GAMMA"
637 kpoint%symmetry = .false.
638 kpoint%verbose = .false.
639 kpoint%full_grid = .true.
640 kpoint%eps_geo = 1.0e-6_dp
641 kpoint%use_real_wfn = .false.
642 kpoint%parallel_group_size = para_env%num_pe
643 kpoint%nkp = num_kpts
644 ALLOCATE (kpoint%xkp(3, num_kpts), kpoint%wkp(num_kpts))
645 kpoint%xkp(1:3, 1) = 0.0_dp
646 kpoint%wkp(1) = 1.0_dp
647
648 CASE ("MONKHORST-PACK", "MACDONALD")
649 mp_grid(1:3) = input_nkp_grid(1:3)
650 kpoint%kp_scheme = input_kp_scheme
651 kpoint%symmetry = .false.
652 kpoint%nkp_grid(1:3) = input_nkp_grid(1:3)
653 kpoint%kp_shift(1:3) = input_kp_shift(1:3)
654 kpoint%gamma_centered = input_gamma_centered
655 kpoint%verbose = .false.
656 kpoint%full_grid = .true.
657 kpoint%eps_geo = 1.0e-6_dp
658 kpoint%use_real_wfn = .false.
659 kpoint%parallel_group_size = para_env%num_pe
660 CALL kpoint_initialize(kpoint, particle_set, cell)
661 num_kpts = kpoint%nkp
662 ALLOCATE (kpt_latt(3, num_kpts))
663 kpt_latt(1:3, 1:num_kpts) = kpoint%xkp(1:3, 1:num_kpts)
664 IF (input_kpoint_symmetry .AND. .NOT. input_full_grid .AND. iw > 0) THEN
665 WRITE (iw, '(T2,A)') &
666 "WANNIER90| SCF k-points are symmetry-reduced; regenerating the full SCF mesh."
667 IF (reuse_scf_mos) THEN
668 WRITE (iw, '(T2,A)') &
669 "WANNIER90| CP2K will try to reconstruct the full-mesh MOs from the SCF orbitals."
670 ELSE
671 WRITE (iw, '(T2,A)') &
672 "WANNIER90| The full exported mesh is diagonalized for the Wannier90 files."
673 END IF
674 END IF
675
676 CASE ("GENERAL")
677 IF (ASSOCIATED(qs_kpoint%xkp_input)) THEN
678 xkp_source => qs_kpoint%xkp_input
679 wkp_source => qs_kpoint%wkp_input
680 ELSE
681 xkp_source => xkp
682 wkp_source => wkp
683 END IF
684 IF (.NOT. ASSOCIATED(xkp_source) .OR. .NOT. ASSOCIATED(wkp_source)) THEN
685 cpabort("Could not access the SCF GENERAL k-point set for the Wannier90 export.")
686 END IF
687 num_kpts = SIZE(wkp_source)
688 ALLOCATE (kpt_latt(3, num_kpts))
689 kpt_latt(1:3, 1:num_kpts) = xkp_source(1:3, 1:num_kpts)
690 IF (mp_grid_explicit) THEN
691 IF (mp_grid(1)*mp_grid(2)*mp_grid(3) /= num_kpts) THEN
692 cpabort("WANNIER90%MP_GRID must contain exactly as many points as the SCF GENERAL mesh.")
693 END IF
694 ELSE
695 CALL infer_wannier_mp_grid(kpt_latt, mp_grid, mp_grid_valid)
696 IF (.NOT. mp_grid_valid) THEN
697 cpabort("Could not infer WANNIER90%MP_GRID from the SCF GENERAL mesh.")
698 END IF
699 END IF
700 wkp_ref = 1.0_dp/real(num_kpts, kind=dp)
701 DO i = 1, num_kpts
702 IF (abs(wkp_source(i) - wkp_ref) > 1.0e-10_dp) THEN
703 cpabort("WANNIER90%KPOINTS_SOURCE SCF requires equally weighted GENERAL k-points.")
704 END IF
705 END DO
706 kpoint%kp_scheme = "GENERAL"
707 kpoint%symmetry = .false.
708 kpoint%nkp_grid(1:3) = mp_grid(1:3)
709 kpoint%verbose = .false.
710 kpoint%full_grid = .true.
711 kpoint%eps_geo = 1.0e-6_dp
712 kpoint%use_real_wfn = .false.
713 kpoint%parallel_group_size = para_env%num_pe
714 kpoint%nkp = num_kpts
715 ALLOCATE (kpoint%xkp(3, num_kpts), kpoint%wkp(num_kpts))
716 kpoint%xkp(1:3, 1:num_kpts) = xkp_source(1:3, 1:num_kpts)
717 kpoint%wkp(1:num_kpts) = wkp_ref
718 IF (input_kpoint_symmetry .AND. .NOT. input_full_grid .AND. iw > 0) THEN
719 WRITE (iw, '(T2,A)') &
720 "WANNIER90| SCF k-points are symmetry-reduced; using the full input GENERAL mesh."
721 IF (reuse_scf_mos) THEN
722 WRITE (iw, '(T2,A)') &
723 "WANNIER90| CP2K will try to reconstruct the full-mesh MOs from the SCF orbitals."
724 ELSE
725 WRITE (iw, '(T2,A)') &
726 "WANNIER90| The full exported mesh is diagonalized for the Wannier90 files."
727 END IF
728 END IF
729
730 CASE DEFAULT
731 cpabort("WANNIER90%KPOINTS_SOURCE SCF does not support this DFT%KPOINTS scheme.")
732 END SELECT
733 CASE DEFAULT
734 cpabort("Unknown WANNIER90%KPOINTS_SOURCE setting.")
735 END SELECT
736 ! number of bands in calculation
737 CALL get_qs_env(qs_env, mos=mos)
738 CALL get_mo_set(mo_set=mos(ispin), nao=nao, nmo=num_bands_tot, nelectron=nelectron)
739 num_bands_tot = min(nao, num_bands_tot + nadd)
740 nscalar = num_bands_tot
741 IF (do_soc) num_bands_tot = 2*nscalar
742 ALLOCATE (keep_band(num_bands_tot))
743 keep_band = .true.
744 DO i = 1, nexcl
745 ib = exclude_bands(i)
746 IF (ib < 1 .OR. ib > num_bands_tot) cpabort("WANNIER90%EXCLUDE_BANDS: index out of range.")
747 IF (ordered_berry .AND. .NOT. keep_band(ib)) THEN
748 cpabort("WANNIER90%EXCLUDE_BANDS: duplicate band index.")
749 END IF
750 keep_band(ib) = .false.
751 END DO
752 num_bands = count(keep_band)
753 IF (num_bands < 1 .OR. num_wann > num_bands) THEN
754 cpabort("WANNIER90 EXCLUDE_BANDS leaves too few bands for WANNIER_FUNCTIONS.")
755 END IF
756 ALLOCATE (band_indices(num_bands))
757 band_indices(:) = pack([(i, i=1, num_bands_tot)], keep_band)
758 lowest_bands = num_bands < num_bands_tot .AND. all(band_indices == [(i, i=1, num_bands)])
759 IF (require_global_gap .AND. .NOT. lowest_bands) THEN
760 cpabort("REQUIRE_GLOBAL_GAP needs a lowest-band prefix and an excluded band above it.")
761 END IF
762 IF (do_z2) THEN
763 IF (num_bands /= nelectron) THEN
764 cpabort("Z2 requires one occupied spinor per electron; adjust EXCLUDE_BANDS.")
765 END IF
766 IF (num_bands == num_bands_tot .OR. mod(num_bands, 2) /= 0) THEN
767 cpabort("Z2 needs an even occupied spinor subspace and at least one excluded conduction band.")
768 END IF
769 IF (any(band_indices /= [(i, i=1, num_bands)])) THEN
770 cpabort("Z2 requires the lowest occupied spinor bands.")
771 END IF
772 END IF
773 IF (do_wilson) THEN
774 IF (nntot /= 1) cpabort("WILSON_LOOP requires exactly one directed neighbour per point.")
775 ALLOCATE (loop_index(num_kpts))
776 i = 1
777 nloop = 0
778 DO WHILE (i <= num_kpts)
779 first_point = i
780 nloop = nloop + 1
781 DO
782 loop_index(i) = nloop
783 IF (nnlist(i, 1) == first_point) EXIT
784 IF (nnlist(i, 1) /= i + 1 .OR. i == num_kpts) THEN
785 cpabort("WILSON_LOOP requires contiguous ordered closed loops.")
786 END IF
787 i = i + 1
788 END DO
789 i = i + 1
790 END DO
791 ALLOCATE (wilson_product(num_bands, num_bands, nloop), loop_sv(nloop))
792 wilson_product = cmplx(0.0_dp, 0.0_dp, dp)
793 DO i = 1, num_bands
794 wilson_product(i, i, :) = cmplx(1.0_dp, 0.0_dp, dp)
795 END DO
796 loop_sv = 1.0_dp
797 ALLOCATE (wcc_out(num_bands, nloop))
798 ELSE
799 ALLOCATE (wcc_out(0, 0))
800 END IF
801 ALLOCATE (link_matrix(num_bands, num_bands))
802 IF (initial_projections == 0 .AND. num_wann /= num_bands .AND. .NOT. ordered_berry) THEN
803 cpabort("IDENTITY projections require WANNIER_FUNCTIONS to match the number of bands.")
804 END IF
805 IF (initial_projections >= 0 .AND. .NOT. ordered_berry .AND. num_wann < 1) THEN
806 cpabort("Initial projections require a positive WANNIER_FUNCTIONS count.")
807 END IF
808 IF (any(window_set) .AND. num_wann == num_bands) THEN
809 cpabort("Energy windows require more bands than Wannier functions.")
810 END IF
811 IF (any(window_set) .AND. num_wann < 1) THEN
812 cpabort("Energy windows require a positive WANNIER_FUNCTIONS count.")
813 END IF
814 ALLOCATE (allowed_band(num_bands))
815 IF (any(window_set)) ALLOCATE (projection_eigenvalues(num_bands, num_kpts))
816 num_atoms = SIZE(particle_set)
817 ALLOCATE (atoms_cart(3, num_atoms))
818 ALLOCATE (atom_symbols(num_atoms))
819 DO i = 1, num_atoms
820 atoms_cart(1:3, i) = particle_set(i)%r(1:3)
821 CALL get_atomic_kind(particle_set(i)%atomic_kind, element_symbol=asym)
822 atom_symbols(i) = asym
823 END DO
824 IF (kpoints_source < w90_kpoints_nnkp) THEN
825 ALLOCATE (nnlist(num_kpts, num_nnmax), nncell(3, num_kpts, num_nnmax))
826 nnlist(:, :) = 0
827 nncell(:, :, :) = 0
828 nntot = 0
829 IF (iw > 0) THEN
830 CALL wannier_setup(mp_grid, num_kpts, real_lattice, recip_lattice, &
831 kpt_latt, nntot, nnlist, nncell, iw)
832 END IF
833 CALL para_env%sum(nntot)
834 CALL para_env%sum(nnlist)
835 CALL para_env%sum(nncell)
836 END IF
837
838 IF (para_env%is_source() .AND. kpoints_source < w90_kpoints_nnkp) THEN
839 ! Write the ordinary file-export input "seed_name.win".
840 WRITE (filename, '(A,A)') trim(seed_name), ".win"
841 CALL open_file(filename, unit_number=iunit, file_status="REPLACE", file_action="WRITE")
842 !
843 CALL m_timestamp(timestamp)
844 WRITE (iunit, "(A)") "! Wannier90 input file generated by CP2K "
845 WRITE (iunit, "(A,/)") "! Creation date "//timestamp
846 !
847 WRITE (iunit, "(A,I5)") "num_wann = ", num_wann
848 IF (num_bands /= num_wann .OR. use_bloch_phases) THEN
849 WRITE (iunit, "(A,I5)") "num_bands = ", num_bands
850 END IF
851 DO i = 1, 4
852 IF (window_set(i)) WRITE (iunit, '(2A,ES24.16)') trim(window_keywords(i)), ' = ', energy_window(i)*evolt
853 END DO
854 WRITE (iunit, "(/,A,/)") "length_unit = bohr"
855 WRITE (iunit, "(/,A,/)") "! System"
856 WRITE (iunit, "(/,A)") "begin unit_cell_cart"
857 WRITE (iunit, "(A)") "bohr"
858 DO i = 1, 3
859 WRITE (iunit, "(3ES24.16)") cell%hmat(1:3, i)
860 END DO
861 WRITE (iunit, "(A,/)") "end unit_cell_cart"
862 WRITE (iunit, "(/,A)") "begin atoms_cart"
863 WRITE (iunit, "(A)") "bohr"
864 DO i = 1, num_atoms
865 WRITE (iunit, "(A,3F15.10)") atom_symbols(i), atoms_cart(1:3, i)
866 END DO
867 WRITE (iunit, "(A,/)") "end atoms_cart"
868 WRITE (iunit, "(/,A,/)") "! Kpoints"
869 WRITE (iunit, "(/,A,3I6/)") "mp_grid = ", mp_grid(1:3)
870 WRITE (iunit, "(A)") "begin kpoints"
871 DO i = 1, num_kpts
872 WRITE (iunit, "(3ES24.16)") kpt_latt(1:3, i)
873 END DO
874 WRITE (iunit, "(A)") "end kpoints"
875 CALL close_file(iunit)
876 ELSE
877 iunit = -1
878 END IF
879
880 ! calculate bands
881 NULLIFY (qs_env_kp)
882 IF (kpoints_source == w90_kpoints_mp_grid .AND. input_kpoint_symmetry .AND. iw > 0) THEN
883 WRITE (iw, '(T2,A)') &
884 "WANNIER90| Atomic k-point symmetry from the SCF calculation is not reused."
885 WRITE (iw, '(T2,A)') &
886 "WANNIER90| A full Monkhorst-Pack grid is generated for the Wannier90 interface."
887 END IF
888 IF (do_kpoints) THEN
889 ! we already do kpoints
890 qs_env_kp => qs_env
891 ELSE
892 ! we start from gamma point only
893 CALL create_kp_from_gamma(qs_env, qs_env_kp)
894 END IF
895 IF (iw > 0) THEN
896 WRITE (unit=iw, fmt="(/,T2,A)") "Start K-Point Calculation ..."
897 END IF
898 ! The Gamma-to-k-point conversion normalizes the spin dimensions for diagonalization.
899 ! Keep the original band selection, but use the converted MO sizes for its workspace.
900 CALL get_qs_env(qs_env=qs_env_kp, para_env=para_env, blacs_env=blacs_env, mos=mos)
901 CALL kpoint_env_initialize(kpoint, para_env, blacs_env)
902 CALL kpoint_initialize_mos(kpoint, mos, nadd)
903 CALL kpoint_initialize_mo_set(kpoint)
904 !
905 CALL get_qs_env(qs_env=qs_env_kp, sab_orb=sab_nl, dft_control=dft_control)
906 CALL kpoint_init_cell_index(kpoint, sab_nl, para_env, dft_control%nimages)
907 !
908 CALL get_qs_env(qs_env=qs_env_kp, matrix_ks_kp=matrix_ks, matrix_s_kp=matrix_s, &
909 scf_env=scf_env, scf_control=scf_control)
910 full_mesh_diagonalized = .false.
911 reused_scf_mos = .false.
912 reuse_reason = ""
913 aligned_degenerate_blocks = 0
914 aligned_degenerate_max_size = 0
915 aligned_degenerate_min_svalue = 0.0_dp
916 IF (reuse_scf_mos) THEN
917 CALL get_kpoint_info(kpoint=kpoint, cell_to_index=cell_to_index)
918 CALL do_general_diag_kp(matrix_ks, matrix_s, qs_kpoint, scf_env, scf_control, .false., &
919 diis_step)
920 IF (validate_reuse_scf_mos) THEN
921 IF (iw > 0) THEN
922 WRITE (iw, '(T2,A)') &
923 "WANNIER90| Validating SCF MO reuse against a full-mesh diagonalization reference."
924 END IF
925 CALL do_general_diag_kp(matrix_ks, matrix_s, kpoint, scf_env, scf_control, .false., diis_step)
926 full_mesh_diagonalized = .true.
927 CALL save_wannier90_mo_snapshot(kpoint, ispin, para_env, reference_mo, reference_eigenvalues)
928 CALL diagnose_wannier90_scf_reuse_candidates(kpoint, qs_kpoint, matrix_s, matrix_ks, &
929 cell_to_index, sab_nl, para_env, iw, ispin, &
930 reuse_candidate_deviation, &
931 reuse_candidate_min_svalue, &
932 reuse_candidate_metric_deviation, &
933 reuse_candidate_residual)
934 IF (iw > 0 .AND. reuse_candidate_deviation < 1.0e100_dp) THEN
935 WRITE (iw, '(T2,A,ES10.3,A,ES10.3,A,ES10.3,A,ES10.3)') &
936 "WANNIER90| Best atom/AO candidate subspace deviation ", &
937 reuse_candidate_deviation, ", minimum singular value ", &
938 reuse_candidate_min_svalue, ", max metric deviation ", &
939 reuse_candidate_metric_deviation, ", max residual ", reuse_candidate_residual
940 END IF
941 END IF
942 CALL prepare_wannier90_scf_mos(kpoint, qs_kpoint, matrix_s, matrix_ks, cell_to_index, &
943 sab_nl, para_env, reused_scf_mos, reuse_reason, &
944 aligned_degenerate_blocks, aligned_degenerate_max_size, &
945 aligned_degenerate_min_svalue, spin_channel=ispin)
946 IF (validate_reuse_scf_mos) THEN
947 IF (reused_scf_mos) THEN
948 CALL validate_wannier90_reused_mos(kpoint, matrix_s, cell_to_index, sab_nl, &
949 para_env, ispin, reference_mo, &
950 reference_eigenvalues, validate_reuse_ok, &
951 validation_subspace_deviation, validation_min_svalue, &
952 validation_eigenvalue_deviation)
953 IF (iw > 0) THEN
954 WRITE (iw, '(T2,A,ES10.3,A,ES10.3,A,ES10.3)') &
955 "WANNIER90| Reused MO validation: subspace deviation ", &
956 validation_subspace_deviation, ", minimum singular value ", &
957 validation_min_svalue, ", eigenvalue deviation ", &
958 validation_eigenvalue_deviation
959 END IF
960 IF (.NOT. validate_reuse_ok) THEN
961 reused_scf_mos = .false.
962 WRITE (reuse_reason, "(A,ES10.3,A,ES10.3)") &
963 "validation failed: dS=", &
964 validation_subspace_deviation, ", dE=", validation_eigenvalue_deviation
965 END IF
966 END IF
967 IF (.NOT. reused_scf_mos) THEN
968 CALL restore_wannier90_mo_snapshot(kpoint, ispin, reference_mo, &
969 reference_eigenvalues)
970 END IF
971 END IF
972 IF (iw > 0) THEN
973 IF (reused_scf_mos) THEN
974 WRITE (iw, '(T2,A)') &
975 "WANNIER90| Reused SCF MO coefficients for the Wannier90 full k-point mesh."
976 IF (use_bloch_phases) THEN
977 IF (initial_projections == 0) WRITE (iw, '(T2,A)') &
978 "WANNIER90| Wrote identity projections for Bloch-phase complete band subspaces."
979 WRITE (iw, '(T2,A,3F10.6)') &
980 "WANNIER90| Applied Bloch phase gauge to reused overlaps around fractional center", &
981 phase_center(1:3)
982 END IF
983 IF (aligned_degenerate_blocks > 0) THEN
984 WRITE (iw, '(T2,A,I0,A,I0,A,ES10.3)') &
985 "WANNIER90| Ritz-stabilized ", aligned_degenerate_blocks, &
986 " degenerate SCF MO subspace(s) with S(k),H(k); largest block has ", &
987 aligned_degenerate_max_size, " band(s), min metric eigenvalue ", &
988 aligned_degenerate_min_svalue
989 END IF
990 ELSE
991 WRITE (iw, '(T2,A,A)') &
992 "WANNIER90| Could not reuse SCF MOs: ", trim(reuse_reason)
993 WRITE (iw, '(T2,A)') &
994 "WANNIER90| Falling back to full-mesh diagonalization for the Wannier90 files."
995 END IF
996 END IF
997 END IF
998 IF (.NOT. reused_scf_mos .AND. .NOT. full_mesh_diagonalized) THEN
999 CALL do_general_diag_kp(matrix_ks, matrix_s, kpoint, scf_env, scf_control, .false., diis_step)
1000 END IF
1001 IF (ALLOCATED(reference_mo)) DEALLOCATE (reference_mo)
1002 IF (ALLOCATED(reference_eigenvalues)) DEALLOCATE (reference_eigenvalues)
1003 !
1004 IF (iw > 0) THEN
1005 WRITE (iw, '(T69,A)') "... Finished"
1006 END IF
1007 !
1008 ! Calculate and print Overlaps
1009 !
1010 ! create a list of unique b vectors and a table of pointers
1011 ! nblist(ik,i) -> +/- b_latt(1:3,x)
1012 ALLOCATE (nblist(num_kpts, nntot))
1013 ALLOCATE (b_latt(3, num_kpts*nntot))
1014 nblist(:, :) = 0
1015 nbs = 0
1016 DO ik = 1, num_kpts
1017 DO i = 1, nntot
1018 bvec(1:3) = kpt_latt(1:3, nnlist(ik, i)) - kpt_latt(1:3, ik) + nncell(1:3, ik, i)
1019 ibs = 0
1020 DO k = 1, nbs
1021 IF (sum(abs(bvec(1:3) - b_latt(1:3, k))) < 1.e-6_dp) THEN
1022 ibs = k
1023 EXIT
1024 END IF
1025 IF (sum(abs(bvec(1:3) + b_latt(1:3, k))) < 1.e-6_dp) THEN
1026 ibs = -k
1027 EXIT
1028 END IF
1029 END DO
1030 IF (ibs /= 0) THEN
1031 ! old lattice vector
1032 nblist(ik, i) = ibs
1033 ELSE
1034 ! new lattice vector
1035 nbs = nbs + 1
1036 b_latt(1:3, nbs) = bvec(1:3)
1037 nblist(ik, i) = nbs
1038 END IF
1039 END DO
1040 END DO
1041 ! calculate all the operator matrices (a|bvec|b)
1042 overlap_nl => sab_nl
1043 NULLIFY (berry_kpoint)
1044 IF (ordered_berry) THEN
1045 CALL get_qs_env(qs_env_kp, sab_all=overlap_nl)
1046 CALL kpoint_create(berry_kpoint)
1047 CALL kpoint_init_cell_index(berry_kpoint, overlap_nl, para_env, nberry_images)
1048 CALL get_kpoint_info(berry_kpoint, cell_to_index=berry_cell_index)
1049 END IF
1050 IF (.NOT. ASSOCIATED(overlap_nl)) cpabort("Explicit overlaps require k-point neighbour lists.")
1051 ALLOCATE (berry_matrix(nbs))
1052 DO i = 1, nbs
1053 NULLIFY (berry_matrix(i)%cosmat)
1054 NULLIFY (berry_matrix(i)%sinmat)
1055 bvec(1:3) = -twopi*matmul(transpose(cell%h_inv(1:3, 1:3)), b_latt(1:3, i))
1056 CALL build_berry_kpoint_matrix(qs_env_kp, berry_matrix(i)%cosmat, &
1057 berry_matrix(i)%sinmat, bvec, ordered=ordered_berry, ordered_kpoints=berry_kpoint)
1058 END DO
1059 ! work matrices for MOs (all group)
1060 kp => kpoint%kp_env(1)%kpoint_env
1061 CALL get_mo_set(kp%mos(ispin), nmo=nmo)
1062 IF (ordered_berry .AND. nmo /= nscalar) THEN
1063 cpabort("WANNIER90: unexpected orbital count in export.")
1064 END IF
1065 NULLIFY (matrix_struct_ao, matrix_struct_work)
1066 CALL cp_fm_struct_create(matrix_struct_work, nrow_global=nao, &
1067 ncol_global=nmo, &
1068 para_env=para_env, &
1069 context=blacs_env)
1070 CALL cp_cfm_create(fmk1_cfm, matrix_struct_work)
1071 CALL cp_cfm_create(fmk2_cfm, matrix_struct_work)
1072 CALL cp_cfm_create(tmp_cfm, matrix_struct_work)
1073 CALL cp_fm_struct_create(matrix_struct_ao, nrow_global=nao, &
1074 ncol_global=nao, &
1075 para_env=para_env, &
1076 context=blacs_env)
1077 CALL cp_fm_create(mat_real, matrix_struct_ao)
1078 CALL cp_fm_create(mat_imag, matrix_struct_ao)
1079 CALL cp_cfm_create(omat_cfm, matrix_struct_ao)
1080 ! work matrices for Mmn(k,b) integrals
1081 NULLIFY (matrix_struct_mmn)
1082 CALL cp_fm_struct_create(matrix_struct_mmn, nrow_global=nmo, &
1083 ncol_global=nmo, &
1084 para_env=para_env, &
1085 context=blacs_env)
1086 CALL cp_cfm_create(mmn_cfm, matrix_struct_mmn)
1087 ! allocate some work matrices
1088 ALLOCATE (rmatrix_full, cmatrix_full)
1089 CALL dbcsr_create(rmatrix_full, template=matrix_s(1, 1)%matrix, &
1090 matrix_type=dbcsr_type_no_symmetry)
1091 CALL dbcsr_create(cmatrix_full, template=matrix_s(1, 1)%matrix, &
1092 matrix_type=dbcsr_type_no_symmetry)
1093 IF (ordered_berry) THEN
1094 ALLOCATE (loop_real, loop_imag)
1095 CALL dbcsr_create(loop_real, template=matrix_s(1, 1)%matrix, matrix_type=dbcsr_type_no_symmetry)
1096 CALL dbcsr_create(loop_imag, template=matrix_s(1, 1)%matrix, matrix_type=dbcsr_type_no_symmetry)
1097 CALL cp_dbcsr_alloc_block_from_nbl(loop_real, overlap_nl)
1098 CALL cp_dbcsr_alloc_block_from_nbl(loop_imag, overlap_nl)
1099 CALL cp_dbcsr_alloc_block_from_nbl(rmatrix_full, overlap_nl)
1100 CALL cp_dbcsr_alloc_block_from_nbl(cmatrix_full, overlap_nl)
1101 ELSE
1102 CALL cp_dbcsr_alloc_block_from_nbl(rmatrix_full, sab_nl, desymmetrize=.true.)
1103 CALL cp_dbcsr_alloc_block_from_nbl(cmatrix_full, sab_nl, desymmetrize=.true.)
1104 END IF
1105 !
1106 CALL get_kpoint_info(kpoint=kpoint, cell_to_index=cell_to_index)
1107 NULLIFY (cmo_coeff_source)
1108 IF (do_soc .OR. do_parity) THEN
1109 ! Parity needs the overlap work matrices even without SOC, and after SOC is done.
1110 ALLOCATE (rmatrix, cmatrix)
1111 CALL dbcsr_create(rmatrix, template=matrix_s(1, 1)%matrix, matrix_type=dbcsr_type_symmetric)
1112 CALL dbcsr_create(cmatrix, template=matrix_s(1, 1)%matrix, matrix_type=dbcsr_type_antisymmetric)
1113 CALL cp_dbcsr_alloc_block_from_nbl(rmatrix, sab_nl)
1114 CALL cp_dbcsr_alloc_block_from_nbl(cmatrix, sab_nl)
1115 END IF
1116 IF (do_soc) THEN
1117 ! Second variation in the scalar KS eigenbasis. Retain selected spinors at each k.
1118 cpassert(all(kpoint%kp_range == [1, num_kpts]))
1119 NULLIFY (soc_matrices)
1120 CALL v_soc_xyz_from_pseudopotential(qs_env_kp, soc_matrices)
1121 ALLOCATE (soc_xyz(nmo, nmo, 3), soc_h(2*nmo, 2*nmo), soc_u(2*nmo, 2*nmo))
1122 ALLOCATE (scalar_values(2*nmo), spinor_values(2*nmo, num_kpts))
1123 ALLOCATE (spinor_coeff(2*nmo, num_bands, num_kpts), scalar_overlap(nmo, nmo))
1124 DO ik = 1, num_kpts
1125 kp => kpoint%kp_env(ik)%kpoint_env
1126 CALL cp_cfm_copy_general(kp%mos(1)%cmo_coeff, fmk1_cfm, para_env)
1127 CALL get_mo_set(kp%mos(1), eigenvalues=eigenvalues)
1128 scalar_values(1:nmo) = eigenvalues(1:nmo)
1129 scalar_values(nmo + 1:) = eigenvalues(1:nmo)
1130 DO axis = 1, 3
1131 CALL dbcsr_set(rmatrix, 0.0_dp)
1132 CALL dbcsr_set(cmatrix, 0.0_dp)
1133 CALL rskp_transform(cmatrix, rmatrix, rsmat=soc_matrices, ispin=axis, &
1134 xkp=kpoint%xkp(:, ik), cell_to_index=cell_to_index, sab_nl=sab_nl)
1135 CALL dbcsr_scale(rmatrix, -1.0_dp)
1136 CALL dbcsr_desymmetrize(rmatrix, rmatrix_full)
1137 CALL dbcsr_desymmetrize(cmatrix, cmatrix_full)
1138 CALL copy_dbcsr_to_fm(rmatrix_full, mat_real)
1139 CALL copy_dbcsr_to_fm(cmatrix_full, mat_imag)
1140 CALL cp_fm_to_cfm(mat_real, mat_imag, omat_cfm)
1141 CALL parallel_gemm("N", "N", nao, nmo, nao, cmplx(1.0_dp, 0.0_dp, dp), &
1142 omat_cfm, fmk1_cfm, cmplx(0.0_dp, 0.0_dp, dp), tmp_cfm)
1143 CALL parallel_gemm("C", "N", nmo, nmo, nao, cmplx(1.0_dp, 0.0_dp, dp), &
1144 fmk1_cfm, tmp_cfm, cmplx(0.0_dp, 0.0_dp, dp), mmn_cfm)
1145 CALL cp_cfm_get_submatrix(mmn_cfm, soc_xyz(:, :, axis))
1146 END DO
1147 soc_h(1:nmo, 1:nmo) = soc_xyz(:, :, 3)
1148 soc_h(nmo + 1:, nmo + 1:) = -soc_xyz(:, :, 3)
1149 ! Match H_KS_spinor_kp and CP2K's real-spherical angular-momentum convention.
1150 soc_h(1:nmo, nmo + 1:) = soc_xyz(:, :, 1) + cmplx(0.0_dp, 1.0_dp, dp)*soc_xyz(:, :, 2)
1151 soc_h(nmo + 1:, 1:nmo) = soc_xyz(:, :, 1) - cmplx(0.0_dp, 1.0_dp, dp)*soc_xyz(:, :, 2)
1152 DO ib = 1, 2*nmo
1153 soc_h(ib, ib) = soc_h(ib, ib) + scalar_values(ib)
1154 END DO
1155 IF (maxval(abs(soc_h - conjg(transpose(soc_h)))) > 1.e-8_dp) THEN
1156 IF (iw > 0) WRITE (iw, '(T2,A,I0,A,ES18.10)') &
1157 "TOPOLOGY| SOC Hermiticity error at point ", ik, ": ", &
1158 maxval(abs(soc_h - conjg(transpose(soc_h))))
1159 cpabort("WANNIER90 SOC: non-Hermitian second-variational Hamiltonian.")
1160 END IF
1161 CALL diag_complex(soc_h, soc_u, spinor_values(:, ik))
1162 spinor_coeff(:, :, ik) = soc_u(:, band_indices)
1163 END DO
1164 CALL dbcsr_deallocate_matrix_set(soc_matrices)
1165 DEALLOCATE (soc_xyz, soc_h, soc_u, scalar_values)
1166 IF (iw > 0) WRITE (iw, '(T2,A,I0)') "TOPOLOGY| Scalar bands in SOC second variation: ", nmo
1167 END IF
1168 ! Eigenvalues determine the projection windows and the sampled spectral gaps.
1169 kp => kpoint%kp_env(1)%kpoint_env
1170 CALL get_mo_set(kp%mos(ispin), nmo=nmo)
1171 cpassert(nmo >= nscalar)
1172 ALLOCATE (eigval(merge(2*nmo, nmo, do_soc)))
1173 direct_gap = huge(1.0_dp)
1174 valence_max = -huge(1.0_dp)
1175 conduction_min = huge(1.0_dp)
1176 CALL get_kpoint_info(kpoint, nkp=nkp, kp_range=kp_range, xkp=xkp)
1177 IF (para_env%is_source()) THEN
1178 WRITE (filename, '(A,A)') trim(seed_name), ".eig"
1179 CALL open_file(filename, unit_number=iunit, file_status="REPLACE", file_action="WRITE")
1180 ELSE
1181 iunit = -1
1182 END IF
1183 !
1184 DO ik = 1, nkp
1185 my_kpgrp = (ik >= kp_range(1) .AND. ik <= kp_range(2))
1186 IF (do_soc) THEN
1187 eigval(:) = spinor_values(:, ik)
1188 ELSE IF (my_kpgrp) THEN
1189 ikpgr = ik - kp_range(1) + 1
1190 kp => kpoint%kp_env(ikpgr)%kpoint_env
1191 CALL get_mo_set(kp%mos(ispin), eigenvalues=eigenvalues)
1192 eigval(1:nmo) = eigenvalues(1:nmo)
1193 ELSE
1194 eigval(1:nmo) = 0.0_dp
1195 END IF
1196 IF (.NOT. do_soc) CALL kpoint%para_env_inter_kp%sum(eigval)
1197 IF (.NOT. ordered_berry .AND. nexcl > 0) THEN
1198 DO ib = 1, num_bands_tot - 1
1199 IF (keep_band(ib) .EQV. keep_band(ib + 1)) cycle
1200 ! Degenerate states outside the outer window cannot enter the Wannier subspace.
1201 kept_index = merge(ib, ib + 1, keep_band(ib))
1202 IF (eigval(kept_index) < energy_window(1) .OR. eigval(kept_index) > energy_window(2)) cycle
1203 IF (abs(eigval(ib + 1) - eigval(ib)) < 1.0e-8_dp) THEN
1204 cpabort("WANNIER90 EXCLUDE_BANDS must retain or remove complete degenerate subspaces.")
1205 END IF
1206 END DO
1207 END IF
1208 IF (do_wilson) THEN
1209 IF (lowest_bands) THEN
1210 valence_max = max(valence_max, eigval(num_bands))
1211 conduction_min = min(conduction_min, eigval(num_bands + 1))
1212 END IF
1213 DO ib = 1, num_bands_tot - 1
1214 IF (any(band_indices == ib) .NEQV. any(band_indices == ib + 1)) THEN
1215 pair_gap = eigval(ib + 1) - eigval(ib)
1216 direct_gap = min(direct_gap, pair_gap)
1217 END IF
1218 END DO
1219 END IF
1220 eigval(:) = eigval*evolt
1221 IF (any(window_set)) THEN
1222 projection_eigenvalues(:, ik) = eigval(band_indices)
1223 CALL wannier90_window_mask(projection_eigenvalues(:, ik), num_wann, energy_window*evolt, &
1224 window_set, allowed_band)
1225 END IF
1226 ! output
1227 IF (iunit > 0) THEN
1228 DO ib = 1, num_bands
1229 WRITE (iunit, "(2I8,F24.14)") ib, ik, eigval(band_indices(ib))
1230 END DO
1231 END IF
1232 END DO
1233 IF (iunit > 0) THEN
1234 CALL close_file(iunit)
1235 END IF
1236 IF (initial_projections > 0 .OR. projection_candidates > 0) THEN
1237 CALL build_wannier90_ao_projections(kpoint, matrix_s, sab_nl, para_env, ispin, band_indices, &
1238 reused_scf_mos .AND. use_bloch_phases, phase_center, ao_projections)
1239 IF (export_ao) THEN
1240 CALL write_wannier90_amn(ao_projections, para_env, trim(seed_name)//"_ao_candidates.amn")
1241 END IF
1242 IF (initial_projections == 2 .OR. export_hybrids) THEN
1243 CALL build_wannier90_hybrid_groups(qs_env, hybrid_groups)
1244 CALL wannier90_hybrid_candidates(ao_projections, hybrid_groups, hybrid_projections)
1245 DEALLOCATE (hybrid_groups)
1246 IF (export_hybrids) THEN
1247 CALL write_wannier90_amn(hybrid_projections, para_env, trim(seed_name)//"_hybrid_candidates.amn")
1248 IF (iw > 0) WRITE (iw, '(T2,A,I0)') &
1249 "WANNIER90| Exported hybrid projection candidates ", SIZE(hybrid_projections, 2)
1250 END IF
1251 IF (initial_projections == 2) THEN
1252 CALL move_alloc(hybrid_projections, ao_projections)
1253 IF (iw > 0) WRITE (iw, '(T2,A)') "WANNIER90| Selecting from the full radial s/p hybrid candidate bank."
1254 ELSE
1255 DEALLOCATE (hybrid_projections)
1256 END IF
1257 END IF
1258 END IF
1259 IF (.NOT. ordered_berry .AND. initial_projections >= 0) THEN
1260 ALLOCATE (initial_amn(num_bands, num_wann, num_kpts))
1261 IF (initial_projections == 0) THEN
1262 initial_amn(:, :, :) = cmplx(0.0_dp, 0.0_dp, kind=dp)
1263 DO i = 1, num_wann
1264 initial_amn(i, i, :) = cmplx(1.0_dp, 0.0_dp, kind=dp)
1265 END DO
1266 ELSE
1267 IF (para_env%is_source() .AND. any(window_set)) THEN
1268 DO ik = 1, num_kpts
1269 CALL wannier90_window_mask(projection_eigenvalues(:, ik), num_wann, energy_window*evolt, &
1270 window_set, allowed_band)
1271 DO ib = 1, num_bands
1272 IF (.NOT. allowed_band(ib)) ao_projections(ib, :, ik) = cmplx(0.0_dp, 0.0_dp, dp)
1273 END DO
1274 END DO
1275 END IF
1276 ALLOCATE (projection_pivots(num_wann))
1277 CALL wannier90_select_projections(ao_projections, para_env, initial_amn, projection_pivots, &
1278 projection_min_svalue)
1279 IF (iw > 0) THEN
1280 WRITE (iw, '(T2,A,ES12.4)') "WANNIER90| Minimum projection singular value ", projection_min_svalue
1281 WRITE (iw, '(T2,A,*(I6))') "WANNIER90| Selected projection candidates ", projection_pivots
1282 END IF
1283 IF (projection_min_svalue < 1.0e-10_dp) THEN
1284 cpabort("No full-rank Wannier90 AO trial set was found on the entire k-point mesh.")
1285 END IF
1286 DEALLOCATE (projection_pivots)
1287 END IF
1288 CALL write_wannier90_amn(initial_amn, para_env, trim(seed_name)//".amn")
1289 DEALLOCATE (initial_amn)
1290 END IF
1291 IF (ALLOCATED(ao_projections)) DEALLOCATE (ao_projections)
1292 IF (para_env%is_source()) THEN
1293 WRITE (filename, '(A,A)') trim(seed_name), ".mmn"
1294 CALL open_file(filename, unit_number=iunit, file_status="REPLACE", file_action="WRITE")
1295 CALL m_timestamp(timestamp)
1296 WRITE (iunit, "(A)") "! Wannier90 file generated by CP2K "//timestamp
1297 WRITE (iunit, "(3I8)") num_bands, num_kpts, nntot
1298 ELSE
1299 iunit = -1
1300 END IF
1301 ! loop over all k-points
1302 DO ik = 1, num_kpts
1303 ! get the MO coefficients for this k-point
1304 my_kpgrp = (ik >= kpoint%kp_range(1) .AND. ik <= kpoint%kp_range(2))
1305 IF (my_kpgrp) THEN
1306 ikk = ik - kpoint%kp_range(1) + 1
1307 kp => kpoint%kp_env(ikk)%kpoint_env
1308 cmo_coeff_source => kp%mos(ispin)%cmo_coeff
1309 ELSE
1310 NULLIFY (cmo_coeff_source, kp)
1311 END IF
1312 CALL wannier90_copy_cfm_general(cmo_coeff_source, fmk1_cfm, para_env)
1313 ! loop over all connected neighbors
1314 DO i = 1, nntot
1315 ! get the MO coefficients for the connected k-point
1316 ik2 = nnlist(ik, i)
1317 mygrp = (ik2 >= kpoint%kp_range(1) .AND. ik2 <= kpoint%kp_range(2))
1318 IF (mygrp) THEN
1319 ikk = ik2 - kpoint%kp_range(1) + 1
1320 kp => kpoint%kp_env(ikk)%kpoint_env
1321 cmo_coeff_source => kp%mos(ispin)%cmo_coeff
1322 ELSE
1323 NULLIFY (cmo_coeff_source, kp)
1324 END IF
1325 CALL wannier90_copy_cfm_general(cmo_coeff_source, fmk2_cfm, para_env)
1326 !
1327 ! transfer realspace overlaps to connected k-point
1328 ibs = nblist(ik, i)
1329 ksign = sign(1.0_dp, real(ibs, kind=dp))
1330 ibs = abs(ibs)
1331 IF (ordered_berry) THEN
1332 ! The cross-k AO operator is not Hermitian. Retain ordered atom pairs
1333 ! and use exp(i*k'.R) [cos(b.r) - i*sin(b.r)] without symmetry shortcuts.
1334 CALL dbcsr_set(rmatrix_full, 0.0_dp)
1335 CALL dbcsr_set(cmatrix_full, 0.0_dp)
1336 CALL dbcsr_set(loop_real, 0.0_dp)
1337 CALL dbcsr_set(loop_imag, 0.0_dp)
1338 CALL rskp_transform(rmatrix_full, cmatrix_full, berry_matrix(ibs)%cosmat, 1, &
1339 kpoint%xkp(:, ik2), berry_cell_index, overlap_nl)
1340 CALL rskp_transform(loop_real, loop_imag, berry_matrix(ibs)%sinmat, 1, &
1341 kpoint%xkp(:, ik2), berry_cell_index, overlap_nl, rs_sign=ksign)
1342 CALL dbcsr_add(rmatrix_full, loop_imag, 1.0_dp, -1.0_dp)
1343 CALL dbcsr_add(cmatrix_full, loop_real, 1.0_dp, 1.0_dp)
1344 ELSE
1345 CALL transform_wannier90_overlap(berry_matrix(ibs)%cosmat, berry_matrix(ibs)%sinmat, &
1346 ksign, kpoint%xkp(:, ik), kpoint%xkp(:, ik2), &
1347 cell_to_index, sab_nl, rmatrix_full, cmatrix_full)
1348 END IF
1349 !
1350 ! calculate M_(mn)^(k,b) = C(k)^H O(k,b) C(k+b)
1351 CALL copy_dbcsr_to_fm(rmatrix_full, mat_real)
1352 CALL copy_dbcsr_to_fm(cmatrix_full, mat_imag)
1353 CALL cp_fm_to_cfm(mat_real, mat_imag, omat_cfm)
1354 CALL parallel_gemm("N", "N", nao, nmo, nao, cmplx(1.0_dp, 0.0_dp, kind=dp), &
1355 omat_cfm, fmk2_cfm, cmplx(0.0_dp, 0.0_dp, kind=dp), tmp_cfm)
1356 CALL parallel_gemm("C", "N", nmo, nmo, nao, cmplx(1.0_dp, 0.0_dp, kind=dp), &
1357 fmk1_cfm, tmp_cfm, cmplx(0.0_dp, 0.0_dp, kind=dp), mmn_cfm)
1358 IF (do_soc) THEN
1359 CALL cp_cfm_get_submatrix(mmn_cfm, scalar_overlap)
1360 link_matrix(:, :) = matmul(conjg(transpose(spinor_coeff(1:nmo, :, ik))), &
1361 matmul(scalar_overlap, spinor_coeff(1:nmo, :, ik2))) + &
1362 matmul(conjg(transpose(spinor_coeff(nmo + 1:, :, ik))), &
1363 matmul(scalar_overlap, spinor_coeff(nmo + 1:, :, ik2)))
1364 END IF
1365 !
1366 ! write to output file
1367 IF (reused_scf_mos .AND. use_bloch_phases) THEN
1368 ! Reused SCF MOs need the same global Bloch gauge in every overlap block.
1369 gauge_arg = twopi*dot_product(kpoint%xkp(1:3, ik2) - kpoint%xkp(1:3, ik), &
1370 phase_center(1:3))
1371 gauge_real = cos(gauge_arg)
1372 gauge_imag = sin(gauge_arg)
1373 ELSE
1374 gauge_real = 1.0_dp
1375 gauge_imag = 0.0_dp
1376 END IF
1377 IF (iunit > 0) THEN
1378 WRITE (iunit, "(2I8,3I5)") ik, ik2, nncell(1:3, ik, i)
1379 END IF
1380 DO ib2 = 1, num_bands
1381 DO ib1 = 1, num_bands
1382 IF (do_soc) THEN
1383 rmmn = real(link_matrix(ib1, ib2), dp)
1384 cmmn = aimag(link_matrix(ib1, ib2))
1385 ELSE
1386 CALL cp_cfm_get_element(mmn_cfm, band_indices(ib1), band_indices(ib2), zmmn)
1387 rmmn = real(zmmn, kind=dp)
1388 cmmn = aimag(zmmn)
1389 END IF
1390 gauge_tmp = gauge_real*rmmn - gauge_imag*cmmn
1391 cmmn = gauge_imag*rmmn + gauge_real*cmmn
1392 rmmn = gauge_tmp
1393 link_matrix(ib1, ib2) = cmplx(rmmn, cmmn, dp)
1394 IF (iunit > 0) THEN
1395 WRITE (iunit, "(2E30.14)") rmmn, cmmn
1396 END IF
1397 END DO
1398 END DO
1399 IF (do_wilson) THEN
1400 iloop = loop_index(ik)
1401 CALL wilson_step(wilson_product(:, :, iloop), link_matrix, link_sv, status, 1.e-10_dp)
1402 IF (status /= 0) THEN
1403 cpabort("Wilson link singular or SVD failed; refine sampling and check subspace.")
1404 END IF
1405 loop_sv(iloop) = min(loop_sv(iloop), link_sv)
1406 END IF
1407 !
1408 END DO
1409 END DO
1410 ! Optional full-state snapshots, before the distributed work matrices are released.
1411 IF (export_state .OR. do_parity) THEN
1412 state_components = 1
1413 IF (do_soc) state_components = 2
1414 ALLOCATE (export_scalar(nao, nscalar), export_coeff(nao*state_components, num_bands))
1415 state_unit = -1
1416 IF (export_state) CALL topology_state_begin(qs_env_kp, trim(seed_name)//".topology", nao, num_bands, num_kpts, &
1417 state_components, num_bands_tot, spin_channel, band_indices, state_unit)
1418 IF (do_parity) THEN
1419 cpassert(all(kpoint%kp_range == [1, num_kpts]))
1420 ALLOCATE (parity_metric(nao, nao), parity_phase(nao), parity_map(nao), &
1421 parity_odd(2**tqc_dimension), ebr_coefficients(2, 2**tqc_dimension), &
1422 parity_energies(num_bands_tot))
1423 parity_odd = -1
1424 IF (do_tqc) THEN
1425 IF (num_bands /= nelectron .OR. mod(num_bands, 2) /= 0 .OR. &
1426 any(band_indices /= [(i, i=1, num_bands)])) THEN
1427 cpabort("INVERSION_TQC requires the full occupied spinor band prefix.")
1428 END IF
1429 END IF
1430 END IF
1431 DO ik = 1, num_kpts
1432 kp => kpoint%kp_env(ik)%kpoint_env
1433 CALL cp_cfm_copy_general(kp%mos(spin_channel)%cmo_coeff, fmk1_cfm, para_env)
1434 CALL cp_cfm_get_submatrix(fmk1_cfm, export_scalar)
1435 IF (do_soc) THEN
1436 export_coeff(:nao, :) = matmul(export_scalar, spinor_coeff(:nscalar, :, ik))
1437 export_coeff(nao + 1:, :) = matmul(export_scalar, spinor_coeff(nscalar + 1:, :, ik))
1438 CALL topology_state_point(state_unit, ik, kpt_latt(:, ik), spinor_values(:, ik), export_coeff)
1439 IF (do_parity) parity_energies(:) = spinor_values(:, ik)
1440 ELSE
1441 export_coeff(:, :) = export_scalar(:, band_indices)
1442 CALL get_mo_set(kp%mos(spin_channel), eigenvalues=eigenvalues)
1443 CALL topology_state_point(state_unit, ik, kpt_latt(:, ik), eigenvalues(:nscalar), export_coeff)
1444 IF (do_parity) parity_energies(:) = eigenvalues(:nscalar)
1445 END IF
1446 IF (.NOT. do_parity) cycle
1447 parity_gap = huge(1.0_dp)
1448 DO jpar = 1, num_bands_tot
1449 IF (any(band_indices == jpar)) cycle
1450 parity_gap = min(parity_gap, minval(abs(parity_energies(band_indices) - parity_energies(jpar))))
1451 END DO
1452 IF (num_bands == num_bands_tot .OR. parity_gap <= gap_tol) THEN
1453 cpabort("PARITY requires an isolated selected subspace and computed excluded bands.")
1454 END IF
1455 IF (maxval(abs(2*kpt_latt(:, ik) - nint(2*kpt_latt(:, ik)))) > 1.e-8_dp) cycle
1456 CALL gaussian_inversion_action(qs_env_kp, kpt_latt(:, ik), parity_origin, parity_map, &
1457 parity_phase, parity_tolerance, status)
1458 IF (status /= 0) THEN
1459 cpabort("PARITY: inversion does not preserve geometry and atomic kinds.")
1460 END IF
1461 CALL dbcsr_set(rmatrix, 0.0_dp)
1462 CALL dbcsr_set(cmatrix, 0.0_dp)
1463 CALL rskp_transform(rmatrix, cmatrix, rsmat=matrix_s, ispin=1, xkp=kpoint%xkp(:, ik), &
1464 cell_to_index=cell_to_index, sab_nl=sab_nl)
1465 CALL dbcsr_desymmetrize(rmatrix, rmatrix_full)
1466 CALL dbcsr_desymmetrize(cmatrix, cmatrix_full)
1467 CALL copy_dbcsr_to_fm(rmatrix_full, mat_real)
1468 CALL copy_dbcsr_to_fm(cmatrix_full, mat_imag)
1469 CALL cp_fm_to_cfm(mat_real, mat_imag, omat_cfm)
1470 CALL cp_cfm_get_submatrix(omat_cfm, parity_metric)
1471 CALL inversion_representation(parity_metric, export_coeff, parity_map, parity_phase, &
1472 parity_energies(band_indices), do_soc .AND. time_reversal, parity_tolerance, &
1473 parity_energy_tolerance, counts, parity_error, parity_energy_error, status, parity_checks)
1474 IF (iw > 0) THEN
1475 WRITE (iw, '(T2,A,3F10.5,A,2I8,A,3ES14.5)') 'TQC| TRIM ', kpt_latt(:, ik), &
1476 ' even/odd states ', counts, ' residuals/gap ', parity_error, parity_energy_error, parity_gap
1477 WRITE (iw, '(T2,A,4ES14.5)') 'TQC| Metric/normalization/inversion/TR residuals: ', parity_checks
1478 END IF
1479 IF (status /= 0) THEN
1480 cpabort("PARITY: metric, subspace or symmetry representation check failed.")
1481 END IF
1482 IF (tqc_dimension == 2 .AND. modulo(nint(2*kpt_latt(3, ik)), 2) /= 0) cycle
1483 trim_id = 1
1484 DO axis = 1, tqc_dimension
1485 trim_id = trim_id + 2**(axis - 1)*modulo(nint(2*kpt_latt(axis, ik)), 2)
1486 END DO
1487 IF (parity_odd(trim_id) >= 0 .AND. parity_odd(trim_id) /= counts(2)) THEN
1488 cpabort("PARITY: repeated equivalent TRIM have inconsistent multiplicities.")
1489 END IF
1490 parity_odd(trim_id) = counts(2)
1491 END DO
1492 IF (state_unit > 0) CALL close_file(state_unit)
1493 IF (do_parity) THEN
1494 IF (all(parity_odd < 0)) THEN
1495 cpabort("PARITY requires a TRIM in the requested dimension.")
1496 END IF
1497 END IF
1498 IF (do_tqc) THEN
1499 IF (any(parity_odd < 0)) THEN
1500 cpabort("INVERSION_TQC requires all four (2D) or eight (3D) TRIM.")
1501 END IF
1502 IF (any(mod(parity_odd, 2) /= 0)) THEN
1503 cpabort("INVERSION_TQC requires whole odd-parity Kramers pairs.")
1504 END IF
1505 parity_odd(:) = parity_odd/2
1506 CALL inversion_indicators(parity_odd, num_bands/2, tqc_dimension, strong, weak, z4, status)
1507 IF (status /= 0) cpabort("Invalid inversion indicator data.")
1508 CALL inversion_ebr_signature(parity_odd, num_bands/2, signed_atomic, nonnegative_atomic, &
1509 ebr_coefficients, status)
1510 IF (status /= 0) cpabort("Invalid inversion EBR data.")
1511 IF (iw > 0) THEN
1512 WRITE (iw, '(T2,A,I0)') 'TQC| Inversion-subgroup dimension: ', tqc_dimension
1513 WRITE (iw, '(T2,A,I0)') 'TQC| Fu-Kane parity index: ', strong
1514 IF (tqc_dimension == 3) THEN
1515 WRITE (iw, '(T2,A,3I3)') 'TQC| Weak parity indices: ', weak
1516 WRITE (iw, '(T2,A,I0)') 'TQC| Inversion Z4 (sum odd pairs mod 4): ', z4
1517 END IF
1518 WRITE (iw, '(T2,A,L1)') 'TQC| Signed atomic signature: ', signed_atomic
1519 WRITE (iw, '(T2,A,L1)') 'TQC| Nonnegative atomic signature: ', nonnegative_atomic
1520 IF (nonnegative_atomic) THEN
1521 DO i = 1, SIZE(parity_odd)
1522 WRITE (iw, '(T2,A,I0,A,2I8)') 'TQC| Center bit index ', i - 1, &
1523 ' even/odd EBR pairs ', ebr_coefficients(:, i)
1524 END DO
1525 WRITE (iw, '(T2,A)') 'TQC| Atomic-compatible symmetry signature; not a proof of trivial topology.'
1526 ELSE IF (signed_atomic) THEN
1527 WRITE (iw, '(T2,A)') 'TQC| Nonnegative EBR obstruction; '// &
1528 'exclude hidden stable topology before calling it fragile.'
1529 ELSE
1530 WRITE (iw, '(T2,A)') 'TQC| Stable inversion-symmetry indicator obstruction.'
1531 END IF
1532 WRITE (iw, '(T2,A)') 'TQC| TRIM checks do not establish a bulk gap; verify band isolation over the BZ.'
1533 WRITE (iw, '(T2,A)') 'TQC| Converge the scalar-state basis used for second-variational SOC.'
1534 END IF
1535 END IF
1536 END IF
1537 DO i = 1, nbs
1538 CALL dbcsr_deallocate_matrix_set(berry_matrix(i)%cosmat)
1539 CALL dbcsr_deallocate_matrix_set(berry_matrix(i)%sinmat)
1540 END DO
1541 DEALLOCATE (berry_matrix)
1542 CALL cp_fm_struct_release(matrix_struct_work)
1543 CALL cp_cfm_release(fmk1_cfm)
1544 CALL cp_cfm_release(fmk2_cfm)
1545 CALL cp_cfm_release(tmp_cfm)
1546 CALL cp_fm_struct_release(matrix_struct_ao)
1547 CALL cp_fm_release(mat_real)
1548 CALL cp_fm_release(mat_imag)
1549 CALL cp_cfm_release(omat_cfm)
1550 CALL cp_fm_struct_release(matrix_struct_mmn)
1551 CALL cp_cfm_release(mmn_cfm)
1552 CALL dbcsr_deallocate_matrix(rmatrix_full)
1553 CALL dbcsr_deallocate_matrix(cmatrix_full)
1554 IF (do_soc .OR. do_parity) THEN
1555 CALL dbcsr_deallocate_matrix(rmatrix)
1556 CALL dbcsr_deallocate_matrix(cmatrix)
1557 END IF
1558 IF (ordered_berry) THEN
1559 CALL dbcsr_deallocate_matrix(loop_real)
1560 CALL dbcsr_deallocate_matrix(loop_imag)
1561 END IF
1562 !
1563 IF (iunit > 0) THEN
1564 CALL close_file(iunit)
1565 END IF
1566 !
1567 IF (do_wilson) THEN
1568 IF (iw > 0 .AND. num_bands < num_bands_tot) WRITE (iw, '(T2,A,ES18.10)') &
1569 "TOPOLOGY| Minimum sampled subspace gap [eV]: ", direct_gap*evolt
1570 IF (direct_gap < gap_tol) cpabort("Wilson subspace is not isolated on the sampled points.")
1571 IF (lowest_bands) THEN
1572 IF (iw > 0) THEN
1573 WRITE (iw, '(T2,A,ES18.10)') &
1574 "TOPOLOGY| Maximum selected-band energy [eV]: ", valence_max*evolt
1575 WRITE (iw, '(T2,A,ES18.10)') &
1576 "TOPOLOGY| Minimum excluded-band energy [eV]: ", conduction_min*evolt
1577 WRITE (iw, '(T2,A,ES18.10)') &
1578 "TOPOLOGY| Sampled indirect gap [eV]: ", (conduction_min - valence_max)*evolt
1579 IF (conduction_min - valence_max <= gap_tol) WRITE (iw, '(T2,A)') &
1580 "TOPOLOGY| Isolated band subspace, but no resolved common spectral gap on these samples."
1581 END IF
1582 IF (require_global_gap .AND. conduction_min - valence_max <= gap_tol) THEN
1583 cpabort("No positive sampled indirect gap above the selected bands.")
1584 END IF
1585 END IF
1586 DO iloop = 1, nloop
1587 CALL wilson_spectrum(wilson_product(:, :, iloop), wcc_out(:, iloop), berry_phase, status)
1588 IF (status /= 0) cpabort("Wilson eigenvalue calculation failed.")
1589 IF (iw > 0) WRITE (iw, '(T2,A,I0,A,F18.12,A,ES12.4)') &
1590 "TOPOLOGY| Loop ", iloop, " Berry phase [rad]: ", berry_phase, " min singular value: ", loop_sv(iloop)
1591 END DO
1592 IF (para_env%is_source()) THEN
1593 CALL open_file(trim(seed_name)//".wilson", unit_number=iunit, file_status="UNKNOWN", file_action="WRITE")
1594 WRITE (iunit, '(A)') "# loop, minimum link singular value, sorted WCC (arg(lambda)/(2*pi))"
1595 DO iloop = 1, nloop
1596 WRITE (iunit, '(I8,*(1X,ES24.16))') iloop, loop_sv(iloop), wcc_out(:, iloop)
1597 END DO
1598 CALL close_file(iunit)
1599 END IF
1600 IF (do_z2) THEN
1601 CALL z2_from_wcc(wcc_out, z2_value, status, 1.e-5_dp)
1602 IF (status /= 0 .AND. iw > 0) WRITE (iw, '(T2,A,I0)') &
1603 "TOPOLOGY| Kramers/crossing check requests refinement, status: ", status
1604 IF (iw > 0) WRITE (iw, '(T2,A,I0)') "TOPOLOGY| Z2 candidate (before sampling convergence): ", z2_value
1605 END IF
1606 IF (do_chern) THEN
1607 CALL chern_from_wcc(wcc_out, chern_value, chern_winding, status, 1.e-5_dp)
1608 IF (status /= 0) THEN
1609 chern_value = huge(0)
1610 IF (iw > 0) WRITE (iw, '(T2,A,I0)') &
1611 "TOPOLOGY| Chern closure/winding check requests refinement, status: ", status
1612 ELSE IF (iw > 0) THEN
1613 WRITE (iw, '(T2,A,I0,A,F18.12)') &
1614 "TOPOLOGY| First Chern candidate: ", chern_value, ", winding: ", chern_winding
1615 END IF
1616 END IF
1617 END IF
1618 ! clean up
1619 IF (ordered_berry) CALL kpoint_release(berry_kpoint)
1620 DEALLOCATE (kpt_latt, atoms_cart, atom_symbols, eigval)
1621 DEALLOCATE (nnlist, nncell)
1622 DEALLOCATE (nblist, b_latt)
1623 IF (para_env%is_source()) THEN
1624 ! Also replace an earlier exclusion map when the seed is reused without exclusions.
1625 CALL open_file(trim(seed_name)//'_band_indices.dat', unit_number=iunit, &
1626 file_status='REPLACE', file_action='WRITE')
1627 WRITE (iunit, '(A)') '# Exported band, original MO index (before EXCLUDE_BANDS)'
1628 DO ib = 1, num_bands
1629 WRITE (iunit, '(2I10)') ib, band_indices(ib)
1630 END DO
1631 CALL close_file(iunit)
1632 END IF
1633 IF (nexcl > 0) DEALLOCATE (exclude_bands)
1634 DEALLOCATE (band_indices, keep_band, allowed_band)
1635 IF (ALLOCATED(projection_eigenvalues)) DEALLOCATE (projection_eigenvalues)
1636 IF (do_kpoints) THEN
1637 NULLIFY (qs_env_kp)
1638 ELSE
1639 CALL qs_env_release(qs_env_kp)
1640 DEALLOCATE (qs_env_kp)
1641 NULLIFY (qs_env_kp)
1642 END IF
1643
1644 CALL kpoint_release(kpoint)
1645
1646 END SUBROUTINE wannier90_files
1647
1648! **************************************************************************************************
1649!> \brief Reconstruct a full Wannier90 k-point MO set from the SCF k-point MOs.
1650!> \param kpoint full Wannier90 export k-point object
1651!> \param qs_kpoint SCF k-point object
1652!> \param matrix_s real-space overlap matrix
1653!> \param matrix_ks real-space Kohn-Sham matrix
1654!> \param cell_to_index real-space cell index table
1655!> \param sab_nl overlap neighbor list
1656!> \param para_env global parallel environment
1657!> \param success true if all full-mesh MOs were reconstructed
1658!> \param reason diagnostic message when reconstruction is not possible
1659!> \param aligned_degenerate_blocks number of aligned degenerate MO blocks
1660!> \param aligned_degenerate_max_size largest aligned degenerate MO block
1661!> \param aligned_degenerate_min_svalue smallest S(k)-metric subspace singular value
1662!> \param spin_channel Optional single spin channel; otherwise reconstruct all channels.
1663! **************************************************************************************************
1664 SUBROUTINE prepare_wannier90_scf_mos(kpoint, qs_kpoint, matrix_s, matrix_ks, cell_to_index, &
1665 sab_nl, para_env, success, reason, aligned_degenerate_blocks, &
1666 aligned_degenerate_max_size, aligned_degenerate_min_svalue, spin_channel)
1667 TYPE(kpoint_type), POINTER :: kpoint, qs_kpoint
1668 TYPE(dbcsr_p_type), POINTER :: matrix_s(:, :), matrix_ks(:, :)
1669 INTEGER, POINTER :: cell_to_index(:, :, :)
1670 TYPE(neighbor_list_set_p_type), POINTER :: sab_nl(:)
1671 TYPE(mp_para_env_type), POINTER :: para_env
1672 LOGICAL, INTENT(OUT) :: success
1673 CHARACTER(LEN=*), INTENT(OUT) :: reason
1674 INTEGER, INTENT(OUT) :: aligned_degenerate_blocks, &
1675 aligned_degenerate_max_size
1676 REAL(kind=dp), INTENT(OUT) :: aligned_degenerate_min_svalue
1677 INTEGER, INTENT(IN), OPTIONAL :: spin_channel
1678
1679 INTEGER :: blocks, first_spin, ispin, last_spin, &
1680 max_size
1681 REAL(kind=dp) :: min_svalue
1682
1683 success = .false.
1684 reason = 'Wannier90 export k-point MOs are not initialized'
1685 aligned_degenerate_blocks = 0
1686 aligned_degenerate_max_size = 0
1687 aligned_degenerate_min_svalue = 0.0_dp
1688 IF (.NOT. ASSOCIATED(kpoint)) RETURN
1689 IF (.NOT. ASSOCIATED(kpoint%kp_env)) RETURN
1690 IF (SIZE(kpoint%kp_env) == 0) RETURN
1691 first_spin = 1
1692 last_spin = SIZE(kpoint%kp_env(1)%kpoint_env%mos)
1693 IF (PRESENT(spin_channel)) THEN
1694 cpassert(spin_channel >= 1 .AND. spin_channel <= last_spin)
1695 first_spin = spin_channel
1696 last_spin = spin_channel
1697 END IF
1698 DO ispin = first_spin, last_spin
1699 CALL prepare_wannier90_spin_mos(kpoint, qs_kpoint, matrix_s, matrix_ks, cell_to_index, &
1700 sab_nl, para_env, ispin, success, reason, blocks, max_size, min_svalue)
1701 IF (.NOT. success) RETURN
1702 IF (blocks > 0) THEN
1703 IF (aligned_degenerate_blocks == 0) aligned_degenerate_min_svalue = min_svalue
1704 aligned_degenerate_min_svalue = min(aligned_degenerate_min_svalue, min_svalue)
1705 aligned_degenerate_blocks = aligned_degenerate_blocks + blocks
1706 aligned_degenerate_max_size = max(aligned_degenerate_max_size, max_size)
1707 END IF
1708 END DO
1709 END SUBROUTINE prepare_wannier90_scf_mos
1710
1711! **************************************************************************************************
1712!> \brief Reconstruct one spin channel with its own MO dimensions and Hamiltonian.
1713!> \param kpoint Full Wannier90 export k-point object.
1714!> \param qs_kpoint Source SCF k-point object.
1715!> \param matrix_s Real-space overlap matrices.
1716!> \param matrix_ks Spin-dependent real-space Hamiltonians.
1717!> \param cell_to_index Real-space cell index table.
1718!> \param sab_nl Overlap neighbour list.
1719!> \param para_env Global parallel environment.
1720!> \param ispin Selected spin channel.
1721!> \param success Whether all MOs in this channel were reconstructed.
1722!> \param reason Explanation if reconstruction is unavailable.
1723!> \param aligned_degenerate_blocks Number of aligned blocks.
1724!> \param aligned_degenerate_max_size Largest aligned block.
1725!> \param aligned_degenerate_min_svalue Smallest metric singular value.
1726! **************************************************************************************************
1727 SUBROUTINE prepare_wannier90_spin_mos(kpoint, qs_kpoint, matrix_s, matrix_ks, cell_to_index, &
1728 sab_nl, para_env, ispin, success, reason, aligned_degenerate_blocks, &
1729 aligned_degenerate_max_size, &
1730 aligned_degenerate_min_svalue)
1731 TYPE(kpoint_type), POINTER :: kpoint, qs_kpoint
1732 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s, matrix_ks
1733 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
1734 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
1735 POINTER :: sab_nl
1736 TYPE(mp_para_env_type), POINTER :: para_env
1737 INTEGER, INTENT(IN) :: ispin
1738 LOGICAL, INTENT(OUT) :: success
1739 CHARACTER(LEN=*), INTENT(OUT) :: reason
1740 INTEGER, INTENT(OUT) :: aligned_degenerate_blocks, &
1741 aligned_degenerate_max_size
1742 REAL(kind=dp), INTENT(OUT) :: aligned_degenerate_min_svalue
1743
1744 CHARACTER(LEN=default_string_length) :: best_reason, candidate_reason
1745 INTEGER :: aligned_blocks, aligned_max_size, candidate_aligned_blocks, &
1746 candidate_aligned_max_size, ik, ikpgr, ikred, isym_try, min_gap_band, min_gap_kpoint, &
1747 min_gap_spin, nao, nao_src, nmo, nmo_src, nsymmetry, num_candidates
1748 INTEGER, ALLOCATABLE, DIMENSION(:) :: source_kpoint, sym_index
1749 INTEGER, DIMENSION(2) :: kp_range, source_kp_range
1750 LOGICAL :: my_kpgrp, my_source_kpgrp, ok, &
1751 source_window
1752 REAL(kind=dp) :: aligned_min_svalue, band_gap, best_residual, candidate_residual, &
1753 degenerate_band_tol, local_min_band_gap, min_band_gap, source_owner_count, &
1754 source_window_min_svalue
1755 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eigenvalues_buffer, occupation_buffer, &
1756 source_eigenvalues_buffer, &
1757 source_occupation_buffer
1758 REAL(kind=dp), DIMENSION(:), POINTER :: eigenvalues, occupation
1759 TYPE(cp_blacs_env_type), POINTER :: blacs_env
1760 TYPE(cp_cfm_type) :: dst_cfm_full, src_cfm, src_cfm_full
1761 TYPE(cp_cfm_type), POINTER :: destination_cfm, source_cfm
1762 TYPE(cp_cfm_type), TARGET :: dst_cfm, empty_destination
1763 TYPE(cp_fm_struct_type), POINTER :: matrix_struct_source, matrix_struct_work
1764 TYPE(kpoint_env_type), POINTER :: kp, kp_source
1765 TYPE(kpoint_sym_type), POINTER :: kpsym
1766
1767 success = .false.
1768 reason = ""
1769 aligned_degenerate_blocks = 0
1770 aligned_degenerate_max_size = 0
1771 aligned_degenerate_min_svalue = huge(1.0_dp)
1772 NULLIFY (matrix_struct_source, matrix_struct_work)
1773
1774 IF (.NOT. ASSOCIATED(kpoint)) THEN
1775 reason = "internal Wannier90 k-point object is not available"
1776 RETURN
1777 END IF
1778 IF (.NOT. ASSOCIATED(qs_kpoint)) THEN
1779 reason = "SCF k-point object is not available"
1780 RETURN
1781 END IF
1782 IF (.NOT. ASSOCIATED(kpoint%kp_env) .OR. .NOT. ASSOCIATED(qs_kpoint%kp_env)) THEN
1783 reason = "k-point MO environments are not initialized"
1784 RETURN
1785 END IF
1786 IF (.NOT. ASSOCIATED(kpoint%blacs_env)) THEN
1787 reason = "Wannier90 k-point BLACS environment is not initialized"
1788 RETURN
1789 END IF
1790
1791 CALL build_wannier90_scf_mapping(kpoint, qs_kpoint, source_kpoint, sym_index, ok, reason)
1792 IF (.NOT. ok) RETURN
1793 nsymmetry = count(sym_index > 0)
1794
1795 kp => kpoint%kp_env(1)%kpoint_env
1796 IF (.NOT. ASSOCIATED(kp%mos(ispin)%cmo_coeff)) THEN
1797 reason = "Wannier90 export k-point MOs are not complex-valued"
1798 DEALLOCATE (source_kpoint, sym_index)
1799 RETURN
1800 END IF
1801 CALL get_mo_set(kp%mos(ispin), nao=nao, nmo=nmo)
1802
1803 kp_source => qs_kpoint%kp_env(1)%kpoint_env
1804 IF (.NOT. ASSOCIATED(kp_source%mos(ispin)%cmo_coeff)) THEN
1805 reason = "SCF MOs are real-valued; complex symmetry phases cannot be reconstructed"
1806 DEALLOCATE (source_kpoint, sym_index)
1807 RETURN
1808 END IF
1809 CALL get_mo_set(kp_source%mos(ispin), nao=nao_src, nmo=nmo_src)
1810 CALL para_env%max(nao_src)
1811 CALL para_env%max(nmo_src)
1812 IF (nao_src /= nao) THEN
1813 reason = "SCF and Wannier90 MO bases have different AO dimensions"
1814 DEALLOCATE (source_kpoint, sym_index)
1815 RETURN
1816 END IF
1817 IF (nmo_src < nmo) THEN
1818 reason = "SCF MO set has fewer bands than the Wannier90 export"
1819 DEALLOCATE (source_kpoint, sym_index)
1820 RETURN
1821 END IF
1822 source_window = nmo_src > nmo
1823 degenerate_band_tol = 1.0e-8_dp
1824 CALL get_kpoint_info(qs_kpoint, kp_range=source_kp_range)
1825 IF (source_kp_range(1) /= 1 .OR. source_kp_range(2) /= qs_kpoint%nkp) THEN
1826 reason = "SCF k-point symmetry data are distributed over k-point parallel groups"
1827 DEALLOCATE (source_kpoint, sym_index)
1828 RETURN
1829 END IF
1830 ! Positive symmetry entries require atom/AO rotations and Bloch phases. Degenerate subspaces
1831 ! fully contained in the exported band window are aligned below; only guard when the Wannier90
1832 ! window cuts through a degenerate SCF manifold at the upper band edge.
1833 IF (nsymmetry > 0 .AND. nmo_src > nmo) THEN
1834 local_min_band_gap = huge(1.0_dp)
1835 min_gap_band = nmo
1836 min_gap_kpoint = 0
1837 min_gap_spin = 0
1838 DO ikred = source_kp_range(1), source_kp_range(2)
1839 ikpgr = ikred - source_kp_range(1) + 1
1840 kp_source => qs_kpoint%kp_env(ikpgr)%kpoint_env
1841 CALL get_mo_set(kp_source%mos(ispin), eigenvalues=eigenvalues)
1842 band_gap = abs(eigenvalues(nmo + 1) - eigenvalues(nmo))
1843 IF (band_gap < local_min_band_gap) THEN
1844 local_min_band_gap = band_gap
1845 min_gap_band = nmo
1846 min_gap_kpoint = ikred
1847 min_gap_spin = ispin
1848 END IF
1849 END DO
1850 min_band_gap = local_min_band_gap
1851 CALL para_env%min(min_band_gap)
1852 IF (abs(local_min_band_gap - min_band_gap) > degenerate_band_tol*epsilon(1.0_dp)) THEN
1853 min_gap_kpoint = 0
1854 min_gap_spin = 0
1855 END IF
1856 CALL para_env%max(min_gap_kpoint)
1857 CALL para_env%max(min_gap_spin)
1858 CALL para_env%max(min_gap_band)
1859 IF (min_band_gap < degenerate_band_tol) THEN
1860 WRITE (reason, "(A,ES9.2,A,I0,A,I0,A,I0)") &
1861 "degenerate atom/AO W90 reuse guarded: edge gap=", min_band_gap, ", k=", &
1862 min_gap_kpoint, ", s=", min_gap_spin, ", nband=", min_gap_band
1863 DEALLOCATE (source_kpoint, sym_index)
1864 RETURN
1865 END IF
1866 END IF
1867 blacs_env => kpoint%blacs_env
1868 CALL cp_fm_struct_create(matrix_struct_work, nrow_global=nao, ncol_global=nmo, &
1869 para_env=para_env, context=blacs_env)
1870 CALL cp_cfm_create(src_cfm, matrix_struct_work)
1871 CALL cp_cfm_create(dst_cfm, matrix_struct_work)
1872 IF (source_window) THEN
1873 CALL cp_fm_struct_create(matrix_struct_source, nrow_global=nao, ncol_global=nmo_src, &
1874 para_env=para_env, context=blacs_env)
1875 CALL cp_cfm_create(src_cfm_full, matrix_struct_source)
1876 CALL cp_cfm_create(dst_cfm_full, matrix_struct_source)
1877 END IF
1878 ALLOCATE (eigenvalues_buffer(nmo), occupation_buffer(nmo), &
1879 source_eigenvalues_buffer(nmo_src), source_occupation_buffer(nmo_src))
1880
1881 CALL get_kpoint_info(kpoint, kp_range=kp_range)
1882 CALL get_kpoint_info(qs_kpoint, kp_range=source_kp_range)
1883
1884 DO ik = 1, kpoint%nkp
1885 ikred = source_kpoint(ik)
1886 my_kpgrp = (ik >= kp_range(1) .AND. ik <= kp_range(2))
1887 my_source_kpgrp = (ikred >= source_kp_range(1) .AND. ikred <= source_kp_range(2))
1888 source_eigenvalues_buffer(1:nmo_src) = 0.0_dp
1889 source_occupation_buffer(1:nmo_src) = 0.0_dp
1890 NULLIFY (source_cfm)
1891 IF (my_source_kpgrp) THEN
1892 ikpgr = ikred - source_kp_range(1) + 1
1893 kp_source => qs_kpoint%kp_env(ikpgr)%kpoint_env
1894 source_cfm => kp_source%mos(ispin)%cmo_coeff
1895 CALL get_mo_set(kp_source%mos(ispin), eigenvalues=eigenvalues, &
1896 occupation_numbers=occupation)
1897 source_eigenvalues_buffer(1:nmo_src) = eigenvalues(1:nmo_src)
1898 source_occupation_buffer(1:nmo_src) = occupation(1:nmo_src)
1899 END IF
1900 IF (source_window) THEN
1901 CALL wannier90_copy_cfm_general(source_cfm, src_cfm_full, para_env)
1902 ELSE
1903 CALL wannier90_copy_cfm_general(source_cfm, src_cfm, para_env)
1904 END IF
1905 IF (my_source_kpgrp) THEN
1906 source_owner_count = 1.0_dp
1907 ELSE
1908 source_owner_count = 0.0_dp
1909 END IF
1910 CALL para_env%sum(source_owner_count)
1911 CALL para_env%sum(source_eigenvalues_buffer)
1912 CALL para_env%sum(source_occupation_buffer)
1913 IF (source_owner_count > 0.0_dp) THEN
1914 source_eigenvalues_buffer(1:nmo_src) = &
1915 source_eigenvalues_buffer(1:nmo_src)/source_owner_count
1916 source_occupation_buffer(1:nmo_src) = &
1917 source_occupation_buffer(1:nmo_src)/source_owner_count
1918 END IF
1919 eigenvalues_buffer(1:nmo) = source_eigenvalues_buffer(1:nmo)
1920 occupation_buffer(1:nmo) = source_occupation_buffer(1:nmo)
1921 IF (source_window) THEN
1922 CALL cp_cfm_to_cfm(src_cfm_full, src_cfm, nmo)
1923 END IF
1924
1925 ok = .false.
1926 reason = ""
1927 aligned_blocks = 0
1928 aligned_max_size = 0
1929 aligned_min_svalue = 0.0_dp
1930 IF (sym_index(ik) > 0) THEN
1931 kpsym => qs_kpoint%kp_sym(ikred)%kpoint_sym
1932 IF (ASSOCIATED(kpsym)) THEN
1933 best_reason = ""
1934 best_residual = huge(1.0_dp)
1935 num_candidates = 0
1936 ! Little-group operations can reach the same target k-point; keep the first valid eigenspace.
1937 DO isym_try = 1, kpsym%nwred
1938 IF (.NOT. kpoint_same_periodic(kpoint%xkp(1:3, ik), &
1939 kpsym%xkp(1:3, isym_try))) cycle
1940 num_candidates = num_candidates + 1
1941 CALL kpoint_transform_scf_mo(src_cfm, dst_cfm, &
1942 qs_kpoint, ikred, isym_try, ok, &
1943 candidate_reason)
1944 IF (.NOT. ok) THEN
1945 reason = candidate_reason
1946 cycle
1947 END IF
1948 CALL ritz_stabilize_wannier90_subspace(dst_cfm, matrix_s, matrix_ks, &
1949 kpoint%xkp(1:3, ik), cell_to_index, &
1950 sab_nl, ispin, eigenvalues_buffer, &
1951 degenerate_band_tol, ok, candidate_reason, &
1952 candidate_aligned_blocks, &
1953 candidate_aligned_max_size, &
1954 aligned_min_svalue, candidate_residual)
1955 IF (candidate_residual < best_residual) THEN
1956 best_residual = candidate_residual
1957 best_reason = candidate_reason
1958 END IF
1959 IF (.NOT. ok) THEN
1960 IF (source_window) THEN
1961 CALL kpoint_transform_scf_mo(src_cfm_full, dst_cfm_full, qs_kpoint, &
1962 ikred, isym_try, ok, candidate_reason)
1963 IF (ok) THEN
1964 CALL ritz_reconstruct_wannier90_window(dst_cfm_full, dst_cfm, matrix_s, &
1965 matrix_ks, kpoint%xkp(1:3, ik), &
1966 cell_to_index, sab_nl, ispin, &
1967 eigenvalues_buffer, nmo, ok, &
1968 candidate_reason, source_window_min_svalue, &
1969 candidate_residual)
1970 IF (candidate_residual < best_residual) THEN
1971 best_residual = candidate_residual
1972 best_reason = candidate_reason
1973 END IF
1974 END IF
1975 ELSE
1976 CALL ritz_reconstruct_wannier90_window(dst_cfm, dst_cfm, matrix_s, matrix_ks, &
1977 kpoint%xkp(1:3, ik), cell_to_index, &
1978 sab_nl, ispin, eigenvalues_buffer, nmo, &
1979 ok, candidate_reason, source_window_min_svalue, &
1980 candidate_residual)
1981 IF (candidate_residual < best_residual) THEN
1982 best_residual = candidate_residual
1983 best_reason = candidate_reason
1984 END IF
1985 END IF
1986 END IF
1987 IF (ok) THEN
1988 aligned_blocks = candidate_aligned_blocks
1989 aligned_max_size = candidate_aligned_max_size
1990 sym_index(ik) = isym_try
1991 EXIT
1992 END IF
1993 reason = candidate_reason
1994 END DO
1995 IF (.NOT. ok .AND. num_candidates > 0 .AND. best_residual < huge(1.0_dp)) THEN
1996 WRITE (reason, "(A,I0,A,ES9.2,A,I0,A,A32)") &
1997 "atom/AO W90 guarded: best/", num_candidates, "=", best_residual, &
1998 " k=", ik, " ", trim(best_reason)
1999 ELSE IF (.NOT. ok .AND. num_candidates == 0) THEN
2000 reason = "no matching SCF symmetry operation candidate"
2001 END IF
2002 ELSE
2003 reason = "SCF k-point symmetry operation is not available"
2004 END IF
2005 ELSE
2006 CALL kpoint_transform_scf_mo(src_cfm, dst_cfm, qs_kpoint, &
2007 ikred, sym_index(ik), ok, reason)
2008 END IF
2009 IF (ok .AND. sym_index(ik) <= 0) THEN
2010 ! Even a direct k-point copy must be a closed H(k),S(k) subspace. This catches
2011 ! incomplete degenerate band windows before they can be exported to Wannier90.
2012 CALL ritz_stabilize_wannier90_subspace(dst_cfm, matrix_s, matrix_ks, &
2013 kpoint%xkp(1:3, ik), cell_to_index, sab_nl, &
2014 ispin, eigenvalues_buffer, degenerate_band_tol, &
2015 ok, reason, aligned_blocks, aligned_max_size, &
2016 aligned_min_svalue, candidate_residual)
2017 IF (.NOT. ok .AND. source_window) THEN
2018 CALL kpoint_transform_scf_mo(src_cfm_full, dst_cfm_full, qs_kpoint, &
2019 ikred, sym_index(ik), ok, reason)
2020 IF (ok) THEN
2021 CALL ritz_reconstruct_wannier90_window(dst_cfm_full, dst_cfm, matrix_s, matrix_ks, &
2022 kpoint%xkp(1:3, ik), cell_to_index, &
2023 sab_nl, ispin, eigenvalues_buffer, nmo, ok, &
2024 reason, source_window_min_svalue, &
2025 candidate_residual)
2026 END IF
2027 ELSE IF (.NOT. ok) THEN
2028 CALL ritz_reconstruct_wannier90_window(dst_cfm, dst_cfm, matrix_s, matrix_ks, &
2029 kpoint%xkp(1:3, ik), &
2030 cell_to_index, sab_nl, ispin, &
2031 eigenvalues_buffer, nmo, ok, reason, &
2032 source_window_min_svalue, candidate_residual)
2033 END IF
2034 END IF
2035 IF (.NOT. ok) THEN
2036 CALL cp_cfm_release(src_cfm)
2037 CALL cp_cfm_release(dst_cfm)
2038 CALL cp_fm_struct_release(matrix_struct_work)
2039 IF (source_window) THEN
2040 CALL cp_cfm_release(src_cfm_full)
2041 CALL cp_cfm_release(dst_cfm_full)
2042 CALL cp_fm_struct_release(matrix_struct_source)
2043 END IF
2044 DEALLOCATE (source_kpoint, sym_index, eigenvalues_buffer, occupation_buffer, &
2045 source_eigenvalues_buffer, source_occupation_buffer)
2046 RETURN
2047 END IF
2048 IF (sym_index(ik) /= 0) THEN
2049 aligned_degenerate_blocks = aligned_degenerate_blocks + aligned_blocks
2050 aligned_degenerate_max_size = max(aligned_degenerate_max_size, aligned_max_size)
2051 IF (aligned_blocks > 0) THEN
2052 aligned_degenerate_min_svalue = min(aligned_degenerate_min_svalue, aligned_min_svalue)
2053 END IF
2054 END IF
2055
2056 IF (my_kpgrp) THEN
2057 ikpgr = ik - kp_range(1) + 1
2058 kp => kpoint%kp_env(ikpgr)%kpoint_env
2059 CALL get_mo_set(kp%mos(ispin), eigenvalues=eigenvalues, &
2060 occupation_numbers=occupation)
2061 eigenvalues(1:nmo) = eigenvalues_buffer(1:nmo)
2062 occupation(1:nmo) = occupation_buffer(1:nmo)
2063 destination_cfm => kp%mos(ispin)%cmo_coeff
2064 ELSE
2065 destination_cfm => empty_destination
2066 END IF
2067 CALL cp_cfm_copy_general(dst_cfm, destination_cfm, para_env)
2068 END DO
2069
2070 CALL cp_cfm_release(src_cfm)
2071 CALL cp_cfm_release(dst_cfm)
2072 CALL cp_fm_struct_release(matrix_struct_work)
2073 IF (source_window) THEN
2074 CALL cp_cfm_release(src_cfm_full)
2075 CALL cp_cfm_release(dst_cfm_full)
2076 CALL cp_fm_struct_release(matrix_struct_source)
2077 END IF
2078 DEALLOCATE (source_kpoint, sym_index, eigenvalues_buffer, occupation_buffer, &
2079 source_eigenvalues_buffer, source_occupation_buffer)
2080 IF (aligned_degenerate_blocks == 0) aligned_degenerate_min_svalue = 0.0_dp
2081 success = .true.
2082
2083 END SUBROUTINE prepare_wannier90_spin_mos
2084
2085! **************************************************************************************************
2086!> \brief Save a full-mesh Wannier90 MO reference on all ranks for diagnostic validation.
2087!> \param kpoint full Wannier90 export k-point object
2088!> \param ispin Selected spin channel.
2089!> \param para_env global parallel environment
2090!> \param mo MO coefficients for the selected spin, indexed as AO, MO, k-point
2091!> \param eigenvalue_snapshot MO eigenvalues for the selected spin, indexed as MO, k-point
2092! **************************************************************************************************
2093 SUBROUTINE save_wannier90_mo_snapshot(kpoint, ispin, para_env, mo, &
2094 eigenvalue_snapshot)
2095 TYPE(kpoint_type), POINTER :: kpoint
2096 INTEGER, INTENT(IN) :: ispin
2097 TYPE(mp_para_env_type), POINTER :: para_env
2098 COMPLEX(KIND=dp), ALLOCATABLE, &
2099 DIMENSION(:, :, :), INTENT(OUT) :: mo
2100 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :), &
2101 INTENT(OUT) :: eigenvalue_snapshot
2102
2103 INTEGER :: ik, ikpgr, nao, nkp, nmo
2104 INTEGER, DIMENSION(2) :: kp_range
2105 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: owner_weight
2106 REAL(kind=dp), DIMENSION(:), POINTER :: eigenvalues
2107 TYPE(kpoint_env_type), POINTER :: kp
2108
2109 CALL get_kpoint_info(kpoint, nkp=nkp, kp_range=kp_range)
2110 kp => kpoint%kp_env(1)%kpoint_env
2111 CALL get_mo_set(kp%mos(ispin), nao=nao, nmo=nmo)
2112 ALLOCATE (mo(nao, nmo, nkp), &
2113 eigenvalue_snapshot(nmo, nkp), owner_weight(nkp))
2114 mo(:, :, :) = cmplx(0.0_dp, 0.0_dp, kind=dp)
2115 eigenvalue_snapshot(:, :) = 0.0_dp
2116 owner_weight(:) = 0.0_dp
2117 DO ik = kp_range(1), kp_range(2)
2118 ikpgr = ik - kp_range(1) + 1
2119 kp => kpoint%kp_env(ikpgr)%kpoint_env
2120 CALL cp_cfm_get_submatrix(kp%mos(ispin)%cmo_coeff, mo(:, :, ik))
2121 CALL get_mo_set(kp%mos(ispin), eigenvalues=eigenvalues)
2122 eigenvalue_snapshot(1:nmo, ik) = eigenvalues(1:nmo)
2123 owner_weight(ik) = 1.0_dp
2124 END DO
2125 CALL para_env%sum(mo)
2126 CALL para_env%sum(eigenvalue_snapshot)
2127 CALL para_env%sum(owner_weight)
2128 DO ik = 1, nkp
2129 IF (owner_weight(ik) > 0.0_dp) THEN
2130 mo(:, :, ik) = mo(:, :, ik)/owner_weight(ik)
2131 eigenvalue_snapshot(:, ik) = &
2132 eigenvalue_snapshot(:, ik)/owner_weight(ik)
2133 END IF
2134 END DO
2135 DEALLOCATE (owner_weight)
2136
2137 END SUBROUTINE save_wannier90_mo_snapshot
2138
2139! **************************************************************************************************
2140!> \brief Restore a full-mesh Wannier90 MO reference after a failed diagnostic reuse attempt.
2141!> \param kpoint full Wannier90 export k-point object
2142!> \param ispin Selected spin channel.
2143!> \param mo complex MO coefficient snapshot
2144!> \param eigenvalue_snapshot MO eigenvalue snapshot
2145! **************************************************************************************************
2146 SUBROUTINE restore_wannier90_mo_snapshot(kpoint, ispin, mo, eigenvalue_snapshot)
2147 TYPE(kpoint_type), POINTER :: kpoint
2148 INTEGER, INTENT(IN) :: ispin
2149 COMPLEX(KIND=dp), DIMENSION(:, :, :), INTENT(IN) :: mo
2150 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: eigenvalue_snapshot
2151
2152 INTEGER :: ik, ikpgr, nmo
2153 INTEGER, DIMENSION(2) :: kp_range
2154 REAL(kind=dp), DIMENSION(:), POINTER :: eigenvalues
2155 TYPE(kpoint_env_type), POINTER :: kp
2156
2157 CALL get_kpoint_info(kpoint, kp_range=kp_range)
2158 nmo = SIZE(eigenvalue_snapshot, 1)
2159 DO ik = kp_range(1), kp_range(2)
2160 ikpgr = ik - kp_range(1) + 1
2161 kp => kpoint%kp_env(ikpgr)%kpoint_env
2162 CALL cp_cfm_set_submatrix(kp%mos(ispin)%cmo_coeff, mo(:, :, ik))
2163 CALL get_mo_set(kp%mos(ispin), eigenvalues=eigenvalues)
2164 eigenvalues(1:nmo) = eigenvalue_snapshot(1:nmo, ik)
2165 END DO
2166
2167 END SUBROUTINE restore_wannier90_mo_snapshot
2168
2169! **************************************************************************************************
2170!> \brief Validate current Wannier90 MOs against a saved full-mesh diagonalization reference.
2171!> \param kpoint full Wannier90 export k-point object
2172!> \param matrix_s real-space overlap matrix
2173!> \param cell_to_index real-space cell index table
2174!> \param sab_nl overlap neighbor list
2175!> \param para_env global parallel environment
2176!> \param ispin Selected spin channel.
2177!> \param reference_mo complex MO coefficient reference
2178!> \param reference_eigenvalues MO eigenvalue reference
2179!> \param success true if the reconstructed MOs match the reference subspaces
2180!> \param max_subspace_deviation largest deviation of S(k)-metric singular values from one
2181!> \param min_svalue smallest S(k)-metric singular value
2182!> \param max_eigenvalue_deviation largest eigenvalue deviation
2183! **************************************************************************************************
2184 SUBROUTINE validate_wannier90_reused_mos(kpoint, matrix_s, cell_to_index, sab_nl, para_env, ispin, &
2185 reference_mo, reference_eigenvalues, &
2186 success, max_subspace_deviation, min_svalue, &
2187 max_eigenvalue_deviation)
2188 TYPE(kpoint_type), POINTER :: kpoint
2189 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s
2190 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
2191 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
2192 POINTER :: sab_nl
2193 TYPE(mp_para_env_type), POINTER :: para_env
2194 INTEGER, INTENT(IN) :: ispin
2195 COMPLEX(KIND=dp), DIMENSION(:, :, :), INTENT(IN) :: reference_mo
2196 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: reference_eigenvalues
2197 LOGICAL, INTENT(OUT) :: success
2198 REAL(kind=dp), INTENT(OUT) :: max_subspace_deviation, min_svalue, &
2199 max_eigenvalue_deviation
2200
2201 REAL(kind=dp), PARAMETER :: eigenvalue_tol = 1.0e-8_dp, &
2202 subspace_tol = 1.0e-4_dp
2203
2204 INTEGER :: ik, ikpgr, nao, nkp, nmo
2205 INTEGER, DIMENSION(2) :: kp_range
2206 LOGICAL :: my_kpgrp, ok
2207 REAL(kind=dp) :: candidate_deviation, candidate_svalue, &
2208 owner_count
2209 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eigenvalue_buffer
2210 REAL(kind=dp), DIMENSION(:), POINTER :: eigenvalues
2211 TYPE(cp_cfm_type) :: cand_cfm, ref_cfm
2212 TYPE(cp_cfm_type), POINTER :: candidate_source
2213 TYPE(cp_fm_struct_type), POINTER :: matrix_struct_work
2214 TYPE(kpoint_env_type), POINTER :: kp
2215
2216 success = .false.
2217 max_subspace_deviation = 0.0_dp
2218 min_svalue = huge(1.0_dp)
2219 max_eigenvalue_deviation = 0.0_dp
2220 NULLIFY (matrix_struct_work)
2221
2222 CALL get_kpoint_info(kpoint, nkp=nkp, kp_range=kp_range)
2223 kp => kpoint%kp_env(1)%kpoint_env
2224 CALL get_mo_set(kp%mos(ispin), nao=nao, nmo=nmo)
2225 cpassert(SIZE(reference_mo, 1) == nao)
2226 cpassert(SIZE(reference_mo, 2) == nmo)
2227 cpassert(SIZE(reference_mo, 3) == nkp)
2228
2229 matrix_struct_work => kp%mos(ispin)%cmo_coeff%matrix_struct
2230 CALL cp_cfm_create(ref_cfm, matrix_struct_work)
2231 CALL cp_cfm_create(cand_cfm, matrix_struct_work)
2232 ALLOCATE (eigenvalue_buffer(nmo))
2233
2234 DO ik = 1, nkp
2235 CALL cp_cfm_set_submatrix(ref_cfm, reference_mo(:, :, ik))
2236 my_kpgrp = (ik >= kp_range(1) .AND. ik <= kp_range(2))
2237 NULLIFY (candidate_source)
2238 IF (my_kpgrp) THEN
2239 ikpgr = ik - kp_range(1) + 1
2240 kp => kpoint%kp_env(ikpgr)%kpoint_env
2241 candidate_source => kp%mos(ispin)%cmo_coeff
2242 CALL get_mo_set(kp%mos(ispin), eigenvalues=eigenvalues)
2243 eigenvalue_buffer(1:nmo) = eigenvalues(1:nmo)
2244 ELSE
2245 eigenvalue_buffer(1:nmo) = 0.0_dp
2246 END IF
2247 CALL wannier90_copy_cfm_general(candidate_source, cand_cfm, para_env)
2248 IF (my_kpgrp) THEN
2249 owner_count = 1.0_dp
2250 ELSE
2251 owner_count = 0.0_dp
2252 END IF
2253 CALL para_env%sum(owner_count)
2254 CALL para_env%sum(eigenvalue_buffer)
2255 IF (owner_count > 0.0_dp) eigenvalue_buffer(1:nmo) = eigenvalue_buffer(1:nmo)/owner_count
2256 max_eigenvalue_deviation = max(max_eigenvalue_deviation, &
2257 maxval(abs(eigenvalue_buffer(1:nmo) - &
2258 reference_eigenvalues(1:nmo, ik))))
2259 CALL measure_wannier90_subspace_error(ref_cfm, cand_cfm, matrix_s, &
2260 kpoint%xkp(1:3, ik), cell_to_index, sab_nl, para_env, &
2261 ok, candidate_deviation, candidate_svalue)
2262 IF (.NOT. ok) THEN
2263 max_subspace_deviation = huge(1.0_dp)
2264 ELSE
2265 max_subspace_deviation = max(max_subspace_deviation, candidate_deviation)
2266 min_svalue = min(min_svalue, candidate_svalue)
2267 END IF
2268 END DO
2269 CALL para_env%max(max_subspace_deviation)
2270 CALL para_env%min(min_svalue)
2271 CALL para_env%max(max_eigenvalue_deviation)
2272 success = max_subspace_deviation < subspace_tol .AND. max_eigenvalue_deviation < eigenvalue_tol
2273
2274 DEALLOCATE (eigenvalue_buffer)
2275 CALL cp_cfm_release(ref_cfm)
2276 CALL cp_cfm_release(cand_cfm)
2277
2278 END SUBROUTINE validate_wannier90_reused_mos
2279
2280! **************************************************************************************************
2281!> \brief Compare atom/AO reuse candidates directly to the full-mesh reference MOs.
2282!> \param kpoint full Wannier90 export k-point object holding reference MOs
2283!> \param qs_kpoint SCF k-point object
2284!> \param matrix_s real-space overlap matrix
2285!> \param matrix_ks real-space Kohn-Sham matrix
2286!> \param cell_to_index real-space cell index table
2287!> \param sab_nl overlap neighbor list
2288!> \param para_env global parallel environment
2289!> \param iw output unit
2290!> \param ispin Selected spin channel.
2291!> \param max_subspace_deviation largest best-candidate subspace deviation
2292!> \param min_svalue smallest best-candidate singular value
2293!> \param max_metric_deviation largest S(k)-metric deviation of a candidate
2294!> \param max_residual largest H(k),S(k) eigen-residual of a candidate
2295! **************************************************************************************************
2296 SUBROUTINE diagnose_wannier90_scf_reuse_candidates(kpoint, qs_kpoint, matrix_s, matrix_ks, &
2297 cell_to_index, sab_nl, para_env, iw, ispin, &
2298 max_subspace_deviation, min_svalue, &
2299 max_metric_deviation, max_residual)
2300 TYPE(kpoint_type), POINTER :: kpoint, qs_kpoint
2301 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s, matrix_ks
2302 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
2303 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
2304 POINTER :: sab_nl
2305 TYPE(mp_para_env_type), POINTER :: para_env
2306 INTEGER, INTENT(IN) :: iw, ispin
2307 REAL(kind=dp), INTENT(OUT) :: max_subspace_deviation, min_svalue, &
2308 max_metric_deviation, max_residual
2309
2310 REAL(kind=dp), PARAMETER :: print_tol = 1.0e-4_dp, &
2311 residual_print_tol = 1.0e-3_dp
2312
2313 CHARACTER(LEN=default_string_length) :: reason
2314 INTEGER :: ik, ikpgr, ikred, isym_try, nao, &
2315 nao_src, nkp, nmo, nmo_src
2316 INTEGER, ALLOCATABLE, DIMENSION(:) :: source_kpoint, sym_index
2317 INTEGER, DIMENSION(2) :: kp_range, source_kp_range
2318 LOGICAL :: my_kpgrp, ok, source_window
2319 REAL(kind=dp) :: best_deviation, best_metric_deviation, best_residual, best_svalue, &
2320 candidate_deviation, candidate_metric_deviation, candidate_metric_min, &
2321 candidate_residual, candidate_svalue, owner_count, ref_metric_deviation, ref_metric_min, &
2322 ref_residual
2323 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: eigenvalues_buffer
2324 REAL(kind=dp), DIMENSION(:), POINTER :: eigenvalues
2325 TYPE(cp_cfm_type) :: dst_cfm, ref_cfm, src_cfm, src_cfm_full
2326 TYPE(cp_cfm_type), POINTER :: source_cfm
2327 TYPE(cp_fm_struct_type), POINTER :: matrix_struct_source, matrix_struct_work
2328 TYPE(kpoint_env_type), POINTER :: kp, kp_source
2329 TYPE(kpoint_sym_type), POINTER :: kpsym
2330
2331 max_subspace_deviation = huge(1.0_dp)
2332 min_svalue = 0.0_dp
2333 max_metric_deviation = huge(1.0_dp)
2334 max_residual = huge(1.0_dp)
2335 NULLIFY (matrix_struct_source, matrix_struct_work)
2336
2337 CALL build_wannier90_scf_mapping(kpoint, qs_kpoint, source_kpoint, sym_index, ok, reason)
2338 IF (.NOT. ok) RETURN
2339 kp => kpoint%kp_env(1)%kpoint_env
2340 kp_source => qs_kpoint%kp_env(1)%kpoint_env
2341 IF (.NOT. ASSOCIATED(kp%mos(ispin)%cmo_coeff) .OR. &
2342 .NOT. ASSOCIATED(kp_source%mos(ispin)%cmo_coeff)) THEN
2343 DEALLOCATE (source_kpoint, sym_index)
2344 RETURN
2345 END IF
2346 CALL get_mo_set(kp%mos(ispin), nao=nao, nmo=nmo)
2347 CALL get_mo_set(kp_source%mos(ispin), nao=nao_src, nmo=nmo_src)
2348 CALL para_env%max(nao_src)
2349 CALL para_env%max(nmo_src)
2350 IF (nao_src /= nao .OR. nmo_src < nmo) THEN
2351 DEALLOCATE (source_kpoint, sym_index)
2352 RETURN
2353 END IF
2354 source_window = nmo_src > nmo
2355 CALL get_kpoint_info(kpoint, nkp=nkp, kp_range=kp_range)
2356 CALL get_kpoint_info(qs_kpoint, kp_range=source_kp_range)
2357 IF (source_kp_range(1) /= 1 .OR. source_kp_range(2) /= qs_kpoint%nkp) THEN
2358 DEALLOCATE (source_kpoint, sym_index)
2359 RETURN
2360 END IF
2361
2362 CALL cp_fm_struct_create(matrix_struct_work, nrow_global=nao, ncol_global=nmo, &
2363 para_env=para_env, context=kpoint%blacs_env)
2364 CALL cp_cfm_create(ref_cfm, matrix_struct_work)
2365 CALL cp_cfm_create(src_cfm, matrix_struct_work)
2366 CALL cp_cfm_create(dst_cfm, matrix_struct_work)
2367 ALLOCATE (eigenvalues_buffer(nmo))
2368 IF (source_window) THEN
2369 CALL cp_fm_struct_create(matrix_struct_source, nrow_global=nao, ncol_global=nmo_src, &
2370 para_env=para_env, context=kpoint%blacs_env)
2371 CALL cp_cfm_create(src_cfm_full, matrix_struct_source)
2372 END IF
2373
2374 max_subspace_deviation = 0.0_dp
2375 min_svalue = huge(1.0_dp)
2376 max_metric_deviation = 0.0_dp
2377 max_residual = 0.0_dp
2378 DO ik = 1, nkp
2379 my_kpgrp = (ik >= kp_range(1) .AND. ik <= kp_range(2))
2380 NULLIFY (source_cfm)
2381 IF (my_kpgrp) THEN
2382 ikpgr = ik - kp_range(1) + 1
2383 kp => kpoint%kp_env(ikpgr)%kpoint_env
2384 source_cfm => kp%mos(ispin)%cmo_coeff
2385 END IF
2386 CALL wannier90_copy_cfm_general(source_cfm, ref_cfm, para_env)
2387
2388 ikred = source_kpoint(ik)
2389 my_kpgrp = (ikred >= source_kp_range(1) .AND. ikred <= source_kp_range(2))
2390 NULLIFY (source_cfm)
2391 IF (my_kpgrp) THEN
2392 ikpgr = ikred - source_kp_range(1) + 1
2393 kp_source => qs_kpoint%kp_env(ikpgr)%kpoint_env
2394 source_cfm => kp_source%mos(ispin)%cmo_coeff
2395 CALL get_mo_set(kp_source%mos(ispin), eigenvalues=eigenvalues)
2396 eigenvalues_buffer(1:nmo) = eigenvalues(1:nmo)
2397 ELSE
2398 eigenvalues_buffer(1:nmo) = 0.0_dp
2399 END IF
2400 IF (source_window) THEN
2401 CALL wannier90_copy_cfm_general(source_cfm, src_cfm_full, para_env)
2402 ELSE
2403 CALL wannier90_copy_cfm_general(source_cfm, src_cfm, para_env)
2404 END IF
2405 IF (my_kpgrp) THEN
2406 owner_count = 1.0_dp
2407 ELSE
2408 owner_count = 0.0_dp
2409 END IF
2410 CALL para_env%sum(owner_count)
2411 CALL para_env%sum(eigenvalues_buffer)
2412 IF (owner_count > 0.0_dp) eigenvalues_buffer(1:nmo) = eigenvalues_buffer(1:nmo)/owner_count
2413 CALL measure_wannier90_eigenspace_quality(ref_cfm, matrix_s, matrix_ks, &
2414 kpoint%xkp(1:3, ik), cell_to_index, sab_nl, &
2415 para_env, ispin, eigenvalues_buffer, ok, &
2416 ref_metric_deviation, ref_metric_min, ref_residual)
2417 IF (ok .AND. para_env%is_source() .AND. iw > 0 .AND. &
2418 (ref_metric_deviation > print_tol .OR. ref_residual > residual_print_tol)) THEN
2419 WRITE (iw, '(T2,A,I0,A,ES10.3,A,ES10.3,A,ES10.3)') &
2420 "WANNIER90| reference k=", ik, " dM=", ref_metric_deviation, &
2421 " smin=", ref_metric_min, " resid=", ref_residual
2422 END IF
2423 IF (source_window) THEN
2424 CALL cp_cfm_to_cfm(src_cfm_full, src_cfm, nmo)
2425 END IF
2426
2427 best_deviation = huge(1.0_dp)
2428 best_metric_deviation = 0.0_dp
2429 best_residual = 0.0_dp
2430 best_svalue = 0.0_dp
2431 IF (sym_index(ik) <= 0) THEN
2432 CALL kpoint_transform_scf_mo(src_cfm, dst_cfm, qs_kpoint, &
2433 ikred, sym_index(ik), ok, reason)
2434 IF (ok) THEN
2435 CALL measure_wannier90_subspace_error(ref_cfm, dst_cfm, matrix_s, &
2436 kpoint%xkp(1:3, ik), cell_to_index, sab_nl, &
2437 para_env, ok, candidate_deviation, candidate_svalue)
2438 IF (ok) THEN
2439 CALL measure_wannier90_eigenspace_quality(dst_cfm, matrix_s, matrix_ks, &
2440 kpoint%xkp(1:3, ik), cell_to_index, sab_nl, &
2441 para_env, ispin, eigenvalues_buffer, ok, &
2442 candidate_metric_deviation, &
2443 candidate_metric_min, candidate_residual)
2444 END IF
2445 IF (ok) THEN
2446 best_deviation = candidate_deviation
2447 best_metric_deviation = candidate_metric_deviation
2448 best_residual = candidate_residual
2449 best_svalue = candidate_svalue
2450 IF (para_env%is_source() .AND. iw > 0 .AND. &
2451 (candidate_deviation > print_tol .OR. candidate_metric_deviation > print_tol .OR. &
2452 candidate_residual > residual_print_tol)) THEN
2453 WRITE (iw, '(T2,A,I0,A,I0,A,I0,A,ES10.3,A,ES10.3,A,ES10.3,A,ES10.3)') &
2454 "WANNIER90| reuse candidate k=", ik, " src=", ikred, " sym=", &
2455 sym_index(ik), " dRef=", candidate_deviation, " dM=", &
2456 candidate_metric_deviation, " smin=", candidate_metric_min, &
2457 " resid=", candidate_residual
2458 END IF
2459 END IF
2460 END IF
2461 ELSE IF (ASSOCIATED(qs_kpoint%kp_sym)) THEN
2462 kpsym => qs_kpoint%kp_sym(ikred)%kpoint_sym
2463 IF (ASSOCIATED(kpsym)) THEN
2464 DO isym_try = 1, kpsym%nwred
2465 IF (.NOT. kpoint_same_periodic(kpoint%xkp(1:3, ik), &
2466 kpsym%xkp(1:3, isym_try))) cycle
2467 CALL kpoint_transform_scf_mo(src_cfm, dst_cfm, &
2468 qs_kpoint, ikred, isym_try, ok, reason)
2469 IF (.NOT. ok) cycle
2470 CALL measure_wannier90_subspace_error(ref_cfm, dst_cfm, &
2471 matrix_s, kpoint%xkp(1:3, ik), cell_to_index, &
2472 sab_nl, para_env, ok, candidate_deviation, &
2473 candidate_svalue)
2474 IF (ok) THEN
2475 CALL measure_wannier90_eigenspace_quality(dst_cfm, matrix_s, matrix_ks, &
2476 kpoint%xkp(1:3, ik), cell_to_index, &
2477 sab_nl, para_env, ispin, &
2478 eigenvalues_buffer, ok, &
2479 candidate_metric_deviation, &
2480 candidate_metric_min, candidate_residual)
2481 END IF
2482 IF (ok .AND. candidate_deviation < best_deviation) THEN
2483 best_deviation = candidate_deviation
2484 best_metric_deviation = candidate_metric_deviation
2485 best_residual = candidate_residual
2486 best_svalue = candidate_svalue
2487 END IF
2488 END DO
2489 END IF
2490 END IF
2491 IF (best_deviation < huge(1.0_dp)) THEN
2492 max_subspace_deviation = max(max_subspace_deviation, best_deviation)
2493 min_svalue = min(min_svalue, best_svalue)
2494 max_metric_deviation = max(max_metric_deviation, best_metric_deviation)
2495 max_residual = max(max_residual, best_residual)
2496 END IF
2497 END DO
2498 CALL para_env%max(max_subspace_deviation)
2499 CALL para_env%min(min_svalue)
2500 CALL para_env%max(max_metric_deviation)
2501 CALL para_env%max(max_residual)
2502
2503 IF (source_window) THEN
2504 CALL cp_cfm_release(src_cfm_full)
2505 CALL cp_fm_struct_release(matrix_struct_source)
2506 END IF
2507 CALL cp_cfm_release(ref_cfm)
2508 CALL cp_cfm_release(src_cfm)
2509 CALL cp_cfm_release(dst_cfm)
2510 CALL cp_fm_struct_release(matrix_struct_work)
2511 DEALLOCATE (eigenvalues_buffer)
2512 DEALLOCATE (source_kpoint, sym_index)
2513
2514 END SUBROUTINE diagnose_wannier90_scf_reuse_candidates
2515
2516! **************************************************************************************************
2517!> \brief Measure the S(k)-metric distance between two Wannier90 MO subspaces.
2518!> \param ref_cfm reference MO coefficients
2519!> \param cand_cfm candidate MO coefficients
2520!> \param matrix_s real-space overlap matrix
2521!> \param xkp target k-point coordinate
2522!> \param cell_to_index real-space cell index table
2523!> \param sab_nl overlap neighbor list
2524!> \param para_env global parallel environment
2525!> \param success true if the metric comparison was performed
2526!> \param max_subspace_deviation largest deviation of singular values from one
2527!> \param min_svalue smallest singular value of C_ref^+ S(k) C_candidate
2528! **************************************************************************************************
2529 SUBROUTINE measure_wannier90_subspace_error(ref_cfm, cand_cfm, matrix_s, &
2530 xkp, cell_to_index, sab_nl, para_env, success, &
2531 max_subspace_deviation, min_svalue)
2532 TYPE(cp_cfm_type), INTENT(IN) :: ref_cfm, cand_cfm
2533 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s
2534 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: xkp
2535 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
2536 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
2537 POINTER :: sab_nl
2538 TYPE(mp_para_env_type), POINTER :: para_env
2539 LOGICAL, INTENT(OUT) :: success
2540 REAL(kind=dp), INTENT(OUT) :: max_subspace_deviation, min_svalue
2541
2542 COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: metric_projected, metric_vectors, &
2543 overlap, ref_coeff, s_cand_coeff
2544 INTEGER :: ib, nao, nmo, nmo_candidate
2545 REAL(kind=dp) :: singular_value
2546 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: metric_values
2547 TYPE(cp_cfm_type) :: s_cand
2548 TYPE(cp_fm_struct_type), POINTER :: matrix_struct_metric
2549
2550 success = .false.
2551 max_subspace_deviation = huge(1.0_dp)
2552 min_svalue = 0.0_dp
2553 NULLIFY (matrix_struct_metric)
2554
2555 CALL cp_cfm_get_info(ref_cfm, nrow_global=nao, ncol_global=nmo, &
2556 matrix_struct=matrix_struct_metric)
2557 CALL cp_cfm_get_info(cand_cfm, ncol_global=nmo_candidate)
2558 IF (nmo_candidate /= nmo) RETURN
2559
2560 CALL cp_cfm_create(s_cand, matrix_struct_metric)
2561 CALL apply_wannier90_kp_matrix(matrix_s, 1, xkp, cell_to_index, sab_nl, &
2562 cand_cfm, s_cand)
2563
2564 ALLOCATE (ref_coeff(nao, nmo), s_cand_coeff(nao, nmo))
2565 CALL cp_cfm_get_submatrix(ref_cfm, ref_coeff)
2566 CALL cp_cfm_get_submatrix(s_cand, s_cand_coeff)
2567
2568 ALLOCATE (overlap(nmo, nmo), &
2569 metric_projected(nmo, nmo), metric_vectors(nmo, nmo), metric_values(nmo))
2570 overlap(:, :) = matmul(conjg(transpose(ref_coeff)), s_cand_coeff)
2571 metric_projected(:, :) = matmul(conjg(transpose(overlap)), overlap)
2572 metric_projected(:, :) = 0.5_dp*(metric_projected + conjg(transpose(metric_projected)))
2573 CALL diag_complex(metric_projected, metric_vectors, metric_values)
2574
2575 min_svalue = huge(1.0_dp)
2576 max_subspace_deviation = 0.0_dp
2577 DO ib = 1, nmo
2578 singular_value = sqrt(max(metric_values(ib), 0.0_dp))
2579 min_svalue = min(min_svalue, singular_value)
2580 max_subspace_deviation = max(max_subspace_deviation, abs(singular_value - 1.0_dp))
2581 END DO
2582 CALL para_env%max(max_subspace_deviation)
2583 CALL para_env%min(min_svalue)
2584 success = .true.
2585
2586 DEALLOCATE (ref_coeff, s_cand_coeff, overlap, metric_projected, metric_vectors, metric_values)
2587 CALL cp_cfm_release(s_cand)
2588
2589 END SUBROUTINE measure_wannier90_subspace_error
2590
2591! **************************************************************************************************
2592!> \brief Measure whether transformed MOs are an H(k),S(k) invariant eigenspace.
2593!> \param cand_cfm candidate MO coefficients
2594!> \param matrix_s real-space overlap matrix
2595!> \param matrix_ks real-space Kohn-Sham matrix
2596!> \param xkp target k-point coordinate
2597!> \param cell_to_index real-space cell index table
2598!> \param sab_nl overlap neighbor list
2599!> \param para_env global parallel environment
2600!> \param ispin spin index
2601!> \param eigenvalues source MO eigenvalues corresponding to the candidate columns
2602!> \param success true if the metric and residual checks were performed
2603!> \param metric_deviation largest deviation of eigenvalues of C^+ S(k) C from one
2604!> \param min_metric_eigenvalue smallest eigenvalue of C^+ S(k) C
2605!> \param residual_norm largest element of H(k) C - S(k) C eps
2606! **************************************************************************************************
2607 SUBROUTINE measure_wannier90_eigenspace_quality(cand_cfm, matrix_s, matrix_ks, xkp, &
2608 cell_to_index, sab_nl, para_env, ispin, eigenvalues, &
2609 success, metric_deviation, min_metric_eigenvalue, &
2610 residual_norm)
2611 TYPE(cp_cfm_type), INTENT(IN) :: cand_cfm
2612 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s, matrix_ks
2613 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: xkp
2614 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
2615 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
2616 POINTER :: sab_nl
2617 TYPE(mp_para_env_type), POINTER :: para_env
2618 INTEGER, INTENT(IN) :: ispin
2619 REAL(kind=dp), DIMENSION(:), INTENT(IN) :: eigenvalues
2620 LOGICAL, INTENT(OUT) :: success
2621 REAL(kind=dp), INTENT(OUT) :: metric_deviation, min_metric_eigenvalue, &
2622 residual_norm
2623
2624 COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: h_coeff, metric_vectors, residual_block, &
2625 s_coeff, s_projected
2626 INTEGER :: ib, nao, nmo
2627 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: metric_values
2628 TYPE(cp_cfm_type) :: h_cand, metric_cfm, s_cand
2629 TYPE(cp_fm_struct_type), POINTER :: matrix_struct_metric, &
2630 matrix_struct_projected
2631
2632 success = .false.
2633 metric_deviation = huge(1.0_dp)
2634 min_metric_eigenvalue = 0.0_dp
2635 residual_norm = huge(1.0_dp)
2636 NULLIFY (matrix_struct_metric, matrix_struct_projected)
2637
2638 CALL cp_cfm_get_info(cand_cfm, nrow_global=nao, ncol_global=nmo, &
2639 matrix_struct=matrix_struct_metric)
2640 IF (SIZE(eigenvalues) < nmo) RETURN
2641
2642 CALL cp_cfm_create(s_cand, matrix_struct_metric)
2643 CALL cp_cfm_create(h_cand, matrix_struct_metric)
2644
2645 CALL apply_wannier90_kp_matrix(matrix_s, 1, xkp, cell_to_index, sab_nl, &
2646 cand_cfm, s_cand)
2647 CALL apply_wannier90_kp_matrix(matrix_ks, ispin, xkp, cell_to_index, sab_nl, &
2648 cand_cfm, h_cand)
2649
2650 ALLOCATE (h_coeff(nao, nmo), metric_vectors(nmo, nmo), &
2651 residual_block(nao, nmo), s_coeff(nao, nmo), s_projected(nmo, nmo), &
2652 metric_values(nmo))
2653 CALL cp_cfm_get_submatrix(s_cand, s_coeff)
2654 CALL cp_cfm_get_submatrix(h_cand, h_coeff)
2655 CALL cp_fm_struct_create(matrix_struct_projected, nrow_global=nmo, ncol_global=nmo, &
2656 para_env=matrix_struct_metric%para_env, &
2657 context=matrix_struct_metric%context)
2658 CALL cp_cfm_create(metric_cfm, matrix_struct_projected)
2659 CALL parallel_gemm("C", "N", nmo, nmo, nao, cmplx(1.0_dp, 0.0_dp, kind=dp), cand_cfm, &
2660 s_cand, cmplx(0.0_dp, 0.0_dp, kind=dp), metric_cfm)
2661 CALL cp_cfm_get_submatrix(metric_cfm, s_projected)
2662 s_projected(:, :) = 0.5_dp*(s_projected + conjg(transpose(s_projected)))
2663 CALL diag_complex(s_projected, metric_vectors, metric_values)
2664 metric_deviation = maxval(abs(metric_values - 1.0_dp))
2665 min_metric_eigenvalue = minval(metric_values)
2666
2667 residual_block(:, :) = h_coeff
2668 DO ib = 1, nmo
2669 residual_block(:, ib) = residual_block(:, ib) - eigenvalues(ib)*s_coeff(:, ib)
2670 END DO
2671 residual_norm = maxval(abs(residual_block))
2672 CALL para_env%max(metric_deviation)
2673 CALL para_env%min(min_metric_eigenvalue)
2674 CALL para_env%max(residual_norm)
2675 success = .true.
2676
2677 DEALLOCATE (h_coeff, metric_vectors, residual_block, s_coeff, s_projected, metric_values)
2678 CALL cp_cfm_release(s_cand)
2679 CALL cp_cfm_release(h_cand)
2680 CALL cp_cfm_release(metric_cfm)
2681 CALL cp_fm_struct_release(matrix_struct_projected)
2682
2683 END SUBROUTINE measure_wannier90_eigenspace_quality
2684
2685! **************************************************************************************************
2686!> \brief Write fixed trial projections in the exported MO gauge for external Wannier90.
2687!> \param amn Band/trial/k-point projection matrices.
2688!> \param para_env Global parallel environment.
2689!> \param filename Output AMN filename.
2690! **************************************************************************************************
2691 SUBROUTINE write_wannier90_amn(amn, para_env, filename)
2692 COMPLEX(KIND=dp), INTENT(IN) :: amn(:, :, :)
2693 TYPE(mp_para_env_type), POINTER :: para_env
2694 CHARACTER(LEN=*), INTENT(IN) :: filename
2695
2696 INTEGER :: ib, ik, iunit, iwann
2697
2698 IF (.NOT. para_env%is_source()) RETURN
2699 CALL open_file(filename, unit_number=iunit, file_status='REPLACE', file_action='WRITE')
2700 WRITE (iunit, '(A)') '! Initial Wannier90 projections generated by CP2K'
2701 WRITE (iunit, '(3I10)') SIZE(amn, 1), SIZE(amn, 3), SIZE(amn, 2)
2702 DO ik = 1, SIZE(amn, 3)
2703 DO iwann = 1, SIZE(amn, 2)
2704 DO ib = 1, SIZE(amn, 1)
2705 WRITE (iunit, '(3I10,2ES24.16)') ib, iwann, ik, real(amn(ib, iwann, ik), dp), &
2706 aimag(amn(ib, iwann, ik))
2707 END DO
2708 END DO
2709 END DO
2710 CALL close_file(iunit)
2711 END SUBROUTINE write_wannier90_amn
2712
2713! **************************************************************************************************
2714!> \brief Enumerate all on-atom radial s/p shell pairs in global spherical-AO ordering.
2715!> \param qs_env Electronic environment providing the basis and atom ordering.
2716!> \param groups Global indices of an s function and the three functions of a p shell.
2717! **************************************************************************************************
2718 SUBROUTINE build_wannier90_hybrid_groups(qs_env, groups)
2719 TYPE(qs_environment_type), POINTER :: qs_env
2720 INTEGER, ALLOCATABLE, INTENT(OUT) :: groups(:, :)
2721
2722 INTEGER :: iatom, ig, ikind, ip, is, iset, ishell, &
2723 n, ng, np, ns, nset, nsgf, offset, pass
2724 INTEGER, ALLOCATABLE :: p_shells(:), s_shells(:)
2725 INTEGER, POINTER :: first_sgf(:, :), l(:, :), nshell(:)
2726 TYPE(gto_basis_set_type), POINTER :: basis
2727 TYPE(particle_type), POINTER :: particles(:)
2728 TYPE(qs_kind_type), POINTER :: qs_kinds(:)
2729
2730 CALL get_qs_env(qs_env, particle_set=particles, qs_kind_set=qs_kinds)
2731 ng = 0
2732 DO pass = 1, 2
2733 offset = 0
2734 ig = 0
2735 DO iatom = 1, SIZE(particles)
2736 CALL get_atomic_kind(particles(iatom)%atomic_kind, kind_number=ikind)
2737 CALL get_qs_kind(qs_kinds(ikind), basis_set=basis, nsgf=nsgf)
2738 IF (ASSOCIATED(basis)) THEN
2739 CALL get_gto_basis_set(basis, nset=nset, nshell=nshell, l=l, first_sgf=first_sgf)
2740 ALLOCATE (s_shells(sum(nshell)), p_shells(sum(nshell)))
2741 ns = 0
2742 np = 0
2743 DO iset = 1, nset
2744 DO ishell = 1, nshell(iset)
2745 n = offset + first_sgf(ishell, iset)
2746 SELECT CASE (l(ishell, iset))
2747 CASE (0)
2748 ns = ns + 1
2749 s_shells(ns) = n
2750 CASE (1)
2751 np = np + 1
2752 p_shells(np) = n
2753 END SELECT
2754 END DO
2755 END DO
2756 IF (pass == 1) THEN
2757 ng = ng + ns*np
2758 ELSE
2759 DO is = 1, ns
2760 DO ip = 1, np
2761 ig = ig + 1
2762 n = p_shells(ip)
2763 groups(:, ig) = [s_shells(is), n, n + 1, n + 2]
2764 END DO
2765 END DO
2766 END IF
2767 DEALLOCATE (s_shells, p_shells)
2768 END IF
2769 offset = offset + nsgf
2770 END DO
2771 IF (pass == 1) THEN
2772 ALLOCATE (groups(4, ng))
2773 END IF
2774 END DO
2775 END SUBROUTINE build_wannier90_hybrid_groups
2776
2777! **************************************************************************************************
2778!> \brief Form C(k)^H S(k) projections on all AO trials using the exported MO coefficients.
2779!> \param kpoint Full export mesh and its distributed MO coefficients.
2780!> \param matrix_s Real-space overlap matrices.
2781!> \param sab_nl Overlap neighbour lists.
2782!> \param para_env Global parallel environment.
2783!> \param ispin Selected spin channel.
2784!> \param bands Original MO indices retained in the export.
2785!> \param apply_phase Whether the overlap export uses the additional global Bloch gauge.
2786!> \param phase_center Fractional origin for that gauge.
2787!> \param projections Band/AO/k-point projections, stored only on the source rank.
2788! **************************************************************************************************
2789 SUBROUTINE build_wannier90_ao_projections(kpoint, matrix_s, sab_nl, para_env, ispin, bands, &
2790 apply_phase, phase_center, projections)
2791 TYPE(kpoint_type), POINTER :: kpoint
2792 TYPE(dbcsr_p_type), POINTER :: matrix_s(:, :)
2793 TYPE(neighbor_list_set_p_type), POINTER :: sab_nl(:)
2794 TYPE(mp_para_env_type), POINTER :: para_env
2795 INTEGER, INTENT(IN) :: ispin, bands(:)
2796 LOGICAL, INTENT(IN) :: apply_phase
2797 REAL(kind=dp), INTENT(IN) :: phase_center(3)
2798 COMPLEX(KIND=dp), ALLOCATABLE, INTENT(OUT) :: projections(:, :, :)
2799
2800 COMPLEX(KIND=dp), ALLOCATABLE :: buffer(:, :)
2801 INTEGER :: ik, iklocal, nao, nk, nmo
2802 INTEGER, POINTER :: cell_to_index(:, :, :)
2803 REAL(kind=dp) :: phase
2804 TYPE(cp_cfm_type) :: mo_cfm, sc_cfm
2805 TYPE(cp_cfm_type), POINTER :: source_cfm
2806 TYPE(cp_fm_struct_type), POINTER :: matrix_struct
2807
2808 NULLIFY (matrix_struct, cell_to_index, source_cfm)
2809 CALL get_mo_set(kpoint%kp_env(1)%kpoint_env%mos(ispin), nao=nao, nmo=nmo)
2810 nk = SIZE(kpoint%xkp, 2)
2811 CALL get_kpoint_info(kpoint, cell_to_index=cell_to_index)
2812 CALL cp_fm_struct_create(matrix_struct, nrow_global=nao, ncol_global=nmo, &
2813 para_env=para_env, context=kpoint%blacs_env)
2814 CALL cp_cfm_create(mo_cfm, matrix_struct)
2815 CALL cp_cfm_create(sc_cfm, matrix_struct)
2816 ALLOCATE (buffer(nmo, nao))
2817 IF (para_env%is_source()) THEN
2818 ALLOCATE (projections(SIZE(bands), nao, nk))
2819 ELSE
2820 ALLOCATE (projections(0, 0, 0))
2821 END IF
2822 DO ik = 1, nk
2823 IF (ik >= kpoint%kp_range(1) .AND. ik <= kpoint%kp_range(2)) THEN
2824 iklocal = ik - kpoint%kp_range(1) + 1
2825 source_cfm => kpoint%kp_env(iklocal)%kpoint_env%mos(ispin)%cmo_coeff
2826 ELSE
2827 NULLIFY (source_cfm)
2828 END IF
2829 CALL wannier90_copy_cfm_general(source_cfm, mo_cfm, para_env)
2830 CALL apply_wannier90_kp_matrix(matrix_s, 1, kpoint%xkp(:, ik), cell_to_index, sab_nl, &
2831 mo_cfm, sc_cfm)
2832 CALL cp_cfm_get_submatrix(sc_cfm, buffer, transpose=.true.)
2833 IF (para_env%is_source()) THEN
2834 projections(:, :, ik) = conjg(buffer(bands, :))
2835 IF (apply_phase) THEN
2836 ! M'(k,b) = exp(i[phi(k+b)-phi(k)]) M(k,b) requires A'(k) = exp(-i phi(k)) A(k).
2837 phase = -twopi*dot_product(kpoint%xkp(:, ik), phase_center)
2838 projections(:, :, ik) = projections(:, :, ik)*cmplx(cos(phase), sin(phase), kind=dp)
2839 END IF
2840 END IF
2841 END DO
2842 DEALLOCATE (buffer)
2843 CALL cp_cfm_release(mo_cfm)
2844 CALL cp_cfm_release(sc_cfm)
2845 CALL cp_fm_struct_release(matrix_struct)
2846 END SUBROUTINE build_wannier90_ao_projections
2847
2848! **************************************************************************************************
2849!> \brief Assemble the non-Hermitian inter-k-point AO overlap from its real-space images.
2850!> \param cosmat Real part of <mu,0|exp(-i b.r)|nu,R> for the stored direction of b.
2851!> \param sinmat Imaginary part of the same integrals.
2852!> \param bsign Sign of b relative to the stored integrals; cosine is even and sine is odd.
2853!> \param kleft Reduced left k-point.
2854!> \param kright Reduced right k-point, equal to kleft+b modulo a reciprocal lattice vector.
2855!> \param cell_to_index Real-space image index table.
2856!> \param sab_nl Symmetric orbital neighbour list.
2857!> \param real_matrix Real part of the general AO overlap matrix.
2858!> \param imag_matrix Imaginary part of the general AO overlap matrix.
2859! **************************************************************************************************
2860 SUBROUTINE transform_wannier90_overlap(cosmat, sinmat, bsign, kleft, kright, cell_to_index, &
2861 sab_nl, real_matrix, imag_matrix)
2862 TYPE(dbcsr_p_type), POINTER :: cosmat(:, :), sinmat(:, :)
2863 REAL(kind=dp), INTENT(IN) :: bsign, kleft(3), kright(3)
2864 INTEGER, POINTER :: cell_to_index(:, :, :)
2865 TYPE(neighbor_list_set_p_type), POINTER :: sab_nl(:)
2866 TYPE(dbcsr_type), INTENT(INOUT) :: real_matrix, imag_matrix
2867
2868 INTEGER :: direction, iatom, ic, icol, &
2869 image_cell(3), irow, jatom
2870 LOGICAL :: found, symmetric, transposed
2871 REAL(kind=dp) :: ci, cr, phase
2872 REAL(kind=dp), POINTER :: cb(:, :), ib(:, :), rb(:, :), sb(:, :)
2873 TYPE(neighbor_list_iterator_p_type), POINTER :: iterator(:)
2874
2875 CALL get_neighbor_list_set_p(sab_nl, symmetric=symmetric)
2876 cpassert(symmetric)
2877 CALL dbcsr_set(real_matrix, 0.0_dp)
2878 CALL dbcsr_set(imag_matrix, 0.0_dp)
2879 CALL neighbor_list_iterator_create(iterator, sab_nl)
2880 DO WHILE (neighbor_list_iterate(iterator) == 0)
2881 CALL get_iterator_info(iterator, iatom=iatom, jatom=jatom, cell=image_cell)
2882 ic = cell_to_index(image_cell(1), image_cell(2), image_cell(3))
2883 cpassert(ic > 0)
2884 irow = min(iatom, jatom)
2885 icol = max(iatom, jatom)
2886 CALL dbcsr_get_block_p(cosmat(1, ic)%matrix, irow, icol, cb, found=found)
2887 cpassert(found)
2888 CALL dbcsr_get_block_p(sinmat(1, ic)%matrix, irow, icol, sb, found=found)
2889 cpassert(found)
2890 DO direction = 1, 2
2891 IF (direction == 1) THEN
2892 irow = iatom
2893 icol = jatom
2894 transposed = iatom > jatom
2895 phase = twopi*dot_product(kright, real(image_cell, kind=dp))
2896 ELSE
2897 ! B_nu,mu(-R,b) = exp(i b.R) B_mu,nu(R,b)^T, not its Hermitian transpose.
2898 ! Self-pair images already occur in both directions in the neighbour list.
2899 IF (iatom == jatom) cycle
2900 irow = jatom
2901 icol = iatom
2902 transposed = iatom < jatom
2903 phase = -twopi*dot_product(kleft, real(image_cell, kind=dp))
2904 END IF
2905 CALL dbcsr_get_block_p(real_matrix, irow, icol, rb, found=found)
2906 cpassert(found)
2907 CALL dbcsr_get_block_p(imag_matrix, irow, icol, ib, found=found)
2908 cpassert(found)
2909 cr = cos(phase)
2910 ci = sin(phase)
2911 IF (transposed) THEN
2912 rb(:, :) = rb(:, :) + cr*transpose(cb) - bsign*ci*transpose(sb)
2913 ib(:, :) = ib(:, :) + ci*transpose(cb) + bsign*cr*transpose(sb)
2914 ELSE
2915 rb(:, :) = rb(:, :) + cr*cb(:, :) - bsign*ci*sb(:, :)
2916 ib(:, :) = ib(:, :) + ci*cb(:, :) + bsign*cr*sb(:, :)
2917 END IF
2918 END DO
2919 END DO
2920 CALL neighbor_list_iterator_release(iterator)
2921 END SUBROUTINE transform_wannier90_overlap
2922
2923! **************************************************************************************************
2924!> \brief Apply a complex k-point matrix to a complex MO coefficient matrix.
2925!> \param rsmat real-space matrix images
2926!> \param ispin spin index for rsmat
2927!> \param xkp target k-point coordinate
2928!> \param cell_to_index real-space cell index table
2929!> \param sab_nl overlap neighbor list
2930!> \param coeff input MO coefficients
2931!> \param RESULT output of applying the k-point matrix to the MO coefficients
2932! **************************************************************************************************
2933 SUBROUTINE apply_wannier90_kp_matrix(rsmat, ispin, xkp, cell_to_index, sab_nl, &
2934 coeff, RESULT)
2935 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rsmat
2936 INTEGER, INTENT(IN) :: ispin
2937 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: xkp
2938 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
2939 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
2940 POINTER :: sab_nl
2941 TYPE(cp_cfm_type), INTENT(IN) :: coeff
2942 TYPE(cp_cfm_type), INTENT(INOUT) :: result
2943
2944 INTEGER :: nao, ncol
2945 TYPE(cp_cfm_type) :: kmat_cfm
2946 TYPE(cp_fm_struct_type), POINTER :: matrix_struct_ao, matrix_struct_coeff
2947 TYPE(cp_fm_type) :: mat_imag, mat_real
2948 TYPE(dbcsr_type), POINTER :: kmat_imag, kmat_imag_full, kmat_real, &
2949 kmat_real_full
2950
2951 NULLIFY (matrix_struct_ao, matrix_struct_coeff, kmat_imag, kmat_imag_full, kmat_real, &
2952 kmat_real_full)
2953
2954 CALL cp_cfm_get_info(coeff, nrow_global=nao, ncol_global=ncol, &
2955 matrix_struct=matrix_struct_coeff)
2956
2957 ALLOCATE (kmat_real, kmat_imag, kmat_real_full, kmat_imag_full)
2958 CALL dbcsr_create(kmat_real, template=rsmat(ispin, 1)%matrix, &
2959 matrix_type=dbcsr_type_symmetric)
2960 CALL dbcsr_create(kmat_imag, template=rsmat(ispin, 1)%matrix, &
2961 matrix_type=dbcsr_type_antisymmetric)
2962 CALL dbcsr_create(kmat_real_full, template=rsmat(ispin, 1)%matrix, &
2963 matrix_type=dbcsr_type_no_symmetry)
2964 CALL dbcsr_create(kmat_imag_full, template=rsmat(ispin, 1)%matrix, &
2965 matrix_type=dbcsr_type_no_symmetry)
2966 CALL cp_dbcsr_alloc_block_from_nbl(kmat_real, sab_nl)
2967 CALL cp_dbcsr_alloc_block_from_nbl(kmat_imag, sab_nl)
2968 CALL dbcsr_set(kmat_real, 0.0_dp)
2969 CALL dbcsr_set(kmat_imag, 0.0_dp)
2970 CALL rskp_transform(kmat_real, kmat_imag, rsmat=rsmat, ispin=ispin, &
2971 xkp=xkp, cell_to_index=cell_to_index, sab_nl=sab_nl)
2972 CALL dbcsr_desymmetrize(kmat_real, kmat_real_full)
2973 CALL dbcsr_desymmetrize(kmat_imag, kmat_imag_full)
2974
2975 CALL cp_fm_struct_create(matrix_struct_ao, nrow_global=nao, ncol_global=nao, &
2976 para_env=matrix_struct_coeff%para_env, &
2977 context=matrix_struct_coeff%context)
2978 CALL cp_fm_create(mat_real, matrix_struct_ao)
2979 CALL cp_fm_create(mat_imag, matrix_struct_ao)
2980 CALL copy_dbcsr_to_fm(kmat_real_full, mat_real)
2981 CALL copy_dbcsr_to_fm(kmat_imag_full, mat_imag)
2982
2983 CALL cp_cfm_create(kmat_cfm, matrix_struct_ao)
2984 CALL cp_fm_to_cfm(mat_real, mat_imag, kmat_cfm)
2985 CALL parallel_gemm("N", "N", nao, ncol, nao, cmplx(1.0_dp, 0.0_dp, kind=dp), kmat_cfm, &
2986 coeff, cmplx(0.0_dp, 0.0_dp, kind=dp), result)
2987
2988 CALL cp_fm_release(mat_real)
2989 CALL cp_fm_release(mat_imag)
2990 CALL cp_cfm_release(kmat_cfm)
2991 CALL cp_fm_struct_release(matrix_struct_ao)
2992 CALL dbcsr_deallocate_matrix(kmat_real)
2993 CALL dbcsr_deallocate_matrix(kmat_imag)
2994 CALL dbcsr_deallocate_matrix(kmat_real_full)
2995 CALL dbcsr_deallocate_matrix(kmat_imag_full)
2996
2997 END SUBROUTINE apply_wannier90_kp_matrix
2998
2999! **************************************************************************************************
3000!> \brief Rayleigh-Ritz stabilize a symmetry-reconstructed Wannier90 MO subspace.
3001!> \param dst_cfm transformed MO coefficients
3002!> \param matrix_s real-space overlap matrix
3003!> \param matrix_ks real-space Kohn-Sham matrix
3004!> \param xkp target k-point coordinate
3005!> \param cell_to_index real-space cell index table
3006!> \param sab_nl overlap neighbor list
3007!> \param ispin spin index
3008!> \param eigenvalues Ritz eigenvalues of the stabilized subspace
3009!> \param degenerate_band_tol degeneracy threshold
3010!> \param success true if the subspace was stabilized
3011!> \param reason diagnostic message
3012!> \param aligned_blocks number of stabilized subspaces
3013!> \param aligned_max_size largest stabilized subspace
3014!> \param aligned_min_svalue smallest S(k)-metric eigenvalue
3015!> \param max_residual largest Ritz residual
3016! **************************************************************************************************
3017 SUBROUTINE ritz_stabilize_wannier90_subspace(dst_cfm, matrix_s, matrix_ks, &
3018 xkp, cell_to_index, sab_nl, ispin, eigenvalues, &
3019 degenerate_band_tol, success, reason, aligned_blocks, &
3020 aligned_max_size, aligned_min_svalue, max_residual)
3021 TYPE(cp_cfm_type), INTENT(IN) :: dst_cfm
3022 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s, matrix_ks
3023 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: xkp
3024 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
3025 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
3026 POINTER :: sab_nl
3027 INTEGER, INTENT(IN) :: ispin
3028 REAL(kind=dp), DIMENSION(:), INTENT(INOUT) :: eigenvalues
3029 REAL(kind=dp), INTENT(IN) :: degenerate_band_tol
3030 LOGICAL, INTENT(OUT) :: success
3031 CHARACTER(LEN=*), INTENT(OUT) :: reason
3032 INTEGER, INTENT(OUT) :: aligned_blocks, aligned_max_size
3033 REAL(kind=dp), INTENT(OUT) :: aligned_min_svalue, max_residual
3034
3035 REAL(kind=dp), PARAMETER :: residual_tol = 1.0e-2_dp
3036
3037 COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: block_coeff, dst_coeff, h_block, h_coeff, &
3038 h_projected, h_projected_work, metric_vectors, residual_block, ritz_vectors, s_block, &
3039 s_coeff, s_projected, stabilized
3040 INTEGER :: block_first, block_last, block_size, ib, &
3041 nao, nmo
3042 REAL(kind=dp) :: metric_deviation, norm_value, &
3043 residual_norm
3044 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: metric_values, ritz_values
3045 TYPE(cp_cfm_type) :: h_dst, s_dst
3046 TYPE(cp_fm_struct_type), POINTER :: matrix_struct_metric
3047
3048 success = .false.
3049 reason = ""
3050 aligned_blocks = 0
3051 aligned_max_size = 0
3052 aligned_min_svalue = huge(1.0_dp)
3053 max_residual = 0.0_dp
3054
3055 NULLIFY (matrix_struct_metric)
3056 CALL cp_cfm_get_info(dst_cfm, nrow_global=nao, ncol_global=nmo, &
3057 matrix_struct=matrix_struct_metric)
3058 IF (SIZE(eigenvalues) < nmo) THEN
3059 reason = "not enough eigenvalues for Wannier90 Ritz subspace stabilization"
3060 RETURN
3061 END IF
3062
3063 CALL cp_cfm_create(s_dst, matrix_struct_metric)
3064 CALL cp_cfm_create(h_dst, matrix_struct_metric)
3065
3066 stabilize_subspace: block
3067 CALL apply_wannier90_kp_matrix(matrix_s, 1, xkp, cell_to_index, sab_nl, &
3068 dst_cfm, s_dst)
3069 CALL apply_wannier90_kp_matrix(matrix_ks, ispin, xkp, cell_to_index, sab_nl, &
3070 dst_cfm, h_dst)
3071
3072 ALLOCATE (dst_coeff(nao, nmo), s_coeff(nao, nmo), h_coeff(nao, nmo))
3073 CALL cp_cfm_get_submatrix(dst_cfm, dst_coeff)
3074 CALL cp_cfm_get_submatrix(s_dst, s_coeff)
3075 CALL cp_cfm_get_submatrix(h_dst, h_coeff)
3076
3077 block_first = 1
3078 DO WHILE (block_first <= nmo)
3079 block_last = block_first
3080 DO WHILE (block_last < nmo)
3081 IF (abs(eigenvalues(block_last + 1) - eigenvalues(block_last)) >= degenerate_band_tol) EXIT
3082 block_last = block_last + 1
3083 END DO
3084 block_size = block_last - block_first + 1
3085 IF (block_size > 1) THEN
3086 ! The atom/AO operation fixes the subspace, while the little-group gauge inside an
3087 ! exactly degenerate manifold is arbitrary. Stabilize only that manifold and verify
3088 ! that it is an invariant H(k),S(k) subspace before exporting it to Wannier90.
3089 ALLOCATE (block_coeff(nao, block_size), h_block(nao, block_size), &
3090 h_projected(block_size, block_size), h_projected_work(block_size, block_size), &
3091 metric_vectors(block_size, block_size), residual_block(nao, block_size), &
3092 ritz_vectors(block_size, block_size), s_block(nao, block_size), &
3093 s_projected(block_size, block_size), stabilized(nao, block_size), &
3094 metric_values(block_size), ritz_values(block_size))
3095 block_coeff(:, :) = dst_coeff(:, block_first:block_last)
3096 s_block(:, :) = s_coeff(:, block_first:block_last)
3097 h_block(:, :) = h_coeff(:, block_first:block_last)
3098 s_projected(:, :) = matmul(conjg(transpose(block_coeff)), s_block)
3099 h_projected(:, :) = matmul(conjg(transpose(block_coeff)), h_block)
3100 s_projected(:, :) = 0.5_dp*(s_projected + conjg(transpose(s_projected)))
3101 h_projected(:, :) = 0.5_dp*(h_projected + conjg(transpose(h_projected)))
3102
3103 CALL diag_complex(s_projected, metric_vectors, metric_values)
3104 aligned_min_svalue = min(aligned_min_svalue, minval(metric_values))
3105 metric_deviation = maxval(abs(metric_values - 1.0_dp))
3106 IF (minval(metric_values) < 1.0e-10_dp) THEN
3107 WRITE (reason, "(A,I0,A,ES9.2,A,ES9.2)") &
3108 "singular metric blk=", block_first, " smin=", minval(metric_values), &
3109 " dS=", metric_deviation
3110 max_residual = huge(1.0_dp)
3111 EXIT stabilize_subspace
3112 END IF
3113
3114 DO ib = 1, block_size
3115 metric_vectors(:, ib) = metric_vectors(:, ib)/sqrt(metric_values(ib))
3116 END DO
3117 h_projected_work(:, :) = matmul(h_projected, metric_vectors)
3118 h_projected(:, :) = matmul(conjg(transpose(metric_vectors)), h_projected_work)
3119 h_projected(:, :) = 0.5_dp*(h_projected + conjg(transpose(h_projected)))
3120 CALL diag_complex(h_projected, ritz_vectors, ritz_values)
3121 h_projected_work(:, :) = matmul(metric_vectors, ritz_vectors)
3122 ritz_vectors(:, :) = h_projected_work
3123 stabilized(:, :) = matmul(block_coeff, ritz_vectors)
3124 residual_block(:, :) = matmul(h_block, ritz_vectors)
3125 h_block(:, :) = residual_block
3126 residual_block(:, :) = matmul(s_block, ritz_vectors)
3127 s_block(:, :) = residual_block
3128 DO ib = 1, block_size
3129 norm_value = sqrt(abs(real(dot_product(stabilized(:, ib), s_block(:, ib)), kind=dp)))
3130 IF (norm_value > epsilon(1.0_dp)) THEN
3131 stabilized(:, ib) = stabilized(:, ib)/norm_value
3132 h_block(:, ib) = h_block(:, ib)/norm_value
3133 s_block(:, ib) = s_block(:, ib)/norm_value
3134 END IF
3135 END DO
3136 residual_block(:, :) = h_block
3137 DO ib = 1, block_size
3138 residual_block(:, ib) = residual_block(:, ib) - &
3139 eigenvalues(block_first + ib - 1)*s_block(:, ib)
3140 END DO
3141 residual_norm = maxval(abs(residual_block))
3142 max_residual = max(max_residual, residual_norm)
3143 IF (residual_norm > residual_tol) THEN
3144 WRITE (reason, "(A,I0,A,ES9.2)") &
3145 "blk=", block_first, " dS=", metric_deviation
3146 EXIT stabilize_subspace
3147 END IF
3148
3149 dst_coeff(:, block_first:block_last) = stabilized
3150 h_coeff(:, block_first:block_last) = h_block
3151 s_coeff(:, block_first:block_last) = s_block
3152 aligned_blocks = aligned_blocks + 1
3153 aligned_max_size = max(aligned_max_size, block_size)
3154 DEALLOCATE (block_coeff, h_block, h_projected, h_projected_work, metric_vectors, &
3155 residual_block, ritz_vectors, s_block, s_projected, stabilized, metric_values, &
3156 ritz_values)
3157 END IF
3158 block_first = block_last + 1
3159 END DO
3160
3161 DO ib = 1, nmo
3162 residual_norm = maxval(abs(h_coeff(:, ib) - eigenvalues(ib)*s_coeff(:, ib)))
3163 max_residual = max(max_residual, residual_norm)
3164 END DO
3165 IF (max_residual > residual_tol) THEN
3166 WRITE (reason, "(A,ES10.3)") &
3167 "atom/AO W90 reuse guarded: Ritz residual=", max_residual
3168 EXIT stabilize_subspace
3169 END IF
3170
3171 CALL cp_cfm_set_submatrix(dst_cfm, dst_coeff)
3172
3173 IF (aligned_blocks == 0) aligned_min_svalue = 0.0_dp
3174 success = .true.
3175 END BLOCK stabilize_subspace
3176
3177 CALL cp_cfm_release(s_dst)
3178 CALL cp_cfm_release(h_dst)
3179
3180 END SUBROUTINE ritz_stabilize_wannier90_subspace
3181
3182! **************************************************************************************************
3183!> \brief Reconstruct a Wannier90 export window from a larger symmetry-transformed SCF MO space.
3184!> \param src_cfm transformed source MO window
3185!> \param dst_cfm exported reconstructed MO coefficients
3186!> \param matrix_s real-space overlap matrix
3187!> \param matrix_ks real-space Kohn-Sham matrix
3188!> \param xkp target k-point coordinate
3189!> \param cell_to_index real-space cell index table
3190!> \param sab_nl overlap neighbor list
3191!> \param ispin spin index
3192!> \param eigenvalues reconstructed target eigenvalues for the exported window
3193!> \param nmo_export number of MOs to export
3194!> \param success true if the reconstructed window is an invariant H(k),S(k) subspace
3195!> \param reason diagnostic message
3196!> \param min_svalue smallest S(k)-metric eigenvalue in the source window
3197!> \param max_residual largest target Ritz residual
3198! **************************************************************************************************
3199 SUBROUTINE ritz_reconstruct_wannier90_window(src_cfm, dst_cfm, matrix_s, &
3200 matrix_ks, xkp, cell_to_index, sab_nl, ispin, &
3201 eigenvalues, nmo_export, success, reason, min_svalue, &
3202 max_residual)
3203 TYPE(cp_cfm_type), INTENT(IN) :: src_cfm, dst_cfm
3204 TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s, matrix_ks
3205 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: xkp
3206 INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
3207 TYPE(neighbor_list_set_p_type), DIMENSION(:), &
3208 POINTER :: sab_nl
3209 INTEGER, INTENT(IN) :: ispin
3210 REAL(kind=dp), DIMENSION(:), INTENT(INOUT) :: eigenvalues
3211 INTEGER, INTENT(IN) :: nmo_export
3212 LOGICAL, INTENT(OUT) :: success
3213 CHARACTER(LEN=*), INTENT(OUT) :: reason
3214 REAL(kind=dp), INTENT(OUT) :: min_svalue, max_residual
3215
3216 REAL(kind=dp), PARAMETER :: eigenvalue_tol = 1.0e-6_dp, &
3217 residual_tol = 1.0e-7_dp
3218
3219 COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: coeff_work, h_coeff, h_projected, &
3220 h_projected_work, metric_vectors, residual_block, ritz_vectors, s_coeff, s_projected, &
3221 source_coeff, stabilized
3222 INTEGER :: ib, nao, nmo_source
3223 REAL(kind=dp) :: max_eigenvalue_shift, metric_deviation, &
3224 norm_value
3225 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: metric_values, ritz_values, &
3226 source_eigenvalues
3227 TYPE(cp_cfm_type) :: h_src, s_src
3228 TYPE(cp_fm_struct_type), POINTER :: matrix_struct_metric
3229
3230 success = .false.
3231 reason = ""
3232 min_svalue = huge(1.0_dp)
3233 max_residual = huge(1.0_dp)
3234
3235 NULLIFY (matrix_struct_metric)
3236 CALL cp_cfm_get_info(src_cfm, nrow_global=nao, ncol_global=nmo_source, &
3237 matrix_struct=matrix_struct_metric)
3238 IF (nmo_export > nmo_source) THEN
3239 reason = "Wannier90 export window is larger than the transformed SCF MO space"
3240 RETURN
3241 END IF
3242 IF (SIZE(eigenvalues) < nmo_export) THEN
3243 reason = "not enough eigenvalue storage for Wannier90 source-window reconstruction"
3244 RETURN
3245 END IF
3246
3247 CALL cp_cfm_create(s_src, matrix_struct_metric)
3248 CALL cp_cfm_create(h_src, matrix_struct_metric)
3249
3250 CALL apply_wannier90_kp_matrix(matrix_s, 1, xkp, cell_to_index, sab_nl, &
3251 src_cfm, s_src)
3252 CALL apply_wannier90_kp_matrix(matrix_ks, ispin, xkp, cell_to_index, sab_nl, &
3253 src_cfm, h_src)
3254
3255 ALLOCATE (source_coeff(nao, nmo_source), s_coeff(nao, nmo_source), &
3256 h_coeff(nao, nmo_source), h_projected(nmo_source, nmo_source), &
3257 h_projected_work(nmo_source, nmo_source), metric_vectors(nmo_source, nmo_source), &
3258 residual_block(nao, nmo_export), ritz_vectors(nmo_source, nmo_source), &
3259 s_projected(nmo_source, nmo_source), stabilized(nao, nmo_export), &
3260 coeff_work(nao, nmo_source), metric_values(nmo_source), ritz_values(nmo_source), &
3261 source_eigenvalues(nmo_export))
3262 source_eigenvalues(1:nmo_export) = eigenvalues(1:nmo_export)
3263 CALL cp_cfm_get_submatrix(src_cfm, source_coeff)
3264 CALL cp_cfm_get_submatrix(s_src, s_coeff)
3265 CALL cp_cfm_get_submatrix(h_src, h_coeff)
3266 s_projected(:, :) = matmul(conjg(transpose(source_coeff)), s_coeff)
3267 h_projected(:, :) = matmul(conjg(transpose(source_coeff)), h_coeff)
3268 s_projected(:, :) = 0.5_dp*(s_projected + conjg(transpose(s_projected)))
3269 h_projected(:, :) = 0.5_dp*(h_projected + conjg(transpose(h_projected)))
3270
3271 reconstruct_window: block
3272 CALL diag_complex(s_projected, metric_vectors, metric_values)
3273 min_svalue = minval(metric_values)
3274 metric_deviation = maxval(abs(metric_values - 1.0_dp))
3275 IF (min_svalue < 1.0e-10_dp) THEN
3276 WRITE (reason, "(A,ES9.2,A,ES9.2)") &
3277 "singular expanded metric smin=", min_svalue, " dS=", metric_deviation
3278 EXIT reconstruct_window
3279 END IF
3280
3281 DO ib = 1, nmo_source
3282 metric_vectors(:, ib) = metric_vectors(:, ib)/sqrt(metric_values(ib))
3283 END DO
3284 h_projected_work(:, :) = matmul(h_projected, metric_vectors)
3285 h_projected(:, :) = matmul(conjg(transpose(metric_vectors)), h_projected_work)
3286 h_projected(:, :) = 0.5_dp*(h_projected + conjg(transpose(h_projected)))
3287 CALL diag_complex(h_projected, ritz_vectors, ritz_values)
3288 h_projected_work(:, :) = matmul(metric_vectors, ritz_vectors)
3289 ritz_vectors(:, :) = h_projected_work
3290 stabilized(:, :) = matmul(source_coeff, ritz_vectors(:, 1:nmo_export))
3291 coeff_work(:, :) = matmul(h_coeff, ritz_vectors)
3292 h_coeff(:, :) = coeff_work
3293 coeff_work(:, :) = matmul(s_coeff, ritz_vectors)
3294 s_coeff(:, :) = coeff_work
3295 DO ib = 1, nmo_export
3296 norm_value = sqrt(abs(real(dot_product(stabilized(:, ib), s_coeff(:, ib)), kind=dp)))
3297 IF (norm_value > epsilon(1.0_dp)) THEN
3298 stabilized(:, ib) = stabilized(:, ib)/norm_value
3299 h_coeff(:, ib) = h_coeff(:, ib)/norm_value
3300 s_coeff(:, ib) = s_coeff(:, ib)/norm_value
3301 END IF
3302 END DO
3303 residual_block(:, :) = h_coeff(:, 1:nmo_export)
3304 DO ib = 1, nmo_export
3305 residual_block(:, ib) = residual_block(:, ib) - ritz_values(ib)*s_coeff(:, ib)
3306 END DO
3307 max_residual = maxval(abs(residual_block))
3308 IF (max_residual > residual_tol) THEN
3309 WRITE (reason, "(A,ES9.2)") &
3310 "expanded dS=", metric_deviation
3311 EXIT reconstruct_window
3312 END IF
3313 max_eigenvalue_shift = maxval(abs(ritz_values(1:nmo_export) - source_eigenvalues(1:nmo_export)))
3314 IF (max_eigenvalue_shift > eigenvalue_tol) THEN
3315 WRITE (reason, "(A,ES9.2)") &
3316 "expanded dS=", metric_deviation
3317 EXIT reconstruct_window
3318 END IF
3319
3320 CALL cp_cfm_set_submatrix(dst_cfm, stabilized)
3321 success = .true.
3322
3323 END BLOCK reconstruct_window
3324
3325 DEALLOCATE (source_coeff, s_coeff, h_coeff, h_projected, h_projected_work, metric_vectors, &
3326 residual_block, ritz_vectors, s_projected, stabilized, coeff_work, metric_values, &
3327 ritz_values, source_eigenvalues)
3328 CALL cp_cfm_release(s_src)
3329 CALL cp_cfm_release(h_src)
3330
3331 END SUBROUTINE ritz_reconstruct_wannier90_window
3332
3333! **************************************************************************************************
3334!> \brief Map the full Wannier90 mesh to SCF representative k-points and symmetry operations.
3335!> \param kpoint full Wannier90 export k-point object
3336!> \param qs_kpoint SCF k-point object
3337!> \param source_kpoint source representative index for each full k-point
3338!> \param sym_index symmetry entry in source kp_sym; 0 direct, -1 time reversal only
3339!> \param success true if every full k-point was mapped
3340!> \param reason diagnostic message
3341! **************************************************************************************************
3342 SUBROUTINE build_wannier90_scf_mapping(kpoint, qs_kpoint, source_kpoint, sym_index, success, &
3343 reason)
3344 TYPE(kpoint_type), POINTER :: kpoint, qs_kpoint
3345 INTEGER, ALLOCATABLE, DIMENSION(:), INTENT(OUT) :: source_kpoint, sym_index
3346 LOGICAL, INTENT(OUT) :: success
3347 CHARACTER(LEN=*), INTENT(OUT) :: reason
3348
3349 INTEGER :: ik, ikred, imatch, isym, nfull
3350 TYPE(kpoint_sym_type), POINTER :: kpsym
3351
3352 success = .false.
3353 reason = ""
3354 nfull = kpoint%nkp
3355 ALLOCATE (source_kpoint(nfull), sym_index(nfull))
3356 source_kpoint(:) = 0
3357 sym_index(:) = 0
3358
3359 DO ikred = 1, qs_kpoint%nkp
3360 imatch = find_matching_kpoint(kpoint%xkp, qs_kpoint%xkp(1:3, ikred))
3361 IF (imatch > 0 .AND. source_kpoint(imatch) == 0) THEN
3362 source_kpoint(imatch) = ikred
3363 sym_index(imatch) = 0
3364 END IF
3365 END DO
3366
3367 ! Prefer pure time-reversal partners before general atom/AO symmetry operations.
3368 DO ikred = 1, qs_kpoint%nkp
3369 imatch = find_matching_kpoint(kpoint%xkp, -qs_kpoint%xkp(1:3, ikred))
3370 IF (imatch > 0 .AND. source_kpoint(imatch) == 0) THEN
3371 source_kpoint(imatch) = ikred
3372 sym_index(imatch) = -1
3373 END IF
3374 END DO
3375
3376 IF (ASSOCIATED(qs_kpoint%kp_sym)) THEN
3377 DO ikred = 1, qs_kpoint%nkp
3378 kpsym => qs_kpoint%kp_sym(ikred)%kpoint_sym
3379 IF (.NOT. ASSOCIATED(kpsym)) cycle
3380 IF (.NOT. kpsym%apply_symmetry) cycle
3381 DO isym = 1, kpsym%nwred
3382 imatch = find_matching_kpoint(kpoint%xkp, kpsym%xkp(1:3, isym))
3383 IF (imatch > 0 .AND. source_kpoint(imatch) == 0) THEN
3384 source_kpoint(imatch) = ikred
3385 sym_index(imatch) = isym
3386 END IF
3387 END DO
3388 END DO
3389 END IF
3390
3391 DO ik = 1, nfull
3392 IF (source_kpoint(ik) == 0) THEN
3393 reason = "not all full-mesh k-points are represented by the SCF symmetry orbits"
3394 RETURN
3395 END IF
3396 END DO
3397 success = .true.
3398
3399 END SUBROUTINE build_wannier90_scf_mapping
3400
3401! **************************************************************************************************
3402!> \brief Find a fractional k-point in a periodic mesh.
3403!> \param xkp_mesh mesh coordinates
3404!> \param xkp_search coordinate to find
3405!> \return matching index, or zero when no match is found
3406! **************************************************************************************************
3407 INTEGER FUNCTION find_matching_kpoint(xkp_mesh, xkp_search) RESULT(ik_match)
3408 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: xkp_mesh
3409 REAL(kind=dp), DIMENSION(3), INTENT(IN) :: xkp_search
3410
3411 INTEGER :: ik
3412
3413 ik_match = 0
3414 DO ik = 1, SIZE(xkp_mesh, 2)
3415 IF (kpoint_same_periodic(xkp_mesh(1:3, ik), xkp_search)) THEN
3416 ik_match = ik
3417 RETURN
3418 END IF
3419 END DO
3420
3421 END FUNCTION find_matching_kpoint
3422
3423! **************************************************************************************************
3424!> \brief Infer a tensor-product Wannier90 mesh from explicit fractional k-point coordinates.
3425!> \param kpt_latt explicit k-point coordinates in reciprocal-lattice units
3426!> \param mp_grid inferred mesh dimensions
3427!> \param valid true if the coordinate set is compatible with a tensor-product mesh
3428! **************************************************************************************************
3429 SUBROUTINE infer_wannier_mp_grid(kpt_latt, mp_grid, valid)
3430 REAL(kind=dp), DIMENSION(:, :), INTENT(IN) :: kpt_latt
3431 INTEGER, DIMENSION(3), INTENT(OUT) :: mp_grid
3432 LOGICAL, INTENT(OUT) :: valid
3433
3434 INTEGER :: coord_id, i, idim, idx, n_unique, &
3435 num_kpts, stride, unique_id
3436 LOGICAL :: known
3437 LOGICAL, ALLOCATABLE, DIMENSION(:) :: seen
3438 REAL(kind=dp) :: coord
3439 REAL(kind=dp), ALLOCATABLE, DIMENSION(:, :) :: unique_coord
3440
3441 num_kpts = SIZE(kpt_latt, 2)
3442 mp_grid(:) = 0
3443 ALLOCATE (unique_coord(3, num_kpts))
3444 DO idim = 1, 3
3445 n_unique = 0
3446 DO i = 1, num_kpts
3447 coord = kpt_latt(idim, i) - floor(kpt_latt(idim, i))
3448 IF (abs(coord - 1.0_dp) < 1.0e-8_dp) coord = 0.0_dp
3449 known = .false.
3450 DO unique_id = 1, n_unique
3451 IF (abs(unique_coord(idim, unique_id) - coord) < 1.0e-8_dp) THEN
3452 known = .true.
3453 EXIT
3454 END IF
3455 END DO
3456 IF (.NOT. known) THEN
3457 n_unique = n_unique + 1
3458 unique_coord(idim, n_unique) = coord
3459 END IF
3460 END DO
3461 mp_grid(idim) = n_unique
3462 END DO
3463 valid = (mp_grid(1)*mp_grid(2)*mp_grid(3) == num_kpts)
3464 IF (valid) THEN
3465 ALLOCATE (seen(num_kpts))
3466 seen(:) = .false.
3467 DO i = 1, num_kpts
3468 idx = 1
3469 stride = 1
3470 DO idim = 1, 3
3471 coord = kpt_latt(idim, i) - floor(kpt_latt(idim, i))
3472 IF (abs(coord - 1.0_dp) < 1.0e-8_dp) coord = 0.0_dp
3473 coord_id = 0
3474 DO unique_id = 1, mp_grid(idim)
3475 IF (abs(unique_coord(idim, unique_id) - coord) < 1.0e-8_dp) THEN
3476 coord_id = unique_id
3477 EXIT
3478 END IF
3479 END DO
3480 cpassert(coord_id > 0)
3481 idx = idx + (coord_id - 1)*stride
3482 stride = stride*mp_grid(idim)
3483 END DO
3484 IF (seen(idx)) valid = .false.
3485 seen(idx) = .true.
3486 END DO
3487 valid = valid .AND. all(seen)
3488 DEALLOCATE (seen)
3489 END IF
3490 DEALLOCATE (unique_coord)
3491
3492 END SUBROUTINE infer_wannier_mp_grid
3493
3494END MODULE qs_wannier90
static GRID_HOST_DEVICE int modulo(int a, int m)
Equivalent of Fortran's MODULO, which always return a positive number. https://gcc....
static GRID_HOST_DEVICE int idx(const orbital a)
Return coset index of given orbital angular momentum.
Define the atomic kind types and their sub types.
subroutine, public get_atomic_kind(atomic_kind, fist_potential, element_symbol, name, mass, kind_number, natom, atom_list, rcov, rvdw, z, qeff, apol, cpol, mm_radius, shell, shell_active, damping)
Get attributes of an atomic kind.
subroutine, public get_gto_basis_set(gto_basis_set, name, aliases, norm_type, kind_radius, ncgf, nset, nsgf, cgf_symbol, sgf_symbol, norm_cgf, set_radius, lmax, lmin, lx, ly, lz, m, ncgf_set, npgf, nsgf_set, nshell, cphi, pgf_radius, sphi, scon, zet, first_cgf, first_sgf, l, last_cgf, last_sgf, n, gcc, maxco, maxl, maxpgf, maxsgf_set, maxshell, maxso, nco_sum, npgf_sum, nshell_sum, maxder, short_kind_radius, npgf_seg_sum, ccon)
...
collects all references to literature in CP2K as new algorithms / method are included from literature...
integer, save, public gresch2017
integer, save, public soluyanov2011
Handles all functions related to the CELL.
Definition cell_types.F:15
subroutine, public get_cell(cell, alpha, beta, gamma, deth, orthorhombic, abc, periodic, h, h_inv, symmetry_id, tag)
Get informations about a simulation cell.
Definition cell_types.F:234
methods related to the blacs parallel environment
Represents a complex full matrix distributed on many processors.
subroutine, public cp_cfm_get_submatrix(fm, target_m, start_row, start_col, n_rows, n_cols, transpose)
Extract a sub-matrix from the full matrix: op(target_m)(1:n_rows,1:n_cols) = fm(start_row:start_row+n...
subroutine, public cp_cfm_get_element(matrix, irow_global, icol_global, alpha)
Get the matrix element by its global index.
subroutine, public cp_cfm_copy_general(source, destination, para_env)
General copy of a cfm matrix to another cfm matrix. Uses non-blocking MPI rather than ScaLAPACK.
subroutine, public cp_cfm_release(matrix)
Releases a full matrix.
subroutine, public cp_fm_to_cfm(msourcer, msourcei, mtarget)
Construct a complex full matrix by taking its real and imaginary parts from two separate real-value f...
subroutine, public cp_cfm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
Creates a new full matrix with the given structure.
subroutine, public cp_cfm_get_info(matrix, name, nrow_global, ncol_global, nrow_block, ncol_block, nrow_local, ncol_local, row_indices, col_indices, local_data, context, matrix_struct, para_env)
Returns information about a full matrix.
subroutine, public cp_cfm_set_submatrix(matrix, new_values, start_row, start_col, n_rows, n_cols, alpha, beta, transpose)
Set a sub-matrix of the full matrix: matrix(start_row:start_row+n_rows,start_col:start_col+n_cols) = ...
Defines control structures, which contain the parameters and the settings for the DFT-based calculati...
subroutine, public dbcsr_scale(matrix, alpha_scalar)
...
subroutine, public dbcsr_deallocate_matrix(matrix)
...
subroutine, public dbcsr_desymmetrize(matrix_a, matrix_b)
...
subroutine, public dbcsr_get_block_p(matrix, row, col, block, found, row_size, col_size)
...
subroutine, public dbcsr_set(matrix, alpha)
...
subroutine, public dbcsr_add(matrix_a, matrix_b, alpha_scalar, beta_scalar)
...
DBCSR operations in CP2K.
subroutine, public copy_dbcsr_to_fm(matrix, fm, plan, check_plan)
Copy a DBCSR matrix to a BLACS matrix.
Utility routines to open and close files.
Definition cp_files.F:22
subroutine, public open_file(file_name, unit_number, file_status, file_form, file_action, file_position, file_pad, file_access, debug)
Opens a file and returns its logical unit number.
Definition cp_files.F:197
subroutine, public close_file(unit_number, file_status)
Closes an open file given by its logical unit number.
Definition cp_files.F:64
represent the structure of a full matrix
subroutine, public cp_fm_struct_create(fmstruct, para_env, context, nrow_global, ncol_global, nrow_block, ncol_block, descriptor, first_p_pos, local_leading_dimension, template_fmstruct, square_blocks, force_block)
allocates and initializes a full matrix structure
subroutine, public cp_fm_struct_release(fmstruct)
releases a full matrix structure
represent a full matrix distributed on many processors
Definition cp_fm_types.F:15
subroutine, public cp_fm_create(matrix, matrix_struct, name, nrow, ncol, set_zero)
creates a new full matrix with the given structure
various routines to log and control the output. The idea is that decisions about where to log should ...
integer function, public cp_logger_get_default_io_unit(logger)
returns the unit nr for the ionode (-1 on all other processors) skips as well checks if the procs cal...
objects that represent the structure of input sections and the data contained in an input section
recursive type(section_vals_type) function, pointer, public section_vals_get_subs_vals(section_vals, subsection_name, i_rep_section, can_return_null)
returns the values of the requested subsection
subroutine, public section_vals_get(section_vals, ref_count, n_repetition, n_subs_vals_rep, section, explicit)
returns various attributes about the section_vals
subroutine, public section_vals_val_get(section_vals, keyword_name, i_rep_section, i_rep_val, n_rep_val, val, l_val, i_val, r_val, c_val, l_vals, i_vals, r_vals, c_vals, explicit)
returns the requested value
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
integer, parameter, public default_path_length
Definition kinds.F:58
Routines needed for kpoint calculation.
subroutine, public kpoint_initialize_mo_set(kpoint)
...
subroutine, public rskp_transform(rmatrix, cmatrix, rsmat, ispin, xkp, cell_to_index, sab_nl, is_complex, rs_sign)
Transformation of real space matrices to a kpoint.
subroutine, public kpoint_init_cell_index(kpoint, sab_nl, para_env, nimages)
Generates the mapping of cell indices and linear RS index CELL (0,0,0) is always mapped to index 1.
subroutine, public kpoint_initialize_mos(kpoint, mos, added_mos, for_aux_fit)
Initialize a set of MOs and density matrix for each kpoint (kpoint group).
subroutine, public kpoint_initialize(kpoint, particle_set, cell)
Generate the kpoints and initialize the kpoint environment.
subroutine, public kpoint_env_initialize(kpoint, para_env, blacs_env, with_aux_fit)
Initialize the kpoint environment.
Utilities for transforming k-point MO coefficients under symmetry operations.
logical function, public kpoint_same_periodic(xkp_a, xkp_b)
Compare two fractional k-points modulo reciprocal lattice vectors.
subroutine, public kpoint_transform_scf_mo(src, dst, qs_kpoint, ikred, isym, success, reason)
Transform one SCF MO coefficient matrix to an equivalent full-mesh k-point.
Types and basic routines needed for a kpoint calculation.
subroutine, public kpoint_release(kpoint)
Release a kpoint environment, deallocate all data.
subroutine, public get_kpoint_info(kpoint, kp_scheme, nkp_grid, kp_shift, symmetry, verbose, full_grid, use_real_wfn, eps_geo, parallel_group_size, kp_range, nkp, xkp, wkp, para_env, blacs_env_all, para_env_kp, para_env_inter_kp, blacs_env, kp_env, kp_aux_env, mpools, iogrp, nkp_groups, kp_dist, cell_to_index, index_to_cell, sab_nl, sab_nl_nosym, inversion_symmetry_only, symmetry_backend, symmetry_reduction_method, gamma_centered, lattice_fft)
Retrieve information from a kpoint environment.
subroutine, public kpoint_create(kpoint)
Create a kpoint environment.
Crystal-symmetry routines originating from the K290/ACMI code.
Definition kpsym.F:14
Machine interface based on Fortran 2003 and POSIX.
Definition machine.F:17
integer, parameter, public timestamp_length
Definition machine.F:48
subroutine, public m_timestamp(timestamp)
Returns a human readable timestamp.
Definition machine.F:384
Definition of mathematical constants and functions.
real(kind=dp), parameter, public twopi
Collection of simple mathematical functions and subroutines.
Definition mathlib.F:15
subroutine, public diag_complex(matrix, eigenvectors, eigenvalues)
Diagonalizes a local complex Hermitian matrix using LAPACK. Based on cp_cfm_heevd.
Definition mathlib.F:1909
Interface to the message passing library MPI.
basic linear algebra operations for full matrixes
Define the data structure for the particle information.
Definition of physical constants:
Definition physcon.F:68
real(kind=dp), parameter, public evolt
Definition physcon.F:183
real(kind=dp), parameter, public angstrom
Definition physcon.F:144
subroutine, public get_qs_env(qs_env, atomic_kind_set, qs_kind_set, cell, super_cell, cell_ref, use_ref_cell, kpoints, dft_control, mos, sab_orb, sab_all, qmmm, qmmm_periodic, mimic, sac_ae, sac_ppl, sac_lri, sap_ppnl, sab_vdw, sab_scp, sap_oce, sab_lrc, sab_se, sab_xtbe, sab_tbe, sab_core, sab_xb, sab_xtb_pp, sab_xtb_nonbond, sab_almo, sab_kp, sab_kp_nosym, sab_cneo, particle_set, energy, force, matrix_h, matrix_h_im, matrix_ks, matrix_ks_im, matrix_vxc, run_rtp, rtp, matrix_h_kp, matrix_h_im_kp, matrix_ks_kp, matrix_ks_im_kp, matrix_vxc_kp, kinetic_kp, matrix_s_kp, matrix_w_kp, matrix_s_ri_aux_kp, matrix_s, matrix_s_ri_aux, matrix_w, matrix_p_mp2, matrix_p_mp2_admm, matrix_vhxc, rho, rho_xc, pw_env, ewald_env, ewald_pw, active_space, mpools, input, para_env, blacs_env, scf_control, rel_control, kinetic, qs_charges, vppl, xcint_weights, rho_core, rho_nlcc, rho_nlcc_g, ks_env, ks_qmmm_env, wf_history, scf_env, local_particles, local_molecules, distribution_2d, dbcsr_dist, molecule_kind_set, molecule_set, subsys, cp_subsys, oce, local_rho_set, rho_atom_set, task_list, task_list_soft, rho0_atom_set, rho0_mpole, rhoz_set, rhoz_cneo_set, ecoul_1c, rho0_s_rs, rho0_s_gs, rhoz_cneo_s_rs, rhoz_cneo_s_gs, do_kpoints, has_unit_metric, requires_mo_derivs, mo_derivs, mo_loc_history, nkind, natom, nelectron_total, nelectron_spin, efield, neighbor_list_id, linres_control, xas_env, virial, cp_ddapc_env, cp_ddapc_ewald, outer_scf_history, outer_scf_ihistory, x_data, et_coupling, dftb_potential, results, se_taper, se_store_int_env, se_nddo_mpole, se_nonbond_env, admm_env, lri_env, lri_density, exstate_env, ec_env, harris_env, dispersion_env, gcp_env, vee, rho_external, external_vxc, mask, mp2_env, bs_env, kg_env, wanniercentres, atprop, ls_scf_env, do_transport, transport_env, v_hartree_rspace, s_mstruct_changed, rho_changed, potential_changed, forces_up_to_date, mscfg_env, almo_scf_env, gradient_history, variable_history, embed_pot, spin_embed_pot, polar_env, mos_last_converged, eeq, rhs, do_rixs, tb_tblite)
Get the QUICKSTEP environment.
subroutine, public qs_env_release(qs_env)
releases the given qs_env (see doc/ReferenceCounting.html)
Initialize a qs_env for kpoint calculations starting from a gamma point qs_env.
Definition qs_gamma2kp.F:14
subroutine, public create_kp_from_gamma(qs_env, qs_env_kp, with_xc_terms)
...
Definition qs_gamma2kp.F:66
Define the quickstep kind type and their sub types.
subroutine, public get_qs_kind(qs_kind, basis_set, basis_type, ncgf, nsgf, all_potential, tnadd_potential, gth_potential, sgp_potential, upf_potential, cneo_potential, se_parameter, dftb_parameter, xtb_parameter, dftb3_param, zatom, zeff, elec_conf, mao, lmax_dftb, alpha_core_charge, ccore_charge, core_charge, core_charge_radius, paw_proj_set, paw_atom, hard_radius, hard0_radius, max_rad_local, covalent_radius, vdw_radius, gpw_type_forced, harmonics, max_iso_not0, max_s_harm, grid_atom, ngrid_ang, ngrid_rad, lmax_rho0, dft_plus_u_atom, l_of_dft_plus_u, n_of_dft_plus_u, u_minus_j, hund_j, u_of_dft_plus_u, j_of_dft_plus_u, alpha_of_dft_plus_u, beta_of_dft_plus_u, j0_of_dft_plus_u, occupation_of_dft_plus_u, dispersion, bs_occupation, magnetization, no_optimize, addel, laddel, naddel, orbitals, max_scf, eps_scf, smear, u_ramping, u_minus_j_target, eps_u_ramping, proj_shell_charge, lr_atom, do_mtlr, u_j_loop, ao_coef, init_u_ramping_each_scf, reltmat, ghost, monovalent, floating, name, element_symbol, pao_basis_size, pao_model_file, pao_potentials, pao_descriptors, nelec)
Get attributes of an atomic kind.
Definition and initialisation of the mo data type.
Definition qs_mo_types.F:22
subroutine, public get_mo_set(mo_set, maxocc, homo, lfomo, nao, nelectron, n_el_f, nmo, eigenvalues, occupation_numbers, mo_coeff, mo_coeff_b, uniform_occupation, kts, mu, flexible_electron_count, cmo_coeff)
Get the components of a MO set data structure.
Calculates the moment integrals <a|r^m|b> and <a|r x d/dr|b>.
Definition qs_moments.F:14
subroutine, public build_berry_kpoint_matrix(qs_env, cosmat, sinmat, kvec, basis_type, ordered, ordered_kpoints)
...
Define the neighbor list data types and the corresponding functionality.
subroutine, public neighbor_list_iterator_create(iterator_set, nl, search, nthread)
Neighbor list iterator functions.
subroutine, public neighbor_list_iterator_release(iterator_set)
...
subroutine, public get_neighbor_list_set_p(neighbor_list_sets, nlist, symmetric)
Return the components of the first neighbor list set.
integer function, public neighbor_list_iterate(iterator_set, mepos)
...
subroutine, public get_iterator_info(iterator_set, mepos, ikind, jkind, nkind, ilist, nlist, inode, nnode, iatom, jatom, r, cell)
...
Different diagonalization schemes that can be used for the iterative solution of the eigenvalue probl...
subroutine, public do_general_diag_kp(matrix_ks, matrix_s, kpoints, scf_env, scf_control, update_p, diis_step, diis_error, qs_env, probe, added_mos_auto_grow, potential)
Kpoint diagonalization routine Transforms matrices to kpoint, distributes kpoint groups,...
module that contains the definitions of the scf types
Interface to Wannier90 code.
subroutine, public prepare_wannier90_scf_mos(kpoint, qs_kpoint, matrix_s, matrix_ks, cell_to_index, sab_nl, para_env, success, reason, aligned_degenerate_blocks, aligned_degenerate_max_size, aligned_degenerate_min_svalue, spin_channel)
Reconstruct a full Wannier90 k-point MO set from the SCF k-point MOs.
subroutine, public wannier90_interface(input, logger, qs_env)
...
parameters that control an scf iteration
subroutine, public v_soc_xyz_from_pseudopotential(qs_env, mat_v_soc_xyz)
V^SOC_µν^(α),R = ħ/2 < ϕ_µ cell O | sum_ℓ ΔV_ℓ^SO(r,r') L^(α) | ϕ_ν cell R>, α = x,...
Inversion action on CP2K Gaussian Bloch coefficients, including lattice phases.
subroutine, public gaussian_inversion_action(qs_env, k, center, mapping, phase, tolerance, status)
Construct sparse AO inversion at a TRIM for the operation r -> 2*center-r.
Versioned Gaussian-basis snapshots for cross-geometry topology overlaps. Atomic units,...
subroutine, public topology_state_begin(qs_env, filename, nao, nband, npoint, nspin, ntotal, spin_channel, bands, unit)
Write basis metadata; all ranks call, only the source writes.
subroutine, public topology_state_point(unit, point, k, eigenvalues, coefficients)
Write one point with eigenvalues in Hartree and physical AO coefficients.
Native character decomposition and metric-aware inversion representations.
subroutine, public inversion_representation(metric, coeff, mapping, phase, energies, check_tr, tolerance, energy_tolerance, counts, error, energy_error, status, diagnostics)
Inversion irreps of an isolated scalar or spinor eigenspace in an AO metric.
Exact inversion-subgroup EBR and symmetry-indicator analysis (spinful TR).
subroutine, public inversion_indicators(odd, pairs, dimension, strong, weak, z4, status)
Fu–Kane indices and inversion indicator with an explicit sign convention.
subroutine, public inversion_ebr_signature(odd, pairs, signed_atomic, nonnegative_atomic, coefficients, status)
Exact signed/nonnegative EBR-signature test for the inversion subgroup.
Local Wilson-loop linear algebra, independent of the electronic-structure representation....
real(kind=dp) function, public wcc_distance(a, b)
Minimum maximal cyclic matching distance between two WCC sets.
subroutine, public wilson_step(product, overlap, minimum_sv, status, sv_tol)
Multiply by the unitary polar factor of an overlap. Reject rank-deficient links.
subroutine, public z2_from_wcc(wcc, invariant, status, pair_tol)
Largest-gap crossing parity for an ordered time-reversal half-surface. Endpoint degeneracy is necessa...
logical function, public surface_resolved(wcc)
Conservative neighbouring-line movement and largest-gap separation checks.
subroutine, public wilson_spectrum(product, wcc, berry, status)
Sorted Wilson eigenphases in reduced units and total Berry phase in radians.
subroutine, public chern_from_wcc(wcc, invariant, winding, status, closure_tol)
First Chern number from determinant Wilson-phase winding on a closed surface. Both transverse endpoin...
Read explicit Wannier90 overlap connections, including nonuniform closed loops.
subroutine, public read_wannier90_nnkpts(filename, real_lattice, recip_lattice, kpt, nnlist, nncell)
Read and validate the cell, points, and directed connections of an nnkp file.
Gauge-covariant selection of fixed trial orbitals for Wannier90.
subroutine, public wannier90_window_mask(eigenvalues, num_wann, windows, window_set, allowed)
Restrict fixed trial selection to the bands admitted by disentanglement windows.
subroutine, public wannier90_select_projections(candidates, para_env, amn, pivots, min_svalue, anchor_kpoint)
Select the same trial orbitals at every k-point using a mesh-wide pivoted QR.
subroutine, public wannier90_hybrid_candidates(ao, groups, candidates)
Form tetrahedral s/p trials from the full AO bank before selecting projections.
Outtakes from Wannier90 code.
Definition wannier90.F:66
subroutine, public wannier_setup(mp_grid_loc, num_kpts_loc, real_lattice_loc, recip_lattice_loc, kpt_latt_loc, nntot_loc, nnlist_loc, nncell_loc, iounit)
...
Definition wannier90.F:140
Type defining parameters related to the simulation cell.
Definition cell_types.F:60
represent a blacs multidimensional parallel environment (for the mpi corrispective see cp_paratypes/m...
Represent a complex full matrix.
keeps the information about the structure of a full matrix
represent a full matrix
type of a logger, at the moment it contains just a print level starting at which level it should be l...
Keeps information about a specific k-point.
Keeps symmetry information about a specific k-point.
Contains information about kpoints.
stores all the informations relevant to an mpi environment
Provides all information about a quickstep kind.