Octopus
wannier90_interface.F90
Go to the documentation of this file.
1!! Copyright (C) 2017-2019 H. Huebener, N. Tancogne-Dejean
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17!!
18
19#include "global.h"
20
22 use batch_oct_m
24 use comm_oct_m
26 use cube_oct_m
28 use debug_oct_m
31 use fft_oct_m
32 use global_oct_m
33 use grid_oct_m
34 use io_oct_m
37 use ions_oct_m
38 use, intrinsic :: iso_fortran_env
43 use loct_oct_m
46 use mesh_oct_m
49 use mpi_oct_m
54 use parser_oct_m
57 use space_oct_m
58 use string_oct_m
64 use types_oct_m
65 use unit_oct_m
67 use utils_oct_m
70
71 implicit none
72
73 integer :: w90_what, w90_mode
74 integer(int64) :: w90_what_default
75
76 integer :: ierr
77 integer :: dim, idim
78 integer :: ii, nik, iter, nst
79
80 type(restart_t) :: restart
81 type(electrons_t), pointer :: sys
82 logical :: w90_spinors, scdm_proj, w90_scdm
83 integer :: w90_nntot, w90_num_bands, w90_num_kpts ! w90 input parameters
84 integer, allocatable :: w90_nnk_list(:,:) !
85 character(len=80) :: w90_prefix ! w90 input file prefix
86 integer :: w90_num_wann ! input paramter
87 real(real64), allocatable :: w90_proj_centers(:,:) ! projections centers
88 integer, allocatable :: w90_proj_lmr(:,:) ! definitions for real valued Y_lm*R_r
89 integer :: w90_nproj ! number of such projections
90 integer, allocatable :: w90_spin_proj_component(:) ! up/down flag
91 real(real64), allocatable :: w90_spin_proj_axis(:,:) ! spin axis (not implemented)
92 integer :: w90_num_exclude
93 logical, allocatable :: exclude_list(:) ! list of excluded bands
94 integer, allocatable :: band_index(:) ! band index after exclusion
95 integer, allocatable :: active_bands(:) ! band indices of non-excluded bands
96 logical :: read_td_states
97 integer :: w90_spin_channel
98 integer :: w90_scdm_entanglement
99 logical :: w90_bloch_sums = .false.
100 logical :: w90_have_dis_win_min = .false.
101 logical :: w90_have_dis_win_max = .false.
102 real(real64) :: w90_dis_win_min = m_zero
103 real(real64) :: w90_dis_win_max = m_zero
104
105 ! scdm variables
106 integer, allocatable :: jpvt(:)
107 complex(real64), allocatable :: uk(:,:,:) ! SCDM-Wannier gauge matrices U(k)
108 complex(real64), allocatable :: chi(:,:), chi_diag(:,:),chi2(:,:)
109 real(real64), allocatable :: x_global(:,:)
110 real(real64), allocatable :: chi_eigenval(:), occ_temp(:), occ_smeared(:,:)
111 real(real64) :: scdm_mu, scdm_sigma, kvec(3), factor(3)
112 integer :: jst, idir, sender, ikpoint
113
114 integer(int64) :: how
115
117 call parser_init()
118
119 call messages_init()
120 call io_init()
121
123
126
127 call calc_mode_par%set_parallelization(p_strategy_states, default = .false.)
128 sys => electrons_t(global_namespace, mpi_world, int(option__calculationmode__dummy, int32))
129
130 !%Variable Wannier90Prefix
131 !%Type string
132 !%Default w90
133 !%Section Utilities::oct-wannier90
134 !%Description
135 !% Prefix for wannier90 files
136 !%End
137 call parse_variable(global_namespace, 'Wannier90Prefix', 'w90', w90_prefix)
138 if (w90_prefix == 'w90') then
139 message(1) = "oct-wannier90: the prefix is set by default to w90"
140 call messages_info(1)
141 end if
142
143 !%Variable Wannier90Mode
144 !%Type integer
145 !%Default 0
146 !%Section Utilities::oct-wannier90
147 !%Description
148 !% Specifies which stage of the Wannier90 interface to use
149 !%Option none 0
150 !% Nothing is done.
151 !%Option w90_setup 1
152 !% Writes parts of the wannier90 input file <tt>w90_prefix.win</tt> corresponding to
153 !% the octopus inp file. Importantly it generates the correct form of Monkhorst-Pack mesh
154 !% written to the file w90_kpoints that has to be used in a gs calculation of Octopus by
155 !% as <tt> include w90_kpoints </tt> instead of the <tt>%KpointsGrid</tt> block.
156 !%Option w90_output 2
157 !% Generates the relevant files for a wannier90 run, specified by the variable <tt>W90_interface_files</tt>.
158 !% This needs files previously generated
159 !% by <tt>wannier90.x -pp w90 </tt>
160 !%Option w90_wannier 3
161 !% Parse the output of wannier90 to generate the Wannier states on the real-space grid.
162 !% The states will be written in the folder wannier. By default, the states are written as
163 !% binary files, similar to the Kohn-Sham states.
164 !%
165 !% Not implemented for spinor states.
166 !%End
167 call parse_variable(global_namespace, 'Wannier90Mode', 0, w90_mode)
168
169 if (w90_mode == 0) then
170 message(1) = "Wannier90Mode must be set to a value different from 0."
171 call messages_fatal(1)
172 end if
173
174 !%Variable Wannier90Files
175 !%Type flag
176 !%Default w90_mmn + w90_amn + w90_eig
177 !%Section Utilities::oct-wannier90
178 !%Description
179 !% Specifies which files to generate.
180 !% Example: <tt>w90_mmn + w90_unk</tt>
181 !%Option w90_mmn bit(1)
182 !% (see Wannier90 documentation)
183 !%Option w90_unk bit(2)
184 !% (see Wannier90 documentation)
185 !%Option w90_amn bit(3)
186 !% (see Wannier90 documentation)
187 !%Option w90_eig bit(4)
188 !% Eigenvalues. See Wannier90 documentation for more details.
189 !%Option w90_spn bit(5)
190 !% Spin. See Wannier90 documentation for more details.
191 !%End
192 w90_what_default = option__wannier90files__w90_mmn + option__wannier90files__w90_amn + option__wannier90files__w90_eig
193 if (sys%st%d%ispin == spinors) w90_what_default = w90_what_default + option__wannier90files__w90_spn
194 call parse_variable(global_namespace, 'Wannier90Files', w90_what_default, w90_what)
195
196 !%Variable Wannier90UseTD
197 !%Type logical
198 !%Default no
199 !%Section Utilities::oct-wannier90
200 !%Description
201 !% By default oct-wannier90 uses the ground-state states to compute the necessary information.
202 !% By setting this variable to yes, oct-wannier90 will use the TD states instead.
203 !%End
204 call parse_variable(global_namespace, 'Wannier90UseTD', .false., read_td_states)
205
206 !%Variable Wannier90UseSCDM
207 !%Type logical
208 !%Default no
209 !%Section Utilities::oct-wannier90
210 !%Description
211 !% By default oct-wannier90 uses the projection method to generate the .amn file.
212 !% By setting this variable to yes, oct-wannier90 will use SCDM method instead.
213 !%End
214 call parse_variable(global_namespace, 'Wannier90UseSCDM', .false., w90_scdm)
215 if (w90_scdm) then
216 !%Variable SCDMsigma
217 !%Type float
218 !%Default 0.2
219 !%Section Utilities::oct-wannier90
220 !%Description
221 !% Broadening of SCDM smearing function.
222 !%End
223 call parse_variable(global_namespace, 'SCDMsigma', 0.2_real64, scdm_sigma)
224
225 !%Variable SCDMmu
226 !%Type float
227 !%Section Utilities::oct-wannier90
228 !%Description
229 !% Energy range up to which states are considered for SCDM.
230 !%End
231 call parse_variable(global_namespace, 'SCDMmu', m_huge, scdm_mu)
232
233 !%Variable SCDMEntanglement
234 !%Type integer
235 !%Default 0
236 !%Section Utilities::oct-wannier90
237 !%Description
238 !% Definition of SCDM smearing function.
239 !% See original paper here: https://arxiv.org/pdf/1703.06958.
240 !%Option scdm_isolated 0
241 !%Option scdm_erfc 1
242 !%Option scdm_gaussian 2
243 !%End
244 call parse_variable(global_namespace, 'SCDMEntanglement', 0, w90_scdm_entanglement)
245 end if
246
247 if (sys%st%parallel_in_states) then
248 call messages_not_implemented("oct-wannier90 with parallelization in states")
249 end if
250 if (sys%kpoints%use_symmetries) then
251 message(1) = 'oct-wannier90: k-points symmetries are not allowed'
252 call messages_fatal(1)
253 end if
254 if (sys%kpoints%use_time_reversal) then
255 message(1) = 'oct-wannier90: time-reversal symmetry is not allowed'
256 call messages_fatal(1)
257 end if
258 if (sys%kpoints%reduced%nshifts > 1) then
259 message(1) = 'oct-wannier90: Wannier90 does not allow for multiple shifts of the k-point grid'
260 call messages_fatal(1)
261 end if
262
263 if (sys%st%d%ispin /= unpolarized) then
264 call messages_experimental("oct-wannier90 with SpinComponnents /= unpolarized")
265 end if
266
267 w90_spinors = .false.
268
269 !%Variable Wannier90SpinChannel
270 !%Type integer
271 !%Section Utilities::oct-wannier90
272 !%Description
273 !% Spin channel used for the Wannierization
274 !%End
275 call parse_variable(global_namespace, 'Wannier90SpinChannel', 1, w90_spin_channel)
276
277 ! "Correct" rlattice/klattice for Wannier90 (3D periodicity assumed here).
278 factor = m_one
279 do idir = sys%space%periodic_dim+1, sys%space%dim
280 factor(idir) = m_two * sys%gr%box%bounding_box_l(idir)
281 end do
282 call sys%ions%latt%scale(factor)
283
284 ! create setup files
285 select case (w90_mode)
286 case (option__wannier90mode__w90_setup)
287 call wannier90_setup(sys%ions, sys%kpoints, sys%space)
288
289 ! load states and calculate interface files
290 case (option__wannier90mode__w90_output)
291 call wannier90_output()
292
293 case (option__wannier90mode__w90_wannier)
294 !%Variable Wannier90ComputeBlochSums
295 !%Type logical
296 !%Default no
297 !%Section Utilities::oct-wannier90
298 !%Description
299 !% Only for Wannier90Mode = w90_wannier
300 !% By setting this variable to yes, oct-wannier90 will also output the Bloch sums
301 !% of the Wannier orbitals. This creates one file per k-point.
302 !%End
303 call parse_variable(global_namespace, 'Wannier90ComputeBlochSums', .false., w90_bloch_sums)
304
306
307 ! normal interface run
308 call states_elec_allocate_wfns(sys%st, sys%gr, wfs_type = type_cmplx, skip=exclude_list)
309 if (read_td_states) then
310 call restart%init(global_namespace, restart_td, restart_type_load, &
311 sys%mc, ierr, sys%gr)
312 else
313 call restart%init(global_namespace, restart_gs, restart_type_load, &
314 sys%mc, ierr, sys%gr)
315 end if
316
317 if (ierr == 0) then
318 call states_elec_look(restart, nik, dim, nst, ierr)
319 if (sys%st%d%ispin == spin_polarized) then
320 nik = nik / 2
321 end if
322 if (dim == sys%st%d%dim .and. nik == sys%kpoints%reduced%npoints .and. nst >= sys%st%nst) then
323 call states_elec_load(restart, global_namespace, sys%space, sys%st, sys%gr, sys%kpoints, &
324 fixed_occ=.true., ierr=ierr, iter=iter, label = ": wannier90", skip=exclude_list)
325 else
326 write(message(1),'(a)') 'Restart structure not commensurate.'
327 call messages_fatal(1)
328 end if
329 end if
330 call restart%end()
331
332 call generate_wannier_states(sys%space, sys%gr, sys%ions, sys%st, sys%kpoints)
333 case default
334 message(1) = "Wannier90Mode is set to an unsupported value."
335 call messages_fatal(1)
336 end select
337
338 safe_deallocate_a(exclude_list)
339 safe_deallocate_a(band_index)
340 safe_deallocate_a(active_bands)
341 safe_deallocate_a(w90_nnk_list)
342 safe_deallocate_a(w90_proj_centers)
343 safe_deallocate_a(w90_proj_lmr)
344
345 safe_deallocate_p(sys)
346 call fft_all_end()
347 call io_end()
349 call messages_end()
350 call parser_end()
351 call global_end()
352
353contains
354
355 ! --------------------------------------------------------------------------
356 subroutine wannier90_setup(ions, kpoints, space)
357 type(ions_t), intent(in) :: ions
358 type(kpoints_t), intent(in) :: kpoints
359 class(space_t), intent(in) :: space
360
361 character(len=80) :: filename
362 integer :: w90_win, ia, axis(3), npath
363
364 push_sub(wannier90_setup)
365
366 assert(space%dim == 3)
367
368 ! open win file
369 filename = trim(adjustl(w90_prefix)) //'.win'
370 w90_win = io_open(trim(filename), global_namespace, action='write')
371
372 write(w90_win,'(a)') '# this file has been created by the Octopus wannier90 utility'
373 write(w90_win,'(a)') ' '
374
375 ! write direct lattice vectors (in angstrom)
376 write(w90_win,'(a)') 'begin unit_cell_cart'
377 write(w90_win,'(a)') 'Ang'
378 do idim = 1,3
379 write(w90_win,'(f13.8,f13.8,f13.8)') units_from_atomic(unit_angstrom, ions%latt%rlattice(1:3,idim))
380 end do
381 write(w90_win,'(a)') 'end unit_cell_cart'
382 write(w90_win,'(a)') ' '
383
384 write(w90_win,'(a)') 'begin atoms_frac'
385 do ia = 1, ions%natoms
386 write(w90_win,'(a,2x,f13.8,f13.8,f13.8)') trim(ions%atom(ia)%label), ions%latt%cart_to_red(ions%pos(:, ia))
387 end do
388 write(w90_win,'(a)') 'end atoms_frac'
389 write(w90_win,'(a)') ' '
390
391 ! This is a default value. In practice, one should use projections
392 write(w90_win,'(a)') 'use_bloch_phases = .true.'
393 write(w90_win,'(a)') ' '
394
395 write(w90_win,'(a10,i4)') 'num_bands ', sys%st%nst
396 write(w90_win,'(a9,i4)') 'num_wann ', sys%st%nst
397 write(w90_win,'(a)') ' '
398
399 if (sys%st%d%ispin == spinors) then
400 write(w90_win,'(a)') 'spinors = .true.'
401 end if
402 if (sys%st%d%ispin == spin_polarized) then
403 if (w90_spin_channel == 1) then
404 write(w90_win, '(a)') 'spin = up'
405 else if (w90_spin_channel == 2) then
406 write(w90_win, '(a)') 'spin = down'
407 else
408 message(1) = 'Wannier90SpinChannel value is invalid.'
409 call messages_fatal(1)
410 end if
411 end if
412
413 ! This is for convenience. This is needed for plotting the Wannier states, if requested.
414 write(w90_win,'(a)') 'write_u_matrices = .true.'
415 write(w90_win,'(a)') 'write_xyz = .true.'
416 write(w90_win,'(a)') ' '
417
418 if (kpoints%reduced%npoints == 1) then
419 write(w90_win,'(a)') 'gamma_only = .true.'
420 write(w90_win,'(a)') ' '
421 else
422 if (.not. parse_is_defined(global_namespace, 'KPointsGrid')) then
423 message(1) = 'oct-wannier90: need Monkhorst-Pack grid. Please specify %KPointsGrid'
424 call messages_fatal(1)
425 end if
426
427 ! In case the user used also a k-point path, we ignore it
428 npath = kpoints%nkpt_in_path()
429
430 axis(1:3) = kpoints%nik_axis(1:3)
431 assert(product(kpoints%nik_axis(1:3)) == kpoints%reduced%npoints - npath)
432
433 write(w90_win,'(a8,i4,i4,i4)') 'mp_grid =', axis(1:3)
434 write(w90_win,'(a)') ' '
435 write(w90_win,'(a)') 'begin kpoints '
436 ! Put a minus sign here for the wrong convention in Octopus
437
438 do ii = 1, kpoints%reduced%npoints-npath
439 write(w90_win,'(f13.8,f13.8,f13.8)') - kpoints%reduced%red_point(1:3,ii)
440 end do
441 write(w90_win,'(a)') 'end kpoints '
442 end if
443
444 call io_close(w90_win)
445
446 pop_sub(wannier90_setup)
447
448 end subroutine wannier90_setup
449
450 ! --------------------------------------------------------------------------
451 subroutine wannier90_output()
452
453 push_sub(wannier90_output)
454
456
457 ! normal interface run
458 call states_elec_allocate_wfns(sys%st, sys%gr, wfs_type = type_cmplx, skip=exclude_list)
459 if (read_td_states) then
460 call restart%init(global_namespace, restart_td, restart_type_load, &
461 sys%mc, ierr, sys%gr)
462 else
463 call restart%init(global_namespace, restart_gs, restart_type_load, &
464 sys%mc, ierr, sys%gr)
465 end if
466
467 if (ierr == 0) then
468 call states_elec_look(restart, nik, dim, nst, ierr)
469 if (sys%st%d%ispin == spin_polarized) then
470 nik = nik / 2
471 end if
472 if (dim == sys%st%d%dim .and. nik == sys%kpoints%reduced%npoints .and. nst >= sys%st%nst) then
473 call states_elec_load(restart, global_namespace, sys%space, sys%st, sys%gr, sys%kpoints, &
474 fixed_occ=.true., ierr=ierr, iter=iter, label = ": wannier90", skip=exclude_list)
475 else
476 write(message(1),'(a)') 'Restart structure not commensurate.'
477 call messages_fatal(1)
478 end if
479 end if
480 call restart%end()
481
482 ! ---- actual interface work ----------
483 if (bitand(w90_what, option__wannier90files__w90_mmn) /= 0) then
484 call create_wannier90_mmn(sys%gr, sys%st)
485 end if
486
487 if (bitand(w90_what, option__wannier90files__w90_unk) /= 0) then
488 call write_unk(sys%space, sys%gr, sys%st, formatted=.false.)
489 end if
490
491 if (bitand(w90_what, option__wannier90files__w90_amn) /= 0) then
492 call create_wannier90_amn(sys%space, sys%gr, sys%ions%latt, sys%st, sys%kpoints)
493 end if
494
495 if (bitand(w90_what, option__wannier90files__w90_eig) /= 0) then
497 end if
498
499 if (bitand(w90_what, option__wannier90files__w90_spn) /= 0) then
500 call create_wannier90_spn(sys%gr, sys%st)
501 end if
502
503 safe_deallocate_a(w90_spin_proj_component)
504 safe_deallocate_a(w90_spin_proj_axis)
505
506 pop_sub(wannier90_output)
507 end subroutine wannier90_output
508
509 ! --------------------------------------------------------------------------
510 subroutine read_wannier90_files()
511 integer :: w90_nnkp, itemp, dummyint, io, spin_channel_win
512 character(len=80) :: filename, dummy, dummy1, dummy2, line
513 logical :: exist, parse_is_ok
514 real(real64) :: dis_win_min_ev, dis_win_max_ev
515 real(real64) :: dummyr(7)
516
517 push_sub(read_wannier90_files)
518
519 w90_num_kpts = product(sys%kpoints%nik_axis(1:3))
520 assert(w90_num_kpts == sys%st%nik)
521
522
523 w90_num_exclude = 0
524
525 ! open nnkp file
526 filename = trim(adjustl(w90_prefix)) //'.nnkp'
527
528 message(1) = "oct-wannier90: Parsing "//filename
529 call messages_info(1)
530
531 inquire(file=filename,exist=exist)
532 if (.not. exist) then
533 message(1) = 'oct-wannier90: Cannot find specified Wannier90 nnkp file.'
534 write(message(2),'(a)') 'Please run wannier90.x -pp '// trim(adjustl(w90_prefix)) // ' first.'
535 call messages_fatal(2)
536 end if
537
538 parse_is_ok = .false.
539
540 ! check number of k-points
541 w90_nnkp = io_open(trim(filename), global_namespace, action='read')
542 do
543 read(w90_nnkp, *, iostat=io) dummy, dummy1
544 if (io == iostat_end) exit
545
546 if (dummy == 'begin' .and. dummy1 == 'kpoints') then
547 read(w90_nnkp,*) itemp
548 if (itemp /= w90_num_kpts) then
549 message(1) = 'oct-wannier90: wannier90 setup seems to have been done with a different number of k-points.'
550 call messages_fatal(1)
551 else
552 parse_is_ok = .true.
553 exit
554 end if
555 end if
556 end do
557 call io_close(w90_nnkp)
558
559 if (.not. parse_is_ok) then
560 message(1) = 'oct-wannier90: Did not find the kpoints block in nnkp file'
561 call messages_fatal(1)
562 end if
563 parse_is_ok = .false.
564
565 ! read from nnkp file
566 ! find the nnkpts block
567 w90_nnkp = io_open(trim(filename), global_namespace, action='read', position='rewind')
568 do
569 read(w90_nnkp, *, iostat=io) dummy, dummy1
570 if (io == iostat_end) exit !End of file
571
572 if (dummy == 'begin' .and. dummy1 == 'nnkpts') then
573 read(w90_nnkp,*) w90_nntot
574 safe_allocate(w90_nnk_list(1:5, 1:w90_num_kpts * w90_nntot))
575 do ii = 1, w90_num_kpts * w90_nntot
576 read(w90_nnkp,*) w90_nnk_list(1:5, ii)
577 end do
578 !make sure we are at the end of the block
579 read(w90_nnkp,*) dummy
580 if (dummy /= 'end') then
581 message(1) = 'oct-wannier90: There dont seem to be enough k-points in nnkpts file to.'
582 call messages_fatal(1)
583 end if
584 parse_is_ok = .true.
585 exit
586 end if
587 end do
588
589 if (.not. parse_is_ok) then
590 message(1) = 'oct-wannier90: Did not find nnkpts block in nnkp file'
591 call messages_fatal(1)
592 end if
593
594 ! read from nnkp file
595 ! find the exclude block
596 safe_allocate(exclude_list(1:sys%st%nst))
597 !By default we use all the bands
598 exclude_list(1:sys%st%nst) = .false.
599 rewind(w90_nnkp)
600 do
601 read(w90_nnkp, *, iostat=io) dummy, dummy1
602 if (io == iostat_end) exit !End of file
603 if (dummy == 'begin' .and. dummy1 == 'exclude_bands') then
604 read(w90_nnkp, *) w90_num_exclude
605 do ii = 1, w90_num_exclude
606 read(w90_nnkp, *) itemp
607 if(itemp > sys%st%nst) then
608 message(1) = 'oct-wannier90: The exclude_bands list contains a state index higher than the number of states.'
609 call messages_fatal(1)
610 end if
611 exclude_list(itemp) = .true.
612 end do
613 !make sure we are at the end of the block
614 read(w90_nnkp, *) dummy
615 if (dummy /= 'end') then
616 message(1) = 'oct-wannier90: There dont seem to be enough bands in exclude_bands list.'
617 call messages_fatal(1)
618 end if
619 exit
620 end if
621 end do
622 call io_close(w90_nnkp)
623
624 !We get the number of bands
625 w90_num_bands = sys%st%nst - w90_num_exclude
626
627 safe_allocate(band_index(1:sys%st%nst))
628 safe_allocate(active_bands(1:w90_num_bands))
629 itemp = 0
630 do ii = 1, sys%st%nst
631 if (exclude_list(ii)) cycle
632 itemp = itemp + 1
633 band_index(ii) = itemp
634 active_bands(itemp) = ii
635 end do
636
637 if (bitand(w90_what, option__wannier90files__w90_amn) /= 0 &
638 .or. w90_mode == option__wannier90mode__w90_wannier ) then
639 ! parse file again for definitions of projections
640 w90_nnkp = io_open(trim(filename), global_namespace, action='read', position='rewind')
641
642 do
643 read(w90_nnkp, *, iostat=io) dummy, dummy1
644 if (io == iostat_end) then !End of file
645 message(1) = 'oct-wannier90: Did not find projections block in w90.nnkp file'
646 call messages_fatal(1)
647 end if
648
649 if (dummy == 'begin' .and. (dummy1 == 'projections' .or. dummy1 == 'spinor_projections')) then
650
651 if (dummy1 == 'spinor_projections') then
652 w90_spinors = .true.
653 if (sys%st%d%ispin /= spinors) then
654 message(1) = 'oct-wannier90: Spinor = .true. is only valid with spinors wavefunctions.'
655 call messages_fatal(1)
656 end if
657
658 message(1) = 'oct-wannier90: Spinor interface incomplete. Note there is no quantization axis implemented'
659 call messages_warning(1)
660 else
661 if (sys%st%d%ispin == spinors) then
662 message(1) = 'oct-wannier90: Octopus has spinors wavefunctions but spinor_projections is not defined.'
663 message(2) = 'oct-wannier90: Please check the input file for wannier 90.'
664 call messages_fatal(2)
665 end if
666 end if
667
668 read(w90_nnkp, *) w90_nproj
669 ! num_wann is given in w90.win, not double checked here
670 ! I assume that the wannier90.x -pp run has checked this
671 ! TODO (Jonas): Implement support for select_projections
672 w90_num_wann = w90_nproj
673 ! In case of no projections, we use the number of bands
674 if(w90_nproj == 0) w90_num_wann = w90_num_bands
675
676 safe_allocate(w90_proj_centers(1:3, 1:w90_nproj))
677 safe_allocate(w90_proj_lmr(1:w90_nproj, 1:3))
678 if (w90_spinors) then
679 safe_allocate(w90_spin_proj_component(1:w90_nproj))
680 end if
681 if (w90_spinors) then
682 safe_allocate(w90_spin_proj_axis(1:w90_nproj, 1:3))
683 end if
684
685 do ii = 1, w90_nproj
686 read(w90_nnkp, *) w90_proj_centers(1:3, ii), w90_proj_lmr(ii, 1:3)
687 ! skip a line for now
688 read(w90_nnkp, *) dummyr(1:7)
689 if (w90_spinors) then
690 read(w90_nnkp, *) w90_spin_proj_component(ii), w90_spin_proj_axis(ii, 1:3)
691 ! use octopus spindim conventions
692 if (w90_spin_proj_component(ii) == -1) w90_spin_proj_component(ii) = 2
693 end if
694 end do
695 !make sure we are at the end of the block
696 read(w90_nnkp, *) dummy
697 if (dummy /= 'end') then
698 message(1) = 'oct-wannier90: There dont seem to be enough projections in nnkpts file to.'
699 call messages_fatal(1)
700 end if
701 exit
702 end if
703 end do
704
705 ! look for auto_projection block
706 scdm_proj = .false.
707 do
708 read(w90_nnkp, *, iostat=io) dummy, dummy1
709 if (io == iostat_end) exit !End of file
710
711 if (dummy == 'begin' .and. dummy1 == 'auto_projections') then
712 scdm_proj = .true.
713 read(w90_nnkp, *) w90_num_wann
714
715 if (.not. w90_scdm) then
716 message(1) = 'oct-wannier90: Found auto_projections block. Currently the only implemented automatic way'
717 message(2) = 'oct-wannier90: to compute projections is the SCDM method.'
718 message(3) = 'oct-wannier90: Please set Wannier90UseSCDM = yes in the inp file.'
719 call messages_fatal(3)
720 end if
721
722 read(w90_nnkp, *) dummyint
723 if (dummyint /= 0) then
724 message(1) = 'oct-wannier90: The second row in auto_projections has to be 0, per Wannier90 documentation.'
725 call messages_fatal(1)
726 end if
727
728 if (w90_num_wann <= 0) then
729 message(1) = 'oct-wannier90: num_wann must be larger than 0 when using auto_projections.'
730 call messages_fatal(1)
731 end if
732 if (w90_num_wann > w90_num_bands) then
733 message(1) = 'oct-wannier90: num_wann cannot be larger than num_bands when using auto_projections.'
734 call messages_fatal(1)
735 end if
736 end if
737 end do
738 call io_close(w90_nnkp)
739
740 end if
741
742 message(1) = "oct-wannier90: Finished parsing "//filename
743 call messages_info(1)
744
745 ! Look extra variables variable
746 ! open win file
747 filename = trim(adjustl(w90_prefix)) //'.win'
748 message(1) = "oct-wannier90: Parsing "//filename
749 call messages_info(1)
750 w90_nnkp = io_open(trim(filename), global_namespace, action='read', position='rewind')
751 do
752 read(w90_nnkp, fmt='(a)', iostat=io) line
753 if (io == iostat_end) exit !End of file
754 if (index(line, '=') > 0) then
755 read(line, *, iostat=io) dummy, dummy2, dummy1
756 else
757 read(line, *, iostat=io) dummy, dummy1
758 end if
759
760 if (dummy == 'dis_win_min') then
761 if (index(line, '=') > 0) then
762 read(line, *, iostat=io) dummy, dummy2, dis_win_min_ev
763 else
764 read(line, *, iostat=io) dummy, dis_win_min_ev
765 end if
766 if (io == 0) w90_have_dis_win_min = .true.
767 end if
768
769 if (dummy == 'dis_win_max') then
770 if (index(line, '=') > 0) then
771 read(line, *, iostat=io) dummy, dummy2, dis_win_max_ev
772 else
773 read(line, *, iostat=io) dummy, dis_win_max_ev
774 end if
775 if (io == 0) w90_have_dis_win_max = .true.
776 end if
777
778 !Spin
779 if (dummy == 'spin') then
780 if (sys%st%d%ispin /= spin_polarized) then
781 message(1) = 'oct-wannier90: The variable spin is set for a non spin-polarized calculation.'
782 call messages_fatal(1)
783 end if
784
785 if (dummy1 == 'up') then
786 spin_channel_win = 1
787 else if (dummy1 == 'down') then
788 spin_channel_win = 2
789 else
790 message(1) = 'oct-wannier90: Error parsing the variable spin.'
791 call messages_fatal(1)
792 end if
793 if (spin_channel_win /= w90_spin_channel) then
794 message(1) = 'spin polarization input from .win and Wannier90SpinChannel do not agree.'
795 call messages_fatal(1)
796 end if
797 end if
798 end do
799 call io_close(w90_nnkp)
800
801 if (w90_have_dis_win_min) then
802 w90_dis_win_min = units_to_atomic(unit_ev, dis_win_min_ev)
803 write(message(1), '(a,f12.5)') 'oct-wannier90: Parsed dis_win_min (eV) = ', dis_win_min_ev
804 call messages_info(1)
805 end if
806 if (w90_have_dis_win_max) then
807 w90_dis_win_max = units_to_atomic(unit_ev, dis_win_max_ev)
808 write(message(1), '(a,f12.5)') 'oct-wannier90: Parsed dis_win_max (eV) = ', dis_win_max_ev
809 call messages_info(1)
810 end if
811
812 if (sys%st%d%ispin == spin_polarized) then
813 write(message(1), '(a,i1)') 'oct-wannier90: Using spin channel ', w90_spin_channel
814 call messages_info(1)
815 end if
816
817 message(1) = "oct-wannier90: Finished parsing "//filename
818 call messages_info(1)
819
820 pop_sub(read_wannier90_files)
821
822 end subroutine read_wannier90_files
823
824 ! --------------------------------------------------------------------------
825 subroutine create_wannier90_mmn(mesh, st)
826 class(mesh_t), intent(in) :: mesh
827 type(states_elec_t), target, intent(in) :: st
828
829 integer :: ist, jst, ik, ip, w90_mmn, iknn, ib
830 real(real64) :: Gcart(3)
831 integer :: G(3)
832 character(len=80) :: filename
833 complex(real64), allocatable :: overlap(:), ss(:)
834 complex(real64), allocatable :: psin(:,:), phase(:)
835 type(wfs_elec_t), pointer :: batch
836 integer :: inode, node_fr, node_to
837 type(mpi_request) :: send_req
838
839 push_sub(create_wannier90_mmn)
840
841 call profiling_in("W90_MMN")
842
843 if (st%parallel_in_states) then
844 call messages_not_implemented("w90_mmn output with states parallelization")
845 end if
846
847 message(1) = "Info: Computing the overlap matrix"
848 call messages_info(1)
849
850
851 filename = './'// trim(adjustl(w90_prefix))//'.mmn'
852 w90_mmn = io_open(trim(filename), global_namespace, action='write')
853
854 ! write header
855 if (st%system_grp%is_root()) then
856 write(w90_mmn,*) 'Created by oct-wannier90'
857 write(w90_mmn,*) w90_num_bands, w90_num_kpts, w90_nntot
858 end if
859
860 safe_allocate(psin(1:mesh%np, 1:st%d%dim))
861 safe_allocate(phase(1:mesh%np))
862 safe_allocate(overlap(1:w90_num_bands))
863 safe_allocate(ss(1:st%nst))
864
865
866 ! loop over the pairs specified in the nnkp file (read before in init)
867 do ii = 1, w90_num_kpts * w90_nntot
868 ik = w90_nnk_list(1, ii)
869 iknn = w90_nnk_list(2, ii)
870 g(1:3) = w90_nnk_list(3:5, ii)
871 if (st%system_grp%is_root()) write(w90_mmn, '(I10,2x,I10,2x,I3,2x,I3,2x,I3)') ik, iknn, g
872 ! For mixed periodicity (wires, slabs) we remove the G vectors along the non-periodic
873 ! directions, as this correspond to the infinite vacuum limit. This does not apply to
874 ! fully non-periodic systems, where all directions use the fictitious box periodicity
875 ! (see the rlattice/klattice correction above) and thus need the phase factor.
876 if (sys%space%periodic_dim > 0) then
877 g(sys%space%periodic_dim+1:sys%space%dim) = 0
878 end if
879
880 ! For spin-polarized calculations, we select the right k-point
881 ik = sys%st%d%get_k_spin_index(ik, w90_spin_channel)
882 iknn = sys%st%d%get_k_spin_index(iknn, w90_spin_channel)
883
884 ! Only treat local k-points
885 if(ik >= st%d%kpt%start .and. ik <= st%d%kpt%end) then
886
887 ! Wannier90 treats everything fully periodic
888 ! Conversion is done with the 3D "correct" klattice
889 call kpoints_to_absolute(sys%ions%latt, real(g, real64) , gcart)
890
891 ! Phase that gives u_{n,k+G}(r) from u_{nk}(r)
892 ! Here the minus sign of Octopus cancels with minus sign of the input G
893 ! (ik and iknn correspond in the list to minus the k-points in Octopus)
894 if (any(g /= 0)) then
895 !$omp parallel do
896 do ip = 1, mesh%np
897 phase(ip) = exp(-m_zi*dot_product(mesh%x(1:3, ip), gcart(1:3)))
898 end do
899 end if
900
901 end if
902
903 ! Loop over distributed bands
904 ! Slow index in M_{mn}^{k,b}, so \psi_n
905 do jst = 1, st%nst
906 if (exclude_list(jst)) cycle
907
908 ! Communication for the local states
909 if ( .not. st%d%kpt%parallel .and. .not. st%parallel_in_states) then
910 call states_elec_get_state(st, mesh, jst, iknn, psin)
911 else
912 node_fr = -1
913 node_to = -1
914 do inode = 0, st%d%kpt%mpi_grp%size-1
915 if(iknn >= st%st_kpt_task(inode,3) .and. iknn <= st%st_kpt_task(inode,4)) then
916 node_fr = inode
917 end if
918 if(ik >= st%st_kpt_task(inode,3) .and. ik <= st%st_kpt_task(inode,4)) then
919 node_to = inode
920 end if
921 end do
922 assert(node_fr > -1)
923 assert(node_to > -1)
924
925 send_req = mpi_request_null
926 ! We have locally the k-point
927 if (state_kpt_is_local(st, jst, iknn)) then
928 call states_elec_get_state(st, mesh, jst, iknn, psin)
929 ! We send it only if we don`t want to use it locally
930 if(node_to /= st%d%kpt%mpi_grp%rank) then
931 call st%d%kpt%mpi_grp%isend(psin, mesh%np*st%d%dim, mpi_double_complex, node_to, send_req)
932 end if
933 end if
934 ! We receive the desired state, only if it is not a local one
935 if(node_to == st%d%kpt%mpi_grp%rank .and. node_to /= node_fr) then
936 call st%d%kpt%mpi_grp%recv(psin, mesh%np*st%d%dim, mpi_double_complex, node_fr)
937 end if
938 if (send_req /= mpi_request_null) then
939 call st%d%kpt%mpi_grp%wait(send_req)
940 end if
941 end if
942
943 overlap = m_zero
944
945 if(ik >= st%d%kpt%start .and. ik <= st%d%kpt%end) then
946
947 ! Do not apply the phase if the phase factor is null
948 if (any(g /= 0)) then
949 ! add phase
950 !$omp parallel
951 do idim = 1, st%d%dim
952 !$omp do
953 do ip = 1, mesh%np
954 psin(ip, idim) = psin(ip, idim) * phase(ip)
955 end do
956 end do
957 !$omp end parallel
958 end if
959
960
961 ! See Eq. (25) in PRB 56, 12847 (1997)
962 ! Fast index in M_{mn}^{k,b}, so m.
963 do ib = st%group%block_start, st%group%block_end
964 batch => st%group%psib(ib, ik)
965 if (all(exclude_list(batch%ist(1:batch%nst)))) cycle
966 call zmesh_batch_mf_dotp(mesh, batch, psin, ss(1:batch%nst), reduce = .false.)
967 do ist = 1, batch%nst
968 if (exclude_list(batch%ist(ist))) cycle
969 overlap(band_index(batch%ist(ist))) = ss(ist)
970 end do
971 end do
972 end if
973
974 call profiling_in("W90_MMN_REDUCE")
975 call mesh%allreduce(overlap)
976 call profiling_out("W90_MMN_REDUCE")
977
978 if(st%d%kpt%parallel) then
979 call comm_allreduce(st%d%kpt%mpi_grp, overlap)
980 end if
981
982 ! write to W90 file
983 if (st%system_grp%is_root()) then
984 do ist = 1, st%nst
985 if (exclude_list(ist)) cycle
986 write(w90_mmn,'(e18.10,2x,e18.10)') overlap(band_index(ist))
987 end do
988 end if
989
990 end do !jst
991 end do
992
993 call io_close(w90_mmn)
994
995 safe_deallocate_a(psin)
996 safe_deallocate_a(phase)
997 safe_deallocate_a(overlap)
998 safe_deallocate_a(ss)
999
1000 call profiling_out("W90_MMN")
1001
1002 pop_sub(create_wannier90_mmn)
1003
1004 end subroutine create_wannier90_mmn
1005
1006 ! --------------------------------------------------------------------------
1007 subroutine create_wannier90_eig()
1008 integer :: ist, ik, w90_eig
1009 character(len=80) :: filename
1010
1011 push_sub(create_wannier90_eig)
1012
1013 if (sys%st%parallel_in_states) then
1014 call messages_not_implemented("w90_eig output with states parallelization")
1015 end if
1016
1017 if (sys%st%system_grp%is_root()) then
1018 filename = './'//trim(adjustl(w90_prefix))//'.eig'
1019 w90_eig = io_open(trim(filename), global_namespace, action='write')
1020 do ik = 1, w90_num_kpts
1021 do ist = 1, sys%st%nst
1022 if (exclude_list(ist)) cycle
1023 if (sys%st%d%ispin /= spin_polarized) then
1024 write(w90_eig,'(I5,2x,I8,2x,e18.10)') band_index(ist), ik, &
1025 units_from_atomic(unit_ev, sys%st%eigenval(ist, ik))
1026 else
1027 write(w90_eig,'(I5,2x,I8,2x,e18.10)') band_index(ist), ik, &
1028 units_from_atomic(unit_ev, sys%st%eigenval(ist, (ik-1)*2+w90_spin_channel))
1029 end if
1030 end do
1031 end do
1032
1033 call io_close(w90_eig)
1034 end if
1035
1036 pop_sub(create_wannier90_eig)
1037 end subroutine create_wannier90_eig
1038
1039 ! --------------------------------------------------------------------------
1040 subroutine write_unk(space, mesh, st, formatted)
1041 class(space_t), intent(in) :: space
1042 class(mesh_t), intent(in) :: mesh
1043 type(states_elec_t), intent(in) :: st
1044 logical, intent(in) :: formatted
1045
1046 integer :: ist, ik, unk_file, ispin
1047 integer :: ix, iy, iz
1048 real(real64) :: w_real, w_imag
1049 character(len=80) :: filename
1050 complex(real64), allocatable :: psi(:)
1051 type(cube_t) :: cube
1052 type(cube_function_t) :: cf
1053
1054 push_sub(write_unk)
1055
1056 if (st%d%kpt%parallel) then
1057 call messages_not_implemented("w90_unk output with k-point parallelization")
1058 end if
1059
1060 if (sys%gr%parallel_in_domains) then
1061 call messages_not_implemented("w90_unk output with domain parallelization")
1062 end if
1063
1064 if (st%parallel_in_states) then
1065 call messages_not_implemented("w90_unk output with states parallelization")
1066 end if
1067
1068 call messages_experimental("Wannier90Files = w90_unk")
1069
1070
1071 safe_allocate(psi(1:mesh%np))
1072
1073 call cube_init(cube, mesh%idx%ll, global_namespace, space, mesh%spacing, &
1074 mesh%coord_system, need_partition=.not.mesh%parallel_in_domains)
1075 call cube_init_cube_map(cube, mesh)
1076
1077 call zcube_function_alloc_rs(cube, cf)
1078 assert(cf%batch_size == 1)
1079
1080 do ikpoint = 1, w90_num_kpts
1081
1082 do ispin = 1, st%d%dim
1083 if (st%system_grp%is_root()) then
1084 write(filename, '(a,i5.5,a1,i1)') './UNK', ikpoint,'.', ispin
1085 ! write header
1086 if (formatted) then
1087 unk_file = io_open(trim(filename), global_namespace, action='write', form='formatted')
1088 write(unk_file, *) mesh%idx%ll(1:mesh%idx%dim), ikpoint, w90_num_bands
1089 else
1090 unk_file = io_open(trim(filename), global_namespace, action='write', form='unformatted')
1091 write(unk_file) mesh%idx%ll(1:mesh%idx%dim), ikpoint, w90_num_bands
1092 end if
1093 end if
1094
1095 ! states
1096 do ist = 1, st%nst
1097 if (exclude_list(ist)) cycle
1098
1099 ik = st%d%get_k_spin_index(ikpoint, w90_spin_channel)
1100 call states_elec_get_state(st, mesh, ispin, ist, ik, psi)
1101
1102 ! put the density in the cube
1103 ! Note: At the moment this does not work for domain parallelization
1104 assert(.not. cube%parallel_in_domains)
1105 call zmesh_to_cube(mesh, psi, cube, cf)
1106
1107 if (st%system_grp%is_root()) then
1108 if (formatted) then
1109 do iz=1,cube%rs_n_global(3)
1110 do iy = 1,cube%rs_n_global(2)
1111 do ix = 1,cube%rs_n_global(1)
1112 w_real = real(cf%zrs(ix,iy,iz,1), real64)
1113 w_imag = aimag(cf%zrs(ix,iy,iz,1))
1114 write (unk_file, *) w_real, w_imag
1115 end do
1116 end do
1117 end do
1118 else
1119 write(unk_file) (((cf%zrs(ix,iy,iz,1), ix=1,cube%rs_n_global(1)), iy=1,cube%rs_n_global(2)), iz=1,cube%rs_n_global(3))
1120 end if
1121 end if
1122 end do
1123 if (st%system_grp%is_root()) call io_close(unk_file)
1124 end do
1125 end do
1126
1127 call zcube_function_free_rs(cube, cf)
1128 call cube_end(cube)
1129
1130 safe_deallocate_a(psi)
1131
1132 pop_sub(write_unk)
1133
1134 end subroutine write_unk
1136 ! --------------------------------------------------------------------------
1137 subroutine create_wannier90_amn(space, mesh, latt, st, kpoints)
1138 class(space_t), intent(in) :: space
1139 class(mesh_t), intent(in) :: mesh
1140 type(lattice_vectors_t), intent(in) :: latt
1141 type(states_elec_t), intent(in) :: st
1142 type(kpoints_t), intent(in) :: kpoints
1143
1144 integer :: ist, ik, w90_amn, idim, iw, ip, igamma
1145 real(real64) :: center(3), kpoint(3), threshold
1146 character(len=80) :: filename
1147 complex(real64), allocatable :: psi(:,:), psi_global(:,:), phase(:), projection(:)
1148 real(real64), allocatable :: ylm(:)
1149 type(orbitalset_t), allocatable :: orbitals(:)
1150
1151 integer :: tag, root_rank
1152
1153 push_sub(create_wannier90_amn)
1154 call profiling_in("W90_AMN")
1155
1156 if (st%parallel_in_states) then
1157 call messages_not_implemented("w90_amn output with states parallelization")
1158 end if
1159
1160 filename = './'// trim(adjustl(w90_prefix))//'.amn'
1161 w90_amn = io_open(trim(filename), global_namespace, action='write')
1162
1163 ! write header
1164 if (st%system_grp%is_root()) then
1165 write(w90_amn,*) 'Created by oct-wannier90'
1166 write(w90_amn,*) w90_num_bands, w90_num_kpts, w90_num_wann
1167 end if
1168
1169 if (scdm_proj) then
1170
1171 safe_allocate(jpvt(1:sys%gr%np_global*sys%st%d%dim))
1172 safe_allocate(psi(1:sys%gr%np, 1:sys%st%d%dim))
1173 safe_allocate(psi_global(1:sys%gr%np_global, 1:sys%st%d%dim))
1174 safe_allocate(x_global(1:sys%gr%np_global, 1:3))
1175 safe_allocate(occ_temp(1:sys%st%nst))
1176 safe_allocate(occ_smeared(1:w90_num_bands,1:w90_num_kpts))
1177
1178 root_rank = 0
1179 if (sys%st%system_grp%is_root()) then
1180 root_rank = sys%st%system_grp%rank
1181 end if
1182
1183 ! choose SCDM smearing function
1184 call choose_scdm_smearing_function(sys%st, active_bands, w90_num_kpts, w90_num_bands, &
1185 w90_spin_channel, w90_scdm_entanglement, scdm_mu, scdm_sigma, occ_smeared)
1186
1187 ! The QR decomposition is only done at the Gamma point.
1188 ! The original SCDM paper (https://arxiv.org/abs/1703.06958) states this is sufficient in most cases for solids.
1189 igamma = 0
1190 do ikpoint = 1, w90_num_kpts
1191 if (kpoints_point_is_gamma(kpoints, ikpoint)) then
1192 igamma = ikpoint
1193 exit
1194 end if
1195 end do
1196 if (igamma == 0) then
1197 ! A generalisation to other k-points is described in https://doi.org/10.1016/j.jcp.2016.12.053
1198 call messages_not_implemented('SCDM: only implemented for Gamma point. Gamma not present.')
1199 end if
1200
1201 ! smear the states at gamma and set occupations at excluded bands to zero
1202 do ist = 1, sys%st%nst
1203 occ_temp(ist)= sys%st%occ(ist, igamma)
1204 if (exclude_list(ist)) then
1205 sys%st%occ(ist, igamma)=m_zero
1206 else
1207 sys%st%occ(ist, igamma)=occ_smeared(band_index(ist), igamma)
1208 end if
1209 end do
1210
1211 ! Perform RRQR decomposition at Gamma point (in real space)
1212 call zstates_elec_rrqr_decomposition(sys%st, sys%namespace, sys%gr, active_bands, igamma, jpvt)
1213
1214 ! reset occupations at gamma
1215 do ist = 1, sys%st%nst
1216 sys%st%occ(ist, igamma) = occ_temp(ist)
1217 end do
1218
1219 safe_allocate(uk(1:w90_num_bands, 1:w90_num_wann, 1:w90_num_kpts))
1220
1221 ! auxiliary arrays for scdm procedure
1222 safe_allocate(chi(1:w90_num_bands, 1:w90_num_wann))
1223 safe_allocate(chi_diag(1:w90_num_wann, 1:w90_num_wann))
1224 safe_allocate(chi2(1:w90_num_wann, 1:w90_num_wann))
1225 safe_allocate(chi_eigenval(1:w90_num_wann))
1226
1227 chi(1:w90_num_bands, 1:w90_num_wann) = m_zero
1228
1229 ! This is an auxiliary array for gathering all states from domain parallelization
1230 do idim=1,3
1231 call par_vec_allgather(sys%gr%pv, x_global(:,idim), sys%gr%x(idim,1:sys%gr%np))
1232 end do
1233
1234 ! TODO: handle spin-polarized case correctly here
1235 do ikpoint = 1, w90_num_kpts
1236 kvec(:) = sys%kpoints%reduced%point(:, ikpoint)
1237 ik = sys%st%d%get_k_spin_index(ikpoint, w90_spin_channel)
1238 do ist = 1, w90_num_bands
1239 sender = -1
1240 if (state_kpt_is_local(sys%st, active_bands(ist), ik)) then
1241 call states_elec_get_state(sys%st, sys%gr, active_bands(ist), ik, psi)
1242 do idim = 1, sys%st%d%dim
1243 call par_vec_allgather(sys%gr%pv, psi_global(:,idim), psi(:,idim))
1244 end do
1245 sender = sys%st%d%kpt%mpi_grp%rank
1246 else
1247 psi_global(:,:) = m_zero
1248 endif
1249 call sys%st%d%kpt%mpi_grp%allreduce_inplace(sender, 1, mpi_integer, mpi_max)
1250 assert(sender >= 0)
1251 ! (Alex) Could be any non-negative value - we never do anything with the tag
1252 tag = (idim - 1)*size(active_bands) + ii
1253 if (sender /= root_rank) then
1254 if (sys%st%system_grp%rank == sender) then
1255 do idim = 1, sys%st%d%dim
1256 call sys%st%system_grp%send(psi_global(:,idim), i8_to_i4(sys%gr%np_global), mpi_double_complex, root_rank, tag=tag)
1257 end do
1258 end if
1259 if (sys%st%system_grp%is_root()) then
1260 do idim = 1, sys%st%d%dim
1261 call sys%st%system_grp%recv(psi_global(:,idim), i8_to_i4(sys%gr%np_global), mpi_double_complex, sender, tag=tag)
1262 end do
1263 end if
1264 end if
1265 if (sys%st%system_grp%is_root()) then
1266 do jst = 1, w90_num_wann
1267 chi(ist, jst) = occ_smeared(ist, ik) * conjg(psi_global(jpvt(jst), 1)) &
1268 * exp(m_zi * dot_product(x_global(jpvt(jst),1:3), kvec(1:3)))
1269 end do
1270 end if
1271 end do
1272
1273 if (sys%st%system_grp%is_root()) then
1274 ! loewdin orhtogonalization of chi.chi
1275 ! this can also be done with SVD, which might be more stable!?
1276 chi_diag = matmul(conjg(transpose(chi)), chi)
1277 call lalg_eigensolve(w90_num_wann, chi_diag, chi_eigenval)
1278 chi2 = conjg(transpose(chi_diag))
1279
1280 !we need the eigenvalues to be >0
1281 if (any(chi_eigenval(:) .lt. m_zero)) then
1282 message(1) = 'SCDM Wannierization failed because chi matrix is'
1283 message(2) = 'ill conditioned. Try changing scdm_sigma and/or'
1284 message(3) = 'scdm_mu.'
1285 call messages_fatal(3)
1286 end if
1287
1288 do ist = 1, w90_num_wann
1289 chi_eigenval(ist) = m_one / sqrt(chi_eigenval(ist))
1290 chi2(ist, 1:w90_num_wann) = chi_eigenval(ist) * chi2(ist, 1:w90_num_wann)
1291 end do
1292 ! the loewdin result would be: matmul(chi_diag,chi2)
1293 ! to get the wannier gauge U(k) we multiply this with the original chi
1294 uk(:,:,ik) = matmul(chi, matmul(chi_diag,chi2))
1295 end if
1296
1297 end do
1298
1299 safe_deallocate_a(chi)
1300 safe_deallocate_a(psi)
1301 safe_deallocate_a(psi_global)
1302 safe_deallocate_a(x_global)
1303 safe_deallocate_a(chi_diag)
1304 safe_deallocate_a(chi2)
1305 safe_deallocate_a(chi_eigenval)
1306 safe_deallocate_a(jpvt)
1307 safe_deallocate_a(occ_temp)
1308 safe_deallocate_a(occ_smeared)
1309
1310 message(1) = "Info: Writing projections obtained from SCDM."
1311 call messages_info(1)
1312
1313 do ik = 1, w90_num_kpts
1314 do ist = 1, st%nst
1315 if (exclude_list(ist)) cycle
1316 if (st%system_grp%is_root()) then
1317 do iw = 1, w90_num_wann
1318 write (w90_amn,'(I5,2x,I5,2x,I5,2x,e18.10,2x,e18.10)') band_index(ist), iw, ik, uk(band_index(ist),iw,ik)
1319 end do
1320 end if
1321 end do !ist
1322 end do! ik
1323
1324 safe_deallocate_a(uk)
1325
1326 else
1327
1328 message(1) = "Info: Computing the projection matrix"
1329 call messages_info(1)
1330
1331 !We use the variable AOThreshold to deterine the threshold on the radii of the atomic orbitals
1332 call parse_variable(global_namespace, 'AOThreshold', 0.001_real64, threshold)
1333
1334 safe_allocate(orbitals(1:w90_nproj))
1335 ! precompute orbitals
1336 do iw=1, w90_nproj
1337 call orbitalset_init(orbitals(iw))
1338
1339 orbitals(iw)%norbs = 1
1340 orbitals(iw)%ndim = 1
1341 orbitals(iw)%radius = -log(threshold)
1342 orbitals(iw)%use_submesh = .false.
1343
1344 ! cartesian coordinate of orbital center
1345 center(1:3) = latt%red_to_cart(w90_proj_centers(1:3, iw))
1346 call submesh_init(orbitals(iw)%sphere, space, mesh, latt, center, orbitals(iw)%radius)
1347
1348 ! get dorb as submesh points
1349 safe_allocate(ylm(1:orbitals(iw)%sphere%np))
1350 ! (this is a routine from pwscf)
1351 call ylm_wannier(ylm, w90_proj_lmr(iw,1), w90_proj_lmr(iw,2), &
1352 orbitals(iw)%sphere%r, orbitals(iw)%sphere%rel_x, orbitals(iw)%sphere%np)
1353
1354 ! apply radial function
1355 if (w90_proj_lmr(iw,3) == 1) then
1356 do ip = 1,orbitals(iw)%sphere%np
1357 ylm(ip) = ylm(ip)*m_two*exp(-orbitals(iw)%sphere%r(ip))
1358 end do
1359 else
1360 call messages_not_implemented("oct-wannier90: r/=1 for the radial part")
1361 end if
1362
1363 safe_allocate(orbitals(iw)%zorb(1:orbitals(iw)%sphere%np, 1, 1))
1364 orbitals(iw)%zorb(1:orbitals(iw)%sphere%np, 1, 1) = ylm(1:orbitals(iw)%sphere%np)
1365 safe_deallocate_a(ylm)
1366
1367 safe_allocate(orbitals(iw)%phase(1:orbitals(iw)%sphere%np, st%d%kpt%start:st%d%kpt%end))
1368 orbitals(iw)%phase(:,:) = m_z0
1369 safe_allocate(orbitals(iw)%eorb_mesh(1:mesh%np, 1, 1, st%d%kpt%start:st%d%kpt%end))
1370 orbitals(iw)%eorb_mesh(:,:,:,:) = m_z0
1371
1372 call orbitalset_update_phase(orbitals(iw), space%dim, st%d%kpt, kpoints, st%d%ispin == spin_polarized, &
1373 kpt_max = w90_num_kpts)
1374
1375 end do
1376
1377 safe_allocate(psi(1:mesh%np, 1:st%d%dim))
1378 safe_allocate(phase(1:mesh%np))
1379 safe_allocate(projection(1:w90_nproj))
1380
1381 do ikpoint = 1, w90_num_kpts
1382 kpoint(1:space%dim) = kpoints%get_point(ikpoint)
1383 !$omp parallel do
1384 do ip = 1, mesh%np
1385 phase(ip) = exp(-m_zi* sum(mesh%x(1:space%dim, ip) * kpoint(1:space%dim)))
1386 end do
1387
1388 !For spin-polarized calculations, we select the right k-point
1389 ik = sys%st%d%get_k_spin_index(ikpoint, w90_spin_channel)
1390
1391 do ist = 1, st%nst
1392 if (exclude_list(ist)) cycle
1393
1394 projection = m_zero
1395
1396 if(ik >= st%d%kpt%start .and. ik <= st%d%kpt%end) then
1397 call states_elec_get_state(st, mesh, ist, ik, psi)
1398
1399 !$omp parallel
1400 do idim = 1, st%d%dim
1401 !The minus sign is here is for the wrong convention of Octopus
1402 !$omp do
1403 do ip = 1, mesh%np
1404 psi(ip, idim) = psi(ip, idim)*phase(ip)
1405 end do
1406 end do
1407 !$omp end parallel
1408
1409 do iw = 1, w90_nproj
1410 idim = 1
1411 if (w90_spinors) idim = w90_spin_proj_component(iw)
1412
1413 !At the moment the orbitals do not depend on idim
1414 !The idim index for eorb_mesh would be for a spin-resolved orbital like j=1/2
1415 projection(iw) = zmf_dotp(mesh, psi(1:mesh%np,idim), &
1416 orbitals(iw)%eorb_mesh(1:mesh%np,1,1,ik), reduce = .false.)
1417 end do
1418
1419 call profiling_in("W90_AMN_REDUCE")
1420 call mesh%allreduce(projection)
1421 call profiling_out("W90_AMN_REDUCE")
1422 end if
1423
1424 if(st%d%kpt%parallel) then
1425 call comm_allreduce(st%d%kpt%mpi_grp, projection)
1426 end if
1427
1428 if (st%system_grp%is_root()) then
1429 do iw = 1, w90_nproj
1430 write (w90_amn,'(I5,2x,I5,2x,I5,2x,e18.10,2x,e18.10)') band_index(ist), iw, ikpoint, projection(iw)
1431 end do
1432 end if
1433 end do !ist
1434 end do !ik
1435
1436 safe_deallocate_a(psi)
1437 safe_deallocate_a(phase)
1438 safe_deallocate_a(projection)
1439
1440 do iw = 1, w90_nproj
1441 call orbitalset_end(orbitals(iw))
1442 end do
1443 safe_deallocate_a(orbitals)
1444 end if
1445
1446 call io_close(w90_amn)
1447
1448 call profiling_out("W90_AMN")
1449
1450 pop_sub(create_wannier90_amn)
1451
1452 end subroutine create_wannier90_amn
1453
1454 ! --------------------------------------------------------------------------
1458 subroutine create_wannier90_spn(mesh, st)
1459 class(mesh_t), intent(in) :: mesh
1460 type(states_elec_t), target, intent(in) :: st
1461
1462 integer :: ist, jst, ik, w90_spn, counter
1463 character(len=80) :: filename
1464 complex(real64), allocatable :: spin(:,:,:)
1465 complex(real64), allocatable :: psim(:,:), psin(:,:)
1466 complex(real64) :: dot_upup, dot_updown, dot_downup, dot_downdown
1467
1468 push_sub(create_wannier90_spn)
1469 call profiling_in("W90_SPN")
1470
1471 assert(st%d%ispin == spinors)
1472
1473 if (st%parallel_in_states) then
1474 call messages_not_implemented("w90_spn output with states parallelization")
1475 end if
1476
1477 message(1) = "Info: Computing the spin file"
1478 call messages_info(1)
1479
1480 filename = './'// trim(adjustl(w90_prefix))//'.spn'
1481 w90_spn = io_open(trim(filename), global_namespace, action='write')
1482
1483 ! write header
1484 if (st%system_grp%is_root()) then
1485 write(w90_spn,*) 'Created by oct-wannier90'
1486 write(w90_spn,*) w90_num_bands, w90_num_kpts
1487 end if
1488
1489 safe_allocate(psim(1:mesh%np, 1:st%d%dim))
1490 safe_allocate(psin(1:mesh%np, 1:st%d%dim))
1491 safe_allocate(spin(1:3, 1:(w90_num_bands*(w90_num_bands+1))/2, 1:w90_num_kpts))
1492 spin = m_zero
1493
1494 ! loop over the pairs specified in the nnkp file (read before in init)
1495 do ik = st%d%kpt%start, st%d%kpt%end
1496 counter = 0
1497 do jst = 1, st%nst
1498 if (exclude_list(jst)) cycle
1499
1500 call states_elec_get_state(st, mesh, jst, ik, psim)
1501 do ist = 1, jst
1502 if (exclude_list(ist)) cycle
1503
1504 counter = counter + 1
1505
1506 call states_elec_get_state(st, mesh, ist, ik, psin)
1507
1508 dot_upup = zmf_dotp(mesh, psin(:, 1), psim(:, 1), reduce = .false.)
1509 dot_downdown = zmf_dotp(mesh, psin(:, 2), psim(:, 2), reduce = .false.)
1510 dot_updown = zmf_dotp(mesh, psin(:, 1), psim(:, 2), reduce = .false.)
1511 dot_downup = zmf_dotp(mesh, psin(:, 2), psim(:, 1), reduce = .false.)
1512
1513 spin(1, counter, ik) = dot_updown + dot_downup
1514 spin(2, counter, ik) = -m_zi * dot_updown + m_zi * dot_downup
1515 spin(3, counter, ik) = dot_upup - dot_downdown
1516 end do !ist
1517 end do
1518 end do
1519
1520 call profiling_in("W90_SPN_REDUCE")
1521 call mesh%allreduce(spin)
1522
1523 if(st%d%kpt%parallel) then
1524 call comm_allreduce(st%d%kpt%mpi_grp, spin)
1525 end if
1526 call profiling_out("W90_SPN_REDUCE")
1527
1528 ! write to W90 file
1529 if (st%system_grp%is_root()) then
1530 do ik = 1, w90_num_kpts
1531 counter = 0
1532 do jst = 1, st%nst
1533 if (exclude_list(jst)) cycle
1534
1535 do ist = 1, jst
1536 if (exclude_list(ist)) cycle
1537
1538 counter = counter + 1
1539 write(w90_spn, '(e18.10,2x,e18.10)') spin(1, counter, ik)
1540 write(w90_spn, '(e18.10,2x,e18.10)') spin(2, counter, ik)
1541 write(w90_spn, '(e18.10,2x,e18.10)') spin(3, counter, ik)
1542 end do
1543 end do
1544 end do !ik
1545 end if
1546
1547 call io_close(w90_spn)
1548
1549 safe_deallocate_a(psim)
1550 safe_deallocate_a(psin)
1551 safe_deallocate_a(spin)
1552
1553 call profiling_out("W90_SPN")
1554
1555 pop_sub(create_wannier90_spn)
1556 end subroutine create_wannier90_spn
1557
1558
1559 ! --------------------------------------------------------------------------
1560 subroutine generate_wannier_states(space, mesh, ions, st, kpoints)
1561 class(space_t), intent(in) :: space
1562 class(mesh_t), intent(in) :: mesh
1563 type(ions_t), intent(in) :: ions
1564 type(states_elec_t), intent(in) :: st
1565 type(kpoints_t), intent(in) :: kpoints
1566
1567 integer :: w90_u_mat, w90_u_dis, w90_xyz, nwann, nik, nib
1568 integer :: ik, iw, iw2, ip, ipmax, rankmax, idmmax, ib
1569 real(real64), allocatable :: centers(:,:), supercell_centers(:,:), new_centers(:,:)
1570 complex(real64), allocatable :: Umnk(:,:,:), Umat(:,:,:), Udis(:,:,:)
1571 complex(real64), allocatable :: zwn(:,:,:), psi(:,:), phase(:,:), phase_bloch(:), zwn_bloch(:,:,:)
1572 character(len=MAX_PATH_LEN) :: fname
1573 real(real64) :: kpoint(3), wmod, wmodmax, xx(space%dim)
1574 character(len=2) :: dum
1575 logical :: exist, have_disentangled
1576 type(unit_t) :: fn_unit
1577 complex(real64) :: scal
1578 type(block_t) :: blk
1579 integer :: supercell(space%dim), ii, jj, kk, ncols, Nreplica, irep, irepmax
1580 real(real64) :: min_image_displ(3), offset(space%dim)
1581 integer :: jw, ib_orig, nactive, j, nfixed
1582 integer, allocatable :: parent(:)
1583 real(real64), parameter :: tol_cluster = 0.25_real64 ! In Bohr
1584
1585 integer, allocatable :: active_rows(:)
1586 complex(real64), allocatable :: uwork(:,:)
1587 logical :: in_window
1588
1589
1590 push_sub(generate_wannier_states)
1591
1592 message(1) = "oct-wannier90: Constructing the Wannier states from the U matrix."
1593 call messages_info(1)
1594
1595 inquire(file=trim(trim(adjustl(w90_prefix))//'_centres.xyz'),exist=exist)
1596 if (.not. exist) then
1597 message(1) = 'oct-wannier90: Cannot find the Wannier90 file seedname_centres.xyz.'
1598 write(message(2),'(a)') 'Please run wannier90.x with "write_xyz=.true." in '// trim(adjustl(w90_prefix)) // '.'
1599 call messages_fatal(2)
1600 end if
1601
1602 w90_xyz = io_open(trim(trim(adjustl(w90_prefix))//'_centres.xyz'), global_namespace, action='read')
1603
1604 safe_allocate(centers(1:3, 1:w90_num_wann))
1605 !Skip two lines
1606 read(w90_xyz, *)
1607 read(w90_xyz, *)
1608 do iw = 1, w90_num_wann
1609 read(w90_xyz, *) dum, centers(1:3, iw)
1610 ! Wannier90 outputs the coordinates in angstrom
1611 centers(1:3, iw) = units_to_atomic(unit_angstrom, centers(1:3, iw))
1612 end do
1613 call io_close(w90_xyz)
1614
1615 ! In order to find "more consistent" Wannier centers, the centers are clustered in groups,
1616 ! Using periodic boundary condition to find the centers that are the closest
1617 safe_allocate(parent(1:w90_num_wann))
1618 do iw = 1, w90_num_wann
1619 parent(iw) = iw
1620 do jw = 1, iw-1
1621 min_image_displ(:) = ions%latt%cart_to_red(centers(:, iw) - centers(:, jw))
1622 min_image_displ(1:space%periodic_dim) = min_image_displ(1:space%periodic_dim) - nint(min_image_displ(1:space%periodic_dim))
1623 min_image_displ = ions%latt%red_to_cart(min_image_displ)
1624 if (norm2(min_image_displ) < tol_cluster) then
1625 parent(iw) = parent(jw)
1626 exit
1627 end if
1628 end do
1629 end do
1630
1631 message(1) = "Info : Clustering of the Wannier centers"
1632 call messages_info(1)
1633
1634 safe_allocate(new_centers(1:3, 1:w90_num_wann))
1635 do iw = 1, w90_num_wann
1636 write(message(1), '(a,i0,a,3(f7.3,a))') 'Info : Original Wannier center ', &
1637 iw, ' (', centers(1, iw), ', ', centers(2, iw), ', ', centers(3, iw), ')'
1638
1639 if (parent(iw) == iw) then
1640 ! We are computing |w_{n,0}>, so we need to take only a shift to get to the central cell
1641 new_centers(1:3, iw) = ions%latt%fold_into_cell(centers(1:3, iw))
1642 else
1643 ! This center has a closeby periodic parent. We then move it to be as close as possible to its "parent"
1644 ! Thanks to the loop above, this should always be inside of very close to a parent inside the unit cell
1645 min_image_displ(:) = ions%latt%cart_to_red(centers(:, iw) - new_centers(:, parent(iw)))
1646 min_image_displ(1:space%periodic_dim) = real(nint(min_image_displ(1:space%periodic_dim)), real64)
1647 min_image_displ = ions%latt%red_to_cart(min_image_displ)
1648 new_centers(1:3, iw) = centers(1:3, iw) - min_image_displ
1649 end if
1650
1651 write(message(2), '(a,i0,a,3(f7.3,a))') 'Info : New Wannier center ', &
1652 iw, ' (', new_centers(1, iw), ', ', new_centers(2, iw), ', ', new_centers(3, iw), ')'
1653 write(message(3), '(a)') ''
1654 call messages_info(3)
1655 end do
1656
1657 ! Getting the offset, for the correct phase factor
1658 centers = new_centers - centers
1659 safe_deallocate_a(parent)
1660 safe_deallocate_a(new_centers)
1661
1662 inquire(file=trim(trim(adjustl(w90_prefix))//'_u.mat'),exist=exist)
1663 if (.not. exist) then
1664 message(1) = 'oct-wannier90: Cannot find the Wannier90 seedname_u.mat file.'
1665 write(message(2),'(a)') 'Please run wannier90.x with "write_u_matrices=.true." in '// trim(adjustl(w90_prefix)) // '.'
1666 call messages_fatal(2)
1667 end if
1668 w90_u_mat = io_open(trim(trim(adjustl(w90_prefix))//'_u.mat'), global_namespace, action='read')
1669
1670 !Skip one line
1671 read(w90_u_mat, *)
1672 !Read num_kpts, num_wann, num_wann for consistency check
1673 read(w90_u_mat, *) nik, nwann, nwann
1674 if (nik /= w90_num_kpts .or. nwann /= w90_num_wann) then
1675 message(1) = "The file contains U matrices is inconsistent with the .win file."
1676 call messages_fatal(1)
1677 end if
1678
1679 ! Read u.mat into a temporary array first
1680 safe_allocate(umat(1:w90_num_wann, 1:w90_num_wann, 1:w90_num_kpts))
1681
1682 do ik = 1, w90_num_kpts
1683 !Skip one line
1684 read(w90_u_mat, *)
1685 !Skip one line (k-point coordinate)
1686 read(w90_u_mat, *)
1687 read(w90_u_mat, '(f15.10,sp,f15.10)') ((umat(iw, iw2, ik), iw=1, w90_num_wann), iw2=1, w90_num_wann)
1688 end do
1689
1690 call io_close(w90_u_mat)
1691
1692 inquire(file=trim(trim(adjustl(w90_prefix))//'_u_dis.mat'),exist=have_disentangled)
1693 if (have_disentangled) then
1694 message(1) = 'oct-wannier90: Found u_dis.mat, will include disentanglement in Wannier state construction.'
1695 call messages_info(1)
1696
1697 ! Read u_dis.mat: dimensions are (w90_num_bands x w90_num_wann) per k-point.
1698 ! The band rows follow the compressed, non-excluded band ordering used by
1699 ! the rest of the Wannier90 interface (i.e. after applying exclude_bands).
1700 w90_u_dis = io_open(trim(trim(adjustl(w90_prefix))//'_u_dis.mat'), global_namespace, action='read')
1701 !Skip one line
1702 read(w90_u_dis, *)
1703 !Read num_kpts, num_wann, num_bands for consistency check
1704 read(w90_u_dis, *) nik, nwann, nib
1705 if (nik /= w90_num_kpts .or. nwann /= w90_num_wann .or. nib /= w90_num_bands) then
1706 message(1) = 'oct-wannier90: The u_dis.mat file is inconsistent with the .win/.nnkp files.'
1707 call messages_fatal(1)
1708 end if
1709
1710 safe_allocate(udis(1:w90_num_bands, 1:w90_num_wann, 1:w90_num_kpts))
1711 do ik = 1, w90_num_kpts
1712 !Skip one line
1713 read(w90_u_dis, *)
1714 !Skip one line (k-point coordinate)
1715 read(w90_u_dis, *)
1716 read(w90_u_dis, '(f15.10,sp,f15.10)') ((udis(ib, iw, ik), ib=1, w90_num_bands), iw=1, w90_num_wann)
1717 end do
1718
1719 call io_close(w90_u_dis)
1720
1721 ! wannier90 v3.1.0 packs in-window bands at the top of U_dis ([B,C,D,0,0] if A and E are outside window)
1722 ! Here we reorder the rows to [0,B,C,D,0]
1723 if (w90_have_dis_win_min .or. w90_have_dis_win_max) then
1724 safe_allocate(active_rows(1:w90_num_bands))
1725 safe_allocate(uwork(1:w90_num_bands, 1:w90_num_wann))
1726
1727 nfixed = 0
1728 do ikpoint = 1, w90_num_kpts
1729 ik = st%d%get_k_spin_index(ikpoint, w90_spin_channel)
1730 nactive = 0
1731 do ib_orig = 1, st%nst
1732 if (exclude_list(ib_orig)) cycle
1733 in_window = .true.
1734 if (w90_have_dis_win_min) in_window = in_window .and. st%eigenval(ib_orig, ik) >= w90_dis_win_min
1735 if (w90_have_dis_win_max) in_window = in_window .and. st%eigenval(ib_orig, ik) <= w90_dis_win_max
1736 if (in_window) then
1737 nactive = nactive + 1
1738 active_rows(nactive) = band_index(ib_orig)
1739 end if
1740 end do
1741
1742 if (nactive > 0) then
1743 uwork = m_z0
1744 do j = 1, nactive
1745 uwork(active_rows(j), 1:w90_num_wann) = udis(j, 1:w90_num_wann, ikpoint)
1746 end do
1747 udis(1:w90_num_bands, 1:w90_num_wann, ikpoint) = uwork(1:w90_num_bands, 1:w90_num_wann)
1748 nfixed = nfixed + 1
1749 end if
1750 end do
1751
1752 if (nfixed > 0) then
1753 write(message(1), '(a,i0,a)') 'oct-wannier90: remapped legacy u_dis row layout for ', nfixed, ' k-points.'
1754 call messages_info(1)
1755 end if
1756
1757 safe_deallocate_a(active_rows)
1758 safe_deallocate_a(uwork)
1759 else
1760 message(1) = 'oct-wannier90: dis_win_min/dis_win_max not found in .win; skipping legacy u_dis remap.'
1761 call messages_warning(1)
1762 end if
1763
1764 ! Combined transformation: V(ib, iw, ik) = sum_j U_dis(ib, j, ik) * U(j, iw, ik)
1765 ! This maps from the num_bands non-excluded bands to num_wann Wannier functions.
1766 safe_allocate(umnk(1:w90_num_bands, 1:w90_num_wann, 1:w90_num_kpts))
1767 umnk = m_z0
1768 do ik = 1, w90_num_kpts
1769 umnk(:, :, ik) = umnk(:, :, ik) + matmul(udis(:, :, ik), umat(:, :, ik))
1770 end do
1771 safe_deallocate_a(udis)
1772 else
1773 ! No disentanglement: in this case w90_num_bands == w90_num_wann.
1774 safe_allocate(umnk(1:w90_num_wann, 1:w90_num_wann, 1:w90_num_kpts))
1775 umnk = umat
1776 end if
1777 safe_deallocate_a(umat)
1778
1779 !We read the output format for the Wannier states
1780 call parse_variable(global_namespace, 'OutputFormat', 0, how)
1781 if (how == 0) then
1782 message(1) = "OutputFormat must be specified for outputing Wannier functions."
1783 call messages_fatal(1)
1784 end if
1785
1786 !%Variable Wannier90Supercell
1787 !%Type block
1788 !%Section Utilities::oct-wannier90
1789 !%Description
1790 !% This block allows to specify the size of the supercell used to compute the Wannier functions
1791 !% If not specified, the code uses a default 1x1x1 cell, i.e., it plots the Wannier90 in the primitive cell.
1792 !%End
1793 if (parse_is_defined(sys%namespace, 'Wannier90Supercell')) then
1794 if (parse_block(sys%namespace, 'Wannier90Supercell', blk) == 0) then
1795 ncols = parse_block_cols(blk, 0)
1796 if (ncols /= space%dim) then
1797 write(message(1),'(a,i3,a,i3)') 'Wannier90Supercell has ', ncols, ' columns but must have ', sys%space%dim
1798 call messages_fatal(1, namespace=sys%namespace)
1799 end if
1800 do ii = 1, space%dim
1801 call parse_block_integer(blk, 0, ii - 1, supercell(ii))
1802 end do
1803
1804 call parse_block_end(blk)
1805 end if
1806 else
1807 supercell = 1
1808 end if
1809
1810 nreplica = product(supercell)
1811
1812 ! The center of each replica of the unit cell
1813 safe_allocate(supercell_centers(1:space%dim, 1:nreplica))
1814 offset(1:space%dim) = -floor((real(supercell(1:space%dim), real64) - m_one) / m_two)
1815 irep = 1
1816 do ii = 0, supercell(1)-1
1817 do jj = 0, supercell(2)-1
1818 do kk = 0, supercell(3)-1
1819 supercell_centers(:, irep) = ions%latt%red_to_cart(offset &
1820 + [real(ii, real64), real(jj, real64), real(kk, real64)])
1821 irep = irep + 1
1822 end do
1823 end do
1824 end do
1825
1826
1827
1828 call io_mkdir('wannier', global_namespace)
1829
1830 !Computing the Wannier states in the primitive cell, from the U matrices
1831 safe_allocate(zwn(1:mesh%np, 1:nreplica, 1:st%d%dim))
1832 safe_allocate(psi(1:mesh%np, 1:st%d%dim))
1833 safe_allocate(phase(1:mesh%np, 1:nreplica))
1834 safe_allocate(phase_bloch(1:mesh%np))
1835 if (w90_bloch_sums) then
1836 safe_allocate(zwn_bloch(1:mesh%np, 1:st%d%dim, st%d%kpt%start:st%d%kpt%end))
1837 end if
1838
1839 do iw = 1, w90_num_wann
1840
1841 zwn(:, :, :) = m_z0
1842
1843 do ik = 1, w90_num_kpts
1844
1845 if (.not. (ik >= st%d%kpt%start .and. ik <= st%d%kpt%end)) cycle
1846
1847 if (w90_bloch_sums) zwn_bloch(:,:,ik) = m_z0
1848
1849 kpoint(1:space%dim) = kpoints%get_point(ik, absolute_coordinates=.true.)
1850
1851 ! We compute the Wannier orbital on a grid centered around the Wannier function
1852 ! The minus sign is here is for the wrong convention of Octopus
1853 do irep = 1, nreplica
1854 !$omp parallel do
1855 do ip = 1, mesh%np
1856 xx = mesh%x(1:space%dim, ip)-centers(1:space%dim, iw) + supercell_centers(1:space%dim, irep)
1857 phase(ip, irep) = exp(-m_zi* sum( xx * kpoint(1:space%dim)))
1858 end do
1859 end do
1860 if (w90_bloch_sums) then
1861 do ip = 1, mesh%np
1862 xx = mesh%x(1:space%dim, ip)
1863 phase_bloch(ip) = exp(-m_zi* sum( xx * kpoint(1:space%dim)))
1864 end do
1865 end if
1866
1867 do iw2 = 1, st%nst
1868 if (exclude_list(iw2)) cycle
1869
1870 if (st%d%ispin /= spin_polarized) then
1871 call states_elec_get_state(st, mesh, iw2, ik, psi)
1872 else
1873 call states_elec_get_state(st, mesh, iw2, (ik-1)*2+w90_spin_channel, psi)
1874 end if
1875
1876 !$omp parallel
1877 do idim = 1, st%d%dim
1878 do irep = 1, nreplica
1879 !$omp do
1880 do ip = 1, mesh%np
1881 zwn(ip, irep, idim) = zwn(ip, irep, idim) + umnk(band_index(iw2), iw, ik) * psi(ip, idim) * phase(ip, irep)
1882 end do
1883 end do
1884 end do
1885 !$omp end parallel
1886
1887 ! See Eq. 19 in arXiv:0605539
1888 if (w90_bloch_sums) then
1889 !$omp parallel
1890 do idim = 1, st%d%dim
1891 !$omp do
1892 do ip = 1, mesh%np
1893 zwn_bloch(ip, idim, ik) = zwn_bloch(ip, idim, ik) &
1894 + umnk(band_index(iw2), iw, ik) * psi(ip, idim) * phase_bloch(ip)
1895 end do
1896 end do
1897 !$omp end parallel
1898 end if
1899 end do!iw2
1900 end do!ik
1901
1902 if(st%d%kpt%parallel) then
1903 call comm_allreduce(st%d%kpt%mpi_grp, zwn)
1904 end if
1905
1906 ! Following what Wannier90 is doing, we fix the global phase by setting the max to be real
1907 ! We also normalize to the number of k-point at this step
1908 if (sys%st%d%ispin /= spinors) then
1909 ipmax = 0
1910 irepmax = 0
1911 wmodmax = m_zero
1912 do irep = 1, nreplica
1913 do ip = 1, mesh%np
1914 wmod = real(zwn(ip, irep, 1)*conjg(zwn(ip, irep, 1)), real64)
1915 if (wmod > wmodmax) then
1916 ipmax = ip
1917 irepmax = irep
1918 wmodmax = wmod
1919 end if
1920 end do
1921 end do
1922 scal = sqrt(wmodmax)/zwn(ipmax, irepmax, 1)/w90_num_kpts
1923 call mesh_minmaxloc(mesh, wmodmax, rankmax, mpi_maxloc)
1924 call mesh%mpi_grp%bcast(scal, 1, mpi_double_complex, rankmax)
1925 call lalg_scal(mesh%np, nreplica, scal, zwn(:,:,1))
1926
1927 if (w90_bloch_sums) then
1928 do ik = st%d%kpt%start, st%d%kpt%end
1929 ipmax = 0
1930 idmmax= 0
1931 wmodmax = m_zero
1932 do idim = 1, st%d%dim
1933 do ip = 1, mesh%np
1934 wmod = real(zwn_bloch(ip, idim, ik)*conjg(zwn_bloch(ip, idim, ik)), real64)
1935 if (wmod > wmodmax) then
1936 ipmax = ip
1937 wmodmax = wmod
1938 idmmax = idim
1939 end if
1940 end do
1941 end do
1942 scal = sqrt(wmodmax)/zwn_bloch(ipmax, idmmax, ik)/w90_num_kpts
1943 call mesh_minmaxloc(mesh, wmodmax, rankmax, mpi_maxloc)
1944 call mesh%mpi_grp%bcast(scal, 1, mpi_double_complex, rankmax)
1945 call lalg_scal(mesh%np, st%d%dim, scal, zwn_bloch(:,:,ik))
1946 end do
1947 end if
1948 end if
1949
1950 ! Output the Wannier states
1951 fn_unit = sqrt(units_out%length**(-space%dim))
1952 do idim = 1, st%d%dim
1953 if (st%d%ispin == spinors) then
1954 write(fname, '(a,i3.3,a4,i1)') 'wannier-', iw, '-isp', idim
1955 else
1956 write(fname, '(a,i3.3,a4,i1)') 'wannier-', iw
1957 end if
1958 if (any(supercell_centers > 1)) then
1959 if (mesh%parallel_in_domains) then
1960 call messages_not_implemented("w90 generate supercell larger than 1x1x1 with domain parallelization")
1961 end if
1962 call io_function_output_supercell(how, "wannier", trim(fname), mesh, &
1963 space, ions%latt, zwn(:, :, idim), supercell_centers, supercell, fn_unit, ierr, global_namespace, &
1964 pos=ions%pos, atoms=ions%atom, grp = st%dom_st_kpt_mpi_grp)
1965 else
1966 call zio_function_output(how, "wannier", trim(fname), global_namespace, space, mesh, &
1967 zwn(:, 1, idim), fn_unit, ierr, pos=ions%pos, atoms=ions%atom, grp = st%dom_st_kpt_mpi_grp)
1968 end if
1969 end do
1970
1971 ! Output Bloch sums of Wannier states
1972 if (w90_bloch_sums) then
1973 do ik = st%d%kpt%start, st%d%kpt%end
1974 do idim = 1, st%d%dim
1975 if (st%d%ispin == spinors) then
1976 write(fname, '(a,i3.3,a4,i1,a3,i5.5)') 'wannier_bloch-', iw, '-isp', idim, '-ik', ik
1977 else
1978 write(fname, '(a,i3.3,a4,i1,a3,i5.5)') 'wannier_bloch-', iw, '-ik', ik
1979 end if
1980 call zio_function_output(how, "wannier", trim(fname), global_namespace, space, mesh, &
1981 zwn_bloch(:, idim, ik), fn_unit, ierr, pos=ions%pos, atoms=ions%atom)
1982 end do
1983 end do
1984 end if
1985
1986 ! Checking the ratio imag/real
1987 if (sys%st%d%ispin /= spinors) then
1988 wmodmax = m_zero
1989 do irep = 1, nreplica
1990 do ip = 1, mesh%np
1991 if(abs(real(zwn(ip, irep, 1), real64)) >= 1e-2_real64) then
1992 wmodmax = max(wmodmax, abs(aimag(zwn(ip, irep, 1)))/abs(real(zwn(ip, irep, 1), real64)))
1993 end if
1994 end do
1995 end do
1996 call mesh_minmaxloc(mesh, wmodmax, rankmax, mpi_maxloc)
1997
1998 write(message(1), '(a,i4,a,f11.6)') 'oct-wannier90: Wannier function ', iw, ' Max. Im/Re Ratio = ', wmodmax
1999 call messages_info(1)
2000 else
2001 write(message(1), '(a,i4)') 'oct-wannier90: Wannier function done ', iw
2002 call messages_info(1)
2003 end if
2004 end do
2005
2006 safe_deallocate_a(umnk)
2007 safe_deallocate_a(zwn)
2008 safe_deallocate_a(psi)
2009 safe_deallocate_a(phase)
2010 safe_deallocate_a(phase_bloch)
2011 safe_deallocate_a(centers)
2012 safe_deallocate_a(supercell_centers)
2013 safe_deallocate_a(umat)
2014 safe_deallocate_a(udis)
2015
2017 end subroutine generate_wannier_states
2018
2019 subroutine choose_scdm_smearing_function(st, act_bands, num_kpts, num_bands, spin_channel, scdm_entanglement, mu, sigma, smeared)
2020 type(states_elec_t), intent(in) :: st
2021 integer, intent(in) :: act_bands(:)
2022 integer, intent(in) :: num_kpts, num_bands
2023 integer, intent(in) :: spin_channel
2024 integer, intent(in) :: scdm_entanglement
2025 real(real64), intent(in) :: mu, sigma
2026 real(real64), intent(out) :: smeared(:,:)
2027
2028 integer :: ik, ist, ikpoint
2029
2030 select case (scdm_entanglement)
2031 case (option__scdmentanglement__scdm_isolated)
2032 do ikpoint = 1, num_kpts
2033 ik = st%d%get_k_spin_index(ikpoint, spin_channel)
2034 smeared(:, ikpoint) = m_zero
2035 do ist = 1, num_bands
2036 if (st%eigenval(act_bands(ist), ik) .le. mu) then
2037 smeared(ist, ikpoint)=m_one
2038 end if
2039 end do
2040 end do
2041
2042 case (option__scdmentanglement__scdm_erfc)
2043 do ikpoint = 1, num_kpts
2044 ik = st%d%get_k_spin_index(ikpoint, spin_channel)
2045 do ist = 1, num_bands
2046 smeared(ist, ikpoint)=m_half*erfc((st%eigenval(act_bands(ist), ik)-mu) / sigma)
2047 end do
2048 end do
2049
2050 case (option__scdmentanglement__scdm_gaussian)
2051 do ikpoint = 1, num_kpts
2052 ik = st%d%get_k_spin_index(ikpoint, spin_channel)
2053 do ist = 1, num_bands
2054 smeared(ist, ikpoint)=exp(-(st%eigenval(act_bands(ist), ik)-mu)**2 / sigma**2)
2055 end do
2056 end do
2057
2058 case default
2059 message(1) = "SCDMEntanglement is set to an unsupported value."
2060 call messages_fatal(1)
2061 end select
2062
2063 end subroutine choose_scdm_smearing_function
2064
2065end program wannier90_interface
2066
2067!! Local Variables:
2068!! mode: f90
2069!! coding: utf-8
2070!! End:
double log(double __x) __attribute__((__nothrow__
double exp(double __x) __attribute__((__nothrow__
double sqrt(double __x) __attribute__((__nothrow__
double floor(double __x) __attribute__((__nothrow__
This module implements batches of mesh functions.
Definition: batch.F90:135
This module handles the calculation mode.
type(calc_mode_par_t), public calc_mode_par
Singleton instance of parallel calculation mode.
integer, parameter, public p_strategy_states
parallelization in states
subroutine, public zcube_function_free_rs(cube, cf)
Deallocates the real space grid.
subroutine, public zcube_function_alloc_rs(cube, cf, in_device, force_alloc)
Allocates locally the real space grid, if PFFT library is not used. Otherwise, it assigns the PFFT re...
subroutine, public cube_end(cube)
Definition: cube.F90:399
subroutine, public cube_init(cube, nn, namespace, space, spacing, coord_system, fft_type, fft_library, dont_optimize, nn_out, mpi_grp, need_partition, tp_enlarge, blocksize, batch_size)
Definition: cube.F90:206
subroutine, public cube_init_cube_map(cube, mesh)
Definition: cube.F90:876
integer, parameter, public unpolarized
Parameters...
integer, parameter, public spinors
integer, parameter, public spin_polarized
Fast Fourier Transform module. This module provides a single interface that works with different FFT ...
Definition: fft.F90:120
subroutine, public fft_all_init(namespace)
initialize the table
Definition: fft.F90:276
subroutine, public fft_all_end()
delete all plans
Definition: fft.F90:391
real(real64), parameter, public m_two
Definition: global.F90:202
subroutine, public global_end()
Finalise parser varinfo file, and MPI.
Definition: global.F90:495
real(real64), parameter, public m_huge
Definition: global.F90:218
real(real64), parameter, public m_zero
Definition: global.F90:200
complex(real64), parameter, public m_z0
Definition: global.F90:210
complex(real64), parameter, public m_zi
Definition: global.F90:214
subroutine, public global_init(communicator)
Initialise Octopus.
Definition: global.F90:376
real(real64), parameter, public m_one
Definition: global.F90:201
This module implements the underlying real-space grid.
Definition: grid.F90:119
Definition: io.F90:116
subroutine, public io_init(defaults)
If the argument defaults is present and set to true, then the routine will not try to read anything f...
Definition: io.F90:165
subroutine, public io_close(iunit, grp)
Definition: io.F90:467
subroutine, public io_end()
Definition: io.F90:271
integer function, public io_open(file, namespace, action, status, form, position, die, recl, grp)
Definition: io.F90:402
logical pure function, public kpoints_point_is_gamma(this, ik)
Definition: kpoints.F90:1720
subroutine, public kpoints_to_absolute(latt, kin, kout)
Definition: kpoints.F90:1137
System information (time, memory, sysname)
Definition: loct.F90:117
This module defines functions over batches of mesh functions.
Definition: mesh_batch.F90:118
subroutine, public zmesh_batch_mf_dotp(mesh, aa, psi, dot, reduce, nst)
calculate the dot products between a batch and a vector of mesh functions
This module defines various routines, operating on mesh functions.
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:120
subroutine, public messages_end()
Definition: messages.F90:273
subroutine, public messages_not_implemented(feature, namespace)
Definition: messages.F90:1068
subroutine, public messages_init(output_dir)
Definition: messages.F90:220
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:525
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:162
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:410
subroutine, public messages_experimental(name, namespace)
Definition: messages.F90:1040
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:594
type(mpi_grp_t), public mpi_world
Definition: mpi.F90:272
This module handles the communicators for the various parallelization strategies.
Definition: multicomm.F90:147
type(namespace_t), public global_namespace
Definition: namespace.F90:135
subroutine, public orbitalset_init(this)
Definition: orbitalset.F90:209
subroutine, public orbitalset_end(this)
Definition: orbitalset.F90:235
subroutine, public orbitalset_update_phase(os, dim, kpt, kpoints, spin_polarized, vec_pot, vec_pot_var, kpt_max)
Build the phase correction to the global phase in case the orbital crosses the border of the simulato...
Definition: orbitalset.F90:285
Some general things and nomenclature:
Definition: par_vec.F90:173
logical function, public parse_is_defined(namespace, name)
Definition: parser.F90:463
subroutine, public parser_init()
Initialise the Octopus parser.
Definition: parser.F90:410
subroutine, public parser_end()
End the Octopus parser.
Definition: parser.F90:442
subroutine, public profiling_end(namespace)
Definition: profiling.F90:415
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:631
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:554
subroutine, public profiling_init(namespace)
Create profiling subdirectory.
Definition: profiling.F90:257
integer, parameter, public restart_gs
Definition: restart.F90:156
integer, parameter, public restart_td
Definition: restart.F90:156
integer, parameter, public restart_type_load
Definition: restart.F90:184
subroutine, public zstates_elec_rrqr_decomposition(st, namespace, mesh, act_bands, ik, jpvt)
Perform RRQR on the transpose states stored in the states object and return the pivot vector.
This module defines routines to write information about states.
logical function, public state_kpt_is_local(st, ist, ik)
check whether a given state (ist, ik) is on the local node
subroutine, public states_elec_allocate_wfns(st, mesh, wfs_type, skip, packed)
Allocates the KS wavefunctions defined within a states_elec_t structure.
subroutine, public states_elec_look(restart, nik, dim, nst, ierr)
Reads the 'states' file in the restart directory, and finds out the nik, dim, and nst contained in it...
This module handles reading and writing restart information for the states_elec_t.
subroutine, public states_elec_load(restart, namespace, space, st, mesh, kpoints, fixed_occ, ierr, iter, lr, lowest_missing, label, verbose, skip)
returns in ierr: <0 => Fatal error, or nothing read =0 => read all wavefunctions >0 => could only rea...
subroutine, public submesh_init(this, space, mesh, latt, center, rc)
Definition: submesh.F90:226
type(type_t), parameter, public type_cmplx
Definition: types.F90:136
brief This module defines the class unit_t which is used by the unit_systems_oct_m module.
Definition: unit.F90:134
This module defines the unit system, used for input and output.
type(unit_t), public unit_angstrom
For XYZ files.
subroutine, public unit_system_init(namespace)
type(unit_t), public unit_ev
For output energies in eV.
This module is intended to contain simple general-purpose utility functions and procedures.
Definition: utils.F90:120
subroutine, public ylm_wannier(ylm, l, mr, rr, xx, nr)
Class describing the electron system.
Definition: electrons.F90:222
Describes mesh distribution to nodes.
Definition: mesh.F90:187
The states_elec_t class contains all electronic wave functions.
batches of electronic states
Definition: wfs_elec.F90:141
int true(void)
subroutine create_wannier90_spn(mesh, st)
Write the spn file containing .
subroutine create_wannier90_eig()
subroutine read_wannier90_files()
subroutine write_unk(space, mesh, st, formatted)
subroutine create_wannier90_mmn(mesh, st)
subroutine wannier90_setup(ions, kpoints, space)
subroutine generate_wannier_states(space, mesh, ions, st, kpoints)
program wannier90_interface
subroutine create_wannier90_amn(space, mesh, latt, st, kpoints)
subroutine choose_scdm_smearing_function(st, act_bands, num_kpts, num_bands, spin_channel, scdm_entanglement, mu, sigma, smeared)
subroutine wannier90_output()