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