Line data Source code
1 : !--------------------------------------------------------------------------------------------------!
2 : ! CP2K: A general program to perform molecular dynamics simulations !
3 : ! Copyright 2000-2026 CP2K developers group <https://cp2k.org> !
4 : ! !
5 : ! SPDX-License-Identifier: GPL-2.0-or-later !
6 : !--------------------------------------------------------------------------------------------------!
7 :
8 : ! **************************************************************************************************
9 : !> \brief Interface to Wannier90 code
10 : !> \par History
11 : !> 06.2016 created [JGH]
12 : !> \author JGH
13 : ! **************************************************************************************************
14 : MODULE qs_wannier90
15 : USE atomic_kind_types, ONLY: get_atomic_kind
16 : USE basis_set_types, ONLY: get_gto_basis_set,&
17 : gto_basis_set_type
18 : USE bibliography, ONLY: Gresch2017,&
19 : Soluyanov2011,&
20 : cite_reference
21 : USE cell_types, ONLY: cell_type,&
22 : get_cell
23 : USE cp_blacs_env, ONLY: cp_blacs_env_type
24 : USE cp_cfm_types, ONLY: &
25 : cp_cfm_copy_general, cp_cfm_create, cp_cfm_get_element, cp_cfm_get_info, &
26 : cp_cfm_get_submatrix, cp_cfm_release, cp_cfm_set_submatrix, cp_cfm_to_cfm, cp_cfm_type, &
27 : cp_fm_to_cfm
28 : USE cp_control_types, ONLY: dft_control_type
29 : USE cp_dbcsr_api, ONLY: &
30 : dbcsr_add, dbcsr_create, dbcsr_deallocate_matrix, dbcsr_desymmetrize, dbcsr_get_block_p, &
31 : dbcsr_p_type, dbcsr_scale, dbcsr_set, dbcsr_type, dbcsr_type_antisymmetric, &
32 : dbcsr_type_no_symmetry, dbcsr_type_symmetric
33 : USE cp_dbcsr_cp2k_link, ONLY: cp_dbcsr_alloc_block_from_nbl
34 : USE cp_dbcsr_operations, ONLY: copy_dbcsr_to_fm,&
35 : dbcsr_deallocate_matrix_set
36 : USE cp_files, ONLY: close_file,&
37 : open_file
38 : USE cp_fm_struct, ONLY: cp_fm_struct_create,&
39 : cp_fm_struct_release,&
40 : cp_fm_struct_type
41 : USE cp_fm_types, ONLY: cp_fm_create,&
42 : cp_fm_release,&
43 : cp_fm_type
44 : USE cp_log_handling, ONLY: cp_logger_get_default_io_unit,&
45 : cp_logger_type
46 : USE input_section_types, ONLY: section_vals_get,&
47 : section_vals_get_subs_vals,&
48 : section_vals_type,&
49 : section_vals_val_get
50 : USE kinds, ONLY: default_path_length,&
51 : default_string_length,&
52 : dp
53 : USE kpoint_methods, ONLY: kpoint_env_initialize,&
54 : kpoint_init_cell_index,&
55 : kpoint_initialize,&
56 : kpoint_initialize_mo_set,&
57 : kpoint_initialize_mos,&
58 : rskp_transform
59 : USE kpoint_mo_symmetry_methods, ONLY: kpoint_same_periodic,&
60 : kpoint_transform_scf_mo
61 : USE kpoint_types, ONLY: get_kpoint_info,&
62 : kpoint_create,&
63 : kpoint_env_type,&
64 : kpoint_release,&
65 : kpoint_sym_type,&
66 : kpoint_type
67 : USE machine, ONLY: m_timestamp,&
68 : timestamp_length
69 : USE mathconstants, ONLY: twopi
70 : USE mathlib, ONLY: diag_complex
71 : USE message_passing, ONLY: mp_para_env_type
72 : USE parallel_gemm_api, ONLY: parallel_gemm
73 : USE particle_types, ONLY: particle_type
74 : USE physcon, ONLY: angstrom,&
75 : evolt
76 : USE qs_environment_types, ONLY: get_qs_env,&
77 : qs_env_release,&
78 : qs_environment_type
79 : USE qs_gamma2kp, ONLY: create_kp_from_gamma
80 : USE qs_kind_types, ONLY: get_qs_kind,&
81 : qs_kind_type
82 : USE qs_mo_types, ONLY: get_mo_set,&
83 : mo_set_type
84 : USE qs_moments, ONLY: build_berry_kpoint_matrix
85 : USE qs_neighbor_list_types, ONLY: get_iterator_info,&
86 : get_neighbor_list_set_p,&
87 : neighbor_list_iterate,&
88 : neighbor_list_iterator_create,&
89 : neighbor_list_iterator_p_type,&
90 : neighbor_list_iterator_release,&
91 : neighbor_list_set_p_type
92 : USE qs_scf_diagonalization, ONLY: do_general_diag_kp
93 : USE qs_scf_types, ONLY: qs_scf_env_type
94 : USE scf_control_types, ONLY: scf_control_type
95 : USE soc_pseudopotential_methods, ONLY: V_SOC_xyz_from_pseudopotential
96 : USE topology_inversion, ONLY: gaussian_inversion_action
97 : USE topology_state_io, ONLY: topology_state_begin,&
98 : topology_state_point
99 : USE topology_symmetry, ONLY: inversion_representation
100 : USE topology_tqc, ONLY: inversion_ebr_signature,&
101 : inversion_indicators
102 : USE topology_wilson, ONLY: chern_from_wcc,&
103 : surface_resolved,&
104 : wcc_distance,&
105 : wilson_spectrum,&
106 : wilson_step,&
107 : z2_from_wcc
108 : USE wannier90, ONLY: wannier_setup
109 : USE wannier90_nnkpts, ONLY: read_wannier90_nnkpts
110 : USE wannier90_projections, ONLY: wannier90_hybrid_candidates,&
111 : wannier90_select_projections,&
112 : wannier90_window_mask
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 :
129 : PUBLIC :: wannier90_interface, prepare_wannier90_scf_mos
130 :
131 : ! **************************************************************************************************
132 :
133 : CONTAINS
134 :
135 : ! **************************************************************************************************
136 : !> \brief ...
137 : !> \param input ...
138 : !> \param logger ...
139 : !> \param qs_env ...
140 : ! **************************************************************************************************
141 13015 : 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 13015 : 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 13015 : CALL timeset(routineN, handle)
162 : w_input => section_vals_get_subs_vals(section_vals=input, &
163 13015 : subsection_name="DFT%PRINT%WANNIER90")
164 13015 : CALL section_vals_get(w_input, explicit=explicit)
165 13015 : IF (explicit) THEN
166 :
167 58 : iw = cp_logger_get_default_io_unit(logger)
168 :
169 58 : IF (iw > 0) THEN
170 : WRITE (iw, '(/,T2,A)') &
171 29 : '!-----------------------------------------------------------------------------!'
172 29 : WRITE (iw, '(T32,A)') "Interface to Wannier90"
173 : WRITE (iw, '(T2,A)') &
174 29 : '!-----------------------------------------------------------------------------!'
175 : END IF
176 :
177 58 : CALL get_qs_env(qs_env, dft_control=dft_control)
178 58 : first_spin = 1
179 58 : last_spin = dft_control%nspins
180 58 : IF (last_spin /= 1 .AND. last_spin /= 2) CPABORT('Wannier90 requires collinear spin channels.')
181 58 : CALL section_vals_val_get(w_input, 'SPIN_CHANNEL', i_val=selection)
182 58 : IF (selection /= 0) THEN
183 4 : 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 118 : DO ispin = first_spin, last_spin
188 60 : CALL section_vals_val_get(w_input, "KPOINTS_SOURCE", i_val=source)
189 60 : CALL section_vals_val_get(w_input, "WILSON_MAX_REFINEMENT", i_val=max_refinement)
190 60 : CALL section_vals_val_get(w_input, "WILSON_TOL", r_val=tolerance)
191 60 : CALL section_vals_val_get(w_input, "Z2", l_val=require_z2)
192 60 : CALL section_vals_val_get(w_input, "CHERN", l_val=require_chern)
193 60 : IF (tolerance <= 0.0_dp) CPABORT("WILSON_TOL must be positive.")
194 60 : IF (source == w90_kpoints_wilson) THEN
195 6 : IF (max_refinement < 1 .OR. max_refinement > 10) THEN
196 0 : CPABORT("WILSON_MAX_REFINEMENT must be between 1 and 10.")
197 : END IF
198 6 : converged = .FALSE.
199 6 : old_z2 = -1
200 6 : old_chern = HUGE(0)
201 12 : DO refinement = 0, max_refinement
202 12 : CALL wannier90_files(qs_env, w_input, iw, ispin, refinement, centres, iz2, ichern)
203 12 : IF (refinement > 0) THEN
204 6 : error = 0.0_dp
205 24 : DO i = 1, SIZE(previous, 2)
206 24 : error = MAX(error, wcc_distance(previous(:, i), centres(:, 2*i - 1)))
207 : END DO
208 6 : movement = 0.0_dp
209 30 : DO i = 2, SIZE(centres, 2)
210 30 : movement = MAX(movement, wcc_distance(centres(:, i - 1), centres(:, i)))
211 : END DO
212 6 : IF (iw > 0) WRITE (iw, '(T2,A,I0,A,ES12.4,A,ES12.4)') &
213 3 : "TOPOLOGY| Refinement ", refinement, ": WCC change ", error, ", transverse step ", movement
214 6 : converged = error < tolerance .AND. movement < 0.1_dp .AND. iz2 == old_z2
215 6 : IF (require_z2) converged = converged .AND. iz2 >= 0 .AND. surface_resolved(centres)
216 6 : IF (require_chern) converged = converged .AND. ichern /= HUGE(0) .AND. ichern == old_chern
217 4 : IF (converged) EXIT
218 : END IF
219 6 : CALL MOVE_ALLOC(centres, previous)
220 6 : old_z2 = iz2
221 10 : old_chern = ichern
222 : END DO
223 6 : IF (.NOT. converged) THEN
224 0 : CPABORT("Wilson surface unconverged; increase WILSON_MAX_REFINEMENT or mesh.")
225 : END IF
226 6 : IF (iw > 0) THEN
227 3 : WRITE (iw, '(T2,A)') "TOPOLOGY| Wilson surface sampling converged."
228 3 : IF (iz2 >= 0) WRITE (iw, '(T2,A,I0)') "TOPOLOGY| Converged Z2 invariant: ", iz2
229 3 : IF (require_chern) WRITE (iw, '(T2,A,I0)') "TOPOLOGY| Converged first Chern number: ", ichern
230 : END IF
231 : ELSE
232 54 : CALL wannier90_files(qs_env, w_input, iw, ispin, 0, centres, iz2, ichern)
233 : END IF
234 178 : IF (ALLOCATED(previous)) DEALLOCATE (previous)
235 : END DO
236 :
237 58 : IF (iw > 0) THEN
238 : WRITE (iw, '(/,T2,A)') &
239 29 : '!--------------------------------End of Wannier90-----------------------------!'
240 : END IF
241 : END IF
242 13015 : CALL timestop(handle)
243 :
244 13015 : 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 6468 : 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 6468 : IF (ASSOCIATED(source)) THEN
260 6468 : CALL cp_cfm_copy_general(source, destination, para_env)
261 : ELSE
262 0 : CALL cp_cfm_copy_general(empty_source, destination, para_env)
263 : END IF
264 :
265 6468 : 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 66 : 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 66 : 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 66 : COMPLEX(KIND=dp), ALLOCATABLE :: ao_projections(:, :, :), export_coeff(:, :), &
295 66 : export_scalar(:, :), hybrid_projections(:, :, :), initial_amn(:, :, :), &
296 66 : link_matrix(:, :), parity_metric(:, :), parity_phase(:), reference_mo(:, :, :), &
297 66 : scalar_overlap(:, :), soc_h(:, :), soc_u(:, :), soc_xyz(:, :, :), spinor_coeff(:, :, :), &
298 66 : 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 66 : INTEGER, ALLOCATABLE :: ebr_coefficients(:, :), hybrid_groups(:, :), loop_index(:), &
307 66 : parity_map(:), parity_odd(:), projection_pivots(:)
308 66 : INTEGER, ALLOCATABLE, DIMENSION(:) :: band_indices, exclude_bands
309 66 : INTEGER, ALLOCATABLE, DIMENSION(:, :) :: nblist, nnlist
310 66 : INTEGER, ALLOCATABLE, DIMENSION(:, :, :) :: nncell
311 : INTEGER, DIMENSION(2) :: kp_range
312 : INTEGER, DIMENSION(3) :: input_nkp_grid, mp_grid
313 66 : INTEGER, DIMENSION(:), POINTER :: invals
314 66 : 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 66 : 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 66 : REAL(KIND=dp), ALLOCATABLE :: loop_sv(:), parity_energies(:), &
331 66 : projection_eigenvalues(:, :), &
332 66 : scalar_values(:), spinor_values(:, :)
333 66 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: eigval
334 66 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: atoms_cart, b_latt, kpt_latt, &
335 66 : 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 66 : REAL(KIND=dp), DIMENSION(:), POINTER :: eigenvalues, wkp, wkp_source
339 66 : REAL(KIND=dp), DIMENSION(:, :), POINTER :: xkp, xkp_source
340 66 : REAL(KIND=dp), POINTER :: rvals(:)
341 66 : 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 66 : 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 66 : 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 66 : POINTER :: overlap_nl, sab_nl
360 66 : 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 66 : CALL section_vals_val_get(input, "SEED_NAME", c_val=seed_name)
369 66 : CALL get_qs_env(qs_env, dft_control=dft_control)
370 66 : IF (dft_control%nspins == 2) THEN
371 6 : IF (LEN_TRIM(seed_name) + 5 > LEN(seed_name)) THEN
372 0 : CPABORT('Wannier90 seed is too long for the spin suffix.')
373 : END IF
374 6 : IF (ispin == 1) seed_name = TRIM(seed_name)//'_up'
375 6 : IF (ispin == 2) seed_name = TRIM(seed_name)//'_down'
376 6 : IF (iw > 0) WRITE (iw, '(T2,A,I0,2A)') 'WANNIER90| Spin channel ', ispin, ' file prefix ', TRIM(seed_name)
377 : END IF
378 66 : CALL section_vals_val_get(input, "MP_GRID", i_vals=invals, explicit=mp_grid_explicit)
379 66 : CALL section_vals_val_get(input, "KPOINTS_SOURCE", i_val=kpoints_source)
380 66 : ordered_berry = kpoints_source >= w90_kpoints_nnkp
381 66 : CALL section_vals_val_get(input, "NNKP_FILE", c_val=nnkp_file)
382 66 : spin_channel = ispin
383 66 : CALL section_vals_val_get(input, "WILSON_LOOP", l_val=do_wilson)
384 66 : CALL section_vals_val_get(input, "Z2", l_val=do_z2)
385 66 : CALL section_vals_val_get(input, "CHERN", l_val=do_chern)
386 66 : CALL section_vals_val_get(input, "REQUIRE_GLOBAL_GAP", l_val=require_global_gap)
387 66 : CALL section_vals_val_get(input, "STATE_EXPORT", l_val=export_state)
388 66 : CALL section_vals_val_get(input, "PARITY", l_val=do_parity)
389 66 : CALL section_vals_val_get(input, "INVERSION_TQC", l_val=do_tqc)
390 66 : CALL section_vals_val_get(input, "TQC_DIMENSION", i_val=tqc_dimension)
391 66 : CALL section_vals_val_get(input, "PARITY_ORIGIN", r_vals=rvals)
392 264 : parity_origin = rvals
393 66 : CALL section_vals_val_get(input, "PARITY_TOLERANCE", r_val=parity_tolerance)
394 66 : CALL section_vals_val_get(input, "PARITY_ENERGY_TOL", r_val=parity_energy_tolerance)
395 66 : do_parity = do_parity .OR. do_tqc .OR. kpoints_source == w90_kpoints_trim
396 66 : IF (do_parity .AND. .NOT. ordered_berry) THEN
397 0 : CPABORT("PARITY requires explicit NNKP, WILSON or TRIM points.")
398 : END IF
399 66 : IF (tqc_dimension < 2 .OR. tqc_dimension > 3) CPABORT("TQC_DIMENSION must be 2 or 3.")
400 66 : IF (parity_tolerance <= 0.0_dp .OR. parity_energy_tolerance <= 0.0_dp) THEN
401 0 : CPABORT("Invalid parity tolerance.")
402 : END IF
403 66 : IF (export_state .AND. .NOT. ordered_berry) THEN
404 0 : CPABORT("STATE_EXPORT requires explicit NNKP, WILSON or TRIM points.")
405 : END IF
406 66 : CALL section_vals_val_get(input, "TIME_REVERSAL", l_val=time_reversal)
407 66 : CALL section_vals_val_get(input, "SOC", l_val=do_soc)
408 66 : IF (do_tqc .AND. (.NOT. do_soc .OR. .NOT. time_reversal)) THEN
409 0 : CPABORT("INVERSION_TQC requires SOC T and TIME_REVERSAL T.")
410 : END IF
411 66 : CALL section_vals_val_get(input, "WILSON_GAP_TOL", r_val=gap_tol)
412 66 : IF (gap_tol <= 0.0_dp) CPABORT("WILSON_GAP_TOL must be positive.")
413 66 : z2_value = -1
414 66 : chern_value = HUGE(0)
415 66 : do_wilson = do_wilson .OR. kpoints_source == w90_kpoints_wilson
416 66 : IF (do_wilson .AND. kpoints_source == w90_kpoints_trim) THEN
417 0 : CPABORT("TRIM points are not Wilson loops; use KPOINTS_SOURCE WILSON.")
418 : END IF
419 66 : IF (do_wilson) CALL cite_reference(Gresch2017)
420 66 : IF (do_z2) CALL cite_reference(Soluyanov2011)
421 66 : IF ((do_wilson .OR. do_soc) .AND. kpoints_source < w90_kpoints_nnkp) THEN
422 0 : CPABORT("Native Wilson/SOC requires KPOINTS_SOURCE NNKP or WILSON.")
423 : END IF
424 66 : IF (do_z2 .AND. (kpoints_source /= w90_kpoints_wilson .OR. .NOT. do_soc .OR. .NOT. time_reversal)) THEN
425 0 : CPABORT("Z2 requires KPOINTS_SOURCE WILSON, SOC T, and TIME_REVERSAL T.")
426 : END IF
427 66 : IF (do_chern .AND. (kpoints_source /= w90_kpoints_wilson .OR. do_z2)) THEN
428 0 : CPABORT("CHERN requires a full WILSON surface and cannot be combined with Z2.")
429 : END IF
430 66 : IF (require_global_gap .AND. .NOT. do_wilson) THEN
431 0 : CPABORT("REQUIRE_GLOBAL_GAP requires Wilson analysis.")
432 : END IF
433 66 : CALL get_qs_env(qs_env, dft_control=dft_control)
434 66 : IF (spin_channel < 1 .OR. spin_channel > dft_control%nspins) THEN
435 0 : CPABORT("WANNIER90%SPIN_CHANNEL is not available in this calculation.")
436 : END IF
437 66 : IF (do_soc .AND. dft_control%nspins /= 1) THEN
438 0 : CPABORT("WANNIER90 SOC currently requires a restricted SCF.")
439 : END IF
440 66 : CALL section_vals_val_get(input, "WANNIER_FUNCTIONS", n_rep_val=n_rep)
441 66 : IF (n_rep > 1 .AND. n_rep /= dft_control%nspins) THEN
442 0 : CPABORT('Specify WANNIER_FUNCTIONS once, or once per spin channel.')
443 : END IF
444 66 : i_rep = 1
445 66 : IF (n_rep > 1) i_rep = ispin
446 66 : CALL section_vals_val_get(input, "WANNIER_FUNCTIONS", i_rep_val=i_rep, i_val=num_wann)
447 66 : CALL section_vals_val_get(input, "ADDED_MOS", i_val=nadd)
448 66 : CALL section_vals_val_get(input, "REUSE_SCF_MOS", l_val=reuse_scf_mos)
449 66 : CALL section_vals_val_get(input, "VALIDATE_REUSE_SCF_MOS", l_val=validate_reuse_scf_mos)
450 66 : CALL section_vals_val_get(input, "USE_BLOCH_PHASES", l_val=use_bloch_phases)
451 66 : CALL section_vals_val_get(input, "INITIAL_PROJECTIONS", i_val=initial_projections)
452 66 : CALL section_vals_val_get(input, "EXPORT_PROJECTION_CANDIDATES", i_val=projection_candidates)
453 66 : export_ao = projection_candidates == 1 .OR. projection_candidates == 3
454 66 : export_hybrids = projection_candidates == 2 .OR. projection_candidates == 3
455 66 : IF (initial_projections == -1 .AND. use_bloch_phases) initial_projections = 0
456 66 : energy_window = [-HUGE(0.0_dp)/100, HUGE(0.0_dp)/100, -HUGE(0.0_dp)/100, HUGE(0.0_dp)/100]
457 330 : DO i = 1, 4
458 264 : CALL section_vals_val_get(input, TRIM(window_keywords(i)), explicit=window_set(i))
459 330 : IF (window_set(i)) CALL section_vals_val_get(input, TRIM(window_keywords(i)), r_val=energy_window(i))
460 : END DO
461 322 : IF (ordered_berry .AND. (initial_projections > 0 .OR. projection_candidates > 0 .OR. ANY(window_set))) THEN
462 0 : CPABORT("AO projections and energy windows require KPOINTS_SOURCE MP_GRID or SCF.")
463 : END IF
464 66 : IF (window_set(3) .AND. .NOT. window_set(4)) CPABORT("DIS_FROZ_MIN requires DIS_FROZ_MAX.")
465 66 : IF (energy_window(1) > energy_window(2) .OR. energy_window(3) > energy_window(4)) THEN
466 0 : CPABORT("Invalid Wannier90 energy window.")
467 : END IF
468 66 : reuse_scf_mos = reuse_scf_mos .AND. kpoints_source == w90_kpoints_scf
469 66 : validate_reuse_scf_mos = validate_reuse_scf_mos .AND. reuse_scf_mos
470 264 : mp_grid(1:3) = invals(1:3)
471 : ! excluded bands
472 66 : CALL section_vals_val_get(input, "EXCLUDE_BANDS", n_rep_val=n_rep)
473 66 : nexcl = 0
474 92 : DO i_rep = 1, n_rep
475 26 : CALL section_vals_val_get(input, "EXCLUDE_BANDS", i_rep_val=i_rep, i_vals=invals)
476 92 : nexcl = nexcl + SIZE(invals)
477 : END DO
478 66 : IF (nexcl > 0) THEN
479 72 : ALLOCATE (exclude_bands(nexcl))
480 24 : nexcl = 0
481 50 : DO i_rep = 1, n_rep
482 26 : CALL section_vals_val_get(input, "EXCLUDE_BANDS", i_rep_val=i_rep, i_vals=invals)
483 144 : exclude_bands(nexcl + 1:nexcl + SIZE(invals)) = invals(:)
484 50 : nexcl = nexcl + SIZE(invals)
485 : END DO
486 : END IF
487 : !
488 : ! lattice -> Angstrom
489 66 : CALL get_qs_env(qs_env, cell=cell)
490 66 : CALL get_cell(cell, h=real_lattice, h_inv=h_inv)
491 : ! k-points
492 66 : CALL get_qs_env(qs_env, particle_set=particle_set)
493 66 : CALL get_qs_env(qs_env, para_env=para_env)
494 66 : phase_center = 0.0_dp
495 302 : DO i = 1, SIZE(particle_set)
496 3842 : phase_center(1:3) = phase_center(1:3) + MATMUL(h_inv, particle_set(i)%r)
497 : END DO
498 264 : phase_center(1:3) = phase_center(1:3)/REAL(SIZE(particle_set), KIND=dp)
499 264 : phase_center(1:3) = phase_center(1:3) - FLOOR(phase_center(1:3))
500 66 : recip_lattice(1:3, 1:3) = h_inv(1:3, 1:3)
501 858 : real_lattice(1:3, 1:3) = angstrom*real_lattice(1:3, 1:3)
502 1650 : recip_lattice(1:3, 1:3) = (twopi/angstrom)*TRANSPOSE(recip_lattice(1:3, 1:3))
503 66 : NULLIFY (kpoint, qs_kpoint, xkp, wkp, xkp_source, wkp_source)
504 66 : CALL get_qs_env(qs_env, do_kpoints=do_kpoints, kpoints=qs_kpoint)
505 66 : input_kpoint_symmetry = .FALSE.
506 66 : input_full_grid = .FALSE.
507 66 : input_kp_scheme = ""
508 66 : 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 66 : nkp=nkp, xkp=xkp, wkp=wkp)
513 : END IF
514 66 : CALL kpoint_create(kpoint)
515 :
516 22 : SELECT CASE (kpoints_source)
517 : CASE (w90_kpoints_nnkp, w90_kpoints_wilson, w90_kpoints_trim)
518 22 : IF (kpoints_source == w90_kpoints_nnkp) THEN
519 4 : CALL read_wannier90_nnkpts(nnkp_file, real_lattice, recip_lattice, kpt_latt, nnlist, nncell)
520 18 : ELSE IF (kpoints_source == w90_kpoints_trim) THEN
521 6 : num_kpts = 2**tqc_dimension
522 42 : ALLOCATE (kpt_latt(3, num_kpts), nnlist(num_kpts, 1), nncell(3, num_kpts, 1))
523 6 : kpt_latt = 0.0_dp
524 6 : nncell = 0
525 38 : DO i = 1, num_kpts
526 112 : DO axis = 1, tqc_dimension
527 112 : IF (BTEST(i - 1, axis - 1)) kpt_latt(axis, i) = 0.5_dp
528 : END DO
529 38 : nnlist(i, 1) = i
530 : END DO
531 : ELSE
532 12 : CALL section_vals_val_get(input, "WILSON_MESH", i_vals=invals)
533 36 : base_mesh = invals
534 12 : IF (base_mesh(1) < 2 .OR. base_mesh(2) < 2) THEN
535 0 : CPABORT("WILSON_MESH entries must be at least two.")
536 : END IF
537 12 : npoint = base_mesh(1)*2**refinement
538 12 : nloop = (base_mesh(2) - 1)*2**refinement + 1
539 12 : CALL section_vals_val_get(input, "WILSON_DIRECTION", i_vals=invals)
540 48 : loop_direction = invals
541 12 : IF (ALL(loop_direction == 0)) CPABORT("WILSON_DIRECTION must be nonzero.")
542 12 : CALL section_vals_val_get(input, "WILSON_ORIGIN", r_vals=rvals)
543 48 : loop_origin = rvals
544 12 : CALL section_vals_val_get(input, "WILSON_TRANSVERSE", r_vals=rvals)
545 48 : 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 48 : loop_direction(1)*transverse(2) - loop_direction(2)*transverse(1)]
549 48 : IF (SUM(bvec**2) < 1.e-20_dp) THEN
550 0 : CPABORT("Wilson loop and transverse vectors must be linearly independent.")
551 : END IF
552 48 : IF (do_chern .AND. MAXVAL(ABS(transverse - NINT(transverse))) > 1.e-10_dp) THEN
553 0 : CPABORT("CHERN requires integer WILSON_TRANSVERSE: a closed full surface, not a half-plane.")
554 : END IF
555 12 : IF (do_z2) THEN
556 28 : 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 0 : 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 40 : 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 0 : CPABORT("Native Z2 requires distinct coordinate axes with windings one and one half.")
565 : END IF
566 : END IF
567 12 : num_kpts = npoint*nloop
568 84 : ALLOCATE (kpt_latt(3, num_kpts), nnlist(num_kpts, 1), nncell(3, num_kpts, 1))
569 12 : nncell = 0
570 60 : DO iloop = 1, nloop
571 48 : first_point = (iloop - 1)*npoint + 1
572 360 : DO ipoint = 1, npoint
573 312 : i = first_point + ipoint - 1
574 : kpt_latt(:, i) = loop_origin + REAL(iloop - 1, dp)/REAL(nloop - 1, dp)*transverse + &
575 1248 : REAL(ipoint - 1, dp)/REAL(npoint, dp)*loop_direction
576 360 : nnlist(i, 1) = i + 1
577 : END DO
578 48 : nnlist(i, 1) = first_point
579 204 : nncell(:, i, 1) = loop_direction
580 : END DO
581 : END IF
582 22 : num_kpts = SIZE(kpt_latt, 2)
583 22 : nntot = SIZE(nnlist, 2)
584 22 : kpoint%kp_scheme = "GENERAL"
585 22 : kpoint%symmetry = .FALSE.
586 22 : kpoint%verbose = .FALSE.
587 22 : kpoint%full_grid = .TRUE.
588 22 : kpoint%eps_geo = 1.0e-6_dp
589 22 : kpoint%use_real_wfn = .FALSE.
590 22 : kpoint%parallel_group_size = para_env%num_pe
591 22 : kpoint%nkp = num_kpts
592 110 : ALLOCATE (kpoint%xkp(3, num_kpts), kpoint%wkp(num_kpts))
593 1462 : kpoint%xkp = kpt_latt
594 : ! Export weights do not enter the converged SCF density.
595 382 : kpoint%wkp = 1.0_dp/REAL(num_kpts, KIND=dp)
596 22 : IF (iw > 0) WRITE (iw, '(T2,A,I0,A,I0)') &
597 11 : "WANNIER90| Explicit points: ", num_kpts, ", neighbours per point: ", nntot
598 : CASE (w90_kpoints_mp_grid)
599 0 : num_kpts = mp_grid(1)*mp_grid(2)*mp_grid(3)
600 0 : ALLOCATE (kpt_latt(3, num_kpts))
601 0 : kpoint%kp_scheme = "MONKHORST-PACK"
602 0 : kpoint%symmetry = .FALSE.
603 0 : kpoint%nkp_grid(1:3) = mp_grid(1:3)
604 0 : kpoint%verbose = .FALSE.
605 0 : kpoint%full_grid = .TRUE.
606 0 : kpoint%eps_geo = 1.0e-6_dp
607 0 : kpoint%use_real_wfn = .FALSE.
608 0 : kpoint%parallel_group_size = para_env%num_pe
609 0 : i = 0
610 0 : DO ix = 0, mp_grid(1) - 1
611 0 : DO iy = 0, mp_grid(2) - 1
612 0 : DO iz = 0, mp_grid(3) - 1
613 0 : i = i + 1
614 0 : kpt_latt(1, i) = REAL(ix, KIND=dp)/REAL(mp_grid(1), KIND=dp)
615 0 : kpt_latt(2, i) = REAL(iy, KIND=dp)/REAL(mp_grid(2), KIND=dp)
616 0 : kpt_latt(3, i) = REAL(iz, KIND=dp)/REAL(mp_grid(3), KIND=dp)
617 : END DO
618 : END DO
619 : END DO
620 0 : kpoint%nkp = num_kpts
621 0 : ALLOCATE (kpoint%xkp(3, num_kpts), kpoint%wkp(num_kpts))
622 0 : 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 0 : kpoint%xkp(:, :) = kpt_latt(:, :)
625 :
626 : CASE (w90_kpoints_scf)
627 44 : IF (.NOT. do_kpoints .OR. .NOT. ASSOCIATED(qs_kpoint)) THEN
628 0 : CPABORT("WANNIER90%KPOINTS_SOURCE SCF requires an active DFT%KPOINTS section.")
629 : END IF
630 44 : SELECT CASE (TRIM(input_kp_scheme))
631 : CASE ("GAMMA")
632 0 : mp_grid(:) = 1
633 0 : num_kpts = 1
634 0 : ALLOCATE (kpt_latt(3, num_kpts))
635 0 : kpt_latt(1:3, 1) = 0.0_dp
636 0 : kpoint%kp_scheme = "GAMMA"
637 0 : kpoint%symmetry = .FALSE.
638 0 : kpoint%verbose = .FALSE.
639 0 : kpoint%full_grid = .TRUE.
640 0 : kpoint%eps_geo = 1.0e-6_dp
641 0 : kpoint%use_real_wfn = .FALSE.
642 0 : kpoint%parallel_group_size = para_env%num_pe
643 0 : kpoint%nkp = num_kpts
644 0 : ALLOCATE (kpoint%xkp(3, num_kpts), kpoint%wkp(num_kpts))
645 0 : kpoint%xkp(1:3, 1) = 0.0_dp
646 0 : kpoint%wkp(1) = 1.0_dp
647 :
648 : CASE ("MONKHORST-PACK", "MACDONALD")
649 38 : mp_grid(1:3) = input_nkp_grid(1:3)
650 38 : kpoint%kp_scheme = input_kp_scheme
651 38 : kpoint%symmetry = .FALSE.
652 152 : kpoint%nkp_grid(1:3) = input_nkp_grid(1:3)
653 152 : kpoint%kp_shift(1:3) = input_kp_shift(1:3)
654 38 : kpoint%gamma_centered = input_gamma_centered
655 38 : kpoint%verbose = .FALSE.
656 38 : kpoint%full_grid = .TRUE.
657 38 : kpoint%eps_geo = 1.0e-6_dp
658 38 : kpoint%use_real_wfn = .FALSE.
659 38 : kpoint%parallel_group_size = para_env%num_pe
660 38 : CALL kpoint_initialize(kpoint, particle_set, cell)
661 38 : num_kpts = kpoint%nkp
662 114 : ALLOCATE (kpt_latt(3, num_kpts))
663 2150 : kpt_latt(1:3, 1:num_kpts) = kpoint%xkp(1:3, 1:num_kpts)
664 38 : IF (input_kpoint_symmetry .AND. .NOT. input_full_grid .AND. iw > 0) THEN
665 : WRITE (iw, '(T2,A)') &
666 18 : "WANNIER90| SCF k-points are symmetry-reduced; regenerating the full SCF mesh."
667 18 : IF (reuse_scf_mos) THEN
668 : WRITE (iw, '(T2,A)') &
669 17 : "WANNIER90| CP2K will try to reconstruct the full-mesh MOs from the SCF orbitals."
670 : ELSE
671 : WRITE (iw, '(T2,A)') &
672 1 : "WANNIER90| The full exported mesh is diagonalized for the Wannier90 files."
673 : END IF
674 : END IF
675 :
676 : CASE ("GENERAL")
677 6 : IF (ASSOCIATED(qs_kpoint%xkp_input)) THEN
678 6 : xkp_source => qs_kpoint%xkp_input
679 6 : wkp_source => qs_kpoint%wkp_input
680 : ELSE
681 0 : xkp_source => xkp
682 0 : wkp_source => wkp
683 : END IF
684 6 : IF (.NOT. ASSOCIATED(xkp_source) .OR. .NOT. ASSOCIATED(wkp_source)) THEN
685 0 : CPABORT("Could not access the SCF GENERAL k-point set for the Wannier90 export.")
686 : END IF
687 6 : num_kpts = SIZE(wkp_source)
688 18 : ALLOCATE (kpt_latt(3, num_kpts))
689 198 : kpt_latt(1:3, 1:num_kpts) = xkp_source(1:3, 1:num_kpts)
690 6 : IF (mp_grid_explicit) THEN
691 0 : IF (mp_grid(1)*mp_grid(2)*mp_grid(3) /= num_kpts) THEN
692 0 : CPABORT("WANNIER90%MP_GRID must contain exactly as many points as the SCF GENERAL mesh.")
693 : END IF
694 : ELSE
695 6 : CALL infer_wannier_mp_grid(kpt_latt, mp_grid, mp_grid_valid)
696 6 : IF (.NOT. mp_grid_valid) THEN
697 0 : CPABORT("Could not infer WANNIER90%MP_GRID from the SCF GENERAL mesh.")
698 : END IF
699 : END IF
700 6 : wkp_ref = 1.0_dp/REAL(num_kpts, KIND=dp)
701 54 : DO i = 1, num_kpts
702 54 : IF (ABS(wkp_source(i) - wkp_ref) > 1.0e-10_dp) THEN
703 0 : CPABORT("WANNIER90%KPOINTS_SOURCE SCF requires equally weighted GENERAL k-points.")
704 : END IF
705 : END DO
706 6 : kpoint%kp_scheme = "GENERAL"
707 6 : kpoint%symmetry = .FALSE.
708 24 : kpoint%nkp_grid(1:3) = mp_grid(1:3)
709 6 : kpoint%verbose = .FALSE.
710 6 : kpoint%full_grid = .TRUE.
711 6 : kpoint%eps_geo = 1.0e-6_dp
712 6 : kpoint%use_real_wfn = .FALSE.
713 6 : kpoint%parallel_group_size = para_env%num_pe
714 6 : kpoint%nkp = num_kpts
715 30 : ALLOCATE (kpoint%xkp(3, num_kpts), kpoint%wkp(num_kpts))
716 390 : kpoint%xkp(1:3, 1:num_kpts) = xkp_source(1:3, 1:num_kpts)
717 54 : kpoint%wkp(1:num_kpts) = wkp_ref
718 6 : IF (input_kpoint_symmetry .AND. .NOT. input_full_grid .AND. iw > 0) THEN
719 : WRITE (iw, '(T2,A)') &
720 2 : "WANNIER90| SCF k-points are symmetry-reduced; using the full input GENERAL mesh."
721 2 : IF (reuse_scf_mos) THEN
722 : WRITE (iw, '(T2,A)') &
723 2 : "WANNIER90| CP2K will try to reconstruct the full-mesh MOs from the SCF orbitals."
724 : ELSE
725 : WRITE (iw, '(T2,A)') &
726 0 : "WANNIER90| The full exported mesh is diagonalized for the Wannier90 files."
727 : END IF
728 : END IF
729 :
730 : CASE DEFAULT
731 44 : CPABORT("WANNIER90%KPOINTS_SOURCE SCF does not support this DFT%KPOINTS scheme.")
732 : END SELECT
733 : CASE DEFAULT
734 66 : CPABORT("Unknown WANNIER90%KPOINTS_SOURCE setting.")
735 : END SELECT
736 : ! number of bands in calculation
737 66 : CALL get_qs_env(qs_env, mos=mos)
738 66 : CALL get_mo_set(mo_set=mos(ispin), nao=nao, nmo=num_bands_tot, nelectron=nelectron)
739 66 : num_bands_tot = MIN(nao, num_bands_tot + nadd)
740 66 : nscalar = num_bands_tot
741 66 : IF (do_soc) num_bands_tot = 2*nscalar
742 198 : ALLOCATE (keep_band(num_bands_tot))
743 418 : keep_band = .TRUE.
744 184 : DO i = 1, nexcl
745 118 : ib = exclude_bands(i)
746 118 : IF (ib < 1 .OR. ib > num_bands_tot) CPABORT("WANNIER90%EXCLUDE_BANDS: index out of range.")
747 118 : IF (ordered_berry .AND. .NOT. keep_band(ib)) THEN
748 0 : CPABORT("WANNIER90%EXCLUDE_BANDS: duplicate band index.")
749 : END IF
750 184 : keep_band(ib) = .FALSE.
751 : END DO
752 418 : num_bands = COUNT(keep_band)
753 66 : IF (num_bands < 1 .OR. num_wann > num_bands) THEN
754 0 : CPABORT("WANNIER90 EXCLUDE_BANDS leaves too few bands for WANNIER_FUNCTIONS.")
755 : END IF
756 198 : ALLOCATE (band_indices(num_bands))
757 770 : band_indices(:) = PACK([(i, i=1, num_bands_tot)], keep_band)
758 530 : lowest_bands = num_bands < num_bands_tot .AND. ALL(band_indices == [(i, i=1, num_bands)])
759 66 : IF (require_global_gap .AND. .NOT. lowest_bands) THEN
760 0 : CPABORT("REQUIRE_GLOBAL_GAP needs a lowest-band prefix and an excluded band above it.")
761 : END IF
762 66 : IF (do_z2) THEN
763 4 : IF (num_bands /= nelectron) THEN
764 0 : CPABORT("Z2 requires one occupied spinor per electron; adjust EXCLUDE_BANDS.")
765 : END IF
766 4 : IF (num_bands == num_bands_tot .OR. MOD(num_bands, 2) /= 0) THEN
767 0 : CPABORT("Z2 needs an even occupied spinor subspace and at least one excluded conduction band.")
768 : END IF
769 68 : IF (ANY(band_indices /= [(i, i=1, num_bands)])) THEN
770 0 : CPABORT("Z2 requires the lowest occupied spinor bands.")
771 : END IF
772 : END IF
773 66 : IF (do_wilson) THEN
774 16 : IF (nntot /= 1) CPABORT("WILSON_LOOP requires exactly one directed neighbour per point.")
775 48 : ALLOCATE (loop_index(num_kpts))
776 68 : i = 1
777 68 : nloop = 0
778 68 : DO WHILE (i <= num_kpts)
779 52 : first_point = i
780 52 : nloop = nloop + 1
781 : DO
782 328 : loop_index(i) = nloop
783 328 : IF (nnlist(i, 1) == first_point) EXIT
784 276 : IF (nnlist(i, 1) /= i + 1 .OR. i == num_kpts) THEN
785 0 : CPABORT("WILSON_LOOP requires contiguous ordered closed loops.")
786 : END IF
787 52 : i = i + 1
788 : END DO
789 52 : i = i + 1
790 : END DO
791 112 : ALLOCATE (wilson_product(num_bands, num_bands, nloop), loop_sv(nloop))
792 16 : wilson_product = CMPLX(0.0_dp, 0.0_dp, dp)
793 60 : DO i = 1, num_bands
794 224 : wilson_product(i, i, :) = CMPLX(1.0_dp, 0.0_dp, dp)
795 : END DO
796 68 : loop_sv = 1.0_dp
797 64 : ALLOCATE (wcc_out(num_bands, nloop))
798 : ELSE
799 50 : ALLOCATE (wcc_out(0, 0))
800 : END IF
801 264 : ALLOCATE (link_matrix(num_bands, num_bands))
802 66 : IF (initial_projections == 0 .AND. num_wann /= num_bands .AND. .NOT. ordered_berry) THEN
803 0 : CPABORT("IDENTITY projections require WANNIER_FUNCTIONS to match the number of bands.")
804 : END IF
805 66 : IF (initial_projections >= 0 .AND. .NOT. ordered_berry .AND. num_wann < 1) THEN
806 0 : CPABORT("Initial projections require a positive WANNIER_FUNCTIONS count.")
807 : END IF
808 322 : IF (ANY(window_set) .AND. num_wann == num_bands) THEN
809 0 : CPABORT("Energy windows require more bands than Wannier functions.")
810 : END IF
811 322 : IF (ANY(window_set) .AND. num_wann < 1) THEN
812 0 : CPABORT("Energy windows require a positive WANNIER_FUNCTIONS count.")
813 : END IF
814 198 : ALLOCATE (allowed_band(num_bands))
815 328 : IF (ANY(window_set)) ALLOCATE (projection_eigenvalues(num_bands, num_kpts))
816 66 : num_atoms = SIZE(particle_set)
817 198 : ALLOCATE (atoms_cart(3, num_atoms))
818 198 : ALLOCATE (atom_symbols(num_atoms))
819 302 : DO i = 1, num_atoms
820 944 : atoms_cart(1:3, i) = particle_set(i)%r(1:3)
821 236 : CALL get_atomic_kind(particle_set(i)%atomic_kind, element_symbol=asym)
822 302 : atom_symbols(i) = asym
823 : END DO
824 66 : IF (kpoints_source < w90_kpoints_nnkp) THEN
825 264 : ALLOCATE (nnlist(num_kpts, num_nnmax), nncell(3, num_kpts, num_nnmax))
826 44 : nnlist(:, :) = 0
827 44 : nncell(:, :, :) = 0
828 44 : nntot = 0
829 44 : IF (iw > 0) THEN
830 : CALL wannier_setup(mp_grid, num_kpts, real_lattice, recip_lattice, &
831 22 : kpt_latt, nntot, nnlist, nncell, iw)
832 : END IF
833 44 : CALL para_env%sum(nntot)
834 44 : CALL para_env%sum(nnlist)
835 44 : CALL para_env%sum(nncell)
836 : END IF
837 :
838 66 : IF (para_env%is_source() .AND. kpoints_source < w90_kpoints_nnkp) THEN
839 : ! Write the ordinary file-export input "seed_name.win".
840 22 : WRITE (filename, '(A,A)') TRIM(seed_name), ".win"
841 22 : CALL open_file(filename, unit_number=iunit, file_status="REPLACE", file_action="WRITE")
842 : !
843 22 : CALL m_timestamp(timestamp)
844 22 : WRITE (iunit, "(A)") "! Wannier90 input file generated by CP2K "
845 22 : WRITE (iunit, "(A,/)") "! Creation date "//timestamp
846 : !
847 22 : WRITE (iunit, "(A,I5)") "num_wann = ", num_wann
848 22 : IF (num_bands /= num_wann .OR. use_bloch_phases) THEN
849 20 : WRITE (iunit, "(A,I5)") "num_bands = ", num_bands
850 : END IF
851 110 : DO i = 1, 4
852 110 : IF (window_set(i)) WRITE (iunit, '(2A,ES24.16)') TRIM(window_keywords(i)), ' = ', energy_window(i)*evolt
853 : END DO
854 22 : WRITE (iunit, "(/,A,/)") "length_unit = bohr"
855 22 : WRITE (iunit, "(/,A,/)") "! System"
856 22 : WRITE (iunit, "(/,A)") "begin unit_cell_cart"
857 22 : WRITE (iunit, "(A)") "bohr"
858 88 : DO i = 1, 3
859 286 : WRITE (iunit, "(3ES24.16)") cell%hmat(1:3, i)
860 : END DO
861 22 : WRITE (iunit, "(A,/)") "end unit_cell_cart"
862 22 : WRITE (iunit, "(/,A)") "begin atoms_cart"
863 22 : WRITE (iunit, "(A)") "bohr"
864 128 : DO i = 1, num_atoms
865 128 : WRITE (iunit, "(A,3F15.10)") atom_symbols(i), atoms_cart(1:3, i)
866 : END DO
867 22 : WRITE (iunit, "(A,/)") "end atoms_cart"
868 22 : WRITE (iunit, "(/,A,/)") "! Kpoints"
869 22 : WRITE (iunit, "(/,A,3I6/)") "mp_grid = ", mp_grid(1:3)
870 22 : WRITE (iunit, "(A)") "begin kpoints"
871 310 : DO i = 1, num_kpts
872 310 : WRITE (iunit, "(3ES24.16)") kpt_latt(1:3, i)
873 : END DO
874 22 : WRITE (iunit, "(A)") "end kpoints"
875 22 : CALL close_file(iunit)
876 : ELSE
877 44 : iunit = -1
878 : END IF
879 :
880 : ! calculate bands
881 66 : NULLIFY (qs_env_kp)
882 66 : IF (kpoints_source == w90_kpoints_mp_grid .AND. input_kpoint_symmetry .AND. iw > 0) THEN
883 : WRITE (iw, '(T2,A)') &
884 0 : "WANNIER90| Atomic k-point symmetry from the SCF calculation is not reused."
885 : WRITE (iw, '(T2,A)') &
886 0 : "WANNIER90| A full Monkhorst-Pack grid is generated for the Wannier90 interface."
887 : END IF
888 66 : IF (do_kpoints) THEN
889 : ! we already do kpoints
890 66 : qs_env_kp => qs_env
891 : ELSE
892 : ! we start from gamma point only
893 0 : CALL create_kp_from_gamma(qs_env, qs_env_kp)
894 : END IF
895 66 : IF (iw > 0) THEN
896 33 : 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 66 : CALL get_qs_env(qs_env=qs_env_kp, para_env=para_env, blacs_env=blacs_env, mos=mos)
901 66 : CALL kpoint_env_initialize(kpoint, para_env, blacs_env)
902 66 : CALL kpoint_initialize_mos(kpoint, mos, nadd)
903 66 : CALL kpoint_initialize_mo_set(kpoint)
904 : !
905 66 : CALL get_qs_env(qs_env=qs_env_kp, sab_orb=sab_nl, dft_control=dft_control)
906 66 : 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 66 : scf_env=scf_env, scf_control=scf_control)
910 66 : full_mesh_diagonalized = .FALSE.
911 66 : reused_scf_mos = .FALSE.
912 66 : reuse_reason = ""
913 66 : aligned_degenerate_blocks = 0
914 66 : aligned_degenerate_max_size = 0
915 66 : aligned_degenerate_min_svalue = 0.0_dp
916 66 : IF (reuse_scf_mos) THEN
917 42 : 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 42 : diis_step)
920 42 : IF (validate_reuse_scf_mos) THEN
921 14 : IF (iw > 0) THEN
922 : WRITE (iw, '(T2,A)') &
923 7 : "WANNIER90| Validating SCF MO reuse against a full-mesh diagonalization reference."
924 : END IF
925 14 : CALL do_general_diag_kp(matrix_ks, matrix_s, kpoint, scf_env, scf_control, .FALSE., diis_step)
926 14 : full_mesh_diagonalized = .TRUE.
927 14 : 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 14 : reuse_candidate_residual)
934 14 : 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 7 : "WANNIER90| Best atom/AO candidate subspace deviation ", &
937 7 : reuse_candidate_deviation, ", minimum singular value ", &
938 7 : reuse_candidate_min_svalue, ", max metric deviation ", &
939 14 : 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 42 : aligned_degenerate_min_svalue, spin_channel=ispin)
946 42 : IF (validate_reuse_scf_mos) THEN
947 14 : 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 14 : validation_eigenvalue_deviation)
953 14 : IF (iw > 0) THEN
954 : WRITE (iw, '(T2,A,ES10.3,A,ES10.3,A,ES10.3)') &
955 7 : "WANNIER90| Reused MO validation: subspace deviation ", &
956 7 : validation_subspace_deviation, ", minimum singular value ", &
957 7 : validation_min_svalue, ", eigenvalue deviation ", &
958 14 : validation_eigenvalue_deviation
959 : END IF
960 14 : IF (.NOT. validate_reuse_ok) THEN
961 2 : reused_scf_mos = .FALSE.
962 : WRITE (reuse_reason, "(A,ES10.3,A,ES10.3)") &
963 2 : "validation failed: dS=", &
964 4 : validation_subspace_deviation, ", dE=", validation_eigenvalue_deviation
965 : END IF
966 : END IF
967 14 : IF (.NOT. reused_scf_mos) THEN
968 : CALL restore_wannier90_mo_snapshot(kpoint, ispin, reference_mo, &
969 2 : reference_eigenvalues)
970 : END IF
971 : END IF
972 42 : IF (iw > 0) THEN
973 21 : IF (reused_scf_mos) THEN
974 : WRITE (iw, '(T2,A)') &
975 17 : "WANNIER90| Reused SCF MO coefficients for the Wannier90 full k-point mesh."
976 17 : IF (use_bloch_phases) THEN
977 10 : IF (initial_projections == 0) WRITE (iw, '(T2,A)') &
978 6 : "WANNIER90| Wrote identity projections for Bloch-phase complete band subspaces."
979 : WRITE (iw, '(T2,A,3F10.6)') &
980 10 : "WANNIER90| Applied Bloch phase gauge to reused overlaps around fractional center", &
981 20 : phase_center(1:3)
982 : END IF
983 17 : IF (aligned_degenerate_blocks > 0) THEN
984 : WRITE (iw, '(T2,A,I0,A,I0,A,ES10.3)') &
985 10 : "WANNIER90| Ritz-stabilized ", aligned_degenerate_blocks, &
986 10 : " degenerate SCF MO subspace(s) with S(k),H(k); largest block has ", &
987 10 : aligned_degenerate_max_size, " band(s), min metric eigenvalue ", &
988 20 : aligned_degenerate_min_svalue
989 : END IF
990 : ELSE
991 : WRITE (iw, '(T2,A,A)') &
992 4 : "WANNIER90| Could not reuse SCF MOs: ", TRIM(reuse_reason)
993 : WRITE (iw, '(T2,A)') &
994 4 : "WANNIER90| Falling back to full-mesh diagonalization for the Wannier90 files."
995 : END IF
996 : END IF
997 : END IF
998 66 : IF (.NOT. reused_scf_mos .AND. .NOT. full_mesh_diagonalized) THEN
999 30 : CALL do_general_diag_kp(matrix_ks, matrix_s, kpoint, scf_env, scf_control, .FALSE., diis_step)
1000 : END IF
1001 66 : IF (ALLOCATED(reference_mo)) DEALLOCATE (reference_mo)
1002 66 : IF (ALLOCATED(reference_eigenvalues)) DEALLOCATE (reference_eigenvalues)
1003 : !
1004 66 : IF (iw > 0) THEN
1005 33 : 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 264 : ALLOCATE (nblist(num_kpts, nntot))
1013 198 : ALLOCATE (b_latt(3, num_kpts*nntot))
1014 66 : nblist(:, :) = 0
1015 66 : nbs = 0
1016 1002 : DO ik = 1, num_kpts
1017 4914 : DO i = 1, nntot
1018 15648 : bvec(1:3) = kpt_latt(1:3, nnlist(ik, i)) - kpt_latt(1:3, ik) + nncell(1:3, ik, i)
1019 7656 : ibs = 0
1020 7656 : DO k = 1, nbs
1021 29984 : 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 22240 : IF (SUM(ABS(bvec(1:3) + b_latt(1:3, k))) < 1.e-6_dp) THEN
1026 1776 : ibs = -k
1027 1776 : EXIT
1028 : END IF
1029 : END DO
1030 4848 : IF (ibs /= 0) THEN
1031 : ! old lattice vector
1032 3752 : nblist(ik, i) = ibs
1033 : ELSE
1034 : ! new lattice vector
1035 160 : nbs = nbs + 1
1036 640 : b_latt(1:3, nbs) = bvec(1:3)
1037 160 : nblist(ik, i) = nbs
1038 : END IF
1039 : END DO
1040 : END DO
1041 : ! calculate all the operator matrices (a|bvec|b)
1042 66 : overlap_nl => sab_nl
1043 66 : NULLIFY (berry_kpoint)
1044 66 : IF (ordered_berry) THEN
1045 22 : CALL get_qs_env(qs_env_kp, sab_all=overlap_nl)
1046 22 : CALL kpoint_create(berry_kpoint)
1047 22 : CALL kpoint_init_cell_index(berry_kpoint, overlap_nl, para_env, nberry_images)
1048 22 : CALL get_kpoint_info(berry_kpoint, cell_to_index=berry_cell_index)
1049 : END IF
1050 66 : IF (.NOT. ASSOCIATED(overlap_nl)) CPABORT("Explicit overlaps require k-point neighbour lists.")
1051 358 : ALLOCATE (berry_matrix(nbs))
1052 226 : DO i = 1, nbs
1053 160 : NULLIFY (berry_matrix(i)%cosmat)
1054 160 : NULLIFY (berry_matrix(i)%sinmat)
1055 800 : 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 226 : berry_matrix(i)%sinmat, bvec, ordered=ordered_berry, ordered_kpoints=berry_kpoint)
1058 : END DO
1059 : ! work matrices for MOs (all group)
1060 66 : kp => kpoint%kp_env(1)%kpoint_env
1061 66 : CALL get_mo_set(kp%mos(ispin), nmo=nmo)
1062 66 : IF (ordered_berry .AND. nmo /= nscalar) THEN
1063 0 : CPABORT("WANNIER90: unexpected orbital count in export.")
1064 : END IF
1065 66 : 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 66 : context=blacs_env)
1070 66 : CALL cp_cfm_create(fmk1_cfm, matrix_struct_work)
1071 66 : CALL cp_cfm_create(fmk2_cfm, matrix_struct_work)
1072 66 : 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 66 : context=blacs_env)
1077 66 : CALL cp_fm_create(mat_real, matrix_struct_ao)
1078 66 : CALL cp_fm_create(mat_imag, matrix_struct_ao)
1079 66 : CALL cp_cfm_create(omat_cfm, matrix_struct_ao)
1080 : ! work matrices for Mmn(k,b) integrals
1081 66 : 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 66 : context=blacs_env)
1086 66 : CALL cp_cfm_create(mmn_cfm, matrix_struct_mmn)
1087 : ! allocate some work matrices
1088 66 : ALLOCATE (rmatrix_full, cmatrix_full)
1089 : CALL dbcsr_create(rmatrix_full, template=matrix_s(1, 1)%matrix, &
1090 66 : matrix_type=dbcsr_type_no_symmetry)
1091 : CALL dbcsr_create(cmatrix_full, template=matrix_s(1, 1)%matrix, &
1092 66 : matrix_type=dbcsr_type_no_symmetry)
1093 66 : IF (ordered_berry) THEN
1094 22 : ALLOCATE (loop_real, loop_imag)
1095 22 : CALL dbcsr_create(loop_real, template=matrix_s(1, 1)%matrix, matrix_type=dbcsr_type_no_symmetry)
1096 22 : CALL dbcsr_create(loop_imag, template=matrix_s(1, 1)%matrix, matrix_type=dbcsr_type_no_symmetry)
1097 22 : CALL cp_dbcsr_alloc_block_from_nbl(loop_real, overlap_nl)
1098 22 : CALL cp_dbcsr_alloc_block_from_nbl(loop_imag, overlap_nl)
1099 22 : CALL cp_dbcsr_alloc_block_from_nbl(rmatrix_full, overlap_nl)
1100 22 : CALL cp_dbcsr_alloc_block_from_nbl(cmatrix_full, overlap_nl)
1101 : ELSE
1102 44 : CALL cp_dbcsr_alloc_block_from_nbl(rmatrix_full, sab_nl, desymmetrize=.TRUE.)
1103 44 : CALL cp_dbcsr_alloc_block_from_nbl(cmatrix_full, sab_nl, desymmetrize=.TRUE.)
1104 : END IF
1105 : !
1106 66 : CALL get_kpoint_info(kpoint=kpoint, cell_to_index=cell_to_index)
1107 66 : NULLIFY (cmo_coeff_source)
1108 66 : IF (do_soc .OR. do_parity) THEN
1109 : ! Parity needs the overlap work matrices even without SOC, and after SOC is done.
1110 12 : ALLOCATE (rmatrix, cmatrix)
1111 12 : CALL dbcsr_create(rmatrix, template=matrix_s(1, 1)%matrix, matrix_type=dbcsr_type_symmetric)
1112 12 : CALL dbcsr_create(cmatrix, template=matrix_s(1, 1)%matrix, matrix_type=dbcsr_type_antisymmetric)
1113 12 : CALL cp_dbcsr_alloc_block_from_nbl(rmatrix, sab_nl)
1114 12 : CALL cp_dbcsr_alloc_block_from_nbl(cmatrix, sab_nl)
1115 : END IF
1116 66 : IF (do_soc) THEN
1117 : ! Second variation in the scalar KS eigenbasis. Retain selected spinors at each k.
1118 30 : CPASSERT(ALL(kpoint%kp_range == [1, num_kpts]))
1119 10 : NULLIFY (soc_matrices)
1120 10 : CALL V_SOC_xyz_from_pseudopotential(qs_env_kp, soc_matrices)
1121 100 : ALLOCATE (soc_xyz(nmo, nmo, 3), soc_h(2*nmo, 2*nmo), soc_u(2*nmo, 2*nmo))
1122 60 : ALLOCATE (scalar_values(2*nmo), spinor_values(2*nmo, num_kpts))
1123 80 : ALLOCATE (spinor_coeff(2*nmo, num_bands, num_kpts), scalar_overlap(nmo, nmo))
1124 146 : DO ik = 1, num_kpts
1125 136 : kp => kpoint%kp_env(ik)%kpoint_env
1126 136 : CALL cp_cfm_copy_general(kp%mos(1)%cmo_coeff, fmk1_cfm, para_env)
1127 136 : CALL get_mo_set(kp%mos(1), eigenvalues=eigenvalues)
1128 984 : scalar_values(1:nmo) = eigenvalues(1:nmo)
1129 984 : scalar_values(nmo + 1:) = eigenvalues(1:nmo)
1130 544 : DO axis = 1, 3
1131 408 : CALL dbcsr_set(rmatrix, 0.0_dp)
1132 408 : CALL dbcsr_set(cmatrix, 0.0_dp)
1133 : CALL rskp_transform(cmatrix, rmatrix, rsmat=soc_matrices, ispin=axis, &
1134 408 : xkp=kpoint%xkp(:, ik), cell_to_index=cell_to_index, sab_nl=sab_nl)
1135 408 : CALL dbcsr_scale(rmatrix, -1.0_dp)
1136 408 : CALL dbcsr_desymmetrize(rmatrix, rmatrix_full)
1137 408 : CALL dbcsr_desymmetrize(cmatrix, cmatrix_full)
1138 408 : CALL copy_dbcsr_to_fm(rmatrix_full, mat_real)
1139 408 : CALL copy_dbcsr_to_fm(cmatrix_full, mat_imag)
1140 408 : 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 408 : 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 408 : fmk1_cfm, tmp_cfm, CMPLX(0.0_dp, 0.0_dp, dp), mmn_cfm)
1145 544 : CALL cp_cfm_get_submatrix(mmn_cfm, soc_xyz(:, :, axis))
1146 : END DO
1147 9592 : soc_h(1:nmo, 1:nmo) = soc_xyz(:, :, 3)
1148 9592 : soc_h(nmo + 1:, nmo + 1:) = -soc_xyz(:, :, 3)
1149 : ! Match H_KS_spinor_kp and CP2K's real-spherical angular-momentum convention.
1150 9592 : soc_h(1:nmo, nmo + 1:) = soc_xyz(:, :, 1) + CMPLX(0.0_dp, 1.0_dp, dp)*soc_xyz(:, :, 2)
1151 9592 : soc_h(nmo + 1:, 1:nmo) = soc_xyz(:, :, 1) - CMPLX(0.0_dp, 1.0_dp, dp)*soc_xyz(:, :, 2)
1152 1832 : DO ib = 1, 2*nmo
1153 1832 : soc_h(ib, ib) = soc_h(ib, ib) + scalar_values(ib)
1154 : END DO
1155 36264 : IF (MAXVAL(ABS(soc_h - CONJG(TRANSPOSE(soc_h)))) > 1.e-8_dp) THEN
1156 0 : IF (iw > 0) WRITE (iw, '(T2,A,I0,A,ES18.10)') &
1157 0 : "TOPOLOGY| SOC Hermiticity error at point ", ik, ": ", &
1158 0 : MAXVAL(ABS(soc_h - CONJG(TRANSPOSE(soc_h))))
1159 0 : CPABORT("WANNIER90 SOC: non-Hermitian second-variational Hamiltonian.")
1160 : END IF
1161 136 : CALL diag_complex(soc_h, soc_u, spinor_values(:, ik))
1162 14802 : spinor_coeff(:, :, ik) = soc_u(:, band_indices)
1163 : END DO
1164 10 : CALL dbcsr_deallocate_matrix_set(soc_matrices)
1165 10 : DEALLOCATE (soc_xyz, soc_h, soc_u, scalar_values)
1166 10 : 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 66 : kp => kpoint%kp_env(1)%kpoint_env
1170 66 : CALL get_mo_set(kp%mos(ispin), nmo=nmo)
1171 66 : CPASSERT(nmo >= nscalar)
1172 254 : ALLOCATE (eigval(MERGE(2*nmo, nmo, do_soc)))
1173 66 : direct_gap = HUGE(1.0_dp)
1174 66 : valence_max = -HUGE(1.0_dp)
1175 66 : conduction_min = HUGE(1.0_dp)
1176 66 : CALL get_kpoint_info(kpoint, nkp=nkp, kp_range=kp_range, xkp=xkp)
1177 66 : IF (para_env%is_source()) THEN
1178 33 : WRITE (filename, '(A,A)') TRIM(seed_name), ".eig"
1179 33 : CALL open_file(filename, unit_number=iunit, file_status="REPLACE", file_action="WRITE")
1180 : ELSE
1181 33 : iunit = -1
1182 : END IF
1183 : !
1184 1002 : DO ik = 1, nkp
1185 936 : my_kpgrp = (ik >= kp_range(1) .AND. ik <= kp_range(2))
1186 936 : IF (do_soc) THEN
1187 1832 : eigval(:) = spinor_values(:, ik)
1188 800 : ELSE IF (my_kpgrp) THEN
1189 800 : ikpgr = ik - kp_range(1) + 1
1190 800 : kp => kpoint%kp_env(ikpgr)%kpoint_env
1191 800 : CALL get_mo_set(kp%mos(ispin), eigenvalues=eigenvalues)
1192 3296 : eigval(1:nmo) = eigenvalues(1:nmo)
1193 : ELSE
1194 0 : eigval(1:nmo) = 0.0_dp
1195 : END IF
1196 936 : IF (.NOT. do_soc) CALL kpoint%para_env_inter_kp%sum(eigval)
1197 936 : IF (.NOT. ordered_berry .AND. nexcl > 0) THEN
1198 80 : DO ib = 1, num_bands_tot - 1
1199 64 : IF (keep_band(ib) .EQV. keep_band(ib + 1)) CYCLE
1200 : ! Degenerate states outside the outer window cannot enter the Wannier subspace.
1201 32 : kept_index = MERGE(ib, ib + 1, keep_band(ib))
1202 32 : IF (eigval(kept_index) < energy_window(1) .OR. eigval(kept_index) > energy_window(2)) CYCLE
1203 16 : IF (ABS(eigval(ib + 1) - eigval(ib)) < 1.0e-8_dp) THEN
1204 0 : CPABORT("WANNIER90 EXCLUDE_BANDS must retain or remove complete degenerate subspaces.")
1205 : END IF
1206 : END DO
1207 : END IF
1208 936 : IF (do_wilson) THEN
1209 328 : IF (lowest_bands) THEN
1210 328 : valence_max = MAX(valence_max, eigval(num_bands))
1211 328 : conduction_min = MIN(conduction_min, eigval(num_bands + 1))
1212 : END IF
1213 1488 : DO ib = 1, num_bands_tot - 1
1214 10032 : IF (ANY(band_indices == ib) .NEQV. ANY(band_indices == ib + 1)) THEN
1215 328 : pair_gap = eigval(ib + 1) - eigval(ib)
1216 328 : direct_gap = MIN(direct_gap, pair_gap)
1217 : END IF
1218 : END DO
1219 : END IF
1220 5128 : eigval(:) = eigval*evolt
1221 4616 : IF (ANY(window_set)) THEN
1222 80 : projection_eigenvalues(:, ik) = eigval(band_indices)
1223 : CALL wannier90_window_mask(projection_eigenvalues(:, ik), num_wann, energy_window*evolt, &
1224 80 : window_set, allowed_band)
1225 : END IF
1226 : ! output
1227 1002 : IF (iunit > 0) THEN
1228 2140 : DO ib = 1, num_bands
1229 2140 : WRITE (iunit, "(2I8,F24.14)") ib, ik, eigval(band_indices(ib))
1230 : END DO
1231 : END IF
1232 : END DO
1233 66 : IF (iunit > 0) THEN
1234 33 : CALL close_file(iunit)
1235 : END IF
1236 66 : 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 12 : reused_scf_mos .AND. use_bloch_phases, phase_center, ao_projections)
1239 10 : IF (export_ao) THEN
1240 2 : CALL write_wannier90_amn(ao_projections, para_env, TRIM(seed_name)//"_ao_candidates.amn")
1241 : END IF
1242 10 : IF (initial_projections == 2 .OR. export_hybrids) THEN
1243 6 : CALL build_wannier90_hybrid_groups(qs_env, hybrid_groups)
1244 6 : CALL wannier90_hybrid_candidates(ao_projections, hybrid_groups, hybrid_projections)
1245 6 : DEALLOCATE (hybrid_groups)
1246 6 : IF (export_hybrids) THEN
1247 2 : CALL write_wannier90_amn(hybrid_projections, para_env, TRIM(seed_name)//"_hybrid_candidates.amn")
1248 2 : IF (iw > 0) WRITE (iw, '(T2,A,I0)') &
1249 1 : "WANNIER90| Exported hybrid projection candidates ", SIZE(hybrid_projections, 2)
1250 : END IF
1251 6 : IF (initial_projections == 2) THEN
1252 4 : CALL MOVE_ALLOC(hybrid_projections, ao_projections)
1253 4 : IF (iw > 0) WRITE (iw, '(T2,A)') "WANNIER90| Selecting from the full radial s/p hybrid candidate bank."
1254 : ELSE
1255 2 : DEALLOCATE (hybrid_projections)
1256 : END IF
1257 : END IF
1258 : END IF
1259 66 : IF (.NOT. ordered_berry .AND. initial_projections >= 0) THEN
1260 120 : ALLOCATE (initial_amn(num_bands, num_wann, num_kpts))
1261 24 : IF (initial_projections == 0) THEN
1262 14 : initial_amn(:, :, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
1263 62 : DO i = 1, num_wann
1264 1342 : initial_amn(i, i, :) = CMPLX(1.0_dp, 0.0_dp, KIND=dp)
1265 : END DO
1266 : ELSE
1267 42 : IF (para_env%is_source() .AND. ANY(window_set)) THEN
1268 9 : DO ik = 1, num_kpts
1269 : CALL wannier90_window_mask(projection_eigenvalues(:, ik), num_wann, energy_window*evolt, &
1270 40 : window_set, allowed_band)
1271 41 : DO ib = 1, num_bands
1272 160 : 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 30 : ALLOCATE (projection_pivots(num_wann))
1277 : CALL wannier90_select_projections(ao_projections, para_env, initial_amn, projection_pivots, &
1278 10 : projection_min_svalue)
1279 10 : IF (iw > 0) THEN
1280 5 : WRITE (iw, '(T2,A,ES12.4)') "WANNIER90| Minimum projection singular value ", projection_min_svalue
1281 5 : WRITE (iw, '(T2,A,*(I6))') "WANNIER90| Selected projection candidates ", projection_pivots
1282 : END IF
1283 10 : IF (projection_min_svalue < 1.0e-10_dp) THEN
1284 0 : CPABORT("No full-rank Wannier90 AO trial set was found on the entire k-point mesh.")
1285 : END IF
1286 20 : DEALLOCATE (projection_pivots)
1287 : END IF
1288 24 : CALL write_wannier90_amn(initial_amn, para_env, TRIM(seed_name)//".amn")
1289 24 : DEALLOCATE (initial_amn)
1290 : END IF
1291 66 : IF (ALLOCATED(ao_projections)) DEALLOCATE (ao_projections)
1292 66 : IF (para_env%is_source()) THEN
1293 33 : WRITE (filename, '(A,A)') TRIM(seed_name), ".mmn"
1294 33 : CALL open_file(filename, unit_number=iunit, file_status="REPLACE", file_action="WRITE")
1295 33 : CALL m_timestamp(timestamp)
1296 33 : WRITE (iunit, "(A)") "! Wannier90 file generated by CP2K "//timestamp
1297 33 : WRITE (iunit, "(3I8)") num_bands, num_kpts, nntot
1298 : ELSE
1299 33 : iunit = -1
1300 : END IF
1301 : ! loop over all k-points
1302 1002 : DO ik = 1, num_kpts
1303 : ! get the MO coefficients for this k-point
1304 936 : my_kpgrp = (ik >= kpoint%kp_range(1) .AND. ik <= kpoint%kp_range(2))
1305 : IF (my_kpgrp) THEN
1306 936 : ikk = ik - kpoint%kp_range(1) + 1
1307 936 : kp => kpoint%kp_env(ikk)%kpoint_env
1308 936 : cmo_coeff_source => kp%mos(ispin)%cmo_coeff
1309 : ELSE
1310 0 : NULLIFY (cmo_coeff_source, kp)
1311 : END IF
1312 936 : CALL wannier90_copy_cfm_general(cmo_coeff_source, fmk1_cfm, para_env)
1313 : ! loop over all connected neighbors
1314 4914 : DO i = 1, nntot
1315 : ! get the MO coefficients for the connected k-point
1316 3912 : ik2 = nnlist(ik, i)
1317 3912 : mygrp = (ik2 >= kpoint%kp_range(1) .AND. ik2 <= kpoint%kp_range(2))
1318 : IF (mygrp) THEN
1319 3912 : ikk = ik2 - kpoint%kp_range(1) + 1
1320 3912 : kp => kpoint%kp_env(ikk)%kpoint_env
1321 3912 : cmo_coeff_source => kp%mos(ispin)%cmo_coeff
1322 : ELSE
1323 0 : NULLIFY (cmo_coeff_source, kp)
1324 : END IF
1325 3912 : CALL wannier90_copy_cfm_general(cmo_coeff_source, fmk2_cfm, para_env)
1326 : !
1327 : ! transfer realspace overlaps to connected k-point
1328 3912 : ibs = nblist(ik, i)
1329 3912 : ksign = SIGN(1.0_dp, REAL(ibs, KIND=dp))
1330 3912 : ibs = ABS(ibs)
1331 3912 : 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 360 : CALL dbcsr_set(rmatrix_full, 0.0_dp)
1335 360 : CALL dbcsr_set(cmatrix_full, 0.0_dp)
1336 360 : CALL dbcsr_set(loop_real, 0.0_dp)
1337 360 : CALL dbcsr_set(loop_imag, 0.0_dp)
1338 : CALL rskp_transform(rmatrix_full, cmatrix_full, berry_matrix(ibs)%cosmat, 1, &
1339 360 : kpoint%xkp(:, ik2), berry_cell_index, overlap_nl)
1340 : CALL rskp_transform(loop_real, loop_imag, berry_matrix(ibs)%sinmat, 1, &
1341 360 : kpoint%xkp(:, ik2), berry_cell_index, overlap_nl, rs_sign=ksign)
1342 360 : CALL dbcsr_add(rmatrix_full, loop_imag, 1.0_dp, -1.0_dp)
1343 360 : 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 3552 : 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 3912 : CALL copy_dbcsr_to_fm(rmatrix_full, mat_real)
1352 3912 : CALL copy_dbcsr_to_fm(cmatrix_full, mat_imag)
1353 3912 : 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 3912 : 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 3912 : fmk1_cfm, tmp_cfm, CMPLX(0.0_dp, 0.0_dp, KIND=dp), mmn_cfm)
1358 3912 : IF (do_soc) THEN
1359 136 : CALL cp_cfm_get_submatrix(mmn_cfm, scalar_overlap)
1360 136 : link_matrix(:, :) = MATMUL(CONJG(TRANSPOSE(spinor_coeff(1:nmo, :, ik))), &
1361 544 : MATMUL(scalar_overlap, spinor_coeff(1:nmo, :, ik2))) + &
1362 544 : MATMUL(CONJG(TRANSPOSE(spinor_coeff(nmo + 1:, :, ik))), &
1363 922480 : MATMUL(scalar_overlap, spinor_coeff(nmo + 1:, :, ik2)))
1364 : END IF
1365 : !
1366 : ! write to output file
1367 3912 : 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 9344 : phase_center(1:3))
1371 2336 : gauge_real = COS(gauge_arg)
1372 2336 : gauge_imag = SIN(gauge_arg)
1373 : ELSE
1374 : gauge_real = 1.0_dp
1375 : gauge_imag = 0.0_dp
1376 : END IF
1377 3912 : IF (iunit > 0) THEN
1378 1956 : WRITE (iunit, "(2I8,3I5)") ik, ik2, nncell(1:3, ik, i)
1379 : END IF
1380 17800 : DO ib2 = 1, num_bands
1381 77704 : DO ib1 = 1, num_bands
1382 59904 : IF (do_soc) THEN
1383 8704 : rmmn = REAL(link_matrix(ib1, ib2), dp)
1384 8704 : cmmn = AIMAG(link_matrix(ib1, ib2))
1385 : ELSE
1386 51200 : CALL cp_cfm_get_element(mmn_cfm, band_indices(ib1), band_indices(ib2), zmmn)
1387 51200 : rmmn = REAL(zmmn, KIND=dp)
1388 51200 : cmmn = AIMAG(zmmn)
1389 : END IF
1390 59904 : gauge_tmp = gauge_real*rmmn - gauge_imag*cmmn
1391 59904 : cmmn = gauge_imag*rmmn + gauge_real*cmmn
1392 59904 : rmmn = gauge_tmp
1393 59904 : link_matrix(ib1, ib2) = CMPLX(rmmn, cmmn, dp)
1394 73792 : IF (iunit > 0) THEN
1395 29952 : WRITE (iunit, "(2E30.14)") rmmn, cmmn
1396 : END IF
1397 : END DO
1398 : END DO
1399 4848 : IF (do_wilson) THEN
1400 328 : iloop = loop_index(ik)
1401 328 : CALL wilson_step(wilson_product(:, :, iloop), link_matrix, link_sv, status, 1.e-10_dp)
1402 328 : IF (status /= 0) THEN
1403 0 : CPABORT("Wilson link singular or SVD failed; refine sampling and check subspace.")
1404 : END IF
1405 328 : 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 66 : IF (export_state .OR. do_parity) THEN
1412 12 : state_components = 1
1413 12 : IF (do_soc) state_components = 2
1414 84 : ALLOCATE (export_scalar(nao, nscalar), export_coeff(nao*state_components, num_bands))
1415 12 : state_unit = -1
1416 12 : IF (export_state) CALL topology_state_begin(qs_env_kp, TRIM(seed_name)//".topology", nao, num_bands, num_kpts, &
1417 4 : state_components, num_bands_tot, spin_channel, band_indices, state_unit)
1418 12 : IF (do_parity) THEN
1419 24 : CPASSERT(ALL(kpoint%kp_range == [1, num_kpts]))
1420 0 : ALLOCATE (parity_metric(nao, nao), parity_phase(nao), parity_map(nao), &
1421 0 : parity_odd(2**tqc_dimension), ebr_coefficients(2, 2**tqc_dimension), &
1422 112 : parity_energies(num_bands_tot))
1423 56 : parity_odd = -1
1424 8 : IF (do_tqc) THEN
1425 102 : IF (num_bands /= nelectron .OR. MOD(num_bands, 2) /= 0 .OR. &
1426 : ANY(band_indices /= [(i, i=1, num_bands)])) THEN
1427 0 : CPABORT("INVERSION_TQC requires the full occupied spinor band prefix.")
1428 : END IF
1429 : END IF
1430 : END IF
1431 156 : DO ik = 1, num_kpts
1432 144 : kp => kpoint%kp_env(ik)%kpoint_env
1433 144 : CALL cp_cfm_copy_general(kp%mos(spin_channel)%cmo_coeff, fmk1_cfm, para_env)
1434 144 : CALL cp_cfm_get_submatrix(fmk1_cfm, export_scalar)
1435 144 : IF (do_soc) THEN
1436 230304 : export_coeff(:nao, :) = MATMUL(export_scalar, spinor_coeff(:nscalar, :, ik))
1437 174560 : export_coeff(nao + 1:, :) = MATMUL(export_scalar, spinor_coeff(nscalar + 1:, :, ik))
1438 32 : CALL topology_state_point(state_unit, ik, kpt_latt(:, ik), spinor_values(:, ik), export_coeff)
1439 688 : IF (do_parity) parity_energies(:) = spinor_values(:, ik)
1440 : ELSE
1441 784 : export_coeff(:, :) = export_scalar(:, band_indices)
1442 112 : CALL get_mo_set(kp%mos(spin_channel), eigenvalues=eigenvalues)
1443 112 : CALL topology_state_point(state_unit, ik, kpt_latt(:, ik), eigenvalues(:nscalar), export_coeff)
1444 128 : IF (do_parity) parity_energies(:) = eigenvalues(:nscalar)
1445 : END IF
1446 144 : IF (.NOT. do_parity) CYCLE
1447 40 : parity_gap = HUGE(1.0_dp)
1448 712 : DO jpar = 1, num_bands_tot
1449 4776 : IF (ANY(band_indices == jpar)) CYCLE
1450 3920 : parity_gap = MIN(parity_gap, MINVAL(ABS(parity_energies(band_indices) - parity_energies(jpar))))
1451 : END DO
1452 40 : IF (num_bands == num_bands_tot .OR. parity_gap <= gap_tol) THEN
1453 0 : CPABORT("PARITY requires an isolated selected subspace and computed excluded bands.")
1454 : END IF
1455 160 : 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 36 : parity_phase, parity_tolerance, status)
1458 36 : IF (status /= 0) THEN
1459 0 : CPABORT("PARITY: inversion does not preserve geometry and atomic kinds.")
1460 : END IF
1461 36 : CALL dbcsr_set(rmatrix, 0.0_dp)
1462 36 : CALL dbcsr_set(cmatrix, 0.0_dp)
1463 : CALL rskp_transform(rmatrix, cmatrix, rsmat=matrix_s, ispin=1, xkp=kpoint%xkp(:, ik), &
1464 36 : cell_to_index=cell_to_index, sab_nl=sab_nl)
1465 36 : CALL dbcsr_desymmetrize(rmatrix, rmatrix_full)
1466 36 : CALL dbcsr_desymmetrize(cmatrix, cmatrix_full)
1467 36 : CALL copy_dbcsr_to_fm(rmatrix_full, mat_real)
1468 36 : CALL copy_dbcsr_to_fm(cmatrix_full, mat_imag)
1469 36 : CALL cp_fm_to_cfm(mat_real, mat_imag, omat_cfm)
1470 36 : 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 300 : parity_energy_tolerance, counts, parity_error, parity_energy_error, status, parity_checks)
1474 36 : IF (iw > 0) THEN
1475 18 : WRITE (iw, '(T2,A,3F10.5,A,2I8,A,3ES14.5)') 'TQC| TRIM ', kpt_latt(:, ik), &
1476 36 : ' even/odd states ', counts, ' residuals/gap ', parity_error, parity_energy_error, parity_gap
1477 18 : WRITE (iw, '(T2,A,4ES14.5)') 'TQC| Metric/normalization/inversion/TR residuals: ', parity_checks
1478 : END IF
1479 36 : IF (status /= 0) THEN
1480 0 : CPABORT("PARITY: metric, subspace or symmetry representation check failed.")
1481 : END IF
1482 36 : IF (tqc_dimension == 2 .AND. MODULO(NINT(2*kpt_latt(3, ik)), 2) /= 0) CYCLE
1483 36 : trim_id = 1
1484 128 : DO axis = 1, tqc_dimension
1485 128 : trim_id = trim_id + 2**(axis - 1)*MODULO(NINT(2*kpt_latt(axis, ik)), 2)
1486 : END DO
1487 36 : IF (parity_odd(trim_id) >= 0 .AND. parity_odd(trim_id) /= counts(2)) THEN
1488 0 : CPABORT("PARITY: repeated equivalent TRIM have inconsistent multiplicities.")
1489 : END IF
1490 156 : parity_odd(trim_id) = counts(2)
1491 : END DO
1492 12 : IF (state_unit > 0) CALL close_file(state_unit)
1493 12 : IF (do_parity) THEN
1494 8 : IF (ALL(parity_odd < 0)) THEN
1495 0 : CPABORT("PARITY requires a TRIM in the requested dimension.")
1496 : END IF
1497 : END IF
1498 12 : IF (do_tqc) THEN
1499 38 : IF (ANY(parity_odd < 0)) THEN
1500 0 : CPABORT("INVERSION_TQC requires all four (2D) or eight (3D) TRIM.")
1501 : END IF
1502 38 : IF (ANY(MOD(parity_odd, 2) /= 0)) THEN
1503 0 : CPABORT("INVERSION_TQC requires whole odd-parity Kramers pairs.")
1504 : END IF
1505 38 : parity_odd(:) = parity_odd/2
1506 6 : CALL inversion_indicators(parity_odd, num_bands/2, tqc_dimension, strong, weak, z4, status)
1507 6 : IF (status /= 0) CPABORT("Invalid inversion indicator data.")
1508 : CALL inversion_ebr_signature(parity_odd, num_bands/2, signed_atomic, nonnegative_atomic, &
1509 6 : ebr_coefficients, status)
1510 6 : IF (status /= 0) CPABORT("Invalid inversion EBR data.")
1511 6 : IF (iw > 0) THEN
1512 3 : WRITE (iw, '(T2,A,I0)') 'TQC| Inversion-subgroup dimension: ', tqc_dimension
1513 3 : WRITE (iw, '(T2,A,I0)') 'TQC| Fu-Kane parity index: ', strong
1514 3 : IF (tqc_dimension == 3) THEN
1515 1 : WRITE (iw, '(T2,A,3I3)') 'TQC| Weak parity indices: ', weak
1516 1 : WRITE (iw, '(T2,A,I0)') 'TQC| Inversion Z4 (sum odd pairs mod 4): ', z4
1517 : END IF
1518 3 : WRITE (iw, '(T2,A,L1)') 'TQC| Signed atomic signature: ', signed_atomic
1519 3 : WRITE (iw, '(T2,A,L1)') 'TQC| Nonnegative atomic signature: ', nonnegative_atomic
1520 3 : IF (nonnegative_atomic) THEN
1521 14 : DO i = 1, SIZE(parity_odd)
1522 12 : WRITE (iw, '(T2,A,I0,A,2I8)') 'TQC| Center bit index ', i - 1, &
1523 26 : ' even/odd EBR pairs ', ebr_coefficients(:, i)
1524 : END DO
1525 2 : WRITE (iw, '(T2,A)') 'TQC| Atomic-compatible symmetry signature; not a proof of trivial topology.'
1526 1 : ELSE IF (signed_atomic) THEN
1527 : WRITE (iw, '(T2,A)') 'TQC| Nonnegative EBR obstruction; '// &
1528 0 : 'exclude hidden stable topology before calling it fragile.'
1529 : ELSE
1530 1 : WRITE (iw, '(T2,A)') 'TQC| Stable inversion-symmetry indicator obstruction.'
1531 : END IF
1532 3 : WRITE (iw, '(T2,A)') 'TQC| TRIM checks do not establish a bulk gap; verify band isolation over the BZ.'
1533 3 : 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 226 : DO i = 1, nbs
1538 160 : CALL dbcsr_deallocate_matrix_set(berry_matrix(i)%cosmat)
1539 226 : CALL dbcsr_deallocate_matrix_set(berry_matrix(i)%sinmat)
1540 : END DO
1541 66 : DEALLOCATE (berry_matrix)
1542 66 : CALL cp_fm_struct_release(matrix_struct_work)
1543 66 : CALL cp_cfm_release(fmk1_cfm)
1544 66 : CALL cp_cfm_release(fmk2_cfm)
1545 66 : CALL cp_cfm_release(tmp_cfm)
1546 66 : CALL cp_fm_struct_release(matrix_struct_ao)
1547 66 : CALL cp_fm_release(mat_real)
1548 66 : CALL cp_fm_release(mat_imag)
1549 66 : CALL cp_cfm_release(omat_cfm)
1550 66 : CALL cp_fm_struct_release(matrix_struct_mmn)
1551 66 : CALL cp_cfm_release(mmn_cfm)
1552 66 : CALL dbcsr_deallocate_matrix(rmatrix_full)
1553 66 : CALL dbcsr_deallocate_matrix(cmatrix_full)
1554 66 : IF (do_soc .OR. do_parity) THEN
1555 12 : CALL dbcsr_deallocate_matrix(rmatrix)
1556 12 : CALL dbcsr_deallocate_matrix(cmatrix)
1557 : END IF
1558 66 : IF (ordered_berry) THEN
1559 22 : CALL dbcsr_deallocate_matrix(loop_real)
1560 22 : CALL dbcsr_deallocate_matrix(loop_imag)
1561 : END IF
1562 : !
1563 66 : IF (iunit > 0) THEN
1564 33 : CALL close_file(iunit)
1565 : END IF
1566 : !
1567 66 : IF (do_wilson) THEN
1568 16 : IF (iw > 0 .AND. num_bands < num_bands_tot) WRITE (iw, '(T2,A,ES18.10)') &
1569 8 : "TOPOLOGY| Minimum sampled subspace gap [eV]: ", direct_gap*evolt
1570 16 : IF (direct_gap < gap_tol) CPABORT("Wilson subspace is not isolated on the sampled points.")
1571 16 : IF (lowest_bands) THEN
1572 16 : IF (iw > 0) THEN
1573 : WRITE (iw, '(T2,A,ES18.10)') &
1574 8 : "TOPOLOGY| Maximum selected-band energy [eV]: ", valence_max*evolt
1575 : WRITE (iw, '(T2,A,ES18.10)') &
1576 8 : "TOPOLOGY| Minimum excluded-band energy [eV]: ", conduction_min*evolt
1577 : WRITE (iw, '(T2,A,ES18.10)') &
1578 8 : "TOPOLOGY| Sampled indirect gap [eV]: ", (conduction_min - valence_max)*evolt
1579 8 : IF (conduction_min - valence_max <= gap_tol) WRITE (iw, '(T2,A)') &
1580 0 : "TOPOLOGY| Isolated band subspace, but no resolved common spectral gap on these samples."
1581 : END IF
1582 16 : IF (require_global_gap .AND. conduction_min - valence_max <= gap_tol) THEN
1583 0 : CPABORT("No positive sampled indirect gap above the selected bands.")
1584 : END IF
1585 : END IF
1586 68 : DO iloop = 1, nloop
1587 52 : CALL wilson_spectrum(wilson_product(:, :, iloop), wcc_out(:, iloop), berry_phase, status)
1588 52 : IF (status /= 0) CPABORT("Wilson eigenvalue calculation failed.")
1589 52 : IF (iw > 0) WRITE (iw, '(T2,A,I0,A,F18.12,A,ES12.4)') &
1590 42 : "TOPOLOGY| Loop ", iloop, " Berry phase [rad]: ", berry_phase, " min singular value: ", loop_sv(iloop)
1591 : END DO
1592 16 : IF (para_env%is_source()) THEN
1593 8 : CALL open_file(TRIM(seed_name)//".wilson", unit_number=iunit, file_status="UNKNOWN", file_action="WRITE")
1594 8 : WRITE (iunit, '(A)') "# loop, minimum link singular value, sorted WCC (arg(lambda)/(2*pi))"
1595 34 : DO iloop = 1, nloop
1596 34 : WRITE (iunit, '(I8,*(1X,ES24.16))') iloop, loop_sv(iloop), wcc_out(:, iloop)
1597 : END DO
1598 8 : CALL close_file(iunit)
1599 : END IF
1600 16 : IF (do_z2) THEN
1601 4 : CALL z2_from_wcc(wcc_out, z2_value, status, 1.e-5_dp)
1602 4 : IF (status /= 0 .AND. iw > 0) WRITE (iw, '(T2,A,I0)') &
1603 0 : "TOPOLOGY| Kramers/crossing check requests refinement, status: ", status
1604 4 : IF (iw > 0) WRITE (iw, '(T2,A,I0)') "TOPOLOGY| Z2 candidate (before sampling convergence): ", z2_value
1605 : END IF
1606 16 : IF (do_chern) THEN
1607 4 : CALL chern_from_wcc(wcc_out, chern_value, chern_winding, status, 1.e-5_dp)
1608 4 : IF (status /= 0) THEN
1609 0 : chern_value = HUGE(0)
1610 0 : IF (iw > 0) WRITE (iw, '(T2,A,I0)') &
1611 0 : "TOPOLOGY| Chern closure/winding check requests refinement, status: ", status
1612 4 : ELSE IF (iw > 0) THEN
1613 : WRITE (iw, '(T2,A,I0,A,F18.12)') &
1614 2 : "TOPOLOGY| First Chern candidate: ", chern_value, ", winding: ", chern_winding
1615 : END IF
1616 : END IF
1617 : END IF
1618 : ! clean up
1619 66 : IF (ordered_berry) CALL kpoint_release(berry_kpoint)
1620 66 : DEALLOCATE (kpt_latt, atoms_cart, atom_symbols, eigval)
1621 66 : DEALLOCATE (nnlist, nncell)
1622 66 : DEALLOCATE (nblist, b_latt)
1623 66 : 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 33 : file_status='REPLACE', file_action='WRITE')
1627 33 : WRITE (iunit, '(A)') '# Exported band, original MO index (before EXCLUDE_BANDS)'
1628 150 : DO ib = 1, num_bands
1629 150 : WRITE (iunit, '(2I10)') ib, band_indices(ib)
1630 : END DO
1631 33 : CALL close_file(iunit)
1632 : END IF
1633 66 : IF (nexcl > 0) DEALLOCATE (exclude_bands)
1634 66 : DEALLOCATE (band_indices, keep_band, allowed_band)
1635 66 : IF (ALLOCATED(projection_eigenvalues)) DEALLOCATE (projection_eigenvalues)
1636 66 : IF (do_kpoints) THEN
1637 66 : NULLIFY (qs_env_kp)
1638 : ELSE
1639 0 : CALL qs_env_release(qs_env_kp)
1640 0 : DEALLOCATE (qs_env_kp)
1641 : NULLIFY (qs_env_kp)
1642 : END IF
1643 :
1644 66 : CALL kpoint_release(kpoint)
1645 :
1646 1056 : 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 46 : 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 46 : success = .FALSE.
1684 46 : reason = 'Wannier90 export k-point MOs are not initialized'
1685 46 : aligned_degenerate_blocks = 0
1686 46 : aligned_degenerate_max_size = 0
1687 46 : aligned_degenerate_min_svalue = 0.0_dp
1688 52 : IF (.NOT. ASSOCIATED(kpoint)) RETURN
1689 46 : IF (.NOT. ASSOCIATED(kpoint%kp_env)) RETURN
1690 46 : IF (SIZE(kpoint%kp_env) == 0) RETURN
1691 46 : first_spin = 1
1692 46 : last_spin = SIZE(kpoint%kp_env(1)%kpoint_env%mos)
1693 46 : IF (PRESENT(spin_channel)) THEN
1694 42 : CPASSERT(spin_channel >= 1 .AND. spin_channel <= last_spin)
1695 : first_spin = spin_channel
1696 : last_spin = spin_channel
1697 : END IF
1698 86 : DO ispin = first_spin, last_spin
1699 : CALL prepare_wannier90_spin_mos(kpoint, qs_kpoint, matrix_s, matrix_ks, cell_to_index, &
1700 46 : sab_nl, para_env, ispin, success, reason, blocks, max_size, min_svalue)
1701 46 : IF (.NOT. success) RETURN
1702 126 : IF (blocks > 0) THEN
1703 24 : IF (aligned_degenerate_blocks == 0) aligned_degenerate_min_svalue = min_svalue
1704 24 : aligned_degenerate_min_svalue = MIN(aligned_degenerate_min_svalue, min_svalue)
1705 24 : aligned_degenerate_blocks = aligned_degenerate_blocks + blocks
1706 24 : aligned_degenerate_max_size = MAX(aligned_degenerate_max_size, max_size)
1707 : END IF
1708 : END DO
1709 46 : 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 46 : 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 46 : 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 46 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: eigenvalues_buffer, occupation_buffer, &
1756 46 : source_eigenvalues_buffer, &
1757 46 : source_occupation_buffer
1758 46 : 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 46 : success = .FALSE.
1768 46 : reason = ""
1769 46 : aligned_degenerate_blocks = 0
1770 46 : aligned_degenerate_max_size = 0
1771 46 : aligned_degenerate_min_svalue = HUGE(1.0_dp)
1772 46 : NULLIFY (matrix_struct_source, matrix_struct_work)
1773 :
1774 46 : IF (.NOT. ASSOCIATED(kpoint)) THEN
1775 0 : reason = "internal Wannier90 k-point object is not available"
1776 0 : RETURN
1777 : END IF
1778 46 : IF (.NOT. ASSOCIATED(qs_kpoint)) THEN
1779 0 : reason = "SCF k-point object is not available"
1780 0 : RETURN
1781 : END IF
1782 46 : IF (.NOT. ASSOCIATED(kpoint%kp_env) .OR. .NOT. ASSOCIATED(qs_kpoint%kp_env)) THEN
1783 0 : reason = "k-point MO environments are not initialized"
1784 0 : RETURN
1785 : END IF
1786 46 : IF (.NOT. ASSOCIATED(kpoint%blacs_env)) THEN
1787 0 : reason = "Wannier90 k-point BLACS environment is not initialized"
1788 0 : RETURN
1789 : END IF
1790 :
1791 46 : CALL build_wannier90_scf_mapping(kpoint, qs_kpoint, source_kpoint, sym_index, ok, reason)
1792 46 : IF (.NOT. ok) RETURN
1793 626 : nsymmetry = COUNT(sym_index > 0)
1794 :
1795 46 : kp => kpoint%kp_env(1)%kpoint_env
1796 46 : IF (.NOT. ASSOCIATED(kp%mos(ispin)%cmo_coeff)) THEN
1797 0 : reason = "Wannier90 export k-point MOs are not complex-valued"
1798 0 : DEALLOCATE (source_kpoint, sym_index)
1799 0 : RETURN
1800 : END IF
1801 46 : CALL get_mo_set(kp%mos(ispin), nao=nao, nmo=nmo)
1802 :
1803 46 : kp_source => qs_kpoint%kp_env(1)%kpoint_env
1804 46 : IF (.NOT. ASSOCIATED(kp_source%mos(ispin)%cmo_coeff)) THEN
1805 0 : reason = "SCF MOs are real-valued; complex symmetry phases cannot be reconstructed"
1806 0 : DEALLOCATE (source_kpoint, sym_index)
1807 0 : RETURN
1808 : END IF
1809 46 : CALL get_mo_set(kp_source%mos(ispin), nao=nao_src, nmo=nmo_src)
1810 46 : CALL para_env%max(nao_src)
1811 46 : CALL para_env%max(nmo_src)
1812 46 : IF (nao_src /= nao) THEN
1813 0 : reason = "SCF and Wannier90 MO bases have different AO dimensions"
1814 0 : DEALLOCATE (source_kpoint, sym_index)
1815 0 : RETURN
1816 : END IF
1817 46 : IF (nmo_src < nmo) THEN
1818 0 : reason = "SCF MO set has fewer bands than the Wannier90 export"
1819 0 : DEALLOCATE (source_kpoint, sym_index)
1820 0 : RETURN
1821 : END IF
1822 46 : source_window = nmo_src > nmo
1823 46 : degenerate_band_tol = 1.0e-8_dp
1824 46 : CALL get_kpoint_info(qs_kpoint, kp_range=source_kp_range)
1825 46 : IF (source_kp_range(1) /= 1 .OR. source_kp_range(2) /= qs_kpoint%nkp) THEN
1826 6 : reason = "SCF k-point symmetry data are distributed over k-point parallel groups"
1827 6 : DEALLOCATE (source_kpoint, sym_index)
1828 6 : 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 40 : IF (nsymmetry > 0 .AND. nmo_src > nmo) THEN
1834 0 : local_min_band_gap = HUGE(1.0_dp)
1835 0 : min_gap_band = nmo
1836 0 : min_gap_kpoint = 0
1837 0 : min_gap_spin = 0
1838 0 : DO ikred = source_kp_range(1), source_kp_range(2)
1839 0 : ikpgr = ikred - source_kp_range(1) + 1
1840 0 : kp_source => qs_kpoint%kp_env(ikpgr)%kpoint_env
1841 0 : CALL get_mo_set(kp_source%mos(ispin), eigenvalues=eigenvalues)
1842 0 : band_gap = ABS(eigenvalues(nmo + 1) - eigenvalues(nmo))
1843 0 : IF (band_gap < local_min_band_gap) THEN
1844 0 : local_min_band_gap = band_gap
1845 0 : min_gap_band = nmo
1846 0 : min_gap_kpoint = ikred
1847 0 : min_gap_spin = ispin
1848 : END IF
1849 : END DO
1850 0 : min_band_gap = local_min_band_gap
1851 0 : CALL para_env%min(min_band_gap)
1852 0 : IF (ABS(local_min_band_gap - min_band_gap) > degenerate_band_tol*EPSILON(1.0_dp)) THEN
1853 0 : min_gap_kpoint = 0
1854 0 : min_gap_spin = 0
1855 : END IF
1856 0 : CALL para_env%max(min_gap_kpoint)
1857 0 : CALL para_env%max(min_gap_spin)
1858 0 : CALL para_env%max(min_gap_band)
1859 0 : IF (min_band_gap < degenerate_band_tol) THEN
1860 : WRITE (reason, "(A,ES9.2,A,I0,A,I0,A,I0)") &
1861 0 : "degenerate atom/AO W90 reuse guarded: edge gap=", min_band_gap, ", k=", &
1862 0 : min_gap_kpoint, ", s=", min_gap_spin, ", nband=", min_gap_band
1863 0 : DEALLOCATE (source_kpoint, sym_index)
1864 0 : RETURN
1865 : END IF
1866 : END IF
1867 40 : blacs_env => kpoint%blacs_env
1868 : CALL cp_fm_struct_create(matrix_struct_work, nrow_global=nao, ncol_global=nmo, &
1869 40 : para_env=para_env, context=blacs_env)
1870 40 : CALL cp_cfm_create(src_cfm, matrix_struct_work)
1871 40 : CALL cp_cfm_create(dst_cfm, matrix_struct_work)
1872 40 : IF (source_window) THEN
1873 : CALL cp_fm_struct_create(matrix_struct_source, nrow_global=nao, ncol_global=nmo_src, &
1874 0 : para_env=para_env, context=blacs_env)
1875 0 : CALL cp_cfm_create(src_cfm_full, matrix_struct_source)
1876 0 : CALL cp_cfm_create(dst_cfm_full, matrix_struct_source)
1877 : END IF
1878 0 : ALLOCATE (eigenvalues_buffer(nmo), occupation_buffer(nmo), &
1879 280 : source_eigenvalues_buffer(nmo_src), source_occupation_buffer(nmo_src))
1880 :
1881 40 : CALL get_kpoint_info(kpoint, kp_range=kp_range)
1882 40 : CALL get_kpoint_info(qs_kpoint, kp_range=source_kp_range)
1883 :
1884 572 : DO ik = 1, kpoint%nkp
1885 532 : ikred = source_kpoint(ik)
1886 532 : my_kpgrp = (ik >= kp_range(1) .AND. ik <= kp_range(2))
1887 532 : my_source_kpgrp = (ikred >= source_kp_range(1) .AND. ikred <= source_kp_range(2))
1888 2628 : source_eigenvalues_buffer(1:nmo_src) = 0.0_dp
1889 2628 : source_occupation_buffer(1:nmo_src) = 0.0_dp
1890 532 : NULLIFY (source_cfm)
1891 532 : IF (my_source_kpgrp) THEN
1892 532 : ikpgr = ikred - source_kp_range(1) + 1
1893 532 : kp_source => qs_kpoint%kp_env(ikpgr)%kpoint_env
1894 532 : source_cfm => kp_source%mos(ispin)%cmo_coeff
1895 : CALL get_mo_set(kp_source%mos(ispin), eigenvalues=eigenvalues, &
1896 532 : occupation_numbers=occupation)
1897 2628 : source_eigenvalues_buffer(1:nmo_src) = eigenvalues(1:nmo_src)
1898 2628 : source_occupation_buffer(1:nmo_src) = occupation(1:nmo_src)
1899 : END IF
1900 532 : IF (source_window) THEN
1901 0 : CALL wannier90_copy_cfm_general(source_cfm, src_cfm_full, para_env)
1902 : ELSE
1903 532 : CALL wannier90_copy_cfm_general(source_cfm, src_cfm, para_env)
1904 : END IF
1905 532 : IF (my_source_kpgrp) THEN
1906 532 : source_owner_count = 1.0_dp
1907 : ELSE
1908 0 : source_owner_count = 0.0_dp
1909 : END IF
1910 532 : CALL para_env%sum(source_owner_count)
1911 532 : CALL para_env%sum(source_eigenvalues_buffer)
1912 532 : CALL para_env%sum(source_occupation_buffer)
1913 532 : IF (source_owner_count > 0.0_dp) THEN
1914 : source_eigenvalues_buffer(1:nmo_src) = &
1915 2628 : source_eigenvalues_buffer(1:nmo_src)/source_owner_count
1916 : source_occupation_buffer(1:nmo_src) = &
1917 2628 : source_occupation_buffer(1:nmo_src)/source_owner_count
1918 : END IF
1919 2628 : eigenvalues_buffer(1:nmo) = source_eigenvalues_buffer(1:nmo)
1920 2628 : occupation_buffer(1:nmo) = source_occupation_buffer(1:nmo)
1921 532 : IF (source_window) THEN
1922 0 : CALL cp_cfm_to_cfm(src_cfm_full, src_cfm, nmo)
1923 : END IF
1924 :
1925 532 : ok = .FALSE.
1926 532 : reason = ""
1927 532 : aligned_blocks = 0
1928 532 : aligned_max_size = 0
1929 532 : aligned_min_svalue = 0.0_dp
1930 532 : IF (sym_index(ik) > 0) THEN
1931 412 : kpsym => qs_kpoint%kp_sym(ikred)%kpoint_sym
1932 412 : IF (ASSOCIATED(kpsym)) THEN
1933 412 : best_reason = ""
1934 412 : best_residual = HUGE(1.0_dp)
1935 412 : num_candidates = 0
1936 : ! Little-group operations can reach the same target k-point; keep the first valid eigenspace.
1937 3198 : DO isym_try = 1, kpsym%nwred
1938 3198 : IF (.NOT. kpoint_same_periodic(kpoint%xkp(1:3, ik), &
1939 : kpsym%xkp(1:3, isym_try))) CYCLE
1940 412 : num_candidates = num_candidates + 1
1941 : CALL kpoint_transform_scf_mo(src_cfm, dst_cfm, &
1942 : qs_kpoint, ikred, isym_try, ok, &
1943 412 : candidate_reason)
1944 412 : IF (.NOT. ok) THEN
1945 0 : 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 412 : aligned_min_svalue, candidate_residual)
1955 412 : IF (candidate_residual < best_residual) THEN
1956 412 : best_residual = candidate_residual
1957 412 : best_reason = candidate_reason
1958 : END IF
1959 412 : IF (.NOT. ok) THEN
1960 0 : IF (source_window) THEN
1961 : CALL kpoint_transform_scf_mo(src_cfm_full, dst_cfm_full, qs_kpoint, &
1962 0 : ikred, isym_try, ok, candidate_reason)
1963 0 : 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 0 : candidate_residual)
1970 0 : IF (candidate_residual < best_residual) THEN
1971 0 : best_residual = candidate_residual
1972 0 : 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 0 : candidate_residual)
1981 0 : IF (candidate_residual < best_residual) THEN
1982 0 : best_residual = candidate_residual
1983 0 : best_reason = candidate_reason
1984 : END IF
1985 : END IF
1986 : END IF
1987 412 : IF (ok) THEN
1988 412 : aligned_blocks = candidate_aligned_blocks
1989 412 : aligned_max_size = candidate_aligned_max_size
1990 412 : sym_index(ik) = isym_try
1991 412 : EXIT
1992 : END IF
1993 412 : reason = candidate_reason
1994 : END DO
1995 412 : 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 0 : "atom/AO W90 guarded: best/", num_candidates, "=", best_residual, &
1998 0 : " k=", ik, " ", TRIM(best_reason)
1999 412 : ELSE IF (.NOT. ok .AND. num_candidates == 0) THEN
2000 0 : reason = "no matching SCF symmetry operation candidate"
2001 : END IF
2002 : ELSE
2003 0 : 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 120 : ikred, sym_index(ik), ok, reason)
2008 : END IF
2009 532 : 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 120 : aligned_min_svalue, candidate_residual)
2017 120 : IF (.NOT. ok .AND. source_window) THEN
2018 : CALL kpoint_transform_scf_mo(src_cfm_full, dst_cfm_full, qs_kpoint, &
2019 0 : ikred, sym_index(ik), ok, reason)
2020 0 : 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 0 : candidate_residual)
2026 : END IF
2027 120 : 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 0 : source_window_min_svalue, candidate_residual)
2033 : END IF
2034 : END IF
2035 532 : IF (.NOT. ok) THEN
2036 0 : CALL cp_cfm_release(src_cfm)
2037 0 : CALL cp_cfm_release(dst_cfm)
2038 0 : CALL cp_fm_struct_release(matrix_struct_work)
2039 0 : IF (source_window) THEN
2040 0 : CALL cp_cfm_release(src_cfm_full)
2041 0 : CALL cp_cfm_release(dst_cfm_full)
2042 0 : CALL cp_fm_struct_release(matrix_struct_source)
2043 : END IF
2044 0 : DEALLOCATE (source_kpoint, sym_index, eigenvalues_buffer, occupation_buffer, &
2045 0 : source_eigenvalues_buffer, source_occupation_buffer)
2046 0 : RETURN
2047 : END IF
2048 532 : IF (sym_index(ik) /= 0) THEN
2049 456 : aligned_degenerate_blocks = aligned_degenerate_blocks + aligned_blocks
2050 456 : aligned_degenerate_max_size = MAX(aligned_degenerate_max_size, aligned_max_size)
2051 456 : IF (aligned_blocks > 0) THEN
2052 360 : aligned_degenerate_min_svalue = MIN(aligned_degenerate_min_svalue, aligned_min_svalue)
2053 : END IF
2054 : END IF
2055 :
2056 532 : IF (my_kpgrp) THEN
2057 532 : ikpgr = ik - kp_range(1) + 1
2058 532 : kp => kpoint%kp_env(ikpgr)%kpoint_env
2059 : CALL get_mo_set(kp%mos(ispin), eigenvalues=eigenvalues, &
2060 532 : occupation_numbers=occupation)
2061 2628 : eigenvalues(1:nmo) = eigenvalues_buffer(1:nmo)
2062 2628 : occupation(1:nmo) = occupation_buffer(1:nmo)
2063 532 : destination_cfm => kp%mos(ispin)%cmo_coeff
2064 : ELSE
2065 : destination_cfm => empty_destination
2066 : END IF
2067 572 : CALL cp_cfm_copy_general(dst_cfm, destination_cfm, para_env)
2068 : END DO
2069 :
2070 40 : CALL cp_cfm_release(src_cfm)
2071 40 : CALL cp_cfm_release(dst_cfm)
2072 40 : CALL cp_fm_struct_release(matrix_struct_work)
2073 40 : IF (source_window) THEN
2074 0 : CALL cp_cfm_release(src_cfm_full)
2075 0 : CALL cp_cfm_release(dst_cfm_full)
2076 0 : CALL cp_fm_struct_release(matrix_struct_source)
2077 : END IF
2078 0 : DEALLOCATE (source_kpoint, sym_index, eigenvalues_buffer, occupation_buffer, &
2079 40 : source_eigenvalues_buffer, source_occupation_buffer)
2080 40 : IF (aligned_degenerate_blocks == 0) aligned_degenerate_min_svalue = 0.0_dp
2081 40 : success = .TRUE.
2082 :
2083 224 : 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 14 : 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 14 : REAL(KIND=dp), DIMENSION(:), POINTER :: eigenvalues
2107 : TYPE(kpoint_env_type), POINTER :: kp
2108 :
2109 14 : CALL get_kpoint_info(kpoint, nkp=nkp, kp_range=kp_range)
2110 14 : kp => kpoint%kp_env(1)%kpoint_env
2111 14 : CALL get_mo_set(kp%mos(ispin), nao=nao, nmo=nmo)
2112 0 : ALLOCATE (mo(nao, nmo, nkp), &
2113 140 : eigenvalue_snapshot(nmo, nkp), owner_weight(nkp))
2114 14 : mo(:, :, :) = CMPLX(0.0_dp, 0.0_dp, KIND=dp)
2115 14 : eigenvalue_snapshot(:, :) = 0.0_dp
2116 14 : owner_weight(:) = 0.0_dp
2117 350 : DO ik = kp_range(1), kp_range(2)
2118 336 : ikpgr = ik - kp_range(1) + 1
2119 336 : kp => kpoint%kp_env(ikpgr)%kpoint_env
2120 336 : CALL cp_cfm_get_submatrix(kp%mos(ispin)%cmo_coeff, mo(:, :, ik))
2121 336 : CALL get_mo_set(kp%mos(ispin), eigenvalues=eigenvalues)
2122 1600 : eigenvalue_snapshot(1:nmo, ik) = eigenvalues(1:nmo)
2123 350 : owner_weight(ik) = 1.0_dp
2124 : END DO
2125 14 : CALL para_env%sum(mo)
2126 14 : CALL para_env%sum(eigenvalue_snapshot)
2127 14 : CALL para_env%sum(owner_weight)
2128 350 : DO ik = 1, nkp
2129 350 : IF (owner_weight(ik) > 0.0_dp) THEN
2130 50000 : mo(:, :, ik) = mo(:, :, ik)/owner_weight(ik)
2131 : eigenvalue_snapshot(:, ik) = &
2132 1600 : eigenvalue_snapshot(:, ik)/owner_weight(ik)
2133 : END IF
2134 : END DO
2135 14 : DEALLOCATE (owner_weight)
2136 :
2137 14 : 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 2 : 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 2 : REAL(KIND=dp), DIMENSION(:), POINTER :: eigenvalues
2155 : TYPE(kpoint_env_type), POINTER :: kp
2156 :
2157 2 : CALL get_kpoint_info(kpoint, kp_range=kp_range)
2158 2 : nmo = SIZE(eigenvalue_snapshot, 1)
2159 18 : DO ik = kp_range(1), kp_range(2)
2160 16 : ikpgr = ik - kp_range(1) + 1
2161 16 : kp => kpoint%kp_env(ikpgr)%kpoint_env
2162 16 : CALL cp_cfm_set_submatrix(kp%mos(ispin)%cmo_coeff, mo(:, :, ik))
2163 16 : CALL get_mo_set(kp%mos(ispin), eigenvalues=eigenvalues)
2164 146 : eigenvalues(1:nmo) = eigenvalue_snapshot(1:nmo, ik)
2165 : END DO
2166 :
2167 2 : 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 14 : SUBROUTINE validate_wannier90_reused_mos(kpoint, matrix_s, cell_to_index, sab_nl, para_env, ispin, &
2185 14 : 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 14 : 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 14 : success = .FALSE.
2217 14 : max_subspace_deviation = 0.0_dp
2218 14 : min_svalue = HUGE(1.0_dp)
2219 14 : max_eigenvalue_deviation = 0.0_dp
2220 14 : NULLIFY (matrix_struct_work)
2221 :
2222 14 : CALL get_kpoint_info(kpoint, nkp=nkp, kp_range=kp_range)
2223 14 : kp => kpoint%kp_env(1)%kpoint_env
2224 14 : CALL get_mo_set(kp%mos(ispin), nao=nao, nmo=nmo)
2225 14 : CPASSERT(SIZE(reference_mo, 1) == nao)
2226 14 : CPASSERT(SIZE(reference_mo, 2) == nmo)
2227 14 : CPASSERT(SIZE(reference_mo, 3) == nkp)
2228 :
2229 14 : matrix_struct_work => kp%mos(ispin)%cmo_coeff%matrix_struct
2230 14 : CALL cp_cfm_create(ref_cfm, matrix_struct_work)
2231 14 : CALL cp_cfm_create(cand_cfm, matrix_struct_work)
2232 42 : ALLOCATE (eigenvalue_buffer(nmo))
2233 :
2234 350 : DO ik = 1, nkp
2235 336 : CALL cp_cfm_set_submatrix(ref_cfm, reference_mo(:, :, ik))
2236 336 : my_kpgrp = (ik >= kp_range(1) .AND. ik <= kp_range(2))
2237 0 : NULLIFY (candidate_source)
2238 : IF (my_kpgrp) THEN
2239 336 : ikpgr = ik - kp_range(1) + 1
2240 336 : kp => kpoint%kp_env(ikpgr)%kpoint_env
2241 336 : candidate_source => kp%mos(ispin)%cmo_coeff
2242 336 : CALL get_mo_set(kp%mos(ispin), eigenvalues=eigenvalues)
2243 1600 : eigenvalue_buffer(1:nmo) = eigenvalues(1:nmo)
2244 : ELSE
2245 0 : eigenvalue_buffer(1:nmo) = 0.0_dp
2246 : END IF
2247 336 : CALL wannier90_copy_cfm_general(candidate_source, cand_cfm, para_env)
2248 336 : IF (my_kpgrp) THEN
2249 336 : owner_count = 1.0_dp
2250 : ELSE
2251 0 : owner_count = 0.0_dp
2252 : END IF
2253 336 : CALL para_env%sum(owner_count)
2254 336 : CALL para_env%sum(eigenvalue_buffer)
2255 1600 : 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 1600 : 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 336 : ok, candidate_deviation, candidate_svalue)
2262 350 : IF (.NOT. ok) THEN
2263 0 : max_subspace_deviation = HUGE(1.0_dp)
2264 : ELSE
2265 336 : max_subspace_deviation = MAX(max_subspace_deviation, candidate_deviation)
2266 336 : min_svalue = MIN(min_svalue, candidate_svalue)
2267 : END IF
2268 : END DO
2269 14 : CALL para_env%max(max_subspace_deviation)
2270 14 : CALL para_env%min(min_svalue)
2271 14 : CALL para_env%max(max_eigenvalue_deviation)
2272 14 : success = max_subspace_deviation < subspace_tol .AND. max_eigenvalue_deviation < eigenvalue_tol
2273 :
2274 14 : DEALLOCATE (eigenvalue_buffer)
2275 14 : CALL cp_cfm_release(ref_cfm)
2276 14 : CALL cp_cfm_release(cand_cfm)
2277 :
2278 28 : 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 14 : 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 14 : 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 14 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: eigenvalues_buffer
2324 14 : 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 14 : max_subspace_deviation = HUGE(1.0_dp)
2332 14 : min_svalue = 0.0_dp
2333 14 : max_metric_deviation = HUGE(1.0_dp)
2334 14 : max_residual = HUGE(1.0_dp)
2335 14 : NULLIFY (matrix_struct_source, matrix_struct_work)
2336 :
2337 14 : CALL build_wannier90_scf_mapping(kpoint, qs_kpoint, source_kpoint, sym_index, ok, reason)
2338 14 : IF (.NOT. ok) RETURN
2339 14 : kp => kpoint%kp_env(1)%kpoint_env
2340 14 : kp_source => qs_kpoint%kp_env(1)%kpoint_env
2341 14 : IF (.NOT. ASSOCIATED(kp%mos(ispin)%cmo_coeff) .OR. &
2342 : .NOT. ASSOCIATED(kp_source%mos(ispin)%cmo_coeff)) THEN
2343 0 : DEALLOCATE (source_kpoint, sym_index)
2344 0 : RETURN
2345 : END IF
2346 14 : CALL get_mo_set(kp%mos(ispin), nao=nao, nmo=nmo)
2347 14 : CALL get_mo_set(kp_source%mos(ispin), nao=nao_src, nmo=nmo_src)
2348 14 : CALL para_env%max(nao_src)
2349 14 : CALL para_env%max(nmo_src)
2350 14 : IF (nao_src /= nao .OR. nmo_src < nmo) THEN
2351 0 : DEALLOCATE (source_kpoint, sym_index)
2352 0 : RETURN
2353 : END IF
2354 14 : source_window = nmo_src > nmo
2355 14 : CALL get_kpoint_info(kpoint, nkp=nkp, kp_range=kp_range)
2356 14 : CALL get_kpoint_info(qs_kpoint, kp_range=source_kp_range)
2357 14 : IF (source_kp_range(1) /= 1 .OR. source_kp_range(2) /= qs_kpoint%nkp) THEN
2358 0 : DEALLOCATE (source_kpoint, sym_index)
2359 0 : RETURN
2360 : END IF
2361 :
2362 : CALL cp_fm_struct_create(matrix_struct_work, nrow_global=nao, ncol_global=nmo, &
2363 14 : para_env=para_env, context=kpoint%blacs_env)
2364 14 : CALL cp_cfm_create(ref_cfm, matrix_struct_work)
2365 14 : CALL cp_cfm_create(src_cfm, matrix_struct_work)
2366 14 : CALL cp_cfm_create(dst_cfm, matrix_struct_work)
2367 42 : ALLOCATE (eigenvalues_buffer(nmo))
2368 14 : IF (source_window) THEN
2369 : CALL cp_fm_struct_create(matrix_struct_source, nrow_global=nao, ncol_global=nmo_src, &
2370 0 : para_env=para_env, context=kpoint%blacs_env)
2371 0 : CALL cp_cfm_create(src_cfm_full, matrix_struct_source)
2372 : END IF
2373 :
2374 14 : max_subspace_deviation = 0.0_dp
2375 14 : min_svalue = HUGE(1.0_dp)
2376 14 : max_metric_deviation = 0.0_dp
2377 14 : max_residual = 0.0_dp
2378 350 : DO ik = 1, nkp
2379 336 : my_kpgrp = (ik >= kp_range(1) .AND. ik <= kp_range(2))
2380 0 : NULLIFY (source_cfm)
2381 : IF (my_kpgrp) THEN
2382 336 : ikpgr = ik - kp_range(1) + 1
2383 336 : kp => kpoint%kp_env(ikpgr)%kpoint_env
2384 336 : source_cfm => kp%mos(ispin)%cmo_coeff
2385 : END IF
2386 336 : CALL wannier90_copy_cfm_general(source_cfm, ref_cfm, para_env)
2387 :
2388 336 : ikred = source_kpoint(ik)
2389 336 : my_kpgrp = (ikred >= source_kp_range(1) .AND. ikred <= source_kp_range(2))
2390 0 : NULLIFY (source_cfm)
2391 : IF (my_kpgrp) THEN
2392 336 : ikpgr = ikred - source_kp_range(1) + 1
2393 336 : kp_source => qs_kpoint%kp_env(ikpgr)%kpoint_env
2394 336 : source_cfm => kp_source%mos(ispin)%cmo_coeff
2395 336 : CALL get_mo_set(kp_source%mos(ispin), eigenvalues=eigenvalues)
2396 1600 : eigenvalues_buffer(1:nmo) = eigenvalues(1:nmo)
2397 : ELSE
2398 0 : eigenvalues_buffer(1:nmo) = 0.0_dp
2399 : END IF
2400 336 : IF (source_window) THEN
2401 0 : CALL wannier90_copy_cfm_general(source_cfm, src_cfm_full, para_env)
2402 : ELSE
2403 336 : CALL wannier90_copy_cfm_general(source_cfm, src_cfm, para_env)
2404 : END IF
2405 336 : IF (my_kpgrp) THEN
2406 336 : owner_count = 1.0_dp
2407 : ELSE
2408 0 : owner_count = 0.0_dp
2409 : END IF
2410 336 : CALL para_env%sum(owner_count)
2411 336 : CALL para_env%sum(eigenvalues_buffer)
2412 1600 : 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 336 : ref_metric_deviation, ref_metric_min, ref_residual)
2417 336 : 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 0 : "WANNIER90| reference k=", ik, " dM=", ref_metric_deviation, &
2421 0 : " smin=", ref_metric_min, " resid=", ref_residual
2422 : END IF
2423 336 : IF (source_window) THEN
2424 0 : CALL cp_cfm_to_cfm(src_cfm_full, src_cfm, nmo)
2425 : END IF
2426 :
2427 336 : best_deviation = HUGE(1.0_dp)
2428 336 : best_metric_deviation = 0.0_dp
2429 336 : best_residual = 0.0_dp
2430 336 : best_svalue = 0.0_dp
2431 336 : IF (sym_index(ik) <= 0) THEN
2432 : CALL kpoint_transform_scf_mo(src_cfm, dst_cfm, qs_kpoint, &
2433 68 : ikred, sym_index(ik), ok, reason)
2434 68 : 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 68 : para_env, ok, candidate_deviation, candidate_svalue)
2438 68 : 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 68 : candidate_metric_min, candidate_residual)
2444 : END IF
2445 68 : IF (ok) THEN
2446 68 : best_deviation = candidate_deviation
2447 68 : best_metric_deviation = candidate_metric_deviation
2448 68 : best_residual = candidate_residual
2449 68 : best_svalue = candidate_svalue
2450 68 : 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 0 : "WANNIER90| reuse candidate k=", ik, " src=", ikred, " sym=", &
2455 0 : sym_index(ik), " dRef=", candidate_deviation, " dM=", &
2456 0 : candidate_metric_deviation, " smin=", candidate_metric_min, &
2457 0 : " resid=", candidate_residual
2458 : END IF
2459 : END IF
2460 : END IF
2461 268 : ELSE IF (ASSOCIATED(qs_kpoint%kp_sym)) THEN
2462 268 : kpsym => qs_kpoint%kp_sym(ikred)%kpoint_sym
2463 268 : IF (ASSOCIATED(kpsym)) THEN
2464 25036 : DO isym_try = 1, kpsym%nwred
2465 24768 : 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 2128 : qs_kpoint, ikred, isym_try, ok, reason)
2469 2128 : 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 2128 : candidate_svalue)
2474 2128 : 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 2128 : candidate_metric_min, candidate_residual)
2481 : END IF
2482 4524 : IF (ok .AND. candidate_deviation < best_deviation) THEN
2483 300 : best_deviation = candidate_deviation
2484 300 : best_metric_deviation = candidate_metric_deviation
2485 300 : best_residual = candidate_residual
2486 300 : best_svalue = candidate_svalue
2487 : END IF
2488 : END DO
2489 : END IF
2490 : END IF
2491 350 : IF (best_deviation < HUGE(1.0_dp)) THEN
2492 336 : max_subspace_deviation = MAX(max_subspace_deviation, best_deviation)
2493 336 : min_svalue = MIN(min_svalue, best_svalue)
2494 336 : max_metric_deviation = MAX(max_metric_deviation, best_metric_deviation)
2495 336 : max_residual = MAX(max_residual, best_residual)
2496 : END IF
2497 : END DO
2498 14 : CALL para_env%max(max_subspace_deviation)
2499 14 : CALL para_env%min(min_svalue)
2500 14 : CALL para_env%max(max_metric_deviation)
2501 14 : CALL para_env%max(max_residual)
2502 :
2503 14 : IF (source_window) THEN
2504 0 : CALL cp_cfm_release(src_cfm_full)
2505 0 : CALL cp_fm_struct_release(matrix_struct_source)
2506 : END IF
2507 14 : CALL cp_cfm_release(ref_cfm)
2508 14 : CALL cp_cfm_release(src_cfm)
2509 14 : CALL cp_cfm_release(dst_cfm)
2510 14 : CALL cp_fm_struct_release(matrix_struct_work)
2511 14 : DEALLOCATE (eigenvalues_buffer)
2512 14 : DEALLOCATE (source_kpoint, sym_index)
2513 :
2514 56 : 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 2532 : 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 2532 : COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: metric_projected, metric_vectors, &
2543 2532 : overlap, ref_coeff, s_cand_coeff
2544 : INTEGER :: ib, nao, nmo, nmo_candidate
2545 : REAL(KIND=dp) :: singular_value
2546 2532 : 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 2532 : success = .FALSE.
2551 2532 : max_subspace_deviation = HUGE(1.0_dp)
2552 2532 : min_svalue = 0.0_dp
2553 2532 : NULLIFY (matrix_struct_metric)
2554 :
2555 : CALL cp_cfm_get_info(ref_cfm, nrow_global=nao, ncol_global=nmo, &
2556 2532 : matrix_struct=matrix_struct_metric)
2557 2532 : CALL cp_cfm_get_info(cand_cfm, ncol_global=nmo_candidate)
2558 2532 : IF (nmo_candidate /= nmo) RETURN
2559 :
2560 2532 : 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 2532 : cand_cfm, s_cand)
2563 :
2564 15192 : ALLOCATE (ref_coeff(nao, nmo), s_cand_coeff(nao, nmo))
2565 2532 : CALL cp_cfm_get_submatrix(ref_cfm, ref_coeff)
2566 2532 : CALL cp_cfm_get_submatrix(s_cand, s_cand_coeff)
2567 :
2568 : ALLOCATE (overlap(nmo, nmo), &
2569 25320 : metric_projected(nmo, nmo), metric_vectors(nmo, nmo), metric_values(nmo))
2570 5311724 : overlap(:, :) = MATMUL(CONJG(TRANSPOSE(ref_coeff)), s_cand_coeff)
2571 196096 : metric_projected(:, :) = MATMUL(CONJG(TRANSPOSE(overlap)), overlap)
2572 87124 : metric_projected(:, :) = 0.5_dp*(metric_projected + CONJG(TRANSPOSE(metric_projected)))
2573 2532 : CALL diag_complex(metric_projected, metric_vectors, metric_values)
2574 :
2575 2532 : min_svalue = HUGE(1.0_dp)
2576 2532 : max_subspace_deviation = 0.0_dp
2577 11096 : DO ib = 1, nmo
2578 8564 : singular_value = SQRT(MAX(metric_values(ib), 0.0_dp))
2579 8564 : min_svalue = MIN(min_svalue, singular_value)
2580 11096 : max_subspace_deviation = MAX(max_subspace_deviation, ABS(singular_value - 1.0_dp))
2581 : END DO
2582 2532 : CALL para_env%max(max_subspace_deviation)
2583 2532 : CALL para_env%min(min_svalue)
2584 2532 : success = .TRUE.
2585 :
2586 2532 : DEALLOCATE (ref_coeff, s_cand_coeff, overlap, metric_projected, metric_vectors, metric_values)
2587 2532 : CALL cp_cfm_release(s_cand)
2588 :
2589 7596 : 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 2532 : SUBROUTINE measure_wannier90_eigenspace_quality(cand_cfm, matrix_s, matrix_ks, xkp, &
2608 2532 : 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 2532 : COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: h_coeff, metric_vectors, residual_block, &
2625 2532 : s_coeff, s_projected
2626 : INTEGER :: ib, nao, nmo
2627 2532 : 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 2532 : success = .FALSE.
2633 2532 : metric_deviation = HUGE(1.0_dp)
2634 2532 : min_metric_eigenvalue = 0.0_dp
2635 2532 : residual_norm = HUGE(1.0_dp)
2636 2532 : NULLIFY (matrix_struct_metric, matrix_struct_projected)
2637 :
2638 : CALL cp_cfm_get_info(cand_cfm, nrow_global=nao, ncol_global=nmo, &
2639 2532 : matrix_struct=matrix_struct_metric)
2640 2532 : IF (SIZE(eigenvalues) < nmo) RETURN
2641 :
2642 2532 : CALL cp_cfm_create(s_cand, matrix_struct_metric)
2643 2532 : 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 2532 : cand_cfm, s_cand)
2647 : CALL apply_wannier90_kp_matrix(matrix_ks, ispin, xkp, cell_to_index, sab_nl, &
2648 2532 : 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 37980 : metric_values(nmo))
2653 2532 : CALL cp_cfm_get_submatrix(s_cand, s_coeff)
2654 2532 : 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 2532 : context=matrix_struct_metric%context)
2658 2532 : 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 2532 : s_cand, CMPLX(0.0_dp, 0.0_dp, KIND=dp), metric_cfm)
2661 2532 : CALL cp_cfm_get_submatrix(metric_cfm, s_projected)
2662 87124 : s_projected(:, :) = 0.5_dp*(s_projected + CONJG(TRANSPOSE(s_projected)))
2663 2532 : CALL diag_complex(s_projected, metric_vectors, metric_values)
2664 11096 : metric_deviation = MAXVAL(ABS(metric_values - 1.0_dp))
2665 11096 : min_metric_eigenvalue = MINVAL(metric_values)
2666 :
2667 309164 : residual_block(:, :) = h_coeff
2668 11096 : DO ib = 1, nmo
2669 309164 : residual_block(:, ib) = residual_block(:, ib) - eigenvalues(ib)*s_coeff(:, ib)
2670 : END DO
2671 309164 : residual_norm = MAXVAL(ABS(residual_block))
2672 2532 : CALL para_env%max(metric_deviation)
2673 2532 : CALL para_env%min(min_metric_eigenvalue)
2674 2532 : CALL para_env%max(residual_norm)
2675 2532 : success = .TRUE.
2676 :
2677 2532 : DEALLOCATE (h_coeff, metric_vectors, residual_block, s_coeff, s_projected, metric_values)
2678 2532 : CALL cp_cfm_release(s_cand)
2679 2532 : CALL cp_cfm_release(h_cand)
2680 2532 : CALL cp_cfm_release(metric_cfm)
2681 2532 : CALL cp_fm_struct_release(matrix_struct_projected)
2682 :
2683 10128 : 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 28 : 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 28 : IF (.NOT. para_env%is_source()) RETURN
2699 14 : CALL open_file(filename, unit_number=iunit, file_status='REPLACE', file_action='WRITE')
2700 14 : WRITE (iunit, '(A)') '! Initial Wannier90 projections generated by CP2K'
2701 14 : WRITE (iunit, '(3I10)') SIZE(amn, 1), SIZE(amn, 3), SIZE(amn, 2)
2702 238 : DO ik = 1, SIZE(amn, 3)
2703 1046 : DO iwann = 1, SIZE(amn, 2)
2704 4328 : DO ib = 1, SIZE(amn, 1)
2705 3296 : WRITE (iunit, '(3I10,2ES24.16)') ib, iwann, ik, REAL(amn(ib, iwann, ik), dp), &
2706 7400 : AIMAG(amn(ib, iwann, ik))
2707 : END DO
2708 : END DO
2709 : END DO
2710 14 : 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 6 : 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 6 : INTEGER, ALLOCATABLE :: p_shells(:), s_shells(:)
2725 6 : INTEGER, POINTER :: first_sgf(:, :), l(:, :), nshell(:)
2726 : TYPE(gto_basis_set_type), POINTER :: basis
2727 6 : TYPE(particle_type), POINTER :: particles(:)
2728 6 : TYPE(qs_kind_type), POINTER :: qs_kinds(:)
2729 :
2730 6 : CALL get_qs_env(qs_env, particle_set=particles, qs_kind_set=qs_kinds)
2731 6 : ng = 0
2732 18 : DO pass = 1, 2
2733 12 : offset = 0
2734 12 : ig = 0
2735 32 : DO iatom = 1, SIZE(particles)
2736 20 : CALL get_atomic_kind(particles(iatom)%atomic_kind, kind_number=ikind)
2737 20 : CALL get_qs_kind(qs_kinds(ikind), basis_set=basis, nsgf=nsgf)
2738 20 : IF (ASSOCIATED(basis)) THEN
2739 20 : CALL get_gto_basis_set(basis, nset=nset, nshell=nshell, l=l, first_sgf=first_sgf)
2740 200 : ALLOCATE (s_shells(SUM(nshell)), p_shells(SUM(nshell)))
2741 20 : ns = 0
2742 20 : np = 0
2743 60 : DO iset = 1, nset
2744 120 : DO ishell = 1, nshell(iset)
2745 60 : n = offset + first_sgf(ishell, iset)
2746 40 : SELECT CASE (l(ishell, iset))
2747 : CASE (0)
2748 40 : ns = ns + 1
2749 40 : s_shells(ns) = n
2750 : CASE (1)
2751 20 : np = np + 1
2752 60 : p_shells(np) = n
2753 : END SELECT
2754 : END DO
2755 : END DO
2756 20 : IF (pass == 1) THEN
2757 10 : ng = ng + ns*np
2758 : ELSE
2759 30 : DO is = 1, ns
2760 50 : DO ip = 1, np
2761 20 : ig = ig + 1
2762 20 : n = p_shells(ip)
2763 120 : groups(:, ig) = [s_shells(is), n, n + 1, n + 2]
2764 : END DO
2765 : END DO
2766 : END IF
2767 40 : DEALLOCATE (s_shells, p_shells)
2768 : END IF
2769 52 : offset = offset + nsgf
2770 : END DO
2771 18 : IF (pass == 1) THEN
2772 18 : ALLOCATE (groups(4, ng))
2773 : END IF
2774 : END DO
2775 12 : 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 10 : 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 10 : 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 10 : NULLIFY (matrix_struct, cell_to_index, source_cfm)
2809 10 : CALL get_mo_set(kpoint%kp_env(1)%kpoint_env%mos(ispin), nao=nao, nmo=nmo)
2810 10 : nk = SIZE(kpoint%xkp, 2)
2811 10 : 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 10 : para_env=para_env, context=kpoint%blacs_env)
2814 10 : CALL cp_cfm_create(mo_cfm, matrix_struct)
2815 10 : CALL cp_cfm_create(sc_cfm, matrix_struct)
2816 40 : ALLOCATE (buffer(nmo, nao))
2817 10 : IF (para_env%is_source()) THEN
2818 25 : ALLOCATE (projections(SIZE(bands), nao, nk))
2819 : ELSE
2820 5 : ALLOCATE (projections(0, 0, 0))
2821 : END IF
2822 90 : DO ik = 1, nk
2823 80 : IF (ik >= kpoint%kp_range(1) .AND. ik <= kpoint%kp_range(2)) THEN
2824 80 : iklocal = ik - kpoint%kp_range(1) + 1
2825 80 : source_cfm => kpoint%kp_env(iklocal)%kpoint_env%mos(ispin)%cmo_coeff
2826 : ELSE
2827 0 : NULLIFY (source_cfm)
2828 : END IF
2829 80 : 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 80 : mo_cfm, sc_cfm)
2832 80 : CALL cp_cfm_get_submatrix(sc_cfm, buffer, transpose=.TRUE.)
2833 90 : IF (para_env%is_source()) THEN
2834 1008 : projections(:, :, ik) = CONJG(buffer(bands, :))
2835 40 : 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 128 : phase = -twopi*DOT_PRODUCT(kpoint%xkp(:, ik), phase_center)
2838 760 : projections(:, :, ik) = projections(:, :, ik)*CMPLX(COS(phase), SIN(phase), KIND=dp)
2839 : END IF
2840 : END IF
2841 : END DO
2842 10 : DEALLOCATE (buffer)
2843 10 : CALL cp_cfm_release(mo_cfm)
2844 10 : CALL cp_cfm_release(sc_cfm)
2845 10 : CALL cp_fm_struct_release(matrix_struct)
2846 20 : 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 3552 : 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 3552 : REAL(KIND=dp), POINTER :: cb(:, :), ib(:, :), rb(:, :), sb(:, :)
2873 3552 : TYPE(neighbor_list_iterator_p_type), POINTER :: iterator(:)
2874 :
2875 3552 : CALL get_neighbor_list_set_p(sab_nl, symmetric=symmetric)
2876 3552 : CPASSERT(symmetric)
2877 3552 : CALL dbcsr_set(real_matrix, 0.0_dp)
2878 3552 : CALL dbcsr_set(imag_matrix, 0.0_dp)
2879 3552 : CALL neighbor_list_iterator_create(iterator, sab_nl)
2880 3729408 : DO WHILE (neighbor_list_iterate(iterator) == 0)
2881 3725856 : CALL get_iterator_info(iterator, iatom=iatom, jatom=jatom, cell=image_cell)
2882 3725856 : ic = cell_to_index(image_cell(1), image_cell(2), image_cell(3))
2883 3725856 : CPASSERT(ic > 0)
2884 3725856 : irow = MIN(iatom, jatom)
2885 3725856 : icol = MAX(iatom, jatom)
2886 3725856 : CALL dbcsr_get_block_p(cosmat(1, ic)%matrix, irow, icol, cb, found=found)
2887 3725856 : CPASSERT(found)
2888 3725856 : CALL dbcsr_get_block_p(sinmat(1, ic)%matrix, irow, icol, sb, found=found)
2889 3725856 : CPASSERT(found)
2890 11181120 : DO direction = 1, 2
2891 7451712 : IF (direction == 1) THEN
2892 3725856 : irow = iatom
2893 3725856 : icol = jatom
2894 3725856 : transposed = iatom > jatom
2895 14903424 : 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 3725856 : IF (iatom == jatom) CYCLE
2900 2786304 : irow = jatom
2901 2786304 : icol = iatom
2902 2786304 : transposed = iatom < jatom
2903 11145216 : phase = -twopi*DOT_PRODUCT(kleft, REAL(image_cell, KIND=dp))
2904 : END IF
2905 6512160 : CALL dbcsr_get_block_p(real_matrix, irow, icol, rb, found=found)
2906 6512160 : CPASSERT(found)
2907 6512160 : CALL dbcsr_get_block_p(imag_matrix, irow, icol, ib, found=found)
2908 6512160 : CPASSERT(found)
2909 6512160 : cr = COS(phase)
2910 6512160 : ci = SIN(phase)
2911 23262336 : IF (transposed) THEN
2912 205685760 : rb(:, :) = rb(:, :) + cr*TRANSPOSE(cb) - bsign*ci*TRANSPOSE(sb)
2913 205685760 : ib(:, :) = ib(:, :) + ci*TRANSPOSE(cb) + bsign*cr*TRANSPOSE(sb)
2914 : ELSE
2915 290364576 : rb(:, :) = rb(:, :) + cr*cb(:, :) - bsign*ci*sb(:, :)
2916 290364576 : ib(:, :) = ib(:, :) + ci*cb(:, :) + bsign*cr*sb(:, :)
2917 : END IF
2918 : END DO
2919 : END DO
2920 3552 : CALL neighbor_list_iterator_release(iterator)
2921 3552 : 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 34960 : 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 8740 : NULLIFY (matrix_struct_ao, matrix_struct_coeff, kmat_imag, kmat_imag_full, kmat_real, &
2952 8740 : kmat_real_full)
2953 :
2954 : CALL cp_cfm_get_info(coeff, nrow_global=nao, ncol_global=ncol, &
2955 8740 : matrix_struct=matrix_struct_coeff)
2956 :
2957 8740 : ALLOCATE (kmat_real, kmat_imag, kmat_real_full, kmat_imag_full)
2958 : CALL dbcsr_create(kmat_real, template=rsmat(ispin, 1)%matrix, &
2959 8740 : matrix_type=dbcsr_type_symmetric)
2960 : CALL dbcsr_create(kmat_imag, template=rsmat(ispin, 1)%matrix, &
2961 8740 : matrix_type=dbcsr_type_antisymmetric)
2962 : CALL dbcsr_create(kmat_real_full, template=rsmat(ispin, 1)%matrix, &
2963 8740 : matrix_type=dbcsr_type_no_symmetry)
2964 : CALL dbcsr_create(kmat_imag_full, template=rsmat(ispin, 1)%matrix, &
2965 8740 : matrix_type=dbcsr_type_no_symmetry)
2966 8740 : CALL cp_dbcsr_alloc_block_from_nbl(kmat_real, sab_nl)
2967 8740 : CALL cp_dbcsr_alloc_block_from_nbl(kmat_imag, sab_nl)
2968 8740 : CALL dbcsr_set(kmat_real, 0.0_dp)
2969 8740 : CALL dbcsr_set(kmat_imag, 0.0_dp)
2970 : CALL rskp_transform(kmat_real, kmat_imag, rsmat=rsmat, ispin=ispin, &
2971 8740 : xkp=xkp, cell_to_index=cell_to_index, sab_nl=sab_nl)
2972 8740 : CALL dbcsr_desymmetrize(kmat_real, kmat_real_full)
2973 8740 : 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 8740 : context=matrix_struct_coeff%context)
2978 8740 : CALL cp_fm_create(mat_real, matrix_struct_ao)
2979 8740 : CALL cp_fm_create(mat_imag, matrix_struct_ao)
2980 8740 : CALL copy_dbcsr_to_fm(kmat_real_full, mat_real)
2981 8740 : CALL copy_dbcsr_to_fm(kmat_imag_full, mat_imag)
2982 :
2983 8740 : CALL cp_cfm_create(kmat_cfm, matrix_struct_ao)
2984 8740 : 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 8740 : coeff, CMPLX(0.0_dp, 0.0_dp, KIND=dp), RESULT)
2987 :
2988 8740 : CALL cp_fm_release(mat_real)
2989 8740 : CALL cp_fm_release(mat_imag)
2990 8740 : CALL cp_cfm_release(kmat_cfm)
2991 8740 : CALL cp_fm_struct_release(matrix_struct_ao)
2992 8740 : CALL dbcsr_deallocate_matrix(kmat_real)
2993 8740 : CALL dbcsr_deallocate_matrix(kmat_imag)
2994 8740 : CALL dbcsr_deallocate_matrix(kmat_real_full)
2995 8740 : CALL dbcsr_deallocate_matrix(kmat_imag_full)
2996 :
2997 8740 : 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 532 : SUBROUTINE ritz_stabilize_wannier90_subspace(dst_cfm, matrix_s, matrix_ks, &
3018 1064 : 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 532 : COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: block_coeff, dst_coeff, h_block, h_coeff, &
3038 532 : h_projected, h_projected_work, metric_vectors, residual_block, ritz_vectors, s_block, &
3039 532 : 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 532 : 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 532 : success = .FALSE.
3049 532 : reason = ""
3050 532 : aligned_blocks = 0
3051 532 : aligned_max_size = 0
3052 532 : aligned_min_svalue = HUGE(1.0_dp)
3053 532 : max_residual = 0.0_dp
3054 :
3055 532 : NULLIFY (matrix_struct_metric)
3056 : CALL cp_cfm_get_info(dst_cfm, nrow_global=nao, ncol_global=nmo, &
3057 532 : matrix_struct=matrix_struct_metric)
3058 532 : IF (SIZE(eigenvalues) < nmo) THEN
3059 0 : reason = "not enough eigenvalues for Wannier90 Ritz subspace stabilization"
3060 : RETURN
3061 : END IF
3062 :
3063 532 : CALL cp_cfm_create(s_dst, matrix_struct_metric)
3064 532 : 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 532 : dst_cfm, s_dst)
3069 : CALL apply_wannier90_kp_matrix(matrix_ks, ispin, xkp, cell_to_index, sab_nl, &
3070 532 : dst_cfm, h_dst)
3071 :
3072 4256 : ALLOCATE (dst_coeff(nao, nmo), s_coeff(nao, nmo), h_coeff(nao, nmo))
3073 532 : CALL cp_cfm_get_submatrix(dst_cfm, dst_coeff)
3074 532 : CALL cp_cfm_get_submatrix(s_dst, s_coeff)
3075 532 : CALL cp_cfm_get_submatrix(h_dst, h_coeff)
3076 :
3077 532 : block_first = 1
3078 1916 : DO WHILE (block_first <= nmo)
3079 : block_last = block_first
3080 2096 : DO WHILE (block_last < nmo)
3081 1564 : IF (ABS(eigenvalues(block_last + 1) - eigenvalues(block_last)) >= degenerate_band_tol) EXIT
3082 1384 : block_last = block_last + 1
3083 : END DO
3084 1384 : block_size = block_last - block_first + 1
3085 1384 : 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 0 : ALLOCATE (block_coeff(nao, block_size), h_block(nao, block_size), &
3090 0 : h_projected(block_size, block_size), h_projected_work(block_size, block_size), &
3091 0 : metric_vectors(block_size, block_size), residual_block(nao, block_size), &
3092 0 : ritz_vectors(block_size, block_size), s_block(nao, block_size), &
3093 0 : s_projected(block_size, block_size), stabilized(nao, block_size), &
3094 12688 : metric_values(block_size), ritz_values(block_size))
3095 63192 : block_coeff(:, :) = dst_coeff(:, block_first:block_last)
3096 63192 : s_block(:, :) = s_coeff(:, block_first:block_last)
3097 63192 : h_block(:, :) = h_coeff(:, block_first:block_last)
3098 977816 : s_projected(:, :) = MATMUL(CONJG(TRANSPOSE(block_coeff)), s_block)
3099 977816 : h_projected(:, :) = MATMUL(CONJG(TRANSPOSE(block_coeff)), h_block)
3100 9032 : s_projected(:, :) = 0.5_dp*(s_projected + CONJG(TRANSPOSE(s_projected)))
3101 9032 : h_projected(:, :) = 0.5_dp*(h_projected + CONJG(TRANSPOSE(h_projected)))
3102 :
3103 488 : CALL diag_complex(s_projected, metric_vectors, metric_values)
3104 1688 : aligned_min_svalue = MIN(aligned_min_svalue, MINVAL(metric_values))
3105 1688 : metric_deviation = MAXVAL(ABS(metric_values - 1.0_dp))
3106 1688 : IF (MINVAL(metric_values) < 1.0e-10_dp) THEN
3107 : WRITE (reason, "(A,I0,A,ES9.2,A,ES9.2)") &
3108 0 : "singular metric blk=", block_first, " smin=", MINVAL(metric_values), &
3109 0 : " dS=", metric_deviation
3110 0 : max_residual = HUGE(1.0_dp)
3111 0 : EXIT stabilize_subspace
3112 : END IF
3113 :
3114 1688 : DO ib = 1, block_size
3115 4760 : metric_vectors(:, ib) = metric_vectors(:, ib)/SQRT(metric_values(ib))
3116 : END DO
3117 53720 : h_projected_work(:, :) = MATMUL(h_projected, metric_vectors)
3118 53720 : h_projected(:, :) = MATMUL(CONJG(TRANSPOSE(metric_vectors)), h_projected_work)
3119 9032 : h_projected(:, :) = 0.5_dp*(h_projected + CONJG(TRANSPOSE(h_projected)))
3120 488 : CALL diag_complex(h_projected, ritz_vectors, ritz_values)
3121 53720 : h_projected_work(:, :) = MATMUL(metric_vectors, ritz_vectors)
3122 4760 : ritz_vectors(:, :) = h_projected_work
3123 653464 : stabilized(:, :) = MATMUL(block_coeff, ritz_vectors)
3124 653464 : residual_block(:, :) = MATMUL(h_block, ritz_vectors)
3125 63192 : h_block(:, :) = residual_block
3126 653464 : residual_block(:, :) = MATMUL(s_block, ritz_vectors)
3127 63192 : s_block(:, :) = residual_block
3128 1688 : DO ib = 1, block_size
3129 62704 : norm_value = SQRT(ABS(REAL(DOT_PRODUCT(stabilized(:, ib), s_block(:, ib)), KIND=dp)))
3130 1688 : IF (norm_value > EPSILON(1.0_dp)) THEN
3131 62704 : stabilized(:, ib) = stabilized(:, ib)/norm_value
3132 62704 : h_block(:, ib) = h_block(:, ib)/norm_value
3133 62704 : s_block(:, ib) = s_block(:, ib)/norm_value
3134 : END IF
3135 : END DO
3136 63192 : residual_block(:, :) = h_block
3137 1688 : DO ib = 1, block_size
3138 : residual_block(:, ib) = residual_block(:, ib) - &
3139 63192 : eigenvalues(block_first + ib - 1)*s_block(:, ib)
3140 : END DO
3141 63192 : residual_norm = MAXVAL(ABS(residual_block))
3142 488 : max_residual = MAX(max_residual, residual_norm)
3143 488 : IF (residual_norm > residual_tol) THEN
3144 : WRITE (reason, "(A,I0,A,ES9.2)") &
3145 0 : "blk=", block_first, " dS=", metric_deviation
3146 0 : EXIT stabilize_subspace
3147 : END IF
3148 :
3149 63192 : dst_coeff(:, block_first:block_last) = stabilized
3150 63192 : h_coeff(:, block_first:block_last) = h_block
3151 63192 : s_coeff(:, block_first:block_last) = s_block
3152 488 : aligned_blocks = aligned_blocks + 1
3153 488 : aligned_max_size = MAX(aligned_max_size, block_size)
3154 0 : DEALLOCATE (block_coeff, h_block, h_projected, h_projected_work, metric_vectors, &
3155 0 : residual_block, ritz_vectors, s_block, s_projected, stabilized, metric_values, &
3156 488 : ritz_values)
3157 : END IF
3158 1384 : block_first = block_last + 1
3159 : END DO
3160 :
3161 2628 : DO ib = 1, nmo
3162 93744 : residual_norm = MAXVAL(ABS(h_coeff(:, ib) - eigenvalues(ib)*s_coeff(:, ib)))
3163 2628 : max_residual = MAX(max_residual, residual_norm)
3164 : END DO
3165 532 : IF (max_residual > residual_tol) THEN
3166 : WRITE (reason, "(A,ES10.3)") &
3167 0 : "atom/AO W90 reuse guarded: Ritz residual=", max_residual
3168 0 : EXIT stabilize_subspace
3169 : END IF
3170 :
3171 532 : CALL cp_cfm_set_submatrix(dst_cfm, dst_coeff)
3172 :
3173 532 : IF (aligned_blocks == 0) aligned_min_svalue = 0.0_dp
3174 532 : success = .TRUE.
3175 : END BLOCK stabilize_subspace
3176 :
3177 532 : CALL cp_cfm_release(s_dst)
3178 532 : CALL cp_cfm_release(h_dst)
3179 :
3180 1064 : 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 0 : SUBROUTINE ritz_reconstruct_wannier90_window(src_cfm, dst_cfm, matrix_s, &
3200 : matrix_ks, xkp, cell_to_index, sab_nl, ispin, &
3201 0 : 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 0 : COMPLEX(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: coeff_work, h_coeff, h_projected, &
3220 0 : h_projected_work, metric_vectors, residual_block, ritz_vectors, s_coeff, s_projected, &
3221 0 : source_coeff, stabilized
3222 : INTEGER :: ib, nao, nmo_source
3223 : REAL(KIND=dp) :: max_eigenvalue_shift, metric_deviation, &
3224 : norm_value
3225 0 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: metric_values, ritz_values, &
3226 0 : source_eigenvalues
3227 : TYPE(cp_cfm_type) :: h_src, s_src
3228 : TYPE(cp_fm_struct_type), POINTER :: matrix_struct_metric
3229 :
3230 0 : success = .FALSE.
3231 0 : reason = ""
3232 0 : min_svalue = HUGE(1.0_dp)
3233 0 : max_residual = HUGE(1.0_dp)
3234 :
3235 0 : NULLIFY (matrix_struct_metric)
3236 : CALL cp_cfm_get_info(src_cfm, nrow_global=nao, ncol_global=nmo_source, &
3237 0 : matrix_struct=matrix_struct_metric)
3238 0 : IF (nmo_export > nmo_source) THEN
3239 0 : reason = "Wannier90 export window is larger than the transformed SCF MO space"
3240 0 : RETURN
3241 : END IF
3242 0 : IF (SIZE(eigenvalues) < nmo_export) THEN
3243 0 : reason = "not enough eigenvalue storage for Wannier90 source-window reconstruction"
3244 : RETURN
3245 : END IF
3246 :
3247 0 : CALL cp_cfm_create(s_src, matrix_struct_metric)
3248 0 : 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 0 : src_cfm, s_src)
3252 : CALL apply_wannier90_kp_matrix(matrix_ks, ispin, xkp, cell_to_index, sab_nl, &
3253 0 : src_cfm, h_src)
3254 :
3255 0 : ALLOCATE (source_coeff(nao, nmo_source), s_coeff(nao, nmo_source), &
3256 0 : h_coeff(nao, nmo_source), h_projected(nmo_source, nmo_source), &
3257 0 : h_projected_work(nmo_source, nmo_source), metric_vectors(nmo_source, nmo_source), &
3258 0 : residual_block(nao, nmo_export), ritz_vectors(nmo_source, nmo_source), &
3259 0 : s_projected(nmo_source, nmo_source), stabilized(nao, nmo_export), &
3260 0 : coeff_work(nao, nmo_source), metric_values(nmo_source), ritz_values(nmo_source), &
3261 0 : source_eigenvalues(nmo_export))
3262 0 : source_eigenvalues(1:nmo_export) = eigenvalues(1:nmo_export)
3263 0 : CALL cp_cfm_get_submatrix(src_cfm, source_coeff)
3264 0 : CALL cp_cfm_get_submatrix(s_src, s_coeff)
3265 0 : CALL cp_cfm_get_submatrix(h_src, h_coeff)
3266 0 : s_projected(:, :) = MATMUL(CONJG(TRANSPOSE(source_coeff)), s_coeff)
3267 0 : h_projected(:, :) = MATMUL(CONJG(TRANSPOSE(source_coeff)), h_coeff)
3268 0 : s_projected(:, :) = 0.5_dp*(s_projected + CONJG(TRANSPOSE(s_projected)))
3269 0 : h_projected(:, :) = 0.5_dp*(h_projected + CONJG(TRANSPOSE(h_projected)))
3270 :
3271 : reconstruct_window: BLOCK
3272 0 : CALL diag_complex(s_projected, metric_vectors, metric_values)
3273 0 : min_svalue = MINVAL(metric_values)
3274 0 : metric_deviation = MAXVAL(ABS(metric_values - 1.0_dp))
3275 0 : IF (min_svalue < 1.0e-10_dp) THEN
3276 : WRITE (reason, "(A,ES9.2,A,ES9.2)") &
3277 0 : "singular expanded metric smin=", min_svalue, " dS=", metric_deviation
3278 0 : EXIT reconstruct_window
3279 : END IF
3280 :
3281 0 : DO ib = 1, nmo_source
3282 0 : metric_vectors(:, ib) = metric_vectors(:, ib)/SQRT(metric_values(ib))
3283 : END DO
3284 0 : h_projected_work(:, :) = MATMUL(h_projected, metric_vectors)
3285 0 : h_projected(:, :) = MATMUL(CONJG(TRANSPOSE(metric_vectors)), h_projected_work)
3286 0 : h_projected(:, :) = 0.5_dp*(h_projected + CONJG(TRANSPOSE(h_projected)))
3287 0 : CALL diag_complex(h_projected, ritz_vectors, ritz_values)
3288 0 : h_projected_work(:, :) = MATMUL(metric_vectors, ritz_vectors)
3289 0 : ritz_vectors(:, :) = h_projected_work
3290 0 : stabilized(:, :) = MATMUL(source_coeff, ritz_vectors(:, 1:nmo_export))
3291 0 : coeff_work(:, :) = MATMUL(h_coeff, ritz_vectors)
3292 0 : h_coeff(:, :) = coeff_work
3293 0 : coeff_work(:, :) = MATMUL(s_coeff, ritz_vectors)
3294 0 : s_coeff(:, :) = coeff_work
3295 0 : DO ib = 1, nmo_export
3296 0 : norm_value = SQRT(ABS(REAL(DOT_PRODUCT(stabilized(:, ib), s_coeff(:, ib)), KIND=dp)))
3297 0 : IF (norm_value > EPSILON(1.0_dp)) THEN
3298 0 : stabilized(:, ib) = stabilized(:, ib)/norm_value
3299 0 : h_coeff(:, ib) = h_coeff(:, ib)/norm_value
3300 0 : s_coeff(:, ib) = s_coeff(:, ib)/norm_value
3301 : END IF
3302 : END DO
3303 0 : residual_block(:, :) = h_coeff(:, 1:nmo_export)
3304 0 : DO ib = 1, nmo_export
3305 0 : residual_block(:, ib) = residual_block(:, ib) - ritz_values(ib)*s_coeff(:, ib)
3306 : END DO
3307 0 : max_residual = MAXVAL(ABS(residual_block))
3308 0 : IF (max_residual > residual_tol) THEN
3309 : WRITE (reason, "(A,ES9.2)") &
3310 0 : "expanded dS=", metric_deviation
3311 0 : EXIT reconstruct_window
3312 : END IF
3313 0 : max_eigenvalue_shift = MAXVAL(ABS(ritz_values(1:nmo_export) - source_eigenvalues(1:nmo_export)))
3314 0 : IF (max_eigenvalue_shift > eigenvalue_tol) THEN
3315 : WRITE (reason, "(A,ES9.2)") &
3316 0 : "expanded dS=", metric_deviation
3317 0 : EXIT reconstruct_window
3318 : END IF
3319 :
3320 0 : CALL cp_cfm_set_submatrix(dst_cfm, stabilized)
3321 0 : success = .TRUE.
3322 :
3323 : END BLOCK reconstruct_window
3324 :
3325 0 : DEALLOCATE (source_coeff, s_coeff, h_coeff, h_projected, h_projected_work, metric_vectors, &
3326 0 : residual_block, ritz_vectors, s_projected, stabilized, coeff_work, metric_values, &
3327 0 : ritz_values, source_eigenvalues)
3328 0 : CALL cp_cfm_release(s_src)
3329 0 : CALL cp_cfm_release(h_src)
3330 :
3331 0 : 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 60 : 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 60 : success = .FALSE.
3353 60 : reason = ""
3354 60 : nfull = kpoint%nkp
3355 240 : ALLOCATE (source_kpoint(nfull), sym_index(nfull))
3356 60 : source_kpoint(:) = 0
3357 60 : sym_index(:) = 0
3358 :
3359 226 : DO ikred = 1, qs_kpoint%nkp
3360 166 : imatch = find_matching_kpoint(kpoint%xkp, qs_kpoint%xkp(1:3, ikred))
3361 226 : IF (imatch > 0 .AND. source_kpoint(imatch) == 0) THEN
3362 166 : source_kpoint(imatch) = ikred
3363 166 : sym_index(imatch) = 0
3364 : END IF
3365 : END DO
3366 :
3367 : ! Prefer pure time-reversal partners before general atom/AO symmetry operations.
3368 226 : DO ikred = 1, qs_kpoint%nkp
3369 664 : imatch = find_matching_kpoint(kpoint%xkp, -qs_kpoint%xkp(1:3, ikred))
3370 226 : IF (imatch > 0 .AND. source_kpoint(imatch) == 0) THEN
3371 70 : source_kpoint(imatch) = ikred
3372 70 : sym_index(imatch) = -1
3373 : END IF
3374 : END DO
3375 :
3376 60 : IF (ASSOCIATED(qs_kpoint%kp_sym)) THEN
3377 226 : DO ikred = 1, qs_kpoint%nkp
3378 166 : kpsym => qs_kpoint%kp_sym(ikred)%kpoint_sym
3379 166 : IF (.NOT. ASSOCIATED(kpsym)) CYCLE
3380 166 : IF (.NOT. kpsym%apply_symmetry) CYCLE
3381 8058 : DO isym = 1, kpsym%nwred
3382 7904 : imatch = find_matching_kpoint(kpoint%xkp, kpsym%xkp(1:3, isym))
3383 8070 : IF (imatch > 0 .AND. source_kpoint(imatch) == 0) THEN
3384 680 : source_kpoint(imatch) = ikred
3385 680 : sym_index(imatch) = isym
3386 : END IF
3387 : END DO
3388 : END DO
3389 : END IF
3390 :
3391 976 : DO ik = 1, nfull
3392 976 : IF (source_kpoint(ik) == 0) THEN
3393 0 : reason = "not all full-mesh k-points are represented by the SCF symmetry orbits"
3394 0 : RETURN
3395 : END IF
3396 : END DO
3397 60 : success = .TRUE.
3398 :
3399 60 : 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 8236 : 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 8236 : ik_match = 0
3414 125162 : DO ik = 1, SIZE(xkp_mesh, 2)
3415 125162 : IF (kpoint_same_periodic(xkp_mesh(1:3, ik), xkp_search)) THEN
3416 8236 : ik_match = ik
3417 8236 : 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 6 : 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 6 : LOGICAL, ALLOCATABLE, DIMENSION(:) :: seen
3438 : REAL(KIND=dp) :: coord
3439 6 : REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: unique_coord
3440 :
3441 6 : num_kpts = SIZE(kpt_latt, 2)
3442 6 : mp_grid(:) = 0
3443 18 : ALLOCATE (unique_coord(3, num_kpts))
3444 24 : DO idim = 1, 3
3445 : n_unique = 0
3446 162 : DO i = 1, num_kpts
3447 144 : coord = kpt_latt(idim, i) - FLOOR(kpt_latt(idim, i))
3448 144 : IF (ABS(coord - 1.0_dp) < 1.0e-8_dp) coord = 0.0_dp
3449 144 : known = .FALSE.
3450 216 : DO unique_id = 1, n_unique
3451 216 : 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 162 : IF (.NOT. known) THEN
3457 36 : n_unique = n_unique + 1
3458 36 : unique_coord(idim, n_unique) = coord
3459 : END IF
3460 : END DO
3461 24 : mp_grid(idim) = n_unique
3462 : END DO
3463 6 : valid = (mp_grid(1)*mp_grid(2)*mp_grid(3) == num_kpts)
3464 6 : IF (valid) THEN
3465 18 : ALLOCATE (seen(num_kpts))
3466 6 : seen(:) = .FALSE.
3467 54 : DO i = 1, num_kpts
3468 : idx = 1
3469 : stride = 1
3470 192 : DO idim = 1, 3
3471 144 : coord = kpt_latt(idim, i) - FLOOR(kpt_latt(idim, i))
3472 144 : IF (ABS(coord - 1.0_dp) < 1.0e-8_dp) coord = 0.0_dp
3473 144 : coord_id = 0
3474 216 : DO unique_id = 1, mp_grid(idim)
3475 216 : 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 144 : CPASSERT(coord_id > 0)
3481 144 : idx = idx + (coord_id - 1)*stride
3482 192 : stride = stride*mp_grid(idim)
3483 : END DO
3484 48 : IF (seen(idx)) valid = .FALSE.
3485 54 : seen(idx) = .TRUE.
3486 : END DO
3487 54 : valid = valid .AND. ALL(seen)
3488 6 : DEALLOCATE (seen)
3489 : END IF
3490 6 : DEALLOCATE (unique_coord)
3491 :
3492 6 : END SUBROUTINE infer_wannier_mp_grid
3493 :
3494 272 : END MODULE qs_wannier90
|